Hi Tom,

a few answers to your questions:

On Mon, Jan 9, 2012 at 3:54 PM, Tom Roche <Tom_Roche@pobox.com> wrote:

>
> :-) I guess I should have explained: I need to copy most of a source
> file, modifying only part, and to write a target file. So my motivation
> for this thread is, first, to be sure I can do the copying correctly
> (*not* merely to copy an netCDF file). Does this seem reasonable?
>

Well ... all I can say is I'd advise against doing this. I think it's the
wrong approach. Better to use 'cp' to make a bit-for-bit copy and then
modify that. I.e.,

infile = 'data.nc'
outfile = 'data_new.nc'
varname = 'ozone'

cmd = paste('cp',infile,outfile)
system(cmd)
ncid = open.ncdf( outfile, write=TRUE )
...modify exact copied file as desired, for example:
var = ncid$var[[varname]]
data_old = var.get.ncdf( ncid, var )
data_new = some_function( data_old )
var.put.ncdf( ncid, var, data_new )
close.ncdf( ncid )

1 Precisions "int" and "float" not supported by var.def.ncdf(...).
>  When I tried to do (formatted for email)
>
>  target.datavars[[target.datavars.i]] <-
>    var.def.ncdf(source.datavar$name, source.datavar$units,
>    target.datavar.dims, source.datavar$missval,
> ->  prec=source.datavar$prec)
>
>  I got
>
> - var.def.ncdf: error: unknown precision specified: int .
> - Known values: short single double integer char byte
>

Sorry, bug. Unfortunately ncdf4 is the new and supported release -- netcdf
library version 4 has been out for years now -- so I concentrate on
supporting that.

2 Copying I/O API global attributes fails. I/O API uses lots of these
>  (33 in my source.nc!), so my diff has
>
> -// global attributes:
> -               :IOAPI_VERSION = "1.0 1997349 (Dec. 15, 1997)" ;
> -               :EXEC_ID = "????????????????                 " ;
> -               :FTYPE = 1 ;
> -               :CDATE = 2011353 ;
> -               :CTIME = 1224 ;
>
> + )
> > for (attr.name in global.attr.name.list) {
> +   source.datavar.attr <- att.get.ncdf(source.file, 0, attr.name)
> +   att.put.ncdf(target.file, 0, attr.name, source.datavar.attr$value)
> + }
>

I think you *don't* want that leading colon (":"). That's the syntax of
ncdump, not the name of the attribute. I.e., the attribute name is "FTYPE",
not ":FTYPE".

3 When I diff my `ncdump`s, i.e.,
>
> $ diff -uwB  <( ncdump -h source.nc ) <( ncdump -h target.nc )
>
>  I get
>

This is why you want to use 'cp' to make a bit-for-bit copy. *In general*,
R cannot guarantee a bit-for-bit copy programatically because it does not
have all the data types that a netcdf file supports. So if you really want
a bit-for-bit copy, use 'cp', that's what it's for. You could, with enough
effort, manipulate the ncdf library into making a file with the same
structure as the original, but it's not set up for that.

Think of it this way. You want to copy a database. The database is held in
a netcdf file. Using ncdf, R provides a way of generating a new database
that has the same information in it as the original database. However, the
bits might be arranged differently. Maybe the original has table-A stored
before table-B, and the copy has table-B stored before table-A. This won't
matter to someone who uses the database. Using 'diff' checks if the actual
bits are arranged exactly the same, not whether the same information is in
the two data sets.


> * the target file has *new* coordinate variables for the dimensions.
>
> * I don't understand why those coordinate variables weren't in the
>  source file. But they're not!
>

ncdf4 provides a mechanism to suppress creating a coordinate variable.


> 4 Attribute="long_name" is missing for every original/copied data
>  variable. Hence when I diff my `ncdump`s I also get, e.g.,
>
>        int TFLAG(TSTEP, VAR, DATE-TIME) ;
>                TFLAG:units = "<YYYYDDD,HHMMSS>" ;
> -               TFLAG:long_name = "TFLAG           " ;
>                TFLAG:var_desc = "..."
>

Just a bug. Fixed in ncdf4.


-- 
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@ucsd.edu

	[[alternative HTML version deleted]]

