feat: working on timefile

This commit is contained in:
2025-11-10 20:19:03 +00:00
parent 0259da7eaa
commit 88146c08b1
6 changed files with 218 additions and 46 deletions
+37 -46
View File
@@ -4,6 +4,7 @@ from pathlib import Path
import re
import logging
import yaml
import time
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
@@ -14,11 +15,11 @@ BOUNDING_BOX_INFO = {
"WINTSC": (499000, 500000, 416000, 417000),
}
# TODO: The dat files im fairly sure are duplicated as it is the whole uk area, not area specific. need to change it
IN_TOP_FOLDER = "./dat_files"
OUT_TOP_FOLDER = "./asc_files"
CONFIG_FILE = "config.yaml"
def get_datetime(file_name: str) -> str:
"""
Extract datetime from a filename using regex pattern matching.
@@ -72,55 +73,45 @@ def process_nimrod_files() -> None:
This function reads all files from IN_TOP_FOLDER, applies the appropriate bounding
box for each area, and exports clipped raster data to OUT_TOP_FOLDER.
"""
# Load configuration
bounding_box_info = load_config()
# Read all file names in the folder
files_to_process = [f for f in os.listdir(Path(IN_TOP_FOLDER))]
logging.info(f"Processing {len(files_to_process)} files...")
os.makedirs(Path(OUT_TOP_FOLDER), exist_ok=True)
# Use default if config is empty
if not bounding_box_info:
bounding_box_info = {
"BRISCS": (607000, 608000, 217000, 218000),
"WINTSC": (499000, 500000, 416000, 417000),
}
for in_file in os.listdir(Path(IN_TOP_FOLDER)):
timestamp = get_datetime(in_file)
out_file_name = f"{timestamp}.asc"
out_file_path = Path(OUT_TOP_FOLDER, out_file_name)
in_file_full = Path(IN_TOP_FOLDER, in_file)
# read all file names in the folder
area_folders = os.listdir(IN_TOP_FOLDER)
try:
image = Nimrod(open(in_file_full, "rb"))
with open(out_file_path, "w") as outfile:
image.extract_asc(outfile)
#logging.info(f"Successfully processed: {in_file_full}")
for area in area_folders:
bounding_box = bounding_box_info.get(area, (0, 0, 0, 0))
logging.info(f"Processing area: {area}, bounding box: {bounding_box}")
xmin, xmax, ymin, ymax = bounding_box
os.makedirs(Path(OUT_TOP_FOLDER, area), exist_ok=True)
for in_file in os.listdir(Path(IN_TOP_FOLDER, area)):
timestamp = get_datetime(in_file)
out_file_name = f"{timestamp}_{area}.asc"
out_file_path = Path(OUT_TOP_FOLDER, area, out_file_name)
in_file_full = Path(IN_TOP_FOLDER, area, in_file)
try:
image = Nimrod(open(in_file_full, "rb"))
image.apply_bbox(xmin, xmax, ymin, ymax)
# image.query() # prints out file_details
with open(out_file_path, "w") as outfile:
image.extract_asc(outfile)
logging.info(f"Successfully processed: {in_file_full}")
except Nimrod.HeaderReadError as e:
logging.error(f"Failed to read file {in_file_full}, is it corrupt?")
logging.error(e)
continue
except Nimrod.PayloadReadError as e:
logging.error(f"Failed to load the raster data in {in_file_full}")
logging.error(e)
continue
except Nimrod.BboxRangeError as e:
logging.error(
f"Bounding Box out of range. Given bounding box: {bounding_box}"
)
logging.error(e)
# Skips the whole area as bounding box will be out of bounds for all files
break
except Nimrod.HeaderReadError as e:
logging.error(f"Failed to read file {in_file_full}, is it corrupt?")
logging.error(e)
continue
except Nimrod.PayloadReadError as e:
logging.error(f"Failed to load the raster data in {in_file_full}")
logging.error(e)
continue
except Nimrod.BboxRangeError as e:
logging.error(
"Bounding Box out of range. Given bounding box: {bounding_box}"
)
logging.error(e)
# Skips the whole area as bounding box will be out of bounds for all files
break
if __name__ == "__main__":
start = time.time()
process_nimrod_files()
end = time.time()
elapsed_time = end - start
logging.info(f"Processing completed in {elapsed_time:.2f} seconds")