Skip to content

Commit 2a178c0

Browse files
authored
Fixing surface_trace_gases/diagnostic_surface_trace_gases.py w/ units and time loop (#4206)
1 parent 78e7b75 commit 2a178c0

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

esmvaltool/diag_scripts/surface_trace_gases/diagnostic_surface_trace_gases.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import matplotlib.pyplot as plt
2121
import numpy as np
2222
import scipy
23+
from cf_units import Unit
2324
from esmvalcore.preprocessor import climate_statistics
2425
from matplotlib import colors, gridspec
2526
from numpy import ma
@@ -1575,10 +1576,13 @@ def preprocess_obs_dataset(obs_dataset, config):
15751576

15761577
trace_gas = config["trace_gas"]
15771578

1578-
# Put observations and model data on same scale for ppm/ppb
1579-
obs_cube = TRACE_GASES_FACTOR[trace_gas] * obs_cube
1580-
# Change units accordingly
1581-
obs_cube.attributes["unit"] = TRACE_GASES_UNITS[trace_gas]
1579+
# If trace gas is N2O = units are [mol mol-1] according to the CMOR table.
1580+
# Applying the scale factor is necessary for ppb.
1581+
if trace_gas == "n2o":
1582+
obs_cube = TRACE_GASES_FACTOR[trace_gas] * obs_cube
1583+
1584+
# Change units to ppm/ppb
1585+
obs_cube.units = Unit(TRACE_GASES_UNITS[trace_gas])
15821586

15831587
# Add the clim_season and season_year coordinates.
15841588
iris.coord_categorisation.add_year(obs_cube, "time", name="year")
@@ -1940,21 +1944,19 @@ def preprocess_colocated_datasets(
19401944
}
19411945

19421946
# Looping over all time steps
1943-
for i, ts in enumerate(time.units.num2date(time.points)):
1947+
for i, _ts in enumerate(time.units.num2date(time.points)):
19441948
if "timeserie_lat" in plots_coloc:
19451949
# Looping over latitude slices
19461950
for key, _ in LATITUDE_RANGES.items():
19471951
# Obs values
1948-
obs_ts = obs_slices[key]["values"].extract(
1949-
iris.Constraint(time=ts)
1950-
)
1952+
obs_ts = obs_slices[key]["values"][i]
19511953
obs_yearly = obs_slices[key]["yearly"].extract(
19521954
iris.Constraint(year=years_ts[i])
19531955
)
19541956
# Model values
19551957
mod_ts = model_slices[key]["values"][
19561958
model_dataset
1957-
][attr["alias"]].extract(iris.Constraint(time=ts))
1959+
][attr["alias"]][i]
19581960
mod_yearly = model_slices[key]["yearly"][
19591961
model_dataset
19601962
][attr["alias"]].extract(

0 commit comments

Comments
 (0)