Datasets:
Download code/layers/frend_weh.py from enzeeoai/starman: direct link, hf CLI and curl.
- Browser
- Download file 5.79 kB
-
https://huggingface.co/datasets/enzeeoai/starman/resolve/main/code/layers/frend_weh.py
- Command line
-
hf download hf://datasets/enzeeoai/starman/code/layers/frend_weh.py
-
curl -L -o frend_weh.py https://huggingface.co/datasets/enzeeoai/starman/resolve/main/code/layers/frend_weh.py
5.79 kB
| """TGO FREND water-equivalent hydrogen (Malakhov et al. 2022) -> per-cell `ice_wt_pct_frend*` (planet mars). | |
| Earth twin: ``ggd318`` ``ice_wt_pct`` (master decision 14). Input is the smoothed map ``WEHMaps.txt`` | |
| (columns X Y WEHMIN WEH WEHMAX; 1 deg pixels, lon -179.5..179.5 east-positive, lat -49.5..49.5, latitude | |
| assumed planetocentric). The map was produced with a 12-degree FWHM Gaussian, so the 1 deg pixels are not | |
| independent. ``SuppMap.txt`` (the raw neutron-suppression map) is NOT used for any published column. FREND is | |
| not filled from GRS: the two stay in separate columns. | |
| """ | |
| from __future__ import annotations | |
| from pathlib import Path | |
| import numpy as np | |
| import pyarrow as pa | |
| from . import _climatology as clim | |
| SOURCE = { | |
| 'product': 'Water content in the upper 1 m of the Martian regolith from TGO FREND (Zenodo 6282972)', | |
| 'version': 'Zenodo record 6282972 v1, WEHMaps.txt (smoothed)', | |
| 'url': 'https://doi.org/10.5281/zenodo.6282972', | |
| 'licence': 'CC BY 4.0 (Zenodo record); cite Malakhov et al. 2022, JGR Planets, doi:10.1029/2022JE007258.', | |
| 'planet': 'mars', | |
| 'file': 'WEHMaps.txt', | |
| } | |
| VERSION = 'v0.1' | |
| MAP_NAME = 'WEHMaps.txt' | |
| UNUSED_FILES = ('SuppMap.txt',) # recorded in the manifest only | |
| LAT_LIMIT_DEG = 50.0 | |
| SRC_RES_KM = 700.0 # 12 deg FWHM at the equator (paper's own figure UNVERIFIED) | |
| _C = {'dtype': 'float32', 'unit': 'wt%', 'comparison': 'planet_specific'} | |
| COLUMNS = { | |
| 'ice_wt_pct_frend': {**_C, 'description': 'FREND water-equivalent hydrogen, upper ~1 m, wt % (smoothed map ' | |
| 'interpolated to the cell; Earth twin: ggd318 ice_wt_pct)'}, | |
| 'ice_wt_pct_frend_min': {**_C, 'description': 'FREND WEHMIN: -1 sigma statistical suppression pushed through the model'}, | |
| 'ice_wt_pct_frend_max': {**_C, 'description': 'FREND WEHMAX: +1 sigma statistical suppression pushed through the model'}, | |
| 'src_res_km': {'dtype': 'float32', 'unit': 'km', 'comparison': 'planet_specific', | |
| 'description': 'effective resolution of the smoothed map (12 deg FWHM at the equator)'}, | |
| 'interp_flag': {'dtype': 'string', 'unit': '', 'comparison': 'categorical', | |
| 'description': 'bilinear interpolation status'}, | |
| } | |
| LIMITATIONS = [ | |
| 'Coverage is +-50 deg latitude only; cells poleward are NaN with outside_coverage and are not filled from GRS.', | |
| 'The map is smoothed (12 deg FWHM, ~700 km), far coarser than the 1 deg pixels and a 5 km cell.', | |
| 'WEHMIN / WEHMAX are 1-sigma statistical ranges, not model or systematic uncertainty.', | |
| 'The WEH zero point is tied to one GRS-derived value at Solis Planum, so FREND and GRS are not independent in absolute level.', | |
| 'Latitude type is assumed planetocentric. Depth ~1 m. SuppMap.txt (raw suppression) is not published.', | |
| ] | |
| def read_map(path) -> dict[str, np.ndarray]: | |
| """Parse the whitespace table (header row X Y WEHMIN WEH WEHMAX) into column arrays.""" | |
| with open(path) as fh: | |
| header = fh.readline().split() | |
| data = np.loadtxt(path, skiprows=1, ndmin=2) | |
| if data.shape[1] != len(header): | |
| raise ValueError(f'{path}: {data.shape[1]} columns but header {header}') | |
| return {name: data[:, i] for i, name in enumerate(header)} | |
| def to_grid(table: dict[str, np.ndarray], column: str): | |
| lon, lat = table['X'], table['Y'] | |
| glon, glat = np.unique(lon), np.unique(lat) | |
| field = np.full((len(glat), len(glon)), np.nan) | |
| field[np.searchsorted(glat, lat), np.searchsorted(glon, lon)] = table[column] | |
| return glat, glon, field | |
| def plan(band_group, ctx): | |
| return [{'key': MAP_NAME, 'url': str(Path(ctx.raw_dir) / MAP_NAME), 'method': 'local'}] | |
| def prepare(ctx) -> None: | |
| ctx.source_checksum = ctx.record_source_file(Path(ctx.raw_dir) / MAP_NAME) | |
| for name in UNUSED_FILES: # recorded in the manifest only | |
| path = Path(ctx.raw_dir) / name | |
| if path.exists(): | |
| ctx.record_source_file(path) | |
| def reduce(band_group, items, ctx): | |
| table = read_map(items.all()[0].path) | |
| cells = ctx.cells(band_group) | |
| return build_rows(cells['region_id'], cells['center_lat'].to_numpy(zero_copy_only=False), | |
| cells['center_lon'].to_numpy(zero_copy_only=False), table) | |
| def build_rows(region_id, lat, lon, table) -> pa.Table: | |
| lat = np.asarray(lat, dtype=np.float64) | |
| out, flags_interp = {}, [] | |
| glat = glon = None | |
| outside = np.abs(lat) > LAT_LIMIT_DEG | |
| for column, name in (('WEH', 'ice_wt_pct_frend'), ('WEHMIN', 'ice_wt_pct_frend_min'), | |
| ('WEHMAX', 'ice_wt_pct_frend_max')): | |
| glat, glon, field = to_grid(table, column) | |
| # the map's edge pixel centres are +-49.5 but it covers to +-50: query at the edge row inside that strip | |
| lat_q = np.clip(lat, glat[0], glat[-1]) | |
| values, flag = clim.bilinear_at(lat_q, lon, glat, glon, field) | |
| clamped = (lat != lat_q) & ~outside | |
| flag = np.where(clamped & (flag == 'bilinear'), 'nearest_fallback', flag) | |
| out[name], flags_interp = np.where(outside, np.nan, values), flags_interp + [flag] | |
| interp = clim.worst_flag(*flags_interp) | |
| interp = np.where(outside, 'outside_coverage', interp) | |
| flags = clim.Flags(len(lat)) | |
| flags.add(outside, 'outside_coverage') | |
| flags.add(~outside & ~np.isfinite(out['ice_wt_pct_frend']), 'no_source') | |
| data = {'region_id': pa.array(list(region_id), type=pa.string())} | |
| for name, values in out.items(): | |
| data[name] = pa.array(values.astype(np.float32)) | |
| data['src_res_km'] = pa.array(np.full(len(lat), SRC_RES_KM, dtype=np.float32)) | |
| data['interp_flag'] = pa.array([str(x) for x in interp], type=pa.string()) | |
| data['quality_flags'] = pa.array(flags.strings(), type=pa.string()) | |
| return pa.table(data) | |