2

Is there a python netCDF4 command/example to change the global metadata _FillValue in a netCDF file? I have tried replacing all -ve values in a netCDF file, but till the time the _FillValue attribute is set, that does not work

user308827
  • 21,227
  • 87
  • 254
  • 417

3 Answers3

7

I don't believe python netCDF4 has a specific function for this, but NCO's ncatted is an ideal tool for this task.

From the docs:

To change the missing value from the IEEE NaN value to a normal IEEE number, like 1.0e36:

ncatted -a _FillValue,,m,f,1.0e36 in.nc
N1B4
  • 3,377
  • 1
  • 21
  • 24
4

You can also use cdo to set the missing entries to another value, for example 999.0

cdo setmisstoc,999.0 in.nc out.nc

However, from the description of your problem, it seems that you don't actually need to change the value for _Fillvalue but instead you want to set negative values to missing. You can achieve this by using the cdo function "set range to miss"

cdo setrtomiss,-1e8,0 in.nc out.nc 

the first argument is a big negative number that is smaller than the smallest value in your dataset. I'm assuming in the above answer that the field in question does already have a value set, otherwise you would need to add it with e.g.

cdo setattribute,field@_FillValue=-1.e32 infile outfile

In general it seems that defining a "global" _FillValue can cause issues (e.g. see https://github.com/Unidata/netcdf-c/issues/458) and anyway if you have a combination of integer and float fields then defining a default float fill can not be applied to all fields. I'm personally of the opinion that it is best practise to define a fill value for each field separately and if possible use the defaults for each data type.

cdo commands can be called directly from python using the cdo package.

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
1

A _FillValue is specific for a variable. With Python NetCDF4 library, I understand that it can only be specified when the variable is created. The _FillValue can be changed though with CDO. This is how I changed a _FillValue myself:

cdo -setattribute,Band1@_FillValue=-1.0 infile.nc outfile.nc
ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
Dobedani
  • 508
  • 5
  • 20