[R] Extracting point data using longitude and latitude from netcdf file using R

Peter Tuju peterenos at ymail.com
Sat Jan 9 12:28:00 CET 2016


I have data file in netcdf with three dimensions (x, y, t) and I want to extract a variable RAINC and RAINNC 
using longitude and latitude for a single point location with all the time, but no lucky. The syntax is as follows;;
setwd( "/run/media/tuju/0767090047/extract_wrf_txt_file" )
rm( list = ls() )                                              
library( ncdf4 )                                     
inp_file <- nc_open( "wrfout_d01_2016-01-07.nc" )time <- ncvar_get( inp_file, "Times" )                      # Reading the time variabledar_lon <- 39.2dar_lat <- -6.866667
RAINC <- ncvar_get( inp_file, varid = "RAINC", start  = c( dar_lon, dar_lat, 1 ), count = c( 1, 1, -1 ) )
RAINNC <- ncvar_get( inp_file, varid = "RAINNC", start  =  c( dar_lon, dar_lat, 1 ), count = c( 1, 1, -1 ) )
RAIN <- RAINC + RAINNCRAIN_TABLE <- cbind( time, RAIN )
write.table( RAIN_TABLE, "Dar_es_Salaam.txt", row.names = FALSE, 
             col.names = c( "Valid Forecast Time",  "Rain (mm)", sep = "\t " )

# But no lucky with the red bolded syntax as I end up with the following error message> RAINC <- ncvar_get( inp_file, varid = "RAINC", start  = c( Lon[2], Lat[2], 1 ), count = c( 1, 1, -1 ) )
Error in Rsx_nc4_get_vara_double: NetCDF: Index exceeds dimension bound
Var: RAINC  Ndims: 3   Start: 0,4294967289,38 Count: 17,1,1
Error in ncvar_get_inner(ncid2use, varid2use, nc$var[[li]]$missval, addOffset,  : 
  C function R_nc4_get_vara_double returned error
However when I cahnge the latitude to postive it works fine. Note latitudes in the file data ranges from -16.71505 to 7.787529 as shown below;
head(ncvar_get(inp_file, "XLAT"))
[1] -16.71505 -16.71505 -16.71505 -16.71505 -16.71505 -16.71505
> tail(ncvar_get(inp_file, "XLAT"))
[1] 7.787529 7.787529 7.787529 7.787529 7.787529 7.787529
## So, how can I get the syntax correct? Please help _____________
Peter  E. Tuju
Dar es Salaam
T A N Z A N I A
----------------------




  
	[[alternative HTML version deleted]]



More information about the R-help mailing list