Thread Subject: ncwriteatt: trouble with _FillValue

Subject: ncwriteatt: trouble with _FillValue

From: Rodney

Date: 16 Jun, 2011 00:15:31

Message: 1 of 7

I get an error when I use ncwriteatt to write the (NetCDF standard)
_FillValue attribute for a variable. If I get rid of the initial
underscore, ncwriteatt is fine, but the attribute really should be
_FillValue. (That attribute is even referenced in the Matlab netcdf
documentation for ncwrite.)

What am I missing?

Thanks.

Subject: ncwriteatt: trouble with _FillValue

From: Ashish

Date: 16 Jun, 2011 13:11:04

Message: 2 of 7

On Wed, 15 Jun 2011 20:15:31 -0400, Rodney <rodneyhoffman@gmail.com> wrote:

> I get an error when I use ncwriteatt to write the (NetCDF standard)
> _FillValue attribute for a variable. If I get rid of the initial
> underscore, ncwriteatt is fine, but the attribute really should be
> _FillValue. (That attribute is even referenced in the Matlab netcdf
> documentation for ncwrite.)
>
> What am I missing?
>
> Thanks.

Rodney,
Could you post some sample code and the error message?
Ashish


nccreate('t.nc','v1','Dimensions',{'d1',1,'d2',2});
ncwriteatt('t.nc','v1','_FillValue',0);
ncdisp('t.nc')

Subject: ncwriteatt: trouble with _FillValue

From: Rodney

Date: 19 Jun, 2011 00:17:35

Message: 3 of 7

On Jun 16, 6:11 am, "Ashish " <first.l...@mathworks.com> wrote:
> On Wed, 15 Jun 2011 20:15:31 -0400, Rodney <rodneyhoff...@gmail.com> wrote:
> > I get an error when I usencwriteattto write the (NetCDF standard)
> > _FillValue attribute for a variable.  If I get rid of the initial
> > underscore,ncwriteattis fine, but the attribute really should be
> > _FillValue.  (That attribute is even referenced in the Matlab netcdf
> > documentation for ncwrite.)
>
> > What am I missing?
>
> > Thanks.
>
> Rodney,
> Could you post some sample code and the error message?
> Ashish
>
> nccreate('t.nc','v1','Dimensions',{'d1',1,'d2',2});ncwriteatt('t.nc','v1','_FillValue',0);
> ncdisp('t.nc')

Ashish,

I'm using Matlab 7.12.0 (R2011a).

I wrote toynnetcdf.m:
 - - - - - - - - [toynetcdf.m] - - - - - - - - -

trials = 42;
testarray = [12, 15, 18, 21, 24];
ncname = 'toy.nc';

nccreate(ncname, 'trials', 'Datatype', 'int32');
ncwrite(ncname, 'trials', trials);
ncwriteatt(ncname, 'trials', 'long_name', 'Number of trials');
ncwriteatt(ncname, 'trials', 'units', 'na');
ncwriteatt(ncname, 'trials', 'FillValue', -2147483647);

nccreate(ncname, 'testarray', 'Dimensions', {'mydim1', 5}, 'Datatype',
'int32');
ncwrite(ncname, 'testarray', testarray);
ncwriteatt(ncname, 'testarray', 'long_name', '1D array');
ncwriteatt(ncname, 'testarray', 'units', 'mm');
ncwriteatt(ncname, 'testarray', '_FillValue', -2147483647);
 - - - - - - - - - - - - - - - -

Here's what happens when I try to run toynetcdf.m:

>> toynetcdf
Warning: The following error was caught while executing 'onCleanup'
class destructor:
Library failure "Can't open HDF5 attribute".
> In toynetcdf at 15
Warning: The following error was caught while executing
'internal.matlab.imagesci.nc' class destructor:
Library failure "Can't open HDF5 attribute".
> In toynetcdf at 15
??? Error using ==> netcdflib
Library failure "NetCDF: Invalid argument".

Error in ==> putAtt at 93
   netcdflib(funstr,ncid,varid,attname,xtype,attvalue);

Error in ==> nc>nc.writeAttribute at 216
           netcdf.putAtt(gid, varid, attName, attValue);

Error in ==> ncwriteatt at 38
ncObj.writeAttribute(location, attName, attValue);

Error in ==> toynetcdf at 15
ncwriteatt(ncname, 'testarray', '_FillValue', -2147483647);

 - - - - - - - - - - - - - - - -

If I remove the underscore, toynetcdf.m runs without complaint and
produces toy.nc, which looks fine (except the attribute name is
missing the leading underscore, of course).

Subject: ncwriteatt: trouble with _FillValue

From: Ashish

Date: 20 Jun, 2011 13:59:10

Message: 4 of 7

Rodney,

Thanks for the details.

I was puzzled for a bit too. Using the low-level interface (the netcdf.X
package), I was able to get to the original error message from the NetCDF
library.
(Note: the nc* functions are internally based on the low-level interface
to the NetCDF library).

     ??? Error using ==> netcdflib
     Library failure "NetCDF: Not a valid data type or _FillValue type
mismatch".

