Chris Hill
Chris Hill

Reputation: 235

Why does netCDF masked data loaded into Python arrays cause problems with astropy Box2DKernel convolution?

When trying to apply Astropy convolution filters to a masked two-dimensional field of numbers read from a netCDF file I encountered two odd behaviors

TypeError                                 Traceback (most recent call last)
<ipython-input-16-6aeef1d2166f> in <module>()
      3 from astropy.convolution import Box2DKernel
      4 b2dk=Box2DKernel(9)
----> 5 sstC=ap_convolve(sstReg,b2dk)

/nfs/.../decorators.py
in convolve(array, kernel, boundary, fill_value, nan_treatment,
normalize_kernel, mask, preserve_nan, normalization_zero_tol)
    825             name = func.__name__
    826
--> 827         func = make_function_with_signature(func, name=name, **wrapped_args)
    828         func = functools.update_wrapper(func, wrapped, assigned=assigned,
    829                                         updated=updated)

/nfs/.../decorators.py
in wrapper(data, *args, **kwargs)
    243                                   AstropyUserWarning)
    244
--> 245             result = func(data, *args, **kwargs)
    246
    247             if unpack and repack:

/nfs/.../convolve.py
in convolve(array, kernel, boundary, fill_value, nan_treatment,
normalize_kernel, mask, preserve_nan, normalization_zero_tol)
    167         # because none of what follows modifies array_internal.
    168         array_dtype = array.dtype
--> 169         array_internal = array.astype(float, copy=False)
    170     else:
    171         raise TypeError("array should be a list or a Numpy array")

TypeError: astype() got an unexpected keyword argument 'copy'

Upvotes: 0

Views: 908

Answers (1)

Chris Hill
Chris Hill

Reputation: 235

A way to fix these problems is to convert the masked array using

numpy.ma.filled()

with float('nan') as the mask value e.g.

phi2=numpy.ma.filled(phi,float('nan'))

. This produces an array phi2 that is not masked. This prevents the copy error from occuring. The use of nan in the convolution causes the convole operation to adjust appropriately. Without the nan the mask value will be convolved with the real data - generally this is not what you want!

Upvotes: 1

Related Questions