[R] EXTRACT POINT DATA FROM NETCDF FILE

Ben Tupper btupper at bigelow.org
Fri Jan 15 23:30:57 CET 2016


Hello Peter,

It's not possible to help you resolve your difficulties with what you have shared. How we know what is mean to 'have no luck'?  What you need to do is provide a reproducible example - something anyone with R might be able to replicate.  Here is some terrific advice on how to make it easy for people to help you.

http://adv-r.had.co.nz/Reproducibility.html

In addition to the above, can you type at the R command line this...

> inp_file

And share the complete output.

Ben

> On Jan 15, 2016, at 9:16 AM, Peter Tuju <peterenos at ymail.com> wrote:
> 
> Daer R, usersI want to get a point data from netcdf file using the longitude and latitude, 
> but have no luck with the code I'm using. Here is the code. Please help!
> 
> rm( list = ls() )                                               # Clearing the workspace
> setwd( "/run/media/tuju/0767090047/extract_wrf_txt_file" )
> library( ncdf4 )                                       # Loading the ncdf4 package to read the netcdf data
> library(ncdf)
> inp_file <- open.ncdf( "wrfout_d01_2015-12-30.nc" )      # Reading the netcdf data
> time <- get.var.ncdf( inp_file, "Times" )                 # Extracting the forecasts time
> 
> sites <- read.csv("Station_Coordinates_TMA.csv", sep = "\t")
> attach(sites)
> 
> source( "whereis.R" )
> lat = get.var.ncdf(inp_file, "XLAT")  
> lon = get.var.ncdf(inp_file, "XLONG")
> 
> lower_left_lon_lat = c( 22, -16 )
> upper_right_lon_lat = c( 56, 7 )
> 
> ix0 = wherenearest( lower_left_lon_lat[1],  lon )
> ix1 = wherenearest( upper_right_lon_lat[1], lon )
> iy0 = wherenearest( lower_left_lon_lat[2],  lat )
> iy1 = wherenearest( upper_right_lon_lat[2], lat )
> 
> countx = ix1 - ix0 + 1
> county = iy1 - iy0 + 1
> 
> rainc = get.var.ncdf( inp_file, "RAINC", start = c( ix0, iy0, 1 ), count = c( countx, county, 1 ))
>  _____________
> Peter  E. Tuju
> Dar es Salaam
> T A N Z A N I A
> ----------------------

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org



More information about the R-help mailing list