Skip to content

Commit

Permalink
Add handling for submission of subannual/hourly timeseries data
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhuppmann committed Jun 20, 2024
1 parent 6e46e87 commit 7979fe2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
4 changes: 4 additions & 0 deletions definitions/subannual/year.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Default entry for the subannual column

- Year:
duration: 1
38 changes: 35 additions & 3 deletions workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,39 @@
def main(df: pyam.IamDataFrame) -> pyam.IamDataFrame:
"""Project/instance-specific workflow for scenario processing"""

# Run the validation and region-processing
dsd = DataStructureDefinition(here / "definitions")
dimensions = ["region", "variable"]
if "subannual" in df.dimensions or df.time_col == "time":
dimensions = dimensions + ["subannual"]

# initialize the codelists and region-processing
dsd = DataStructureDefinition(here / "definitions", dimensions=dimensions)
processor = RegionProcessor.from_directory(path=here / "mappings", dsd=dsd)
return process(df, dsd, processor=processor)

# run the validation and region-processing
df = process(df, dsd, processor=processor)

# convert to subannual format if data provided in datetime format
if df.time_col == "time":
logger.info('Re-casting from "time" column to categorical "subannual" format')
df = df.swap_time_for_year(subannual=OE_SUBANNUAL_FORMAT)

# check that any datetime-like items in "subannual" are valid datetime and UTC+01:00
if "subannual" in df.dimensions:
_datetime = [s for s in df.subannual if s not in dsd.subannual]

for d in _datetime:
try:
_dt = datetime.strptime(f"2020-{d}", "%Y-%m-%d %H:%M%z")
except ValueError:
try:
datetime.strptime(f"2020-{d}", "%Y-%m-%d %H:%M")
except ValueError:
raise ValueError(f"Invalid subannual timeslice: {d}")

raise ValueError(f"Missing timezone: {d}")

# casting to datetime with timezone was successful
if not (_dt.tzname() == EXP_TZ or _dt.utcoffset() == EXP_TIME_OFFSET):
raise ValueError(f"Invalid timezone: {d}")

return df

0 comments on commit 7979fe2

Please sign in to comment.