Looking up
http://www.unidata.ucar.edu/software/netcdf/docs/BestPractices.html#Missing%20Data%20Values
Note:
"The _FillValue attribute should have the same data type as the variable
it describes. If the variable is packed using scale_factor and add_offset
attributes, the _FillValue attribute should have the data type of the
packed data."

Try (In MATLAB, all numbers are 'double' by default)

    ncwriteatt(ncname, 'testarray', '_FillValue', int32(-2147483647));


I'll look into addressing in the MATLAB function(s).

Ashish

Subject: ncwriteatt: trouble with _FillValue

From: Ashish

Date: 20 Jun, 2011 14:04:33

Message: 5 of 7

On Mon, 20 Jun 2011 09:59:10 -0400, Ashish <first.last@mathworks.com>
wrote:

> Rodney,
>
> Thanks for the details.
>
> I was puzzled for a bit too. Using the low-level interface (the netcdf.X
> package), I was able to get to the original error message from the
> NetCDF library.
> (Note: the nc* functions are internally based on the low-level interface
> to the NetCDF library).
>
> ??? Error using ==> netcdflib
> Library failure "NetCDF: Not a valid data type or _FillValue type
> mismatch".
>
> Looking up
> http://www.unidata.ucar.edu/software/netcdf/docs/BestPractices.html#Missing%20Data%20Values
> Note:
> "The _FillValue attribute should have the same data type as the variable
> it describes. If the variable is packed using scale_factor and
> add_offset attributes, the _FillValue attribute should have the data
> type of the packed data."
>
> Try (In MATLAB, all numbers are 'double' by default)
>
> ncwriteatt(ncname, 'testarray', '_FillValue', int32(-2147483647));
>
>
> I'll look into addressing in the MATLAB function(s).
>
> Ashish

Or ( I should have mentioned this first), use the 'FillValue' parameter in
NCCREATE.
_FillValue is a special attribute handled differently by the NetCDF
library, and is probably best defined during creation.

NCCREATE will perform an internal check of the datatype and will error if
they dont match.

   nccreate(ncname, 'trials', 'Datatype',
'int32','FillValue',int32(-2147483647));

Subject: ncwriteatt: trouble with _FillValue

From: Rodney

Date: 20 Jun, 2011 15:12:05

Message: 6 of 7

On Jun 20, 7:04 am, "Ashish " <first.l...@mathworks.com> wrote:
> On Mon, 20 Jun 2011 09:59:10 -0400, Ashish  <first.l...@mathworks.com>  
> wrote:
>
>
>
>
>
>
>
>
>
> > Rodney,
>
> > Thanks for the details.
>
> > I was puzzled for a bit too. Using the low-level interface (the netcdf.X  
> > package), I was able to get to the original error message from the  
> > NetCDF library.
> > (Note: the nc* functions are internally based on the low-level interface  
> > to the NetCDF library).
>
> >      ??? Error using ==> netcdflib
> >      Library failure "NetCDF: Not a valid data type or _FillValue type  
> > mismatch".
>
> > Looking up  
> >http://www.unidata.ucar.edu/software/netcdf/docs/BestPractices.html#M...
> > Note:
> > "The _FillValue attribute should have the same data type as the variable  
> > it describes. If the variable is packed using scale_factor and  
> > add_offset attributes, the _FillValue attribute should have the data  
> > type of the packed data."
>
> > Try (In MATLAB, all numbers are 'double' by default)
>
> >     ncwriteatt(ncname, 'testarray', '_FillValue', int32(-2147483647));
>
> > I'll look into addressing in the MATLAB function(s).
>
> > Ashish
>
> Or ( I should have mentioned this first), use the 'FillValue' parameter in  
> NCCREATE.
> _FillValue is a special attribute handled differently by the NetCDF  
> library, and is probably best defined during creation.
>
> NCCREATE will perform an internal check of the datatype and will error if  
> they dont match.
>
>    nccreate(ncname, 'trials', 'Datatype',  
> 'int32','FillValue',int32(-2147483647));

  Ashish,

Ah, of course, specifying FillValue during variable creation is the
way to go. Thanks. That works for me.

One small remaining question. I have a char variable and tried to
define its _FillValue as the empty string:
    nccreate('ncname, 'svar', 'Dimensions', {'slen', 20, 'nprof',
Inf}, "Datatype', 'char', 'FillValue', '');
Matlab didn't complain, but ncdump of the output file doesn't show any
_FillValue attribute for that variable.

  -- Rodney

Subject: ncwriteatt: trouble with _FillValue

From: Ashish

Date: 20 Jun, 2011 17:06:37

Message: 7 of 7

> One small remaining question. I have a char variable and tried to
> define its _FillValue as the empty string:
> nccreate('ncname, 'svar', 'Dimensions', {'slen', 20, 'nprof',
> Inf}, "Datatype', 'char', 'FillValue', '');
> Matlab didn't complain, but ncdump of the output file doesn't show any
> _FillValue attribute for that variable.
>
> -- Rodney


Havent tried it yet, but what would a FillValue of '' imply?

Maybe you are trying to disable the fill value? (Set 'FillValue' to
'disable').

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com