print( 'beginning imports')

from glob import glob
import pdb
import os
import shutil
from run_diags import run_diags

print('end imports')

### Set this part ##########################
source_root = '/global/cfs/cdirs/e3sm/emulate/postproc_e3sm/slice'  # Loop over all the cases in here. 
refpath = '/global/cfs/cdirs/e3sm/acme_diags/obs_for_e3sm_diags/climatology/' # NERSC
destroot = '/global/cfs/cdirs/e3sm/www/wagmanbe/slice/'
#############################################  

# Loop over the climatology of the cases in source root, computing diags and storing them in a subdirectory. 
for d in os.listdir( source_root):
    if os.path.isdir( os.path.join( source_root, d )):
        # create the destination dirs if they dont exist.
        testpath = os.path.join(source_root, d)
        destpath = os.path.join(destroot, d)                           # put the diags here so you can view on web
        destpath_symbolic = os.path.join(source_root, d, 'e3sm_diags') # create a symbolic link to the diags with the rest of the output. 
        if not os.path.exists(destpath):
            os.makedirs(destpath)
        if not os.path.exists(destpath_symbolic): 
            os.symlink(destpath, destpath_symbolic)
            
        # Check if diags have been run. If they have finished you will find destroot/zonal_mean_xy/index.html -- not sure thats the last one but it will do. If testing it will only produce lat-lon so you won't find it. 
        if not os.path.exists(os.path.join( destpath, 'zonal_mean_xy', 'index.html' )):
            # Go get the casename. Now it's not always 'case_scripts' as it was when I had the awkward create_clone problem.
            casename = os.path.basename( destpath ) 
            print( 'calling diags on {}'.format( testpath))
            run_diags( refpath, testpath, casename, destpath, testing=False)

