def get_oil_densities_at_5c(oil_columns, field_indexes, weathering): densities = [] props = get_oil_properties_by_category(oil_columns, field_indexes, 'density_at_0_5_c_g_ml_astm_d5002') prop_names = list(props.keys()) for idx, vals in enumerate(zip(*list(props.values()))): density_obj = dict(list(zip(prop_names, [v[0].value for v in vals]))) # add some properties to the oil that we expect density_obj['idx'] = idx density_obj['weathering'] = weathering[idx] density_obj['ref_temp_k'] = 273.15 + 5.0 density_obj['kg_m_3'] = density_obj['density_5_c_g_ml'] if density_obj['kg_m_3'] is not None: density_obj['kg_m_3'] *= 1000.0 # prune some properties that we don't want in our object del density_obj['density_0_c_g_ml'] del density_obj['density_5_c_g_ml'] densities.append(density_obj) return [Density(**d) for d in densities if d['kg_m_3'] not in (None, 0.0)]
def test_add_density_to_oil(self): oil_obj = ImportedRecord(**OilTestCase.get_mock_oil_file_record()) density_obj = \ Density(**DensityTestCase.get_mock_density_file_record()) oil_obj.densities.append(density_obj) self.add_objs_and_assert_ids([oil_obj, density_obj]) assert oil_obj.densities == [density_obj] assert density_obj.imported == oil_obj
def add_densities(oil, row_dict): for i in range(1, 5): obj_args = ('kg_m_3', 'ref_temp_k', 'weathering') row_fields = ['density{0}_{1}'.format(i, a) for a in obj_args] if any([row_dict.get(k) for k in row_fields]): densityargs = {} for col, arg in zip(row_fields, obj_args): densityargs[arg] = row_dict.get(col) fix_weathering(densityargs) oil.densities.append(Density(**densityargs))
def add_densities(imported_rec, oil): ''' Rules: - If no density value exists, estimate it from the API. So at the end, we will always have at least one density at 15 degrees Celsius. - If a density measurement at some temperature exists, but no API, then we estimate API from density. So at the end, we will always have an API value. - In both the previous cases, we have estimated the corollary values and ensured that they are consistent. But if a record contains both an API and a number of densities, these values may conflict. In this case, we will reject the creation of the oil record. - This is not in the document, but Bill & Chris have verbally stated they would like there to always be a 15C density value. ''' for d in imported_rec.densities: if d.kg_m_3 is not None: oil.densities.append(d) if imported_rec.api is not None: oil.api = imported_rec.api elif oil.densities: # estimate our api from density d_0 = density_at_temperature(oil, 273.15 + 15) oil.api = (141.5 * 1000 / d_0) - 131.5 # oil.estimated.api = True else: print('Warning: no densities and no api for record {0}'.format( imported_rec.adios_oil_id)) if not [ d for d in oil.densities if (d.ref_temp_k is not None and np.isclose( d.ref_temp_k, 273.0 + 15, atol=.15)) ]: # add a 15C density from api kg_m_3, ref_temp_k = estimate_density_from_api(oil.api) oil.densities.append( Density(kg_m_3=kg_m_3, ref_temp_k=ref_temp_k, weathering=0.0))
def test_init_with_args(self): density_obj = Density(**self.get_mock_density_file_record()) self.assert_mock_density_object(density_obj) self.add_objs_and_assert_ids(density_obj)
def test_init_no_args(self): density_obj = Density() self.add_objs_and_assert_ids(density_obj)