Ross Maidment
2011-Sep-07 21:25 UTC
[R] Editing the variables attributes section in the netCDF header of netCDF files created using the package ncdf.
Hi, I am using the package ncdf to create netCDF files and I want to mimic the the header of an exiting netCDF file created outside of R. Below is what the existing header looks like (part of it that is different): netcdf ccd1984_05_08 { dimensions: lat = 1974 ; lon = 1894 ; time = UNLIMITED ; // (1 currently) variables: int time(time) ; time:long_name = "time" ; time:units = "days since 1984-05-01 0:0:0" ; time:day_begins = "06:15" ; double lat(lat) ; lat:long_name = "latitude" ; lat:standard_name = "latitude" ; lat:units = "degrees_north" ; lat:axis = "Y" ; double lon(lon) ; lon:long_name = "longitude" ; lon:standard_name = "longitude" ; lon:units = "degrees_east" ; lon:axis = "X" ; byte ccd(time, lat, lon) ; ccd:Unit_duration = 7.5f ; ccd:units = "Unit_duration mins" ; ccd:long_name = "Cold Cloud Duration" ; ccd:short_name = "ccd" ; ccd:_FillValue = -1b ; And here is my attempt when using the ncdf package: netcdf ccd1983_04_08_r { dimensions: lon = 1894 ; lat = 1974 ; time = UNLIMITED ; // (1 currently) variables: double lon(lon) ; lon:units = "" ; double lat(lat) ; lat:units = "" ; double time(time) ; time:units = "days since 1900-01-01" ; byte ccd(time, lat, lon) ; ccd:units = "" ; ccd:missing_value = -99b ; ccd:long_name = "Cold Cloud Duration" ; I am unable to replicate the variables attributes that exist in the first example in the second example. Is there anyway to transfer across the header information or edit the ncdf package to do this? Thanks in advance, Ross (PhD Student)
David William Pierce
2011-Sep-07 21:56 UTC
[R] Editing the variables attributes section in the netCDF header of netCDF files created using the package ncdf.
On Wed, Sep 7, 2011 at 2:25 PM, Ross Maidment < R.I.Maidment@pgr.reading.ac.uk> wrote:> Hi, > > I am using the package ncdf to create netCDF files and I want to mimic the > the header of an exiting netCDF file created outside of R. Below is what the > existing header looks like (part of it that is different): > > netcdf ccd1984_05_08 { > dimensions: > lat = 1974 ; > [...listings omitted...] > I am unable to replicate the variables attributes that exist in the first > example in the second example. Is there anyway to transfer across the header > information or edit the ncdf package to do this? >Hi Ross, to specify the units attribute in the output file, you just set the units parameter correctly when calling dim.def.ncdf. Since your ncdf-created file has blank units attributes, I can only assume your R code is doing something like this: dim_lon <- dim.def.ncdf( 'lon', "", lonvals ) when it should be doing something more like this: dim_lon <- dim.def.ncdf( 'lon', "degrees_east", lonvals ), etc. Easiest way to "copy" a dimension is R code like this: file2copy = 'old_file.nc' ncid_old = open.ncdf( file2copy ) dim2copy = "lon" old_dim = ncid_old$dim[[ dim2copy ]] new_dim = dim.def.ncdf( old_dim$name, old_dim$units, old_dim$vals, unlim=old_dim$unlim, longname=old_dim$longname ) For attributes other than units, you can use the att.put.ncdf() call. You make this call AFTER you have created the output file: ... ncid_out = create.ncdf( 'new_output_file.nc', list(output_vars1,...,output_varN)) att.put.ncdf( ncid_out, 'lat', 'axis', 'Y' ) att.put.ncdf( ncid_out, 'ccd', 'Unit_duration', 7.5 ) etc. --Dave> > ______________________________________________ > R-help@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 W. Pierce Division of Climate, Atmospheric Science, and Physical Oceanography Scripps Institution of Oceanography (858) 534-8276 (voice) / (858) 534-8561 (fax) dpierce@ucsd.edu [[alternative HTML version deleted]]
RMaidment
2011-Sep-09 22:18 UTC
[R] Editing the variables attributes section in the netCDF header of netCDF files created using the package ncdf.
Hi Dave, Many thanks for you prompt and useful reply. It worked a treat! Another question which you might be able to answer is, is it possible to set the order of the variables attributes in which they appear? For example, I would like to set the order of Example 2 to Example 1: Example 1: double lon(lon) ; lon:long_name = "longitude" ; lon:standard_name = "longitude" ; lon:units = "degrees_east" ; lon:axis = "X" ; Example 2: double lon(lon) ; lon:units = "degrees_east" ; lon:long_name = "longitude" ; lon:standard_name = "longitude" ; lon:axis = "X" ; Many thanks, Ross -- View this message in context: http://r.789695.n4.nabble.com/Editing-the-variables-attributes-section-in-the-netCDF-header-of-netCDF-files-created-using-the-pack-tp3797349p3802927.html Sent from the R help mailing list archive at Nabble.com.
David William Pierce
2011-Sep-10 14:48 UTC
[R] Editing the variables attributes section in the netCDF header of netCDF files created using the package ncdf.
On Fri, Sep 9, 2011 at 3:18 PM, RMaidment <r.i.maidment at pgr.reading.ac.uk> wrote:> Another question which you might be able to answer is, is it possible to set > the order of the variables attributes in which they appear? [...]Hi Ross, The ncdf package makes the units and longname attributes automatically, so those will always be the first and second attributes, and the ones you add using att.put.ncdf will follow later in the order of your att.put.ncdf calls. So no, there is no way of arranging them all in arbitrary order. I would personally consider any software that cares about the order of the attributes to be non-compliant with netCDF standards, but perhaps there is some software out there that does care about the order anyway. Regards, --Dave -- David W. Pierce Division of Climate, Atmospheric Science, and Physical Oceanography Scripps Institution of Oceanography (858) 534-8276 (voice) ?/ ?(858) 534-8561 (fax) ? ?dpierce at ucsd.edu
Reasonably Related Threads
- read a netcdf file _Fill_value=-32768
- Extracting subset from netCDF file using lat/lon and converting into .csv in R
- Copying ncdf dims and vars from one file to the other
- Extracting subset from netCDF file using lat/lon and converting into .csv in R
- Inconsistent results from var.get.nc in RNetCDF