barronh / pseudonetcdf

PseudoNetCDF like NetCDF except for many scientific format backends
GNU Lesser General Public License v3.0
76 stars 35 forks source link

Deriving veriables within a PNC object before aggregation #102

Closed bnmurphy closed 4 years ago

bnmurphy commented 4 years ago

I’d like to add two netcdf variables to each other before slicing or averaging. So I’m trying this:

pncf = pnc.pncopen(inpath, format='ioapi', mode='rs') pnc_gp = pncf.eval('X = Y + Z', inplace=True)

And it says the operation is unsupported for netCDF4 variables. Do I need to break everything up into arrays and reassemble somehow?

barronh commented 4 years ago

This is a source of some annoyance to me. The code below will work before or after slicing.

pnc_gp = pncf.eval('X = Y[:]+ Z[:]', inplace=True)

The problem is that a newly opened netcdf file has NetCDFVariables while a processed file has PseudoNeCDFVariables. A PseudoNeCDFVariable implements math operators while a NeCDFVariable does not. However, when sliced both offer numpy arrays that support math.

So, expressions with slices (i.e, [:]) will work with either and expressions without will only work after some processing.

The short term answer is slicing in the expression.

barronh commented 4 years ago

By the way, the inplace=True will only work on files with write access or that have been copied into memory.

pncf = pnc.pncopen(inpath, format='ioapi', mode='rs').copy()
pnc_gp = pncf.eval('X = Y + Z', inplace=True) 

Or

pncf = pnc.pncopen(inpath, format='ioapi', mode='rs')
pnc_gp = pncf.eval('X = Y[:]+ Z[:]')