|
| 1 | +import re |
| 2 | +import uuid |
| 3 | +from collections.abc import Callable |
| 4 | +from datetime import datetime, timezone |
| 5 | +from logging import getLogger |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from ctorm.config import ( |
| 9 | + MD5_CHECKSUM_PATTERN, |
| 10 | + CtormPipeline, |
| 11 | + CtormPreparedFile, |
| 12 | + CtormPreparedGranule, |
| 13 | +) |
| 14 | + |
| 15 | +log = getLogger(__name__) |
| 16 | + |
| 17 | +DATA_TYPE_MAP = { |
| 18 | + ".context.json": "metadata", |
| 19 | + ".dataset.json": "metadata", |
| 20 | + ".h5": "data", |
| 21 | + ".iso.xml": "metadata", |
| 22 | + ".jpg": "browse", |
| 23 | + ".log": "metadata", |
| 24 | + ".md5": "metadata", |
| 25 | + ".met.json": "metadata", |
| 26 | + ".nc": "data", |
| 27 | + ".pdf": "qa", |
| 28 | + ".png": "browse", |
| 29 | + ".qa.h5": "qa", |
| 30 | + ".rc.yaml": "metadata", |
| 31 | + ".tif": "data", |
| 32 | + ".xml": "data", |
| 33 | +} |
| 34 | + |
| 35 | +type CtormCnmSGeneratorType = Callable[[CtormPreparedGranule, str], dict] |
| 36 | + |
| 37 | + |
| 38 | +class CtormCnmSGenerator: |
| 39 | + """input dict like: |
| 40 | + ``` |
| 41 | + { |
| 42 | + "bm": { |
| 43 | + "sds-n-cumulus-prod-nisar-products": "B1", |
| 44 | + "sds-n-cumulus-prod-nisar-jpl-private-data": "B2" |
| 45 | + }, |
| 46 | + "g": "NISAR_L0_RRST_VC08_20250821T101036_20250821T101041_P00408_J_001", |
| 47 | + "c": "NISAR_L0A_RRST_BETA_V1", |
| 48 | + "cv": "1", |
| 49 | + "f": [ |
| 50 | + { |
| 51 | + "f": "s3://$B1/NISAR_L0A_RRST_BETA_V1/$G/$G.bin", |
| 52 | + "m": "e989430f4c5bfa04eeb26e56f4cd8375", |
| 53 | + "s": 1073829760 |
| 54 | + }, |
| 55 | + { |
| 56 | + "f": "s3://$B1/NISAR_L0A_RRST_BETA_V1/$G/$G.rc.yaml", |
| 57 | + "s": 120597, |
| 58 | + "m": "11bcaec780f0d246f3996b3f08680410" |
| 59 | + }, |
| 60 | + { |
| 61 | + "f": "s3://$B1/NISAR_L0A_RRST_BETA_V1/$G/$G.bin.qa", |
| 62 | + "s": 1134, |
| 63 | + }, |
| 64 | + ] |
| 65 | + }, |
| 66 | + ``` |
| 67 | + """ |
| 68 | + |
| 69 | + def _assemble_file_dict( |
| 70 | + self, file_dict: CtormPreparedFile, g_name: str, bucket_map: dict |
| 71 | + ): |
| 72 | + file_s3uri = file_dict["f"].replace("$G", g_name) |
| 73 | + for bucket, replacetoken in bucket_map.items(): |
| 74 | + file_s3uri = file_s3uri.replace(f"${replacetoken}", bucket) |
| 75 | + filename = Path(file_s3uri).name |
| 76 | + return { |
| 77 | + "name": filename, |
| 78 | + "type": self._get_type(filename), |
| 79 | + "uri": file_s3uri, |
| 80 | + "size": file_dict["s"], |
| 81 | + "checksum": self._get_checksum(file_dict), |
| 82 | + "checksumType": "md5", |
| 83 | + } |
| 84 | + |
| 85 | + def __call__( |
| 86 | + self, |
| 87 | + granule_dict: CtormPreparedGranule, |
| 88 | + provider: str, |
| 89 | + ) -> dict: |
| 90 | + cnm_s = { |
| 91 | + "identifier": str(uuid.uuid4()), |
| 92 | + "collection": granule_dict["c"], |
| 93 | + "version": "1.3", |
| 94 | + "submissionTime": datetime.now(tz=timezone.utc).strftime( |
| 95 | + "%Y-%m-%dT%H:%M:%S.%fZ" |
| 96 | + ), |
| 97 | + "product": { |
| 98 | + "name": granule_dict["g"], |
| 99 | + "dataVersion": granule_dict["cv"], |
| 100 | + "files": [ |
| 101 | + self._assemble_file_dict( |
| 102 | + file, granule_dict["g"], granule_dict["bm"] |
| 103 | + ) |
| 104 | + for file in granule_dict["f"] |
| 105 | + ], |
| 106 | + }, |
| 107 | + "provider": provider, |
| 108 | + } |
| 109 | + |
| 110 | + return cnm_s |
| 111 | + |
| 112 | + def _get_type(self, filename: str): |
| 113 | + |
| 114 | + suffixes = Path(filename).suffixes |
| 115 | + while suffixes: |
| 116 | + data_type = DATA_TYPE_MAP.get("".join(suffixes)) |
| 117 | + |
| 118 | + if data_type: |
| 119 | + return data_type |
| 120 | + |
| 121 | + suffixes = suffixes[1:] |
| 122 | + |
| 123 | + log.debug("suffix from %s is not in map. Using 'data'", filename) |
| 124 | + return "data" |
| 125 | + |
| 126 | + def _get_checksum(self, file_dict: CtormPreparedFile): |
| 127 | + m = MD5_CHECKSUM_PATTERN.match(file_dict.get("m", "not md5")) |
| 128 | + if m: |
| 129 | + return m.group(1) |
| 130 | + |
| 131 | + return "00000000000000000000000000000000" |
0 commit comments