From 010724dc25c0cf2212e3122fdf960bff127a850d Mon Sep 17 00:00:00 2001 From: weiqi-tori Date: Sun, 3 Nov 2024 17:39:29 +0800 Subject: [PATCH] Use context manager to open and close nc file --- city_metrix/layers/cams.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/city_metrix/layers/cams.py b/city_metrix/layers/cams.py index 34c8892..729d37b 100644 --- a/city_metrix/layers/cams.py +++ b/city_metrix/layers/cams.py @@ -48,8 +48,8 @@ def get_data(self, bbox): # load netcdf files dataarray_list = [] for nc_file in os.listdir(fname): - dataarray = xr.open_dataset(f'{fname}/{nc_file}') - dataarray_list.append(dataarray) + with xr.open_dataset(f'{fname}/{nc_file}') as dataarray: + dataarray_list.append(dataarray) # not all variables have 'model_level', concatenate without 'model_level' dimension dataarray_list = [