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

### Set this part ##########################
source_root = '/global/cfs/cdirs/e3sm/emulate/postproc_e3sm/slice'  # Loop over all the cases in here. 
casename = 'case_scripts' # This is an artifact of the create_clone script but it's okay. 
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)
            
        print( 'calling diags on {}'.format( testpath))
        run_diags( refpath, testpath, casename, destpath)

