def test_other_to_egads_calcs(self): """ Test scalar operations between other scalar and egads class""" egadstest1 = egads.EgadsData(self.value1, units='') self.assertEqual(self.value2 + self.value1, self.value2 + egadstest1, 'Other to Egads scalar addition not equal') self.assertEqual(self.value2 - self.value1, self.value2 - egadstest1, 'Other to Egads scalar subtraction not equal') self.assertEqual(self.value2 * self.value1, self.value2 * egadstest1, 'Other to Egads scalar multiplication not equal') self.assertEqual(self.value2 / self.value1, self.value2 / egadstest1, 'Other to Egads scalar division not equal') self.assertEqual(self.value2**self.value1, self.value2**egadstest1, 'Other to Egads scalar power not equal')
def setUp(self): self.na_filename = tempfile.mktemp('.na') self.nc_filename = tempfile.mktemp('.nc') f = einput.NasaAmes() f.save_na_file(self.na_filename, NA_DICT, float_format='%.4f') f.close() self.authors = 'John Doe; email: [email protected]' self.special_com = 'This is a test file for verifying the status of the EGADS NASA Ames functionality.' self.time = egads.EgadsData( value=[51143.42, 51144.42, 51145.42, 51146.42, 51147.42], units='seconds after midnight', _FillValue=-9900., scale_factor=1., standard_name='Time_np')
def setUp(self): self.data1 = egads.EgadsData(value=[0.5, 2.3, 6.2, 8.1, 4.], units='mm', long_name='a common data', scale_factor=1., _FillValue=-999) self.data2 = egads.EgadsData(value=[0., 1., 2., 3., 4.], units='days since 20170101 00:00:00Z', long_name='a common time vector', scale_factor=1., _FillValue=-999) self.file = FILE_NAME_ALT f = einput.NetCdf(self.file, 'w') f.add_dim(DIM1_NAME, DIM1_LEN) f.add_dim(DIM2_NAME, DIM2_LEN) f.write_variable(random_data, VAR_NAME, (DIM1_NAME, ), 'double') f.write_variable(random_mult_data, VAR_MULT_NAME, ( DIM1_NAME, DIM2_NAME, ), 'double') f.add_attribute('units', VAR_UNITS, VAR_NAME) f.add_attribute('units', VAR_MULT_UNITS, VAR_MULT_NAME) f.close()
def test_egads_to_other_calcs(self): """ Test scalar operations between egads class and other scalar""" egadstest1 = egads.EgadsData(self.value1, units='') self.assertEqual(self.value1 + self.value2, egadstest1 + self.value2, 'Egads to other scalar addition not equal') self.assertEqual(self.value1 - self.value2, egadstest1 - self.value2, 'Egads to other scalar subtraction not equal') self.assertEqual(self.value1 * self.value2, egadstest1 * self.value2, 'Egads to other scalar multiplication not equal') self.assertEqual(self.value1 / self.value2, egadstest1 / self.value2, 'Egads to other scalar division not equal') self.assertEqual(self.value1**self.value2, egadstest1**self.value2, 'Egads to other scalar power not equal')
def test_alg_set_other_units(self): """ Test sample algorithm modifying passed input units """ in_egads = egads.EgadsData(IN1, IN_UNITS1, {'long_name': LONG_NAME1}) out1 = self.single_set_units_alg.run(in_egads) self.assertEqual(out1, OUT1, "Single algorithm setting units value not equal") self.assertEqual( out1.units, IN_UNITS1 + '/s', "Single algorithm setting units output units not equal, returned {0}" .format(out1.units)) self.assertEqual( out1.metadata['long_name'], 'first derivative of ' + LONG_NAME1, 'Single algorithm setting units output long name not equal, returned {0}' .format(out1.metadata['long_name']))
def read_variable(self, varname, input_range=None): """ Reads in a variable from currently opened NetCDF file and maps the NetCDF attributies to an :class:`~egads.core.EgadsData` instance. :param string varname: Name of NetCDF variable to read in. :param vector input_range: Optional -- Range of values in each dimension to input. :TODO: add example :returns: Values and metadata of the specified variable in an EgadsData instance. :rtype: EgadsData """ try: varin = self.f.variables[varname] except KeyError: print "ERROR: Variable %s does not exist in %s" % (varname, self.filename) raise KeyError except Exception: print "Error: Unexpected error" raise if input_range is None: value = varin[:] else: obj = 'slice(input_range[0], input_range[1])' for i in xrange(2, len(input_range), 2): obj = obj + ', slice(input_range[%i], input_range[%i])' % ( i, i + 1) value = varin[eval(obj)] variable_attrs = self.get_attribute_list(varname) variable_attrs['cdf_name'] = varname variable_metadata = egads.core.metadata.VariableMetadata( variable_attrs, self.file_metadata) data = egads.EgadsData(value, variable_metadata=variable_metadata) return data
def test_call_copy(self): """ Testing copy of EgadsData using call to self """ value2 = egads.EgadsData(self.value1) self.assertEqual(self.value1.units, value2.units, 'Units do not match after assignment') assert_array_equal(self.value1.value, value2.value, 'Values do not match after assignment') value2 = value2.rescale('km') self.assertEqual(self.value1.units, 'm', ['Original units have changed to', self.value1.units]) assert_array_equal(self.value1.value, numpy.array([1, 2, 3]), 'Original array has changed') self.value1 = self.value1.rescale('cm') self.assertEqual(value2.units, 'km', 'New units have changed') assert_array_equal(value2.value, numpy.array([.001, .002, .003]), 'New array has changed')
def read_variable(self, varname, input_range=None): """ Reads in a variable from currently opened NetCDF file and maps the NetCDF attributies to an :class:`~egads.core.EgadsData` instance. :param string varname: Name of NetCDF variable to read in. :param vector input_range: Optional - Range of values in each dimension to input. """ logging.debug( 'egads - netcdf_io.py - EgadsNetCdf - read_variable - varname ' + str(varname) + ', input_range ' + str(input_range)) try: varin = self.f.variables[varname] except KeyError: logging.exception( 'egads - netcdf_io.py - EgadsNetCdf - read_variable - KeyError, variable does not exist in netcdf file' ) raise KeyError("ERROR: Variable %s does not exist in %s" % (varname, self.filename)) except Exception: logging.exception( 'egads - netcdf_io.py - EgadsNetCdf - read_variable - Exception, unexpected error' ) raise Exception("Error: Unexpected error") if input_range is None: value = varin[:] else: obj = 'slice(input_range[0], input_range[1])' for i in xrange(2, len(input_range), 2): obj = obj + ', slice(input_range[%i], input_range[%i])' % ( i, i + 1) value = varin[eval(obj)] variable_attrs = self.get_attribute_list(varname) variable_attrs['cdf_name'] = varname variable_metadata = egads.core.metadata.VariableMetadata( variable_attrs, self.file_metadata) data = egads.EgadsData(value, variable_metadata=variable_metadata) logging.debug( 'egads - netcdf_io.py - EgadsNetCdf - read_variable - varname ' + str(varname) + ' -> data read OK') return data
def setUp(self): self.sea_level = egads.EgadsData( value=[1.0, 2.0, 4.0, 5.0, 6.0, 8.0, 11.0, 12.0], units='mm', long_name='sea level anomalies') self.sea_level_nan = egads.EgadsData(value=[ nan, nan, 1.0, 2.0, nan, 4.0, 5.0, 6.0, nan, 8.0, nan, nan, 11.0, 12.0, nan, nan ], units='mm', long_name='sea level anomalies') self.sea_level_lite = egads.EgadsData( value=[2.0, 4.0, 5.0, 6.0, 8.0, 11.0], units='mm', long_name='sea level anomalies') self.time = egads.EgadsData( value=[2.0, 3.0, 5.0, 6.0, 7.0, 9.0, 12.0, 13.0], units='s', long_name='time') self.time_nan = egads.EgadsData(value=[ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0 ], units='s', long_name='time') self.time_lite = egads.EgadsData(value=[3.0, 5.0, 6.0, 7.0, 9.0, 12.0], units='s', long_name='time') self.new_time = egads.EgadsData(value=[ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0 ], units='s', long_name='time') self.interp_sea_level = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] self.complex_sea_level = [1.0, 1.6, 3.2, 4.8, 6.2, 7.6, 9.7, 11.0] self.array_test = numpy.zeros(8) + 10 self.array_test_nan = numpy.zeros(12) + 10 self.array_test_lite = numpy.zeros(6) + 10
def test_egads_to_other_calcs(self): """ Test vector operations between egads class and other vector""" egadstest1 = egads.EgadsData(self.value1, units='') add = egadstest1 + self.value2 subtract = egadstest1 - self.value2 multiply = egadstest1 * self.value2 divide = egadstest1 / self.value2 power = egadstest1**self.value2 assert_array_equal(self.value1 + self.value2, add.value, 'Egads to Egads vector addition not equal') assert_array_equal(self.value1 - self.value2, subtract.value, 'Egads to Egads vector subtraction not equal') assert_array_equal(self.value1 * self.value2, multiply.value, 'Egads to Egads vector multiplication not equal') assert_array_equal(self.value1 / self.value2, divide.value, 'Egads to Egads vector division not equal') assert_array_equal(self.value1**self.value2, power.value, 'Egads to Egads vector power not equal')
def test_other_to_egads_calcs(self): """ Test vector operations between other vector and egads class""" egadstest2 = egads.EgadsData(self.value2, units='') add = self.value1 + egadstest2 subtract = self.value1 - egadstest2 multiply = self.value1 * egadstest2 divide = self.value1 / egadstest2 power = self.value1**egadstest2 assert_array_equal(self.value1 + self.value2, add.value, 'Egads to Egads vector addition not equal') assert_array_equal(self.value1 - self.value2, subtract.value, 'Egads to Egads vector subtraction not equal') assert_array_equal(self.value1 * self.value2, multiply.value, 'Egads to Egads vector multiplication not equal') assert_array_equal(self.value1 / self.value2, divide.value, 'Egads to Egads vector division not equal') assert_array_equal(self.value1**self.value2, power.value, 'Egads to Egads vector power not equal')
def test_alg_pass_other_units(self): """ Test sample algorithm auto-conversion""" in_egads = egads.EgadsData(IN1 * 10, 'mm') out1 = self.single_alg.run(in_egads) self.assertEqual(out1, OUT1, "Single algorithm auto-conversion value not equal") self.assertEqual( out1.units, OUT_UNITS1, "Single algorithm auto-conversion output units not equal") self.assertEqual( out1.metadata.parent['Processor'], "TestAlgorithmSingleIO", "Single algorithm auto-conversion processor name does not match") self.assertEqual( out1.metadata.parent['ProcessorDate'], DATE, "Single algorithm auto-conversion processed date does not match") self.assertEqual( out1.metadata.parent['ProcessorVersion'], VERSION, "Single algorithm processor auto-conversion version does not match" )
def read_variable(self, varname, time_units=None): """ Read in variable from currently open NASA Ames file to :class: EgadsData object. Any additional variable metadata is additionally read in. :param string|int varname: String name or sequential number of variable to read in from currently open file. """ if isinstance(varname, int): varnum = varname else: var_list = self.get_variable_list() varnum = var_list.index(varname) (variable, units, miss, scale) = self.f.getVariable(varnum) variable_metadata = egads.core.metadata.VariableMetadata({'name':variable, 'units':units, '_FillValue':miss, 'scale_factor':scale}, self.file_metadata) convertor = nappy.nc_interface.na_to_cdms.NADictToCdmsObjects(self.f, variables=[varnum], time_warning=False, time_units=time_units) (cdms_primary, cdms_aux, global_attrs) = convertor.convert() na_data = cdms_primary[0] data = egads.EgadsData(na_data, variable_metadata) return data
def setUp(self): self.Ucapf = egads.EgadsData( value=[10], units='Hz', long_name='output frequency of capacitive probe') self.Racpa = egads.EgadsData( value=287.058 / 1003.5, units='', long_name='Air gas constant divided by specific heat cap') self.cpa = egads.EgadsData( value=1003.5, units='J/kg/K', long_name='specific heat of air at constant pressure') self.V_t = egads.EgadsData(value=200, units='m/s', long_name='true air speed') self.P_s = egads.EgadsData(value=[920], units='hPa', long_name='static pressure') self.P_sr = self.P_s self.T_t = egads.EgadsData(value=345, units='K', long_name='total temperature') self.dP = egads.EgadsData(value=[177.31], units='hPa', long_name='dynamic pressure') self.delta_P_r = self.dP.copy() self.delta_P_v = self.dP.copy() self.delta_P_v = self.delta_P_v * 0.1 self.delta_P_h = self.dP.copy() self.delta_P_h = self.delta_P_h * 0.15 self.r = egads.EgadsData(value=1e-3, units='g/kg', long_name='water vapor mixing ratio') self.T_s = egads.EgadsData(value=[298.15], units='K', long_name='static temperature') self.T_v = egads.EgadsData(value=[29], units='K', long_name='virtual temperature') self.theta = egads.EgadsData(value=288.76752, units='K', long_name='potential temperature') self.P_surface = egads.EgadsData(value=[1013.25], units='hPa', long_name='surface pressure') self.R_a_g = egads.EgadsData( value=[287.058 / 9.8], units='m/K', long_name='air gas constant divided by gravity') self.M = egads.EgadsData(0.5081, units='', long_name='mach number') self.e = egads.EgadsData(1, units='', long_name='thermometer recovery factor') self.C_t = egads.EgadsData(value=1, units='%/degC', long_name='temperature correction coeff') self.Fmin = egads.EgadsData(value=2, units='Hz', long_name='minimum acceptible frequency') self.C_0 = egads.EgadsData(value=0.5, units='', long_name='0th calibration coeff') self.C_1 = egads.EgadsData(value=0.6, units='', long_name='1st calibration coeff') self.C_2 = egads.EgadsData(value=0.5, units='', long_name='2nd calibration coeff') self.C_alpha = egads.EgadsData(value=[1, 0.9], units='', long_name='angle of attack calibration') self.C_beta = egads.EgadsData(value=[0.8, 0.9], units='', long_name='sideslip calibration') self.C_errstat = egads.EgadsData(value=[0.01, 0.009, 0.0001, 0.00008], units='', long_name='static error coeffs') self.r_f = egads.EgadsData(value=0.5, units='', long_name='probe recovery factor') self.alpha = egads.EgadsData(value=0.01, units='rad', long_name='angle of attack') self.beta = egads.EgadsData(value=0.005, units='rad', long_name='sideslip') self.array_test = numpy.zeros(10) + 10 self.array_shape = self.array_test.shape self.coeff_test = 1 self.coeff2_test = [1, 1] self.coeff4_test = [1, 1, 1, 1]
def test_egads_vector_assignment(self): """ Test initialization of EgadsData instance with vector input """ egadstest = egads.EgadsData(self.value1) assert_array_equal(self.value1, egadstest.value, 'Vector assignment not equal')
def setUp(self): self.value1 = egads.EgadsData([1, 2, 3], units='m')
#!/usr/bin/env python # -*- coding: utf-8 -*- import egads data1 = egads.EgadsData(value=[5.0, 2.0, -2.0, 0.5, 4.0], units='mm', name='sea level', scale_factor=1, _FillValue=-9999) data2 = egads.EgadsData(value=[1.0, 3.0, -1.0, 2.5, 6.0], units='mm', name='corr sea level', scale_factor=1, _FillValue=-9999) time = egads.EgadsData(value=[1.0, 2.0, 3.0, 4.0, 5.0], units='seconds since 19700101T00:00:00', name='time') filename = "na_example_file.na" f = egads.input.NasaAmes() na_dict = f.create_na_dict() scom = [ '========SPECIAL COMMENTS===========', 'this file has been created with egads', '=========END=========' ] ncom = [
def setUp(self): self.C1 = egads.EgadsData(value=[[11,10.5,20.21], [33,150,20]], units='cm^-3', long_name='number concentration of hydrometeors') self.C2 = egads.EgadsData(value=[[11,10, 20], [33,150,20]], units='', long_name='number concentration of hydrometeors') self.C3 = egads.EgadsData(value=[[0.01,0,0.0001], [0.0,3.1,2.0]], units='cm^-3', long_name='number concentration of hydrometeors') self.C4 = egads.EgadsData(value=[[1.0,0.03,12.0], [0.0,3.0,12.0]], units='cm^-3', long_name='number concentration of hydrometeors') self.C5 = egads.EgadsData(value=[[0.3,0.2,0.5], [0.7,0.6,0.1]], units='cm^-3', long_name='number concentration of hydrometeors') self.D1 = egads.EgadsData(value=[1,1.2,3], units='um', long_name='average diameter') self.D2 = egads.EgadsData(value=[0.1,1.0,2.0], units='um', long_name='average diameter') self.D3 = egads.EgadsData(value=[0.3,0.5,0.1], units='um', long_name='average diameter') self.S1 = egads.EgadsData(value=[[1,0.5,3], [1,0.9,1.1]], units='', long_name='shape factors') self.S2 = egads.EgadsData(value=[[1.1,1.2,1], [0.9,1.1,0.98]], units='', long_name='shape factors') self.De1 = egads.EgadsData(value=[0.01,2,7], units='g/cm^3', long_name='density') self.De2 = egads.EgadsData(value=[0.4,11,0.2], units='g/cm^3', long_name='density') self.E1 = egads.EgadsData(value=[2,5,1], units='', long_name='extinction efficiency') self.P1 = egads.EgadsData(value=[[3.0,10.0,2.0,1.0], [7.0,2.0,3.0,1.0]], units='', long_name='number of particles') self.V1 = egads.EgadsData(value=[3.0,2.0,1.0,1.0], units='m^3', long_name='sample volume')