def addField(self, fieldName, fieldValue, longName=None, units=None, **kwargs): """ Add a field to the sounding .. _ndarray: http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.htm Parameters ---------- fieldName : str Name of the field to be added. It supports abbreviations (see :py:attr:`abbreviations`) fieldValue : any Corresponding field value. If the field value is an ndarray_ then it will be converted to a :py:class:`~SkewTplus.sounding.soundingArray` with the corresponding metadata. longName : str, optional Long name of the field units : str , optional units of the field missingValue : float, optional Value for missing data. Occurrences of this value will be masked. The default missingValue can be change with :py:meth:`setMissingValue` """ missingValue = kwargs.pop('missingValue', self.missingValue) fieldName = self._getFieldName(fieldName) if isinstance(fieldValue, ndarray): _fieldValue = masked_invalid(fieldValue) _fieldValue = masked_values(_fieldValue, missingValue) _fieldValue.harden_mask() self._soundingData[fieldName] = soundingArray( _fieldValue, longName=longName, units=units, missingValue=missingValue) else: self._soundingData[fieldName] = fieldValue
def execute(Tlo, Plo, Thi, Phi): C = 0.034167 Tratio = Thi/Tlo Pratio = Phi/Plo logTratio = log(Tratio) logPratio = log(Pratio) # Guard against divide-by-zero errors, again logPratio = masked_values(logPratio, 0, copy=False) result = C * logTratio / logPratio return result
def test_matrix_indexing(self): # Tests conversions and indexing x1 = np.matrix([[1, 2, 3], [4, 3, 2]]) x2 = masked_array(x1, mask=[[1, 0, 0], [0, 1, 0]]) x3 = masked_array(x1, mask=[[0, 1, 0], [1, 0, 0]]) x4 = masked_array(x1) # test conversion to strings str(x2) # raises? repr(x2) # raises? # tests of indexing assert_(type(x2[1, 0]) is type(x1[1, 0])) assert_(x1[1, 0] == x2[1, 0]) assert_(x2[1, 1] is masked) assert_equal(x1[0, 2], x2[0, 2]) assert_equal(x1[0, 1:], x2[0, 1:]) assert_equal(x1[:, 2], x2[:, 2]) assert_equal(x1[:], x2[:]) assert_equal(x1[1:], x3[1:]) x1[0, 2] = 9 x2[0, 2] = 9 assert_equal(x1, x2) x1[0, 1:] = 99 x2[0, 1:] = 99 assert_equal(x1, x2) x2[0, 1] = masked assert_equal(x1, x2) x2[0, 1:] = masked assert_equal(x1, x2) x2[0, :] = x1[0, :] x2[0, 1] = masked assert_(allequal(getmask(x2), np.array([[0, 1, 0], [0, 1, 0]]))) x3[1, :] = masked_array([1, 2, 3], [1, 1, 0]) assert_(allequal(getmask(x3)[1], masked_array([1, 1, 0]))) assert_(allequal(getmask(x3[1]), masked_array([1, 1, 0]))) x4[1, :] = masked_array([1, 2, 3], [1, 1, 0]) assert_(allequal(getmask(x4[1]), masked_array([1, 1, 0]))) assert_(allequal(x4[1], masked_array([1, 2, 3]))) x1 = np.matrix(np.arange(5) * 1.0) x2 = masked_values(x1, 3.0) assert_equal(x1, x2) assert_(allequal(masked_array([0, 0, 0, 1, 0], dtype=MaskType), x2.mask)) assert_equal(3.0, x2.fill_value)
def getCleanSounding(self): """ Clean the sounding, when Temperature or pressure has a missing values, removing that pressure level. It returns the pressure in Pa and temperatures in Celsius """ pressure = self['pressure'] temperature = self['temperature'] dewPointTemperature = self['dewPointTemperature'] dewPointTemperature[getmaskarray( dewPointTemperature)] = self.missingValue if pressure.units is not None: if pressure.units.lower() == 'hpa': hPa = True elif pressure.units.lower() == 'pa': hPa = False else: raise fatalError( "Error getting clean sounding plot", "Pressure units not supported:%s" % pressure.units, "Supported: hPa or Pa ") else: hPa = True if temperature.units is not None: if dewPointTemperature.units is not None: if temperature.units.lower( ) != dewPointTemperature.units.lower(): raise fatalError( "Error getting clean sounding plot", "Temperature units: %s" % temperature.units, "Dew Point Temp: %s" % dewPointTemperature.units) if temperature.units.lower() == 'c': celsius = True elif temperature.units.lower() == 'k': celsius = False else: raise fatalError( "Error getting clean sounding plot", "Temperature units not supported:%s" % temperature.units, "Supported: C or K ") else: celsius = True mask = ~mask_or( getmaskarray(pressure), getmaskarray(temperature), shrink=False) if hPa: _pressure = pressure.data[mask] else: _pressure = pressure.data[mask] / 100 if celsius: _temperature = temperature.data[mask] _dewPointTemperature = masked_values( dewPointTemperature.data[mask], self.missingValue) else: _temperature = temperature.data[mask] - 273.15 _dewPointTemperature = masked_values( dewPointTemperature.data[mask], self.missingValue) - 273.15 return (_pressure, _temperature, _dewPointTemperature)
def __init__(self, inputData=None, fileFormat=None, stationId=None): """ Sounding Initialization It supports 4 types of initialization: * From a University of Wyoming txt file * From a University of Wyoming Website * Form an ARM sounding Netcdf file * From a dictionary with the field names, field values pairs All the field are added trough the :py:meth:`addField` method. The fields names support abbreviations when they are set or get. But internally the field names stored are not abbreviated. See :py:attr:`SkewT.sounding.abbreviations`. If the fileFormat keyword is not specified, it is assumed that the input data correspond to a filePath and it will try determine the format automatically. .. _datetime:`https://docs.python.org/2/library/datetime.html` Parameters ---------- inputData : str, datetime_ or dict If inputData is a dictionary, the sounding fields are taken from the dictionary (field names, field values pairs). If fileFormat is "web", then the inputData is considered the date of the sounding. See :py:meth:fetchFromWeb for date input format. Otherwise, if inputData is an string, it is considered a file path. If fileFormat is None (default) the initialization will try to automatically detect the file format. If inputData is None, the create instance correspond to an empty sounding. fileFormat : str File format. Supported values: * 'txt', "wrf", "uw or "wyoming" : University of Wyoming * "netcdf", "arm" : ARM netcdf soundings * 'web' : University of Wyoming website stationId : str Station ID. Used only when "web" is selected as format. See :py:meth:fetchFromWeb for more details. """ self._soundingData = dict() self.missingValue = -9999. self.addField('StationNumber', '(No Number)') self.addField('SoundingDate', '(No Date)') if inputData is not None: if fileFormat == "web": self.fetchFromWeb(inputData, stationId) else: if isinstance(inputData, str): # A file path was given if not isfile(inputData): raise fatalError("The file does not exists: %s\n" % (inputData)) if fileFormat is None: # Try automatic detection of file format try: self.fetchFromARMFile(inputData) except OSError: # If it is not a NETCDF , try TXT self.fetchFromTxtFile(inputData) else: # If the argument is an string assume that is # in the university of wyoming format if fileFormat.lower() in ('txt', "wrf", "uw", "wyoming"): self.fetchFromTxtFile(inputData) elif fileFormat.lower() in ('netcdf', 'arm'): self.fetchFromARMFile(inputData) else: raise fatalError( "Error loading Sounding", "Selected format not supported:%s" % fileFormat, "Accepted formats:%s" % str( ('txt', "wrf", "uw", "wyoming", "netcdf"))) elif isinstance(inputData, dict): for fieldName, fieldValue in inputData.items(): if isinstance(fieldValue, ndarray): dataValues = masked_invalid(fieldValue) dataValues = masked_values(fieldValue, self.missingValue) dataValues.harden_mask() self.addField(fieldName, fieldValue) else: self.addField(fieldName, fieldValue) else: raise fatalError( "Input Data not supported", "type(inputData)=%s" % str(type(inputData)))