Skip to content
Snippets Groups Projects
Commit b80f9cad authored by Aurore Dupuis's avatar Aurore Dupuis
Browse files

squash! Refactoring snow products reading.

parent 0da78b1c
No related branches found
No related tags found
3 merge requests!90Develop,!84Merge develop,!78Resolve "Synthesis on zipped products does not work"
...@@ -78,7 +78,7 @@ if __name__ == "__main__": ...@@ -78,7 +78,7 @@ if __name__ == "__main__":
date_start = global_config.get("date_start", None) date_start = global_config.get("date_start", None)
date_stop = global_config.get("date_stop", None) date_stop = global_config.get("date_stop", None)
date_margin = global_config.get("date_margin", None) date_margin = global_config.get("date_margin", None)
log_level = global_config.get("log", "INFO") log_level = global_config.get("log_level", "INFO")
config_file = global_config.get("config_file", None) config_file = global_config.get("config_file", None)
h2_chain_version = global_config.get("chain_version", None) h2_chain_version = global_config.get("chain_version", None)
product_counter = global_config.get("product_counter", "1") product_counter = global_config.get("product_counter", "1")
......
...@@ -48,7 +48,7 @@ class SnowProduct: ...@@ -48,7 +48,7 @@ class SnowProduct:
self.snow_mask = None self.snow_mask = None
self.metadata_file = None self.metadata_file = None
self.product_path = dirname(absolute_filename) self.product_path = absolute_filename
name_splitted = self.product_name.split("_") name_splitted = self.product_name.split("_")
platform = name_splitted[0] platform = name_splitted[0]
...@@ -66,95 +66,88 @@ class SnowProduct: ...@@ -66,95 +66,88 @@ class SnowProduct:
zip_file = absolute_filename zip_file = absolute_filename
if zip_file is not None: if zip_file is not None:
logging.debug("zipfile : " + op.join(absolute_filename, zip_file))
logging.info("The snow product is stored in a zip, extracting zip file.") logging.info("The snow product is stored in a zip, extracting zip file.")
extract_snow_mask(op.join(absolute_filename, zip_file), tmp) extract_snow_mask(op.join(absolute_filename, zip_file), tmp)
# self.product_path = op.join(tmp, op.basename(op.splitext(zip_file)[0]))
self.product_path = tmp self.product_path = tmp
logging.debug("product_path : " + self.product_path)
if SENTINEL2 in platform: if SENTINEL2 in platform:
self.acquisition_date = datetime.strptime(name_splitted[1], MUSCATE_DATETIME_FORMAT) self.acquisition_date = datetime.strptime(name_splitted[1], MUSCATE_DATETIME_FORMAT)
self.tile_id = name_splitted[3] self.tile_id = name_splitted[3]
self.snow_mask = find_files(self.product_path, ".*_SNW_R2.TIF")[0] self.snow_mask = find_files(self.product_path, ".*_SNW_R2.(tif|TIF)")[0]
self.metadata_file = find_files(self.product_path, "LIS_METADATA.XML")[0] self.metadata_file = find_files(self.product_path, ".*_MTD_ALL.(xml|XML)")[0]
elif LANDSAT8_OLITIRS_XS == platform: elif LANDSAT8_OLITIRS_XS == platform:
self.acquisition_date = datetime.strptime(name_splitted[1], MUSCATE_DATETIME_FORMAT) self.acquisition_date = datetime.strptime(name_splitted[1], MUSCATE_DATETIME_FORMAT)
self.tile_id = name_splitted[3] self.tile_id = name_splitted[3]
self.snow_mask = find_files(self.product_path, ".*_SNW_XS.TIF")[0] self.snow_mask = find_files(self.product_path, ".*_SNW_XS.(tif|TIF)")[0]
self.metadata_file = find_files(self.product_path, "LIS_METADATA.XML")[0] self.metadata_file = find_files(self.product_path, ".*_MTD_ALL.(xml|XML)")[0]
elif LANDSAT8 in platform and N2A in self.product_name: elif LANDSAT8 in platform and N2A in self.product_name:
self.acquisition_date = datetime.strptime(name_splitted[3], LANDSAT_DATETIME_FORMAT) self.acquisition_date = datetime.strptime(name_splitted[3], LANDSAT_DATETIME_FORMAT)
self.tile_id = name_splitted[5] self.tile_id = name_splitted[5]
self.snow_mask = find_files(self.product_path, ".*_SNW_XS.TIF")[0] self.snow_mask = find_files(self.product_path, ".*_SNW_XS.(tif/TIF)")[0]
self.metadata_file = find_files(self.product_path, "LIS_METADATA.XML")[0] self.metadata_file = find_files(self.product_path, ".*_MTD_ALL.(xml|XML)")[0]
elif LIS in platform: elif LIS in platform:
self.acquisition_date = datetime.strptime(name_splitted[3], LIS_DATETIME_FORMAT) self.acquisition_date = datetime.strptime(name_splitted[3], LIS_DATETIME_FORMAT)
self.tile_id = name_splitted[2] self.tile_id = name_splitted[2]
self.snow_mask = find_files(self.product_path, "LIS_SNOW-.*.TIF")[0] self.snow_mask = find_files(self.product_path, ".*SNOW-FS.*C_.*(tif|TIF)")[0]
self.metadata_file = find_files(self.product_path, "LIS_SNOW-.*.XML")[0] self.metadata_file = find_files(self.product_path, ".*SNOW-.*.(xml|XML)")[2]
else: else:
msg = "Unknown platform or producer: " + platform msg = "Unknown platform or producer: " + platform
logging.error(msg) logging.error(msg)
raise UnknownPlatform(msg) raise UnknownPlatform(msg)
if self.snow_mask is None: if self.snow_mask is None:
self.snow_mask = find_files(self.product_path, "LIS_SEB.TIF")[0] self.snow_mask = find_files(self.product_path, "LIS_SEB")[0]
if self.metadata_file is None: if self.metadata_file is None:
self.metadata_file = find_files(self.product_path, "LIS_METADATA.XML")[0] self.metadata_file = find_files(self.product_path, "LIS_METADATA.XML")[0]
self.log_product() self.log_product()
def __repr__(self):
def __repr__(self): return op.join(self.product_path)
return op.join(self.product_path, self.product_name)
def __str__(self):
def __str__(self): return op.join(self.product_path)
return op.join(self.product_path, self.product_name)
def log_product(self):
def log_product(self): logging.debug("-------------------------------------")
logging.debug("-------------------------------------") logging.debug("SNOW PRODUCT")
logging.debug("SNOW PRODUCT") logging.debug("-------------------------------------")
logging.debug("-------------------------------------") logging.debug("product_name : " + self.product_name)
logging.debug("product_name : " + self.product_name) logging.debug("acquisition_date : " + str(self.acquisition_date))
logging.debug("acquisition_date : " + str(self.acquisition_date)) logging.debug("tile_id : " + self.tile_id)
logging.debug("tile_id : " + self.tile_id) logging.debug("snow_mask : " + self.snow_mask)
logging.debug("snow_mask : " + self.snow_mask) logging.debug("metadata_file : " + self.metadata_file)
logging.debug("metadata_file : " + self.metadata_file)
def get_snow_mask(self):
def get_snow_mask(self): if self.snow_mask and op.exists(self.snow_mask):
if self.snow_mask and op.exists(self.snow_mask): return self.snow_mask
return self.snow_mask else:
else: msg = "The snow mask has not been found."
msg = "The snow mask has not been found." logging.error(msg)
logging.error(msg) raise NoSnowProductFound(msg)
raise NoSnowProductFound(msg)
def get_metadata(self):
def get_metadata(self): if self.metadata_file and op.exists(self.metadata_file):
if self.metadata_file and op.exists(self.metadata_file): return self.metadata_file
return self.metadata_file else:
else: logging.info("The metadata file has not been found.")
logging.info("The metadata file has not been found.")
def extract_from_zipfile(file_name, output_folder):
def extract_from_zipfile(file_name, output_folder, patterns=[]):
""" Extract from the zip file all files corresponding """ Extract from the zip file all files corresponding
to one of the provided patterns to one of the provided patterns
""" """
f = open(file_name, 'r') zip_input = zipfile.ZipFile(file_name)
z = zipfile.ZipFile(f) zip_input.extractall(path=output_folder)
extracted_files = []
for pattern in patterns:
for name in z.namelist():
if pattern in name:
logging.debug(name)
z.extract(name, output_folder)
extracted_files.append(op.join(output_folder, name))
f.close()
return extracted_files
def extract_snow_mask(zip_file, output_folder): def extract_snow_mask(zip_file, output_folder):
if op.exists(zip_file): if op.exists(zip_file):
extract_from_zipfile(zip_file, output_folder, ["_SNW_R2.tif", "_SNW_XS.tif", "_MTD_ALL.xml"]) extract_from_zipfile(zip_file, output_folder)
else: else:
msg = "Extraction failed - zipfile does not exist : " + zip_file msg = "Extraction failed - zipfile does not exist : " + zip_file
logging.error(msg) logging.error(msg)
...@@ -170,11 +163,18 @@ def find_files(folder, pattern): ...@@ -170,11 +163,18 @@ def find_files(folder, pattern):
for root, dirs, files in os.walk(folder): for root, dirs, files in os.walk(folder):
for file in files: for file in files:
logging.debug("file:" + file)
if re.match(pattern, file): if re.match(pattern, file):
logging.debug("match file :%s", file) logging.debug("match file :%s", file)
matches.append(os.path.join(root, file)) matches.append(os.path.join(root, file))
# for dir in dirs:
# logging.debug("dir:" + dir)
# find_files(dir,pattern)
if len(matches) == 0: if len(matches) == 0:
matches = [None] matches = [None]
return matches return matches
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment