Python Forum

Full Version: netCDF issue with filling a variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello guys,

I'm working with some netCDF climatic data and I want to change specific values inside of some multidimensional variables. Here is an example to explain this.

I open  a netCDF file  in read/write mode:

In [1]: from netCDF4 import Dataset

In [2]: f = Dataset(r'G:\TRAVAIL_DE_MASTER\TestCOORD\copy.nc', 'a', format='NETCDF3_CLASSIC')
If I read the value of the variable "evap" with the following coordinate (time=3rd, latitude=7th, longitude=2nd) I am able to obtain the following value:
In [4]: f.variables['evap'][3][7][2]
Out[4]: 0.13604085
Then I would like for example to change this specific value to 1234.0, like that:
In [6]: f.variables['evap'][3][7][2] = 1234.
It returns no errors but if I look back to the variable, the value remains unchanged, I don't understand why (this occurs the same when a file is opened in write mode):

In [7]: f.variables['evap'][3][7][2]
Out[7]: 0.13604085
This occurs only with variables of more that 1 dimension. If I do the same with a 1 dimension variable I'm able to change de values. Here's an example with the variable "rlat" (rotated latitude) of the same dataset:

In [8]: f.variables['rlat'][3]
Out[8]: -16.9

In [9]: f.variables['rlat'][3] =  1234.

In [10]: f.variables['rlat'][3]
Out[10]: 1234.0
Here the value has sucessfully been changed to 1234.0.
What is the cause of this? Im I using the numpy arrays on a wrong way with the netCDF4 librairy?
(Sep-19-2016, 05:17 PM)HeavyLoads Wrote: [ -> ]Im I using the numpy arrays on a wrong way with the netCDF4 librairy?
This is a really specialized question, so it is very difficult to help with when we can't reproduce the problem locally. Can you provide a .nc which can reproduce the problem? Can we use pip to get the module you're using?