Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to dump in multiple files #609

Merged
merged 2 commits into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pcmdi_metrics/driver/outputmetrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ def setup_out_file(self):
self.out_file.realm = self.realm
self.out_file.table = self.table_realm
self.out_file.case_id = self.parameter.case_id
if hasattr(self, "obs_or_model"):
self.out_file.model_version = self.obs_or_model
for key in self.out_file.keys():
if hasattr(self.parameter, key):
setattr(self.out_file, key, getattr(self.parameter, key))
if hasattr(self, key):
setattr(self.out_file, key, getattr(self, key))

DataSet.apply_custom_keys(self.out_file, self.parameter.custom_keys, self.var)

def add_region(self, region):
Expand Down Expand Up @@ -291,5 +299,6 @@ def write_on_exit(self):
self.out_file.write(self.metrics_dictionary,
json_structure=["model", "reference", "rip", "region", "statistic", "season"],
indent=4,
separators=(',', ': '))
separators=(',', ': '),
mode="r+")
self.out_file.write(self.metrics_dictionary, type='txt')
11 changes: 8 additions & 3 deletions pcmdi_metrics/io/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def __call__(self):
def read(self):
pass

def write(self, data, type='json', *args, **kwargs):
def write(self, data, type='json', mode="w", *args, **kwargs):
self.type = type.lower()
file_name = self()
dir_path = os.path.split(file_name)[0]
Expand Down Expand Up @@ -309,8 +309,13 @@ def write(self, data, type='json', *args, **kwargs):
del(kwargs[k])
data["json_version"] = json_version
data["json_structure"] = json_structure
f = open(file_name, 'w')
out_dict = OrderedDict({"provenance": generateProvenance()})

if mode == "r+" and os.path.exists(file_name):
f = open(file_name)
out_dict = json.load(f)
else:
out_dict = OrderedDict({"provenance": generateProvenance()})
f = open(file_name, "w")
out_dict.update(data)
json.dump(out_dict, f, cls=CDMSDomainsEncoder, *args, **kwargs)
f.close()
Expand Down
4 changes: 3 additions & 1 deletion pcmdi_metrics/pcmdi/mean_climate_metrics_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def run_diags(self):
self.output_metric.add_region(self.region)
# Runs obs vs obs, obs vs model, or model vs model
self.run_reference_and_test_comparison()
self.output_metric.write_on_exit()
self.output_metric.setup_out_file()
self.output_metric.write_on_exit()

def load_obs_dict(self):
''' Loads obs_info_dictionary.json and appends
Expand Down Expand Up @@ -181,6 +182,7 @@ def run_reference_and_test_comparison(self):
try:
tst = self.determine_obs_or_model(test_data_set_is_obs,
test, self.parameter.test_data_path)
self.output_metric.obs_or_model = tst.obs_or_model
# TODO Make this a custom exception. This exception is for
# when a model doesn't have sftlf for a given region
except RuntimeError:
Expand Down
4 changes: 2 additions & 2 deletions pcmdi_metrics/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = 'v1.2'
__git_tag_describe__ = 'v1.2-61-gde534f5'
__git_sha1__ = 'de534f543dce2db1a7761932ef1f5bf6e32bf324'
__git_tag_describe__ = 'v1.2-58-gec93e57'
__git_sha1__ = 'ec93e57248be0ad4b6c81d860e30714717f82c6b'
4 changes: 4 additions & 0 deletions tests/pcmdi/basic_test_parameters_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@
test_clims_interpolated_output = os.path.join(
'pcmdi_install_test_results',
'interpolated_model_clims')

# Filename template for json files:
output_json_template = "%(variable)%(level)_%(model_version)_%(target_grid_name)_%(regrid_tool)_%(regrid_method)_metricsssss"

# FILENAME FOR INTERPOLATED CLIMATOLOGIES OUTPUT
filename_output_template = "%(variable)%(level)_" + \
"%(model_version)_%(table)_historical_%(realization)_%(period)" + \
Expand Down