Commit 4c1ee107 authored by Bruno Duyé's avatar Bruno Duyé

Make time periods compatible with DB.nomics time period format

parent 9954918a
......@@ -39,6 +39,7 @@ Options:
import logging
import os
import re
import subprocess
import sys
from collections import defaultdict
......@@ -232,14 +233,30 @@ def treat_dataset(dataset_dict, appendix_path, appendix_dict):
# Prepare value
observation_value_str = str(numbers.parse_decimal(observation_dict["DataValue"], locale='en_us'))
# Add value to observations list
if "TimePeriod" in observation_dict:
time_period = observation_dict["TimePeriod"]
else:
if dataset_dict["dimension_code"] == "FREQ" and dataset_source_file_info["dimension_value_code"] == 'Q':
# Quarterly time period, devised in two fields and in roman naturals notation
time_period = "{}Q{}".format(observation_dict['Year'], roman_numerals_to_int(observation_dict['Quarter']))
if dataset_dict.get("dimension_code"):
assert dataset_dict["dimension_code"] == "FREQ", "Unhandled case"
if dataset_source_file_info["dimension_value_code"] == 'Q':
# Quarterly period
if "TimePeriod" in observation_dict:
# Period given in one field, but not in the same notation as DB.nomics one
match = re.search(r"^(\d{4})(Q[1-4])$", observation_dict["TimePeriod"])
assert match, "Unhandled quarterly period format found in {!r}: {!r}".format(appendix_path, observation_dict["TimePeriod"])
time_period = "{}-{}".format(match.group(1), match.group(2)) # '2015Q3' => '2015-Q3'
else:
# Period devised in two fields and in roman naturals notation
time_period = "{}-Q{}".format(observation_dict['Year'], roman_numerals_to_int(observation_dict['Quarter']))
elif dataset_source_file_info["dimension_value_code"] == 'M':
# Monthly period
assert "TimePeriod" in observation_dict
# Period given in one field, but not in the same notation as DB.nomics one
match = re.search(r"^(\d{4})(M(0[1-9]|1[0-2]))$", observation_dict["TimePeriod"])
assert match, "Unhandled monthly period format found in {!r}: {!r}".format(appendix_path, observation_dict["TimePeriod"])
time_period = "{}-{}".format(match.group(1), match.group(3)) # '2015M10' => '2015-10'
else:
time_period = observation_dict["Year"]
time_period = observation_dict.get("Year") or observation_dict["TimePeriod"]
else:
# Annual period
time_period = observation_dict.get("Year") or observation_dict["TimePeriod"]
assert time_period is not None
observations[series_code].append((time_period, observation_value_str))
# Write dataset observations files
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment