[R] [netcdfgroup] [ncdf4] error converting GEIA data to netCDF

Roy Mendelssohn roy.mendelssohn at noaa.gov
Tue Aug 28 18:07:32 CEST 2012


Hi Tom:

Sorry for top-posting.  My bad etiquette for the day.  The order that you do things when you create nercdf files matters a lot.  Here is a section from the help for ncdf4:

> 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 callnc_create to create the netCDF file. Finally, call ncvar_put to write your data to the newly created netCDF file, and nc_closewhen you are done.


And here is the relevant section from your code:

> netcdf.file <- nc_create(
>   filename=netcdf.fn,
> #  vars=list(emis.var),
> #  verbose=TRUE)
>   vars=list(emis.var))
> 
> # Write data to data variable: gotta have file first.
> # Gotta convert 2d global.emis.mx[lat,lon] to 3d global.emis.arr[time,lat,lon]
> # Do this before adding _FillValue to prevent:
> # > Error in R_nc4_put_vara_double: NetCDF: Not a valid data type or _FillValue type mismatch
> ## global.emis.arr <- global.emis.mx
> ## dim(global.emis.arr) <- c(1, dim(global.emis.mx))
> ## global.emis.arr[1,,] <- global.emis.mx
> 
> # Note
> # * global.emis.mx[lat,lon]
> # * datavar needs [lon, lat, time] (with time *last*)
> 
> ncvar_put(
>   nc=netcdf.file,
>   varid=emis.var,
> #  vals=global.emis.arr,
>   vals=t(global.emis.mx),
> #  start=rep.int(1, length(dim(global.emis.arr))),
>   start=c(1, 1, 1),
> #  count=dim(global.emis.arr))
>   count=c(-1,-1, 1)) # -1 -> all data

You can't write until all dimensions have been defined, and all variables defined.

HTH,

-Roy



On Aug 28, 2012, at 4:29 AM, Tom Roche wrote:

> 
> Tom Roche Mon, 27 Aug 2012 23:31:23 -0400
>>> summary: I can successfully ncvar_put(...) data to a file, but when
>>> I try to ncvar_get(...) the same data
> 
> in the one and only datavar
> 
>>> I get
> 
>>>> Error in if (nc$var[[li]]$hasAddOffset) addOffset = nc$var[[li]]$addOffset else addOffset = 0 : 
>>>>  argument is of length zero
> 
> David W. Pierce Mon, 27 Aug 2012 21:35:35 -0700
>> If you want to create a file and write to it, and then read back in from that same file, close it first and then reopen it.
> 
> So nc_sync is not enough--good to know. But ...
> 
> Unfortunately I do that (nc_close then nc_open) in the latest code @
> 
> https://github.com/TomRoche/GEIA_to_NetCDF
> 
> (direct link to relevant file=
> 
> https://github.com/TomRoche/GEIA_to_netCDF/blob/master/GEIA.to.netCDF.r
> 
> ) but no fix--same error. Any other suggestions?
> 
> your assistance is appreciated, Tom Roche <Tom_Roche at pobox.com>
> 
> _______________________________________________
> netcdfgroup mailing list
> netcdfgroup at unidata.ucar.edu
> For list information or to unsubscribe,  visit: http://www.unidata.ucar.edu/mailing_lists/ 

**********************
"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
1352 Lighthouse Avenue
Pacific Grove, CA 93950-2097

e-mail: Roy.Mendelssohn at noaa.gov (Note new e-mail address)
voice: (831)-648-9029
fax: (831)-648-8440
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