|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +This script loads v0 uncompressed TOAST observations and writes v1 compressed files. |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | +import sys |
| 9 | +import shutil |
| 10 | +import re |
| 11 | +import glob |
| 12 | + |
| 13 | +import datetime |
| 14 | + |
| 15 | +import argparse |
| 16 | + |
| 17 | +import numpy as np |
| 18 | + |
| 19 | +from astropy import units as u |
| 20 | + |
| 21 | +import toast |
| 22 | +import toast.ops |
| 23 | + |
| 24 | +from toast.timing import gather_timers, dump, Timer |
| 25 | + |
| 26 | +from toast.observation import default_values as defaults |
| 27 | + |
| 28 | + |
| 29 | +def parse_arguments(): |
| 30 | + """ |
| 31 | + Defines and parses the arguments for the script. |
| 32 | + """ |
| 33 | + parser = argparse.ArgumentParser( |
| 34 | + description="Compress CMB-S4 simulation data" |
| 35 | + ) |
| 36 | + |
| 37 | + parser.add_argument( |
| 38 | + "--verify", |
| 39 | + required=False, |
| 40 | + action="store_true", |
| 41 | + default=False, |
| 42 | + help="Re-load the converted data and verify consistency", |
| 43 | + ) |
| 44 | + |
| 45 | + parser.add_argument( |
| 46 | + "--obs", |
| 47 | + type=str, |
| 48 | + required=False, |
| 49 | + nargs="+", |
| 50 | + help="One or more observation files", |
| 51 | + ) |
| 52 | + |
| 53 | + # The operators we want to configure from the command line or a parameter file. |
| 54 | + operators = list() |
| 55 | + |
| 56 | + # Parse all of the operator configuration |
| 57 | + config, args, jobargs = toast.parse_config(parser, operators=operators) |
| 58 | + |
| 59 | + return config, args, jobargs |
| 60 | + |
| 61 | + |
| 62 | +def main(): |
| 63 | + env = toast.utils.Environment.get() |
| 64 | + log = toast.utils.Logger.get() |
| 65 | + env.enable_function_timers() |
| 66 | + global_timer = toast.timing.GlobalTimers.get() |
| 67 | + global_timer.start("compress HDF5 (total)") |
| 68 | + |
| 69 | + config, args, jobargs = parse_arguments() |
| 70 | + |
| 71 | + # Default group size |
| 72 | + comm = toast.Comm() |
| 73 | + |
| 74 | + # Process each observation |
| 75 | + for obs_path in args.obs: |
| 76 | + obs_dir = os.path.dirname(obs_path) |
| 77 | + file_root = os.path.splitext(obs_path)[0] |
| 78 | + if comm.world_rank == 0: |
| 79 | + print(f"Working on {obs_path}:") |
| 80 | + backup = f"{file_root}_uncompressed.h5" |
| 81 | + timer = Timer() |
| 82 | + timer.start() |
| 83 | + obs = toast.io.load_hdf5( |
| 84 | + obs_path, |
| 85 | + comm, |
| 86 | + process_rows=comm.group_size, |
| 87 | + meta=None, |
| 88 | + detdata=None, |
| 89 | + shared=None, |
| 90 | + intervals=None, |
| 91 | + detectors=None, |
| 92 | + force_serial=False, |
| 93 | + ) |
| 94 | + |
| 95 | + if comm.comm_world is not None: |
| 96 | + comm.comm_world.barrier() |
| 97 | + timer.stop() |
| 98 | + if comm.world_rank == 0: |
| 99 | + print(f" Load {obs_path} in {timer.seconds()} s", flush=True) |
| 100 | + |
| 101 | + if comm.world_rank == 0: |
| 102 | + os.rename(obs_path, backup) |
| 103 | + |
| 104 | + if comm.comm_world is not None: |
| 105 | + comm.comm_world.barrier() |
| 106 | + |
| 107 | + timer.start() |
| 108 | + obf = toast.io.save_hdf5( |
| 109 | + obs, |
| 110 | + obs_dir, |
| 111 | + meta=None, |
| 112 | + detdata=[ |
| 113 | + (defaults.det_data, {"type": "flac"}), |
| 114 | + (defaults.det_flags, {"type": "gzip"}), |
| 115 | + ], |
| 116 | + shared=None, |
| 117 | + intervals=None, |
| 118 | + config=None, |
| 119 | + times=defaults.times, |
| 120 | + force_serial=False, |
| 121 | + ) |
| 122 | + if comm.comm_world is not None: |
| 123 | + comm.comm_world.barrier() |
| 124 | + timer.stop() |
| 125 | + if comm.world_rank == 0: |
| 126 | + print(f" Save {obs_path} in {timer.seconds()} s", flush=True) |
| 127 | + |
| 128 | + if obf != obs_path: |
| 129 | + msg = f"Generated HDF5 ({obf}) does not match original " |
| 130 | + msg += f"file name ({obs_path})" |
| 131 | + raise RuntimeError(msg) |
| 132 | + |
| 133 | + if args.verify: |
| 134 | + timer.start() |
| 135 | + compare = toast.io.load_hdf5( |
| 136 | + obs_path, |
| 137 | + comm, |
| 138 | + process_rows=comm.group_size, |
| 139 | + ) |
| 140 | + if comm.comm_world is not None: |
| 141 | + comm.comm_world.barrier() |
| 142 | + timer.stop() |
| 143 | + if comm.world_rank == 0: |
| 144 | + print( |
| 145 | + f" Re-load {obs_path} for verification in {timer.seconds()} s", |
| 146 | + flush=True |
| 147 | + ) |
| 148 | + |
| 149 | + if compare != obs: |
| 150 | + msg = f"Observation HDF5 verify failed:\n" |
| 151 | + msg += f"Input = {obs}\n" |
| 152 | + msg += f"Loaded = {compare}" |
| 153 | + log.error(msg) |
| 154 | + raise RuntimeError(msg) |
| 155 | + elif comm.world_rank == 0: |
| 156 | + print(f" Verification PASS", flush=True) |
| 157 | + else: |
| 158 | + if comm.world_rank == 0: |
| 159 | + print(f" Skipping verification", flush=True) |
| 160 | + |
| 161 | + # Dump all the timing information to the output dir |
| 162 | + |
| 163 | + global_timer.stop("compress HDF5 (total)") |
| 164 | + alltimers = gather_timers(comm=comm.comm_world) |
| 165 | + if comm.world_rank == 0: |
| 166 | + out = os.path.join(".", "timing") |
| 167 | + dump(alltimers, out) |
| 168 | + |
| 169 | + |
| 170 | +if __name__ == "__main__": |
| 171 | + world, procs, rank = toast.mpi.get_world() |
| 172 | + with toast.mpi.exception_guard(comm=world): |
| 173 | + main() |
0 commit comments