def test_nan(self): """Accept and return not-a-number, do not mess up finite values""" SA = np.array([34.0, np.nan, 35.0]) t = 10.0 p = 0 rho = gsw.rho(SA, t, p) rho0 = gsw.rho(SA[0], t, p) # Should return NaN for NaN self.assertTrue(np.isnan(rho[1])) # Should return correct value for not NaN self.assertEqual(rho[0], rho0)
def test_masked(self): """Accept and return correctly masked arrays""" SA = np.array([33, 34, -99, 35]) # One salinity value missing t = [18, 17, 12, 8] p = [0, 10, 50, 100] SA = np.ma.masked_where(SA < 0, SA) # Make masked array rho = gsw.rho(SA, t, p) rho0 = gsw.rho(SA[0], t[0], p[0]) # Return array should have the same mask self.assertTrue(np.all(rho.mask == SA.mask)) # Correct value for non-masked entries self.assertEqual(rho[0], rho0)
def check_density_algorithm_execution(self, publish_granule, granule_from_transform): #------------------------------------------------------------------ # Calculate the correct density from the input granule data #------------------------------------------------------------------ input_rdt_to_transform = RecordDictionaryTool.load_from_granule( publish_granule) output_rdt_transform = RecordDictionaryTool.load_from_granule( granule_from_transform) conductivity = input_rdt_to_transform['conductivity'] pressure = input_rdt_to_transform['pressure'] temperature = input_rdt_to_transform['temp'] longitude = input_rdt_to_transform['lon'] latitude = input_rdt_to_transform['lat'] sp = SP_from_cndr(r=conductivity / cte.C3515, t=temperature, p=pressure) sa = SA_from_SP(sp, pressure, longitude, latitude) dens_value = rho(sa, temperature, pressure) out_density = output_rdt_transform['density'] log.debug("density output from the transform: %s", out_density) log.debug("values of density expected from the transform: %s", dens_value) numpy.testing.assert_array_almost_equal(out_density, dens_value, decimal=3)
def execute(input=None, context=None, config=None, params=None, state=None): rdt = RecordDictionaryTool.load_from_granule(input) out_rdt = RecordDictionaryTool(stream_definition_id=params) conductivity = rdt['conductivity'] pressure = rdt['pressure'] temperature = rdt['temp'] longitude = rdt['lon'] if rdt['lon'] is not None else 0 latitude = rdt['lat'] if rdt['lat'] is not None else 0 sp = SP_from_cndr(r=conductivity/cte.C3515, t=temperature, p=pressure) log.debug("Density algorithm calculated the sp (practical salinity) values: %s", sp) sa = SA_from_SP(sp, pressure, longitude, latitude) log.debug("Density algorithm calculated the sa (actual salinity) values: %s", sa) dens_value = rho(sa, temperature, pressure) for key, value in rdt.iteritems(): if key in out_rdt: if key=='conductivity' or key=='temp' or key=='pressure': continue out_rdt[key] = value[:] out_rdt['density'] = dens_value log.debug("Density algorithm returning density values: %s", out_rdt['density']) return out_rdt.to_granule()
def test_list_input(self): """Lists may be used instead of arrays on input""" # Test 1D SA = [30,32,34,36] t = 10.0 p = [0, 100, 1000, 4000] rho = gsw.rho(SA, t, p) self.assertTrue(rho.shape == (4,)) # Test 2D SA = [30,32,34,36] t = [[2,4,6,8], [12,14,16,18]] p = 100.0 rho = gsw.rho(SA, t, p) self.assertTrue(rho.shape == (2,4))
def check_density_algorithm_execution(self, publish_granule, granule_from_transform): #------------------------------------------------------------------ # Calculate the correct density from the input granule data #------------------------------------------------------------------ input_rdt_to_transform = RecordDictionaryTool.load_from_granule(publish_granule) output_rdt_transform = RecordDictionaryTool.load_from_granule(granule_from_transform) conductivity = input_rdt_to_transform['conductivity'] pressure = input_rdt_to_transform['pressure'] temperature = input_rdt_to_transform['temp'] longitude = input_rdt_to_transform['lon'] latitude = input_rdt_to_transform['lat'] sp = SP_from_cndr(r=conductivity/cte.C3515, t=temperature, p=pressure) sa = SA_from_SP(sp, pressure, longitude, latitude) dens_value = rho(sa, temperature, pressure) out_density = output_rdt_transform['density'] #----------------------------------------------------------------------------- # Check that the output data from the transform has the correct density values #----------------------------------------------------------------------------- self.assertTrue(dens_value.all() == out_density.all())
def execute(input=None, context=None, config=None, params=None, state=None): rdt = RecordDictionaryTool.load_from_granule(input) out_rdt = RecordDictionaryTool(stream_definition_id=params) conductivity = rdt["conductivity"] pressure = rdt["pressure"] temperature = rdt["temp"] longitude = rdt["lon"] if rdt["lon"] is not None else 0 latitude = rdt["lat"] if rdt["lat"] is not None else 0 sp = SP_from_cndr(r=conductivity / cte.C3515, t=temperature, p=pressure) log.debug("Density algorithm calculated the sp (practical salinity) values: %s", sp) sa = SA_from_SP(sp, pressure, longitude, latitude) log.debug("Density algorithm calculated the sa (actual salinity) values: %s", sa) dens_value = rho(sa, temperature, pressure) for key, value in rdt.iteritems(): if key in out_rdt: if key == "conductivity" or key == "temp" or key == "pressure": continue out_rdt[key] = value[:] out_rdt["density"] = dens_value log.debug("Density algorithm returning density values: %s", out_rdt["density"]) return out_rdt.to_granule()
def check_density_algorithm_execution(self, publish_granule, granule_from_transform): #------------------------------------------------------------------ # Calculate the correct density from the input granule data #------------------------------------------------------------------ input_rdt_to_transform = RecordDictionaryTool.load_from_granule(publish_granule) output_rdt_transform = RecordDictionaryTool.load_from_granule(granule_from_transform) conductivity = input_rdt_to_transform['conductivity'] pressure = input_rdt_to_transform['pressure'] temperature = input_rdt_to_transform['temp'] longitude = input_rdt_to_transform['lon'] latitude = input_rdt_to_transform['lat'] sp = SP_from_cndr(r=conductivity/cte.C3515, t=temperature, p=pressure) sa = SA_from_SP(sp, pressure, longitude, latitude) dens_value = rho(sa, temperature, pressure) out_density = output_rdt_transform['density'] log.debug("density output from the transform: %s", out_density) log.debug("values of density expected from the transform: %s", dens_value) numpy.testing.assert_array_almost_equal(out_density, dens_value, decimal=3)
def test_scalar_input(self): """Accept scalar input, return a scalar""" SA = 35.0 t = 10.0 p = 1000.0 rho = gsw.rho(SA, t, p) self.assertTrue(np.isscalar(rho))
def execute(self, granule): """Processes incoming data!!!! """ rdt = RecordDictionaryTool.load_from_granule(granule) #todo: use only flat dicts for now, may change later... # rdt0 = rdt['coordinates'] # rdt1 = rdt['data'] temperature = get_safe(rdt, 'temp') conductivity = get_safe(rdt, 'cond') density = get_safe(rdt, 'dens') longitude = get_safe(rdt, 'lon') latitude = get_safe(rdt, 'lat') time = get_safe(rdt, 'time') height = get_safe(rdt, 'height') log.warn('Got conductivity: %s' % str(conductivity)) log.warn('Got density: %s' % str(density)) log.warn('Got temperature: %s' % str(temperature)) sp = SP_from_cndr(r=conductivity/cte.C3515, t=temperature, p=density) sa = SA_from_SP(sp, density, longitude, latitude) density = rho(sa, temperature, density) log.warn('Got density: %s' % str(density)) # Use the constructor to put data into a granule #psc = PointSupplementConstructor(point_definition=self.outgoing_stream_def, stream_id=self.streams['output']) ### Assumes the config argument for output streams is known and there is only one 'output'. ### the stream id is part of the metadata which much go in each stream granule - this is awkward to do at the ### application level like this! root_rdt = RecordDictionaryTool(param_dictionary=self.dens) #todo: use only flat dicts for now, may change later... # data_rdt = RecordDictionaryTool(taxonomy=self.tx) # coord_rdt = RecordDictionaryTool(taxonomy=self.tx) root_rdt['density'] = density root_rdt['time'] = time root_rdt['lat'] = latitude root_rdt['lon'] = longitude root_rdt['height'] = height # root_rdt['coordinates'] = coord_rdt # root_rdt['data'] = data_rdt return build_granule(data_producer_id='ctd_L2_density', param_dictionary=self.dens, record_dictionary=root_rdt)
def test_array_input(self): """Accept array input, return broadcasted shape""" # Test 1D SA = np.array([30,32,34,36]) t = 10.0 p = np.array([0, 100, 1000, 4000]) rho = gsw.rho(SA, t, p) self.assertTrue(rho.shape == np.broadcast(SA,t,p).shape) # Test 2D SA = np.array([30,32,34,36]) t = np.array([[2,4,6,8], [12,14,16,18]]) p = np.array([100.0]) rho = gsw.rho(SA, t, p) self.assertTrue(rho.shape == np.broadcast(SA,t,p).shape) # Test 3D SA = 30 + np.linspace(0,1,24).reshape((2,3,4)) t = 18.0 p = 0 rho = gsw.rho(SA, t, p) self.assertTrue(rho.shape == np.broadcast(SA,t,p).shape)
def execute(self, granule): """Processes incoming data!!!! """ # Use the deconstructor to pull data from a granule psd = PointSupplementStreamParser( stream_definition=self.incoming_stream_def, stream_granule=granule) conductivity = psd.get_values('conductivity') pressure = psd.get_values('pressure') temperature = psd.get_values('temperature') longitude = psd.get_values('longitude') latitude = psd.get_values('latitude') height = psd.get_values('height') time = psd.get_values('time') log.warn('Got conductivity: %s' % str(conductivity)) log.warn('Got pressure: %s' % str(pressure)) log.warn('Got temperature: %s' % str(temperature)) sp = SP_from_cndr(r=conductivity / cte.C3515, t=temperature, p=pressure) sa = SA_from_SP(sp, pressure, longitude, latitude) density = rho(sa, temperature, pressure) log.warn('Got density: %s' % str(density)) # Use the constructor to put data into a granule psc = PointSupplementConstructor( point_definition=self.outgoing_stream_def, stream_id=self.streams['output']) ### Assumes the config argument for output streams is known and there is only one 'output'. ### the stream id is part of the metadata which much go in each stream granule - this is awkward to do at the ### application level like this! for i in xrange(len(density)): point_id = psc.add_point(time=time[i], location=(longitude[i], latitude[i], height[i])) psc.add_scalar_point_coverage(point_id=point_id, coverage_id='density', value=density[i]) return psc.close_stream_granule()
def test_correct(self): r"""Test that the computations give correct answer Standard values from http://www.teos-10.org/pubs/gsw/html/gsw_rho.html """ SA = [34.7118, 34.8915, 35.0256, 34.8472, 34.7366, 34.7324] t = [28.7856, 28.4329, 22.8103, 10.2600, 6.8863, 4.4036] p = [10, 50, 125, 250, 600, 1000] rho_official = np.array((1021.840173185531, 1022.262689926782, 1024.427715941676, 1027.790201811623, 1029.837714725961, 1032.002404116447)) rho = gsw.rho(SA, t, p) self.assertTrue(np.all(abs(rho-rho_official) < 1.0e-12))
def execute(self, granule): """Processes incoming data!!!! """ # Use the deconstructor to pull data from a granule psd = PointSupplementStreamParser(stream_definition=self.incoming_stream_def, stream_granule=granule) conductivity = psd.get_values('conductivity') pressure = psd.get_values('pressure') temperature = psd.get_values('temperature') longitude = psd.get_values('longitude') latitude = psd.get_values('latitude') height = psd.get_values('height') time = psd.get_values('time') log.warn('Got conductivity: %s' % str(conductivity)) log.warn('Got pressure: %s' % str(pressure)) log.warn('Got temperature: %s' % str(temperature)) sp = SP_from_cndr(r=conductivity/cte.C3515, t=temperature, p=pressure) sa = SA_from_SP(sp, pressure, longitude, latitude) density = rho(sa, temperature, pressure) log.warn('Got density: %s' % str(density)) # Use the constructor to put data into a granule psc = PointSupplementConstructor(point_definition=self.outgoing_stream_def, stream_id=self.streams['output']) ### Assumes the config argument for output streams is known and there is only one 'output'. ### the stream id is part of the metadata which much go in each stream granule - this is awkward to do at the ### application level like this! for i in xrange(len(density)): point_id = psc.add_point(time=time[i],location=(longitude[i],latitude[i],height[i])) psc.add_scalar_point_coverage(point_id=point_id, coverage_id='density', value=density[i]) return psc.close_stream_granule()
def execute(input=None, context=None, config=None, params=None, state=None): """ Dependencies ------------ PRACSAL, PRESWAT_L1, longitude, latitude, TEMPWAT_L1 Algorithms used ------------ 1. PRACSAL = gsw_SP_from_C((CONDWAT_L1 * 10),TEMPWAT_L1,PRESWAT_L1) 2. absolute_salinity = gsw_SA_from_SP(PRACSAL,PRESWAT_L1,longitude,latitude) 3. conservative_temperature = gsw_CT_from_t(absolute_salinity,TEMPWAT_L1,PRESWAT_L1) 4. DENSITY = gsw_rho(absolute_salinity,conservative_temperature,PRESWAT_L1) Reference ------------ The calculations below are based on the following spreadsheet document: https://docs.google.com/spreadsheet/ccc?key=0Au7PUzWoCKU4dDRMeVI0RU9yY180Z0Y5U0hyMUZERmc#gid=0 """ lat = params['lat'] lon = params['lon'] stream_def_id = params['stream_def'] rdt = RecordDictionaryTool.load_from_granule(input) out_rdt = RecordDictionaryTool(stream_definition_id=stream_def_id) out_rdt['time'] = rdt['time'] conductivity = rdt['conductivity'] pressure = rdt['pressure'] temperature = rdt['temp'] log.debug('L2 transform using L1 values: temp %s, pressure %s, conductivity %s', temperature, pressure, conductivity) latitude = np.ones(conductivity.shape) * lat longitude = np.ones(conductivity.shape) * lon log.debug("Using latitude: %s, longitude: %s", latitude, longitude) # Doing: PRACSAL = gsw_SP_from_C((CONDWAT_L1 * 10),TEMPWAT_L1,PRESWAT_L1) pracsal = gsw.sp_from_c(conductivity * 10, temperature, pressure) old_pracsal = SP_from_cndr(conductivity * 10, t=temperature, p=pressure) log.debug("CTDBP Density algorithm calculated the pracsal (practical salinity) values: %s (old: %s)", pracsal, old_pracsal) # Doing: absolute_salinity = gsw_SA_from_SP(PRACSAL,PRESWAT_L1,longitude,latitude) absolute_salinity = gsw.sa_from_sp(pracsal, pressure, longitude, latitude) old_absolute_salinity = SA_from_SP(old_pracsal, pressure, longitude, latitude) log.debug('absolute_salinity = SA_from_SP(pracsal=%s, pressure=%s, longitude=%s, latitude=%s)=%s (old value: %s)', pracsal, pressure, longitude, latitude, absolute_salinity, old_absolute_salinity) log.debug("CTDBP Density algorithm calculated the absolute_salinity (actual salinity) values: %s", absolute_salinity) conservative_temperature = gsw.ct_from_t(absolute_salinity, temperature, pressure) old_conservative_temperature = conservative_t(old_absolute_salinity, temperature, pressure) log.debug("CTDBP Density algorithm calculated the conservative temperature values: %s (old value: %s)", conservative_temperature, old_conservative_temperature) # Doing: DENSITY = gsw_rho(absolute_salinity,conservative_temperature,PRESWAT_L1) dens_value = gsw.rho(absolute_salinity, conservative_temperature, pressure) old_dens_value = rho(old_absolute_salinity, old_conservative_temperature, pressure) log.debug("Calculated density values: %s (old: %s)", dens_value, old_dens_value) for key, value in rdt.iteritems(): if key in out_rdt: if key=='conductivity' or key=='temp' or key=='pressure': continue out_rdt[key] = value[:] out_rdt['density'] = dens_value return out_rdt.to_granule()
def parseScience(gliderTbdDir, fileType): """ Module: parseScience() Date: 2012-11-27 Author: [email protected] Modified: 2013-10-15 By: [email protected] Inputs: None Outputs: Inserts into SQLite database file Purpose: Grabs a db cursor and calls dbd2asc for all non-parsed tbd files. Looks for 'sci_' and splits sensor list. Finds pos of sensors of interest so list can change if tbdlist.dat changes. Uses pos of SOI to store values in correct field. """ cur = gliderlog.cursor() curParsed = gliderlog.cursor() sbdParsedStructure = 0 # #Regex for lines beginning with m_present_time #m_present_time structure: '1277271054.36734 pattMPresentTime = re.compile(r'1[0-9]{9}\.[0-9]{5}') # select = "select %sfile from %sFiles where parsed = 0 \ ORDER BY %sfile ASC" % (fileType, fileType, fileType) cur.execute(select) for(tbdFile) in cur: if DEBUG > 0: print "Running dbd2asc on %s/%s" % (gliderTbdDir, tbdFile[0]) command = "/opt/dinkum/dbd2asc -c /opt/glider_data_pipeline/cache \ %s/%s" % (gliderTbdDir, tbdFile[0]) p = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True) (sIn, sOut) = (p.stdin, p.stdout) myBuffer = sOut.xreadlines() # curInsert = gliderlog.cursor() curInsert.execute('begin') for (line) in myBuffer: #2011-06-02 rdc changed to sci_ to reflect new tbd file format #Look to see if line starts with "sci_" -- assume that a list #of sensors follows. Split into sensors, create tbdSensors #table and store values in tbdSensorReadings. if (line.startswith("sci_")): #we need to split and create here, not print sensorList = line.split() if (DEBUG > 1): print "sensorList: %s" % (sensorList) if DEBUG > 1: for (sensor) in sensorList: print "Factored %s" % (sensor) #Get order of sensors m_present_timePos = sensorList.index('sci_m_present_time') sci_water_tempPos = sensorList.index('sci_water_temp') sci_water_condPos = sensorList.index('sci_water_cond') sci_water_pressurePos = sensorList.index('sci_water_pressure') sci_bbfl2s_chlor_scaledPos = sensorList.index('sci_bbfl2s_chlor_scaled') if pattMPresentTime.search(line): index = 0 matchobj = pattMPresentTime.search(line) if matchobj: if DEBUG > 1: print "MATCH ON pattMPresentTime" print "m_present_time: %s" % matchobj.group(0) m_present_time = float(matchobj.group(0)) line = line.replace('NaN','nan') sensorReadings = line.split() if DEBUG > 1: print sensorReadings sci_water_cond = float(sensorReadings[sci_water_condPos]) sci_water_temp = float(sensorReadings[sci_water_tempPos]) sci_water_pressure = float(sensorReadings[sci_water_pressurePos]) sci_water_condRatio = (sci_water_cond*0.23302418791070513) #Salinity and density calculations and db update # if (sci_water_condRatio > 0 and sci_water_temp > 0 and \ sci_water_pressure > 0): calc_salinity = sw.salt(sci_water_condRatio, \ sci_water_temp, sci_water_pressure) #get most recent position #[TO DO] Fudge it for now -- add to plotValues lon = -82 lat = 27 absSalinty = gsw.SA_Sstar_from_SP(calc_salinity, \ sci_water_pressure, lon, lat)[0] calc_density = (gsw.rho(absSalinty, sci_water_temp, \ sci_water_pressure) - 1000) else: sci_water_condRatio = 'nan' calc_salinity = 'nan' calc_density = 'nan' absSalinty = 'nan' curInsert.execute("INSERT INTO plotValues(m_present_time,\ m_depth, m_water_depth, sci_water_temp, sci_water_cond, sci_water_pressure,\ calc_salinity, absSalinty, calc_density,sci_bbfl2s_chlor_scaled, sci_moteopd_corr1,\ sci_moteopd_corr2,sci_moteopd_corr3,sci_moteopd_corr4,sci_moteopd_corr5,\ sci_moteopd_corr6,sci_moteopd_corr7,sci_moteopd_corr8,sci_moteopd_corr9,\ sci_moteopd_corr10,sci_moteopd_corr11,m_avg_climb_rate,m_avg_dive_rate,m_lat, \ m_lon, m_tot_horz_dist, m_avg_speed,sci_moteopd_volt) \ VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", \ (sensorReadings[m_present_timePos],'nan','nan',\ sensorReadings[sci_water_tempPos],sci_water_condRatio,\ sensorReadings[sci_water_pressurePos],calc_salinity, \ absSalinty, calc_density,sensorReadings[sci_bbfl2s_chlor_scaledPos],0,0,0,0,0,0,0,0,0,0,0 \ ,'nan','nan','nan','nan','nan','nan',0)) curInsert.execute('commit') curInsert.close() select = "UPDATE %sFiles set parsed = '1' where %sfile = '%s'" \ % (fileType, fileType, tbdFile[0]) curParsed.execute(select) curParsed.close() cur.close()
#test_print("thermobaric_CT25") #isopycnal_slope_ratio_CT25 = gsw.isopycnal_slope_ratio_CT25(SA_chck_cast, CT_chck_cast, gsw_cv.p_chck_cast, gsw_cv.pr) #test_print("isopycnal_slope_ratio_CT25") #G_CT_CT25, p_mid_G_CT_CT25 = gsw.isopycnal_vs_ntp_CT_ratio_CT25(SA_chck_cast, CT_chck_cast, gsw_cv.p_chck_cast, gsw_cv.pr) #test_print("G_CT_CT25") #test_print("p_mid_G_CT_CT25") #ntpptCT_CT25 = gsw.ntp_pt_vs_CT_ratio_CT25(SA_chck_cast, CT_chck_cast, gsw_cv.p_chck_cast) #test_print("ntpptCT_CT25") """ basic thermodynamic properties """ gsw_cv.p_chck_cast = np.reshape( gsw_cv.p_chck_cast, gsw_cv.z_from_p.shape ) rho = gsw.rho(SA_chck_cast, gsw_cv.t_chck_cast, gsw_cv.p_chck_cast) test_print("rho") pot_rho = gsw.pot_rho(SA_chck_cast, gsw_cv.t_chck_cast, gsw_cv.p_chck_cast, gsw_cv.pr) test_print("pot_rho") specvol = gsw.specvol(SA_chck_cast, gsw_cv.t_chck_cast, gsw_cv.p_chck_cast) test_print("specvol") specvol_anom = gsw.specvol_anom(SA_chck_cast, gsw_cv.t_chck_cast, gsw_cv.p_chck_cast) test_print("specvol_anom") alpha_wrt_CT = gsw.alpha_wrt_CT(SA_chck_cast, gsw_cv.t_chck_cast, gsw_cv.p_chck_cast) test_print("alpha_wrt_CT") alpha_wrt_pt = gsw.alpha_wrt_pt(SA_chck_cast, gsw_cv.t_chck_cast, gsw_cv.p_chck_cast)