from pathlib import Path
import rich
import mertisreader as mr
print("Libraries imported successfully.")Libraries imported successfully.
mertisreaderThis page is a Quarto version of the old notebook walkthrough. It keeps the narrative and executable examples together, while the package API remains documented through docstrings.
Use a Python virtual environment or conda environment, then install the package in editable mode while the migration is in progress.
MERTISDataPackReadercandidate_roots = [Path.cwd().resolve(), Path.cwd().resolve().parent, Path.cwd().resolve().parent.parent]
repo_root = next(root for root in candidate_roots if (root / "data").exists())
input_path = repo_root / "data/bcmer_tm_all_START-20200409T000000_END-20200410T000000_CRE-20240717T132010-ParamEventBootSciHK-short/cal"
output_path = Path("/tmp/")
log_level = "INFO"
if not input_path.exists():
print(f"Input path {input_path} does not exist.")
else:
print(f"Reading path {input_path}")
ms_reader = mr.MERTISDataPackReader(input_dir=input_path, output_dir=output_path, log_level=log_level)
print(f"MERTISDataPackReader initialized with input directory: {ms_reader.input_dir}")
print(f"Output directory: {ms_reader.output_dir}")
print(f"Log level: {ms_reader.log_level}")2026-11-24 19:26:53,076|2112643|INFO|input_dir=PosixPath('/home/kidpixo/work/esa/mertis_data_management/mertisreader/data/bcmer_tm_all_START-20200409T000000_END-20200410T000000_CRE-20240717T132010-ParamEventBootSciHK-short/cal')
Reading path /home/kidpixo/work/esa/mertis_data_management/mertisreader/data/bcmer_tm_all_START-20200409T000000_END-20200410T000000_CRE-20240717T132010-ParamEventBootSciHK-short/cal
MERTISDataPackReader initialized with input directory: /home/kidpixo/work/esa/mertis_data_management/mertisreader/data/bcmer_tm_all_START-20200409T000000_END-20200410T000000_CRE-20240717T132010-ParamEventBootSciHK-short/cal
Output directory: /tmp
Log level: INFO
All files in input_dir :
Counter({'.dat': 2, '.lblx': 3, '.fits': 1})
All files in input_dir matching old pattern <v0.2.6 (\d{8}_\d{8}):
Counter()
All files in input_dir matching new pattern >=v0.2.6 (mer_cal_sc_tis_YYYYMMDD_1-...):
Counter({'mer_cal_sc_tis': 1})
{ 'hk_default': ['mer_cal_hk_default_20200409_1-0651130766-12538__0_1.dat'], 'hk_extended': ['mer_cal_hk_extended_20200409_1-0651130766-12595__0_1.dat'], 'sc_tis': ['mer_cal_sc_tis_20200409_1-0651130819-21186__0_1.fits'], 'sc_tir': [], 'sc_tis_ql': [], 'sc_tir_ql': [] }
{'hk_default': 1, 'hk_extended': 1, 'sc_tis': 1, 'sc_tir': 0, 'sc_tis_ql': 0, 'sc_tir_ql': 0}
Reading filetype: hk_default from mer_cal_hk_default_20200409_1-0651130766-12538__0_1
Processing label: /home/kidpixo/work/esa/mertis_data_management/mertisreader/data/bcmer_tm_all_START-20200409T000000_END-20200410T000000_CRE-20240717T132010-ParamEventBootSciHK-short/cal/mer_cal_hk_default_20200409_1-0651130766-12538__0_1.lblx
Now processing a Table_Delimited structure: MERTIS_CAL_DEFAULT_HOUSEKEEPING_DATA_TABLE
Reading filetype: hk_extended from mer_cal_hk_extended_20200409_1-0651130766-12595__0_1
Processing label: /home/kidpixo/work/esa/mertis_data_management/mertisreader/data/bcmer_tm_all_START-20200409T000000_END-20200410T000000_CRE-20240717T132010-ParamEventBootSciHK-short/cal/mer_cal_hk_extended_20200409_1-0651130766-12595__0_1.lblx
Now processing a Table_Delimited structure: MERTIS_CAL_EXTENDED_HOUSEKEEPING_DATA_TABLE
Reading filetype: tis from /home/kidpixo/work/esa/mertis_data_management/mertisreader/data/bcmer_tm_all_START-20200409T000000_END-20200410T000 000_CRE-20240717T132010-ParamEventBootSciHK-short/cal/mer_cal_sc_tis_20200409_1-0651130819-21186__0_1.fits
Reading filetype: tis from mer_cal_sc_tis_20200409_1-0651130819-21186__0_1
Collected data keys: dict_keys(['hk_default', 'hk_extended', 'tis'])
hk_default: 1 files collected
hk_extended: 1 files collected
tis: 1 files collected
n_wav=40 # generic wavelengths : not precise enough for scientific analysis!
| | tis_stem | finite(geo) | geo.size |
|---:|:------------------------------------------------|--------------:|-----------:|
| 0 | mer_cal_sc_tis_20200409_1-0651130819-21186__0_1 | 672 | 10500 |
Indices of measurements targets (HK_STAT_TIS_DATA_ACQ_TARGET):
space_index_merged.shape=(21,)
bb7_index_merged.shape=(0,)
bb3_index_merged.shape=(0,)
planet_index_merged.shape=(0,)
Collected data statistics:
Number of TIS files: 1
Number of HK files: 2
Number of TIR files: 0
Number of TIS QL files: 0
Number of TIR QL files: 0
if ms_reader.processing_level != "RAW":
print(f"Geometry data keys: {ms_reader.geom_ls.keys()}")
print(f"Frames data keys: {ms_reader.frames.keys()}")
print(f"Wavelengths data keys: {ms_reader.wavelengths.keys()}")
print(f"MERTIS TIS metadata keys: {ms_reader.mertis_tis_metadata.keys()}")
print(f"Space index merged shape: {ms_reader.space_index_merged.shape}")
print(f"BB7 index merged shape: {ms_reader.bb7_index_merged.shape}")
print(f"BB3 index merged shape: {ms_reader.bb3_index_merged.shape}")
print(f"Planet index merged shape: {ms_reader.planet_index_merged.shape}")Geometry data keys: dict_keys(['mer_cal_sc_tis_20200409_1-0651130819-21186__0_1'])
Frames data keys: dict_keys(['mer_cal_sc_tis_20200409_1-0651130819-21186__0_1'])
Wavelengths data keys: dict_keys(['mer_cal_sc_tis_20200409_1-0651130819-21186__0_1'])
MERTIS TIS metadata keys: dict_keys(['mer_cal_sc_tis_20200409_1-0651130819-21186__0_1'])
Space index merged shape: (21,)
BB7 index merged shape: (0,)
BB3 index merged shape: (0,)
Planet index merged shape: (0,)
For large datasets, use lazy mode with the helper methods introduced in the MERTISDataPackReader docs. They keep I/O deferred until you explicitly access data.
lazy_reader = mr.MERTISDataPackReader(input_path, lazy=True)
lazy_reader.data_collector()
# Get product without loading data into memory
product = lazy_reader.get_tis_product()
print(product['frames']) # LazyArray - data still on disk
print(product['frames'].shape) # Accessible without I/O
print(product['wavelengths']) # LazyArray or None depending on level2026-11-24 19:26:53,660|2112643|INFO|input_dir=PosixPath('/home/kidpixo/work/esa/mertis_data_management/mertisreader/data/bcmer_tm_all_START-20200409T000000_END-20200410T000000_CRE-20240717T132010-ParamEventBootSciHK-short/cal')
Reading filetype: hk_default from mer_cal_hk_default_20200409_1-0651130766-12538__0_1
Processing label: /home/kidpixo/work/esa/mertis_data_management/mertisreader/data/bcmer_tm_all_START-20200409T000000_END-20200410T000000_CRE-20240717T132010-ParamEventBootSciHK-short/cal/mer_cal_hk_default_20200409_1-0651130766-12538__0_1.lblx
Now processing a Table_Delimited structure: MERTIS_CAL_DEFAULT_HOUSEKEEPING_DATA_TABLE
Reading filetype: hk_extended from mer_cal_hk_extended_20200409_1-0651130766-12595__0_1
Processing label: /home/kidpixo/work/esa/mertis_data_management/mertisreader/data/bcmer_tm_all_START-20200409T000000_END-20200410T000000_CRE-20240717T132010-ParamEventBootSciHK-short/cal/mer_cal_hk_extended_20200409_1-0651130766-12595__0_1.lblx
Now processing a Table_Delimited structure: MERTIS_CAL_EXTENDED_HOUSEKEEPING_DATA_TABLE
Reading filetype: tis from mer_cal_sc_tis_20200409_1-0651130819-21186__0_1
LazyArray(shape=(40, 100, 21), dtype=>f8, status=lazy (memmap))
(40, 100, 21)
LazyArray(shape=(40, 100), dtype=>f8, status=lazy (memmap))
Materialize only when running actual analysis:
TIR (thermal infrared) files have a different structure from TIS: instead of a single science image cube, they carry a metadata table plus two channel tables (CHANNEL_A and CHANNEL_B), each a BinTable with one row per frame. The reader collects them under the tir key.
raw_path = repo_root / "data/bcmer_tm_all_START-20200409T000000_END-20200410T000000_CRE-20240717T132010-ParamEventBootSciHK-short/raw"
tir_reader = mr.MERTISDataPackReader(input_dir=raw_path, output_dir=output_path, log_level="INFO")
tir_reader.data_collector()
print(f"Collected file types: { {k: len(v) for k, v in tir_reader.collect_data.items()} }")2026-11-24 19:26:53,721|2112643|INFO|input_dir=PosixPath('/home/kidpixo/work/esa/mertis_data_management/mertisreader/data/bcmer_tm_all_START-20200409T000000_END-20200410T000000_CRE-20240717T132010-ParamEventBootSciHK-short/raw')
Reading filetype: hk_default from mer_raw_hk_default_20200409_1-0651130766-12538__0_1
Processing label: /home/kidpixo/work/esa/mertis_data_management/mertisreader/data/bcmer_tm_all_START-20200409T000000_END-20200410T000000_CRE-20240717T132010-ParamEventBootSciHK-short/raw/mer_raw_hk_default_20200409_1-0651130766-12538__0_1.lblx
Now processing a Table_Delimited structure: MERTIS_RAW_DEFAULT_HOUSEKEEPING_DATA_TABLE
Reading filetype: hk_extended from mer_raw_hk_extended_20200409_1-0651130766-12595__0_1
Processing label: /home/kidpixo/work/esa/mertis_data_management/mertisreader/data/bcmer_tm_all_START-20200409T000000_END-20200410T000000_CRE-20240717T132010-ParamEventBootSciHK-short/raw/mer_raw_hk_extended_20200409_1-0651130766-12595__0_1.lblx
Now processing a Table_Delimited structure: MERTIS_RAW_EXTENDED_HOUSEKEEPING_DATA_TABLE
Reading filetype: tis from mer_raw_sc_tis_20200409_1-0651130819-21186__0_1
Reading filetype: tir from mer_raw_sc_tir_20200409_1-0651130751-42768__0_1
Collected file types: {'hk_default': 1, 'hk_extended': 1, 'tis': 1, 'tir': 1}
In eager mode the full FITS HDU list is available under fits_data:
MERTIS_TIR_METADATA
MERTIS_TIR_CHANNEL_A_RAW_SCIENCE_DATA
MERTIS_TIR_CHANNEL_B_RAW_SCIENCE_DATA
The metadata table holds one row per acquisition, and the two channel tables share the same row count:
import numpy as np
metadata = hdul["MERTIS_TIR_METADATA"].data
channel_a = hdul["MERTIS_TIR_CHANNEL_A_RAW_SCIENCE_DATA"].data
channel_b = hdul["MERTIS_TIR_CHANNEL_B_RAW_SCIENCE_DATA"].data
print(f"Metadata rows: {len(metadata)}")
print(f"Channel A rows: {len(channel_a)}, Channel B rows: {len(channel_b)}")
print(f"First acquisition target: {metadata['HK_STAT_TIR_DATA_ACQ_TARGET'][0]}")Metadata rows: 19
Channel A rows: 19, Channel B rows: 19
First acquisition target: Space
In lazy mode the channel tables are wrapped in LazyArray proxies and the data stays on disk until materialized:
tir_lazy = mr.MERTISDataPackReader(input_dir=raw_path, output_dir=output_path, log_level="INFO", lazy=True)
tir_lazy.data_collector()
channels = tir_lazy.collect_data["tir"][0]["channels"]
print(f"Channels: {list(channels)}")
frames_a = channels["MERTIS_TIR_CHANNEL_A_RAW_SCIENCE_DATA"]
print(f"Channel A shape (no I/O): {frames_a.shape}")
print(f"Materialized: {frames_a.materialize().shape}")2026-11-24 19:26:53,793|2112643|INFO|input_dir=PosixPath('/home/kidpixo/work/esa/mertis_data_management/mertisreader/data/bcmer_tm_all_START-20200409T000000_END-20200410T000000_CRE-20240717T132010-ParamEventBootSciHK-short/raw')
Reading filetype: hk_default from mer_raw_hk_default_20200409_1-0651130766-12538__0_1
Processing label: /home/kidpixo/work/esa/mertis_data_management/mertisreader/data/bcmer_tm_all_START-20200409T000000_END-20200410T000000_CRE-20240717T132010-ParamEventBootSciHK-short/raw/mer_raw_hk_default_20200409_1-0651130766-12538__0_1.lblx
Now processing a Table_Delimited structure: MERTIS_RAW_DEFAULT_HOUSEKEEPING_DATA_TABLE
Reading filetype: hk_extended from mer_raw_hk_extended_20200409_1-0651130766-12595__0_1
Processing label: /home/kidpixo/work/esa/mertis_data_management/mertisreader/data/bcmer_tm_all_START-20200409T000000_END-20200410T000000_CRE-20240717T132010-ParamEventBootSciHK-short/raw/mer_raw_hk_extended_20200409_1-0651130766-12595__0_1.lblx
Now processing a Table_Delimited structure: MERTIS_RAW_EXTENDED_HOUSEKEEPING_DATA_TABLE
Reading filetype: tis from mer_raw_sc_tis_20200409_1-0651130819-21186__0_1
Reading filetype: tir from mer_raw_sc_tir_20200409_1-0651130751-42768__0_1
Channels: ['MERTIS_TIR_CHANNEL_A_RAW_SCIENCE_DATA', 'MERTIS_TIR_CHANNEL_B_RAW_SCIENCE_DATA']
Channel A shape (no I/O): (19,)
Materialized: (19,)
The geofile module converts assembled data into geopandas GeoDataFrames and GIS files (GeoParquet, GeoPackage, GeoJSON). It needs the optional geo extra — pip install mertisreader[geo] — and works on the reader state already assembled above.
Each valid pixel becomes a row: a polygon footprint built from the four pixel corners, or a point at the pixel center. The CRS is resolved from the target in the PDS4 label (IAU codes for Moon, Venus, Mercury).
from mertisreader.geofile import build_geodataframe, write_geofile
gdf = build_geodataframe(ms_reader, geometry="polygon")
print(f"Rows (valid pixels): {len(gdf)}")
print(f"CRS: {gdf.crs}")
print(f"Geometry type: {gdf.geometry.iloc[0].geom_type}")
print(f"Columns: pixel, frame, geometry + {gdf.shape[1] - 3} data columns")2026-11-24 19:26:53,893|2112643|WARNING|Target 'Earth' is not one of ['MERCURY', 'MOON', 'VENUS']; falling back to EPSG:4326
Rows (valid pixels): 105
CRS: EPSG:4326
Geometry type: Polygon
Columns: pixel, frame, geometry + 74 data columns
The data columns include per-geometry-variable statistics (*_median, *_std over the 5 corner samples), the metadata time columns, and one column per spectral wavelength channel:
spectral_cols = [c for c in gdf.columns if c not in ("pixel", "frame", "geometry")
and not c.endswith(("_median", "_std"))
and not c.startswith(("TIME", "TimeStamp", "HK_STAT"))]
print(f"Spectral columns ({len(spectral_cols)}): {spectral_cols[:3]} ...")
print(gdf[["pixel", "frame", "TIME_UTC", spectral_cols[0]]].head(3))Spectral columns (40): ['13.934', '13.756', '13.578'] ...
pixel frame TIME_UTC 13.934
0 38 0 2020-04-09T05:40:20.710Z 0.000000
1 38 1 2020-04-09T05:40:31.748Z 13.990876
2 38 2 2020-04-09T05:40:32.548Z 14.714969
Writing to a GIS-ready file — the geometry is built once and cached (geometry cubes never change once generated), so exporting to several formats is cheap:
out_parquet = write_geofile(ms_reader, output_dir=output_path, geometry="polygon", file_format="parquet")
print(f"Written: {out_parquet.name} ({out_parquet.stat().st_size // 1024} KB)")
out_gpkg = write_geofile(ms_reader, output_dir=output_path, geometry="polygon", file_format="gpkg")
print(f"Written: {out_gpkg.name} ({out_gpkg.stat().st_size // 1024} KB)")2026-11-24 19:26:53,947|2112643|WARNING|Target 'Earth' is not one of ['MERCURY', 'MOON', 'VENUS']; falling back to EPSG:4326
2026-11-24 19:26:53,991|2112643|WARNING|Target 'Earth' is not one of ['MERCURY', 'MOON', 'VENUS']; falling back to EPSG:4326
Written: mer_cal_sc_tis_20200409_1-0651130819-21186__0_1.polygon.parquet (103 KB)
Written: mer_cal_sc_tis_20200409_1-0651130819-21186__0_1.polygon.gpkg (192 KB)
The GeoPackage (or GeoParquet) can be opened directly in QGIS or loaded back with geopandas.read_file() / geopandas.read_parquet().
The exported GeoDataFrame is a normal geopandas object, so the pixel footprints can be plotted directly, colored by any data column. The spectral columns are named by their wavelength in micrometers — here we pick one and select a single acquisition frame (frames sweep across the scene, so one frame gives a clean footprint strip):
import matplotlib.pyplot as plt
col = spectral_cols[len(spectral_cols) // 2] # a mid-infrared wavelength
frames_with_signal = gdf.groupby("frame")[col].max()
frame_id = frames_with_signal[frames_with_signal > 0].index[-1]
footprint = gdf[gdf.frame == frame_id]
ax = footprint.plot(column=col, cmap="viridis", legend=True, edgecolor="k")
ax.set_xlabel("Longitude [deg]")
ax.set_ylabel("Latitude [deg]")
ax.set_title(f"TIS radiance at {col} um, frame {frame_id}")
plt.show()
Each polygon is one pixel’s ground footprint (from the four corner coordinates in the geometry cube), filled with the radiance measured by that pixel at the chosen wavelength. Plotting every frame at once shows how the footprints sweep along the track:
Once you know which file key you want to explore, you can inspect frames, metadata, and plots exactly as in the notebook version. The API reference on this site comes from quartodoc-generated pages built from docstrings.