diff --git a/app/let_it_snow_fsc.py b/app/let_it_snow_fsc.py index b2afcb62d16ccc0445b49194e75533d05757bf25..a9fc445dbf7e12401f88ebd84c44e8a8f7575ce5 100755 --- a/app/let_it_snow_fsc.py +++ b/app/let_it_snow_fsc.py @@ -136,6 +136,9 @@ def let_it_snow_fsc(config_file, input_dir, output_dir, dem, tcd, water_mask, h2 logging.info("Reading configuration = " + config_file) try: config = FscConfig(data, input_dir, dem, tcd, water_mask) + config.dump_configuration(output_dir, config_file, input_dir, + logging.getLevelName(logging.getLogger().getEffectiveLevel()), h2_chain_version, + product_counter) except LisConfigurationException: sys.exit(CONFIGURATION_ERROR) except UnknownProductException: diff --git a/app/let_it_snow_synthesis.py b/app/let_it_snow_synthesis.py index 855f007057fa1ff8b750599db49fd28f6cbe0ed0..95910f01145e756706a0f33c5567fd0a890ecb31 100755 --- a/app/let_it_snow_synthesis.py +++ b/app/let_it_snow_synthesis.py @@ -151,6 +151,9 @@ def let_it_snow_synthesis(config_file, tile_id, input_products_list, densificati try: config = SynthesisConfig(data, tile_id, input_products_list, date_start, date_stop, date_margin=date_margin, densification_products_list=densification_products_list) + config.dump_configuration(output_dir, config_file, + logging.getLevelName(logging.getLogger().getEffectiveLevel()), h2_chain_version, + product_counter) except LisConfigurationException: sys.exit(CONFIGURATION_ERROR) except IOError: diff --git a/python/s2snow/fsc_config.py b/python/s2snow/fsc_config.py index 62e52e9fb8fb318fd392b0cfbca7d771eb609617..762d99afb4845c550421553e49f33edf06d7f894 100755 --- a/python/s2snow/fsc_config.py +++ b/python/s2snow/fsc_config.py @@ -19,6 +19,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import json import logging import os import zipfile @@ -560,6 +561,22 @@ class FscConfig: logging.debug("fscOg_Eq : " + str(self.fscOg_Eq)) logging.debug("fscToc_Eq : " + str(self.fscToc_Eq)) + def dump_configuration(self, dump_path, config_file, input_dir, log, chain_version, product_counter): + # Get the fields as a dictionary + conf_keys = ["dem", "tcd", "water_mask"] + key_translation = {"tcd": "tcd_file", "water_mask": "water_mask_path"} + full_data = vars(self) + data = {key: full_data[key_translation.get(key, key)] for key in conf_keys} + data["output_dir"] = dump_path + data["input_dir"] = input_dir + data["log"] = log + data["config_file"] = config_file + data["chain_version"] = chain_version + data["product_counter"] = product_counter + file_path = os.path.join(dump_path, 'fsc_config.json') + with open(file_path, 'w', encoding='utf-8') as f: + json.dump(data, f, ensure_ascii=False, indent=4) + def retrieve_mission(self, absoluteFilename): mission_key = None if '.SAFE' in absoluteFilename: diff --git a/python/s2snow/synthesis_config.py b/python/s2snow/synthesis_config.py index c35aab62f6610fd52a13b2b575a5056d401fafc9..36dc31a9cc98c7fa04d92eeb9f590556dfc33ae1 100755 --- a/python/s2snow/synthesis_config.py +++ b/python/s2snow/synthesis_config.py @@ -19,11 +19,14 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import json import logging +import os from datetime import datetime from datetime import timedelta from s2snow.lis_exception import LisConfigurationException +from s2snow.utils import JSONCustomEncoder class SynthesisConfig: @@ -90,3 +93,18 @@ class SynthesisConfig: logging.debug("output_dates_filename : " + str(self.output_dates_filename)) logging.debug("nb_threads : " + str(self.nb_threads)) logging.debug("ram : " + str(self.ram)) + + def dump_configuration(self, dump_path, config_file, log_level, chain_version, product_counter): + # Get the fields as a dictionary + conf_keys = ["tile_id", "input_products_list", "date_start", "date_stop", "date_margin", + "densification_products_list"] + full_data = vars(self) + data = {key: full_data[key] for key in conf_keys} + data["output_dir"] = dump_path + data["config_file"] = config_file + data["log_level"] = log_level + data["chain_version"] = chain_version + data["product_counter"] = product_counter + file_path = os.path.join(dump_path, 'synthesis_config.json') + with open(file_path, 'w', encoding='utf-8') as f: + json.dump(data, f, cls=JSONCustomEncoder, ensure_ascii=False, indent=4) diff --git a/python/s2snow/utils.py b/python/s2snow/utils.py index 0ba1cb7ef70cddbd3cbc0a87f4e2d9d4dc77b631..23c81e0f5e9bd06818d859ff0ded396bf7419864 100755 --- a/python/s2snow/utils.py +++ b/python/s2snow/utils.py @@ -21,12 +21,14 @@ # import glob +import json import logging import os import os.path as op import subprocess import uuid from datetime import datetime +from datetime import timedelta from distutils import spawn import numpy as np @@ -227,3 +229,12 @@ def compute_percent(image_path, value, no_data=-10000): return (float(count_pix) / float(tot_pix)) * 100 else: return 0 + + +class JSONCustomEncoder(json.JSONEncoder): + def default(self, o): + if isinstance(o, datetime): + return datetime_to_str(o) + elif isinstance(o, timedelta): + return o.days + return super().default(o)