Grokbase Groups R r-help April 2012
FAQ

[R] how to read netcdf file in R

Yogesh Tiwari
Apr 2, 2012 at 8:54 am
Dear R Users,

I am using R 2.14.1 on windows

How to read netcdf files in R ?
Which packeges do we need to install for this,
and what commands are used for reading netcdf files.

Thanks,

Best Regards,
Yogesh Tiwari



--
Yogesh K. Tiwari (Dr.rer.nat),
Scientist,
Centre for Climate Change Research,
Indian Institute of Tropical Meteorology,
Homi Bhabha Road,
Pashan,
Pune-411008
INDIA

Phone: 0091-99 2273 9513 (Cell)
: 0091-20-25904452 (O)
Fax : 0091-20-258 93 825
reply

Search Discussions

4 responses

  • Adrienne Wootten at Apr 2, 2012 at 1:02 pm
    Yogesh,

    The package I most commonly use for reading and working with netcdfs
    is called ncdf. It has a collection of commands for working with
    netcdf files, but the open command is open.ncdf in this package

    Adrienne

    --
    Adrienne Wootten
    Graduate Research Assistant
    State Climate Office of North Carolina
    Department of Marine, Earth and Atmospheric Sciences
    North Carolina State University
    On Mon, Apr 2, 2012 at 4:54 AM, Yogesh Tiwari wrote:
    Dear R Users,

    I am using R 2.14.1 on windows

    How ?to read netcdf files in R ?
    Which packeges do we need to install for this,
    and what commands are used for reading netcdf files.

    Thanks,

    Best Regards,
    Yogesh Tiwari



    --
    Yogesh K. Tiwari (Dr.rer.nat),
    Scientist,
    Centre for Climate Change Research,
    Indian Institute of Tropical Meteorology,
    Homi Bhabha Road,
    Pashan,
    Pune-411008
    INDIA

    Phone: 0091-99 2273 9513 (Cell)
    ? ? ? ? : 0091-20-25904452 (O)
    Fax ? ?: 0091-20-258 93 825

    ? ? ? ?[[alternative HTML version deleted]]

    ______________________________________________
    R-help at r-project.org mailing list
    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.
  • David William Pierce at Apr 2, 2012 at 3:50 pm

    On Mon, Apr 2, 2012 at 1:54 AM, Yogesh Tiwari wrote:

    How ?to read netcdf files in R ?
    Which packeges do we need to install for this,
    and what commands are used for reading netcdf files.
    Typical code would look something like this:

    library('ncdf'')

    filename <- 'data.nc'
    varname <- 'Temperature'

    ncid <- open.ncdf( filename )
    data <- get.var.ncdf( ncid, varname )

    If you need to see what variables the file contains to get the
    variable names, just do a "print(ncid)" after you open it.

    If it is a very large file, too large for the data to fit in memory
    all at once, you can step through and read each timestep in the file
    separately using the "start=" and "count=" arguments to the
    get.var.ncdf() call.

    Regards,

    --Dave

    --
    David W. Pierce
    Division of Climate, Atmospheric Science, and Physical Oceanography
    Scripps Institution of Oceanography, La Jolla, California, USA
    (858) 534-8276 (voice) ?/ ?(858) 534-8561 (fax) ? ?dpierce at ucsd.edu
  • Yogesh Tiwari at Apr 13, 2012 at 7:09 am
    Dear David,

    Thanks,

    I could read and open .nc file in R, but now how to plot a simple filled
    color.
    Data and variables of .nc after reading in R are as below. Data have time
    steps of 99 (monthly mean data for 8 years).
    If I want to fill / plot co2 variable at time=1 then how to do it. Also,
    how can select lat lon
    over a particular region. For example, fill co2 [lat=0:40, lon=50:90,
    time=1] and then overplot a continental map.
    how to write this fill command in R,
    Thanks, Yogesh
    library(ncdf)
    Warning message:
    package �ncdf� was built under R version 2.14.2
    file.nc=open.ncdf("AIRS_2002_10_added-all-years.nc")
    print(file.nc)
    [1] "file AIRS_2002_10_added-all-years.nc has 3 dimensions:"
    [1] "time Size: 99"
    [1] "lat Size: 91"
    [1] "lon Size: 144"
    [1] "------------------------"
    [1] "file AIRS_2002_10_added-all-years.nc has 1 variables:"
    [1] "float co2[lon,lat,time] Longname:co2 Missval:1e+30"

    On Mon, Apr 2, 2012 at 9:20 PM, David William Pierce wrote:
    On Mon, Apr 2, 2012 at 1:54 AM, Yogesh Tiwari wrote:

    How to read netcdf files in R ?
    Which packeges do we need to install for this,
    and what commands are used for reading netcdf files.
    Typical code would look something like this:

    library('ncdf'')

    filename <- 'data.nc'
    varname <- 'Temperature'

    ncid <- open.ncdf( filename )
    data <- get.var.ncdf( ncid, varname )

    If you need to see what variables the file contains to get the
    variable names, just do a "print(ncid)" after you open it.

    If it is a very large file, too large for the data to fit in memory
    all at once, you can step through and read each timestep in the file
    separately using the "start=" and "count=" arguments to the
    get.var.ncdf() call.

    Regards,

    --Dave

    --
    David W. Pierce
    Division of Climate, Atmospheric Science, and Physical Oceanography
    Scripps Institution of Oceanography, La Jolla, California, USA
    (858) 534-8276 (voice) / (858) 534-8561 (fax) dpi...@...edu


    --
    Yogesh K. Tiwari (Dr.rer.nat),
    Scientist,
    Centre for Climate Change Research,
    Indian Institute of Tropical Meteorology,
    Homi Bhabha Road,
    Pashan,
    Pune-411008
    INDIA

    Phone: 0091-99 2273 9513 (Cell)
    : 0091-20-25904452 (O)
    Fax : 0091-20-258 93 825
  • David William Pierce at Apr 13, 2012 at 3:52 pm

    On Fri, Apr 13, 2012 at 12:09 AM, Yogesh Tiwari wrote:
    Dear David,

    Thanks,

    I could read and open .nc file in R, but now how to plot a simple filled
    color. [...]
    Hi Yogesh,

    glad to hear that the ncdf package is doing its job correctly. I'm
    sure you understand that I don't have the resources to answer
    miscellaneous general questions about how to use R, especially
    considering that there are good instruction manuals freely available
    on the web. You can also buy a textbook on R if you want to learn it
    in a more structured fashion. Either way, R is a capable system that
    rewards a modest effort devoted to learning how to use it.

    Regards,

    --Dave

    --
    David W. Pierce
    Division of Climate, Atmospheric Science, and Physical Oceanography
    Scripps Institution of Oceanography, La Jolla, California, USA
    (858) 534-8276 (voice) ?/ ?(858) 534-8561 (fax) ? ?dpierce at ucsd.edu

Related Discussions

Discussion Navigation
viewthread | post