Hi Lily:

> On Jul 26, 2016, at 2:00 PM, lily li <chocol...@gmail.com> wrote:
> 
> Here are the results. Yes, I tried to read netcdf files, but cannot grasp the 
> contents. Thanks for helping out.
> 
> > str(pre1)

A guide to netcdf files and R can be found at 
https://www.image.ucar.edu/GSP/Software/Netcdf/, it would be worth your time to 
read through it.  Not certain why you want .csv files,  but I can give you a 
rough idea of how to read a slice into a variable in R,  it is then up to you 
to save the result as a .csv.

netcdf files have parameters, precipitation in this case, defined on a grid, in 
your case (lon, lat, time).  The file contains dimensions,  which gives the 
number of points in each dimension of the grid, and coordinate variables,  
which give the values of that coordinate variable at each grid point.  So if I 
wanted to to subset at a certain lat, lon - I would first need to get those 
values

> lat <- ncvar_get(pre1, "lat")
> lon <- ncvar_get(pre1, "lon")
> 

I have no idea of contents of your file, but let's say you know there is a lat 
of 30, and a lon of 120, so I would have to find where they occur:

> myLat <- which(lat == 30)
> myLon <- which(lon == 120)

then you get the slice at that lat-lon for all time by telling ncvar_get where 
to start on the grid, and how many steps to take:

> myPrecip <- ncvar_get(pre1,"precipitation", start = c(myLat, myLon,1), count 
> = c(1, 1, -1))

where the -1 value tells it to get all the values along the time dimension.  
You now have that slice of the data stored in myPrecip.  I hope this gets you 
started, but i strongly urge you to read through the link above.

-Roy

> 
> On Tue, Jul 26, 2016 at 2:52 PM, Roy Mendelssohn - NOAA Federal 
> <roy.mendelss...@noaa.gov> wrote:
> Hi Lily:
> 
> I doubt the mail-list would pass through the netcdf file. Instead, could you 
> do the following, and post the results:
> 
> library(ncdf4
> pre1 = nc_open('sample_precip_daily.nc')
> str(pre1)
> nc_close(pre1)
> 
> I have a feeling you haven't worked much with netcdf files. I will try to 
> find a tutorial also to help you along.
> 
> Thanks,
> 
> -Roy
> 
> > On Jul 26, 2016, at 12:07 PM, lily li <chocol...@gmail.com> wrote:
> >
> > Thanks for your reply. But it says "Error in (function (classes, fdef,
> > mtable)):
> > unable to find an inherited method for function 'brick' for signature
> > 'ncdf4' "
> >
> > The dataset is attached. It contains daily precipitation data for 20 years,
> > within a rectangle, so that there are several grid points. I use the code
> > to open it, but don't know how to get csv files, while each file contains
> > continuous daily precipitation data for each grid cell.
> > pre1 = nc_open('sample_precip_daily.nc')
> > pre1
> > pre1_rd = ncvar_get(pre1, 'precipitation')
> > nc_close(pre1)
> >
> > Thanks for your help.
> >
> > On Tue, Jul 26, 2016 at 4:08 AM, Jon Skoien <jon.sko...@jrc.ec.europa.eu>
> > wrote:
> >
> >> You could try with the brick function from the raster package.
> >>
> >> bvar = brick(netcdfName)
> >>
> >> This uses the ncdf4 functions for opening and reading the netcdf, but
> >> makes it easier to extract data for each day:
> >>
> >> p1 = rasterToPoints(bvar[[1]])
> >> and write p1 to csv.
> >>
> >> Best,
> >> Jon
> >>
> >>
> >>
> >> On 7/26/2016 6:54 AM, lily li wrote:
> >>
> >>> Hi all,
> >>>
> >>> I have a problem in opening netcdf files. If one netcdf file contains
> >>> longitude, latitude, and daily precipitation. How to relate each
> >>> precipitation record to its associated location, and export them as csv
> >>> files? Thanks.
> >>>
> >>> I just use nc_open(), ncvar_get(), but it is not very helpful. Thanks for
> >>> any ideas.
> >>>
> >>>        [[alternative HTML version deleted]]
> >>>
> >>> ______________________________________________
> >>> R-help@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.
> >>>
> >>>
> >> --
> >> Jon Olav Skøien
> >> Joint Research Centre - European Commission
> >> Institute for Space, Security & Migration
> >> Disaster Risk Management Unit
> >>
> >> Via E. Fermi 2749, TP 122,  I-21027 Ispra (VA), ITALY
> >>
> >> jon.sko...@jrc.ec.europa.eu
> >> Tel:  +39 0332 789205
> >>
> >> Disclaimer: Views expressed in this email are those of the individual and
> >> do not necessarily represent official views of the European Commission.
> >>
> > ______________________________________________
> > R-help@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.
> 
> **********************
> "The contents of this message do not reflect any position of the U.S. 
> Government or NOAA."
> **********************
> Roy Mendelssohn
> Supervisory Operations Research Analyst
> NOAA/NMFS
> Environmental Research Division
> Southwest Fisheries Science Center
> ***Note new address and phone***
> 110 Shaffer Road
> Santa Cruz, CA 95060
> Phone: (831)-420-3666
> Fax: (831) 420-3980
> e-mail: roy.mendelss...@noaa.gov www: http://www.pfeg.noaa.gov/
> 
> "Old age and treachery will overcome youth and skill."
> "From those who have been given much, much will be expected"
> "the arc of the moral universe is long, but it bends toward justice" -MLK Jr.
> 
> 

**********************
"The contents of this message do not reflect any position of the U.S. 
Government or NOAA."
**********************
Roy Mendelssohn
Supervisory Operations Research Analyst
NOAA/NMFS
Environmental Research Division
Southwest Fisheries Science Center
***Note new address and phone***
110 Shaffer Road
Santa Cruz, CA 95060
Phone: (831)-420-3666
Fax: (831) 420-3980
e-mail: roy.mendelss...@noaa.gov www: http://www.pfeg.noaa.gov/

"Old age and treachery will overcome youth and skill."
"From those who have been given much, much will be expected" 
"the arc of the moral universe is long, but it bends toward justice" -MLK Jr.

______________________________________________
R-help@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.

Reply via email to