[R] About manipulating NetCDF files in ncdf package.

Roy Mendelssohn - NOAA Federal roy.mendelssohn at noaa.gov
Wed May 20 16:52:12 CEST 2015

Hi Thanh:

You are confounding  several issues here, and are providing only incomplete information about your data.  The confounded issues are:

1.  How to write a netcdf with a given coordinate system.

2.  How to calculate a monthly average or monthly subsample from a larger sample.

I would guess that you data are really dimensioned something like (time,lat, lon) and what would help is showing the results of say:


Also, in your original netcdf file there were probably coordinate variables for time, latitude and longitude, you need to read those in and be aware of their dimensions, and the latitude and longitude data will form the basis of your new netcdf file.

To create and write a new netcdf file you will need to understand somewhat the structure of a netcdf file.  As the help for the ncdf4-package says:

> If you want to WRITE data to a new netCDF file, the procedure is to first define the dimensions your data array has, then define the variable, then create the file. So, first call ncdim_def to define the dimensions that your data exists along (for example, latitude, longitude, and time). Then call ncvar_def to define a variable that uses those dimensions, and will hold your data. Then call nc_create to create the netCDF file. Finally, call ncvar_put to write your data to the newly created netCDF file, and nc_close when you are done.

Also look at the following web site:


As for issue two, your data are just 3-D arrays, and it is just a question of normal array averaging or other operations to get you monthly data.



> On May 20, 2015, at 4:49 AM, Thanh Huyền Trần <pizgy.st288 at gmail.com> wrote:
> Dear R-project staffs,
> I'm now dealing with some NetCDF files for my scientific work and I'm
> reading it and manipulating it with R.
> So far I would like to ask some questions:
> 1. I have a NetCDF file for time-series data (*daily* time step) in a
> domain (with longitudes and latitudes) with two variables are two component
> of wind speeds (u-wind and v-wind). I would like  to creat a new NetCDF
> file with the synthesized wind speed from the two components from the
> existing file in the same spatial resolution but with *monthly* time steps.
> Which syntax can I use to compile the code for this?
> (please find the enclosed picture for variables information)
> 2. I have another NetCDF file for time-series data *(hourly)*  in a domain
> (with longitudes and latitudes) for downward solar radiation which I want
> to transfer to *monthly* data.Which syntax can I use to compile the code
> for this?
> Thanks and looking forward to your guidance.
> Best regards,
> Thanh Huyen Tran
> -- 
> Thanh Huyen Tran
> *Master student of Hydrosciences and Engineering Programme,*
> *Faculty of Environmental Sciences*
> *University of Technology Dresden, Germany*
> <wind components variables.png>______________________________________________
> 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.

"The contents of this message do not reflect any position of the U.S. Government or NOAA."
Roy Mendelssohn
Supervisory Operations Research Analyst
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.Mendelssohn at 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.

More information about the R-help mailing list