88from glob import glob
99from numpy .typing import NDArray
1010from datetime import datetime , timedelta , date
11+ import psutil
1112
1213from WITOIL_iMagine .src .utils .utils import Utils
1314from WITOIL_iMagine .src .utils .config import Config
1617
1718logger = logging .getLogger (__name__ )
1819
20+ def print_memory_debug (tag , obj = None ):
21+ process = psutil .Process ()
22+ mem_used = process .memory_info ().rss / 1024 ** 2 # MB
23+ if obj is not None :
24+ try :
25+ size = obj .nbytes / 1024 ** 2 # MB
26+ except AttributeError :
27+ size = sys .getsizeof (obj ) / 1024 ** 2
28+ print (f"DEBUG [{ tag } ] - Used RAM: { mem_used :.2f} MB, Object size: { size :.2f} MB" )
29+ else :
30+ print (f"DEBUG [{ tag } ] - Used RAM: { mem_used :.2f} MB" )
31+
1932class PreProcessing :
2033 """
2134 This class embeds functions for pre-processing, with a user-interface purpose.
@@ -61,13 +74,15 @@ def process_currents(self, oce_path: str = None):
6174 if glob (oce_path ) == []:
6275 oce_path = f"{ self .exp_folder } /oce_files/*.nc"
6376 concat = xr .open_mfdataset (oce_path , combine = "nested" , engine = "netcdf4" )
77+ print_memory_debug ("process_currents - concat" , concat )
6478 concat = concat .drop_duplicates (dim = "time" , keep = "last" )
6579 if self .config ["input_files" ]["set_domain" ]:
6680 concat = concat .sel (
6781 lon = slice (lon_min , lon_max ), lat = slice (lat_min , lat_max )
6882 )
6983 # Interpolating the values in time, transforming it from daily to hourly values
7084 concat = concat .resample (time = "1h" ).interpolate ("linear" )
85+ print_memory_debug ("process_currents - after resample" , concat )
7186 Utils .write_mrc (concat , exp_folder = self .exp_folder )
7287 self ._copy_nc_files (oce_path , f"{ self .exp_folder } /oce_files/" )
7388 print ("DEBUG: process_currents() completed" )
@@ -82,9 +97,11 @@ def process_winds(self, met_path: str = None):
8297 if glob (met_path ) == []:
8398 met_path = f"{ self .exp_folder } /met_files/*.nc"
8499 concat = xr .open_mfdataset (met_path , combine = "nested" , engine = "netcdf4" )
100+ print_memory_debug ("process_winds - concat" , concat )
85101 concat = concat .drop_duplicates (dim = "time" , keep = "first" )
86102 # Interpolating the values in time, transforming it from daily to hourly values
87103 concat = concat .resample (time = "1h" ).interpolate ("linear" )
104+ print_memory_debug ("process_winds - after resample" , concat )
88105 # Handle longitude and latitude
89106 concat ["lon" ] = xr .where (
90107 concat ["lon" ] > 180 , concat ["lon" ] - 360 , concat ["lon" ]
@@ -125,6 +142,7 @@ def process_bathymetry(self,gebco):
125142
126143 # interpolation on medslik grid
127144 med = gebco .interp (lon = grid .lon .values .tolist (),lat = grid .lat .values .tolist ())
145+ print_memory_debug ("process_bathymetry - interpolated gebco" , med )
128146 print ("DEBUG: bathymetry memory usage (MB):" , med .nbytes / 1e6 )
129147 print ("DEBUG: GEBCO dimensions:" , gebco .sizes )
130148 print ("DEBUG: interpolated med dimensions:" , med .sizes )
@@ -146,6 +164,8 @@ def process_bathymetry(self,gebco):
146164 mdk_z = np .array (mdk_z )
147165 land_mask = np .where (mdk_z <= 0 )
148166 mdk_z [land_mask ]= 9999
167+ print_memory_debug ("process_bathymetry - mdk_z array" , mdk_z )
168+
149169
150170 BathFile = open (f'{ self .exp_folder } /bnc_files/dtm.bath' , "w" )
151171 BathFile .write ("MEDSLIK-II compatible bathymetry file. Degraded resolution based on GEBCO 30''\n " )
@@ -175,6 +195,7 @@ def process_coastline(self,gshhs):
175195 ymax = grid .lat .max () + buffer
176196
177197 shp = gpd .read_file (gshhs )
198+ print_memory_debug ("process_coastline - gshhs read" , shp )
178199
179200 # Cropping to a smaller area
180201 shp = shp .cx [xmin :xmax , ymin :ymax ]
@@ -268,6 +289,7 @@ def write_config_files(self,
268289 s_num = None ,
269290 ):
270291 print ("DEBUG: write_config_files() called" )
292+ print_memory_debug ("before write_config_files" )
271293 print (f" simname={ spill_dictionary ['simname' ]} , separate_slicks={ separate_slicks } , s_num={ s_num } " )
272294 # obtaining the variables
273295 simname = spill_dictionary ["simname" ]
0 commit comments