Skip to content

Commit

Permalink
separate convert_daily_to_annual() from standardize_units()
Browse files Browse the repository at this point in the history
  • Loading branch information
bl-young committed Nov 2, 2022
1 parent 5bb98b3 commit 651b62f
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions flowsa/flowby.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,17 @@ def _getFlowBy(
fb = cls(df, full_name=full_name or '', config=config or {})
return fb

def convert_daily_to_annual(self: FB) -> FB:
if any(self.Unit.str.contains('/d')):
log.info('Converting daily flows %s to annual',
[unit for unit in self.Unit.unique() if '/d' in unit])
return (
self
.assign(FlowAmount=self.FlowAmount.mask(
self.Unit.str.contains('/d'), self.FlowAmount * 365),
Unit=self.Unit.str.replace('/d', ''))
)

def standardize_units(self: FB) -> FB:
exchange_rate = (
literature_values
Expand All @@ -257,16 +268,9 @@ def standardize_units(self: FB) -> FB:
'conversion_factor': 1 / exchange_rate}).to_frame().T
])

if any(self.Unit.str.contains('/d')):
log.info('Converting daily flows %s to annual',
[unit for unit in self.Unit.unique() if '/d' in unit])

standardized = (
self
.assign(Unit=self.Unit.str.strip())
.assign(FlowAmount=self.FlowAmount.mask(
self.Unit.str.contains('/d'), self.FlowAmount * 365),
Unit=self.Unit.str.replace('/d', ''))
.merge(conversion_table, how='left',
left_on='Unit', right_on='old_unit')
.assign(Unit=lambda x: x.new_unit.mask(x.new_unit.isna(), x.Unit),
Expand Down Expand Up @@ -1468,12 +1472,13 @@ def convert_units_and_flows(
self = self.assign(FlowAmount=self.FlowAmount
* self.config['adjustment_factor'])

standardized = self.standardize_units()
self = self.convert_daily_to_annual()
if self.config.get('fedefl_mapping'):
return standardized.map_to_fedefl_list()
mapped = self.map_to_fedefl_list()
else:
return standardized.rename(columns={'FlowName': 'Flowable',
'Compartment': 'Context'})
mapped = self.rename(columns={'FlowName': 'Flowable',
'Compartment': 'Context'})
return (mapped.standardize_units())

def convert_activity_to_emissions(
self: 'FlowByActivity'
Expand Down

2 comments on commit 651b62f

@bl-young
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matthewlchambers @catherinebirney this should convert daily to annual first prior to mapping, then standardizing units. I didn't see any reason why standardize units would be problematic if the dataframe contains Flowable and Context but let me know if you have issues

@catherinebirney
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bl-young - thanks, this solved the water fbs issue! Not having any unit-related problems for employment fbs (which skips fed mapping)

Please sign in to comment.