Skip to content

Commit 653c62d

Browse files
authored
Merge pull request #169 from ClimateImpactLab/climate_format_update
Climate input files format update
2 parents ff17ee8 + 1f1d8e6 commit 653c62d

File tree

5 files changed

+31
-116
lines changed

5 files changed

+31
-116
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- Dropped optional/unused dependencies `click`, `dask-jobqueue`, `geopandas`, `gurobipy`, `ipywidgets`, `seaborn`. ([PR #99](https://github.com/ClimateImpactLab/dscim/pull/99), [@brews](https://github.com/brews))
1616
- Switch build system from `setuptools` to `hatchling`. ([PR #128](https://github.com/ClimateImpactLab/dscim/pull/128), [@brews](https://github.com/brews))
1717
- Clean up unit test for `dscim.utils.utils.c_equivalence`. ([PR #135](https://github.com/ClimateImpactLab/dscim/pull/135), [@brews](https://github.com/brews))
18+
- Reformat gmst/gmsl pulse files by removing unnecessary dimensions and indices. ([PR #169](https://github.com/ClimateImpactLab/dscim/pull/169), [@JMGilbert](https://github.com/JMGilbert))
1819

1920
### Fixed
2021

src/dscim/menu/simple_storage.py

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -131,39 +131,8 @@ def gmsl_anomalies(self):
131131
"""This function takes coastal sector's GMSL relative to 1991-2009.
132132
No rebasing occurs, as coastal damages are rebased to the same period.
133133
"""
134-
df = xr.open_zarr(self.gmsl_fair_path)
135-
df = df.chunk(df.dims)
136-
137-
datasets = []
138-
139-
# collapse runtype dimension into two variables, and label
140-
# each one (control, pulse) for medians and full simulations
141-
for var in df.keys():
142-
ds = df[var].to_dataset(dim="runtype")
143-
ds = ds.rename({k: f"{k}_{var}" for k in ds.keys()})
144-
datasets.append(ds)
145-
146-
anomaly = xr.combine_by_coords(datasets, combine_attrs="override")
147-
148-
# drop unnecessary coordinates
149-
anomaly = anomaly.drop_vars(
150-
["confidence", "kind", "locations", "workflow_src"],
151-
errors="ignore",
152-
)
153-
154-
# rename variables
155-
if (
156-
"pulse_gmsl_median" in anomaly.keys()
157-
and "control_gmsl_median" in anomaly.keys()
158-
):
159-
anomaly = anomaly.rename(
160-
{
161-
"pulse_gmsl_median": "medianparams_pulse_gmsl",
162-
"control_gmsl_median": "medianparams_control_gmsl",
163-
}
164-
)
165-
else:
166-
pass
134+
anomaly = xr.open_dataset(self.gmsl_fair_path)
135+
anomaly = anomaly.chunk(anomaly.dims)
167136

168137
return anomaly
169138

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def climate():
2222
gmsl_path=os.path.join(data_dir, "gmsl.csv"),
2323
gmst_path=os.path.join(datadir, "gmst.csv"),
2424
gmst_fair_path=os.path.join(datadir, "fair_temps_sims.nc4"),
25-
gmsl_fair_path=os.path.join(datadir, "fair_slr_sims.zarr"),
25+
gmsl_fair_path=os.path.join(datadir, "fair_slr_sims.nc4"),
2626
damages_pulse_conversion_path=os.path.join(
2727
datadir,
2828
"scenario_rcp45-rcp85-ssp245-ssp460-ssp370_CO2_Fossil-CH4-N2O_conversion_pulseyears_2020-2100_5yrincrements_v3.0_newformat.nc",
132 KB
Binary file not shown.

tests/test_simple_storage.py

Lines changed: 27 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,22 @@ def test_climate_gmsl_anomalies(tmp_path):
159159
d.mkdir()
160160
infile_path = d / "gmsl_fair.zarr"
161161
gases = ["CO2_Fossil", "CH4", "N2O"]
162-
# This struction might appear weird, but matches that used for initial EPA runs.
162+
163163
ds_in = xr.Dataset(
164164
{
165-
"gmsl": (
166-
["runtype", "pulse_year", "simulation", "runid", "year", "gas"],
167-
np.ones((2, 1, 1, 2, 1, 3)),
165+
"pulse_gmsl": (
166+
["pulse_year", "runid", "year", "gas"],
167+
np.ones((1, 2, 1, 3)),
168+
),
169+
"control_gmsl": (
170+
["runid", "year"],
171+
np.ones((2, 1)),
168172
),
169173
},
170174
coords={
171175
"gas": (["gas"], gases),
172176
"pulse_year": (["pulse_year"], [2020]),
173177
"runid": (["runid"], [1, 2]),
174-
"runtype": (["runtype"], ["control", "pulse"]),
175-
"simulation": (["simulation"], [1]),
176178
"year": (["year"], [2020]),
177179
},
178180
)
@@ -181,19 +183,18 @@ def test_climate_gmsl_anomalies(tmp_path):
181183
expected = xr.Dataset(
182184
{
183185
"pulse_gmsl": (
184-
["pulse_year", "simulation", "runid", "year", "gas"],
185-
np.ones((1, 1, 2, 1, 3)),
186+
["pulse_year", "runid", "year", "gas"],
187+
np.ones((1, 2, 1, 3)),
186188
),
187189
"control_gmsl": (
188-
["pulse_year", "simulation", "runid", "year", "gas"],
189-
np.ones((1, 1, 2, 1, 3)),
190+
["runid", "year"],
191+
np.ones((2, 1)),
190192
),
191193
},
192194
coords={
193195
"gas": (["gas"], gases),
194196
"pulse_year": (["pulse_year"], [2020]),
195197
"runid": (["runid"], [1, 2]),
196-
"simulation": (["simulation"], [1]),
197198
"year": (["year"], [2020]),
198199
},
199200
)
@@ -214,66 +215,6 @@ def test_climate_gmsl_anomalies(tmp_path):
214215
xr.testing.assert_equal(actual, expected)
215216

216217

217-
@pytest.mark.parametrize(
218-
"unnecesary_coord", ["confidence", "kind", "locations", "workflow_src"]
219-
)
220-
def test_climate_gmsl_anomalies_dropcoords(tmp_path, unnecesary_coord):
221-
"""
222-
Test that Climate instances give "gmsl_anomalies" with "unnecessary coords" dropped
223-
"""
224-
# Set up input data in temporary directory because Climate needs to read
225-
# from a magical Zarr Store.
226-
d = tmp_path / "climate"
227-
d.mkdir()
228-
infile_path = d / "gmsl_fair.zarr"
229-
gases = ["CO2_Fossil", "CH4", "N2O"]
230-
# This struction might appear weird, but matches that used for initial EPA runs.
231-
ds_in = xr.Dataset(
232-
{
233-
"gmsl": (
234-
[
235-
unnecesary_coord,
236-
"runtype",
237-
"pulse_year",
238-
"simulation",
239-
"runid",
240-
"year",
241-
"gas",
242-
],
243-
np.ones((1, 2, 1, 1, 2, 1, 3)),
244-
),
245-
},
246-
coords={
247-
"gas": (["gas"], gases),
248-
"pulse_year": (["pulse_year"], [2020]),
249-
"runid": (["runid"], [1, 2]),
250-
"runtype": (["runtype"], ["control", "pulse"]),
251-
"simulation": (["simulation"], [1]),
252-
"year": (["year"], [2020]),
253-
unnecesary_coord: (
254-
[unnecesary_coord],
255-
["foobar"],
256-
), # This one is unnecessary.
257-
},
258-
)
259-
ds_in.to_zarr(infile_path)
260-
261-
clim = Climate(
262-
gmst_path="",
263-
gmsl_path="",
264-
gmst_fair_path="bacon",
265-
gmsl_fair_path=str(infile_path),
266-
pulse_year=2020,
267-
damages_pulse_conversion_path="bacon",
268-
ecs_mask_path=None,
269-
emission_scenarios=None,
270-
gases=gases,
271-
)
272-
actual = clim.gmsl_anomalies
273-
274-
assert unnecesary_coord not in actual.coords
275-
276-
277218
def test_climate_gmst_anomalies(tmp_path):
278219
"""
279220
Test that Climate instances give "gmst_anomalies" from a GMST FAIR NetCDF file path
@@ -418,20 +359,22 @@ def test_climate_anomalies(tmp_path):
418359
gmsl_infile_path = d / "gmsl_fair.zarr"
419360
gases_gmsl = ["CO2_Fossil", "CH4", "N2O"]
420361
gmsl_infile_path = d / "gmsl_fair2.zarr"
421-
# As usual, this struction might appear weird, but matches that used for initial EPA runs.
362+
422363
gmsl_in = xr.Dataset(
423364
{
424-
"gmsl": (
425-
["runtype", "pulse_year", "simulation", "runid", "year", "gas"],
426-
np.ones((2, 1, 1, 2, 3, 3)),
365+
"pulse_gmsl": (
366+
["pulse_year", "runid", "year", "gas"],
367+
np.ones((1, 2, 3, 3)),
368+
),
369+
"control_gmsl": (
370+
["runid", "year"],
371+
np.ones((2, 3)),
427372
),
428373
},
429374
coords={
430375
"gas": (["gas"], gases_gmsl),
431376
"pulse_year": (["pulse_year"], [2002]),
432377
"runid": (["runid"], [1, 2]),
433-
"runtype": (["runtype"], ["control", "pulse"]),
434-
"simulation": (["simulation"], [1]),
435378
"year": (["year"], [2000, 2001, 2002]),
436379
},
437380
)
@@ -442,18 +385,20 @@ def test_climate_anomalies(tmp_path):
442385
"control_temperature": (["gas", "runid", "year"], np.zeros((3, 2, 1))),
443386
"pulse_temperature": (["gas", "runid", "year"], np.zeros((3, 2, 1))),
444387
"control_gmsl": (
445-
["simulation", "runid", "year", "gas"],
446-
np.ones((1, 2, 1, 3)),
388+
[
389+
"runid",
390+
"year",
391+
],
392+
np.ones((2, 1)),
447393
),
448394
"pulse_gmsl": (
449-
["simulation", "runid", "year", "gas"],
450-
np.ones((1, 2, 1, 3)),
395+
["runid", "year", "gas"],
396+
np.ones((2, 1, 3)),
451397
),
452398
},
453399
coords={
454400
"year": (["year"], [2002]),
455401
"gas": (["gas"], gases_gmst),
456-
"simulation": (["simulation"], [1]),
457402
"runid": (["runid"], [1, 2]),
458403
},
459404
).chunk({"year": 11})

0 commit comments

Comments
 (0)