def __add__(self, other): # Check that the units are compatable if not _das2.can_merge(self.units, '+', other.units): raise DatasetError("Operation %s + %s is invalid"%(self.units, other.units)) # Get the scaling factor rFactor = 1.0 if self.units == 'UTC' or self.units == 'ns1970': rFactor = _das2.convert(1.0, other.units, 'ns') my_ary = self.array.astype("int64", copy=False) other_ary = other.array * rFactor other_ary = other_ary.astype("int64") new_ary = my_ary + other_ary new_ary = new_ary.astype(numpy.dtype('M8[ns]'), copy=False) else: rFactor = _das2.convert(1.0, other.units, self.units) new_ary = self.array + (other.array * rFactor) var = Variable(self.dim, None, new_ary, self.units, fill=self.fill) return var
def containsAny(self, quant): """Are any of the given values within the range of this Variable Args: quant (number, Quantity) : the value to check must be of a type that is comparable to the values in the backing array but not nessecarily in the same units. If a Quantity is supplied than unit conversions are applied before comparing values. Returns: bool True if variable.max <= value <= variable.max, for all supplied values or False otherwise """ # Simple value case (no units) rMin = self.array.min() rMax = self.array.max() if isinstance(quant, Quantity): if not _das2.convertible(self.units, quant.unit): raise ValueError( "This Variable's units, %s, are not convertable to %s"%( self.units, quant.unit )) if (self.units != quant.unit): rMin = _das2.convert(rMin, self.units, quant.unit) rMax = _das2.convert(rMax, self.units, quant.unit) # Can't use loops. aTest = numpy.logical_and(quant.value < rMin, quant.value > rMax) if numpy.any( aTest ): return False return True
def to_value(self, unit=None): """Return the numeric value, possibly in different units. Args: unit (str) : If None this is the same as accessing the .value member Returns: value : ndarray or scaler The value, which may be an array, in the requested units. Raises: ValueError if the units of the current Quantity are not compatible with the desired units """ if unit == None or unit == self.unit: return self.value if not _das2.convertible(self.unit, unit): raise ValueError( "This Quantities's units, %s, are not convertable to %s"%( self.unit, unit )) rScale = _das2.convert(1.0, self.unit, unit) return rScale * self.value
def _varAttrs(zVar, var, sType='c'): # Set the variable properties, all vars should have: # CATDESC, DEPEND_*, FIELDNAM, FILLVAL, FORMAT, # UNITS, VALIDMIN, VALIDMAX if sType == 'c': zVar.attrs['VAR_TYPE'] = 'support_data' else: if var.name in ('center','reference','offset','mean','median','mode'): zVar.attrs['VAR_TYPE'] = 'data' else: zVar.attrs['VAR_TYPE'] = 'support_data' if var.fill != None: zVar.attrs['FILLVAL'] = var.fill elif 'fill' in var.dim.props: zVar.attrs['FILLVAL'] = var.dim.props['fill'] # If this is a main variable (reference, center, mean, median, mode) # let it use the properties from the dimension if var.name in ('reference', 'center', 'mean', 'median', 'mode'): for sProp in var.dim.props: if sProp.lower() in g_lSkip: continue if sProp.lower() in g_dPropTrans: sCProp = g_dPropTrans[sProp.lower()] else: sCProp = sProp zVar.attrs[sCProp] = var.dim.props[sProp] # CDF folds together the concepts of min, max and max_error, min_error # into the same metadat value. Take min and max first if available for sRole in ('min','min_error','uncertianty','std_dev'): if sRole in var.dim.vars: zVar.attrs['DELTA_MINUS_VAR'] = var.dim.name + "_" + sRole break for sRole in ('max','max_error','uncertianty','std_dev'): if sRole in var.dim.vars: zVar.attrs['DELTA_PLUS_VAR'] = var.dim.name + "_" + sRole break # If this variable doesn't have a CATDESC, make one up. if 'CATDESC' not in zVar.attrs: zVar.attrs['CATDESC'] = "%s%s %s%s"%( var.dim.name[0].upper(), var.dim.name[1:], var.name[0].upper(), var.name[1:] ) # Handle the Datum values, range and tagwidth if 'range' in var.dim.props: prop = var.dim.props['range'] if isinstance(prop, Quantity): if len(prop.value) > 0: rMin = _das2.convert(prop.value[0], prop.unit, var.units) zVar.attrs['SCALEMIN'] = rMin if len(prop.value) > 1: rMax = _das2.convert(prop.value[1], prop.unit, var.units) zVar.attrs['SCALEMAX'] = rMax else: perr("WARNING: Property %s:%s -> 'range' is not of type Quantity"%( var.dim.name, var.name)) for sProp in ('tagWidth','cacheRange'): if sProp in var.dim.props: zVar.attrs[ g_dPropTrans[sProp] ] = str(var.dim.props[sProp])