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

Anthoni, Peter (IMK) peter.anthoni at kit.edu
Sat Jan 9 13:14:43 CET 2016


Hi Peter,

the start in nc_varget requires a latitude and longitude index, not the latitude and longitude in double format.
So you need to figure out what index your latitude and longitude correspond to, which will depends on what data are in your netCDF.

it might have looked like that it worked for a positive latitude, but you got the data from the latitude index 6 or 7, depends on how the double was transformed into an integer.

best regards
Peter

> On 09 Jan 2016, at 12:28, Peter Tuju via R-help <r-help at r-project.org> wrote:
> 
> 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]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list