def rt2dt(array, units_in, units_out=None, dummy1=None): '''Convert reference times to date-time objects The returned array is always independent. :Parameters: array: numpy array-like units_in: `Units` units_out: *optional* Ignored. dummy1: Ignored. :Returns: `numpy.ndarray` An array of `cf.Datetime` objects with the same shape as *array*. ''' mask = None if numpy_ma_isMA(array): # num2date has issues if the mask is nomask mask = array.mask if mask is numpy_ma_nomask or not numpy_ma_is_masked(array): array = array.view(numpy_ndarray) units = units_in.units calendar = getattr(units_in, 'calendar', 'standard') array = cftime.num2date(array, units, calendar, only_use_cftime_datetimes=True) # array = units_in._utime.num2date(array) if mask is not None: array = numpy_ma_masked_where(mask, array) ndim = numpy_ndim(array) if mask is None: # There is no missing data return numpy_array(array, dtype=object) # return numpy_vectorize( # partial(dt2Dt, calendar=units_in._calendar), # otypes=[object])(array) else: # There is missing data if not ndim: return numpy_ma_masked_all((), dtype=object) else: # array = numpy_array(array) # array = numpy_vectorize( # partial(dt2Dt, calendar=units_in._calendar), # otypes=[object])(array) return numpy_ma_masked_where(mask, array)
def __init__(self, array): """**Initialization** :Parameters: array: numpy array The array to be stored on disk in a temporary file. **Examples:** >>> f = CachedArray(numpy.array([1, 2, 3, 4, 5])) >>> f = CachedArray(numpy.ma.array([1, 2, 3, 4, 5])) """ super().__init__() # ------------------------------------------------------------ # Use mkstemp because we want to be responsible for deleting # the temporary file when done with it. # ------------------------------------------------------------ _partition_dir = mkdtemp( prefix="cf_cachedarray_", dir=CONSTANTS["TEMPDIR"] ) fd, _partition_file = mkstemp( prefix="cf_cachedarray_", suffix=".npy", dir=_partition_dir ) close(fd) # The name of the temporary file storing the array self._set_component("_partition_dir", _partition_dir) self._set_component("_partition_file", _partition_file) # Numpy data type of the array self._set_component("dtype", array.dtype) # Tuple of the array's dimension sizes self._set_component("shape", array.shape) # Number of elements in the array self._set_component("size", array.size) # Number of dimensions in the array self._set_component("ndim", array.ndim) if numpy_ma_is_masked(array): # Array is a masked array. Save it as record array with # 'data' and 'mask' elements because this seems much # faster than using numpy.ma.dump. self._set_component("_masked_as_record", True) numpy_save(_partition_file, array.toflex()) else: self._set_component("_masked_as_record", False) if hasattr(array, "mask"): # Array is a masked array with no masked elements numpy_save(_partition_file, array.view(numpy_ndarray)) else: # Array is not a masked array. numpy_save(_partition_file, array)