Skip to content

Commit

Permalink
Merge pull request #88 from wri/bugfix/close_nc
Browse files Browse the repository at this point in the history
Use context manager to open and close nc file
  • Loading branch information
weiqi-tori authored Nov 3, 2024
2 parents b9b045a + ca22dde commit 84647e9
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions city_metrix/layers/era_5_hottest_day.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 84647e9

Please sign in to comment.