Skip to content

Commit 40f4581

Browse files
committed
Wrap open_dataarray and open_mfdataset in with...
1 parent 138e76b commit 40f4581

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

message_ix_buildings/chilled/core/climate.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,31 +156,35 @@ def map_calculated_variables(args):
156156

157157
log.info("Reading: " + filepath)
158158
if config.rcp == "rcp26":
159-
dst = xr.open_mfdataset(
159+
with xr.open_mfdataset(
160160
filepath,
161161
chunks={"lon": get_paths(config, "chunk_size")},
162162
concat_dim="time",
163163
use_cftime=True,
164-
) # Setting for RCP2.6
164+
) as ras:
165+
dst = ras.load()
165166
else:
166-
dst = xr.open_mfdataset(
167+
with xr.open_mfdataset(
167168
filepath,
168169
chunks={"lon": get_paths(config, "chunk_size")},
169-
) # , concat_dim='time' ) # Setting for RCP6.0
170+
) as ras:
171+
dst = ras.load()
170172

171173
dst_crop = dst.sel(time=slice(years_clim[0], years_clim[1]))
172174
t_out_ave = dst_crop[config.davar].astype("float32") - 273.16
173175
t_out_ave = t_out_ave.transpose("lat", "lon", "time")
174176
t_oa_gbm = t_out_ave.groupby("time.month")
175177

176-
i_sol_v = xr.open_dataarray(
178+
with xr.open_dataarray(
177179
os.path.join(dle_path, "EWEMBI_vert_irrad_1980-2009_avg.nc")
178-
) # Values in daily Wh/m2
180+
) as ras:
181+
i_sol_v = ras.load() # Values in daily Wh/m2
179182

180183
# Horizontal irradiation
181-
i_sol_h = xr.open_dataarray(
184+
with xr.open_dataarray(
182185
os.path.join(dle_path, "EWEMBI_horiz_irrad_1980-2009_avg.nc")
183-
) # Values in daily Wh/m2
186+
) as ras:
187+
i_sol_h = ras.load() # Values in daily Wh/m2
184188

185189
if config.arch_setting == "regional":
186190
xr.open_dataset(
@@ -844,14 +848,15 @@ def aggregate_urban_rural_files(config: "Config"):
844848
for parset in par_var.itertuples():
845849
for var in varlist:
846850
log.info("." + var)
847-
mds = xr.open_mfdataset(
851+
with xr.open_mfdataset(
848852
os.path.join(
849853
output_path_vdd,
850854
suff + "_" + str(parset.Index) + "_" + var + "_*.nc",
851855
),
852856
concat_dim="urt",
853857
combine="nested",
854-
) # +urt[0]
858+
) as ras:
859+
mds = ras.load()
855860
# mds['arch'] = al #arcs#[:4]
856861
mds["urt"] = config.urts # arcs#[:4]
857862

0 commit comments

Comments
 (0)