diff --git a/city_metrix/layers/era_5_hottest_day.py b/city_metrix/layers/era_5_hottest_day.py index 92c2982..ed4ef04 100644 --- a/city_metrix/layers/era_5_hottest_day.py +++ b/city_metrix/layers/era_5_hottest_day.py @@ -96,12 +96,11 @@ def hourly_mean_temperature(image): }, f'download_{i}.nc') - dataarray = xr.open_dataset(f'download_{i}.nc') - - # Subset times for the day - times = [valid_time.astype('datetime64[s]').astype(datetime).replace(tzinfo=pytz.UTC) for valid_time in dataarray['valid_time'].values] - indices = [i for i, value in enumerate(times) if value in utc_times] - subset_dataarray = dataarray.isel(valid_time=indices) + with xr.open_dataset(f'download_{i}.nc') as dataarray: + # Subset times for the day + times = [valid_time.astype('datetime64[s]').astype(datetime).replace(tzinfo=pytz.UTC) for valid_time in dataarray['valid_time'].values] + indices = [i for i, value in enumerate(times) if value in utc_times] + subset_dataarray = dataarray.isel(valid_time=indices).load() dataarray_list.append(subset_dataarray)