Project

General

Profile

python-cdo can delete tmp files in jupyter notebook, but not in python scrip

Added by Jan Streffing 26 days ago

I'm faced with the following behaviour. I have a postproccessing script collection on levante: /work/ab0246/a270092/software/release_evaluation_tool2/scripts/

As part of that, I have a python script where I load data with cdo and dask:

# Add the parent directory to sys.path and load config
import sys
import os
from cdo import *
cdo = Cdo(cdo=os.path.join(sys.prefix, 'bin')+'/cdo')
from update_status import update_status
from collections import OrderedDict
from tqdm import tqdm
import dask
from dask.delayed import delayed
from dask.diagnostics import ProgressBar

#Name of model release
model_version  = 'AWI-CM-v3.3'

#Spinup
spinup_path    = '/work/ab0246/a270092/runtime/awicm3-v3.3/SPIN/outdata/'
spinup_name    = model_version+'_spinup'
spinup_start   = 1350
spinup_end     = 1849

#Preindustrial Control
pi_ctrl_path   = '/work/ab0246/a270092/runtime/awicm3-v3.3/PI/outdata/'
pi_ctrl_name   = model_version+'_pi-control'
pi_ctrl_start  = 1850
pi_ctrl_end    = 2014

#Historic
historic_path  = '/work/ab0246/a270092/runtime/awicm3-v3.3/HIST/outdata//outdata/'
historic_name  = model_version+'_historic'
historic_start = 1850
historic_end   = 2014

SCRIPT_NAME = os.path.basename(__file__)  # Get the current script name

print(SCRIPT_NAME)

# Mark as started
update_status(SCRIPT_NAME, " Started")

#Config
figsize=(7.2, 3.8)
var = ['ssr', 'str', 'tsr', 'ttr', 'sf', 'slhf', 'sshf']
exps = list(range(spinup_start, spinup_end+1))
ofile = "radiation_budget.png" 
#var must have order:
#1. Surface net solar radiation
#2. Surface net thermal radiation
#3. Top net solar radiation
#4. Top net thermal radiation

# Load model Data
def load_parallel(variable,path):
    data1 = cdo.yearmean(input='-fldmean '+str(path),returnArray=variable)/accumulation_period
    return data1

data = OrderedDict()

for v in var:

    datat = []
    t = []
    temporary = []
    for exp in tqdm(exps):

        path = spinup_path+'/oifs/atm_remapped_1m_'+v+'_1m_'+f'{exp:04d}-{exp:04d}.nc'
        temporary = dask.delayed(load_parallel)(v,path)
        t.append(temporary)

    with ProgressBar():
        datat = dask.compute(t)
    data[v] = np.squeeze(datat)

When I execute the same code I get the following error:

python part2_rad_balance.py
/work/ab0246/a270092/software/miniforge3/envs/reval/lib/python3.12/site-packages/pyfesom2/climatology.py:14: UserWarning: The seawater library is deprecated! Please use gsw instead.
  import seawater as sw
osgeo is not installed, conversion to Geo formats like Geotiff (fesom2GeoFormat) will not work.
/work/ab0246/a270092/input/fesom2/core2/pickle_mesh_py3_fesom2
The usepickle == True)
The pickle file for FESOM2 exists.
The mesh will be loaded from /work/ab0246/a270092/input/fesom2/core2/pickle_mesh_py3_fesom2
part2_rad_balance.py
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 500/500 [00:00<00:00, 40599.99it/s]
[                                        ] | 0% Completed | 7.26 sException ignored in: <function CdoTempfileStore.__del__ at 0x7fe3de1a1580>
Traceback (most recent call last):
  File "/work/ab0246/a270092/software/miniforge3/envs/reval/lib/python3.12/site-packages/cdo/cdo.py", line 829, in __del__
Exception ignored in: <function CdoTempfileStore.__del__ at 0x7fe3de1a1580>
Traceback (most recent call last):
  File "/work/ab0246/a270092/software/miniforge3/envs/reval/lib/python3.12/site-packages/cdo/cdo.py", line 829, in __del__
  File "/work/ab0246/a270092/software/miniforge3/envs/reval/lib/python3.12/site-packages/cdo/cdo.py", line 829, in __del__
  File "/work/ab0246/a270092/software/miniforge3/envs/reval/lib/python3.12/site-packages/cdo/cdo.py", line 829, in __del__
  File "/work/ab0246/a270092/software/miniforge3/envs/reval/lib/python3.12/site-packages/cdo/cdo.py", line 829, in __del__
Traceback (most recent call last):
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/cdoPylmduh0rv'
    os.remove(filename)

The same code copy & pasted into a jupyter notebook works fine. It seems python script and notebook behave differently when creating and destroying tmp files? How can I turn this as part of a python script?


Replies (1)

RE: python-cdo can delete tmp files in jupyter notebook, but not in python scrip - Added by Jan Streffing 24 days ago

In case anyone else runs into this, here is what solved the problem for me:

Instead of:

    with ProgressBar():
        datat = dask.compute(t)

which defaults to multiprocessing, use:

    with ProgressBar():
        datat = dask.compute(*t, scheduler='threads')
    (1-1/1)