From 9df8d4b203ce1a7e4db236298d8b0b83325a66f3 Mon Sep 17 00:00:00 2001 From: Chris Rowe Date: Thu, 31 Oct 2024 18:10:20 -0400 Subject: [PATCH 1/2] Use context manager to open and close nc file --- city_metrix/layers/era_5_hottest_day.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/city_metrix/layers/era_5_hottest_day.py b/city_metrix/layers/era_5_hottest_day.py index 92c2982..3ec9f1c 100644 --- a/city_metrix/layers/era_5_hottest_day.py +++ b/city_metrix/layers/era_5_hottest_day.py @@ -96,14 +96,13 @@ def hourly_mean_temperature(image): }, f'download_{i}.nc') - dataarray = xr.open_dataset(f'download_{i}.nc') + 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) - # 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) - - dataarray_list.append(subset_dataarray) + dataarray_list.append(subset_dataarray) # Remove local file os.remove(f'download_{i}.nc') From ca22dde6d4a7d8a64f7e69098a1251d677f65b12 Mon Sep 17 00:00:00 2001 From: Chris Rowe Date: Thu, 31 Oct 2024 20:29:27 -0400 Subject: [PATCH 2/2] Load data from file before releasing from context --- city_metrix/layers/era_5_hottest_day.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/city_metrix/layers/era_5_hottest_day.py b/city_metrix/layers/era_5_hottest_day.py index 3ec9f1c..ed4ef04 100644 --- a/city_metrix/layers/era_5_hottest_day.py +++ b/city_metrix/layers/era_5_hottest_day.py @@ -100,9 +100,9 @@ def hourly_mean_temperature(image): # 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) + subset_dataarray = dataarray.isel(valid_time=indices).load() - dataarray_list.append(subset_dataarray) + dataarray_list.append(subset_dataarray) # Remove local file os.remove(f'download_{i}.nc')