cesm-hawc 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. cesm_hawc-0.3.0/LICENSE +21 -0
  2. cesm_hawc-0.3.0/PKG-INFO +205 -0
  3. cesm_hawc-0.3.0/README.md +162 -0
  4. cesm_hawc-0.3.0/pyproject.toml +73 -0
  5. cesm_hawc-0.3.0/setup.cfg +4 -0
  6. cesm_hawc-0.3.0/src/cesm_hawc/__init__.py +15 -0
  7. cesm_hawc-0.3.0/src/cesm_hawc/calibration.py +111 -0
  8. cesm_hawc-0.3.0/src/cesm_hawc/cli.py +772 -0
  9. cesm_hawc-0.3.0/src/cesm_hawc/config.py +327 -0
  10. cesm_hawc-0.3.0/src/cesm_hawc/constituents.py +256 -0
  11. cesm_hawc-0.3.0/src/cesm_hawc/convergence.py +90 -0
  12. cesm_hawc-0.3.0/src/cesm_hawc/data/example_column.nc +0 -0
  13. cesm_hawc-0.3.0/src/cesm_hawc/dispatch.py +93 -0
  14. cesm_hawc-0.3.0/src/cesm_hawc/env.py +74 -0
  15. cesm_hawc-0.3.0/src/cesm_hawc/file_index.py +191 -0
  16. cesm_hawc-0.3.0/src/cesm_hawc/noise.py +35 -0
  17. cesm_hawc-0.3.0/src/cesm_hawc/orbit_files.py +288 -0
  18. cesm_hawc-0.3.0/src/cesm_hawc/outputs.py +54 -0
  19. cesm_hawc-0.3.0/src/cesm_hawc/resume.py +108 -0
  20. cesm_hawc-0.3.0/src/cesm_hawc/save_inputs.py +138 -0
  21. cesm_hawc-0.3.0/src/cesm_hawc/simulation.py +279 -0
  22. cesm_hawc-0.3.0/src/cesm_hawc/waccm.py +627 -0
  23. cesm_hawc-0.3.0/src/cesm_hawc.egg-info/PKG-INFO +205 -0
  24. cesm_hawc-0.3.0/src/cesm_hawc.egg-info/SOURCES.txt +38 -0
  25. cesm_hawc-0.3.0/src/cesm_hawc.egg-info/dependency_links.txt +1 -0
  26. cesm_hawc-0.3.0/src/cesm_hawc.egg-info/entry_points.txt +2 -0
  27. cesm_hawc-0.3.0/src/cesm_hawc.egg-info/requires.txt +20 -0
  28. cesm_hawc-0.3.0/src/cesm_hawc.egg-info/top_level.txt +1 -0
  29. cesm_hawc-0.3.0/tests/test_calibration.py +26 -0
  30. cesm_hawc-0.3.0/tests/test_cli.py +149 -0
  31. cesm_hawc-0.3.0/tests/test_config.py +75 -0
  32. cesm_hawc-0.3.0/tests/test_constituents.py +47 -0
  33. cesm_hawc-0.3.0/tests/test_convergence.py +45 -0
  34. cesm_hawc-0.3.0/tests/test_file_index.py +64 -0
  35. cesm_hawc-0.3.0/tests/test_orbit_files.py +87 -0
  36. cesm_hawc-0.3.0/tests/test_save_inputs.py +63 -0
  37. cesm_hawc-0.3.0/tests/test_save_inputs_roundtrip.py +102 -0
  38. cesm_hawc-0.3.0/tests/test_simulation.py +75 -0
  39. cesm_hawc-0.3.0/tests/test_waccm_atmosphere.py +121 -0
  40. cesm_hawc-0.3.0/tests/test_waccm_math.py +57 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Victoria McDonald
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,205 @@
1
+ Metadata-Version: 2.4
2
+ Name: cesm-hawc
3
+ Version: 0.3.0
4
+ Summary: Simulate HAWC ALI limb observations and retrievals from CESM2/WACCM output
5
+ Author: Victoria McDonald
6
+ License-Expression: MIT
7
+ Project-URL: Documentation, https://torimcd.github.io/cesm-hawc/
8
+ Project-URL: Source, https://github.com/torimcd/cesm-hawc
9
+ Project-URL: Issues, https://github.com/torimcd/cesm-hawc/issues
10
+ Project-URL: Changelog, https://torimcd.github.io/cesm-hawc/changelog
11
+ Keywords: CESM,WACCM,stratospheric aerosol,limb scattering,HAWC,ALI,remote sensing
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3 :: Only
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Classifier: Programming Language :: Python :: 3.14
21
+ Classifier: Topic :: Scientific/Engineering :: Atmospheric Science
22
+ Requires-Python: >=3.11
23
+ Description-Content-Type: text/markdown
24
+ License-File: LICENSE
25
+ Requires-Dist: numpy
26
+ Requires-Dist: xarray
27
+ Requires-Dist: scipy
28
+ Requires-Dist: pandas
29
+ Requires-Dist: netCDF4
30
+ Requires-Dist: dask
31
+ Provides-Extra: sim
32
+ Requires-Dist: aliprocessing>=2026.10.0; extra == "sim"
33
+ Requires-Dist: hawcsimulator>=2026.6.0; extra == "sim"
34
+ Requires-Dist: sasktran2; extra == "sim"
35
+ Provides-Extra: dev
36
+ Requires-Dist: pytest; extra == "dev"
37
+ Requires-Dist: jupyter; extra == "dev"
38
+ Requires-Dist: matplotlib; extra == "dev"
39
+ Requires-Dist: cartopy; extra == "dev"
40
+ Provides-Extra: docs
41
+ Requires-Dist: jupyter-book>=2; extra == "docs"
42
+ Dynamic: license-file
43
+
44
+ # cesm-hawc
45
+
46
+ Feed CESM2/WACCM atmosphere model output into the
47
+ [HAWC ALI simulator](https://github.com/usask-arg/hawc-simulator) to
48
+ simulate what the HAWCSat Aerosol Limb Imager would observe.
49
+
50
+ **Documentation:** <https://torimcd.github.io/cesm-hawc/>, including a step-by-step
51
+ [tutorial](https://torimcd.github.io/cesm-hawc/tutorial) on an example column.
52
+
53
+ ## What this does
54
+
55
+ Given CESM/WACCM history output this package:
56
+
57
+ 1. Extracts one or many atmospheric columns (T, P, O₃, MAM4 sulfate aerosol)
58
+ 2. Converts MAM4 modal aerosol to extinction profiles using mode-matched Mie databases
59
+ 3. Runs the `IdealALISimulator` forward model and, optionally, the L2 retrieval
60
+ 4. Outputs retrieved aerosol extinction and median radius profiles
61
+
62
+ Requires Python >=3.11. It has two tiers:
63
+
64
+ - **Base** (`pip install cesm-hawc`) — WACCM column extraction and saving
65
+ simulator-ready inputs. Only needs numpy, xarray, scipy, pandas, netCDF4
66
+ and dask.
67
+ - **`[sim]` extra** — the full forward model and L2 retrieval, via
68
+ `hawcsimulator` + `sasktran2`. Install it into the project's conda
69
+ environment (see below).
70
+
71
+ ## Install
72
+
73
+ To run the simulator, install into the project's conda environment
74
+ (recommended):
75
+
76
+ ```bash
77
+ curl -O https://raw.githubusercontent.com/torimcd/cesm-hawc/main/environment.yml
78
+ conda env create -f environment.yml
79
+ conda activate hawc_env
80
+ pip install "cesm-hawc[sim]"
81
+ ```
82
+
83
+ For the base tier only (no simulator), pip on its own is enough:
84
+
85
+ ```bash
86
+ pip install cesm-hawc
87
+ ```
88
+
89
+ **From source (development):**
90
+
91
+ ```bash
92
+ git clone https://github.com/torimcd/cesm-hawc
93
+ cd cesm-hawc
94
+ conda env create -f environment.yml
95
+ conda activate hawc_env
96
+ pip install -e ".[sim,dev]"
97
+ ```
98
+
99
+ See the [installation guide](https://torimcd.github.io/cesm-hawc/installation)
100
+ for details.
101
+
102
+ **Alliance Canada HPC (Fir/Rorqual/Narval):**
103
+
104
+ ```bash
105
+ git clone https://github.com/torimcd/cesm-hawc
106
+ cd cesm-hawc
107
+ bash scripts/setup/create_env.sh
108
+ ```
109
+
110
+ That script creates the `hawc_env` micromamba environment, installs all
111
+ dependencies, and registers the package. It takes 5–10 minutes on first run.
112
+
113
+ ## Quick start
114
+
115
+ ### CLI
116
+
117
+ Copy `config.example.toml` to `config.toml` and fill in your paths. Each run
118
+ processes one model case, described by the `[case]` table, and writes under
119
+ `out_dir/<case name>/`.
120
+
121
+ ```bash
122
+ # Base tier: extract and save WACCM column inputs, no [sim] extra needed
123
+ cesm-hawc save-inputs --config config.toml --mode fixed
124
+
125
+ # [sim] tier: run the forward model (+ L2 retrieval if run_l2 = true)
126
+ cesm-hawc run --config config.toml --mode fixed
127
+ ```
128
+
129
+ `--mode` selects how the model is sampled:
130
+
131
+ | Mode | Samples | Config table |
132
+ |------|---------|--------------|
133
+ | `fixed` | one column at a fixed tangent point and solar geometry, per history file | `[fixed]` |
134
+ | `orbit` | columns along a real HAWC orbit ground track, moved onto the case's dates | `[orbit]` |
135
+
136
+ Add `--dry-run` to see the job count without running anything, and
137
+ `--case-name NAME` to run a different case from the same config. Run
138
+ `cesm-hawc save-inputs --help` / `cesm-hawc run --help` for the full flag
139
+ list.
140
+
141
+ ### Python API
142
+
143
+ ```python
144
+ import cesm_hawc
145
+ from cesm_hawc.noise import default_noise_model
146
+ from cesm_hawc.simulation import run_ali_simulation
147
+
148
+ cesm_hawc.configure_environment() # once per process; the CLI does this for you
149
+
150
+ result = run_ali_simulation(
151
+ "path/to/my_case.cam.h0.2035-02.nc",
152
+ lat=30.6, lon=180.0,
153
+ run_l2=True,
154
+ noise_model=default_noise_model(),
155
+ )
156
+ l2 = result["data"]["l2"] # retrieved extinction, median radius, ...
157
+ ```
158
+
159
+ Library users calling into `cesm_hawc.orbit_files`, `cesm_hawc.calibration`,
160
+ or `cesm_hawc.simulation` directly (rather than through the CLI) should call
161
+ `cesm_hawc.configure_environment()` once at startup — it disables astropy's
162
+ IERS auto-download, silences noisy third-party logging, and patches a known
163
+ `hawcsimulator` calibration-cache race condition.
164
+
165
+ ## Using saved inputs without cesm-hawc
166
+
167
+ When `sasktran2` is installed, each file written by `save-inputs` contains
168
+ everything needed to rebuild the simulator's aerosol and gas constituents
169
+ with native `sasktran2` calls: per-mode reference and truth extinction,
170
+ clipped median radius, and the Mie database build parameters as attributes.
171
+ See [Using saved inputs](https://torimcd.github.io/cesm-hawc/saved-inputs) for a complete example.
172
+
173
+ ## Required WACCM output variables
174
+
175
+ Add these to `fincl` in `user_nl_cam` if not already present:
176
+
177
+ | Variable | Description |
178
+ |----------|-------------|
179
+ | `T`, `Q`, `PS` | Temperature, humidity, surface pressure (required) |
180
+ | `hyam`, `hybm`, `P0` | Hybrid level coefficients and reference pressure (`hyam`, `hybm` required) |
181
+ | `O3`, `NO2`, `SO2`, `H2O` | Gas chemistry (mol/mol) |
182
+ | `so4_a1`, `so4_a3` | Dry sulfate mass mixing ratio (kg/kg) |
183
+ | `num_a1`, `num_a3` | Aerosol number mixing ratio (#/kg) |
184
+ | `dgnumwet1`, `dgnumwet3` | Aerosol wet median diameter (m) |
185
+
186
+ See [WACCM output variables](https://torimcd.github.io/cesm-hawc/waccm-variables) for details.
187
+
188
+ ## Citing
189
+
190
+ If you use cesm-hawc in published work, please cite it using the details in
191
+ [`CITATION.cff`](CITATION.cff) (on GitHub, "Cite this repository"), and cite
192
+ the simulator packages it builds on; see
193
+ [Citing cesm-hawc](https://torimcd.github.io/cesm-hawc/citing).
194
+
195
+ ## Testing
196
+
197
+ ```bash
198
+ pip install -e ".[dev]"
199
+ pytest
200
+ ```
201
+
202
+ Tests that need `sasktran2`/`hawcsimulator` are automatically skipped if
203
+ those aren't installed. A small bundled example column
204
+ (`src/cesm_hawc/data/example_column.nc`) is used for fixture-based tests
205
+ and doesn't require any external data.
@@ -0,0 +1,162 @@
1
+ # cesm-hawc
2
+
3
+ Feed CESM2/WACCM atmosphere model output into the
4
+ [HAWC ALI simulator](https://github.com/usask-arg/hawc-simulator) to
5
+ simulate what the HAWCSat Aerosol Limb Imager would observe.
6
+
7
+ **Documentation:** <https://torimcd.github.io/cesm-hawc/>, including a step-by-step
8
+ [tutorial](https://torimcd.github.io/cesm-hawc/tutorial) on an example column.
9
+
10
+ ## What this does
11
+
12
+ Given CESM/WACCM history output this package:
13
+
14
+ 1. Extracts one or many atmospheric columns (T, P, O₃, MAM4 sulfate aerosol)
15
+ 2. Converts MAM4 modal aerosol to extinction profiles using mode-matched Mie databases
16
+ 3. Runs the `IdealALISimulator` forward model and, optionally, the L2 retrieval
17
+ 4. Outputs retrieved aerosol extinction and median radius profiles
18
+
19
+ Requires Python >=3.11. It has two tiers:
20
+
21
+ - **Base** (`pip install cesm-hawc`) — WACCM column extraction and saving
22
+ simulator-ready inputs. Only needs numpy, xarray, scipy, pandas, netCDF4
23
+ and dask.
24
+ - **`[sim]` extra** — the full forward model and L2 retrieval, via
25
+ `hawcsimulator` + `sasktran2`. Install it into the project's conda
26
+ environment (see below).
27
+
28
+ ## Install
29
+
30
+ To run the simulator, install into the project's conda environment
31
+ (recommended):
32
+
33
+ ```bash
34
+ curl -O https://raw.githubusercontent.com/torimcd/cesm-hawc/main/environment.yml
35
+ conda env create -f environment.yml
36
+ conda activate hawc_env
37
+ pip install "cesm-hawc[sim]"
38
+ ```
39
+
40
+ For the base tier only (no simulator), pip on its own is enough:
41
+
42
+ ```bash
43
+ pip install cesm-hawc
44
+ ```
45
+
46
+ **From source (development):**
47
+
48
+ ```bash
49
+ git clone https://github.com/torimcd/cesm-hawc
50
+ cd cesm-hawc
51
+ conda env create -f environment.yml
52
+ conda activate hawc_env
53
+ pip install -e ".[sim,dev]"
54
+ ```
55
+
56
+ See the [installation guide](https://torimcd.github.io/cesm-hawc/installation)
57
+ for details.
58
+
59
+ **Alliance Canada HPC (Fir/Rorqual/Narval):**
60
+
61
+ ```bash
62
+ git clone https://github.com/torimcd/cesm-hawc
63
+ cd cesm-hawc
64
+ bash scripts/setup/create_env.sh
65
+ ```
66
+
67
+ That script creates the `hawc_env` micromamba environment, installs all
68
+ dependencies, and registers the package. It takes 5–10 minutes on first run.
69
+
70
+ ## Quick start
71
+
72
+ ### CLI
73
+
74
+ Copy `config.example.toml` to `config.toml` and fill in your paths. Each run
75
+ processes one model case, described by the `[case]` table, and writes under
76
+ `out_dir/<case name>/`.
77
+
78
+ ```bash
79
+ # Base tier: extract and save WACCM column inputs, no [sim] extra needed
80
+ cesm-hawc save-inputs --config config.toml --mode fixed
81
+
82
+ # [sim] tier: run the forward model (+ L2 retrieval if run_l2 = true)
83
+ cesm-hawc run --config config.toml --mode fixed
84
+ ```
85
+
86
+ `--mode` selects how the model is sampled:
87
+
88
+ | Mode | Samples | Config table |
89
+ |------|---------|--------------|
90
+ | `fixed` | one column at a fixed tangent point and solar geometry, per history file | `[fixed]` |
91
+ | `orbit` | columns along a real HAWC orbit ground track, moved onto the case's dates | `[orbit]` |
92
+
93
+ Add `--dry-run` to see the job count without running anything, and
94
+ `--case-name NAME` to run a different case from the same config. Run
95
+ `cesm-hawc save-inputs --help` / `cesm-hawc run --help` for the full flag
96
+ list.
97
+
98
+ ### Python API
99
+
100
+ ```python
101
+ import cesm_hawc
102
+ from cesm_hawc.noise import default_noise_model
103
+ from cesm_hawc.simulation import run_ali_simulation
104
+
105
+ cesm_hawc.configure_environment() # once per process; the CLI does this for you
106
+
107
+ result = run_ali_simulation(
108
+ "path/to/my_case.cam.h0.2035-02.nc",
109
+ lat=30.6, lon=180.0,
110
+ run_l2=True,
111
+ noise_model=default_noise_model(),
112
+ )
113
+ l2 = result["data"]["l2"] # retrieved extinction, median radius, ...
114
+ ```
115
+
116
+ Library users calling into `cesm_hawc.orbit_files`, `cesm_hawc.calibration`,
117
+ or `cesm_hawc.simulation` directly (rather than through the CLI) should call
118
+ `cesm_hawc.configure_environment()` once at startup — it disables astropy's
119
+ IERS auto-download, silences noisy third-party logging, and patches a known
120
+ `hawcsimulator` calibration-cache race condition.
121
+
122
+ ## Using saved inputs without cesm-hawc
123
+
124
+ When `sasktran2` is installed, each file written by `save-inputs` contains
125
+ everything needed to rebuild the simulator's aerosol and gas constituents
126
+ with native `sasktran2` calls: per-mode reference and truth extinction,
127
+ clipped median radius, and the Mie database build parameters as attributes.
128
+ See [Using saved inputs](https://torimcd.github.io/cesm-hawc/saved-inputs) for a complete example.
129
+
130
+ ## Required WACCM output variables
131
+
132
+ Add these to `fincl` in `user_nl_cam` if not already present:
133
+
134
+ | Variable | Description |
135
+ |----------|-------------|
136
+ | `T`, `Q`, `PS` | Temperature, humidity, surface pressure (required) |
137
+ | `hyam`, `hybm`, `P0` | Hybrid level coefficients and reference pressure (`hyam`, `hybm` required) |
138
+ | `O3`, `NO2`, `SO2`, `H2O` | Gas chemistry (mol/mol) |
139
+ | `so4_a1`, `so4_a3` | Dry sulfate mass mixing ratio (kg/kg) |
140
+ | `num_a1`, `num_a3` | Aerosol number mixing ratio (#/kg) |
141
+ | `dgnumwet1`, `dgnumwet3` | Aerosol wet median diameter (m) |
142
+
143
+ See [WACCM output variables](https://torimcd.github.io/cesm-hawc/waccm-variables) for details.
144
+
145
+ ## Citing
146
+
147
+ If you use cesm-hawc in published work, please cite it using the details in
148
+ [`CITATION.cff`](CITATION.cff) (on GitHub, "Cite this repository"), and cite
149
+ the simulator packages it builds on; see
150
+ [Citing cesm-hawc](https://torimcd.github.io/cesm-hawc/citing).
151
+
152
+ ## Testing
153
+
154
+ ```bash
155
+ pip install -e ".[dev]"
156
+ pytest
157
+ ```
158
+
159
+ Tests that need `sasktran2`/`hawcsimulator` are automatically skipped if
160
+ those aren't installed. A small bundled example column
161
+ (`src/cesm_hawc/data/example_column.nc`) is used for fixture-based tests
162
+ and doesn't require any external data.
@@ -0,0 +1,73 @@
1
+ [build-system]
2
+ requires = ["setuptools>=77"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "cesm-hawc"
7
+ dynamic = ["version"]
8
+ description = "Simulate HAWC ALI limb observations and retrievals from CESM2/WACCM output"
9
+ readme = "README.md"
10
+ license = "MIT"
11
+ license-files = ["LICENSE"]
12
+ authors = [{ name = "Victoria McDonald" }]
13
+ requires-python = ">=3.11"
14
+ keywords = ["CESM", "WACCM", "stratospheric aerosol", "limb scattering", "HAWC", "ALI", "remote sensing"]
15
+ classifiers = [
16
+ "Development Status :: 4 - Beta",
17
+ "Intended Audience :: Science/Research",
18
+ "Operating System :: OS Independent",
19
+ "Programming Language :: Python :: 3",
20
+ "Programming Language :: Python :: 3 :: Only",
21
+ "Programming Language :: Python :: 3.11",
22
+ "Programming Language :: Python :: 3.12",
23
+ "Programming Language :: Python :: 3.13",
24
+ "Programming Language :: Python :: 3.14",
25
+ "Topic :: Scientific/Engineering :: Atmospheric Science",
26
+ ]
27
+ dependencies = [
28
+ "numpy",
29
+ "xarray",
30
+ "scipy",
31
+ "pandas",
32
+ "netCDF4",
33
+ "dask",
34
+ ]
35
+
36
+ [project.urls]
37
+ Documentation = "https://torimcd.github.io/cesm-hawc/"
38
+ Source = "https://github.com/torimcd/cesm-hawc"
39
+ Issues = "https://github.com/torimcd/cesm-hawc/issues"
40
+ Changelog = "https://torimcd.github.io/cesm-hawc/changelog"
41
+
42
+ [project.scripts]
43
+ cesm-hawc = "cesm_hawc.cli:main"
44
+
45
+ [project.optional-dependencies]
46
+ # End-to-end simulation (the `cesm-hawc run` CLI command / cesm_hawc.simulation
47
+ # / cesm_hawc.constituents).
48
+ sim = [
49
+ "aliprocessing>=2026.10.0",
50
+ "hawcsimulator>=2026.6.0",
51
+ "sasktran2",
52
+ ]
53
+ dev = ["pytest", "jupyter", "matplotlib", "cartopy"]
54
+ docs = ["jupyter-book>=2"]
55
+
56
+ [tool.setuptools.dynamic]
57
+ version = { attr = "cesm_hawc.__version__" }
58
+
59
+ [tool.setuptools.packages.find]
60
+ where = ["src"]
61
+
62
+ [tool.setuptools.package-data]
63
+ cesm_hawc = ["data/*.nc"]
64
+
65
+ [tool.ruff.lint]
66
+ select = ["E4", "E7", "E9", "F", "D"]
67
+ ignore = ["D105", "D107"] # magic methods; __init__ is documented on the class
68
+
69
+ [tool.ruff.lint.pydocstyle]
70
+ convention = "numpy"
71
+
72
+ [tool.pytest.ini_options]
73
+ testpaths = ["tests"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,15 @@
1
+ """Simulate HAWC ALI observations and retrievals from CESM2/WACCM output.
2
+
3
+ The package has two tiers. The base install extracts WACCM columns and
4
+ saves simulator-ready inputs (:mod:`cesm_hawc.waccm`,
5
+ :mod:`cesm_hawc.save_inputs`). The ``[sim]`` extra adds the ALI forward
6
+ model and L2 retrieval (:mod:`cesm_hawc.simulation`,
7
+ :mod:`cesm_hawc.constituents`). Call :func:`configure_environment` once
8
+ before using the ``[sim]`` tier from your own code.
9
+ """
10
+
11
+ from cesm_hawc.env import configure_environment
12
+
13
+ __all__ = ["configure_environment"]
14
+
15
+ __version__ = "0.3.0" # the package version; pyproject.toml reads it from here
@@ -0,0 +1,111 @@
1
+ """Workarounds for cache races when many simulations run in parallel.
2
+
3
+ ``hawcsimulator`` and ``aliprocessing`` build cached files the first time a
4
+ simulator is constructed. When many worker processes start at once, they
5
+ race to write those files. The functions here pre-build the caches once in
6
+ the main process and stop ``hawcsimulator`` rewriting its calibration file
7
+ on every call. They do nothing if the ``[sim]`` extra isn't installed.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import logging
13
+ import os
14
+
15
+ log = logging.getLogger(__name__)
16
+
17
+ _patched = False
18
+
19
+
20
+ def _cache_file_path(name: str, version: str) -> str:
21
+ """Path of ``hawcsimulator``'s cached calibration file for ``(name, version)``.
22
+
23
+ Built the same way as ``hawcsimulator.ali.calibration``, from the
24
+ platform's user data directory (e.g. ``~/.local/share`` on Linux,
25
+ ``~/Library/Application Support`` on macOS).
26
+ """
27
+ import appdirs # a hawcsimulator dependency
28
+
29
+ data_dir = appdirs.AppDirs(appname="hawc-simulator", appauthor="usask-arg").user_data_dir
30
+ return os.path.join(data_dir, "ali", "calibration", f"{name}_{version}.nc")
31
+
32
+
33
+ def patch_calibration_database_race() -> None:
34
+ """Stop ``hawcsimulator`` rewriting its calibration cache on every call.
35
+
36
+ ``hawcsimulator``'s ``calibration_database()`` rewrites its cached NetCDF
37
+ file every time it is called, including whenever an
38
+ ``IdealALISimulator`` is constructed. With many worker processes using
39
+ the same (often network-mounted) file, this causes ``PermissionError``
40
+ and ``KeyError`` races. After patching, an existing cache file is reused
41
+ instead. This is safe because the file's contents depend only on its
42
+ ``(name, version)``.
43
+
44
+ Safe to call more than once; does nothing if ``hawcsimulator`` isn't
45
+ installed. :func:`cesm_hawc.configure_environment` calls it for you.
46
+ """
47
+ global _patched
48
+ if _patched:
49
+ return
50
+ try:
51
+ from hawcsimulator.ali import calibration as _cal_mod
52
+ from hawcsimulator.ali.configurations import (
53
+ ideal_dolp_imager as _ideal_dolp_imager_mod,
54
+ )
55
+ except ImportError:
56
+ return
57
+
58
+ _orig_calibration_database = _cal_mod.calibration_database
59
+
60
+ def _safe_calibration_database(name: str, version: str):
61
+ cache_file = _cache_file_path(name, version)
62
+ if os.path.exists(cache_file):
63
+ return cache_file
64
+ return _orig_calibration_database(name, version)
65
+
66
+ _cal_mod.calibration_database = _safe_calibration_database
67
+ _ideal_dolp_imager_mod.calibration_database = _safe_calibration_database
68
+ _patched = True
69
+
70
+
71
+ def warm_calibration_database(name: str = "ideal_spectrograph", version: str = "v1") -> None:
72
+ """Build ``hawcsimulator``'s calibration cache file once.
73
+
74
+ Call this in the main process before starting worker processes, so they
75
+ don't all race to create the file. Failures are logged as warnings, not
76
+ raised.
77
+
78
+ Parameters
79
+ ----------
80
+ name, version : str, optional
81
+ Calibration dataset to build. The defaults are the dataset the
82
+ ``ideal_dolp_imager`` simulator uses.
83
+ """
84
+ try:
85
+ from hawcsimulator.ali.calibration import calibration_database
86
+ except ImportError:
87
+ return
88
+ try:
89
+ calibration_database(name, version)
90
+ except Exception as e:
91
+ log.warning("Could not pre-warm calibration database: %s", e)
92
+
93
+
94
+ def warm_retrieval_optical_database() -> None:
95
+ """Build the retrieval's Mie database cache once.
96
+
97
+ Every ``IdealALISimulator`` builds ``aliprocessing``'s
98
+ ``aerosol_median_radius_db()`` when constructed. Call this in the main
99
+ process before starting worker processes, so they don't race to write
100
+ it (a worker reading a half-written file fails with an xarray "did not
101
+ find a match in any of xarray's currently installed IO backends"
102
+ error). Failures are logged as warnings, not raised.
103
+ """
104
+ try:
105
+ from aliprocessing.l2.optical import aerosol_median_radius_db
106
+ except ImportError:
107
+ return
108
+ try:
109
+ aerosol_median_radius_db()
110
+ except Exception as e:
111
+ log.warning("Could not pre-warm retrieval optical database: %s", e)