<repo_name>jeffbaars/rcm_python<file_sep>/plot_wrfout_grid.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re, math
import numpy as np
#import matplotlib.pyplot as plt
from make_cmap import *
from netCDF4 import Dataset as NetCDFFile
from utils_wrfout import *
from utils_date import *
from utils_cmap import *
import pickle
from collections import defaultdict
from make_cmap import *
import matplotlib as mpl
mpl.use('Agg')

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
plot_dir   = rcm_dir + '/plots'
pickle_dir = rcm_dir + '/pickle'
run_dirs   = rcm_dir + '/rundirs'
geo_em     = rcm_dir + '/data' + '/geo_em.d02.nc'
data_dirs  = ['/home/disk/r2d2/steed/cmip5/rcp8.5', \
              '/home/disk/vader/steed/cmip5/rcp8.5', \
              '/home/disk/jabba/steed/cmip5/rcp8.5']

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
#vars = ['PREC', 'T2MAX', 'T2MIN', 'SPDUV10MAX', 'SPDUV10MEAN', 'SPDUV10STD']
vars = ['PREC', 'T2MAX', 'T2MIN']

sdt = '197001'
edt = '209912'

models = ['access1.0', 'access1.3', 'bcc-csm1.1', 'canesm2', \
          'ccsm4', 'csiro-mk3.6.0', 'fgoals-g2', 'gfdl-cm3', \
          'giss-e2-h', 'miroc5', 'mri-cgcm3', 'noresm1-m']

#---------------------------------------------------------------------------
# Make run directory, load geo_em file.
#---------------------------------------------------------------------------
rundir = run_dirs + '/plot_wrfout_grid.' + str(os.getpid())
print 'making ', rundir
os.makedirs(rundir)

print 'Loading ', geo_em
(lat, lon, hgt) = load_geo_em(geo_em)

#---------------------------------------------------------------------------
# For each model, make a maps of every season...
#---------------------------------------------------------------------------
for m in range(len(models)):
    model = models[m]

    #--- Get list of extracted files for this model.
    files = get_extract_files(data_dirs, model, sdt, edt, 0)

    #--- Get files broken out by year and season.
    (spr_files, sum_files, fal_files, win_files, ann_files, years) = \
                get_seasonal_files(files)

    #--- Loop over year, making maps of each 
    for y in range(len(years)):
        yyyy = years[y]

        #--- Winter plots.
        if (win_files[yyyy] == win_files[yyyy]):
            iret = run_mapper('T2MAX', model, rundir, 'winter', win_files,\
                              yyyy, lat, lon, levs_temp, cmap_temp)
            iret = run_mapper('T2MIN', model, rundir, 'winter', win_files,\
                              yyyy, lat, lon, levs_temp, cmap_temp)
            iret = run_mapper('PREC', model, rundir, 'winter', win_files,\
                              yyyy, lat, lon, levs_pcp, cmap_pcp)

        #--- Summer plots.
        if (sum_files[yyyy] == sum_files[yyyy]):
            iret = run_mapper('T2MAX', model, rundir, 'summer', sum_files,\
                              yyyy, lat, lon, levs_temp, cmap_temp)
            iret = run_mapper('T2MIN', model, rundir, 'summer', sum_files,\
                              yyyy, lat, lon, levs_temp, cmap_temp)
            iret = run_mapper('PREC', model, rundir, 'summer', sum_files,\
                              yyyy, lat, lon, levs_pcp, cmap_pcp)

        #--- Spring plots.
        if (spr_files[yyyy] == spr_files[yyyy]):
            iret = run_mapper('T2MAX', model, rundir, 'spring', spr_files,\
                              yyyy, lat, lon, levs_temp, cmap_temp)
            iret = run_mapper('T2MIN', model, rundir, 'spring', spr_files,\
                              yyyy, lat, lon, levs_temp, cmap_temp)
            iret = run_mapper('PREC', model, rundir, 'spring', spr_files,\
                              yyyy, lat, lon, levs_pcp, cmap_pcp)

        #--- Fall plots.
        if (fal_files[yyyy] == fal_files[yyyy]):
            iret = run_mapper('T2MAX', model, rundir, 'fall', fal_files,\
                              yyyy, lat, lon, levs_temp, cmap_temp)
            iret = run_mapper('T2MIN', model, rundir, 'fall', fal_files,\
                              yyyy, lat, lon, levs_temp, cmap_temp)
            iret = run_mapper('PREC', model, rundir, 'fall', fal_files,\
                              yyyy, lat, lon, levs_pcp, cmap_pcp)

        #--- Annual plots.
        if (ann_files[yyyy] == ann_files[yyyy]):
            iret = run_mapper('T2MAX', model, rundir, 'annual', ann_files,\
                              yyyy, lat, lon, levs_temp, cmap_temp)
            iret = run_mapper('T2MIN', model, rundir, 'annual', ann_files,\
                              yyyy, lat, lon, levs_temp, cmap_temp)
            iret = run_mapper('PREC', model, rundir, 'annual', ann_files,\
                              yyyy, lat, lon, levs_pcp, cmap_pcp)


sys.exit()

<file_sep>/utils_snotel_obs.py
#!/usr/bin/python
import os, sys, glob, re
import numpy as np
import csv

in2mm = 25.4

#---------------------------------------------------------------------------
# Read in a swe stations file.
#---------------------------------------------------------------------------
def read_swe_stations_file(station_file):
    stns      = []
    latpts    = []
    lonpts    = []
    elevs     = []
    stn_names = []
    with open(station_file) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        line_count = 0
        for row in csv_reader:
            stns.append(row[0])
            latpts.append(float(row[1]))
            lonpts.append(float(row[2]))
            elevs.append(float(row[3]))
            s = row[4]
            s = s.strip(' "\'\t\r\n')
            stn_names.append(s)

    return stns, latpts, lonpts, elevs, stn_names

#---------------------------------------------------------------------------
# Read in a snotel data file. Columns are Water Year,Day,Oct,Nov,Dec,Jan,
# Feb,Mar,Apr,May,Jun,Jul,Aug,Sep.
#---------------------------------------------------------------------------
def read_snotel(swe_file):
    yyyy    = []
    swe_obs = []    
    with open(swe_file) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        line_count = 0
        for row in csv_reader:

            #--- Skip comments.
            if row[0].startswith('#'):
                continue

            #--- Skip non-1st-of-the-month data.
            if row[1] != '01':
                continue

            #--- Skip if there's no actual value for this year.
            if not row[8]:
                continue

            #--- Grab April (1st).
            yyyy.append(row[0])
            swe_obs.append(float(row[8]) * in2mm)
    
    return yyyy, swe_obs

<file_sep>/old/utils_wrfout_20190523.py
#!/usr/bin/python
import os, sys, glob, re, math
import numpy as np
from netCDF4 import Dataset as NetCDFFile
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, cm, shiftgrid
from collections import defaultdict
from utils_cmap import *
import pickle
from utils_date import *
from utils_ghcnd_obs import *

dtfmt = "%Y%m%d%H"

#seasons    = ['winter']
#seasons    = ['annual', 'spring', 'summer', 'fall', 'winter']
seasons    = ['spring', 'summer', 'fall', 'winter', 'annual']
seasons_mo = ['ALL', 'MAM', 'JJA', 'SON', 'DJF']
seasons_lab = {
    'annual': 'Jan-Dec',    
    'spring': 'Mar-Apr-May',
    'summer': 'Jun-Jul-Aug',
    'fall':   'Sep-Oct-Nov',
    'winter': 'Dec-Jan-Feb'
    }
var_lab = {
    'T2MAX': 'Average Max Temperature ($^\circ$C)',
    'T2MIN': 'Average Min Temperature ($^\circ$C)',
    'PREC': 'Total Precipitation (in.)',
    'SPDUV10MEAN': 'Average Wind Speed (m/s)',
    'SPDUV10MAX': 'Maximum Wind Speed (m/s)',
    'SNOW': 'Snow Water Equivalent (mm)',
    'SWDOWN': 'Shortwave Down Radiation (W/m^2)'    
    }
ylims = {
    'PRECmin':  [0, 1.0],
    'PRECmax':  [0, 5.0],    
    'PRECavg':  [0, 45.0],
    'PRECtot':  [0, 40.0],        
    'T2MAXavg': [-5, 40.0],
    'T2MAXmax': [0, 50.0],    
    'T2MINmin': [-30.0, 25.0],
    'T2MINmax': [-30.0, 25.0],
    'T2MINavg': [-20, 25.0],
    'SPDUV10MEANavg': [0, 5.0],
    'SPDUV10MAXmax': [0, 20.0]    
    }
labels = {
    'PRECtot':  'Total Precipitation (in.)',
    'PRECmax':  'Maximum Precipitation (in.)',
    'PRECmin':  'Minimum Precipitation (in.)',
    'PRECavg':  'Average Precipitation (in.)',        
    'T2MAXavg': 'Average Maximum Temperature ($^\circ$C)',
    'T2MAXmax': 'Max Maximum Temperature ($^\circ$C)',
    'T2MAXmin': 'Min Maximum Temperature ($^\circ$C)',    
    'T2MINavg': 'Average Minimum Temperature ($^\circ$C)',
    'T2MINmax': 'Max Minimum Temperature ($^\circ$C)',
    'T2MINmin': 'Min Minimum Temperature ($^\circ$C)',
    'SPDUV10MEANavg': 'Average Wind Speed (m/s)',
    'SPDUV10MAXmax': 'Maximum Wind Speed (m/s)'    
    }

colorbar_labs = {
    'T2MAX': 'Temperature ($^\circ$C)',
    'T2MIN': 'Temperature ($^\circ$C)',
    'PREC': 'Precipitation (in.)',
    'SPDUV10MEAN': 'Wind Speed (m/s)',
    'SPDUV10MAX': 'Wind Speed (m/s)',
    'SNOW': 'Snow Water Equivalent (mm)',
    'SWDOWN': 'Shortwave Down Radiation (W/m^2)'     
    }

zooms = ['Z1', 'Z2']
target_lat = 47.44472
target_lon = -122.31361
fudge_lat = 1.7
fudge_lon = 2.1
minlatZ2 = target_lat - fudge_lat
maxlatZ2 = target_lat + fudge_lat
minlonZ2 = target_lon - fudge_lon
maxlonZ2 = target_lon + fudge_lon
minlat = {
    'Z1': 39.0,
    'Z2': minlatZ2 }
maxlat = {
    'Z1': 51.0,
    'Z2': maxlatZ2 }
minlon = {
    'Z1': -131.0,
    'Z2': minlonZ2 }
maxlon = {
    'Z1': -108.0,
    'Z2': maxlonZ2 }

mm2in           = 0.0393700787
min_season_days = 85
std_lapse       = 0.0065  #-- std. atmos. lapse rate
smooth_fact     = 5       #-- odd number, used for smoothing time series plots.

fs      = 9
titlefs = 9
width   = 10
height  = 8
maplw   = 1.0
mvc     = -9999.0

#---------------------------------------------------------------------------
# Load geo_em file.
#---------------------------------------------------------------------------
def load_geo_em(geo_em):
    nc = NetCDFFile(geo_em, 'r')
    lat = nc.variables['XLAT_M'][0,:,:]
    lon = nc.variables['XLONG_M'][0,:,:]
    hgt = nc.variables['HGT_M'][0,:,:]    
    lat = np.transpose(lat)
    lon = np.transpose(lon)
    hgt = np.transpose(hgt)
    nc.close()
    return lat, lon, hgt

#---------------------------------------------------------------------------
# Get netcdf dimension
#---------------------------------------------------------------------------
def get_nc_dim(ncin, dimname):
    nc = NetCDFFile(ncin, 'r')
    dim = len(nc.dimensions[dimname])
    nc.close()
    return dim

#---------------------------------------------------------------------------
# Load wrfout file.
#---------------------------------------------------------------------------
def load_wrfout(wrfout, vars):
    nc = NetCDFFile(wrfout, 'r')
    ntimes = len(nc.dimensions['Time'])

    dataout = {}
    for v in range(len(vars)):
        var = vars[v]
        dat_tmp = nc.variables[var][:,:,:]
        dat_tmp = np.transpose(dat_tmp)
        dataout[var] = dat_tmp

    #--- Get date from global attributes and clean it up and trim it down.
    dt_all = nc.START_DATE
    dt = dt_all.replace('-', '')
    dt = dt.replace(':', '')
    dt = dt.replace('_', '')        
    dt = dt[0:10]
    nc.close()
    return dataout, ntimes, dt

#---------------------------------------------------------------------------
# Load SWE wrfout file.
#---------------------------------------------------------------------------
def load_wrfout_swe(wrfout, vars):
    nc = NetCDFFile(wrfout, 'r')
    ntimes = len(nc.dimensions['Time'])

    dataout = {}
    for v in range(len(vars)):
        var = vars[v]
        dat_tmp = nc.variables[var][:,:,:]
        dat_tmp = np.transpose(dat_tmp)
        dataout[var] = dat_tmp
    nc.close()
    return dataout, ntimes

#---------------------------------------------------------------------------
# Load wrfout file.
#---------------------------------------------------------------------------
def sum_wrfout_slice(files, vars, maski, maskj):    
    sums = {}
    cnts = {}

    for f in range(len(files)):
        print 'reading ', files[f]
        nc = NetCDFFile(files[f], 'r')
        ntimes = len(nc.dimensions['Time'])

        for v in range(len(vars)):
            var = vars[v]
            dat_tmp = nc.variables[var][:,:,:]
            dat_tmp = np.transpose(dat_tmp)

            sum_c = 0
            cnt_c = 0
            for n in range(len(maski)):
                sum_c = sum_c + sum(dat_tmp[maski[n], maskj[n],:])
                cnt_c = cnt_c + len(dat_tmp[maski[n], maskj[n],:])

            if (var in sums):
                sums[var] = sums[var] + sum_c
                cnts[var] = cnts[var] + cnt_c
            else:
                sums[var] = sum_c
                cnts[var] = cnt_c

    return sums, cnts

#---------------------------------------------------------------------------
# For ensemble standard deviation plots....
#---------------------------------------------------------------------------
def get_ensmean_std(var, models, years, season, \
                    sum_files, spr_files, win_files, fal_files, ann_files, \
                    pickle_file):
    nyears = 0
    nmodels = len(models)
    files = []
    dts_all = []
    for y in range(len(years)):
        yyyy = years[y]

        for m in range(nmodels):
            model = models[m]
            sum_files_c = sum_files[model]
            spr_files_c = spr_files[model]
            win_files_c = win_files[model]
            fal_files_c = fal_files[model]
            ann_files_c = ann_files[model]

            if season == 'summer':
                if (sum_files_c[yyyy] == sum_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(sum_files_c[yyyy])):
                        files.append(sum_files_c[yyyy][f])
                    else:
                        continue
            elif season == 'spring':
                if (spr_files_c[yyyy] == spr_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(spr_files_c[yyyy])):
                        files.append(spr_files_c[yyyy][f])
                    else:
                        continue
            elif season == 'winter':
                if (win_files_c[yyyy] == win_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(win_files_c[yyyy])):
                        files.append(win_files_c[yyyy][f])
                    else:
                        continue
            elif season == 'fall':                        
                if (fal_files_c[yyyy] == fal_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(fal_files_c[yyyy])):
                        files.append(fal_files_c[yyyy][f])
                    else:
                        continue
            elif season == 'annual':
                if (ann_files_c[yyyy] == ann_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(ann_files_c[yyyy])):
                        files.append(ann_files_c[yyyy][f])
                    else:
                        continue

    print 'files = ', files
    
    #--- Get sorted, unique set of dates from files found above.
    nfiles = len(files)
    dts_all = []
    for f in range(nfiles):
        dt_c = re.findall(r'\.(\d{10})\.', files[f])[0]        
        dts_all.append(dt_c)
    dts_all = list(sorted(set(dts_all)))

    #--- For each unique date, read in files for each model.
    for d in range(len(dts_all)):

        #--- First make sure all members have a file.
        files_c = []
        print dts_all[d]
        for f in range(nfiles):
            dt_c = re.findall(r'\.(\d{10})\.', files[f])[0]
            if dt_c == dts_all[d]:
                files_c.append(files[f])
        if len(files_c) != nmodels:
            print 'missing member(s) for date = ', dts_all[d]
            sys.exit()

        #--- Now read in files for each member.
        for n in range(len(files_c)):
            print 'loading ', files_c[n]
            (vardat, ntimes, dt) = load_wrfout(files_c[n], [var])
            plotdat = vardat[var][:,:,:]
            nx,ny,nt = plotdat.shape
            if n == 0:
                dat_all = np.ones((nx, ny, nt, nmodels)) * np.nan
                stdevs = np.ones((nx, ny, nt)) * np.nan
            dat_all[:,:,:,n] = plotdat

        #--- For each ntimes (these should be monthly files, so ntimes are
        #--- days and will be ~30 days), calculate a standard deviation.
        for nt in range(ntimes):
            dat_c = np.squeeze(dat_all[:,:,nt,:])
            stdevs[:,:,nt] = np.std(dat_c, axis=2)

        print stdevs
        print stdevs.shape
        sys.exit()

#        #--- Calculate the mean of the standard deviations.
#        if d == 0:
#            stdevmean = np.ones((nx, ny, len(dts_all))) * np.nan
#        stdevmean[:,:,d] = np.mean(stdevs, axis=2)
        
        #--- Create pickle file.
        pickle.dump((varmean), open(pickle_file,'wb'), -1)

    return varmean, nyears

#---------------------------------------------------------------------------
# For ensemble mean plots, get system nco command.
#---------------------------------------------------------------------------
def get_ensmean_syscom(file_plot, var, models, years, season, \
                       sum_files, spr_files, \
                       win_files, fal_files, ann_files, ncocom):
    syscom = ncocom
    nyears = 0
    for m in range(len(models)):
        model = models[m]
        sum_files_c = sum_files[model]
        spr_files_c = spr_files[model]
        win_files_c = win_files[model]
        fal_files_c = fal_files[model]
        ann_files_c = ann_files[model]
        for y in range(len(years)):
            yyyy = years[y]
            if season == 'summer':
                if (sum_files_c[yyyy] == sum_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(sum_files_c[yyyy])):
                        syscom = syscom + ' ' + sum_files_c[yyyy][f]
                    else:
                        continue
            elif season == 'spring':
                if (spr_files_c[yyyy] == spr_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(spr_files_c[yyyy])):
                        syscom = syscom + ' ' + spr_files_c[yyyy][f]
                    else:
                        continue
            elif season == 'winter':
                if (win_files_c[yyyy] == win_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(win_files_c[yyyy])):
                        syscom = syscom + ' ' + win_files_c[yyyy][f]
                    else:
                        continue
            elif season == 'fall':                        
                if (fal_files_c[yyyy] == fal_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(fal_files_c[yyyy])):
                        syscom = syscom + ' ' + fal_files_c[yyyy][f]
                    else:
                        continue
            elif season == 'annual':
                if (ann_files_c[yyyy] == ann_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(ann_files_c[yyyy])):
                        syscom = syscom + ' ' + ann_files_c[yyyy][f]
                    else:
                        continue

    return syscom, nyears

#---------------------------------------------------------------------------
# For ensemble mean plots, run system nco command.
#---------------------------------------------------------------------------
def run_ensmean_syscom(file_plot, var, ncocom, syscom, yyyy_s, yyyy_e, \
                       season, rundir, ):

    #--- If system command is too big (too many files to send
    #--- to nco), break it up into 1000 piece chunks.
    files_c = syscom.replace(ncocom, '')
    scsplit = files_c.split()
    nfiles = len(scsplit)

    niter = 1000
    if nfiles > niter:
        nt =  int(math.ceil(float(nfiles) / float(niter)))
        files_all = []
        for n in range(nt):
            file_c = rundir + '/' + var + '_' + season + '_' + \
                     yyyy_s + '_' + yyyy_e + '_subset' + str(n) + '.nc'
            if os.path.isfile(file_c):
                print 'this file already exists: ', file_c
                files_all.append(file_c)
                continue
            nb = n * niter
            ne = (n+1) * niter
            syscom = ncocom + ' ' + ' '.join(scsplit[nb:ne]) + ' ' + file_c
            print syscom
            iret = os.system(syscom)
            if iret != 0:
                print 'problem running nco! iret = ', iret
                return mvc
            files_all.append(file_c)
        syscom = ncocom + ' ' + ' '.join(files_all) + ' ' + file_plot
        print syscom                
        iret = os.system(syscom)
        if iret != 0:
            print 'problem running nco! iret = ', iret
            return mvc
    else:
        syscom = syscom + ' ' + file_plot
        print 'syscom = ', syscom
        iret = os.system(syscom)
        if iret != 0:
            print 'problem running nco! iret = ', iret
            return mvc

    return 1

#---------------------------------------------------------------------------
# Get plot file name.
#---------------------------------------------------------------------------
def get_plotfname(plot_dir, stn, sdt, edt, season, var, stat):
    plotfname = plot_dir + '/' + stn + '_' + sdt + '_' + edt + '_' + \
                season + '_' + var + '_' + stat + '.png'
    return plotfname

#---------------------------------------------------------------------------
# Get plot title.
#---------------------------------------------------------------------------
def get_title(season, stn, var, stat, years):
    titleout = station_name_dict[stn] + ' (' + stn.upper() + '), ' + \
               seasons_lab[season] + ' ' + labels[var+stat] + ', ' + \
               str(years[0]) + ' - ' + str(years[len(years)-1])
    return titleout

#---------------------------------------------------------------------------
# Get list of extracted wrfout files between sdt_yyyymm and edt_yyyymm.
#---------------------------------------------------------------------------
def get_extract_files(data_dirs, model, sdt_yyyymm, edt_yyyymm, snowrad):
    files_out = []
    for d in range(len(data_dirs)):
        path_c = data_dirs[d] + '/' + model + '/extract/*.nc'
        files = glob.glob(path_c)
        
        for f in range(len(files)):

            if snowrad == 0:
                if 'snowrad' in files[f]:
                    continue
            else:
                if 'snowrad' not in files[f]:
                    continue

            dt_c = re.findall(r'\.(\d{10})\.', files[f])
            if (dt_c):
                dt_c = dt_c[0]
                yyyymm_c = dt_c[0:6]
                if (yyyymm_c >= sdt_yyyymm and yyyymm_c <= edt_yyyymm):
                    files_out.append(files[f])
    if (len(files_out) == 0):
        sys.exit('get_extract_files:  no files found!')
    return list(sorted(set(files_out)))

#---------------------------------------------------------------------------
# Get seasonal stats.
#---------------------------------------------------------------------------
def get_seasonal_files(files):

    springs = defaultdict(list)
    summers = defaultdict(list)
    falls   = defaultdict(list)
    winters = defaultdict(list)
    annuals = defaultdict(list)
    spring_ndays = {}
    summer_ndays = {}
    fall_ndays   = {}
    winter_ndays = {}
    annual_ndays = {}    
    years_all = []

    #--- Loop over all files, binning them into seasons / years.
    for f in range(len(files)):
        dt_c = re.findall(r'\.(\d{10})\.', files[f])[0]
        if (dt_c):
            yyyy = dt_c[0:4]
            mm   = dt_c[4:6]
            years_all.append(yyyy)
            if (mm == '03' or mm == '04' or mm == '05'):
                if ((yyyy) in spring_ndays):
                    spring_ndays[yyyy] += 1
                else:
                    spring_ndays[yyyy] = 1
                springs[yyyy].append(files[f])
            elif (mm == '06' or mm == '07' or mm == '08'):
                if ((yyyy) in summer_ndays):
                    summer_ndays[yyyy] += 1
                else:
                    summer_ndays[yyyy] = 1
                summers[yyyy].append(files[f])
            elif (mm == '09' or mm == '10' or mm == '11'):
                if ((yyyy) in fall_ndays):
                    fall_ndays[yyyy] += 1
                else:
                    fall_ndays[yyyy] = 1
                falls[yyyy].append(files[f])
            elif (mm == '12'):
                yyyyw = str(int(yyyy) + 1)
                if ((yyyyw) in winter_ndays):
                    winter_ndays[yyyyw] += 1
                else:
                    winter_ndays[yyyyw] = 1
                winters[yyyyw].append(files[f])
            elif (mm == '01' or mm == '02'):
                if ((yyyy) in winter_ndays):
                    winter_ndays[yyyy] += 1
                else:
                    winter_ndays[yyyy] = 1
                winters[yyyy].append(files[f])

            if ((yyyy) in annual_ndays):
                annual_ndays[yyyy] += 1
            else:
                annual_ndays[yyyy] = 1
            annuals[yyyy].append(files[f])

    #--- Get list of unique, sorted years found above.
    years = list(sorted(set(years_all)))

    #--- Check number of data points per unique years and nan out
    #--- ones that do not have enough (< min_season_days).
    for y in range(len(years)):
        yyyy = years[y]
        if (yyyy in spring_ndays):
            if (spring_ndays[yyyy] < 3):
                springs[yyyy] = np.nan
        else:
            springs[yyyy] = np.nan
        if (yyyy in summer_ndays):
            if (summer_ndays[yyyy] < 3):
                summers[yyyy] = np.nan
        else:
            summers[yyyy] = np.nan
        if (yyyy in fall_ndays):            
            if (fall_ndays[yyyy] < 3):
                falls[yyyy] = np.nan
        else:
            falls[yyyy] = np.nan
        if (yyyy in winter_ndays):            
            if (winter_ndays[yyyy] < 3):
                winters[yyyy] = np.nan
        else:
            winters[yyyy] = np.nan
        if (yyyy in annual_ndays):            
            if (annual_ndays[yyyy] < 3):
                annuals[yyyy] = np.nan
        else:
            annuals[yyyy] = np.nan

    return springs, summers, falls, winters, annuals, years

#---------------------------------------------------------------------------
# Get seasonal files (new)
#---------------------------------------------------------------------------
def get_seasonal_files_new(files):

    seafiles = defaultdict(list)
    ndays = {}

#    springs = defaultdict(list)
#    summers = defaultdict(list)
#    falls   = defaultdict(list)
#    winters = defaultdict(list)
#    spring_ndays = {}
#    summer_ndays = {}
#    fall_ndays   = {}
#    winter_ndays = {}

    years_all = []

    #--- Loop over all files, binning them into seasons / years.
    for f in range(len(files)):
        dt_c = re.findall(r'\.(\d{10})\.', files[f])[0]
        if (dt_c):
            yyyy = dt_c[0:4]
            mm   = dt_c[4:6]

            if mm == '12':
                yyyy = str(int(yyyy) + 1)
            else:
                yyyy = yyyy
            years_all.append(yyyy)

            if (mm == '03' or mm == '04' or mm == '05'):
                season = 'spring'
            if (mm == '06' or mm == '07' or mm == '08'):
                season = 'summer'
            if (mm == '09' or mm == '10' or mm == '11'):
                season = 'fall'
            if (mm == '12' or mm == '01' or mm == '02'):
                season = 'winter'

            if ((yyyy) in ndays):
                ndays[season+yyyy] += 1
            else:
                ndays[season+yyyy] = 1
            seafiles[season,yyyy].append(files[f])

    #--- Get list of unique, sorted years found above.
    years = list(sorted(set(years_all)))

    return seafiles, years

'''
    #--- Check number of data points per unique years and nan out
    #--- ones that do not have enough (< min_season_days).
    for y in range(len(years)):
        yyyy = years[y]
        if (yyyy in spring_ndays):
            if (spring_ndays[yyyy] < 3):
                springs[yyyy] = np.nan
        else:
            springs[yyyy] = np.nan
        if (yyyy in summer_ndays):
            if (summer_ndays[yyyy] < 3):
                summers[yyyy] = np.nan
        else:
            summers[yyyy] = np.nan
        if (yyyy in fall_ndays):            
            if (fall_ndays[yyyy] < 3):
                falls[yyyy] = np.nan
        else:
            falls[yyyy] = np.nan
        if (yyyy in winter_ndays):            
            if (winter_ndays[yyyy] < 3):
                winters[yyyy] = np.nan
        else:
            winters[yyyy] = np.nan
'''


#---------------------------------------------------------------------------
# Get seasonal stats (totals or averages currently).
#---------------------------------------------------------------------------
def get_seasonal_stats(data_all, dts_all, models, stns, var, stat):

    out_sum = {}
    out_avg = {}
    out_max = {}
    out_min = {}
        
    years_all = []
    stn_pr = 'KSEA'
    
    for m in range(len(models)):
        mod = models[m]
        data_all_c = data_all[mod]
        for s in range(len(stns)):
            stn = stns[s]
            ndays = {}
            years_mod_stn = []
            for d in range(len(dts_all)):
                yyyy = dts_all[d][0:4]
                mm = dts_all[d][4:6]
                years_mod_stn.append(yyyy)

                if ((var,stn,dts_all[d]) in data_all_c):
                    dat_c = data_all_c[var,stn,dts_all[d]]
                else:
                    ndays[season+yyyy] = np.nan
                    out_sum[season,mod,stn,yyyy] = np.nan
                    out_max[season,mod,stn,yyyy] = np.nan
                    out_min[season,mod,stn,yyyy] = np.nan
                    continue
                
                #--- Spring.
                if (mm == '03' or mm == '04' or mm == '05'):
                    season = 'spring'
                    #--- Count number of days for this year's spring.
                    if (season+yyyy in ndays):
                        ndays[season+yyyy] += 1
                    else:
                        ndays[season+yyyy] = 1

                    if ((season,mod,stn,yyyy) in out_sum):
                        out_sum[season,mod,stn,yyyy] = out_sum[\
                            season,mod,stn,yyyy] + dat_c
                    else:
                        out_sum[season,mod,stn,yyyy] = dat_c

                    #--- Get maximum.
                    if ((season,mod,stn,yyyy) in out_max):
                        if (dat_c > out_max[season,mod,stn,yyyy]):
                            out_max[season,mod,stn,yyyy] = dat_c

#                            if (stn == stn_pr):
#                                print '-----'
#                                print stn, mod, yyyy, dts_all[d], dat_c
#                                print 'out_max[season,mod,stn,yyyy] = ', \
#                                      out_max[season,mod,stn,yyyy]
                    else:
                        out_max[season,mod,stn,yyyy] = dat_c

                    #--- Get minimum.
                    if ((season,mod,stn,yyyy) in out_min):
                        if (dat_c < out_min[season,mod,stn,yyyy]):
                            out_min[season,mod,stn,yyyy] = dat_c
                    else:
                        out_min[season,mod,stn,yyyy] = dat_c
                        
                #--- Summer.
                if (mm == '06' or mm == '07' or mm == '08'):
                    season = 'summer'
                    #--- Count number of days for this year's summer.
                    if (season+yyyy in ndays):
                        ndays[season+yyyy] += 1
                    else:
                        ndays[season+yyyy] = 1
                    if ((season,mod,stn,yyyy) in out_sum):
                        out_sum[season,mod,stn,yyyy] = out_sum[\
                            season,mod,stn,yyyy] + dat_c
                    else:
                        out_sum[season,mod,stn,yyyy] = dat_c

                    #--- Get maximum.
                    if ((season,mod,stn,yyyy) in out_max):
                        if (dat_c > out_max[season,mod,stn,yyyy]):
                            out_max[season,mod,stn,yyyy] = dat_c
                    else:
                        out_max[season,mod,stn,yyyy] = dat_c

                    #--- Get minimum.
                    if ((season,mod,stn,yyyy) in out_min):
                        if (dat_c < out_min[season,mod,stn,yyyy]):
                            out_min[season,mod,stn,yyyy] = dat_c
                    else:
                        out_min[season,mod,stn,yyyy] = dat_c

                #--- Fall.
                if (mm == '09' or mm == '10' or mm == '11'):
                    season = 'fall'
                    #--- Count number of days for this year's fall.
                    if (season+yyyy in ndays):
                        ndays[season+yyyy] += 1
                    else:
                        ndays[season+yyyy] = 1
                    if ((season,mod,stn,yyyy) in out_sum):
                        out_sum[season,mod,stn,yyyy] = out_sum[\
                            season,mod,stn,yyyy] + dat_c
                    else:
                        out_sum[season,mod,stn,yyyy] = dat_c

                    #--- Get maximum.
                    if ((season,mod,stn,yyyy) in out_max):
                        if (dat_c > out_max[season,mod,stn,yyyy]):
                            out_max[season,mod,stn,yyyy] = dat_c
                    else:
                        out_max[season,mod,stn,yyyy] = dat_c

                    #--- Get minimum.
                    if ((season,mod,stn,yyyy) in out_min):
                        if (dat_c < out_min[season,mod,stn,yyyy]):
                            out_min[season,mod,stn,yyyy] = dat_c
                    else:
                        out_min[season,mod,stn,yyyy] = dat_c

                #--- Winter, December only.  Put December winter data into
                #--- next year's winter-- naming winters by their Jan/Feb
                #--- year rather than their Dec year.
                if (mm == '12'):
                    season = 'winter'
                    yyyyw = str(int(yyyy) + 1)
                    #--- Count number of days for this year's winter.
                    if (season+yyyyw in ndays):
                        ndays[season+yyyyw] += 1
                    else:
                        ndays[season+yyyyw] = 1
                    if ((season,mod,stn,yyyyw) in out_sum):
                        out_sum[season,mod,stn,yyyyw] = out_sum[\
                            season,mod,stn,yyyyw] + dat_c
                    else:
                        out_sum[season,mod,stn,yyyyw] = dat_c

                    #--- Get maximum.
                    if ((season,mod,stn,yyyyw) in out_max):
                        if (dat_c > out_max[season,mod,stn,yyyyw]):
                            out_max[season,mod,stn,yyyyw] = dat_c
                    else:
                        out_max[season,mod,stn,yyyyw] = dat_c

                    #--- Get minimum.
                    if ((season,mod,stn,yyyyw) in out_min):
                        if (dat_c < out_min[season,mod,stn,yyyyw]):
                            out_min[season,mod,stn,yyyyw] = dat_c
                    else:
                        out_min[season,mod,stn,yyyyw] = dat_c
                        
                #--- Winter, Jan & Feb.
                if (mm == '01' or mm == '02'):
                    season = 'winter'
                    #--- Count number of days for this year's winter.
                    if (season+yyyy in ndays):
                        ndays[season+yyyy] += 1
                    else:
                        ndays[season+yyyy] = 1
                    if ((season,mod,stn,yyyy) in out_sum):
                        out_sum[season,mod,stn,yyyy] = out_sum[\
                            season,mod,stn,yyyy] + dat_c
                    else:
                        out_sum[season,mod,stn,yyyy] = dat_c

                    #--- Get maximum.
                    if ((season,mod,stn,yyyy) in out_max):
                        if (dat_c > out_max[season,mod,stn,yyyy]):
                            out_max[season,mod,stn,yyyy] = dat_c
                    else:
                        out_max[season,mod,stn,yyyy] = dat_c

                    #--- Get minimum.
                    if ((season,mod,stn,yyyy) in out_min):
                        if (dat_c < out_min[season,mod,stn,yyyy]):
                            out_min[season,mod,stn,yyyy] = dat_c
                    else:
                        out_min[season,mod,stn,yyyy] = dat_c
                        
            #--- Get list of unique, sorted years found above.
            years = list(sorted(set(years_mod_stn)))

#            if (mod == 'bcc-csm1.1' and stn == 'KSEA'):
#                for yyyy in years:
#                    if ((season+yyyy) in ndays):
#                        print season,yyyy, ndays[season+yyyy]
#                    else:
#                        print season,yyyy, 'missing'

            #--- Check number of data points per unique years and nan out
            #--- ones that do not have enough (< min_season_days).
            for y in range(len(years)):
                yyyy = years[y]
                for s in range(len(seasons)):
                    season = seasons[s]
                    if ((season+yyyy) in ndays):
                        if (ndays[season+yyyy] < min_season_days):
                            out_sum[season,mod,stn,yyyy] = np.nan
                            out_max[season,mod,stn,yyyy] = np.nan
                            out_min[season,mod,stn,yyyy] = np.nan
                    else:
                        ndays[season+yyyy] = np.nan
                        out_sum[season,mod,stn,yyyy] = np.nan
                        out_max[season,mod,stn,yyyy] = np.nan
                        out_min[season,mod,stn,yyyy] = np.nan

            #--- if stat requested was 'avg', do averaging.
            if (stat == 'avg' or stat == 'max' or stat == 'min'):
                for y in range(len(years)):
                    yyyy = years[y]
                    for s in range(len(seasons)):
                        season = seasons[s]
                        out_avg[season,mod,stn,yyyy] = out_sum[\
                            season,mod,stn,yyyy] / ndays[season+yyyy]
                
            #--- Keep around all years found for this model and station.
            years_all.append(years_mod_stn)

    #--- Final, unique, sorted list of years found.
    years = list(sorted(set(years_all[0])))            

    if (stat == 'avg'):
        return out_avg, years
    elif (stat == 'max'):
        return out_max, years
    elif (stat == 'min'):
        return out_min, years
    else:
        return out_sum, years

#---------------------------------------------------------------------------
# Make time series of sent-in data for a set of stations for one model.
#---------------------------------------------------------------------------
def ts_stns(statplot, years, stns, mod, titlein, plotfname, var, stat, \
            cols, seasonsplot):

    fig, ax = plt.subplots( figsize=(10,6) )
    xlabs = []
    for s in range(len(stns)):
        ptot = []
        stn = stns[s]
        for y in range(len(years)):
            yyyy = years[y]
            for ss in range(len(seasons)):
                season = seasons[ss]
                if (season in seasonsplot):
                    if (var == 'PREC'):
                        ptot.append(statplot[season,mod,stns[s],yyyy] * mm2in)
                    elif (var == 'T2MAX' or var == 'T2MIN'):
                        ptot.append(statplot[season,mod,stns[s],yyyy] - 273.15)
                    elif (var.find('SPD') >= 0):
                        ptot.append(statplot[season,mod,stns[s],yyyy])
                    if (s == 0):
                        xlabs.append(season.title() + ' ' + years[y])
        plt.plot(ptot, alpha = 0.3, label = stn, color = cols[s])
        lab_c = stn + ', ' + str(smooth_fact) + '-pt smoothing'
        plt.plot(smooth(ptot, smooth_fact), label = lab_c, color = cols[s])

    #--- y-axis labeling.
    plt.ylim(ylims[var+stat])
    plt.ylabel('Seasonal ' + labels[var+stat], fontsize=fs+1)
    plt.tick_params(axis='y', which='major', labelsize=fs+1)    

    #--- x-axis labels.
    xticks_c = range(0,len(xlabs), 4)
    xlabs_c = [ xlabs[i] for i in xticks_c ]    
    plt.xticks(xticks_c)
    plt.tick_params(axis='x', which='major', labelsize=fs-1)
    ax.set_xticklabels(xlabs_c, rotation=90)        
    plt.xlabel('Season', fontsize=fs+1)

    plt.title(titlein, fontsize=titlefs, fontweight='bold')

    plt.tight_layout()
    plt.grid()
    plt.legend(fontsize = fs-1, loc = 'best')

    print 'xli ', plotfname, ' &'
    plt.savefig(plotfname)
    plt.close()

    return 1

#---------------------------------------------------------------------------
# Make time series of sent-in data for a set of models for one station.
#---------------------------------------------------------------------------
def ts_mods(statplot, years, obsplot, years_obs, stn, models, titlein, \
            plotfname, var, stat, cols, seasonsplot):

    fig, ax = plt.subplots( figsize=(10,6) )

    lw_sm = 1.8

    #--- Plot models.
    xlabs = []
    mod_all = np.ones((len(models),len(years))) * np.nan
    for m in range(len(models)):
        ptot = []
        mod = models[m]
        for y in range(len(years)):
            yyyy = years[y]
            for ss in range(len(seasons)):
                season = seasons[ss]
                key = (season,mod,stn,yyyy)
                if (season in seasonsplot):
                    if (var == 'PREC'):
                        ptot_c = statplot[key] * mm2in
                    elif (var == 'T2MAX' or var == 'T2MIN'):
                        ptot_c = statplot[key] - 273.15
                    elif (var.find('SPD') >= 0):
                        ptot_c = statplot[key]
                    if (m == 0):
                        xlabs.append(years[y])

                    ptot.append(ptot_c)
                    
            mod_all[m,y] = ptot_c
            
#        plt.plot(ptot, alpha = 0.20, label = mod.upper(), color = cols[m], \
#                 linewidth=lw_sm)
        lab_c = mod.upper() + ', ' + str(smooth_fact) + '-pt smoothing'
        plt.plot(smooth(ptot, smooth_fact), label = lab_c, color = cols[m], \
                 linewidth=lw_sm, alpha = 0.23)

    #--- Plot ensemble mean.
    plt.plot(np.mean(mod_all, axis=0), label = 'Ensemble Mean', \
             color = 'darkgreen', linewidth=2.2)
    
    #--- Plot observations.
    otot = []
    for y in range(len(years_obs)):
#        yyyy = years[y]
        yyyy = years_obs[y]
        for ss in range(len(seasons)):
            season = seasons[ss]
            key = (season,stn,yyyy)
            if (season in seasonsplot):
                otot.append(obsplot[key])
    plt.plot(otot, alpha=0.5, label='Observed', color='black', \
             linestyle='None', marker='o', markersize=3)
    lab_c = 'Observed ' + str(smooth_fact) + '-pt smoothing'
    plt.plot(smooth(otot,smooth_fact), label=lab_c, color='black', \
             linewidth=lw_sm)

    #--- y-axis labeling.
    plt.ylim(ylims[var+stat])
    plt.ylabel('Seasonal ' + labels[var+stat], fontsize=fs+1)
    plt.tick_params(axis='y', which='major', labelsize=fs+1)    

    #--- x-axis labels.
    xticks_c = range(0,len(xlabs), 4)
    xlabs_c = [ xlabs[i] for i in xticks_c ]    
    plt.xticks(xticks_c)
    plt.tick_params(axis='x', which='major', labelsize=fs-1)
    ax.set_xticklabels(xlabs_c, rotation=90)        
    plt.xlabel('Year', fontsize=fs+1)

    plt.title(titlein, fontsize=titlefs, fontweight='bold')

    plt.tight_layout()
    plt.grid()
    plt.legend(fontsize = fs-1, loc = 'best')

    print 'xli ', plotfname, ' &'
    plt.savefig(plotfname)
    plt.close()

    return 1

#---------------------------------------------------------------------------
# Loop over each models, grabbing data from extract wrfout files.
#---------------------------------------------------------------------------
def load_extract_data(geo_em, stns, latpts, lonpts, elevs, models, \
                      data_dirs, sdt, edt, vars, pickle_dir, load_pickle):

    #--- Load geo em file to get lat/lon/elev grids.  Also interpolate
    #--- model elevation to stns lat/lon point locations.
    print 'Loading:\n', geo_em
    (lat, lon, hgt) = load_geo_em(geo_em)
    elevs_mod  = []
    elevs_diff = []    
    for ns in range(len(stns)):
        dat_interp = bilinear_interpolate(lat, lon, hgt, \
                                          latpts[ns], lonpts[ns], 0)
        elevs_mod.append(dat_interp)
        elevs_diff.append(elevs[ns] - dat_interp)

    for m in range(len(models)):
        data_all = {}
        dts_all = []
        model = models[m]
        pickle_file = pickle_dir + '/extract_' + model + '_' + \
                      sdt + '_' + edt + '.pickle'

        #--- Get list of extracted files for this model.
        files = get_extract_files(data_dirs, model, sdt, edt, 0)

        files = list(sorted(set(files)))

        print 'Loading: '
        #--- Loop over all extract files, reading them in.
        for f in range(len(files)):
    
            (vardat, ntimes, dt) = load_wrfout(files[f], vars)
            print files[f], ntimes
                
            #--- Loop over each vars, interpolating data to stns for
            #--- each ntimes.
            for v in range(len(vars)):
    
                var = vars[v]
                vardat_c = vardat[var]
                    
                #--- Loop over each ntime, grabbing interpolated data for
                #--- variable var for each stns.
                for n in range(ntimes):
                    dt_c = time_increment(dt, n, dtfmt)
                    for ns in range(len(stns)):
                        dat_interp = bilinear_interpolate(lat, lon, \
                                                          vardat_c[:,:,n],\
                                                          latpts[ns], \
                                                          lonpts[ns], 0)
    
                        #--- Do lapse rate correction for temperature.
                        if (var == 'T2MAX' or var == 'T2MIN'):
                            dat_c = dat_interp - std_lapse * elevs_diff[ns]
                        else:
                            dat_c = dat_interp
    
                        data_all[var, stns[ns], dt_c] = dat_c
                        dts_all.append(dt_c)
    
        #--- Get sorted unique list of all dates seen.
        dts_unique = list(sorted(set(dts_all)))
        
        #--- Create pickle file.
        pickle.dump((model, data_all, dts_unique, vars, stns), \
                    open(pickle_file,'wb'), -1)

    return data_all, dts_unique, models, vars, stns

#---------------------------------------------------------------------------
# Interpolate from a sent-in grid to a sent in lat/lon point.
#---------------------------------------------------------------------------
def bilinear_interpolate(latgrid, longrid, datagrid, latpt, lonpt, deb):

    totdiff = abs(latgrid - latpt) + abs(longrid - lonpt)
    (i,j) = np.unravel_index(totdiff.argmin(), totdiff.shape)

    #--- Get lat/lon box in which latpt,lonpt resides.
    if (latpt >= latgrid[i,j] and lonpt >= longrid[i,j]):
        iif = i
        jjf = j
    elif (latpt < latgrid[i,j] and lonpt < longrid[i,j]):
        iif = i-1
        jjf = j-1
    elif (latpt >= latgrid[i,j] and lonpt < longrid[i,j]):
        iif = i-1
        jjf = j
    elif (latpt < latgrid[i,j] and lonpt >= longrid[i,j]):
        iif = i
        jjf = j-1

    (nx, ny) = np.shape(latgrid)

    if (deb == 1):
        print 'nx, ny  = ', nx, ny
        print 'iif,jjf = ', iif,jjf
        print 'latgrid[iif+1,jjf] = ', latgrid[iif+1,jjf]
        print 'latgrid[iif,jjf]   = ', latgrid[iif,jjf]

    if (iif >= (nx-1) or jjf >= (ny-1) or iif < 0 or jjf < 0):
        return mvc

    #--- Do bilinear interpolation to latpt,lonpt.
    dlat  = latgrid[iif,jjf+1] - latgrid[iif,jjf]
    dlon  = longrid[iif+1,jjf] - longrid[iif,jjf]
    dslat = latgrid[iif,jjf+1] - latpt
    dslon = longrid[iif+1,jjf] - lonpt

    wrgt = 1 - (dslon/dlon)
    wup  = 1 - (dslat/dlat)

    vll = datagrid[iif,jjf]
    vlr = datagrid[iif,jjf+1]
    vul = datagrid[iif+1,jjf]
    vur = datagrid[iif+1,jjf+1]

    if (deb > 1):
        print 'll lat, lon, val = ', latgrid[iif,jjf], longrid[iif,jjf], vll
        print 'lr lat, lon, val = ', latgrid[iif+1,jjf], longrid[iif+1,jjf], vlr
        print 'ur lat, lon, val = ', latgrid[iif+1,jjf+1], longrid[iif+1,jjf+1],vur
        print 'ul lat, lon, val = ', latgrid[iif,jjf+1], longrid[iif,jjf+1], vul
        print 'latpt, lonpt = ', latpt, lonpt
        
        print 'vll = ', vll
        print 'vlr = ', vlr
        print 'vul = ', vul
        print 'vur = ', vur

    datout = (1-wrgt) * ((1-wup) * vll + wup * vul) + \
        (wrgt) * ((1-wup) * vlr + wup * vur)

#    if (deb == 1):
#        print 'datout = ', datout
#        sys.exit()
#    if (datout == 0.0):
#        print 'nx, ny  = ', nx, ny
#        print 'iif,jjf = ', iif,jjf
#        print 'latgrid[iif+1,jjf] = ', latgrid[iif+1,jjf]
#        print 'latgrid[iif,jjf]   = ', latgrid[iif,jjf]
#        
#        print 'll lat, lon, val = ', latgrid[iif,jjf], longrid[iif,jjf], vll
#        print 'lr lat, lon, val = ', latgrid[iif+1,jjf], longrid[iif+1,jjf], vl#r
#        print 'ur lat, lon, val = ', latgrid[iif+1,jjf+1], longrid[iif+1,jjf+1],vur
#        print 'ul lat, lon, val = ', latgrid[iif,jjf+1], longrid[iif,jjf+1], vu#l
#        print 'latpt, lonpt = ', latpt, lonpt
#        
#        print 'vll = ', vll
#        print 'vlr = ', vlr
#        print 'vul = ', vul
#        print 'vur = ', vur
#
#        sys.exit()

    return datout

#---------------------------------------------------------------------------
# Run mapper.
#---------------------------------------------------------------------------
def run_mapper(var, model, rundir, season, files, yyyy, \
               lat, lon, levs_c, my_cmap ):

    file_plot = rundir + '/' + model + '_' + season + '_' + yyyy + '_' + \
                var + '.nc'
    if (var == 'T2MAX' or var == 'T2MIN'):
        if (os.path.exists(file_plot)):
            print 'nc file ', file_plot, ' already exists-- using it'
        else:
            syscom = 'ncra'
            for f in range(len(files[yyyy])):
                print yyyy, files[yyyy][f]
                syscom = syscom + ' ' + files[yyyy][f]
            syscom = syscom + ' ' + file_plot
            print 'syscom = ', syscom        
            os.system(syscom)
    elif (var == 'PREC'):
        if (os.path.exists(file_plot)):
            print 'nc file ', file_plot, ' already exists-- using it'
        else:
            syscom = 'ncra -y ttl'
            for f in range(len(files[yyyy])):
                print yyyy, files[yyyy][f]
                syscom = syscom + ' ' + files[yyyy][f]
            syscom = syscom + ' ' + file_plot
            print 'syscom = ', syscom        
            os.system(syscom)

    print 'Loading ', var, ' from ', file_plot
    (vardat, ntimes, dt) = load_wrfout(file_plot, [var])

    syscom = 'rm -f ' + file_plot
    os.system(syscom)
    
    titlein = model.upper() + ', ' + var_lab[var] + ', ' + \
              seasons_lab[season] + ' ' + yyyy

    plotdat = vardat[var][:,:,0]
    if (var == 'PREC'):
        plotdat = plotdat * mm2in
    else:
        plotdat = plotdat - 273.15

    for z in range(len(zooms)):
        plotfname = rundir + '/' + model + '_' + yyyy + '_' + season + '_' + \
                    var + '_' + zooms[z] + '.png'
        print 'colorbar_labs[var] = ', colorbar_labs[var]
        mapper(var, lat, lon, plotdat, levs_c, my_cmap, maplw, \
               colorbar_labs[var], titlein, plotfname, zooms[z])
    return 1

#---------------------------------------------------------------------------
# Mapper.
#---------------------------------------------------------------------------
def mapper(var, lat, lon, grid, levs, cmap_in, maplw, colorbar_lab, titlein, \
           plotfname, zoom):

    ur_lat = maxlat[zoom]
    ur_lon = maxlon[zoom]
    ll_lat = minlat[zoom]
    ll_lon = minlon[zoom]
    lat_ctr = ll_lat + ((ur_lat - ll_lat) * 0.5)
    lon_ctr = ll_lon + ((ur_lon - ll_lon) * 0.5)

    if (zoom == 'Z1'):
        res = 'i'
    elif (zoom == 'Z2'):
        res = 'h'
        
    fig = plt.figure(figsize=(width,height))
    # left, bottom, width, height:
    ax = fig.add_axes([0.00,0.05,0.99,0.91])
    map = Basemap(resolution = res,projection='lcc',\
                  llcrnrlon= ll_lon, llcrnrlat=ll_lat,\
                  urcrnrlon= ur_lon, urcrnrlat= ur_lat,\
                  lat_0=lat_ctr,lon_0=lon_ctr,lat_1=(ur_lat - ll_lat))

    #--- Get lat and lon data in map's x/y coordinates.
    x,y = map(lon, lat)

    #--- Draw coastlines, country boundaries, fill continents.
    map.drawcoastlines(linewidth = maplw)
    map.drawstates(linewidth = maplw)
    map.drawcountries(linewidth = maplw)

    #--- Draw the edge of the map projection region (the projection limb)
    map.drawmapboundary(linewidth = maplw)

    #--- Draw lat/lon grid lines every 30 degrees.
    map.drawmeridians(np.arange(0, 360, 30), linewidth = maplw)
    map.drawparallels(np.arange(-90, 90, 30), linewidth = maplw)

    cs = plt.contourf(x, y, grid, levs, cmap=cmap_in)

    cbar = map.colorbar(cs, location='bottom', pad="3%", size=0.1, ticks=levs)
    cbar.set_label(colorbar_lab, fontsize=fs, size=fs-1)
    cbar.ax.tick_params(labelsize=fs-2)

    if var != 'SNOW':
        csl = plt.contour(x, y, grid, levs, colors = 'black', linewidths=0.8)
        plt.clabel(csl, inline=1, fontsize=10, fmt='%2.1f', fontweights='bold')

    plt.title(titlein, fontsize=titlefs, fontweight='bold')

    #--- Save plot.
    print 'xli ', plotfname, ' &'
    plt.savefig(plotfname)

    plt.close()

#---------------------------------------------------------------------------
# Gaussian smoother, taken from
# https://www.swharden.com/wp/2008-11-17-linear-data-smoothing-in-python/
#---------------------------------------------------------------------------
def smoothListGaussian(list,strippedXs=False,degree=5):  

     window=degree*2-1  
     weight=np.array([1.0]*window)  
     weightGauss=[]  

     for i in range(window):  
         i=i-degree+1  
         frac=i/float(window)  
         gauss=1/(np.exp((4*(frac))**2))  
         weightGauss.append(gauss)  

     weight=np.array(weightGauss)*weight  
     smoothed=[0.0]*(len(list)-window)  

     for i in range(len(smoothed)):  
         smoothed[i]=sum(np.array(list[i:i+window])*weight)/sum(weight)  

     return smoothed  

#def running_mean(x, N):
#    cumsum = np.cumsum(np.insert(x, 0, 0)) 
#    return (cumsum[N:] - cumsum[:-N]) / float(N)

#---------------------------------------------------------------------------
# Smoother like Matlab's running average smoother 'smooth', taken from
# https://stackoverflow.com/questions/40443020/matlabs-smooth-implementation-n-point-moving-average-in-numpy-python
#---------------------------------------------------------------------------
def smooth(a,WSZ):
    # a: NumPy 1-D array containing the data to be smoothed
    # WSZ: smoothing window size needs, which must be odd number,
    # as in the original MATLAB implementation
    out0 = np.convolve(a,np.ones(WSZ,dtype=int),'valid')/WSZ    
    r = np.arange(1,WSZ-1,2)
    start = np.cumsum(a[:WSZ-1])[::2]/r
    stop = (np.cumsum(a[:-WSZ:-1])[::2]/r)[::-1]
    return np.concatenate((  start , out0, stop  ))
<file_sep>/old/old_old/test_read_ghcnd_obs_20180412.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re
import numpy as np
#from make_cmap import *
#from netCDF4 import Dataset as NetCDFFile
#from utils_wrfout import *
#from utils_date import *
#import pickle

#from ghcndextractor import *
#from ghcndextractor import ghcndextractor
import codecs
from utils_ghcnd_obs import *

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
plot_dir   = rcm_dir + '/plots'
pickle_dir = rcm_dir + '/pickle'
#ghcnFolder  = rcm_dir + '/obs'
ghcnd_dir   = rcm_dir + '/obs/ghcnd_all'
data_dir   = '/home/disk/r2d2/steed/cmip5/rcp8.5'

geo_em = '/home/disk/a125/steed/run/geo_em.d02.nc'

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
vars = ['PREC', 'T2MAX', 'T2MIN', 'SPDUV10MAX', 'SPDUV10MEAN', 'SPDUV10STD']
#sdt = '202001'
#edt = '202912'
sdt = '197001'
edt = '209912'

dtfmt = "%Y%m%d%H"

stns   = ['KSEA', 'KSMP', 'KYKM', 'KGEG', 'KPDX', 'KMFR', 'KHQM']
models = ['gfdl-cm3', 'miroc5']
stn_cols = ['b', 'r', 'g', 'c', 'm', 'k', 'grey']
mod_cols = ['b', 'r']
latpts = [47.44472, 47.27667, 46.56417, 47.62139, 45.59083, \
          42.38111, 46.97278]
lonpts = [-122.31361, -121.33722, -120.53361, -117.52778, -122.60028, \
          -122.87222, -123.93028]
elevs  = [130.0, 1207.0, 333.0, 735.0, 8.0, 405.0, 4.0]

ghcnd_dict = {
    'KSEA': 'USW00024233',
    'KSMP': 'USW00024237',
    'KYKM': 'USW00024243',
    'KGEG': 'USW00024157',
    'KPDX': 'USW00024229',
    'KMFR': 'USW00024225',
    'KHQM': 'USW00094225'}

#---------------------------------------------------------------------------
#
#---------------------------------------------------------------------------
elements = ['TMAX', 'TMIN', 'PRCP']

for s in range(len(stns)):
    stn_ghcnd = ghcnd_dict[stns[s]]
    file_c = ghcnd_dir + '/' + stn_ghcnd + '.dly'

    if os.path.isfile(file_c):
        print 'reading ', file_c
        (datag, dts_all) = read_ghcnd(file_c, elements)

    test = get_seasonal_stats_ghcnd(datag, dts_all)

#    for dt in dts_all:
#        print dt
#
#    for key in datag.keys():
#        print(key)
#
#    sys.exit()
#
#    el = 'TMAX'
#    dt = '19701205'
#    key = (el,dt)
##    if (el,yr,mo,dy) in datag:
#    if key in datag:        
#        print 'datag[key] = ', datag[key]
#    else:
#        print 'cannot see key in datag! key = ', key
#    sys.exit()

sys.exit()


<file_sep>/old/test_read_ghcnd_obs.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re
import numpy as np
#from make_cmap import *
#from netCDF4 import Dataset as NetCDFFile
#from utils_wrfout import *
#from utils_date import *
import pickle

#from ghcndextractor import *
#from ghcndextractor import ghcndextractor
import codecs
from utils_ghcnd_obs import *

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
plot_dir   = rcm_dir + '/plots'
pickle_dir = rcm_dir + '/pickle'
ghcnd_dir   = rcm_dir + '/obs/ghcnd_all'
data_dir   = '/home/disk/r2d2/steed/cmip5/rcp8.5'

geo_em = '/home/disk/a125/steed/run/geo_em.d02.nc'

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
vars = ['PREC', 'T2MAX', 'T2MIN', 'SPDUV10MAX', 'SPDUV10MEAN', 'SPDUV10STD']
#sdt = '202001'
#edt = '202912'
sdt = '197001'
edt = '201712'

#dtfmt = "%Y%m%d%H"

#stns   = ['KSEA', 'KSMP', 'KYKM', 'KGEG', 'KPDX', 'KMFR', 'KHQM']
stns   = ['KSEA']
models = ['gfdl-cm3', 'miroc5']
stn_cols = ['b', 'r', 'g', 'c', 'm', 'k', 'grey']
mod_cols = ['b', 'r']

#latpts = [47.44472, 47.27667, 46.56417, 47.62139, 45.59083, \
#          42.38111, 46.97278]
#lonpts = [-122.31361, -121.33722, -120.53361, -117.52778, -122.60028, \
#          -122.87222, -123.93028]
#elevs  = [130.0, 1207.0, 333.0, 735.0, 8.0, 405.0, 4.0]

latpts = [47.44472]
lonpts = [-122.31361]
elevs  = [130.0]

elements = ['TMAX']

#---------------------------------------------------------------------------
#
#---------------------------------------------------------------------------
(data_all, dts_all) = read_ghcnd(stns, elements, ghcnd_dir, sdt, edt)

tc = data_all['TMAX','KSEA','20170101']
print (tc * (9./5.)) + 32
tc = data_all['TMAX','KSEA','20170701']
print (tc * (9./5.)) + 32
tc = data_all['TMAX','KSEA','20170702']
print (tc * (9./5.)) + 32
tc = data_all['TMAX','KSEA','20170703']
print (tc * (9./5.)) + 32


##---------------------------------------------------------------------------
##
##---------------------------------------------------------------------------
#elements = ['TMAX', 'TMIN', 'PRCP']
#
#load_pickle = 1
#pickle_file = pickle_dir + '/ghcnd_test.pickle'
#if load_pickle and os.path.isfile(pickle_file):
#    print 'reading pickle file ', pickle_file
#    (data_all, dts_all, elements, stns) = \
#               pickle.load(open(pickle_file, 'rb'))
#    print 'done!'
#else:
#    (data_all, dts_all) = read_ghcnd(stns, elements, ghcnd_dir, sdt, edt)
#    pickle.dump((data_all, dts_all, elements, stns), \
#                open(pickle_file,'wb'), -1)
#
#var = 'TMAX'
#(tmx_mx, years) = get_seasonal_stats_ghcnd(data_all, dts_all, stns, var, 'max')
#(tmx_avg, years) = get_seasonal_stats_ghcnd(data_all, dts_all, stns, var, 'avg')
#var = 'TMIN'
#(tmn_mn, years) = get_seasonal_stats_ghcnd(data_all, dts_all, stns, var, 'min')
#(tmn_avg, years) = get_seasonal_stats_ghcnd(data_all, dts_all, stns, var, 'avg')
#var = 'PRCP'
#(pcp_mx, years) = get_seasonal_stats_ghcnd(data_all, dts_all, stns, var, 'max')
#(pcp_tot, years) = get_seasonal_stats_ghcnd(data_all, dts_all, stns, var, 'tot')
#
#for year in years:
#    key = ('fall','KSEA',year)
#    if (key in pcp_tot):
#        print year, pcp_tot[key]
#    else:
#        print key, ' not in there'

sys.exit()


<file_sep>/igtg_check_miroc5.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re
import numpy as np
from igtg_check_utils_miroc5 import *
import matplotlib.pyplot as plt
#import pickle
from make_cmap import *
from Scientific.IO.NetCDF import *

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir  = '/home/disk/spock/jbaars/rcm'
py_dir   = rcm_dir + '/python'
plot_dir = rcm_dir + '/plots'
data_dir = '/home/disk/mass/jbaars/cmip5/rcp8.5/miroc5/test_unstaggertos'

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
varname = 'tos'
file_old = data_dir + '/' + 'inlatlon'
#file_new = data_dir + '/' + 'indata'
file_new = data_dir + '/' + 'testdata'

itime = 1
ilev  = 1

title = 'both'
plotfname = './both8.png'
#title = 'inlatlon'
#plotfname = './inlatlon.png'
#title = 'indata'
#plotfname = './indata.png'

#i1 = 4
#j1 = 107
#i2 = 174
#j2 = 183
i1 = 147
j1 = 92
i2 = 1
j2 = 1

#minlat = 50.0
#maxlat = 70.0
#minlon = -10.0
#maxlon = 20.0
#minlat = 55.0
#maxlat = 65.0
#minlon = 0.0
#maxlon = 10.0

#--- account for python counters starting at 0.
i1 = i1 - 1
j1 = j1 - 1

#---------------------------------------------------------------------------
# Load old and new files.
#---------------------------------------------------------------------------
(lat_old, lon_old, tos_old) = load_nc(file_old, 0)
(lat_new, lon_new, tos_new) = load_nc(file_new, 1)

print 'lat_old = ', lat_old
print 'lat_new = ', lat_new
print ''
print 'tos_old = ', tos_old
print 'tos_new = ', tos_new
print ''
print 'lon_old = ', lon_old  
print 'lon_new = ', lon_new  

#sys.exit()

#nx,ny = np.shape(lon_old)
#print 'nx, ny = ', nx, ny
#for ii in range(nx):
#    for jj in range(ny):
#
#        if (lat_old[ii,jj] > minlat and lat_old[ii,jj] < maxlat):
#
#            lon_c = lon_old[ii,jj]
#            if (lon_c > 180.0):
#                lon_c = lon_c - 360
#            
#            print 'is ', minlon, ' < ', lon_c, ' < ', maxlon
#            if (lon_c > minlon and lon_c < maxlon):
#                print 'yes'
#            else:
#                print 'no'
#sys.exit()


lat_thresh = 4.0
lon_thresh = 5.0
print 'lat_old[i1,j1], lon_old[i1,j1] = ', lat_old[i1,j1], lon_old[i1,j1]
maxlat = lat_old[i1,j1] + lat_thresh
minlat = lat_old[i1,j1] - lat_thresh
maxlon = lon_old[i1,j1] + lon_thresh - 360
minlon = lon_old[i1,j1] - lon_thresh - 360

print 'minlat, maxlat = ', minlat, maxlat
print 'minlon, maxlon = ', minlon, maxlon
#sys.exit()

#---------------------------------------------------------------------------
# Plot old data.
#---------------------------------------------------------------------------
mapper(i1, j1, i2, j2, lat_old, lon_old, lat_new, lon_new, tos_new, \
       minlat, maxlat, minlon, maxlon, title, plotfname)

sys.exit()

<file_sep>/old/old_old/utils_wrfout_20180116.py
#!/usr/bin/python
import os, sys, glob, re
import numpy as np
from netCDF4 import Dataset as NetCDFFile

#from datetime import date, timedelta, datetime
#import time, stat
#from collections import defaultdict
#from utils_date import *
#from mpl_toolkits.basemap import Basemap, cm
#import matplotlib.pyplot as plt

mm2in = 0.0393700787

#---------------------------------------------------------------------------
# Load geo_em file.
#---------------------------------------------------------------------------
def load_geo_em(geo_em):
    nc = NetCDFFile(geo_em, 'r')
    lat = nc.variables['XLAT_M'][0,:,:]
    lon = nc.variables['XLONG_M'][0,:,:]
    lat = np.transpose(lat)
    lon = np.transpose(lon)
    nc.close()
    return lat, lon

#---------------------------------------------------------------------------
# Load wrfout file.
#---------------------------------------------------------------------------
def load_wrfout(wrfout, var):
    nc = NetCDFFile(wrfout, 'r')
    ntimes = len(nc.dimensions['Time'])
    dataout = nc.variables[var][:,:,:]
    dataout = np.transpose(dataout)

    #--- Get date from global attributes and clean it up and trim it down.
    dt_all = nc.START_DATE
    dt = dt_all.replace('-', '')
    dt = dt.replace(':', '')
    dt = dt.replace('_', '')        
    dt = dt[0:10]
    nc.close()
    return dataout, ntimes, dt

#---------------------------------------------------------------------------
# Get list of extracted wrfout files between sdt_yyyymm and edt_yyyymm.
#---------------------------------------------------------------------------
def get_extract_files(data_dir, model, sdt_yyyymm, edt_yyyymm):
    files_out = []
    files = glob.glob(data_dir + '/' + model + '*' + '.nc')
    for f in range(len(files)):
        dt_c = re.findall(r'\.(\d{10})\.', files[f])
        if (dt_c):
            dt_c = dt_c[0]
            yyyymm_c = dt_c[0:6]
            if (yyyymm_c >= sdt_yyyymm and yyyymm_c <= edt_yyyymm):
                files_out.append(files[f])

    return files_out

#---------------------------------------------------------------------------
# Get seasonal totals.  NEED TO SORT OUT HOW TO HANDLE WINTERS AT THE
# EDGES (FIRST YEAR AND LAST) WHICH WON"T HAVE A FULL 3 MONTHS.
#---------------------------------------------------------------------------
def get_seasonal_totals(data_all, dts_all, models, stns):
    springs = {}
    summers = {}
    falls   = {}
    winters = {}
    
    for s in range(len(stns)):
        stn = stns[s]
        for m in range(len(models)):
            mod = models[m]
            for d in range(len(dts_all)):
                yyyy = dts_all[d][0:4]
                mm = dts_all[d][4:6]
                if (mm == '03' or mm == '04' or mm == '05'):

#                   if (stn == stn_pr and yyyy == yyyy_pr):
#                       print dts_all[d], mm, data_all[mod,stn,dts_all[d]]

                    if ((mod,stn,yyyy) in springs):
                        springs[mod,stn,yyyy] = springs[mod,stn,yyyy] + \
                                                data_all[mod,stn,dts_all[d]]
                    else:
                        springs[mod,stn,yyyy] = data_all[mod,stn,dts_all[d]]

#                    if (stn == stn_pr and yyyy == yyyy_pr):
#                        print 'springs tot = ', springs[mod,stn,yyyy]
                        
                if (mm == '06' or mm == '07' or mm == '08'):
                    if ((mod,stn,yyyy) in summers):
                        summers[mod,stn,yyyy] = summers[mod,stn,yyyy] + \
                                                data_all[mod,stn,dts_all[d]]
                    else:
                        summers[mod,stn,yyyy] = data_all[mod,stn,dts_all[d]]
                if (mm == '09' or mm == '10' or mm == '11'):
                    if ((mod,stn,yyyy) in falls):
                        falls[mod,stn,yyyy] = falls[mod,stn,yyyy] + \
                                              data_all[mod,stn,dts_all[d]]
                    else:
                        falls[mod,stn,yyyy] = data_all[mod,stn,dts_all[d]]
                if (mm == '12' or mm == '01' or mm == '02'):
                    if ((mod,stn,yyyy) in winters):
                        winters[mod,stn,yyyy] = winters[mod,stn,yyyy] + \
                                                data_all[mod,stn,dts_all[d]]
                    else:
                        winters[mod,stn,yyyy] = data_all[mod,stn,dts_all[d]]

    return springs, summers, falls, winters


#---------------------------------------------------------------------------
# Interpolate from a sent-in grid to a sent in lat/lon point.
#---------------------------------------------------------------------------
def bilinear_interpolate(latgrid, longrid, datagrid, latpt, lonpt, deb):

    totdiff = abs(latgrid - latpt) + abs(longrid - lonpt)
    (i,j) = np.unravel_index(totdiff.argmin(), totdiff.shape)

    #--- Get lat/lon box in which latpt,lonpt resides.
    if (latpt >= latgrid[i,j] and lonpt >= longrid[i,j]):
        iif = i
        jjf = j
    elif (latpt < latgrid[i,j] and lonpt < longrid[i,j]):
        iif = i-1
        jjf = j-1
    elif (latpt >= latgrid[i,j] and lonpt < longrid[i,j]):
        iif = i-1
        jjf = j
    elif (latpt < latgrid[i,j] and lonpt >= longrid[i,j]):
        iif = i
        jjf = j-1

    (nx, ny) = np.shape(latgrid)

    if (deb == 1):
        print 'nx, ny  = ', nx, ny
        print 'iif,jjf = ', iif,jjf
        print 'latgrid[iif+1,jjf] = ', latgrid[iif+1,jjf]
        print 'latgrid[iif,jjf]   = ', latgrid[iif,jjf]

    if (iif >= (nx-1) or jjf >= (ny-1) or iif < 0 or jjf < 0):
        return mvc

    #--- Do bilinear interpolation to latpt,lonpt.
    dlat  = latgrid[iif,jjf+1] - latgrid[iif,jjf]
    dlon  = longrid[iif+1,jjf] - longrid[iif,jjf]
    dslat = latgrid[iif,jjf+1] - latpt
    dslon = longrid[iif+1,jjf] - lonpt

    wrgt = 1 - (dslon/dlon)
    wup  = 1 - (dslat/dlat)

    vll = datagrid[iif,jjf]
    vlr = datagrid[iif,jjf+1]
    vul = datagrid[iif+1,jjf]
    vur = datagrid[iif+1,jjf+1]

    if (deb > 1):
        print 'll lat, lon, val = ', latgrid[iif,jjf], longrid[iif,jjf], vll
        print 'lr lat, lon, val = ', latgrid[iif+1,jjf], longrid[iif+1,jjf], vlr
        print 'ur lat, lon, val = ', latgrid[iif+1,jjf+1], longrid[iif+1,jjf+1],vur
        print 'ul lat, lon, val = ', latgrid[iif,jjf+1], longrid[iif,jjf+1], vul
        print 'latpt, lonpt = ', latpt, lonpt
        
        print 'vll = ', vll
        print 'vlr = ', vlr
        print 'vul = ', vul
        print 'vur = ', vur

    datout = (1-wrgt) * ((1-wup) * vll + wup * vul) + \
        (wrgt) * ((1-wup) * vlr + wup * vur)

#    if (deb == 1):
#        print 'datout = ', datout
#        sys.exit()
#    if (datout == 0.0):
#        print 'nx, ny  = ', nx, ny
#        print 'iif,jjf = ', iif,jjf
#        print 'latgrid[iif+1,jjf] = ', latgrid[iif+1,jjf]
#        print 'latgrid[iif,jjf]   = ', latgrid[iif,jjf]
#        
#        print 'll lat, lon, val = ', latgrid[iif,jjf], longrid[iif,jjf], vll
#        print 'lr lat, lon, val = ', latgrid[iif+1,jjf], longrid[iif+1,jjf], vl#r
#        print 'ur lat, lon, val = ', latgrid[iif+1,jjf+1], longrid[iif+1,jjf+1],vur
#        print 'ul lat, lon, val = ', latgrid[iif,jjf+1], longrid[iif,jjf+1], vu#l
#        print 'latpt, lonpt = ', latpt, lonpt
#        
#        print 'vll = ', vll
#        print 'vlr = ', vlr
#        print 'vul = ', vul
#        print 'vur = ', vur
#
#        sys.exit()


    return datout
<file_sep>/create_wrf_state_mask.py
#!/usr/bin/python
import os, sys, glob, re
import numpy as np
from netCDF4 import Dataset as NetCDFFile
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, cm, shiftgrid
import pickle
from utils_date import *
import csv

ll_lat =   45.3
ur_lat =   49.3
ll_lon = -125.0
ur_lon = -116.5

fs      = 9
titlefs = 9
width   = 10
height  = 8
#width   = 30
#height  = 24
maplw   = 1.0

geo_em = '/home/disk/a125/steed/run/geo_em.d02.nc'
mask_file = '/home/disk/spock/jbaars/rcm/data/mask_pts.dat'

#---------------------------------------------------------------------------
# Read mask lat/lon points file.
#---------------------------------------------------------------------------
mask = open(mask_file)
csvReader = csv.reader(mask)
maski = []
maskj = []
for row in csvReader:
    maski.append(int(row[0]))
    maskj.append(int(row[1]))

#---------------------------------------------------------------------------
# Load geo_em file.
#---------------------------------------------------------------------------
print 'Loading ', geo_em
nc = NetCDFFile(geo_em, 'r')
lat = nc.variables['XLAT_M'][0,:,:]
lon = nc.variables['XLONG_M'][0,:,:]
hgt = nc.variables['HGT_M'][0,:,:]    
lat = np.transpose(lat)
lon = np.transpose(lon)
hgt = np.transpose(hgt)
nc.close()

#---------------------------------------------------------------------------
#
#---------------------------------------------------------------------------
lat_ctr = ll_lat + ((ur_lat - ll_lat) * 0.5)
lon_ctr = ll_lon + ((ur_lon - ll_lon) * 0.5)

res = 'i'
#res = 'h'
        
fig = plt.figure(figsize=(width,height))
# left, bottom, width, height:
ax = fig.add_axes([0.00,0.05,0.99,0.91])
map = Basemap(resolution = res,projection='lcc',\
              llcrnrlon= ll_lon, llcrnrlat=ll_lat,\
              urcrnrlon= ur_lon, urcrnrlat= ur_lat,\
              lat_0=lat_ctr,lon_0=lon_ctr,lat_1=(ur_lat - ll_lat))

#--- Get lat and lon data in map's x/y coordinates.
x,y = map(lon, lat)

#--- Draw coastlines, country boundaries, fill continents.
map.drawcoastlines(linewidth = maplw)
map.drawstates(linewidth = maplw)
map.drawcountries(linewidth = maplw)

#--- Draw the edge of the map projection region (the projection limb)
map.drawmapboundary(linewidth = maplw)

#-- Add dots grid points.
(nx,ny) = lon.shape

#for x in range(nx):
#    for y in range(ny):
#        lat_c = lat[x,y]
#        lon_c = lon[x,y]
#        if (lat_c >= ll_lat and lat_c <= ur_lat and \
#            lon_c >= ll_lon and lon_c <= ur_lon):
#            xs, ys = map(lon_c, lat_c)
#            plt.plot(xs, ys, 'ro', markersize = 3)
#            plt.text(xs, ys, str(x) + ',' + str(y), fontsize=8)

plt.title('WA state mask grid points', fontsize=titlefs, fontweight='bold')

for m in range(len(maski)):
    lat_c = lat[maski[m],maskj[m]]
    lon_c = lon[maski[m],maskj[m]]
    xs, ys = map(lon_c, lat_c)
    plt.plot(xs, ys, 'ro', markersize = 4)


#--- Save plot.
plotfname = './WA_mask.png'
print 'xli ', plotfname, ' &'
plt.savefig(plotfname)

plt.close()

sys.exit()

<file_sep>/old/old_old/plot_wrfout_grid_20180703.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re, math
import numpy as np
#import matplotlib.pyplot as plt
from make_cmap import *
from netCDF4 import Dataset as NetCDFFile
from utils_wrfout import *
from utils_date import *
import pickle
from collections import defaultdict
from make_cmap import *

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
plot_dir   = rcm_dir + '/plots'
pickle_dir = rcm_dir + '/pickle'
run_dirs   = rcm_dir + '/rundirs'
data_dir = '/home/disk/r2d2/steed/cmip5/rcp8.5'

geo_em = '/home/disk/a125/steed/run/geo_em.d02.nc'

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
#vars = ['PREC', 'T2MAX', 'T2MIN', 'SPDUV10MAX', 'SPDUV10MEAN', 'SPDUV10STD']
vars = ['PREC', 'T2MAX', 'T2MIN']
vars = ['T2MIN']
#sdt = '202001'
#edt = '202912'
#sdt = '197001'
sdt = '204501'
edt = '209912'
#models = ['gfdl-cm3']
models = ['miroc5']
dtfmt = "%Y%m%d%H"

#var_c = 'T2MIN'
##print 'levs[var_c] = ', levs[var_c]
#print 'cmaps[var_c] = ', cmaps[var_c]
#test =  cmaps[var_c].extend(cmaps[var_c])
#print 'test = ', test
#sys.exit()

#---------------------------------------------------------------------------
# Make a color map using make_cmap.
#---------------------------------------------------------------------------
levs_norm_c = []
for i in range(len(levs_temp)):
    x = float(levs_temp[i])
    norm_c = (x - min(levs_temp)) / (max(levs_temp) - min(levs_temp))
    levs_norm_c.append(norm_c)
cmap_temp = make_cmap(cmap_temp, bit = True, position = levs_norm_c)

levs_norm_c = []
for i in range(len(levs_pcp)):
    x = float(levs_pcp[i])
    norm_c = (x - min(levs_pcp)) / (max(levs_pcp) - min(levs_pcp))
    levs_norm_c.append(norm_c)
cmap_pcp = make_cmap(cmap_pcp, bit = True, position = levs_norm_c)

#---------------------------------------------------------------------------
# Make run directory, load geo_em file.
#---------------------------------------------------------------------------
rundir = run_dirs + '/plot_wrfout_grid.' + str(os.getpid())
print 'making ', rundir
os.makedirs(rundir)

print 'Loading ', geo_em
(lat, lon, hgt) = load_geo_em(geo_em)

#---------------------------------------------------------------------------
# For each model, make a maps of every season...
#---------------------------------------------------------------------------
for m in range(len(models)):

    model = models[m]
    data_dir_c = data_dir + '/' + model + '/extract'

    #--- Get list of extracted files for this model.
    files = get_extract_files(data_dir_c, model, sdt, edt)

    #--- Get files broken out by year and season.
    (spr_files, sum_files, fal_files, win_files, years) = \
                get_seasonal_files(files)

    #--- Loop over year, making maps of each 
    for y in range(len(years)):
        yyyy = years[y]

        #--- Winter plots.
        if (win_files[yyyy] == win_files[yyyy]):
            iret = run_mapper('T2MAX', model, rundir, 'winter', win_files,\
                              yyyy, lat, lon, levs_temp, cmap_temp)
            iret = run_mapper('T2MIN', model, rundir, 'winter', win_files,\
                              yyyy, lat, lon, levs_temp, cmap_temp)
            iret = run_mapper('PREC', model, rundir, 'winter', win_files,\
                              yyyy, lat, lon, levs_pcp, cmap_pcp)

        #--- Summer plots.
        if (sum_files[yyyy] == sum_files[yyyy]):
            iret = run_mapper('T2MAX', model, rundir, 'summer', sum_files,\
                              yyyy, lat, lon, levs_temp, cmap_temp)
            iret = run_mapper('T2MIN', model, rundir, 'summer', sum_files,\
                              yyyy, lat, lon, levs_temp, cmap_temp)
            iret = run_mapper('PREC', model, rundir, 'summer', sum_files,\
                              yyyy, lat, lon, levs_pcp, cmap_pcp)

        #--- Spring plots.
        if (spr_files[yyyy] == spr_files[yyyy]):
            iret = run_mapper('T2MAX', model, rundir, 'spring', spr_files,\
                              yyyy, lat, lon, levs_temp, cmap_temp)
            iret = run_mapper('T2MIN', model, rundir, 'spring', spr_files,\
                              yyyy, lat, lon, levs_temp, cmap_temp)
            iret = run_mapper('PREC', model, rundir, 'spring', spr_files,\
                              yyyy, lat, lon, levs_pcp, cmap_pcp)

        #--- Fall plots.
        if (fal_files[yyyy] == fal_files[yyyy]):
            iret = run_mapper('T2MAX', model, rundir, 'fall', fal_files,\
                              yyyy, lat, lon, levs_temp, cmap_temp)
            iret = run_mapper('T2MIN', model, rundir, 'fall', fal_files,\
                              yyyy, lat, lon, levs_temp, cmap_temp)
            iret = run_mapper('PREC', model, rundir, 'fall', fal_files,\
                              yyyy, lat, lon, levs_pcp, cmap_pcp)



#        #--- Winter plots.
#        if (win_files[yyyy] == win_files[yyyy]):
#            for v in range(len(vars)):
#                var_c = vars[v]
#                iret = run_mapper(vars[v], model, rundir, 'winter', win_files,\
#                                  yyyy, lat, lon, levs[var_c], cmaps[var_c])
#
#        #--- Summer plots.
#        if (sum_files[yyyy] == sum_files[yyyy]):
#            for v in range(len(vars)):
#                var_c = vars[v]
#                iret = run_mapper(var_c, model, rundir, 'summer', sum_files,\
#                                  yyyy, lat, lon, \
#                                  levs[var_c], cmaps[str(var_c)])
#
#        #--- Spring plots.
#        if (spr_files[yyyy] == spr_files[yyyy]):
#            for v in range(len(vars)):
#                var_c = vars[v]
#                iret = run_mapper(vars[v], model, rundir, 'spring', spr_files,\
#                                  yyyy, lat, lon, levs[var_c], cmaps[var_c])
#            
#        #--- Fall plots.
#        if (fal_files[yyyy] == fal_files[yyyy]):
#            for v in range(len(vars)):
#                var_c = vars[v]
#                iret = run_mapper(vars[v], model, rundir, 'fall', fal_files,\
#                                  yyyy, lat, lon, levs[var_c], cmaps[var_c])
#
#        sys.exit()


sys.exit()

<file_sep>/old/old_old/plot_wrfout_grid_20180322.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re, math
import numpy as np
#import matplotlib.pyplot as plt
from make_cmap import *
from netCDF4 import Dataset as NetCDFFile
from utils_wrfout import *
from utils_date import *
import pickle
from collections import defaultdict
from make_cmap import *

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
plot_dir   = rcm_dir + '/plots'
pickle_dir = rcm_dir + '/pickle'
run_dirs   = rcm_dir + '/rundirs'
data_dir = '/home/disk/r2d2/steed/cmip5/rcp8.5'

geo_em = '/home/disk/a125/steed/run/geo_em.d02.nc'

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
vars = ['PREC', 'T2MAX', 'T2MIN', 'SPDUV10MAX', 'SPDUV10MEAN', 'SPDUV10STD']
#sdt = '202001'
#edt = '202912'
sdt = '197001'
edt = '209912'
models = ['gfdl-cm3']
dtfmt = "%Y%m%d%H"

#---------------------------------------------------------------------------
# Make a color map using make_cmap.
#---------------------------------------------------------------------------
#if (varname == 'tos'):
#    levs_c = levs_temp
#    cmap_c = cmap_temp
#elif(varname == 'ua'):
#    levs_c = levs_prec
#    cmap_c = cmap_prec
levs_c = levs_temp
cmap_c = cmap_temp
        
levs_norm_c = []
for i in range(len(levs_c)):
    x = float(levs_c[i])
    norm_c = (x - min(levs_c)) / (max(levs_c) - min(levs_c))
    levs_norm_c.append(norm_c)

my_cmap = make_cmap(cmap_c, bit = True, position = levs_norm_c)

#---------------------------------------------------------------------------
# Make run directory, load geo_em file.
#---------------------------------------------------------------------------
rundir = run_dirs + '/plot_wrfout_grid.' + str(os.getpid())
print 'making ', rundir
os.makedirs(rundir)

print 'Loading ', geo_em
(lat, lon, hgt) = load_geo_em(geo_em)

#---------------------------------------------------------------------------
# For each model, make a maps of every season...
#---------------------------------------------------------------------------
for m in range(len(models)):

    model = models[m]
    data_dir_c = data_dir + '/' + model + '/extract'

    #--- Get list of extracted files for this model.
    files = get_extract_files(data_dir_c, model, sdt, edt)

    #--- Get files broken out by year and season.
    (spr_files, sum_files, fal_files, win_files, years) = \
                get_seasonal_files(files)

    #--- Loop over year, making maps of each 
    for y in range(len(years)):
        yyyy = years[y]

        #--- Winter plots.
        if (win_files[yyyy] == win_files[yyyy]):
            test = run_mapper('T2MAX', model, rundir, 'winter', win_files, \
                              yyyy, 'Temperature (K)', lat, lon, levs_c, \
                              my_cmap)
            
        if (sum_files[yyyy] == sum_files[yyyy]):
            test = run_mapper('T2MAX', model, rundir, 'summer', sum_files, \
                              yyyy, 'Temperature (K)', lat, lon, levs_c, \
                              my_cmap)

            test = run_mapper('PREC', model, rundir, 'summer', sum_files, \
                              yyyy, 'Precipitation (in.)', lat, lon, levs_c, \
                              my_cmap)
            sys.exit()

        sys.exit()
            
#            file_plot = rundir + '/sum_' + yyyy + '.nc'
#            syscom = 'ncra'
#            for f in range(len(sum_files[yyyy])):
#                print yyyy, sum_files[yyyy][f]
#                syscom = syscom + ' ' + sum_files[yyyy][f]
#            syscom = syscom + ' ' + file_plot
#            print 'syscom = ', syscom
#            os.system(syscom)
#
#            var = 'T2MAX'
#            print 'Loading ', var, ' from ', file_plot
#            (vardat, ntimes, dt) = load_wrfout(file_plot, [var])
#
#            colorbar_lab = 'Temperature (K)'
#            titlein = model.upper() + \
#                      ', Average Max Temperature (K), Summer (JJA) ' + yyyy
#
#            plotdat = vardat[var][:,:,0]
#            for z in range(len(zooms)):
#                plotfname = rundir + '/sum_' + var + '_' + yyyy + '_' + \
#                            zooms[z] + '.png'
#                mapper(lat, lon, plotdat, levs_c, my_cmap, maplw, \
#                       colorbar_lab, titlein, plotfname, zooms[z])

    sys.exit()


sys.exit()

<file_sep>/plot_lat_tos.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re
import numpy as np
from igtg_check_utils import *
import matplotlib.pyplot as plt
#import pickle
from make_cmap import *

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir  = '/home/disk/spock/jbaars/rcm'
py_dir   = rcm_dir + '/python'
plot_dir = rcm_dir + '/plots'

x = range(140,161)
dat = [ 285.5343, 285.7745, 286.0928, 286.3722, 286.6160, 286.6160, \
        286.8224, 286.3380, 286.5203, 286.7137, 286.8461, 286.9745, \
        287.1689, 287.3819, 287.6205, 287.6205, 287.4508, 287.9211, \
        288.3215, 288.6165, 288.8190 ]
#x = range(0,len(dat))

plt.plot(x, dat, 'b')
plt.plot(x, dat, 'bo')
plt.grid()

plt.title('j = 92, i = 140 - 160')
plt.savefig('lat_tos_test.png')
plt.close()

sys.exit()

<file_sep>/utils_load_data.py
#!/usr/bin/python
import os, sys, glob, re, math
import numpy as np
from collections import defaultdict
from utils_cmap import *
import pickle
from utils_date import *
from utils_stats import *
from utils_ghcnd_obs import *

#---------------------------------------------------------------------------
# Get model pickle file name
#---------------------------------------------------------------------------
def get_mod_pkl_name (pdir, model, sdt, edt):
    pf = pdir + '/extract_' + model + '_' + sdt + '_' + edt + '.pkl'
    return pf

#---------------------------------------------------------------------------
# Load geo_em file.
#---------------------------------------------------------------------------
def load_geo_em(geo_em):
    nc = NetCDFFile(geo_em, 'r')
    lat = nc.variables['XLAT_M'][0,:,:]
    lon = nc.variables['XLONG_M'][0,:,:]
    hgt = nc.variables['HGT_M'][0,:,:]    
    lat = np.transpose(lat)
    lon = np.transpose(lon)
    hgt = np.transpose(hgt)
    nc.close()
    return lat, lon, hgt

#---------------------------------------------------------------------------
# Get netcdf dimension
#---------------------------------------------------------------------------
def get_nc_dim(ncin, dimname):
    nc = NetCDFFile(ncin, 'r')
    dim = len(nc.dimensions[dimname])
    nc.close()
    return dim

#---------------------------------------------------------------------------
# Load wrfout file.
#---------------------------------------------------------------------------
def load_wrfout(wrfout, vars):
    nc = NetCDFFile(wrfout, 'r')
    ntimes = len(nc.dimensions['Time'])

    dataout = {}
    for v in range(len(vars)):
        var = vars[v]
        dat_tmp = nc.variables[var][:,:,:]
        dat_tmp = np.transpose(dat_tmp)
        dataout[var] = dat_tmp

    #--- Get date from global attributes and clean it up and trim it down.
    dt_all = nc.START_DATE
    dt = dt_all.replace('-', '')
    dt = dt.replace(':', '')
    dt = dt.replace('_', '')        
    dt = dt[0:10]
    nc.close()
    return dataout, ntimes, dt

#---------------------------------------------------------------------------
# Load SWE wrfout file.
#---------------------------------------------------------------------------
def load_wrfout_swe(wrfout, vars):
    nc = NetCDFFile(wrfout, 'r')
    ntimes = len(nc.dimensions['Time'])

    dataout = {}
    for v in range(len(vars)):
        var = vars[v]
        dat_tmp = nc.variables[var][:,:,:]
        dat_tmp = np.transpose(dat_tmp)
        dataout[var] = dat_tmp
    nc.close()
    return dataout, ntimes

#---------------------------------------------------------------------------
# Get list of extracted wrfout files between sdt_yyyymm and edt_yyyymm.
#---------------------------------------------------------------------------
def get_extract_files(data_dirs, model, sdt_yyyymm, edt_yyyymm, snowrad):
    files_out = []
    for d in range(len(data_dirs)):
        path_c = data_dirs[d] + '/' + model + '/extract/*.nc'
        files = glob.glob(path_c)
        
        for f in range(len(files)):

            if snowrad == 0:
                if 'snowrad' in files[f]:
                    continue
            else:
                if 'snowrad' not in files[f]:
                    continue

            dt_c = re.findall(r'\.(\d{10})\.', files[f])
            if (dt_c):
                dt_c = dt_c[0]
                yyyymm_c = dt_c[0:6]
                if (yyyymm_c >= sdt_yyyymm and yyyymm_c <= edt_yyyymm):
                    files_out.append(files[f])
    if (len(files_out) == 0):
        sys.exit('get_extract_files:  no files found!')
    return list(sorted(set(files_out)))

#---------------------------------------------------------------------------
# Get seasonal stats.
#---------------------------------------------------------------------------
def get_seasonal_files(files):

    springs = defaultdict(list)
    summers = defaultdict(list)
    falls   = defaultdict(list)
    winters = defaultdict(list)
    annuals = defaultdict(list)
    spring_ndays = {}
    summer_ndays = {}
    fall_ndays   = {}
    winter_ndays = {}
    annual_ndays = {}    
    years_all = []

    #--- Loop over all files, binning them into seasons / years.
    for f in range(len(files)):
        dt_c = re.findall(r'\.(\d{10})\.', files[f])[0]
        if (dt_c):
            yyyy = dt_c[0:4]
            mm   = dt_c[4:6]
            years_all.append(yyyy)
            if (mm == '03' or mm == '04' or mm == '05'):
                if ((yyyy) in spring_ndays):
                    spring_ndays[yyyy] += 1
                else:
                    spring_ndays[yyyy] = 1
                springs[yyyy].append(files[f])
            elif (mm == '06' or mm == '07' or mm == '08'):
                if ((yyyy) in summer_ndays):
                    summer_ndays[yyyy] += 1
                else:
                    summer_ndays[yyyy] = 1
                summers[yyyy].append(files[f])
            elif (mm == '09' or mm == '10' or mm == '11'):
                if ((yyyy) in fall_ndays):
                    fall_ndays[yyyy] += 1
                else:
                    fall_ndays[yyyy] = 1
                falls[yyyy].append(files[f])
            elif (mm == '12'):
                yyyyw = str(int(yyyy) + 1)
                if ((yyyyw) in winter_ndays):
                    winter_ndays[yyyyw] += 1
                else:
                    winter_ndays[yyyyw] = 1
                winters[yyyyw].append(files[f])
            elif (mm == '01' or mm == '02'):
                if ((yyyy) in winter_ndays):
                    winter_ndays[yyyy] += 1
                else:
                    winter_ndays[yyyy] = 1
                winters[yyyy].append(files[f])

            if ((yyyy) in annual_ndays):
                annual_ndays[yyyy] += 1
            else:
                annual_ndays[yyyy] = 1
            annuals[yyyy].append(files[f])

    #--- Get list of unique, sorted years found above.
    years = list(sorted(set(years_all)))

    #--- Check number of data points per unique years and nan out
    #--- ones that do not have enough (< min_season_days).
    for y in range(len(years)):
        yyyy = years[y]
        if (yyyy in spring_ndays):
            if (spring_ndays[yyyy] < 3):
                springs[yyyy] = np.nan
        else:
            springs[yyyy] = np.nan
        if (yyyy in summer_ndays):
            if (summer_ndays[yyyy] < 3):
                summers[yyyy] = np.nan
        else:
            summers[yyyy] = np.nan
        if (yyyy in fall_ndays):            
            if (fall_ndays[yyyy] < 3):
                falls[yyyy] = np.nan
        else:
            falls[yyyy] = np.nan
        if (yyyy in winter_ndays):            
            if (winter_ndays[yyyy] < 3):
                winters[yyyy] = np.nan
        else:
            winters[yyyy] = np.nan
        if (yyyy in annual_ndays):            
            if (annual_ndays[yyyy] < 3):
                annuals[yyyy] = np.nan
        else:
            annuals[yyyy] = np.nan

    return springs, summers, falls, winters, annuals, years

#---------------------------------------------------------------------------
# Get seasonal files (new)
#---------------------------------------------------------------------------
def get_seasonal_files_new(files):

    seafiles = defaultdict(list)
    ndays = {}
    years_all = []

    #--- Loop over all files, binning them into seasons / years.
    for f in range(len(files)):
        dt_c = re.findall(r'\.(\d{10})\.', files[f])[0]
        if (dt_c):
            yyyy = dt_c[0:4]
            mm   = dt_c[4:6]

            if mm == '12':
                yyyy = str(int(yyyy) + 1)
            else:
                yyyy = yyyy
            years_all.append(yyyy)

            if (mm == '03' or mm == '04' or mm == '05'):
                season = 'spring'
            if (mm == '06' or mm == '07' or mm == '08'):
                season = 'summer'
            if (mm == '09' or mm == '10' or mm == '11'):
                season = 'fall'
            if (mm == '12' or mm == '01' or mm == '02'):
                season = 'winter'

            if ((yyyy) in ndays):
                ndays[season+yyyy] += 1
            else:
                ndays[season+yyyy] = 1
            seafiles[season,yyyy].append(files[f])

    #--- Get list of unique, sorted years found above.
    years = list(sorted(set(years_all)))

    return seafiles, years

#---------------------------------------------------------------------------
# Loop over each models, grabbing data from extract wrfout files.
#---------------------------------------------------------------------------
def load_extract_data(geo_em, stns, latpts, lonpts, elevs, models, \
                      data_dirs, sdt, edt, varsin, pickle_dir, load_pickle):

    #--- Create temporary list of variables, removing 'T2MEAN' if in original
    #--- list (we'll calculate T2MEAN if necessary after loading data).
    vars_tmp = list(varsin)
    if 'T2MEAN' in vars_tmp:
        i = vars_tmp.index('T2MEAN')
        del vars_tmp[i]

    #--- Load geo em file to get lat/lon/elev grids.  Also interpolate
    #--- model elevation to stns lat/lon point locations.
    print 'Loading:\n', geo_em
    (lat, lon, hgt) = load_geo_em(geo_em)
    elevs_mod  = []
    elevs_diff = []    
    for ns in range(len(stns)):
        dat_interp = bilinear_interpolate(lat, lon, hgt, \
                                          latpts[ns], lonpts[ns], 0)
        elevs_mod.append(dat_interp)
        elevs_diff.append(elevs[ns] - dat_interp)

    for m in range(len(models)):
        data_all = {}
        dts_all = []
        model = models[m]
        pickle_file = get_mod_pkl_name(pickle_dir, model, sdt, edt)

        #--- Get list of extracted files for this model.
        print 'model = ', model
        files = get_extract_files(data_dirs, model, sdt, edt, 0)
        files = list(sorted(set(files)))

        print 'Loading: '
        #--- Loop over all extract files, reading them in.
        for f in range(len(files)):
    
            (vardat, ntimes, dt) = load_wrfout(files[f], vars_tmp)
            print files[f], ntimes
                
            #--- Loop over each vars_tmp, interpolating data to stns for
            #--- each ntimes.
            for v in range(len(vars_tmp)):
    
                var = vars_tmp[v]
                vardat_c = vardat[var]
                    
                #--- Loop over each ntime, grabbing interpolated data for
                #--- variable var for each stns.
                for n in range(ntimes):
                    dt_c = time_increment(dt, n, dtfmt)
                    for ns in range(len(stns)):
                        dat_interp = bilinear_interpolate(lat, lon, \
                                                          vardat_c[:,:,n],\
                                                          latpts[ns], \
                                                          lonpts[ns], 0)
    
                        #--- Do lapse rate correction for temperature.
                        if (var == 'T2MAX' or var == 'T2MIN'):
                            dat_c = dat_interp - std_lapse * elevs_diff[ns]
                        else:
                            dat_c = dat_interp
    
                        data_all[var, stns[ns], dt_c] = dat_c
                        dts_all.append(dt_c)
    
        #--- Get sorted unique list of all dates seen.
        dts_unique = list(sorted(set(dts_all)))

        #--- If T2MEAN was requested, calculate it from T2MAX and T2MIN for
        #--- every station and date.
        if 'T2MEAN' in varsin:
            for ns in range(len(stns)):
                for d in range(len(dts_unique)):
                    keymax = ('T2MAX', stns[ns], dts_unique[d])
                    keymin = ('T2MIN', stns[ns], dts_unique[d])
                    if keymax in data_all and keymin in data_all:
                        keymean = ('T2MEAN', stns[ns], dts_unique[d])

#                        if data_all[keymax] < (273-60) or \
#                           data_all[keymax] > (273+60) or \
#                           data_all[keymin] < (273-60) or \
#                           data_all[keymin] > (273+60):
#                            print 'stns[ns] = ', stns[ns]
#                            print 'dts_unique[d] = ', dts_unique[d]
#                            print 'max = ', data_all[keymax]
#                            print 'min = ', data_all[keymin]
##                            sys.exit()
                        
                        data_all[keymean] = np.mean([data_all[keymax], \
                                                     data_all[keymin]])

        #--- Create pickle file.
        pickle.dump((model, data_all, dts_unique, varsin, stns), \
                    open(pickle_file,'wb'), -1)

    return data_all, dts_unique, models, varsin, stns
<file_sep>/plot_sst_map.py
#!/usr/bin/python
import os, sys, glob, re
import numpy as np
from netCDF4 import Dataset as NetCDFFile
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, cm, shiftgrid
from collections import defaultdict
from utils_cmap import *
import pickle
from utils_date import *
from utils_wrfout import *
from matplotlib import colors as c
import numpy.ma as ma

#----------------------------------------------------------------------------
#----------------------------------------------------------------------------
model = 'ccsm4'
dt = '1999072900'
#geo_em = '/home/disk/spock/jbaars/rcm/data/geo_em.d02.nc'
geo_em = '/home/disk/riker/steed/geo_em.d02.nc.urban.water.nc'

wrfout = './wrf2d.d02.1hr.' + dt + '.nc'

gcm = '/home/disk/r2d2/steed/cmip5/rcp8.5/ccsm4/data/ncungrib/' + \
      'ncFILE:1999-07-01_00'

##----------------------------------------------------------------------------
## Read gcm.
##----------------------------------------------------------------------------
#nc = NetCDFFile(gcm, 'r')
#lat_gcm = nc.variables['lat'][:]
#lon_gcm = nc.variables['lon'][:]
#lm_gcm  = nc.variables['LANDSEA'][:,:]
#nc.close()
#
#xs, ys = np.meshgrid(lon_gcm, lat_gcm)
#
##----------------------------------------------------------------------------
## Plot it.
##----------------------------------------------------------------------------
#titlein = 'GCM Land Mask, ' + model + ', ' + dt
#plotfname = './gcm_landmask.png'
#zoom = 'Z1'
#
#ur_lat = maxlat[zoom]
#ur_lon = maxlon[zoom]
#ll_lat = minlat[zoom]
#ll_lon = minlon[zoom]
#lat_ctr = ll_lat + ((ur_lat - ll_lat) * 0.5)
#lon_ctr = ll_lon + ((ur_lon - ll_lon) * 0.5)
#
#if (zoom == 'Z1'):
#    res = 'i'
#elif (zoom == 'Z2'):
#    res = 'h'
#        
#fig = plt.figure(figsize=(width,height))
## left, bottom, width, height:
#ax = fig.add_axes([0.00,0.05,0.99,0.91])
#map = Basemap(resolution = res,projection='lcc',\
#              llcrnrlon= ll_lon, llcrnrlat=ll_lat,\
#              urcrnrlon= ur_lon, urcrnrlat= ur_lat,\
#              lat_0=lat_ctr,lon_0=lon_ctr,lat_1=(ur_lat - ll_lat))
#
##--- Get lat and lon data in map's x/y coordinates.
##x,y = map(lon_gcm, lat_gcm)
#x,y = map(xs, ys)
#
##--- Draw coastlines, country boundaries, fill continents.
#map.drawcoastlines(linewidth = maplw)
#map.drawstates(linewidth = maplw)
#map.drawcountries(linewidth = maplw)
#
##--- Draw the edge of the map projection region (the projection limb)
#map.drawmapboundary(linewidth = maplw)
##--- Draw lat/lon grid lines every 30 degrees.
#map.drawmeridians(np.arange(0, 360, 30), linewidth = maplw)
#map.drawparallels(np.arange(-90, 90, 30), linewidth = maplw)
#
#cMap = c.ListedColormap(['cornflowerblue','darkkhaki'])
#plt.pcolormesh(x, y, lm_gcm, cmap=cMap)
#
#plt.title(titlein, fontsize=titlefs, fontweight='bold')
#
##--- Save plot.
#print 'xli ', plotfname, ' &'
#plt.savefig(plotfname)
#
#plt.close()
#
#sys.exit()

#----------------------------------------------------------------------------
# Read geo_em.
#----------------------------------------------------------------------------
nc = NetCDFFile(geo_em, 'r')
lat  = nc.variables['XLAT_M'][0,:,:]
lon  = nc.variables['XLONG_M'][0,:,:]
hgt  = nc.variables['HGT_M'][0,:,:]
lm   = nc.variables['LANDMASK'][0,:,:]
luse = nc.variables['LU_INDEX'][0,:,:]

lat  = np.transpose(lat)
lon  = np.transpose(lon)
hgt  = np.transpose(hgt)
lm   = np.transpose(lm)
luse = np.transpose(luse)
nc.close()

urban = 13
water = 17

luse[luse == urban] = 100
luse[luse == water] = 101
luse[luse < 100]    = 102

#----------------------------------------------------------------------------
# Read wrfout.
#----------------------------------------------------------------------------
nc = NetCDFFile(wrfout, 'r')
sst = nc.variables['SST'][0,:,:]
sst = np.transpose(sst)
nc.close()

sst = ((sst - 273.15) * 9.0/5.0) + 32

#----------------------------------------------------------------------------
# Plot it.
#----------------------------------------------------------------------------
plotfname = './test.png'
titlein = 'Surface Temperature, Land Mask, Urban Land Use, ' + model + \
          ', ' + dt
zoom = 'Z2'

minlatZ2 = 46.5
maxlatZ2 = 49.0
minlonZ2 = -124.0
maxlonZ2 = -121.0
#minlatZ2 = 44.0
#maxlatZ2 = 47.0
#minlonZ2 = -125.0
#maxlonZ2 = -120.0

#ur_lat = maxlat[zoom]
#ur_lon = maxlon[zoom]
#ll_lat = minlat[zoom]
#ll_lon = minlon[zoom]
ur_lat = maxlatZ2
ur_lon = maxlonZ2
ll_lat = minlatZ2
ll_lon = minlonZ2
lat_ctr = ll_lat + ((ur_lat - ll_lat) * 0.5)
lon_ctr = ll_lon + ((ur_lon - ll_lon) * 0.5)

if (zoom == 'Z1'):
    res = 'i'
elif (zoom == 'Z2'):
    res = 'h'
        
fig = plt.figure(figsize=(width,height))
# left, bottom, width, height:
ax = fig.add_axes([0.00,0.05,0.99,0.91])
map = Basemap(resolution = res,projection='lcc',\
              llcrnrlon= ll_lon, llcrnrlat=ll_lat,\
              urcrnrlon= ur_lon, urcrnrlat= ur_lat,\
              lat_0=lat_ctr,lon_0=lon_ctr,lat_1=(ur_lat - ll_lat))

#--- Get lat and lon data in map's x/y coordinates.
x,y = map(lon, lat)

#--- Draw coastlines, country boundaries, fill continents.
map.drawcoastlines(linewidth = maplw)
map.drawstates(linewidth = maplw)
map.drawcountries(linewidth = maplw)

#--- Draw the edge of the map projection region (the projection limb)
map.drawmapboundary(linewidth = maplw)
#--- Draw lat/lon grid lines every 30 degrees.
map.drawmeridians(np.arange(0, 360, 30), linewidth = maplw)
map.drawparallels(np.arange(-90, 90, 30), linewidth = maplw)

cMap = c.ListedColormap(['darkgray', 'royalblue','darkkhaki'])
CS = plt.contour(x, y, sst, [52,56,60,64,68,72,76,80], color = 'white')
plt.clabel(CS, inline=1, fontsize=10, fmt='%2.0f')
plt.pcolormesh(x, y, luse, cmap=cMap)

#--- Plot station locations.
stn_plot = ['KPDX', 'KSEA']
stn_lats = [45.59083, 47.44472]
stn_lons = [-122.60028, -122.31361]
for sp in range(len(stn_plot)):
    xs, ys = map(stn_lons[sp], stn_lats[sp])
    plt.plot(xs, ys, 'rs', markersize = 5, markeredgewidth = 1.1)
    plt.text(xs, ys, stn_plot[sp])

plt.title(titlein, fontsize=titlefs, fontweight='bold')

#--- Save plot.
print 'xli ', plotfname, ' &'
plt.savefig(plotfname)
#plt.show()

plt.close()
<file_sep>/plot_rcm_stns_cei.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re
import numpy as np
from utils_wrfout import *
from utils_load_data import *
from utils_plot import *
from utils_date import *
from utils_ghcnd_obs import *
from utils_cei import *
import cPickle as pickle  #-- cPickle is faster, but deprecated in python 3.0
import csv

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
#plot_dir   = rcm_dir + '/plots'
plot_dir   = rcm_dir + '/plots/new'
pickle_dir = rcm_dir + '/pickle'
data_dir   = rcm_dir + '/data'
ghcnd_dir  = data_dir + '/ghcnd/ghcnd_all'
data_dirs  = ['/home/disk/r2d2/steed/cmip5/rcp8.5', \
              '/home/disk/vader/steed/cmip5/rcp8.5', \
              '/home/disk/jabba/steed/cmip5/rcp8.5']

geo_em = data_dir + '/geo_em.d02.nc'
station_file = data_dir + '/station_file.txt'

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
#vars_mod =['PREC', 'T2MAX', 'T2MIN', 'SPDUV10MAX', 'SPDUV10MEAN', 'SPDUV10STD']
vars_mod = ['PREC', 'T2MAX', 'T2MIN', 'T2MEAN']

sdt = '197001'
#edt = '209912'
edt = '197912'

#--- GHCND data downloaded through June 2019, so 2019 is a partial year, so
#--- only include obs through 2018.
#edt_obs = '201812'
edt_obs = '197912' 

var_obs_dict = {
    'PREC': 'PRCP',
    'T2MAX': 'TMAX',
    'T2MIN': 'TMIN',
    'T2MEAN': 'T2MEAN'    
    }
stats_dict = {
    'PREC': ['max', 'tot'],
    'T2MAX': ['max', 'avg'],
    'T2MIN': ['min', 'max', 'avg'],
    'T2MEAN': ['min', 'max', 'avg'],    
    }

#models = ['access1.0', 'access1.3', 'bcc-csm1.1', 'canesm2', \
#          'ccsm4', 'csiro-mk3.6.0', 'fgoals-g2', 'gfdl-cm3', \
#          'giss-e2-h', 'miroc5', 'mri-cgcm3', 'noresm1-m']
models = ['access1.0', 'access1.3']

mod_cols = ['indigo', 'blue', 'deepskyblue', \
            'darkgreen', 'lime', 'yellow', \
            'magenta', 'red', 'salmon', 'gray', 'darkgray', 'lightblue']

#---------------------------------------------------------------------------
# Load stations file.
#---------------------------------------------------------------------------
stns   = []
latpts = []
lonpts = []
elevs  = []
with open(station_file) as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        stns.append(row[0])
        latpts.append(float(row[1]))
        lonpts.append(float(row[2]))
        elevs.append(float(row[3]))

##---------------------------------------------------------------------------
## Load GHCND obs.
##---------------------------------------------------------------------------
#vars_obs = []
#for var in vars_mod:
#    vars_obs.append(var_obs_dict[var])
#
#pf = pickle_dir + '/obs_daily_test.pkl'
#if os.path.isfile(pf):
#    print 'Loading ', pf
#    (obs_all, obs_dts_all, stns, latpts, lonpts, elevs) = \
#               pickle.load(open(pf, 'rb'))
#else:
#    (obs_all, obs_dts_all) = read_ghcnd(stns, vars_obs, ghcnd_dir, sdt, edt_obs)
#    print 'Creating ', pf
#    pickle.dump((obs_all, obs_dts_all, stns, latpts, lonpts, elevs), \
#                open(pf,'wb'), -1)
#
#---------------------------------------------------------------------------
# Load model data.
#---------------------------------------------------------------------------
#pf_all = pickle_dir + '/mod_daily.pkl'
pf_all = 'junk'
if os.path.isfile(pf_all):
    print 'Loading ', pf_all
    (mod_all, mod_dts_all) = pickle.load(open(pf_all, 'rb'))
else:
    mod_all = {}
    mod_dts_all = []
    for m in range(len(models)):
        model = models[m]
        pf = get_mod_pkl_name(pickle_dir, model, sdt, edt)
        print 'Loading ', pf
        (model, data_c, dts_unique, vars, stns) = pickle.load(open(pf, 'rb'))
        mod_all[model] = data_c
        mod_dts_all = mod_dts_all + dts_unique
#    print 'Creating ', pf_all
#    pickle.dump((mod_all, mod_dts_all), open(pf_all,'wb'), -1)

#---------------------------------------------------------------------------
# 
#---------------------------------------------------------------------------
pf = pickle_dir + '/cei_daily_perc_mxmn.pkl'
if os.path.isfile(pf):
    (daily_perc_mxmn) = pickle.load(open(pf, 'rb'))
else:
    daily_perc_mxmn = get_daily_perc_mxmn(models, stns, mod_all, sdt)
    print 'Creating ', pf
    pickle.dump((daily_perc_mxmn), open(pf,'wb'), -1)

test = get_monthly_mxmn(daily_perc_mxmn, mod_all, models, stns, sdt)

sys.exit()
<file_sep>/igtg_check_utils.py
#!/usr/bin/python
import  os, os.path, time, glob, re, math
import sys, string, readline, h5py
from Scientific.IO.NetCDF import *
import numpy as np
#from datetime import datetime, timedelta, time
from datetime import datetime
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, cm, shiftgrid
#from rw_plotsettings import *
import pickle

colorbar_lab = 'm/s';
maplw = 0.8
#levs_spd = [ 0.0, 2.5, 5.0, 7.5, 10.0, 12.5, 15.0, 17.5, 20.0, 22.5,
#             25.0, 27.5, 30.0, 32.5, 35.0, 37.5 ]
levs_spd = [ -24, -21, -18, -15, -12, -9, -6, -3, 3, 6, 9, 12, 15, 18, \
             21, 24]
cmap_spd = [(255,255,255), \
            (192,192,192), \
            (128,128,128 ), \
            (0,255,255), \
            (32,178,170), \
            (0,255,0), \
            (0,128,0), \
            (255,0,204), \
            (199,21,133), \
            (0,0,255), \
            (0,0,128), \
            (255,255,0), \
            (255,204,17), \
            (255,69,0), \
            (0,0,0),\
            (255,255,255)]
levs_tos = [ 264, 266, 268, 270, 272, 274, 276, 278, 280, 282, 284, 286, \
             288, 290, 292, 294]
            
cmap_tos = [(255,255,255), \
            (192,192,192), \
            (128,128,128 ), \
            (0,255,255), \
            (32,178,170), \
            (0,255,0), \
            (0,128,0), \
            (255,0,204), \
            (199,21,133), \
            (0,0,255), \
            (0,0,128), \
            (255,255,0), \
            (255,204,17), \
            (255,69,0), \
            (0,0,0),\
            (255,255,255)]

#---------------------------------------------------------------------------
# Load 2-D lat/lon netcdf data file.
#---------------------------------------------------------------------------
def load_nc(filein, varname, itime, ilev):

    print 'opening ', filein

    f = NetCDFFile(filein, 'r')
    ll_shape = f.variables['lat'].shape
    data_all = f.variables[varname]
    fillval = data_all._FillValue[0]
    
    ndims = len(f.dimensions)
    dims  = f.dimensions
    dimnames = f.dimensions.keys()

    for d in range(ndims):
        print d, dimnames[d], dims[dimnames[d]]
        #--- Find ny dimension.
        if (dimnames[d] == 'lat'):
            ny = dims[dimnames[d]]
        elif (dimnames[d] == 'j'):
            ny = dims[dimnames[d]]
        #--- Find nx dimension.            
        if (dimnames[d] == 'lon'):
            nx = dims[dimnames[d]]
        elif (dimnames[d] == 'i'):
            nx = dims[dimnames[d]]

    #--- Fill in 2-D lat / lon terr arrays, if needed (if read-in
    #--- lat/lon are 1-D).
    if (len(ll_shape) == 1):
        lon_tmp = f.variables['lon'][:]
        lat_tmp = f.variables['lat'][:]
        lonout = np.empty([nx,ny])
        latout = np.empty([nx,ny])
        for y in range(ny):
            latout[:,y] = lat_tmp[y]
        for x in range(nx):
            lonout[x,:] = lon_tmp[x]
    elif (len(ll_shape) == 2):
        lonout = f.variables['lon'][:,:]
        latout = f.variables['lat'][:,:]
        lonout = np.transpose(lonout)
        latout = np.transpose(latout)        
        
    #--- Convert from 0-to-360 longitudes to -180-to-180.
    rows,cols = np.where(lonout >= 180.0)
    lonout[rows,cols] = lonout[rows,cols] - 360.0 

    #--- If data has 3 dimensions, assume it's (nx,ny,time).
    if (len(data_all.shape) == 3):
        data_tmp = f.variables[varname][itime,:,:]
    #--- If data has 4 dimensions, assume it's (nx,ny,time,levs).
    elif (len(data_all.shape) == 4):
        data_tmp = f.variables[varname][itime,ilev,:,:]        
    
    f.close()

    #--- transpose data array to be x/y (lon/lat).
    dataout = np.transpose(data_tmp)
    dataout[dataout == fillval] = np.NAN

#    print 'latout.shape = ', latout.shape
#    print 'lonout.shape = ', lonout.shape
#    print 'dataout.shape = ', dataout.shape
#
#    sys.exit()

    return latout, lonout, dataout

##---------------------------------------------------------------------------
## Load netcdf data file.
##---------------------------------------------------------------------------
#def load_nc(filein, varname, itime, ilev):
#    file = NetCDFFile(filein, 'r')
#    lat_tmp  = file.variables['lat'][:]
#    lon_tmp  = file.variables['lon'][:]
#    data_all = file.variables[varname]
#    fillval = data_all._FillValue[0]
#    data_tmp = file.variables[varname][itime,ilev,:,:]
#    file.close()
#
#    #--- Convert from 0-to-360 longitudes to -180-to-180.
#    ipos = np.where(lon_tmp >= 180.0)
#    iwest = ipos[0]
#    lon_tmp[iwest] = lon_tmp[iwest] - 360.0
#
#    #--- transpose data array to be x/y (lon/lat).
#    dataout = np.transpose(data_tmp)
#    dataout[dataout == fillval] = np.NAN
#
#    #--- Fill in 2-D lat / lon terr arrays.
#    nx = len(lon_tmp)
#    ny = len(lat_tmp)
#
#    lonout = np.empty([nx,ny])
#    latout = np.empty([nx,ny])
#    for y in range(ny):
#        latout[:,y] = lat_tmp[y]
#    for x in range(nx):
#        lonout[x,:] = lon_tmp[x]
#
#    return latout, lonout, dataout

#---------------------------------------------------------------------------
# Mapper.
#---------------------------------------------------------------------------
def mapper(lat, lon, grid, minlat, maxlat, minlon, maxlon, \
           levs, cmap, maplw, colorbar_lab, title, plotfname):

    ur_lat = maxlat
    ur_lon = maxlon
    ll_lat = minlat
    ll_lon = minlon
    lat_ctr = ll_lat + ((ur_lat - ll_lat) * 0.5)
    lon_ctr = ll_lon + ((ur_lon - ll_lon) * 0.5)

    width   = 6.5
    height  = 6.5
    fs      = 9
    titlefs = 9

    fig = plt.figure(figsize=(width,height))
    # left, bottom, width, height:
    ax = fig.add_axes([0.00,0.05,0.99,0.91])

    #---
#    map = Basemap(projection='ortho',lat_0=45,lon_0=-100,resolution='l')
    map = Basemap(resolution='i',projection='lcc',\
                  llcrnrlon= ll_lon, llcrnrlat=ll_lat,\
                  urcrnrlon= ur_lon, urcrnrlat= ur_lat,\
                  lat_0=lat_ctr,lon_0=lon_ctr,lat_1=(ur_lat - ll_lat))

    #--- Get lat and lon data in map's x/y coordinates.
    x,y = map(lon, lat)

    #--- Draw coastlines, country boundaries, fill continents.
    map.drawcoastlines(linewidth = maplw)
    map.drawstates(linewidth = maplw)
    map.drawcountries(linewidth = maplw)

    #--- Draw the edge of the map projection region (the projection limb)
    map.drawmapboundary(linewidth = maplw)

    #--- Draw lat/lon grid lines every 30 degrees.
    map.drawmeridians(np.arange(0, 360, 30), linewidth = maplw)
    map.drawparallels(np.arange(-90, 90, 30), linewidth = maplw)
        
    cs = plt.contourf(x, y, grid, levs, cmap=cmap)

    cbar = map.colorbar(cs, location='bottom', pad="3%", size=0.1, ticks=levs)
    cbar.set_label(colorbar_lab, fontsize=fs, size=fs-1)
    cbar.ax.tick_params(labelsize=fs-1)

    plt.title(title, fontsize=titlefs, fontweight='bold')

    #--- Save plot.
    print 'creating ', plotfname
    plt.savefig(plotfname)

    plt.close()

<file_sep>/old/old_old/utils_wrfout_20180117.py
#!/usr/bin/python
import os, sys, glob, re
import numpy as np
from netCDF4 import Dataset as NetCDFFile
import matplotlib.pyplot as plt

#from datetime import date, timedelta, datetime
#import time, stat
#from collections import defaultdict
#from utils_date import *
#from mpl_toolkits.basemap import Basemap, cm


mm2in = 0.0393700787
min_season_days = 85

fs      = 9
titlefs = 9
width   = 10
height  = 6

#---------------------------------------------------------------------------
# Load geo_em file.
#---------------------------------------------------------------------------
def load_geo_em(geo_em):
    nc = NetCDFFile(geo_em, 'r')
    lat = nc.variables['XLAT_M'][0,:,:]
    lon = nc.variables['XLONG_M'][0,:,:]
    lat = np.transpose(lat)
    lon = np.transpose(lon)
    nc.close()
    return lat, lon

#---------------------------------------------------------------------------
# Load wrfout file.
#---------------------------------------------------------------------------
def load_wrfout(wrfout, var):
    nc = NetCDFFile(wrfout, 'r')
    ntimes = len(nc.dimensions['Time'])
    dataout = nc.variables[var][:,:,:]
    dataout = np.transpose(dataout)

    #--- Get date from global attributes and clean it up and trim it down.
    dt_all = nc.START_DATE
    dt = dt_all.replace('-', '')
    dt = dt.replace(':', '')
    dt = dt.replace('_', '')        
    dt = dt[0:10]
    nc.close()
    return dataout, ntimes, dt

#---------------------------------------------------------------------------
# Get list of extracted wrfout files between sdt_yyyymm and edt_yyyymm.
#---------------------------------------------------------------------------
def get_extract_files(data_dir, model, sdt_yyyymm, edt_yyyymm):
    files_out = []
    files = glob.glob(data_dir + '/' + model + '*' + '.nc')
    for f in range(len(files)):
        dt_c = re.findall(r'\.(\d{10})\.', files[f])
        if (dt_c):
            dt_c = dt_c[0]
            yyyymm_c = dt_c[0:6]
            if (yyyymm_c >= sdt_yyyymm and yyyymm_c <= edt_yyyymm):
                files_out.append(files[f])

    return files_out

#---------------------------------------------------------------------------
# Get seasonal totals.
#---------------------------------------------------------------------------
def get_seasonal_totals(data_all, dts_all, models, stns):

    springs = {}
    summers = {}
    falls   = {}
    winters = {}
    years_all = []
    
    for s in range(len(stns)):
        stn = stns[s]
        for m in range(len(models)):
            mod = models[m]

            spring_ndays = {}
            summer_ndays = {}
            fall_ndays   = {}
            winter_ndays = {}
            years_mod_stn = []
            
            for d in range(len(dts_all)):
                yyyy = dts_all[d][0:4]
                mm = dts_all[d][4:6]
                years_mod_stn.append(yyyy)

                #--- Spring.
                if (mm == '03' or mm == '04' or mm == '05'):

                    #--- Count number of days for this year's spring.
                    if ((yyyy) in spring_ndays):
                        spring_ndays[yyyy] += 1
                    else:
                        spring_ndays[yyyy] = 1

                    if ((mod,stn,yyyy) in springs):
                        springs[mod,stn,yyyy] = springs[mod,stn,yyyy] + \
                                                data_all[mod,stn,dts_all[d]]
                    else:
                        springs[mod,stn,yyyy] = data_all[mod,stn,dts_all[d]]
                        
                #--- Summer.
                if (mm == '06' or mm == '07' or mm == '08'):
                    #--- Count number of days for this year's summer.
                    if ((yyyy) in summer_ndays):
                        summer_ndays[yyyy] += 1
                    else:
                        summer_ndays[yyyy] = 1
                    if ((mod,stn,yyyy) in summers):
                        summers[mod,stn,yyyy] = summers[mod,stn,yyyy] + \
                                                data_all[mod,stn,dts_all[d]]
                    else:
                        summers[mod,stn,yyyy] = data_all[mod,stn,dts_all[d]]

                #--- Fall.
                if (mm == '09' or mm == '10' or mm == '11'):
                    #--- Count number of days for this year's fall.
                    if ((yyyy) in fall_ndays):
                        fall_ndays[yyyy] += 1
                    else:
                        fall_ndays[yyyy] = 1
                    if ((mod,stn,yyyy) in falls):
                        falls[mod,stn,yyyy] = falls[mod,stn,yyyy] + \
                                              data_all[mod,stn,dts_all[d]]
                    else:
                        falls[mod,stn,yyyy] = data_all[mod,stn,dts_all[d]]

                #--- Winter, December only.  Put December winter data into
                #--- next year's winter-- naming winters by their Jan/Feb
                #--- year rather than their Dec year.
                if (mm == '12'):
                    yyyyw = str(int(yyyy) + 1)
                    #--- Count number of days for this year's winter.
                    if ((yyyyw) in winter_ndays):
                        winter_ndays[yyyyw] += 1
                    else:
                        winter_ndays[yyyyw] = 1
                    if ((mod,stn,yyyyw) in winters):
                        winters[mod,stn,yyyyw] = winters[mod,stn,yyyyw] + \
                                                 data_all[mod,stn,dts_all[d]]
                    else:
                        winters[mod,stn,yyyyw] = data_all[mod,stn,dts_all[d]]
                #--- Winter, Jan & Feb.
                if (mm == '01' or mm == '02'):
                    #--- Count number of days for this year's winter.
                    if ((yyyy) in winter_ndays):
                        winter_ndays[yyyy] += 1
                    else:
                        winter_ndays[yyyy] = 1
                    if ((mod,stn,yyyy) in winters):
                        winters[mod,stn,yyyy] = winters[mod,stn,yyyy] + \
                                                data_all[mod,stn,dts_all[d]]
                    else:
                        winters[mod,stn,yyyy] = data_all[mod,stn,dts_all[d]]

            years = list(sorted(set(years_mod_stn)))

            for y in range(len(years)):
                yyyy = years[y]
                if (spring_ndays[yyyy] < min_season_days):
                    springs[mod,stn,yyyy] = np.nan
                if (summer_ndays[yyyy] < min_season_days):
                    summers[mod,stn,yyyy] = np.nan
                if (fall_ndays[yyyy] < min_season_days):
                    falls[mod,stn,yyyy] = np.nan
                if (winter_ndays[yyyy] < min_season_days):
                    winters[mod,stn,yyyy] = np.nan

            years_all.append(years_mod_stn)

    years = list(sorted(set(years_all[0])))            
            
    return springs, summers, falls, winters, years

#---------------------------------------------------------------------------
# Make time series for seasonsal total precip for a set of stations.
#---------------------------------------------------------------------------
def ts(winters, springs, summers, falls, years, stns, mod, titlein, plotfname):

    fig, ax = plt.subplots( figsize=(10,6) )
    title1 = mod + ', ' + str(years[0]) + ' - ' + str(years[len(years)-1])

    xlabs = []
    for s in range(len(stns)):
        ptot = []
        stn = stns[s]
        print stn
        for y in range(len(years)):
            yyyy = years[y]
            ptot.append(winters[mod,stns[s],yyyy] * mm2in)
            ptot.append(springs[mod,stns[s],yyyy] * mm2in)
            ptot.append(summers[mod,stns[s],yyyy] * mm2in)
            ptot.append(falls[mod,stns[s],yyyy]   * mm2in)
            if (s == 0):
                xlabs.append('Winter ' + years[y])
                xlabs.append('Spring ' + years[y])
                xlabs.append('Summer ' + years[y])
                xlabs.append('Fall '   + years[y])            
        print stn, ptot
        plt.plot(ptot, label = stn)

    plt.ylim([0, 40.0])
    plt.xlabel('Season', fontsize=fs+1)
    plt.ylabel('Forecast Seasonal Total Precipitation (in.)', fontsize=fs+1)

    plt.xticks(range(0,len(xlabs)))
    plt.tick_params(axis='x', which='major', labelsize=fs-1)
    plt.tick_params(axis='y', which='major', labelsize=fs+1)    
    plt.title(title1, fontsize=titlefs, fontweight='bold')

    ax.set_xticklabels( xlabs, rotation=90)
    plt.tight_layout()
    plt.grid()
    plt.legend(fontsize = fs, loc = 'best')

    print 'creating ', plotfname
    plt.savefig(plotfname)
    return 1

#---------------------------------------------------------------------------
# Interpolate from a sent-in grid to a sent in lat/lon point.
#---------------------------------------------------------------------------
def bilinear_interpolate(latgrid, longrid, datagrid, latpt, lonpt, deb):

    totdiff = abs(latgrid - latpt) + abs(longrid - lonpt)
    (i,j) = np.unravel_index(totdiff.argmin(), totdiff.shape)

    #--- Get lat/lon box in which latpt,lonpt resides.
    if (latpt >= latgrid[i,j] and lonpt >= longrid[i,j]):
        iif = i
        jjf = j
    elif (latpt < latgrid[i,j] and lonpt < longrid[i,j]):
        iif = i-1
        jjf = j-1
    elif (latpt >= latgrid[i,j] and lonpt < longrid[i,j]):
        iif = i-1
        jjf = j
    elif (latpt < latgrid[i,j] and lonpt >= longrid[i,j]):
        iif = i
        jjf = j-1

    (nx, ny) = np.shape(latgrid)

    if (deb == 1):
        print 'nx, ny  = ', nx, ny
        print 'iif,jjf = ', iif,jjf
        print 'latgrid[iif+1,jjf] = ', latgrid[iif+1,jjf]
        print 'latgrid[iif,jjf]   = ', latgrid[iif,jjf]

    if (iif >= (nx-1) or jjf >= (ny-1) or iif < 0 or jjf < 0):
        return mvc

    #--- Do bilinear interpolation to latpt,lonpt.
    dlat  = latgrid[iif,jjf+1] - latgrid[iif,jjf]
    dlon  = longrid[iif+1,jjf] - longrid[iif,jjf]
    dslat = latgrid[iif,jjf+1] - latpt
    dslon = longrid[iif+1,jjf] - lonpt

    wrgt = 1 - (dslon/dlon)
    wup  = 1 - (dslat/dlat)

    vll = datagrid[iif,jjf]
    vlr = datagrid[iif,jjf+1]
    vul = datagrid[iif+1,jjf]
    vur = datagrid[iif+1,jjf+1]

    if (deb > 1):
        print 'll lat, lon, val = ', latgrid[iif,jjf], longrid[iif,jjf], vll
        print 'lr lat, lon, val = ', latgrid[iif+1,jjf], longrid[iif+1,jjf], vlr
        print 'ur lat, lon, val = ', latgrid[iif+1,jjf+1], longrid[iif+1,jjf+1],vur
        print 'ul lat, lon, val = ', latgrid[iif,jjf+1], longrid[iif,jjf+1], vul
        print 'latpt, lonpt = ', latpt, lonpt
        
        print 'vll = ', vll
        print 'vlr = ', vlr
        print 'vul = ', vul
        print 'vur = ', vur

    datout = (1-wrgt) * ((1-wup) * vll + wup * vul) + \
        (wrgt) * ((1-wup) * vlr + wup * vur)

#    if (deb == 1):
#        print 'datout = ', datout
#        sys.exit()
#    if (datout == 0.0):
#        print 'nx, ny  = ', nx, ny
#        print 'iif,jjf = ', iif,jjf
#        print 'latgrid[iif+1,jjf] = ', latgrid[iif+1,jjf]
#        print 'latgrid[iif,jjf]   = ', latgrid[iif,jjf]
#        
#        print 'll lat, lon, val = ', latgrid[iif,jjf], longrid[iif,jjf], vll
#        print 'lr lat, lon, val = ', latgrid[iif+1,jjf], longrid[iif+1,jjf], vl#r
#        print 'ur lat, lon, val = ', latgrid[iif+1,jjf+1], longrid[iif+1,jjf+1],vur
#        print 'ul lat, lon, val = ', latgrid[iif,jjf+1], longrid[iif,jjf+1], vu#l
#        print 'latpt, lonpt = ', latpt, lonpt
#        
#        print 'vll = ', vll
#        print 'vlr = ', vlr
#        print 'vul = ', vul
#        print 'vur = ', vur
#
#        sys.exit()


    return datout
<file_sep>/plot_wrfout_stns.py~
#!/usr/bin/python
import  sys, os, os.path, time, glob, re
import numpy as np
from utils_wrfout import *
from utils_date import *
from utils_ghcnd_obs import *
import cPickle as pickle  #-- cPickle is faster, but deprecated in python 3.0
import csv

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
#plot_dir   = rcm_dir + '/plots'
plot_dir   = rcm_dir + '/plots/new'
pickle_dir = rcm_dir + '/pickle'
data_dir   = rcm_dir + '/data'
obs_dir    = rcm_dir + '/obs'
ghcnd_dir  = obs_dir + '/ghcnd_all'
data_dirs  = ['/home/disk/r2d2/steed/cmip5/rcp8.5', \
              '/home/disk/vader/steed/cmip5/rcp8.5', \
              '/home/disk/jabba/steed/cmip5/rcp8.5']

geo_em = data_dir + '/geo_em.d02.nc'
station_file = obs_dir + '/station_file.txt'

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
#vars_mod =['PREC', 'T2MAX', 'T2MIN', 'SPDUV10MAX', 'SPDUV10MEAN', 'SPDUV10STD']
vars_mod = ['PREC', 'T2MAX', 'T2MIN', 'T2MEAN']

sdt = '197001'
edt = '209912'

#--- GHCND data downloaded through June 2019, so 2019 is a partial year, so
#--- only include obs through 2018.
edt_obs = '201812' 

var_obs_dict = {
    'PREC': 'PRCP',
    'T2MAX': 'TMAX',
    'T2MIN': 'TMIN',
    'T2MEAN': 'T2MEAN'    
    }
stats_dict = {
    'PREC': ['max', 'tot'],
    'T2MAX': ['max', 'avg'],
    'T2MIN': ['min', 'max', 'avg'],
    'T2MEAN': ['min', 'max', 'avg'],    
    }

models = ['access1.0', 'access1.3', 'bcc-csm1.1', 'canesm2', \
          'ccsm4', 'csiro-mk3.6.0', 'fgoals-g2', 'gfdl-cm3', \
          'giss-e2-h', 'miroc5', 'mri-cgcm3', 'noresm1-m']

mod_cols = ['indigo', 'blue', 'deepskyblue', \
            'darkgreen', 'lime', 'yellow', \
            'magenta', 'red', 'salmon', 'gray', 'darkgray', 'lightblue']

#---------------------------------------------------------------------------
# Load stations file.
#---------------------------------------------------------------------------
stns   = []
latpts = []
lonpts = []
elevs  = []
with open(station_file) as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        stns.append(row[0])
        latpts.append(float(row[1]))
        lonpts.append(float(row[2]))
        elevs.append(float(row[3]))

#---------------------------------------------------------------------------
# Load GHCND obs.
#---------------------------------------------------------------------------
load_obs_all = 0
pickle_file = pickle_dir + '/obs_stats.pkl'
if load_obs_all == 0:
    #--- If not loading all obs, just load stats pickle file.
    if os.path.isfile(pickle_file):
        print 'reading pickle file ', pickle_file
        (obs,yy_obs) = pickle.load(open(pickle_file, 'rb'))
    else:
        sys.exit('cannot see ' + pickle_file)
else:
    vars_obs = []
    for var in vars_mod:
        vars_obs.append(var_obs_dict[var])

    #--- Load GHCND obs; get seasonal stats on them.
    (data_all, dts_all) = read_ghcnd(stns, vars_obs, ghcnd_dir, sdt, edt_obs)

    obs = {}
    yy_obs = {}
    for var in vars_mod:
        varo = var_obs_dict[var]
        stats = stats_dict[var]
        for stat in stats:
            (obs[varo,stat], yy_obs[varo,stat]) = get_seasonal_stats_ghcnd(\
                data_all,dts_all,stns,varo,stat)
    pickle.dump((obs,yy_obs), open(pickle_file,'wb'), -1)

#---------------------------------------------------------------------------
# Load model data.
#---------------------------------------------------------------------------
load_mod_all = 0
pf_mod_stats = pickle_dir + '/mod_stats.pkl'
if load_mod_all == 0:
    #--- If not loading all model data, just load stats pickle file.
    if os.path.isfile(pf_mod_stats):
        print 'reading pickle file ', pf_mod_stats
        (mod,yy_mod) = pickle.load(open(pf_mod_stats, 'rb'))
    else:
        sys.exit('cannot see ' + pf_mod_stats)
else:

#    (data_all, dts_unique, models, vars_all, stns) = \
#               load_extract_data(geo_em, stns, latpts, lonpts, elevs, models, \
#                                 data_dirs, sdt, edt, vars_mod, pickle_dir, 0)
    data_all = {}
    dts_all = []
    for m in range(len(models)):
        model = models[m]
        pf = get_mod_pkl_name(pickle_dir, model, sdt, edt)
        print 'loading ', pf
        (model, data_c, dts_unique, vars, stns) = pickle.load(open(pf, 'rb'))
        data_all[model] = data_c
        dts_all = dts_all + dts_unique
    mod = {}
    yy_mod = {}
    for var in vars_mod:
        stats = stats_dict[var]        
        for stat in stats:
            (mod[var,stat], yy_mod[var,stat]) = get_seasonal_stats(\
                data_all, dts_unique, models, stns, var, stat)
    pickle.dump((mod,yy_mod), open(pf_mod_stats,'wb'), -1)

#---------------------------------------------------------------------------
# 
#---------------------------------------------------------------------------
'''
var = 'T2MEAN'
stat = 'max'
statplot = mod[(var,stat)]
stn = 'KSEA'
season = 'annual'
for m in range(len(models)):
    mod = models[m]
    print '-----'
    yyyy = '2097'
    key = (season,mod,stn,yyyy)
    print mod, var, stat, stn, yyyy, season, statplot[key] 
    yyyy = '2098'
    key = (season,mod,stn,yyyy)
    print mod, var, stat, stn, yyyy, season, statplot[key] 
    yyyy = '2099'   
    key = (season,mod,stn,yyyy)
    print mod, var, stat, stn, yyyy, season, statplot[key]

#sys.exit()
'''
    
#---------------------------------------------------------------------------
# Make a time series plots of each station, variable, stat and season.
#---------------------------------------------------------------------------
#--- Do multiple models on a plot for one station.
for ss in range(len(stns)):
    stn = stns[ss]
    for v in range(len(vars_mod)):
        var = vars_mod[v]
        varo = var_obs_dict[var]
        stats = stats_dict[var]
        for st in range(len(stats)):
            stat = stats[st]
            for s in range(len(seasons)):
                season = seasons[s]
                plotfname = get_plotfname(plot_dir,stn,sdt,edt,season,var,stat)
                titlein = get_title(season,stn,var,stat,yy_mod[var,stat])
                iret = ts_mods(mod[var,stat],yy_mod[var,stat], \
                               obs[varo,stat],yy_obs[varo,stat], stn, \
                               models, titlein, plotfname, var, stat, \
                               mod_cols, [season])
#                sys.exit()
#    sys.exit()
sys.exit()

<file_sep>/old/old_old/plot_wrfout_stns_20181108.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re
import numpy as np
from make_cmap import *
from netCDF4 import Dataset as NetCDFFile
from utils_wrfout import *
from utils_date import *
from utils_ghcnd_obs import *
import pickle
import csv

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
plot_dir   = rcm_dir + '/plots'
pickle_dir = rcm_dir + '/pickle'
data_dir   = rcm_dir + '/data'
obs_dir    = rcm_dir + '/obs'
ghcnd_dir  = obs_dir + '/ghcnd_all'
data_dirs  = ['/home/disk/r2d2/steed/cmip5/rcp8.5', \
              '/home/disk/vader/steed/cmip5/rcp8.5', \
              '/home/disk/jabba/steed/cmip5/rcp8.5']

geo_em = data_dir + '/geo_em.d02.nc'
station_file = obs_dir + '/station_file.txt'

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
#vars_mod =['PREC', 'T2MAX', 'T2MIN', 'SPDUV10MAX', 'SPDUV10MEAN', 'SPDUV10STD']
vars_mod = ['PREC', 'T2MAX', 'T2MIN']

sdt = '197001'
edt = '209912'

var_obs_dict = {
    'PREC': 'PRCP',
    'T2MAX': 'TMAX',
    'T2MIN': 'TMIN'
    }
stats_dict = {
    'PREC': ['max', 'tot'],
    'T2MAX': ['max', 'avg'],
    'T2MIN': ['min', 'max', 'avg']
    }

models   = ['gfdl-cm3', 'miroc5', 'bcc-csm1.1', \
            'access1.3', 'canesm2', 'noresm1-m', \
            'ccsm4', 'csiro-mk3.6.0', 'fgoals-g2' ]
mod_cols = ['indigo', 'blue', 'deepskyblue', \
            'darkgreen', 'lime', 'yellow', \
            'magenta', 'red', 'salmon']

stns   = []
latpts = []
lonpts = []
elevs  = []
with open(station_file) as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        stns.append(row[0])
        latpts.append(float(row[1]))
        lonpts.append(float(row[2]))
        elevs.append(float(row[3]))

stn_cols = ['b', 'r', 'g', 'c', 'm', 'k', 'grey']
    
#---------------------------------------------------------------------------
# Load obs.
#---------------------------------------------------------------------------
load_obs_all = 0
pickle_file = pickle_dir + '/obs_stats.pickle'
if load_obs_all == 0:
    #--- If not loading all obs, just load stats pickle file.
    if os.path.isfile(pickle_file):
        print 'reading pickle file ', pickle_file
        (obs,yy_obs) = pickle.load(open(pickle_file, 'rb'))
    else:
        sys.exit('cannot see ' + pickle_file)
else:
    vars_obs = []
    for var in vars_mod:
        vars_obs.append(var_obs_dict[var])

    #--- Load GHCND obs; get seasonal stats on them.
    (data_all, dts_all) = read_ghcnd(stns, vars_obs, ghcnd_dir, sdt, edt)
    obs = {}
    yy_obs = {}
    for var in vars_mod:
        varo = var_obs_dict[var]
        stats = stats_dict[var]
        for stat in stats:
            (obs[varo,stat], yy_obs[varo,stat]) = get_seasonal_stats_ghcnd(\
                data_all,dts_all,stns,varo,stat)

    pickle.dump((obs,yy_obs), open(pickle_file,'wb'), -1)

#---------------------------------------------------------------------------
# Load model data.
#---------------------------------------------------------------------------
load_mod_all = 1
pickle_file = pickle_dir + '/mod_stats.pickle'

if load_mod_all == 0:
    #--- If not loading all model data, just load stats pickle file.
    if os.path.isfile(pickle_file):
        print 'reading pickle file ', pickle_file
        (mod,yy_mod) = pickle.load(open(pickle_file, 'rb'))
    else:
        sys.exit('cannot see ' + pickle_file)
else:
    pickle_file_all = pickle_dir + '/extract_' + sdt + '_' + edt + '.pickle'
    pickle_file_all = 'testjunk'
    print 'reading pickle file ', pickle_file_all
    if os.path.isfile(pickle_file_all):
        (data_all, dts_unique, models, vars, stns) = \
                   pickle.load(open(pickle_file_all, 'rb'))
        print 'done!'
    else:
        (data_all, dts_unique, models, vars_all, stns) = \
                   load_extract_data(geo_em, stns, latpts, lonpts, elevs, \
                                     models, data_dirs, sdt, edt, vars_mod, \
                                     pickle_dir, 0)
        
    mod = {}
    yy_mod = {}
    for var in vars_mod:
        stats = stats_dict[var]        
        for stat in stats:
            (mod[var,stat], yy_mod[var,stat]) = get_seasonal_stats(\
                data_all,dts_unique,models,stns,var,stat)

    pickle.dump((mod,yy_mod), open(pickle_file,'wb'), -1)

#var = 'T2MAX'
#stat = 'avg'
#key = (var,stat)
#test = mod[key]
#years = yy_mod[key]

#season = 'winter'
##model = 'bcc-csm1.1'
#model = 'gfdl-cm3'
#stn = 'KSEA'
#
#for yyyy in years:
#    key = (season,model,stn,yyyy)
#    dat_c = test[key]
#    print yyyy, dat_c


#for key in mod:
#    print key, mod[key]

sys.exit()

#---------------------------------------------------------------------------
# Get seasonal precipitation totals and make a time series plot of them.
#---------------------------------------------------------------------------
##--- Do multiple stations on a plot for one model.
#mod = 'miroc5'
#for s in range(len(seasons)):
#    season = seasons[s]
#    plotfname = plot_dir + '/' + mod + '_' + sdt + '_' + edt + '_' + season + \
#                '_' + var + '_tot.png'
#    titlein = 'Total ' + seasons_lab[season] + ' Precipitation, ' + \
#              mod.upper() + ', ' + str(years[0]) + ' - ' + \
#              str(years[len(years)-1])
#    iret = ts_stns(pcp_tot, years, stns, mod, titlein, plotfname, 'PREC', \
#                   'tot', stn_cols, [season])
#
#    plotfname = plot_dir + '/' + mod + '_' + sdt + '_' + edt + '_' + season + \
#                '_' + var + '_max.png'
#    titlein = 'Maximum 24-h ' + seasons_lab[season] + ' Precipitation, ' + \
#              mod.upper() + ', ' + str(years[0]) + ' - ' + \
#              str(years[len(years)-1])
#    iret = ts_stns(pcp_max, years, stns, mod, titlein, plotfname, 'PREC', \
#                   'max', stn_cols, [season])

#--- Do multiple models on a plot for one station.
for ss in range(len(stns)):
    stn = stns[ss]
    for v in range(len(vars_mod)):
        var = vars_mod[v]
        varo = var_obs_dict[var]
        stats = stats_dict[var]
        for st in range(len(stats)):
            stat = stats[st]
            for s in range(len(seasons)):
                season = seasons[s]
                plotfname = get_plotfname(plot_dir,stn,sdt,edt,season,var,stat)
                titlein = get_title(season,stn,var,stat,yy_mod[var,stat])
                iret = ts_mods(mod[var,stat],yy_mod[var,stat], \
                               obs[varo,stat],yy_obs[varo,stat], stn, \
                               models, titlein, plotfname, var, stat, \
                               mod_cols, [season])
#                sys.exit()

sys.exit()

#---------------------------------------------------------------------------
# Get seasonal temperature maxes and mins.
#---------------------------------------------------------------------------
#--- Do multiple stations on a plot for one model.
#mod = 'miroc5'
#for s in range(len(seasons)):
#    season = seasons[s]
#
#    #--- Max temperature.
#    var = 'T2MAX'
#    plotfname = plot_dir + '/' + mod + '_' + sdt + '_' + edt + '_' + season + \
#                '_' + var + '_max.png'
#    titlein = 'Maximum Seasonal Temperature, ' + seasons_lab[season] + ', ' +\
#              mod.upper() + ', ' + str(years[0]) + ' - ' + \
#              str(years[len(years)-1])
#    iret = ts_stns(tmx_max, years, stns, mod, titlein, plotfname, var, 'max', \
#              stn_cols, [season])
#
#    #--- Min temperature.
#    var = 'T2MIN'
#    plotfname = plot_dir + '/' + mod + '_' + sdt + '_' + edt + '_' + season + \
#                '_' + var + '_min.png'
#    titlein = 'Minimum Seasonal Temperature, ' + seasons_lab[season] + ', ' +\
#              mod.upper() + ', ' + str(years[0]) + ' - ' + \
#              str(years[len(years)-1])
#    iret = ts_stns(tmn_min, years, stns, mod, titlein, plotfname, var, 'min', \
#              stn_cols, [season])
#
#sys.exit()
#
##--- Do multiple models on a plot for one station.
#for ss in range(len(stns)):
#    stn = stns[ss]
#    for s in range(len(seasons)):
#        season = seasons[s]
#
#        #--- Max temperature.
#        var = 'T2MAX'
#        plotfname = plot_dir + '/' + stn + '_' + sdt + '_' + edt + '_' + \
#                    season + '_' + var + '_max.png'
#        titlein = 'Maximum Seasonal Temperature, ' + seasons_lab[season] + \
#                  ', ' + stn.upper() + ', ' + str(years[0]) + ' - ' + \
#                  str(years[len(years)-1])
#        iret = ts_mods(tmx_max, years, stn, models, titlein, plotfname, \
#                       var, 'max', stn_cols, [season])
#
#        #--- Min temperature.
#        var = 'T2MIN'
#        plotfname = plot_dir + '/' + stn + '_' + sdt + '_' + edt + '_' + \
#                    season + '_' + var + '_min.png'
#        titlein = 'Minimum Seasonal Temperature, ' + seasons_lab[season] + \
#                  ', ' + stn.upper() + ', ' + str(years[0]) + ' - ' + \
#                  str(years[len(years)-1])
#        iret = ts_mods(tmn_min, years, stn, models, titlein, plotfname, \
#                       var, 'min', stn_cols, [season])
#
##---------------------------------------------------------------------------
## Get seasonal temperature averages.
##---------------------------------------------------------------------------
#(tmx_avg, years) = get_seasonal_stats(data_all, dts_unique, models, stns, \
#                                      'T2MAX', 'avg')
#(tmn_avg, years) = get_seasonal_stats(data_all, dts_unique, models, stns, \
#                                      'T2MIN', 'avg')
#
##--- Do multiple stations on a plot for one model.
#mod = 'gfdl-cm3'
#for s in range(len(seasons)):
#    season = seasons[s]
#
#    #--- Max temperature.
#    var = 'T2MAX'
#    plotfname = plot_dir + '/' + mod + '_' + sdt + '_' + edt + '_' + season + \
#                '_' + var + '_avg.png'
#    titlein = 'Average Maximum Temperature, ' + seasons_lab[season] + ', ' +\
#              mod.upper() + ', ' + str(years[0]) + ' - ' + \
#              str(years[len(years)-1])
#    iret = ts_stns(tmx_avg, years, stns, mod, titlein, plotfname, var, 'avg', \
#              stn_cols, [season])
#
#    #--- Min temperature.
#    var = 'T2MIN'
#    plotfname = plot_dir + '/' + mod + '_' + sdt + '_' + edt + '_' + season + \
#                '_' + var + '_avg.png'
#    titlein = 'Average Minimum Temperature, ' + seasons_lab[season] + ', ' +\
#              mod.upper() + ', ' + str(years[0]) + ' - ' + \
#              str(years[len(years)-1])
#    iret = ts_stns(tmn_avg, years, stns, mod, titlein, plotfname, var, 'avg', \
#              stn_cols, [season])
#
#
##--- Do multiple models on a plot for one station.
#for ss in range(len(stns)):
#    stn = stns[ss]
#    for s in range(len(seasons)):
#        season = seasons[s]
#
#        #--- Max temperature.
#        var = 'T2MAX'
#        plotfname = plot_dir + '/' + stn + '_' + sdt + '_' + edt + '_' + \
#                    season + '_' + var + '_avg.png'
#        titlein = 'Average Maximum Temperature, ' + seasons_lab[season] + \
#                  ', ' + stn.upper() + ', ' + str(years[0]) + ' - ' + \
#                  str(years[len(years)-1])
#        iret = ts_mods(tmx_avg, years, stn, models, titlein, plotfname, \
#                       var, 'avg', stn_cols, [season])
#
#        #--- Min temperature.
#        var = 'T2MIN'
#        plotfname = plot_dir + '/' + stn + '_' + sdt + '_' + edt + '_' + \
#                    season + '_' + var + '_avg.png'
#        titlein = 'Average Minimum Temperature, ' + seasons_lab[season] + \
#                  ', ' + stn.upper() + ', ' + str(years[0]) + ' - ' + \
#                  str(years[len(years)-1])
#        iret = ts_mods(tmn_avg, years, stn, models, titlein, plotfname, \
#                       var, 'avg', stn_cols, [season])
#
#sys.exit()
#
##---------------------------------------------------------------------------
## Wind Speed.
##---------------------------------------------------------------------------
#mod = 'gfdl-cm3'
#var = 'SPDUV10MEAN'
#(spdavg_avg, years) = get_seasonal_stats(data_all, dts_unique, models, stns, \
#                                         var, 'avg')
#for s in range(len(seasons)):
#    season = seasons[s]
#
#    plotfname = plot_dir + '/' + mod + '_' + sdt + '_' + edt + '_' + season + \
#                '_' + var + '_avg.png'
#    titlein = 'Average Wind Speed, ' + seasons_lab[season] + ', ' +\
#              mod.upper() + ', ' + str(years[0]) + ' - ' + \
#              str(years[len(years)-1])
#    iret = ts_stns(spdavg_avg, years, stns, mod, titlein, plotfname, var, \
#                   'avg', stn_cols, [season])
#
#var = 'SPDUV10MAX'
#(spdmax_max, years) = get_seasonal_stats(data_all, dts_unique, models, stns, \
#                                         var, 'max')
#for s in range(len(seasons)):
#    season = seasons[s]
#
#    plotfname = plot_dir + '/' + mod + '_' + sdt + '_' + edt + '_' + season + \
#                '_' + var + '_max.png'
#    titlein = 'Maximum Wind Speed, ' + seasons_lab[season] + ', ' +\
#              mod.upper() + ', ' + str(years[0]) + ' - ' + \
#              str(years[len(years)-1])
#    iret = ts_stns(spdmax_max, years, stns, mod, titlein, plotfname, var, \
#                   'max', stn_cols, [season])
#
#
#sys.exit()

<file_sep>/igtg_check_miroc5_v2.py
#!/usr/bin/python
import  os, os.path, time, glob, re, math
import sys, string, readline, h5py
from Scientific.IO.NetCDF import *
import numpy as np
#from datetime import datetime, timedelta, time
from datetime import datetime
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, cm, shiftgrid
#from rw_plotsettings import *
import pickle
import csv
from numpy import genfromtxt

maplw = 0.8

plotfname = './test_pts2.png'
title = 'pts'
#ptsfile = '/home/disk/mass/jbaars/cmip5/rcp8.5/miroc5/test_unstaggertos/pts.txt'
ptsfile = '/home/disk/mass/jbaars/cmip5/rcp8.5/miroc5/test_unstaggertos/pts3.txt'

indices = []
lats = []
lons = []
f = open(ptsfile, 'rt')
try:
    reader = csv.reader(f)
    for index, lat, lon in reader:
        indices.append(float(index))
        lats.append(float(lat))
        lons.append(float(lon))
finally:
    f.close()

maxlat = max(lats) + 0.5
minlat = min(lats) - 0.5
maxlon = max(lons) + 0.5
minlon = min(lons) - 0.5

ur_lat = maxlat
ur_lon = maxlon
ll_lat = minlat
ll_lon = minlon
lat_ctr = ll_lat + ((ur_lat - ll_lat) * 0.5)
lon_ctr = ll_lon + ((ur_lon - ll_lon) * 0.5)

width   = 12
height  = 4
fs      = 9
titlefs = 9

fig = plt.figure(figsize=(width,height))
# left, bottom, width, height:
ax = fig.add_axes([0.00,0.05,0.99,0.91])

#---
map = Basemap(resolution='i',projection='lcc',\
              llcrnrlon= ll_lon, llcrnrlat=ll_lat,\
              urcrnrlon= ur_lon, urcrnrlat= ur_lat,\
              lat_0=lat_ctr,lon_0=lon_ctr,lat_1=(ur_lat - ll_lat))

#--- Draw coastlines, country boundaries, fill continents.
map.drawcoastlines(linewidth = maplw)
map.drawstates(linewidth = maplw)
map.drawcountries(linewidth = maplw)

#--- Draw the edge of the map projection region (the projection limb)
map.drawmapboundary(linewidth = maplw)

#--- Draw lat/lon grid lines every 30 degrees.
map.drawmeridians(np.arange(0, 360, 30), linewidth = maplw)
map.drawparallels(np.arange(-90, 90, 30), linewidth = maplw)

#-- Add dots for grid points for 2nd grid.
for i in range(len(lats)):

    xs, ys = map(lons[i], lats[i])

    if(indices[i] == 2):
        mark = 'gx'
        ms = 5.0
        plt.plot(xs, ys, mark, markersize = 3, mew=ms)
        xs2 = xs
        ys2 = ys
    if (indices[i] == 1):
        mark = 'ro'
        ms = 1.0
        plt.plot([xs,xs2], [ys,ys2], linewidth=2, color='g')
    else:
        mark = 'bo'
        ms = 1.0

    plt.plot(xs, ys, mark, markersize = 6, mew=ms)

#    strp = "%d,%d,%.3f" % (ii+1, jj+1, tos_new[ii+1,jj+1])
#    plt.text(xs, ys, strp, fontsize=6)

plt.title(title, fontsize=titlefs, fontweight='bold')

#--- Save plot.
print 'creating ', plotfname
plt.savefig(plotfname)

plt.close()

<file_sep>/old/old_old/igtg_check_20161103.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re
import numpy as np
from igtg_check_utils import *
import matplotlib.pyplot as plt
#import pickle
from make_cmap import *

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir  = '/home/disk/spock/jbaars/rcm'
py_dir   = rcm_dir + '/python'
plot_dir = rcm_dir + '/plots'
data_dir = rcm_dir + '/igtg.nml.works.3d.4d'

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
varname = 'ua'
file_old = data_dir + '/' + varname + '.1990010106.1991010100.nc'
file_new = data_dir + '/' + varname + '_new.nc'

itime = 1
ilev  = 1

title_old = varname + ' OLD, itime = ' + str(itime) + ', ilev = ' + \
            str(ilev)
plotfname_old = './' + varname + '_old_itime' + str(itime) + '_ilev' + \
                str(ilev) + '.png'
title_new = varname + ' NEW, itime = ' + str(itime) + ', ilev = ' + \
            str(ilev)
plotfname_new = './' + varname + '_new_itime' + str(itime) + '_ilev' + \
                str(ilev) + '.png'

minlat = 20.0
maxlat = 50.0
minlon = -120.0
maxlon = -50.0

#minlat = 20.0
#maxlat = 90.0
#minlon = -175.0
#maxlon = -85.0

#---------------------------------------------------------------------------
# Make a color map using make_cmap.
#---------------------------------------------------------------------------
levs_norm_spd = []
for i in range(len(levs_spd)):
    x = float(levs_spd[i])
    norm_c = (x - min(levs_spd)) / (max(levs_spd) - min(levs_spd))
    levs_norm_spd.append(norm_c)

my_cmap = make_cmap(cmap_spd, bit = True, position = levs_norm_spd)

#---------------------------------------------------------------------------
# Load old and new files.
#---------------------------------------------------------------------------
(lat_old, lon_old, data_old) = load_nc(file_old, varname, itime, ilev)
(lat_new, lon_new, data_new) = load_nc(file_new, varname, itime, ilev)
        
#---------------------------------------------------------------------------
# Plot old data.
#---------------------------------------------------------------------------
#mapper(lat_old, lon_old, data_old, minlat, maxlat, minlon, maxlon, \
#       levs_spd, my_cmap, maplw, colorbar_lab, title_old, plotfname_old)
#
#sys.exit()

mapper(lat_new, lon_new, data_new, minlat, maxlat, minlon, maxlon, \
       levs_spd, my_cmap, maplw, colorbar_lab, title_new, plotfname_new)

sys.exit()

<file_sep>/utils_plot.py
#!/usr/bin/python
import os, sys, glob, re, math
import numpy as np
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, cm, shiftgrid
from utils_cmap import *
from utils_date import *

zooms = ['Z1', 'Z2']

seasons    = ['spring', 'summer', 'fall', 'winter', 'annual']
seasons_mo = ['ALL', 'MAM', 'JJA', 'SON', 'DJF']
seasons_lab = {
    'annual': 'Jan-Dec',    
    'spring': 'Mar-Apr-May',
    'summer': 'Jun-Jul-Aug',
    'fall':   'Sep-Oct-Nov',
    'winter': 'Dec-Jan-Feb'
    }
var_lab = {
    'T2MAX': 'Average Max Temperature ($^\circ$C)',
    'T2MIN': 'Average Min Temperature ($^\circ$C)',
    'T2MEAN': 'Average Mean Temperature ($^\circ$C)',    
    'PREC': 'Total Precipitation (in.)',
    'SPDUV10MEAN': 'Average Wind Speed (m/s)',
    'SPDUV10MAX': 'Maximum Wind Speed (m/s)',
    'SNOW': 'Snow Water Equivalent (mm)',
    'SWDOWN': 'Shortwave Down Radiation (W/m^2)'    
    }
ylims = {
    'PRECmin':  [0, 1.0],
    'PRECmax':  [0, 5.0],    
    'PRECavg':  [0, 45.0],
    'PRECtot':  [0, 40.0],        
    'T2MAXavg': [-5, 40.0],
    'T2MAXmax': [0, 50.0],    
    'T2MINmin': [-30.0, 25.0],
    'T2MINmax': [-30.0, 25.0],
    'T2MINavg': [-20, 25.0],
    'T2MEANmin': [-30.0, 25.0],
    'T2MEANmax': [-10.0, 50.0],
    'T2MEANavg': [-10.0, 50.0],
    'SPDUV10MEANavg': [0, 5.0],
    'SPDUV10MAXmax': [0, 20.0],
    'SNOWtot': [0, 2000.0]
    }
labels = {
    'PRECtot':  'Total Precipitation (in.)',
    'PRECmax':  'Maximum Precipitation (in.)',
    'PRECmin':  'Minimum Precipitation (in.)',
    'PRECavg':  'Average Precipitation (in.)',        
    'T2MAXavg': 'Average Maximum Temperature ($^\circ$C)',
    'T2MAXmax': 'Max Maximum Temperature ($^\circ$C)',
    'T2MAXmin': 'Min Maximum Temperature ($^\circ$C)',    
    'T2MINavg': 'Average Minimum Temperature ($^\circ$C)',
    'T2MINmax': 'Max Minimum Temperature ($^\circ$C)',
    'T2MINmin': 'Min Minimum Temperature ($^\circ$C)',
    'T2MEANavg': 'Average Mean Temperature ($^\circ$C)',
    'T2MEANmax': 'Max Mean Temperature ($^\circ$C)',
    'T2MEANmin': 'Min Mean Temperature ($^\circ$C)',
    'SPDUV10MEANavg': 'Average Wind Speed (m/s)',
    'SPDUV10MAXmax': 'Maximum Wind Speed (m/s)',
    'SNOWtot': 'Snow Water Equivalent (mm)'
    }

colorbar_labs = {
    'T2MAX': 'Temperature ($^\circ$C)',
    'T2MIN': 'Temperature ($^\circ$C)',
    'T2MEAN': 'Temperature ($^\circ$C)',    
    'PREC': 'Precipitation (in.)',
    'SPDUV10MEAN': 'Wind Speed (m/s)',
    'SPDUV10MAX': 'Wind Speed (m/s)',
    'SNOW': 'Snow Water Equivalent (mm)',
    'SWDOWN': 'Shortwave Down Radiation (W/m^2)'     
    }

target_lat = 47.44472
target_lon = -122.31361
fudge_lat = 1.7
fudge_lon = 2.1
minlatZ2 = target_lat - fudge_lat
maxlatZ2 = target_lat + fudge_lat
minlonZ2 = target_lon - fudge_lon
maxlonZ2 = target_lon + fudge_lon
minlat = {
    'Z1': 39.0,
    'Z2': minlatZ2 }
maxlat = {
    'Z1': 51.0,
    'Z2': maxlatZ2 }
minlon = {
    'Z1': -131.0,
    'Z2': minlonZ2 }
maxlon = {
    'Z1': -108.0,
    'Z2': maxlonZ2 }

fs      = 9
titlefs = 9
width   = 10
height  = 8
maplw   = 1.0

#---------------------------------------------------------------------------
# Get plot file name.
#---------------------------------------------------------------------------
def get_plotfname(plot_dir, stn, sdt, edt, season, var, stat):
    plotfname = plot_dir + '/' + stn + '_' + sdt + '_' + edt + '_' + \
                season + '_' + var + '_' + stat + '.png'
    return plotfname

#---------------------------------------------------------------------------
# Get plot title.
#---------------------------------------------------------------------------
def get_title(season, stn, var, stat, years):
    titleout = station_name_dict[stn] + ' (' + stn.upper() + '), ' + \
               seasons_lab[season] + ' ' + labels[var+stat] + ', ' + \
               str(years[0]) + ' - ' + str(years[len(years)-1])
    return titleout

#---------------------------------------------------------------------------
# Make time series of sent-in data for a set of stations for one model.
#---------------------------------------------------------------------------
def ts_stns(statplot, years, stns, mod, titlein, plotfname, var, stat, \
            cols, seasonsplot):

    fig, ax = plt.subplots( figsize=(10,6) )
    xlabs = []
    for s in range(len(stns)):
        ptot = []
        stn = stns[s]
        for y in range(len(years)):
            yyyy = years[y]
            for ss in range(len(seasons)):
                season = seasons[ss]
                if (season in seasonsplot):
                    if (var == 'PREC'):
                        ptot.append(statplot[season,mod,stns[s],yyyy] * mm2in)
                    elif (var == 'T2MAX' or var == 'T2MIN' or var == 'T2MEAN'):
                        ptot.append(statplot[season,mod,stns[s],yyyy] - 273.15)
                    elif (var.find('SPD') >= 0):
                        ptot.append(statplot[season,mod,stns[s],yyyy])
                    if (s == 0):
                        xlabs.append(season.title() + ' ' + years[y])
        plt.plot(ptot, alpha = 0.3, label = stn, color = cols[s])
        plt.plot(ptot, label = stn, color = cols[s])        

    #--- y-axis labeling.
    plt.ylim(ylims[var+stat])
    plt.ylabel('Seasonal ' + labels[var+stat], fontsize=fs+1)
    plt.tick_params(axis='y', which='major', labelsize=fs+1)    

    #--- x-axis labels.
    xticks_c = range(0,len(xlabs), 4)
    xlabs_c = [ xlabs[i] for i in xticks_c ]    
    plt.xticks(xticks_c)
    plt.tick_params(axis='x', which='major', labelsize=fs-1)
    ax.set_xticklabels(xlabs_c, rotation=90)        
    plt.xlabel('Season', fontsize=fs+1)

    plt.title(titlein, fontsize=titlefs, fontweight='bold')

    plt.tight_layout()
    plt.grid()
    plt.legend(fontsize = fs-1, loc = 'best')

    print 'xli ', plotfname, ' &'
    plt.savefig(plotfname)
    plt.close()

    return 1

#---------------------------------------------------------------------------
# Make time series of sent-in data for a set of models for one station.
#---------------------------------------------------------------------------
def ts_mods(statplot, years, obsplot, years_obs, stn, models, titlein, \
            plotfname, var, stat, cols, seasonsplot):

    fig, ax = plt.subplots( figsize=(10,6) )
    lw_sm = 1.8

    #--- Plot models.
    xlabs = []
    mod_all = np.ones((len(models),len(years))) * np.nan
    for m in range(len(models)):
        ptot = []
        mod = models[m]
        
        for y in range(len(years)):
            yyyy = years[y]
            for ss in range(len(seasons)):
                season = seasons[ss]
                key = (season,mod,stn,yyyy)
                if (season in seasonsplot):
                    if (var == 'PREC'):
                        ptot_c = statplot[key] * mm2in
                    elif (var == 'T2MAX' or var == 'T2MIN' or var == 'T2MEAN'):
                        ptot_c = statplot[key] - 273.15
                    elif (var.find('SPD') >= 0):
                        ptot_c = statplot[key]
                    if (m == 0):
                        xlabs.append(years[y])

                    ptot.append(ptot_c)
            mod_all[m,y] = ptot_c
            
        plt.plot(ptot, label = mod.upper(), \
                 color = cols[m], linewidth=lw_sm, alpha = 0.23) 

    #--- Plot ensemble mean.
    plt.plot(np.nanmean(mod_all, axis=0), label = 'Ensemble Mean', \
             color = 'darkgreen', linewidth=2.2)
    
    #--- Plot observations.
    otot = []
    for y in range(len(years_obs)):
        yyyy = years_obs[y]
        for ss in range(len(seasons)):
            season = seasons[ss]
            key = (season,stn,yyyy)
            if (season in seasonsplot):
                otot.append(obsplot[key])
    plt.plot(otot, label='Observed', color='black', \
             linestyle='None', marker='o', markersize=3)

    #--- y-axis labeling.
    plt.ylim(ylims[var+stat])
    plt.ylabel('Seasonal ' + labels[var+stat], fontsize=fs+1)
    plt.tick_params(axis='y', which='major', labelsize=fs+1)    

    #--- x-axis labels.
    xticks_c = range(0,len(xlabs), 4)
    xlabs_c = [ xlabs[i] for i in xticks_c ]    
    plt.xticks(xticks_c)
    plt.tick_params(axis='x', which='major', labelsize=fs-1)
    ax.set_xticklabels(xlabs_c, rotation=90)        
    plt.xlabel('Year', fontsize=fs+1)

    plt.title(titlein, fontsize=titlefs, fontweight='bold')

    plt.tight_layout()
    plt.grid()
    plt.legend(fontsize = fs-1, loc = 'best')

    print 'xli ', plotfname, ' &'
    plt.savefig(plotfname)
    plt.close()
    
    return 1

#---------------------------------------------------------------------------
# Make time series of sent-in data for a set of models for one station.
#---------------------------------------------------------------------------
def ts_swe(swe_all, years, swe_obs, years_obs, stn, s, models, titlein, \
           plotfname, var, stat, cols):

    (nm,ns,nf) = swe_all.shape
    fig, ax = plt.subplots( figsize=(10,6) )
    lw_sm = 1.8

    #--- Plot models.
    xlabs = []
    mod_all = np.ones((len(models),len(years))) * np.nan
    for m in range(len(models)):
        mod = models[m]

        swe_plot = []
        for f in range(nf):
            swe_plot.append(swe_all[m,s,f])

        plt.plot(swe_plot, label = mod.upper(), color = cols[m], \
                           linewidth=lw_sm, alpha = 0.23)

    #--- Plot ensemble mean.
    plt.plot(np.nanmean(swe_all[:,s,:], axis=0), label = 'Ensemble Mean', \
             color = 'darkgreen', linewidth=2.2)

    #--- Plot observations.
    obs_plot      = []
    obs_yyyy_plot = []    
    for y in range(len(years)):
        if years[y] in years_obs:
            i = years_obs.index(years[y])
            obs_plot.append(swe_obs[i])
        else:
            obs_plot.append(np.nan)            
    plt.plot(obs_plot, label='Observed', color='black', \
             linestyle='None', marker='o', markersize=3)

    #--- y-axis labeling.
    plt.ylim(ylims[var+stat])
    plt.ylabel(labels[var+stat], fontsize=fs+1)
    plt.tick_params(axis='y', which='major', labelsize=fs+1)    

    #--- x-axis labels.
    xticks_c = range(0,len(years), 4)
    xlabs_c = [ years[i] for i in xticks_c ]    
    plt.xticks(xticks_c)
    plt.tick_params(axis='x', which='major', labelsize=fs-1)
    ax.set_xticklabels(xlabs_c, rotation=90)        
    plt.xlabel('Year', fontsize=fs+1)

    plt.title(titlein, fontsize=titlefs, fontweight='bold')

    plt.tight_layout()
    plt.grid()
    plt.legend(fontsize = fs-1, loc = 'best')

    print 'xli ', plotfname, ' &'
    plt.savefig(plotfname)
    plt.close()
    
    return 1

#---------------------------------------------------------------------------
# Run mapper.
#---------------------------------------------------------------------------
def run_mapper(var, model, rundir, season, files, yyyy, \
               lat, lon, levs_c, my_cmap ):

    file_plot = rundir + '/' + model + '_' + season + '_' + yyyy + '_' + \
                var + '.nc'
    if (var == 'T2MAX' or var == 'T2MIN'):
        if (os.path.exists(file_plot)):
            print 'nc file ', file_plot, ' already exists-- using it'
        else:
            syscom = 'ncra'
            for f in range(len(files[yyyy])):
                print yyyy, files[yyyy][f]
                syscom = syscom + ' ' + files[yyyy][f]
            syscom = syscom + ' ' + file_plot
            print 'syscom = ', syscom        
            os.system(syscom)
    elif (var == 'PREC'):
        if (os.path.exists(file_plot)):
            print 'nc file ', file_plot, ' already exists-- using it'
        else:
            syscom = 'ncra -y ttl'
            for f in range(len(files[yyyy])):
                print yyyy, files[yyyy][f]
                syscom = syscom + ' ' + files[yyyy][f]
            syscom = syscom + ' ' + file_plot
            print 'syscom = ', syscom        
            os.system(syscom)

    print 'Loading ', var, ' from ', file_plot
    (vardat, ntimes, dt) = load_wrfout(file_plot, [var])

    syscom = 'rm -f ' + file_plot
    os.system(syscom)
    
    titlein = model.upper() + ', ' + var_lab[var] + ', ' + \
              seasons_lab[season] + ' ' + yyyy

    plotdat = vardat[var][:,:,0]
    if (var == 'PREC'):
        plotdat = plotdat * mm2in
    else:
        plotdat = plotdat - 273.15

    for z in range(len(zooms)):
        plotfname = rundir + '/' + model + '_' + yyyy + '_' + season + '_' + \
                    var + '_' + zooms[z] + '.png'
        print 'colorbar_labs[var] = ', colorbar_labs[var]
        mapper(var, lat, lon, plotdat, levs_c, my_cmap, maplw, \
               colorbar_labs[var], titlein, plotfname, zooms[z])
    return 1

#---------------------------------------------------------------------------
# Mapper.
#---------------------------------------------------------------------------
def mapper(var, lat, lon, grid, levs, cmap_in, maplw, colorbar_lab, titlein, \
           plotfname, zoom):

    ur_lat = maxlat[zoom]
    ur_lon = maxlon[zoom]
    ll_lat = minlat[zoom]
    ll_lon = minlon[zoom]
    lat_ctr = ll_lat + ((ur_lat - ll_lat) * 0.5)
    lon_ctr = ll_lon + ((ur_lon - ll_lon) * 0.5)

    if (zoom == 'Z1'):
        res = 'i'
    elif (zoom == 'Z2'):
        res = 'h'
        
    fig = plt.figure(figsize=(width,height))
    # left, bottom, width, height:
    ax = fig.add_axes([0.00,0.05,0.99,0.91])
    map = Basemap(resolution = res,projection='lcc',\
                  llcrnrlon= ll_lon, llcrnrlat=ll_lat,\
                  urcrnrlon= ur_lon, urcrnrlat= ur_lat,\
                  lat_0=lat_ctr,lon_0=lon_ctr,lat_1=(ur_lat - ll_lat))

    #--- Get lat and lon data in map's x/y coordinates.
    x,y = map(lon, lat)

    #--- Draw coastlines, country boundaries, fill continents.
    map.drawcoastlines(linewidth = maplw)
    map.drawstates(linewidth = maplw)
    map.drawcountries(linewidth = maplw)

    #--- Draw the edge of the map projection region (the projection limb)
    map.drawmapboundary(linewidth = maplw)

    #--- Draw lat/lon grid lines every 30 degrees.
    map.drawmeridians(np.arange(0, 360, 30), linewidth = maplw)
    map.drawparallels(np.arange(-90, 90, 30), linewidth = maplw)

    cs = plt.contourf(x, y, grid, levs, cmap=cmap_in)

    cbar = map.colorbar(cs, location='bottom', pad="3%", size=0.1, ticks=levs)
    cbar.set_label(colorbar_lab, fontsize=fs, size=fs-1)
    cbar.ax.tick_params(labelsize=fs-2)

    if var != 'SNOW':
        csl = plt.contour(x, y, grid, levs, colors = 'black', linewidths=0.8)
        plt.clabel(csl, inline=1, fontsize=10, fmt='%2.1f', fontweights='bold')

    plt.title(titlein, fontsize=titlefs, fontweight='bold')

    #--- Save plot.
    print 'xli ', plotfname, ' &'
    plt.savefig(plotfname)

    plt.close()

#---------------------------------------------------------------------------
# Smoother like Matlab's running average smoother 'smooth', taken from
# https://stackoverflow.com/questions/40443020/matlabs-smooth-implementation-n-point-moving-average-in-numpy-python
#---------------------------------------------------------------------------
def smooth(a,WSZ):
    # a: NumPy 1-D array containing the data to be smoothed
    # WSZ: smoothing window size needs, which must be odd number,
    # as in the original MATLAB implementation
    out0 = np.convolve(a,np.ones(WSZ,dtype=int),'valid')/WSZ    
    r = np.arange(1,WSZ-1,2)
    start = np.cumsum(a[:WSZ-1])[::2]/r
    stop = (np.cumsum(a[:-WSZ:-1])[::2]/r)[::-1]
    return np.concatenate((  start , out0, stop  ))
<file_sep>/plot_wrfout_grid_avg_snowrad.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re, math
import numpy as np
#import matplotlib.pyplot as plt
from make_cmap import *
from netCDF4 import Dataset as NetCDFFile
from utils_wrfout import *
from utils_date import *
from utils_cmap import *
import pickle
from collections import defaultdict
from make_cmap import *

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
plot_dir   = rcm_dir + '/plots'
pickle_dir = rcm_dir + '/pickle'
run_dirs   = rcm_dir + '/rundirs'
geo_em     = rcm_dir + '/data' + '/geo_em.d02.nc'
data_dirs  = ['/home/disk/r2d2/steed/cmip5/rcp8.5', \
              '/home/disk/vader/steed/cmip5/rcp8.5', \
              '/home/disk/jabba/steed/cmip5/rcp8.5']

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
#vars = ['SNOW', 'SWDOWN']
vars = ['SNOW']

sdts = ['197001', '203001', '207001']
edts = ['200012', '206012', '209912']

models = ['access1.0', 'access1.3', 'bcc-csm1.1', 'canesm2', \
          'ccsm4', 'csiro-mk3.6.0', 'fgoals-g2', 'gfdl-cm3', \
          'giss-e2-h', 'miroc5', 'mri-cgcm3', 'noresm1-m']
#models = ['ccsm4']

#---------------------------------------------------------------------------
# Make run directory, load geo_em file.
#---------------------------------------------------------------------------
rundir = run_dirs + '/plot_wrfout_grid_avg_snowrad.' + str(os.getpid())
print 'making ', rundir
os.makedirs(rundir)

#rundir = '/home/disk/spock/jbaars/rcm/rundirs/plot_wrfout_grid_avg.4149'

print 'Loading ', geo_em
(lat, lon, hgt) = load_geo_em(geo_em)

#---------------------------------------------------------------------------
# For each model, make a maps short-wave down radiation (SWDOWN).
#---------------------------------------------------------------------------
#var = 'SWDOWN'
#for m in range(len(models)):
#    model = models[m]
#    for n in range(len(sdts)):
#        sdt = sdts[n]
#        edt = edts[n]
#        yyyy_s = sdt[0:4]
#        yyyy_e = edt[0:4]        
#
#        #--- Get list of extracted files for this model.
#        files = get_extract_files(data_dirs, model, sdt, edt, 1)
#
#        #--- Get files broken out by year and season.
#        (spr_files, sum_files, fal_files, win_files, years) = \
#                    get_seasonal_files(files)
#
#        for season in seasons:
#            file_plot = rundir + '/' + model + '_' + var + '_' + season + \
#                        '_' + yyyy_s + '_' + yyyy_e + '.nc'
#            syscom = 'ncra'
#            nyears = 0
#            for y in range(len(years)):
#                yyyy = years[y]
#
#                if season == 'summer':
#                    if (sum_files[yyyy] == sum_files[yyyy]):
#                        nyears += 1
#                    for f in range(len(sum_files[yyyy])):
#                        syscom = syscom + ' ' + sum_files[yyyy][f]
#                    else:
#                        continue
#                elif season == 'spring':
#                    if (spr_files[yyyy] == spr_files[yyyy]):
#                        nyears += 1
#                        for f in range(len(spr_files[yyyy])):
#                            syscom = syscom + ' ' + spr_files[yyyy][f]
#                        else:
#                            continue
#                elif season == 'winter':
#                    if (win_files[yyyy] == win_files[yyyy]):
#                        nyears += 1
#                        for f in range(len(win_files[yyyy])):
#                            syscom = syscom + ' ' + win_files[yyyy][f]
#                        else:
#                            continue
#                elif season == 'fall':                        
#                    if (fal_files[yyyy] == fal_files[yyyy]):
#                        nyears += 1
#                        for f in range(len(fal_files[yyyy])):
#                            syscom = syscom + ' ' + fal_files[yyyy][f]
#                        else:
#                            continue
#
#            syscom = syscom + ' ' + file_plot
#            print 'syscom = ', syscom        
#            os.system(syscom)
#
#            print 'Loading ', var, ' from ', file_plot
#            nc = NetCDFFile(file_plot, 'r')
#            ntimes = len(nc.dimensions['Time'])
#            vardat = {}
#            dat_tmp = nc.variables[var][:,:,:]
#            dat_tmp = np.transpose(dat_tmp)
#            vardat[var] = dat_tmp
#            nc.close()
#            
#            titlein = model.upper() + ', ' + var_lab[var] + ', ' + \
#                      seasons_lab[season] + ', ' + yyyy_s + ' - ' + yyyy_e
#    
#            plotdat = vardat[var][:,:,0]
#            if (var == 'PREC'):
#                plotdat = plotdat * mm2in
#    
#            for z in range(len(zooms)):
#                plotfname = rundir + '/' + model + '_' + sdt + '_' + \
#                            edt + '_' + season + '_' + var + '_' + \
#                            zooms[z] + '.png'
#                mapper(var, lat, lon, plotdat, levs_swdown, cmap_swdown, \
#                       maplw, colorbar_labs[var], titlein, plotfname, \
#                       zooms[z])
##            sys.exit()

'''
#---------------------------------------------------------------------------
# For each model, make a maps Snow Water Equivalent.
#---------------------------------------------------------------------------
var = 'SNOW'
for m in range(len(models)):

    model = models[m]

    for n in range(len(sdts)):
        sdt = sdts[n]
        edt = edts[n]
        yyyy_s = sdt[0:4]
        yyyy_e = edt[0:4]        

        #--- Get list of extracted files for this model.
        files = []
        for d in range(len(data_dirs)):
#            path_c = data_dirs[d] + '/' + model + '/extract/*0401*.nc'
            path_c = data_dirs[d] + '/' + model + '/swe/*0401*.nc'
            files_c = glob.glob(path_c)
        
            for f in range(len(files_c)):
                if 'snowrad' not in files_c[f]:
                    continue

                dt_c = re.findall(r'\.(\d{10})\.', files_c[f])
                if (dt_c):
                    dt_c = dt_c[0]
                    yyyymm_c = dt_c[0:6]
                    if (yyyymm_c >= sdt and yyyymm_c <= edt):
                        files.append(files_c[f])

        #--- Make plot for each zooms.
        file_plot = rundir + '/' + model + '_' + var + '_apr1_' + \
                    yyyy_s + '_' + yyyy_e + '.nc'
        syscom = 'ncra'
        for f in range(len(files)):
            syscom = syscom + ' ' + files[f]
            
        syscom = syscom + ' ' + file_plot
        print 'syscom = ', syscom        
        os.system(syscom)
    
        print 'Loading ', var, ' from ', file_plot
        nc = NetCDFFile(file_plot, 'r')
        ntimes = len(nc.dimensions['Time'])
        vardat = {}
        dat_tmp = nc.variables[var][:,:,:]
        dat_tmp = np.transpose(dat_tmp)
        vardat[var] = dat_tmp
        nc.close()
        titlein = model.upper() + ', ' + var_lab[var] + ', ' + \
                  'Apr 1st, ' + yyyy_s + ' - ' + yyyy_e
    
        plotdat = vardat[var][:,:,0]
    
        for z in range(len(zooms)):
            plotfname = rundir + '/' + model + '_' + sdt + '_' + \
                        edt + '_apr1_' + var + '_' + \
                        zooms[z] + '.png'
            mapper(var, lat, lon, plotdat, levs_swe, cmap_swe, \
                   maplw, colorbar_labs[var], titlein, plotfname, \
                   zooms[z])
'''
                  
#---------------------------------------------------------------------------
# Make map of Snow Water Equivalent for ensemble mean.
#---------------------------------------------------------------------------
#(nx,ny) = lat.shape
#snowrad_mean = np.ones((nx, ny, len(sdts))) * np.nan
#var = 'SNOW'
#for n in range(len(sdts)):
#    sdt = sdts[n]
#    edt = edts[n]
#    yyyy_s = sdt[0:4]
#    yyyy_e = edt[0:4]        
#
#    files = []
#    dts_all = []
#    for m in range(len(models)):
#        model = models[m]
#        for d in range(len(data_dirs)):
#            path_c = data_dirs[d] + '/' + model + '/swe/*0401*.nc'
#            files_c = glob.glob(path_c)
#        
#            for f in range(len(files_c)):
#                if 'snowrad' not in files_c[f]:
#                    continue
#
#                dt_c = re.findall(r'\.(\d{10})\.', files_c[f])
#
#                if (dt_c):
#                    dt_c = dt_c[0]
#                    yyyymm_c = dt_c[0:6]
#                    if (yyyymm_c >= sdt and yyyymm_c <= edt):
#                        files.append(files_c[f])
#                        dts_all.append(dt_c)
#                        
#    #--- Make plot for each zooms.
##    file_plot = rundir + '/ensmean_' + var + '_apr1_' + \
##                yyyy_s + '_' + yyyy_e + '.nc'
#
#    for f in range(len(files)):
#        print files[f], dts_all[f]
#        (vardat, ntimes) = load_wrfout_swe(files[f], [var])
#        plotdat = vardat[var][:,:,0]
#        nx,ny = plotdat.shape
#        if f == 0:
#            dat_all = np.ones((nx, ny, len(files))) * np.nan
#        dat_all[:,:,f] = plotdat
#        print 'np.amax(plotdat) = ', np.amax(plotdat)
#
#    snowrad_mean[:,:,n] = np.mean(dat_all, axis=2)
#    snowrad_std = np.std(dat_all, axis=2)    
#
#    print 'np.amax(snowrad_std)  = ', np.amax(snowrad_std)
#    print 'np.amax(snowrad_mean) = ', np.amax(snowrad_mean[:,:,n])
#    print 'np.amax(dat_all[:,:,0]) = ', np.amax(dat_all[:,:,0])
#    print 'np.amax(dat_all[:,:,1]) = ', np.amax(dat_all[:,:,1])            
#
#    for z in range(len(zooms)):
#        titlein = 'Ensemble Mean, ' + var_lab[var] + ', ' + \
#                  'Apr 1st, ' + yyyy_s + ' - ' + yyyy_e
#        plotfname = rundir + '/ensmean_' + sdt + '_' + \
#                    edt + '_apr1_' + var + '_' + zooms[z] + '.png'
#        mapper(var, lat, lon, snowrad_mean[:,:,n], levs_swe, cmap_swe, \
#               maplw, colorbar_labs[var], titlein, plotfname, \
#               zooms[z])
#
#        titlein = 'Ensemble Standard Deviation, ' + var_lab[var] + ', ' + \
#                  'Apr 1st, ' + yyyy_s + ' - ' + yyyy_e
#        plotfname = rundir + '/ensstd_' + sdt + '_' + \
#                    edt + '_apr1_' + var + '_' + zooms[z] + '.png'
#        mapper(var, lat, lon, snowrad_std, levs_swe, cmap_swe, \
#               maplw, colorbar_labs[var], titlein, plotfname, \
#               zooms[z])
#

pf = pickle_dir + '/swe_mean.pkl'
#pickle.dump((snowrad_mean), open(pf,'wb'), -1)

(snowrad_mean) = pickle.load(open(pf, 'rb'))
var = 'SNOW'
for n in range(1,len(sdts)):
    diff_c = snowrad_mean[:,:,n] - snowrad_mean[:,:,0]
    dttit = '(' + sdts[n][0:4] + '-' + edts[n][0:4] + ') - ' + \
            '(' + sdts[0][0:4] + '-' + edts[0][0:4] + ')'

    print diff_c
    print np.amin(diff_c)
    print np.amax(diff_c)
    print diff_c.shape
    
    for z in range(len(zooms)):
        titlein = 'Apr 1st ' + var_lab[var] + \
                  ', Ensemble Mean Difference, ' + ', ' + dttit
        plotfname = rundir + '/ensmean_diff_' + sdts[n] + '_' + \
                    edts[n] + '_apr1_' + var + '_' + zooms[z] + '.png'
        mapper(var, lat, lon, diff_c, levs_swe_diff, cmap_swe_diff, \
               maplw, colorbar_labs[var], titlein, plotfname, \
               zooms[z])

##---------------------------------------------------------------------------
## Make map of difference.
##---------------------------------------------------------------------------
#for m in range(len(models)):
#    model = models[m]
#
#    syscom = 'ncdiff'
#    file_diff = rundir + '/' + model + '_' + var + '_apr1_' + 'diff.nc'
#
#    for n in range(len(sdts)):
#        sdt = sdts[n]
#        edt = edts[n]
#        yyyy_s = sdt[0:4]
#        yyyy_e = edt[0:4]        
#        file_plot = rundir + '/' + model + '_' + var + '_apr1_' + \
#                    yyyy_s + '_' + yyyy_e + '.nc'
#            if os.path.isfile(file_plot):
#                syscom = syscom + ' ' + file_plot
#
#    syscom = syscom + ' ' + file_diff
#
#    print 'syscom = ', syscom        
#    os.system(syscom)
#
#    print 'Loading ', var, ' from ', file_diff
#    nc = NetCDFFile(file_diff, 'r')
#    ntimes = len(nc.dimensions['Time'])
#    vardat = {}
#    dat_tmp = nc.variables[var][:,:,:]
#    dat_tmp = np.transpose(dat_tmp)
#    vardat[var] = dat_tmp
#    nc.close()
#
#    titlein = model.upper() + ', ' + var_lab[var] + ', ' + \
#              'Apr 1st, Difference, ' + yyyy_s + ' - ' + yyyy_e
#        
#    plotdat = vardat[var][:,:,0]
#    
#    for z in range(len(zooms)):
#        plotfname = rundir + '/' + model + '_' + sdt + '_' + \
#                    edt + '_apr1_diff_' + var + '_' + \
#                    zooms[z] + '.png'
#        mapper(var, lat, lon, plotdat, levs_swe, cmap_swe, \
#               maplw, colorbar_labs[var], titlein, plotfname, \
#               zooms[z])

sys.exit()

<file_sep>/utils_cei.py
#!/usr/bin/python
#----------------------------------------------------------------------------
# Climate extreme indices based on Rick's code which is based on:
# http://etccdi.pacificclimate.org/list_27_indices.shtml
#----------------------------------------------------------------------------
import os, sys, glob, re, math
import numpy as np
from utils_date import *
from utils_ghcnd_obs import *

#basefirst = 1971
#baselast  = 2000
#first     = 1970
#last      = 2099

basefirst = 1971
baselast  = 1974
first     = 1970
last      = 1979

#--- percentages used for...
percs = [0.1, 0.9]

dpm = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]

#----------------------------------------------------------------------------
# calculate daily percentages of the historical period.
#----------------------------------------------------------------------------
def get_daily_perc_mxmn(models, stns, mod_all_in, sdt):

    bdt = sdt + '0100'
    daily_perc = {}

    #--- Loop over models.
    for m in range(len(models)):
        model = models[m]
        mod_all = mod_all_in[model]
        #--- Loop over stations.
        for s in range(len(stns)):
            stn = stns[s]
            #--- Loop over each day of the year.
            for d in range(0,364):
                fdate = time_increment(bdt, d, dtfmt)
                basemmddhh = fdate[4:10]
                txunsorted = []
                tnunsorted = []
                #--- Loop over every year within the "base" period.
                for y in range(basefirst, baselast+1):
                    basedate = str(y) + basemmddhh
                    #--- Per CEI definition of daily percentage calc, Looks
                    #--- +/- 2 days from basedate, presumably to increase
                    #--- sample size(?).
                    for n in range(-2, 3):
                        dt = time_increment(basedate, n, dtfmt)
                        key = ('T2MAX', stn, dt)
                        if key in mod_all:
                            txunsorted.append(mod_all['T2MAX', stn, dt])
                        key = ('T2MIN', stn, dt)
                        if key in mod_all:
                            tnunsorted.append(mod_all['T2MIN', stn, dt])

                #--- Sort lists of maxes and mins.
                txsorted = list(sorted(txunsorted))
                tnsorted = list(sorted(tnunsorted))

                #--- Get percentage (percentages defined in percs) for
                #--- max and min.
                for p in range(len(percs)):
                    perc = percs[p]
                    np = int(round(float(len(txsorted)+1) * perc))
                    key = (model, 'T2MAX', stn, fdate, str(perc))
                    daily_perc[key] = txsorted[np]
                    
                    np = int(round(float(len(tnsorted)+1) * perc))
                    key = (model, 'T2MIN', stn, fdate, str(perc))
                    daily_perc[key] = tnsorted[np]
                    
    return daily_perc

#----------------------------------------------------------------------------
# Calculate monthly max and min climate index.
#----------------------------------------------------------------------------
def get_monthly_mxmn(daily_perc, mod_all, models, stns, sdt):

    model = models[0]
    stn = stns[0]

    for m in range(0,12):
        mstr = str(m+1).zfill(2)
        for y in range(first, last+1):
            bdt = str(y) + mstr + '0100'
            yyyymm = str(y) + mstr
            for d in range(0, dpm[m]):
                fdate = time_increment(bdt, d, dtfmt)
                basedate = '1970' + fdate[4:10]

                key   = ('T2MAX', stn, fdate)
                keydp = (model, 'T2MAX', stn, fdate)                

                if mod_all[key] > daily_perc[keydp]

                print fdate
            sys.exit()


#    #--- Loop over models.
#    for m in range(len(models)):
#        model = models[m]
#        mod_all = mod_all_in[model]
#        #--- Loop over stations.
#        for s in range(len(stns)):
#            stn = stns[s]
#
#            for m in range(0,12):
#                print m
#                for y in range(first, last+1):
#                    print y

                    
        sys.exit()



<file_sep>/old/old_old/plot_wrfout_stns_20180117.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re
import numpy as np
#import matplotlib.pyplot as plt
from make_cmap import *
from netCDF4 import Dataset as NetCDFFile
from utils_wrfout import *
from utils_date import *
import pickle

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
plot_dir   = rcm_dir + '/plots'
pickle_dir = rcm_dir + '/pickle'
data_dir = '/home/disk/r2d2/steed/cmip5/rcp8.5'

geo_em = '/home/disk/a125/steed/run/geo_em.d02.nc'

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
var = 'PREC'
sdt = '202001'
edt = '202912'
models = ['gfdl-cm3']
dtfmt = "%Y%m%d%H"

stns = ['KSEA', 'KSMP', 'KYKM']
latpts = [47.44472, 47.27667, 46.56417]
lonpts = [-122.31361, -121.33722, -120.53361]
#stns = ['KSEA']
#latpts = [47.44472]
#lonpts = [-122.31361]
#stns = ['KSMP']
#latpts = [47.27667]
#lonpts = [-121.33722]

#---------------------------------------------------------------------------
# Loop over each models, grabbing data from extract wrfout files.
#---------------------------------------------------------------------------
pickle_file = pickle_dir + '/' + var + '_' + sdt + '_' + edt + '.pickle'
if os.path.isfile(pickle_file):
    (data_all, dts_unique, models, stns) = pickle.load(open(pickle_file, 'rb'))
else:
    print 'Loading ', geo_em
    (lat, lon) = load_geo_em(geo_em)

    data_all = {}
    dts_all = []
    for m in range(len(models)):

        model = models[m]
        data_dir_c = data_dir + '/' + model + '/extract'

        #--- Get list of extracted files for this model.
        files = get_extract_files(data_dir_c, model, sdt, edt)

        #--- Loop over all extract files, reading them in.
        for f in range(len(files)):
            print 'Loading ', files[f]
            (vardat, ntimes, dt) = load_wrfout(files[f], var)

            #--- Loop over each ntime, grabbing interpolated data for
            #--- variable var for each stns.
            for n in range(ntimes):
                dt_c = time_increment(dt, n, dtfmt)
                for ns in range(len(stns)):
                    prec_interp = bilinear_interpolate(lat, lon, vardat[:,:,n],\
                                                       latpts[ns], lonpts[ns],\
                                                       0)
                    data_all[model, stns[ns], dt_c] = prec_interp
                    dts_all.append(dt_c)

    #--- Get sorted unique list of all dates seen.
    dts_unique = list(sorted(set(dts_all)))
    
    #--- Create pickle file.
    pickle.dump((data_all, dts_unique, models, stns),open(pickle_file,'wb'), -1)

#---------------------------------------------------------------------------
# Get seasonal totals.
#---------------------------------------------------------------------------
(springs, summers, falls, winters, years) = \
          get_seasonal_totals(data_all, dts_unique, models, stns)

#---------------------------------------------------------------------------
# Plot times series of total precip for each station.
#---------------------------------------------------------------------------
mod = 'gfdl-cm3'
plotfname = 'test.png'
titlein = mod + ', ' + str(years[0]) + ' - ' + str(years[len(years)-1])
iret = ts(winters, springs, summers, falls, years, stns, mod, \
          titlein, plotfname)

#---------------------------------------------------------------------------
# 
#---------------------------------------------------------------------------

sys.exit()

<file_sep>/plot_rcm_stns_swe.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re, math
import numpy as np
#import matplotlib.pyplot as plt
from make_cmap import *
from netCDF4 import Dataset as NetCDFFile
from utils_wrfout import *
from utils_load_data import *
from utils_plot import *
from utils_date import *
from utils_cmap import *
from utils_snotel_obs import *
import pickle
from collections import defaultdict
from make_cmap import *
import csv

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
plot_dir   = rcm_dir + '/plots'
pickle_dir = rcm_dir + '/pickle'
data_dir   = rcm_dir + '/data'
snotel_dir = data_dir + '/snotel'
run_dirs   = rcm_dir + '/rundirs'
geo_em     = rcm_dir + '/data' + '/geo_em.d02.nc'
data_dirs  = ['/home/disk/r2d2/steed/cmip5/rcp8.5', \
              '/home/disk/vader/steed/cmip5/rcp8.5', \
              '/home/disk/jabba/steed/cmip5/rcp8.5']
station_file = data_dir + '/station_file_swe.txt'

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
varswe = 'SNOW'
stat = 'tot'

sdt = '197001'
edt = '209912'

models = ['access1.0', 'access1.3', 'bcc-csm1.1', 'canesm2', \
          'ccsm4', 'csiro-mk3.6.0', 'fgoals-g2', 'gfdl-cm3', \
          'giss-e2-h', 'miroc5', 'mri-cgcm3', 'noresm1-m']
mod_cols = ['indigo', 'blue', 'deepskyblue', \
            'darkgreen', 'lime', 'yellow', \
            'magenta', 'red', 'salmon', 'gray', 'darkgray', 'lightblue']

yyyy_s = sdt[0:4]
yyyy_e = edt[0:4]        

#---------------------------------------------------------------------------
# Load stations file.
#---------------------------------------------------------------------------
print 'Loading ', station_file
(stns, latpts, lonpts, elevs, stn_names) = read_swe_stations_file(station_file)

#---------------------------------------------------------------------------
# Load SNOTEL data for each stns, if data exists.
#---------------------------------------------------------------------------
swe_yyyy = {}
swe_obs  = {}
for s in range(len(stns)):
    stn = stns[s]
    swe_file = snotel_dir + '/' + stns[s] + '.csv'
    if os.path.isfile(swe_file):
        print 'Loading ', swe_file
        (swe_yyyy[stn], swe_obs[stn]) = read_snotel(swe_file)
    
#---------------------------------------------------------------------------
# Load geo_em file.
#---------------------------------------------------------------------------
print 'Loading ', geo_em
(lat, lon, hgt) = load_geo_em(geo_em)

#---------------------------------------------------------------------------
# Get model station elevations.
#---------------------------------------------------------------------------
stn_mod_elevs = []
print 'stn, actual elev, mod elev'
for s in range(len(stns)):
    elev_c = bilinear_interpolate(lat, lon, hgt, latpts[s], lonpts[s], 0)
    stn_mod_elevs.append(elev_c)
    print stns[s], elevs[s], stn_mod_elevs[s]

#---------------------------------------------------------------------------
# For each model, read in Snow Water Equivalent data for all stns.
#---------------------------------------------------------------------------
pfswe = pickle_dir + '/swe_stns.pkl'
if os.path.isfile(pfswe):
    print 'Loading ', pfswe
    (stns, dts_all, swe_all) = pickle.load(open(pfswe, 'rb'))
else:
    for m in range(len(models)):
        model = models[m]

        #--- Get list of extracted files for this model.
        files = []
        for d in range(len(data_dirs)):
            path_c = data_dirs[d] + '/' + model + '/swe/*0401*.nc'
            files_c = glob.glob(path_c)
        
            for f in range(len(files_c)):
                if 'snowrad' not in files_c[f]:
                    continue
            
                dt_c = re.findall(r'\.(\d{10})\.', files_c[f])
                if (dt_c):
                    dt_c = dt_c[0]
                    yyyymm_c = dt_c[0:6]
                    if (yyyymm_c >= sdt and yyyymm_c <= edt):
                        files.append(files_c[f])

        #--- Initialize our big SWE array.  
        if 'swe_all' not in locals():
            swe_all = np.ones((len(models), len(stns), len(files))) * np.nan

        #--- Loop over files found above reading in Apr 1 SWE for every station.
        dts_c = []    
        for f in range(len(files)):
            print files[f]
            (vardat, ntimes) = load_wrfout_swe(files[f], [varswe])
            dt_c = re.findall(r'\.(\d{10})\.', files[f])
            dts_c.append(dt_c)

            for s in range(len(stns)):
                dat_interp = bilinear_interpolate(lat, lon, \
                                                  vardat[varswe][:,:,0], \
                                                  latpts[s], lonpts[s], 0)
                swe_all[m,s,f] = dat_interp

        #--- Initialize our dates array if this is the first model.  Otherwise
        #--- do a date check to make sure # of dates are identical to first
        #--- model's.  Bomb otherwise, as that means a model is missing a file.
        if 'dts_all' not in locals():
            dts_all = dts_c
        else:
            if sorted(dts_c) != sorted(dts_all):
                print 'dts_c   = ', dts_c
                print 'dts_all = ', dts_all                
                sys.exit('dts problem!')

    pickle.dump((stns, dts_all, swe_all), open(pfswe,'wb'), -1)

#---------------------------------------------------------------------------
# nan out 1970, because that's a partial winter given models started 01/01/70.
#---------------------------------------------------------------------------
swe_all[:,:,0] = np.nan

#---------------------------------------------------------------------------
# Get years from dts_all.
#---------------------------------------------------------------------------
years = []
for d in range(len(dts_all)):
    years.append(dts_all[d][0][0:4])

#---------------------------------------------------------------------------
# Make plots using ts_swe.
#---------------------------------------------------------------------------
(nm,ns,nf) = swe_all.shape
for s in range(len(stns)):
    stn = stns[s]
    plotfname = plot_dir + '/' + stn + '_' + sdt + '_' + edt + '_swe.png'
    titlein = stn_names[s] + ' (' + stn.upper() + '), ' + \
              '(model elev: ' + str(int(stn_mod_elevs[s])) + '-m, ' + \
              'actual elev: ' + str(int(elevs[s])) + '-m), ' + \
              labels[varswe+stat] + ', ' + \
              str(years[0]) + ' - ' + str(years[len(years)-1])
    
    iret = ts_swe(swe_all, years, swe_obs[stn], swe_yyyy[stn], stn, s, \
                  models, titlein, plotfname, varswe, stat, mod_cols)
        
sys.exit()

<file_sep>/old/old_old/plot_wrfout_stns_20180123.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re
import numpy as np
#import matplotlib.pyplot as plt
from make_cmap import *
from netCDF4 import Dataset as NetCDFFile
from utils_wrfout import *
from utils_date import *
import pickle

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
plot_dir   = rcm_dir + '/plots'
pickle_dir = rcm_dir + '/pickle'
data_dir = '/home/disk/r2d2/steed/cmip5/rcp8.5'

geo_em = '/home/disk/a125/steed/run/geo_em.d02.nc'

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
vars = ['PREC', 'T2MAX', 'T2MIN', 'SPDUV10MAX', 'SPDUV10MEAN', 'SPDUV10STD']
sdt = '202001'
edt = '202912'
models = ['gfdl-cm3']
dtfmt = "%Y%m%d%H"

stns = ['KSEA', 'KSMP', 'KYKM']
latpts = [47.44472, 47.27667, 46.56417]
lonpts = [-122.31361, -121.33722, -120.53361]

#---------------------------------------------------------------------------
# Loop over each models, grabbing data from extract wrfout files.
#---------------------------------------------------------------------------
pickle_file = pickle_dir + '/extract_' + sdt + '_' + edt + '.pickle'
if os.path.isfile(pickle_file):
    (data_all, dts_unique, models, vars, stns) = \
               pickle.load(open(pickle_file, 'rb'))
else:
    print 'Loading ', geo_em
    (lat, lon) = load_geo_em(geo_em)

    data_all = {}
    dts_all = []
    for m in range(len(models)):

        model = models[m]
        data_dir_c = data_dir + '/' + model + '/extract'

        #--- Get list of extracted files for this model.
        files = get_extract_files(data_dir_c, model, sdt, edt)

        #--- Loop over all extract files, reading them in.
        for f in range(len(files)):

            print 'Loading ', files[f]
            (vardat, ntimes, dt) = load_wrfout(files[f], vars)

            #--- Loop over each vars, interpolating data to stns for
            #--- each ntimes.
            for v in range(len(vars)):

                var = vars[v]
                vardat_c = vardat[var]
                
                #--- Loop over each ntime, grabbing interpolated data for
                #--- variable var for each stns.
                for n in range(ntimes):
                    dt_c = time_increment(dt, n, dtfmt)
                    for ns in range(len(stns)):
                        dat_interp = bilinear_interpolate(lat, lon, \
                                                          vardat_c[:,:,n],\
                                                          latpts[ns], \
                                                          lonpts[ns], 0)
                        data_all[model, var, stns[ns], dt_c] = dat_interp
                        dts_all.append(dt_c)

    #--- Get sorted unique list of all dates seen.
    dts_unique = list(sorted(set(dts_all)))
    
    #--- Create pickle file.
    pickle.dump((data_all, dts_unique, models, vars, stns), \
                open(pickle_file,'wb'), -1)

#---------------------------------------------------------------------------
# Get seasonal precipitation totals and make a time series plot of them.
#---------------------------------------------------------------------------
(spr_pcp_tot, sum_pcp_tot, fal_pcp_tot, win_pcp_tot, years) = \
          get_seasonal_stats(data_all, dts_unique, models, stns, 'PREC', 'tot')

mod = 'gfdl-cm3'
plotfname = plot_dir + '/' + mod + '_season_precip.png'
titlein = mod + ', ' + str(years[0]) + ' - ' + str(years[len(years)-1])
iret = ts(win_pcp_tot, spr_pcp_tot, sum_pcp_tot, fal_pcp_tot, years, \
          stns, mod, titlein, plotfname, 'PREC')

#---------------------------------------------------------------------------
# Get seasonal temperature averages.
#---------------------------------------------------------------------------
(spr_tmx_avg, sum_tmx_avg, fal_tmx_avg, win_tmx_avg, years) = \
          get_seasonal_stats(data_all, dts_unique, models, stns, 'T2MAX', 'avg')
(spr_tmn_avg, sum_tmn_avg, fal_tmn_avg, win_tmn_avg, years) = \
          get_seasonal_stats(data_all, dts_unique, models, stns, 'T2MIN', 'avg')

(spr_spdavg_avg, sum_spdavg_avg, fal_spdavg_avg, win_spdavg_avg, years) = \
                 get_seasonal_stats(data_all, dts_unique, models, stns, \
                                    'SPDUV10MEAN', 'avg')

mod = 'gfdl-cm3'
plotfname = plot_dir + '/' + mod + '_season_tmx_avg.png'
titlein = mod + ', TMAX Average, ' + str(years[0]) + ' - ' + \
          str(years[len(years)-1])
iret = ts(win_tmx_avg, spr_tmx_avg, sum_tmx_avg, fal_tmx_avg, years, \
          stns, mod, titlein, plotfname, 'T2MAX')

mod = 'gfdl-cm3'
plotfname = plot_dir + '/' + mod + '_season_tmn_avg.png'
titlein = mod + ', TMIN Average, ' + str(years[0]) + ' - ' + \
          str(years[len(years)-1])
iret = ts(win_tmn_avg, spr_tmn_avg, sum_tmn_avg, fal_tmn_avg, years, \
          stns, mod, titlein, plotfname, 'T2MIN')

#---------------------------------------------------------------------------
# 
#---------------------------------------------------------------------------

sys.exit()

<file_sep>/old/plot_wrfout_grid_avg_20190516.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re, math
import numpy as np
#import matplotlib.pyplot as plt
#from make_cmap import *
from netCDF4 import Dataset as NetCDFFile
from utils_wrfout import *
from utils_date import *
import pickle
from collections import defaultdict

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
plot_dir   = rcm_dir + '/plots'
pickle_dir = rcm_dir + '/pickle'
run_dirs   = rcm_dir + '/rundirs'
geo_em     = rcm_dir + '/data' + '/geo_em.d02.nc'
data_dirs  = ['/home/disk/r2d2/steed/cmip5/rcp8.5', \
              '/home/disk/vader/steed/cmip5/rcp8.5', \
              '/home/disk/jabba/steed/cmip5/rcp8.5']

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
#vars = ['PREC', 'T2MAX', 'T2MIN', 'SPDUV10MAX', 'SPDUV10MEAN', 'SPDUV10STD']
#vars = ['T2MAX', 'T2MIN', 'PREC']
vars = ['T2MAX', 'T2MIN']
#vars = ['PREC']
#vars = ['SPDUV10MAX', 'SPDUV10MEAN']

sdts = ['197001', '203001', '207001']
edts = ['200012', '206012', '209912']

models = ['access1.0', 'access1.3', 'bcc-csm1.1', 'canesm2', \
          'ccsm4', 'csiro-mk3.6.0', 'fgoals-g2', 'gfdl-cm3', \
          'giss-e2-h', 'miroc5', 'mri-cgcm3', 'noresm1-m']

dtfmt = "%Y%m%d%H"

#---------------------------------------------------------------------------
# Make run directory, load geo_em file.
#---------------------------------------------------------------------------
rundir = run_dirs + '/plot_wrfout_grid_avg.' + str(os.getpid())
print 'making ', rundir
os.makedirs(rundir)

#rundir = run_dirs + '/plot_wrfout_grid_avg.23227' #
#rundir = run_dirs + '/plot_wrfout_grid_avg.16978' # PREC
#rundir = run_dirs + '/plot_wrfout_grid_avg.27465'

print 'Loading ', geo_em
(lat, lon, hgt) = load_geo_em(geo_em)

'''
#---------------------------------------------------------------------------
# For each model, make a map of every season...
#---------------------------------------------------------------------------
for m in range(len(models)):
    model = models[m]
    for n in range(len(sdts)):
        sdt = sdts[n]
        edt = edts[n]
        yyyy_s = sdt[0:4]
        yyyy_e = edt[0:4]        

        #--- Get list of extracted files for this model.
        files = get_extract_files(data_dirs, model, sdt, edt, 0)

        #--- Get files broken out by year and season.
        (spr_files, sum_files, fal_files, win_files, ann_files, years) = \
                    get_seasonal_files(files)

        for var in vars:
            nyears_ann = 0
            for season in seasons:
                file_plot = rundir + '/' + model + '_' + var + '_' + season + \
                            '_' + yyyy_s + '_' + yyyy_e + '.nc'

                if (var == 'T2MAX' or var == 'T2MIN'):
                    syscom = 'ncra'
                elif (var == 'PREC'):
                    syscom = 'ncra -y ttl'

                nyears = 0
                for y in range(len(years)):
                    yyyy = years[y]
                    if season == 'summer':
                        if (sum_files[yyyy] == sum_files[yyyy]):
                            nyears += 1
                            for f in range(len(sum_files[yyyy])):
                                syscom = syscom + ' ' + sum_files[yyyy][f]
                            else:
                                continue
                    elif season == 'spring':
                        if (spr_files[yyyy] == spr_files[yyyy]):
                            nyears += 1
                            for f in range(len(spr_files[yyyy])):
                                syscom = syscom + ' ' + spr_files[yyyy][f]
                            else:
                                continue
                    elif season == 'winter':
                        if (win_files[yyyy] == win_files[yyyy]):
                            nyears += 1
                            for f in range(len(win_files[yyyy])):
                                syscom = syscom + ' ' + win_files[yyyy][f]
                            else:
                                continue
                    elif season == 'fall':                        
                        if (fal_files[yyyy] == fal_files[yyyy]):
                            nyears += 1
                            for f in range(len(fal_files[yyyy])):
                                syscom = syscom + ' ' + fal_files[yyyy][f]
                            else:
                                continue
                    elif season == 'annual':                        
                        if (ann_files[yyyy] == ann_files[yyyy]):
                            nyears += 1
                            for f in range(len(ann_files[yyyy])):
                                syscom = syscom + ' ' + ann_files[yyyy][f]
                            else:
                                continue

                nyears_ann = nyears_ann + nyears

                syscom = syscom + ' ' + file_plot
                print 'syscom = ', syscom        
                os.system(syscom)
        
                print 'Loading ', var, ' from ', file_plot
                (vardat, ntimes, dt) = load_wrfout(file_plot, [var])
        
                titlein = model.upper() + ', Average ' + var_lab[var] + ', ' + \
                          seasons_lab[season] + ', ' + yyyy_s + ' - ' + yyyy_e
        
                plotdat = vardat[var][:,:,0]
                if (var == 'PREC'):
                    if 'prec_tot' in locals():
                        prec_tot = np.add(prec_tot, plotdat)
                    else:
                        prec_tot = plotdat
                    
                    plotdat = (plotdat / nyears) * mm2in
                    levs_c = levs_pcp
                    cmap_c = cmap_pcp
                else:
                    plotdat = plotdat - 273.15
                    levs_c = levs_temp
                    cmap_c = cmap_temp
        
                for z in range(len(zooms)):
                    plotfname = rundir + '/' + model + '_' + sdt + '_' + \
                                edt + '_' + season + '_' + var + '_' + \
                                zooms[z] + '.png'
                    mapper(var, lat, lon, plotdat, levs_c, cmap_c, \
                           maplw, colorbar_labs[var], titlein, plotfname, \
                           zooms[z])
#                sys.exit()

            if (var == 'PREC'):
                file_plot = rundir + '/' + model + '_' + var + '_annual_' + \
                            yyyy_s + '_' + yyyy_e + '.nc'
                f = NetCDFFile(file_plot, 'w')
                (nx,ny) = prec_tot.shape
                f.createDimension('nx', nx)
                f.createDimension('ny', ny)
                pcpnc = f.createVariable('pcp', 'f', ('nx','ny'))
                pcpnc[:,:] = prec_tot
                pcpnc.description = 'total annual precip'
                pcpnc.units       = 'mm'
                f.close()

sys.exit()
'''

#---------------------------------------------------------------------------
# Ensemble spread plots for every season...
#---------------------------------------------------------------------------
for n in range(len(sdts)):
    spr_files = {}
    sum_files = {}
    fal_files = {}
    win_files = {}
    ann_files = {}    
    
    sdt = sdts[n]
    edt = edts[n]
    yyyy_s = sdt[0:4]
    yyyy_e = edt[0:4]        

    #--- Fill seasonal file dictionaries for each model.
    for m in range(len(models)):
        model = models[m]

        #--- Get list of extracted files for this model.
        files = get_extract_files(data_dirs, model, sdt, edt, 0)

        #--- Get files broken out by year and season.
        (spr_files[model], sum_files[model], fal_files[model], \
         win_files[model], ann_files[model], years) = get_seasonal_files(files)

    #--- For each variable and season, make a plot.
    for var in vars:
        if (var == 'T2MAX' or var == 'T2MIN' or var == 'SPDUV10MAX' or \
            var == 'SPDUV10MEAN'):
            ncocom = 'ncra'
        elif (var == 'PREC'):
            ncocom = 'ncra -y ttl'

        for season in seasons:
            file_plot = rundir + '/' + var + '_' + season + \
                        '_' + yyyy_s + '_' + yyyy_e + '.nc'

#            (ensstd, nyears) = get_ensmean_std(file_plot, var, models, \
#                                               years, season, \
#                                               sum_files, spr_files, \
#                                               win_files, fal_files, \
#                                               ann_files, ncocom)
            (ensstd, nyears) = get_ensmean_std(file_plot, var, models, \
                                               years, season, files)

            print ensstd
            
#            if os.path.isfile(file_plot):
#                print 'file_plot already exists.  file_plot = ', file_plot
#            else:
#                iret = run_ensmean_syscom(file_plot, var, ncocom, syscom, \
#                                          yyyy_s, yyyy_e, season, rundir)

            #--- Add number of years (nmodels * years) as dimension to nc file.
            syscom = 'ncap2 -s \'defdim(\"nmodyears\",' + str(nyears) + \
                ')\'' + ' ' +  file_plot
            print syscom
            os.system(syscom)
            
            print 'Loading ', var, ' from ', file_plot
            (vardat, ntimes, dt) = load_wrfout(file_plot, [var])

            titlein = 'Ensemble Mean, ' + var_lab[var] + ', ' + \
                      seasons_lab[season] + ', ' + yyyy_s + ' - ' + yyyy_e

            plotdat = vardat[var][:,:,0]
            if (var == 'PREC'):
                plotdat = (plotdat / nyears) * mm2in
                levs_c = levs_pcp
                cmap_c = cmap_pcp
            elif var == 'T2MAX' or var == 'T2MIN':
                plotdat = plotdat - 273.15
                levs_c = levs_temp
                cmap_c = cmap_temp
            elif var == 'SPDUV10MAX':
                levs_c = levs_spd
                cmap_c = cmap_spd

            for z in range(len(zooms)):
                plotfname = rundir + '/map_all_' + sdt + '_' + edt + '_' + \
                            season + '_' + var + '_' + zooms[z] + '.png'

                print var, lat, lon, plotdat, levs_c, cmap_c, \
                       maplw, colorbar_labs[var], titlein, plotfname, \
                       zooms[z]
                
                mapper(var, lat, lon, plotdat, levs_c, cmap_c, \
                       maplw, colorbar_labs[var], titlein, plotfname, \
                       zooms[z])
            #sys.exit()
sys.exit()

'''
#---------------------------------------------------------------------------
# Ensemble mean plots for every season...
#---------------------------------------------------------------------------
for n in range(len(sdts)):
    spr_files = {}
    sum_files = {}
    fal_files = {}
    win_files = {}
    ann_files = {}    
    
    sdt = sdts[n]
    edt = edts[n]
    yyyy_s = sdt[0:4]
    yyyy_e = edt[0:4]        

    #--- Fill seasonal file dictionaries for each model.
    for m in range(len(models)):
        model = models[m]

        #--- Get list of extracted files for this model.
        files = get_extract_files(data_dirs, model, sdt, edt, 0)

        #--- Get files broken out by year and season.
        (spr_files[model], sum_files[model], fal_files[model], \
         win_files[model], ann_files[model], years) = get_seasonal_files(files)

    #--- For each variable and season, make a plot.
    for var in vars:
        if (var == 'T2MAX' or var == 'T2MIN' or var == 'SPDUV10MAX' or \
            var == 'SPDUV10MEAN'):
            ncocom = 'ncra'
        elif (var == 'PREC'):
            ncocom = 'ncra -y ttl'

        for season in seasons:
            file_plot = rundir + '/' + var + '_' + season + \
                        '_' + yyyy_s + '_' + yyyy_e + '.nc'

            (syscom, nyears) = get_ensmean_syscom(file_plot, var, models, \
                                                  years, season, \
                                                  sum_files, spr_files, \
                                                  win_files, fal_files, \
                                                  ann_files, ncocom)
            if os.path.isfile(file_plot):
                print 'file_plot already exists.  file_plot = ', file_plot
            else:
                iret = run_ensmean_syscom(file_plot, var, ncocom, syscom, \
                                          yyyy_s, yyyy_e, season, rundir)

            #--- Add number of years (nmodels * years) as dimension to nc file.
            syscom = 'ncap2 -s \'defdim(\"nmodyears\",' + str(nyears) + \
                ')\'' + ' ' +  file_plot
            print syscom
            os.system(syscom)
            
            print 'Loading ', var, ' from ', file_plot
            (vardat, ntimes, dt) = load_wrfout(file_plot, [var])

            titlein = 'Ensemble Mean, ' + var_lab[var] + ', ' + \
                      seasons_lab[season] + ', ' + yyyy_s + ' - ' + yyyy_e

            plotdat = vardat[var][:,:,0]
            if (var == 'PREC'):
                plotdat = (plotdat / nyears) * mm2in
                levs_c = levs_pcp
                cmap_c = cmap_pcp
            elif var == 'T2MAX' or var == 'T2MIN':
                plotdat = plotdat - 273.15
                levs_c = levs_temp
                cmap_c = cmap_temp
            elif var == 'SPDUV10MAX':
                levs_c = levs_spd
                cmap_c = cmap_spd

            for z in range(len(zooms)):
                plotfname = rundir + '/map_all_' + sdt + '_' + edt + '_' + \
                            season + '_' + var + '_' + zooms[z] + '.png'

                print var, lat, lon, plotdat, levs_c, cmap_c, \
                       maplw, colorbar_labs[var], titlein, plotfname, \
                       zooms[z]
                
                mapper(var, lat, lon, plotdat, levs_c, cmap_c, \
                       maplw, colorbar_labs[var], titlein, plotfname, \
                       zooms[z])
            #sys.exit()
#sys.exit()
'''
                
#---------------------------------------------------------------------------
# Get difference plots for the two sdts / edts periods.
#---------------------------------------------------------------------------
for nd in range(1,len(sdts)):
    print nd, sdts[nd]
    sdt_hist = sdts[0]
    edt_hist = edts[0]
    yyyy_s_hist = sdt_hist[0:4]
    yyyy_e_hist = edt_hist[0:4]        
    sdt_fore = sdts[nd]
    edt_fore = edts[nd]
    yyyy_s_fore = sdt_fore[0:4]
    yyyy_e_fore = edt_fore[0:4]        
    
    for var in vars:
        for season in seasons:

            #--- Get diff between forecast and historical for var and season.
            file_hist = rundir + '/' + var + '_' + season + \
                        '_' + yyyy_s_hist + '_' + yyyy_e_hist + '.nc'
            forename = yyyy_s_fore + '_' + yyyy_e_fore
            file_fore = rundir + '/' + var + '_' + season + \
                        '_' + forename + '.nc'
            file_diff = rundir + '/' + var + '_' + season + '_' + \
                        forename + '_diff.nc'
            syscom = 'ncdiff -O ' + file_fore + ' ' + file_hist + ' ' +file_diff
            print 'syscom = ', syscom
            os.system(syscom)

            #--- Load diff file.
            print 'Loading ', var, ' from ', file_diff
            (vardat, ntimes, dt) = load_wrfout(file_diff, [var])

            plotdat = vardat[var][:,:,0]

            nameadd = ''
            if (var == 'PREC'):
                (vardat, ntimes, dt) = load_wrfout(file_hist, [var])
                vardat_hist = vardat[var][:,:,0]
                nmodyears = get_nc_dim(file_hist, 'nmodyears')
                vardat_hist = (vardat_hist / nmodyears) * mm2in

                (vardat, ntimes, dt) = load_wrfout(file_fore, [var])
                vardat_fore = vardat[var][:,:,0]
                nmodyears = get_nc_dim(file_fore, 'nmodyears')
                vardat_fore = (vardat_fore / nmodyears) * mm2in

                #--- Do plot of percentage difference:
                plotdat = ((vardat_fore - vardat_hist) / \
                           ((vardat_fore + vardat_hist) / 2)) * 100
                levs_c = levs_pcp_percdiff
                cmap_c = cmap_pcp_percdiff
                var_long = 'Total Precipitation'
                cb_lab = 'Total Precip Percentage Difference (%)'
                stat_c = 'Percentage Difference'
                units_c = '%'
                nameadd = '_PERCDIFF'

#                #--- Do plot of actual difference:
#                plotdat = vardat_fore - vardat_hist
#                cb_lab = colorbar_labs[var]
#                if season == 'annual' or season == 'fall' or season == 'winter':
#                    levs_c = levs_pcp_diff_annual
#                    cmap_c = cmap_pcp_diff_annual
#                else:
#                    levs_c = levs_pcp_diff
#                    cmap_c = cmap_pcp_diff
#                var_long = 'Total Precipitation'
#                stat_c = 'Difference'
#                units_c = 'in.'
                
            elif var == 'T2MAX':
                cb_lab = colorbar_labs[var]
                levs_c = levs_temp_diff
                cmap_c = cmap_temp_diff
                var_long = 'Average Maximum Temperature'
                stat_c = 'Difference'
                units_c = '$^\circ$C'
            elif var == 'T2MIN':            
                cb_lab = colorbar_labs[var]
                levs_c = levs_temp_diff
                cmap_c = cmap_temp_diff
                var_long = 'Average Minimum Temperature'
                stat_c = 'Difference'
                units_c = '$^\circ$C'
            elif var == 'SPDUV10MAX':            
                diff_c = plotdat
                (vardat, ntimes, dt) = load_wrfout(file_hist, [var])
                vardat_hist = vardat[var][:,:,0]
                (vardat, ntimes, dt) = load_wrfout(file_fore, [var])
                vardat_fore = vardat[var][:,:,0]            
                plotdat = ((vardat_fore - vardat_hist) / \
                           ((vardat_fore + vardat_hist) / 2)) * 100
                levs_c = levs_spd_diff
                cmap_c = cmap_spd_diff
                var_long = 'Average Maximum Wind Speed'
                cb_lab = colorbar_labs[var]
                stat_c = 'Difference'
                units_c = 'm/s'

#            titlein = 'Ensemble Mean, ' + var_lab[var] + ', ' + \
#                      seasons_lab[season] + ', ' + yyyy_s + ' - ' + yyyy_e

            titlein =  seasons_lab[season] + ' ' + var_long + ' ' + stat_c + \
                      ' (' + units_c + ')' + \
                      ', (' + yyyy_s_fore + ' - ' + yyyy_e_fore + ') ' + \
                      ' - ' + \
                      '(' + yyyy_s_hist + ' - ' + yyyy_e_hist + ')'
            
            for z in range(len(zooms)):
                plotfname = rundir + '/map_diff_' + sdt_fore + '_' + \
                            edt_fore + '_' + season + '_' + var + nameadd + \
                            '_' + zooms[z] + '.png'
                mapper(var, lat, lon, plotdat, levs_c, cmap_c, \
                       maplw, cb_lab, titlein, plotfname, zooms[z])
            #sys.exit()
sys.exit()

<file_sep>/old/old_old/test_read_ghcnd_obs_20180411.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re
import numpy as np
#from make_cmap import *
#from netCDF4 import Dataset as NetCDFFile
#from utils_wrfout import *
#from utils_date import *
#import pickle

#from ghcndextractor import *
#from ghcndextractor import ghcndextractor
import codecs

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
plot_dir   = rcm_dir + '/plots'
pickle_dir = rcm_dir + '/pickle'
#ghcnFolder  = rcm_dir + '/obs'
ghcnd_dir   = rcm_dir + '/obs/ghcnd_all'
data_dir   = '/home/disk/r2d2/steed/cmip5/rcp8.5'

geo_em = '/home/disk/a125/steed/run/geo_em.d02.nc'

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
vars = ['PREC', 'T2MAX', 'T2MIN', 'SPDUV10MAX', 'SPDUV10MEAN', 'SPDUV10STD']
#sdt = '202001'
#edt = '202912'
sdt = '197001'
edt = '209912'

dtfmt = "%Y%m%d%H"

stns   = ['KSEA', 'KSMP', 'KYKM', 'KGEG', 'KPDX', 'KMFR', 'KHQM']
models = ['gfdl-cm3', 'miroc5']
stn_cols = ['b', 'r', 'g', 'c', 'm', 'k', 'grey']
mod_cols = ['b', 'r']
latpts = [47.44472, 47.27667, 46.56417, 47.62139, 45.59083, \
          42.38111, 46.97278]
lonpts = [-122.31361, -121.33722, -120.53361, -117.52778, -122.60028, \
          -122.87222, -123.93028]
elevs  = [130.0, 1207.0, 333.0, 735.0, 8.0, 405.0, 4.0]

#---------------------------------------------------------------------------
#
#---------------------------------------------------------------------------
stn_ghcnd = 'USW00024233'
file_c = ghcnd_dir + '/' + stn_ghcnd + '.dly'

if not os.path.isfile(file_c):
    print 'i do not see ', file_c
    sys.exit()

print 'i see ', file_c
readLoc = codecs.open(file_c, "r", "utf-8")
allLines = readLoc.readlines()
readLoc.close
    
for lineOfData in allLines:
    print lineOfData
    countryCode = lineOfData[0:2]
    stationID = lineOfData[0:11]
    stationMonthCode = lineOfData[0:17]
    year = lineOfData[11:15]
    month = lineOfData[15:17]
    element = lineOfData[17:21]

#    print 'year, month, element = ', year, month, element
    for x in range(0, 30):
        dayOM = x + 1
        offsetStart = (x*8)+21
        offsetEnd = offsetStart + 8
        ld = lineOfData[offsetStart:offsetEnd]
        val = ld[0:5]
        mflag = ld[5:6]
        qflag = ld[6:7]
        sflag = ld[7:8]

#        print '-----'
#        print 'ld = ', element, ld
#        print 'val = ', val
#        print 'mflag = ', mflag
#        print 'qflag = ', qflag
#        print 'sflag = ', sflag        

        if val == -9999.0:
            val = np.nan
        
        if element == 'TMAX' or element == 'TMIN':
            #--- Temperatures are in tenths of degrees C.
            valout = float(val) / 10.0
        elif element == 'PRCP':
            #--- Precip is in tenths of a mm.  Converting to inches.
            valout = (float(val) / 10.0) * 0.0393701
            
        if qflag and qflag.strip():
            print 'qflag is not empty, mflag, qflag, sflag = ', \
                  mflag, ',', qflag, ',', sflag
            valout = np.nan            

        print element, year, month, valout
#        sys.exit()
    
#    sys.exit()
        


#readLoc = codecs.open(file_c, "r", "utf-8")
#allLines = readLoc.readlines()
#readLoc.close
#
#for eachReadLine in allLines:
#    readRow(eachReadLine)


#ghcndextractor.countries = ["US"]
#ghcndextractor.states = ["NJ"]     
#ghcndextractor.ghcnFolder = "/Users/d035331/Documents/Demo/DemoData_Weather/ghcnd_all"   
#     
#ghcndextractor.getStationsFromFiles()
#stations = ghcndextractor.getCSVStationMetaData()
#ghcndextractor.readDailyFiles()
#dayCSV = ghcndextractor.getDailyDataCSV(["12"], ["25"], ["USW00014780"])
#print(dayCSV)
#
#
#
#
#
#stn_ghcnd = 'USW00024233'
#
##file_c = ghcnd_dir + '/' + stn_ghcnd + '.dly'
#
#stationIDCodes = ['USW00024233']
#iret = readDailyFiles()


sys.exit()

<file_sep>/old/old_old/utils_test.py
#!/usr/bin/python
import os, sys, glob, re
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, cm, shiftgrid

#from netCDF4 import Dataset as NetCDFFile
#from collections import defaultdict
#from utils_cmap import *
#import pickle
#from utils_date import *



def plot_it(data_all, dts_all, models, stns, var, stat, cols, \
            data_obs, dts_obs):

    mod = 'gfdl-cm3'
    var = 'T2MAX'
    stn = 'KSEA'
    stat = 'avg'
    plotfname = 'test.png'

    fig, ax = plt.subplots( figsize=(10,6) )
    lw_sm = 1.8

    for m in range(len(models)):
        ptot = []
        mod = models[m]
        for d in range(len(dts_all)):
            dt_c = dts_all[d]
            yyyy = dt_c[0:4]
            mm = dt_c[4:6]
            if (yyyy == '1999' and (mm >= '06' and mm <= '08')):
                key = (mod,var,stn,dt_c)
                if (key in data_all):
                    mod_c = data_all[key]
                    ptot.append(mod_c - 273.15)
        lab_c = mod.upper()
        plt.plot(ptot, label = lab_c, color = cols[m], linewidth=lw_sm)

    ptot = []
    for d in range(len(dts_obs)):
        dat_c = data_obs['TMAX','KSEA',dts_obs[d]]
        ptot.append(dat_c)
    lab_c = 'Obs'
    plt.plot(ptot, label = lab_c, color = 'black', linewidth=2.1)

    print 'xli ', plotfname, ' &'
    plt.savefig(plotfname)
    plt.close()

    return -9999
<file_sep>/igtg_check.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re
import numpy as np
from igtg_check_utils import *
import matplotlib.pyplot as plt
#import pickle
from make_cmap import *

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir  = '/home/disk/spock/jbaars/rcm'
py_dir   = rcm_dir + '/python'
plot_dir = rcm_dir + '/plots'
#data_dir = rcm_dir + '/igtg.nml.works.3d.4d'
data_dir = '/home/disk/mass/steed/cmip5/rcp4.5/access1.0/common/' \
           'igtg.nml.works.3d.tos'
#data_dir = '/home/disk/mass/steed/cmip5/rcp4.5/access1.0/common/testdata'

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
#varname = 'ua'
varname = 'tos'
#file_old = data_dir + '/' + varname + '.1990010106.1991010100.nc'
#file_new = data_dir + '/' + varname + '_new.nc'
file_old = data_dir + '/' + 'indata'
file_new = data_dir + '/' + 'outdata'

itime = 1
ilev  = 1

title_old = varname + ' OLD, itime = ' + str(itime) + ', ilev = ' + \
            str(ilev)
plotfname_old = './' + varname + '_old_itime' + str(itime) + '_ilev' + \
                str(ilev) + '.png'
title_new = varname + ' NEW, itime = ' + str(itime) + ', ilev = ' + \
            str(ilev)
plotfname_new = './' + varname + '_new_itime' + str(itime) + '_ilev' + \
                str(ilev) + '.png'

#minlat = 20.0
#maxlat = 50.0
#minlon = -120.0
#maxlon = -50.0

minlat = 20.0
maxlat = 70.0
minlon = -175.0
maxlon = -85.0

#---------------------------------------------------------------------------
# Make a color map using make_cmap.
#---------------------------------------------------------------------------
if (varname == 'tos'):
    levs_c = levs_tos
    cmap_c = cmap_tos
elif(varname == 'ua'):
    levs_c = levs_spd
    cmap_c = cmap_spd
        
levs_norm_c = []
for i in range(len(levs_c)):
    x = float(levs_c[i])
    norm_c = (x - min(levs_c)) / (max(levs_c) - min(levs_c))
    levs_norm_c.append(norm_c)

my_cmap = make_cmap(cmap_c, bit = True, position = levs_norm_c)

#---------------------------------------------------------------------------
# Load old and new files.
#---------------------------------------------------------------------------
(lat_old, lon_old, data_old) = load_nc(file_old, varname, itime, ilev)

print 'lat_old, lon_old, data_old = ', lat_old, lon_old, data_old

(lat_new, lon_new, data_new) = load_nc(file_new, varname, itime, ilev)

print 'lat_new, lon_new, data_new = ', lat_new, lon_new, data_new

#sys.exit()

        
#---------------------------------------------------------------------------
# Plot old data.
#---------------------------------------------------------------------------
mapper(lat_old, lon_old, data_old, minlat, maxlat, minlon, maxlon, \
       levs_c, my_cmap, maplw, colorbar_lab, title_old, plotfname_old)

mapper(lat_new, lon_new, data_new, minlat, maxlat, minlon, maxlon, \
       levs_c, my_cmap, maplw, colorbar_lab, title_new, plotfname_new)

sys.exit()

<file_sep>/old/old_old/areal_cdd_compare_20180524.py
#!/usr/bin/python
import os, sys, glob, re
import numpy as np
from netCDF4 import Dataset as NetCDFFile
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, cm, shiftgrid
import pickle
from utils_date import *
from utils_wrfout import *
import csv

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
plot_dir   = rcm_dir + '/plots'
pickle_dir = rcm_dir + '/pickle'
data_dir   = rcm_dir + '/data'
cdd_data   = data_dir + '/mask_pts.dat'
mask_file  = data_dir + '/mask_pts.dat'

geo_em    = '/home/disk/a125/steed/run/geo_em.d02.nc'

data_dirs = ['/home/disk/r2d2/steed/cmip5/rcp8.5', \
              '/home/disk/vader/steed/cmip5/rcp8.5', \
              '/home/disk/jabba/steed/cmip5/rcp8.5']

pickle_file = pickle_dir + '/T2MAX_slice.pickle'

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
sdt = '197001'
edt = '201712'
#edt = '198012'

#vars_mod = ['PREC', 'T2MAX', 'T2MIN']
vars_mod = ['T2MAX']
models   = ['gfdl-cm3', 'miroc5', 'bcc-csm1.1', 'access1.3']
mod_cols = ['b',        'r',      'g',          'y']

#---------------------------------------------------------------------------
# Read mask lat/lon points file.
#---------------------------------------------------------------------------
mask = open(mask_file)
csvReader = csv.reader(mask)
maski = []
maskj = []
for row in csvReader:
    maski.append(int(row[0]))
    maskj.append(int(row[1]))
mask.close()

#---------------------------------------------------------------------------
# Read Climate Division Data file.
#---------------------------------------------------------------------------
cdd_yyyy = {}
cdd_tmx_avg = {}
for s in range(len(seasons)):
    sm = seasons_mo[s]
    season = seasons[s]
    cdd_file = data_dir + '/cdd_' + sm + '_WA.dat'
    print 'reading ', cdd_file
    cdd = open(cdd_file)
    csvReader = csv.reader(cdd)
    yyyy = []
    tmx_avg = []
    for row in csvReader:
        (yyyy_c,val) = row[0].split()
        yyyy.append(int(yyyy_c))
        if float(val) < -100:
            tmx_avg.append(np.nan)
        else:
            tmx_avg.append(float(val))        
    cdd.close()
    cdd_yyyy[season] = yyyy
    cdd_tmx_avg[season] = tmx_avg
    
#---------------------------------------------------------------------------
# For each model, read in grids and keep "slice" of it as defined by
# mask_file read in above.
#---------------------------------------------------------------------------
#sums_all = {}
#cnts_all = {}
#years_all = []
#for m in range(len(models)):
#    mod = models[m]
#    files = get_extract_files(data_dirs, mod, sdt, edt)            
#    files = list(sorted(set(files)))
#    (seafiles, years) = get_seasonal_files_new(files)
#
#    years_all.append(years)
#
#    for y in range(len(years)):
#        yyyy = years[y]
#        print 'loading ', mod, yyyy
#        for s in range(len(seasons)):
#            season = seasons[s]
#            if (seafiles[season,yyyy] == seafiles[season,yyyy]):
#                (sums, cnts) = sum_wrfout_slice(seafiles[season,yyyy], \
#                                                vars_mod, maski, maskj)
#                for var in vars_mod:
#                    key = (mod,season,var,yyyy)
#                    #--- Winter of first year will only have Jan-Feb.  So
#                    #--- nan it out.
#                    if (y == 0 and season == 'winter'):
#                        sums_all[key] = np.nan
#                        cnts_all[key] = np.nan
#                    else:
#                        if (var in sums):
#                            sums_all[key] = sums[var]
#                            cnts_all[key] = cnts[var]
#
#years_all = list(sorted(set(years_all[0])))
#pickle.dump((sums_all,cnts_all,years_all), open(pickle_file,'wb'), -1)
#
#sys.exit()

if os.path.isfile(pickle_file):
    print 'reading pickle file ', pickle_file
    (sums_all,cnts_all,years_all) = pickle.load(open(pickle_file, 'rb'))

#---------------------------------------------------------------------------
# Make plot for each seasons.
#---------------------------------------------------------------------------
var = 'T2MAX'
stat = 'avg'

lw_sm = 1.8
smooth_fact = 5       #-- odd number, used for smoothing time series plots.

for s in range(len(seasons)):

    fig, ax = plt.subplots( figsize=(10,6) )

    season = seasons[s]
    cdd_yyyy_c = cdd_yyyy[season]

    test = np.asarray(cdd_tmx_avg[season])
    cdd_tmx_avg_c = (test - 32.0) * (5./9.)    

#    plt.plot(cdd_tmx_avg_c, alpha=0.4, label='Observed', color='black', \
#             linestyle='None', marker='o', markersize=3)
    lab_c = 'Observed ' + str(smooth_fact) + '-pt smoothing'
    plt.plot(smooth(cdd_tmx_avg_c, smooth_fact), label=lab_c, color='black', \
             linewidth=lw_sm)

    for m in range(len(models)):
        mod = models[m]
        modp = []
        xlabs = []
        for y in range(len(cdd_yyyy_c)):
            yyyy = cdd_yyyy_c[y]
            xlabs.append(yyyy)
            key = (mod,season,var,str(yyyy))
            if key in sums_all:
                tc = (sums_all[key] / cnts_all[key]) - 273.15
                tf = (tc * 9./5.) + 32.0
                modp.append(tc)                
            else:
                modp.append(np.nan)

#        plt.plot(modp, alpha = 0.17, label = mod.upper(), color = mod_cols[m], \
#                 linewidth=lw_sm)
        lab_c = mod.upper() + ', ' + str(smooth_fact) + '-pt smoothing'
        plt.plot(smooth(modp, smooth_fact), label = lab_c, color = mod_cols[m],\
                 linewidth=lw_sm)

    #--- y-axis labeling.
#    plt.ylim(ylims[var+stat])
    plt.ylabel('Seasonal ' + labels[var+stat], fontsize=fs+1)
    plt.tick_params(axis='y', which='major', labelsize=fs+1)    

    #--- x-axis labels.
    xticks_c = range(0,len(xlabs), 1)
    xlabs_c = [ xlabs[i] for i in xticks_c ]    
    plt.xticks(xticks_c)
    plt.tick_params(axis='x', which='major', labelsize=fs-1)
    ax.set_xticklabels(xlabs_c, rotation=90)        
    plt.xlabel('Year', fontsize=fs+1)

    titlein = 'Climate Division Data Areal Verification, WA state, ' + \
              seasons_lab[season] + ', ' + labels[var+stat] + ', ' + \
              sdt + ' - ' + edt
    plt.title(titlein, fontsize=titlefs, fontweight='bold')

    plt.tight_layout()
    plt.grid()
    plt.legend(fontsize = fs-1, loc = 'best')

    plotfname = plot_dir + '/areal_' + season + '.png'
    print 'xli ', plotfname, ' &'
    plt.savefig(plotfname)
    plt.close()


sys.exit()

<file_sep>/utils_stats.py
#!/usr/bin/python
import os, sys, glob, re, math
import numpy as np
from collections import defaultdict
import pickle
from utils_date import *
from utils_plot import *
from utils_ghcnd_obs import *

dtfmt = "%Y%m%d%H"

mm2in           = 0.0393700787
min_season_days = 85
std_lapse       = 0.0065  #-- std. atmos. lapse rate
mvc             = -9999.0

#---------------------------------------------------------------------------
# Load wrfout file.
#---------------------------------------------------------------------------
def sum_wrfout_slice(files, vars, maski, maskj):    
    sums = {}
    cnts = {}

    for f in range(len(files)):
        print 'reading ', files[f]
        nc = NetCDFFile(files[f], 'r')
        ntimes = len(nc.dimensions['Time'])

        for v in range(len(vars)):
            var = vars[v]
            dat_tmp = nc.variables[var][:,:,:]
            dat_tmp = np.transpose(dat_tmp)

            sum_c = 0
            cnt_c = 0
            for n in range(len(maski)):
                sum_c = sum_c + sum(dat_tmp[maski[n], maskj[n],:])
                cnt_c = cnt_c + len(dat_tmp[maski[n], maskj[n],:])

            if (var in sums):
                sums[var] = sums[var] + sum_c
                cnts[var] = cnts[var] + cnt_c
            else:
                sums[var] = sum_c
                cnts[var] = cnt_c

    return sums, cnts

#---------------------------------------------------------------------------
# For ensemble standard deviation plots....
#---------------------------------------------------------------------------
def get_ensmean_std(var, models, years, season, \
                    sum_files, spr_files, win_files, fal_files, ann_files, \
                    pickle_file):
    nyears = 0
    nmodels = len(models)
    files = []
    dts_all = []
    for y in range(len(years)):
        yyyy = years[y]

        for m in range(nmodels):
            model = models[m]
            sum_files_c = sum_files[model]
            spr_files_c = spr_files[model]
            win_files_c = win_files[model]
            fal_files_c = fal_files[model]
            ann_files_c = ann_files[model]

            if season == 'summer':
                if (sum_files_c[yyyy] == sum_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(sum_files_c[yyyy])):
                        files.append(sum_files_c[yyyy][f])
                    else:
                        continue
            elif season == 'spring':
                if (spr_files_c[yyyy] == spr_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(spr_files_c[yyyy])):
                        files.append(spr_files_c[yyyy][f])
                    else:
                        continue
            elif season == 'winter':
                if (win_files_c[yyyy] == win_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(win_files_c[yyyy])):
                        files.append(win_files_c[yyyy][f])
                    else:
                        continue
            elif season == 'fall':                        
                if (fal_files_c[yyyy] == fal_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(fal_files_c[yyyy])):
                        files.append(fal_files_c[yyyy][f])
                    else:
                        continue
            elif season == 'annual':
                if (ann_files_c[yyyy] == ann_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(ann_files_c[yyyy])):
                        files.append(ann_files_c[yyyy][f])
                    else:
                        continue

    print 'files = ', files
    
    #--- Get sorted, unique set of dates from files found above.
    nfiles = len(files)
    dts_all = []
    for f in range(nfiles):
        dt_c = re.findall(r'\.(\d{10})\.', files[f])[0]        
        dts_all.append(dt_c)
    dts_all = list(sorted(set(dts_all)))

    #--- For each unique date, read in files for each model.
    for d in range(len(dts_all)):

        #--- First make sure all members have a file.
        files_c = []
        print dts_all[d]
        for f in range(nfiles):
            dt_c = re.findall(r'\.(\d{10})\.', files[f])[0]
            if dt_c == dts_all[d]:
                files_c.append(files[f])
        if len(files_c) != nmodels:
            print 'missing member(s) for date = ', dts_all[d]
            sys.exit()

        #--- Now read in files for each member.
        for n in range(len(files_c)):
            print 'loading ', files_c[n]
            (vardat, ntimes, dt) = load_wrfout(files_c[n], [var])
            plotdat = vardat[var][:,:,:]
            nx,ny,nt = plotdat.shape
            if n == 0:
                dat_all = np.ones((nx, ny, nt, nmodels)) * np.nan
                stdevs_tmp = np.ones((nx, ny, nt)) * np.nan                
            if n == 0 and d == 0:
                stdevs = np.ones((nx, ny, nt)) * np.nan
            dat_all[:,:,:,n] = plotdat

        #--- For each ntimes (these should be monthly files, so ntimes are
        #--- days and will be ~30 days), calculate a standard deviation.
        for nt in range(ntimes):
            dat_c = np.squeeze(dat_all[:,:,nt,:])
            stdevs_tmp[:,:,nt] = np.std(dat_c, axis=2)

        #--- Add this month's standard deviation grids to our final array,
        #--- stdevs.
        if d == 0:
            stdevs = stdevs_tmp
        else:
            stdevs = np.concatenate((stdevs, stdevs_tmp), axis = 2)

    #--- Calculate the mean of the daily standard deviations.
    stdevmean = np.mean(stdevs, axis=2)
        
    #--- Create pickle file.
    pickle.dump((stdevmean), open(pickle_file,'wb'), -1)

    return stdevmean

#---------------------------------------------------------------------------
# For ensemble mean plots, get system nco command.
#---------------------------------------------------------------------------
def get_ensmean_syscom(file_plot, var, models, years, season, \
                       sum_files, spr_files, \
                       win_files, fal_files, ann_files, ncocom):
    syscom = ncocom
    nyears = 0
    for m in range(len(models)):
        model = models[m]
        sum_files_c = sum_files[model]
        spr_files_c = spr_files[model]
        win_files_c = win_files[model]
        fal_files_c = fal_files[model]
        ann_files_c = ann_files[model]
        for y in range(len(years)):
            yyyy = years[y]
            if season == 'summer':
                if (sum_files_c[yyyy] == sum_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(sum_files_c[yyyy])):
                        syscom = syscom + ' ' + sum_files_c[yyyy][f]
                    else:
                        continue
            elif season == 'spring':
                if (spr_files_c[yyyy] == spr_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(spr_files_c[yyyy])):
                        syscom = syscom + ' ' + spr_files_c[yyyy][f]
                    else:
                        continue
            elif season == 'winter':
                if (win_files_c[yyyy] == win_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(win_files_c[yyyy])):
                        syscom = syscom + ' ' + win_files_c[yyyy][f]
                    else:
                        continue
            elif season == 'fall':                        
                if (fal_files_c[yyyy] == fal_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(fal_files_c[yyyy])):
                        syscom = syscom + ' ' + fal_files_c[yyyy][f]
                    else:
                        continue
            elif season == 'annual':
                if (ann_files_c[yyyy] == ann_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(ann_files_c[yyyy])):
                        syscom = syscom + ' ' + ann_files_c[yyyy][f]
                    else:
                        continue

    return syscom, nyears

#---------------------------------------------------------------------------
# For ensemble mean plots, run system nco command.
#---------------------------------------------------------------------------
def run_ensmean_syscom(file_plot, var, ncocom, syscom, yyyy_s, yyyy_e, \
                       season, rundir, ):

    #--- If system command is too big (too many files to send
    #--- to nco), break it up into 1000 piece chunks.
    files_c = syscom.replace(ncocom, '')
    scsplit = files_c.split()
    nfiles = len(scsplit)

    niter = 1000
    if nfiles > niter:
        nt =  int(math.ceil(float(nfiles) / float(niter)))
        files_all = []
        for n in range(nt):
            file_c = rundir + '/' + var + '_' + season + '_' + \
                     yyyy_s + '_' + yyyy_e + '_subset' + str(n) + '.nc'
            if os.path.isfile(file_c):
                print 'this file already exists: ', file_c
                files_all.append(file_c)
                continue
            nb = n * niter
            ne = (n+1) * niter
            syscom = ncocom + ' ' + ' '.join(scsplit[nb:ne]) + ' ' + file_c
            print syscom
            iret = os.system(syscom)
            if iret != 0:
                print 'problem running nco! iret = ', iret
                return mvc
            files_all.append(file_c)
        syscom = ncocom + ' ' + ' '.join(files_all) + ' ' + file_plot
        print syscom                
        iret = os.system(syscom)
        if iret != 0:
            print 'problem running nco! iret = ', iret
            return mvc
    else:
        syscom = syscom + ' ' + file_plot
        print 'syscom = ', syscom
        iret = os.system(syscom)
        if iret != 0:
            print 'problem running nco! iret = ', iret
            return mvc

    return 1

#---------------------------------------------------------------------------
# Get seasonal stats (totals or averages currently).
#---------------------------------------------------------------------------
def get_seasonal_stats(data_all, dts_all, models, stns, var, stat):

    out_sum   = {}
    out_avg   = {}
    out_max   = {}
    out_min   = {}
    years_all = []
    
    for m in range(len(models)):
        mod = models[m]
        data_all_c = data_all[mod]
        for s in range(len(stns)):
            stn = stns[s]
            ndays = {}
            years_mod_stn = []
            for d in range(len(dts_all)):
                yyyy = dts_all[d][0:4]
                mm = dts_all[d][4:6]
                years_mod_stn.append(yyyy)

                #--- Very first date of all model runs will have unusable
                #--- (all 0's) in the fields.  So don't try and use it.
                if dts_all[d] == '1970010100':
                    continue

                if ((var,stn,dts_all[d]) in data_all_c):
                    dat_c = data_all_c[var,stn,dts_all[d]]
                else:
                    ndays[season+yyyy] = np.nan
                    out_sum[season,mod,stn,yyyy] = np.nan
                    out_max[season,mod,stn,yyyy] = np.nan
                    out_min[season,mod,stn,yyyy] = np.nan
                    continue

                #--- annual is all days so get sum/max/min for every dts_all.
                (ndays, out_sum, out_max, out_min) = \
                        get_season_summaxmin(dat_c, mod, stn, 'annual',yyyy,mm,\
                                             ndays, out_sum, out_max, out_min)

                if (mm == '03' or mm == '04' or mm == '05'):
                    season = 'spring'
                if (mm == '06' or mm == '07' or mm == '08'):
                    season = 'summer'
                if (mm == '09' or mm == '10' or mm == '11'):
                    season = 'fall'
                if (mm == '12' or mm == '01' or mm == '02'):
                    season = 'winter'
                (ndays, out_sum, out_max, out_min) = \
                        get_season_summaxmin(dat_c, mod, stn, season, yyyy, mm,\
                                             ndays, out_sum, out_max, out_min)
                        
            #--- Get list of unique, sorted years found above.
            years = list(sorted(set(years_mod_stn)))

            #--- Check number of data points per unique years and nan out
            #--- ones that do not have enough (< min_season_days).
            for y in range(len(years)):
                yyyy = years[y]
                for s in range(len(seasons)):
                    season = seasons[s]
                    if ((season+yyyy) in ndays):
                        if (ndays[season+yyyy] < min_season_days):
                            out_sum[season,mod,stn,yyyy] = np.nan
                            out_max[season,mod,stn,yyyy] = np.nan
                            out_min[season,mod,stn,yyyy] = np.nan
                    else:
                        ndays[season+yyyy] = np.nan
                        out_sum[season,mod,stn,yyyy] = np.nan
                        out_max[season,mod,stn,yyyy] = np.nan
                        out_min[season,mod,stn,yyyy] = np.nan

            #--- if stat requested was 'avg', do averaging.
            if (stat == 'avg' or stat == 'max' or stat == 'min'):
                for y in range(len(years)):
                    yyyy = years[y]
                    for s in range(len(seasons)):
                        season = seasons[s]
                        out_avg[season,mod,stn,yyyy] = out_sum[\
                            season,mod,stn,yyyy] / ndays[season+yyyy]
                
            #--- Keep around all years found for this model and station.
            years_all.append(years_mod_stn)

    #--- Final, unique, sorted list of years found.
    years = list(sorted(set(years_all[0])))            

    if (stat == 'avg'):
        return out_avg, years
    elif (stat == 'max'):
        return out_max, years
    elif (stat == 'min'):
        return out_min, years
    else:
        return out_sum, years

#---------------------------------------------------------------------------
# Interpolate from a sent-in grid to a sent in lat/lon point.
#---------------------------------------------------------------------------
def bilinear_interpolate(latgrid, longrid, datagrid, latpt, lonpt, deb):

    totdiff = abs(latgrid - latpt) + abs(longrid - lonpt)
    (i,j) = np.unravel_index(totdiff.argmin(), totdiff.shape)

    #--- Get lat/lon box in which latpt,lonpt resides.
    if (latpt >= latgrid[i,j] and lonpt >= longrid[i,j]):
        iif = i
        jjf = j
    elif (latpt < latgrid[i,j] and lonpt < longrid[i,j]):
        iif = i-1
        jjf = j-1
    elif (latpt >= latgrid[i,j] and lonpt < longrid[i,j]):
        iif = i-1
        jjf = j
    elif (latpt < latgrid[i,j] and lonpt >= longrid[i,j]):
        iif = i
        jjf = j-1

    (nx, ny) = np.shape(latgrid)

    if (deb == 1):
        print 'nx, ny  = ', nx, ny
        print 'iif,jjf = ', iif,jjf
        print 'latgrid[iif+1,jjf] = ', latgrid[iif+1,jjf]
        print 'latgrid[iif,jjf]   = ', latgrid[iif,jjf]

    if (iif >= (nx-1) or jjf >= (ny-1) or iif < 0 or jjf < 0):
        return mvc

    #--- Do bilinear interpolation to latpt,lonpt.
    dlat  = latgrid[iif,jjf+1] - latgrid[iif,jjf]
    dlon  = longrid[iif+1,jjf] - longrid[iif,jjf]
    dslat = latgrid[iif,jjf+1] - latpt
    dslon = longrid[iif+1,jjf] - lonpt

    wrgt = 1 - (dslon/dlon)
    wup  = 1 - (dslat/dlat)

    vll = datagrid[iif,jjf]
    vlr = datagrid[iif,jjf+1]
    vul = datagrid[iif+1,jjf]
    vur = datagrid[iif+1,jjf+1]

    if (deb > 1):
        print 'll lat, lon, val = ', latgrid[iif,jjf], longrid[iif,jjf], vll
        print 'lr lat, lon, val = ', latgrid[iif+1,jjf], longrid[iif+1,jjf], vlr
        print 'ur lat, lon, val = ', latgrid[iif+1,jjf+1], longrid[iif+1,jjf+1],vur
        print 'ul lat, lon, val = ', latgrid[iif,jjf+1], longrid[iif,jjf+1], vul
        print 'latpt, lonpt = ', latpt, lonpt
        
        print 'vll = ', vll
        print 'vlr = ', vlr
        print 'vul = ', vul
        print 'vur = ', vur

    datout = (1-wrgt) * ((1-wup) * vll + wup * vul) + \
        (wrgt) * ((1-wup) * vlr + wup * vur)

#    if (deb == 1):
#        print 'datout = ', datout
#        sys.exit()
#    if (datout == 0.0):
#        print 'nx, ny  = ', nx, ny
#        print 'iif,jjf = ', iif,jjf
#        print 'latgrid[iif+1,jjf] = ', latgrid[iif+1,jjf]
#        print 'latgrid[iif,jjf]   = ', latgrid[iif,jjf]
#        
#        print 'll lat, lon, val = ', latgrid[iif,jjf], longrid[iif,jjf], vll
#        print 'lr lat, lon, val = ', latgrid[iif+1,jjf], longrid[iif+1,jjf], vl#r
#        print 'ur lat, lon, val = ', latgrid[iif+1,jjf+1], longrid[iif+1,jjf+1],vur
#        print 'ul lat, lon, val = ', latgrid[iif,jjf+1], longrid[iif,jjf+1], vu#l
#        print 'latpt, lonpt = ', latpt, lonpt
#        
#        print 'vll = ', vll
#        print 'vlr = ', vlr
#        print 'vul = ', vul
#        print 'vur = ', vur
#
#        sys.exit()

    return datout

#----------------------------------------------------------------------------
# Add data to sums, max's and min's arrays.
#----------------------------------------------------------------------------
def get_season_summaxmin(dat_c, mod, stn, season, yyyyin, month, ndays, \
                         out_sum, out_max, out_min):

    #--- Look out for cases where data is NaN (e.g. the 31st of June) and
    #--- do nothing (return) in those cases.
    if np.isnan(dat_c):
        return ndays, out_sum, out_max, out_min
        
    #--- Put December winter data into next year's winter-- naming
    #--- winters by their Jan/Feb year rather than their Dec year.
    if month == '12':
        yyyy = str(int(yyyyin) + 1)
    else:
        yyyy = yyyyin
        
    #--- Count number of days for this season and year.
    if (season+yyyy) in ndays:
        ndays[season+yyyy] += 1
    else:
        ndays[season+yyyy] = 1

    key = (season,mod,stn,yyyy)

    #--- Get sum of sent in data.
    if key in out_sum:
        out_sum[key] = out_sum[key] + dat_c
    else:
        out_sum[key] = dat_c

    #--- Get maximum.
    if key in out_max:
        if dat_c > out_max[key]:
            out_max[key] = dat_c
    else:
        out_max[key] = dat_c
            
    #--- Get minimum.
    if key in out_min:
        if dat_c < out_min[key]:
            out_min[key] = dat_c
    else:
        out_min[key] = dat_c

    return ndays, out_sum, out_max, out_min
<file_sep>/old/old_old/igtg_check_utils_20161103.py
#!/usr/bin/python
import  os, os.path, time, glob, re, math
import sys, string, readline, h5py
from Scientific.IO.NetCDF import *
import numpy as np
#from datetime import datetime, timedelta, time
from datetime import datetime
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, cm, shiftgrid
#from rw_plotsettings import *
import pickle

colorbar_lab = 'm/s';
maplw = 0.8
#levs_spd = [ 0.0, 2.5, 5.0, 7.5, 10.0, 12.5, 15.0, 17.5, 20.0, 22.5,
#             25.0, 27.5, 30.0, 32.5, 35.0, 37.5 ]
levs_spd = [ -24, -21, -18, -15, -12, -9, -6, -3, 3, 6, 9, 12, 15, 18, \
             21, 24]
cmap_spd = [(255,255,255), \
            (192,192,192), \
            (128,128,128 ), \
            (0,255,255), \
            (32,178,170), \
            (0,255,0), \
            (0,128,0), \
            (255,0,204), \
            (199,21,133), \
            (0,0,255), \
            (0,0,128), \
            (255,255,0), \
            (255,204,17), \
            (255,69,0), \
            (0,0,0),\
            (255,255,255)]

#---------------------------------------------------------------------------
# Load geog file.
#---------------------------------------------------------------------------
def load_nc(filein, varname, itime, ilev):
    file = NetCDFFile(filein, 'r')
    lat_tmp  = file.variables['lat'][:]
    lon_tmp  = file.variables['lon'][:]
    data_all = file.variables[varname]
    fillval = data_all._FillValue[0]
    data_tmp = file.variables[varname][itime,ilev,:,:]
    file.close()

    #--- Convert from 0-to-360 longitudes to -180-to-180.
    ipos = np.where(lon_tmp >= 180.0)
    iwest = ipos[0]
    lon_tmp[iwest] = lon_tmp[iwest] - 360.0

    #--- transpose data array to be x/y (lon/lat).
    dataout = np.transpose(data_tmp)
    dataout[dataout == fillval] = np.NAN

    #--- Fill in 2-D lat / lon terr arrays.
    nx = len(lon_tmp)
    ny = len(lat_tmp)

    lonout = np.empty([nx,ny])
    latout = np.empty([nx,ny])
    for y in range(ny):
        latout[:,y] = lat_tmp[y]
    for x in range(nx):
        lonout[x,:] = lon_tmp[x]

    return latout, lonout, dataout

#---------------------------------------------------------------------------
# Mapper.
#---------------------------------------------------------------------------
def mapper(lat, lon, grid, minlat, maxlat, minlon, maxlon, \
           levs, cmap, maplw, colorbar_lab, title, plotfname):

    ur_lat = maxlat
    ur_lon = maxlon
    ll_lat = minlat
    ll_lon = minlon
    lat_ctr = ll_lat + ((ur_lat - ll_lat) * 0.5)
    lon_ctr = ll_lon + ((ur_lon - ll_lon) * 0.5)

    width   = 6.5
    height  = 6.5
    fs      = 9
    titlefs = 9

    fig = plt.figure(figsize=(width,height))
    # left, bottom, width, height:
    ax = fig.add_axes([0.00,0.05,0.99,0.91])

    #---
    map = Basemap(projection='ortho',lat_0=45,lon_0=-100,resolution='l')
#    map = Basemap(resolution='i',projection='lcc',\
#                  llcrnrlon= ll_lon, llcrnrlat=ll_lat,\
#                  urcrnrlon= ur_lon, urcrnrlat= ur_lat,\
#                  lat_0=lat_ctr,lon_0=lon_ctr,lat_1=(ur_lat - ll_lat))

    #--- Get lat and lon data in map's x/y coordinates.
    x,y = map(lon, lat)

    #--- Draw coastlines, country boundaries, fill continents.
    map.drawcoastlines(linewidth = maplw)
    map.drawstates(linewidth = maplw)
    map.drawcountries(linewidth = maplw)

    #--- Draw the edge of the map projection region (the projection limb)
    map.drawmapboundary(linewidth = maplw)

    #--- Draw lat/lon grid lines every 30 degrees.
    map.drawmeridians(np.arange(0, 360, 30), linewidth = maplw)
    map.drawparallels(np.arange(-90, 90, 30), linewidth = maplw)
        
    cs = plt.contourf(x, y, grid, levs, cmap=cmap)

    cbar = map.colorbar(cs, location='bottom', pad="3%", size=0.1, ticks=levs)
    cbar.set_label(colorbar_lab, fontsize=fs, size=fs-1)
    cbar.ax.tick_params(labelsize=fs-1)

    plt.title(title, fontsize=titlefs, fontweight='bold')

    #--- Save plot.
    print 'creating ', plotfname
    plt.savefig(plotfname)

    plt.close()

<file_sep>/old/old_old/areal_cdd_compare_20180523.py
#!/usr/bin/python
import os, sys, glob, re
import numpy as np
from netCDF4 import Dataset as NetCDFFile
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, cm, shiftgrid
import pickle
from utils_date import *
from utils_wrfout import *
import csv

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
plot_dir   = rcm_dir + '/plots'
pickle_dir = rcm_dir + '/pickle'
data_dir   = rcm_dir + '/data'
cdd_data   = data_dir + '/mask_pts.dat'
mask_file  = data_dir + '/mask_pts.dat'

geo_em    = '/home/disk/a125/steed/run/geo_em.d02.nc'

data_dirs = ['/home/disk/r2d2/steed/cmip5/rcp8.5', \
              '/home/disk/vader/steed/cmip5/rcp8.5', \
              '/home/disk/jabba/steed/cmip5/rcp8.5']

pickle_file = pickle_dir + '/T2MAX_slice.pickle'

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
sdt = '197001'
#edt = '201712'
edt = '197512'

#vars_mod = ['PREC', 'T2MAX', 'T2MIN']
vars_mod = ['T2MAX']
models   = ['gfdl-cm3', 'miroc5', 'bcc-csm1.1', 'access1.3']
mod_cols = ['b',        'r',      'g',          'y']

#---------------------------------------------------------------------------
# Read mask lat/lon points file.
#---------------------------------------------------------------------------
mask = open(mask_file)
csvReader = csv.reader(mask)
maski = []
maskj = []
for row in csvReader:
    maski.append(int(row[0]))
    maskj.append(int(row[1]))
mask.close()

#---------------------------------------------------------------------------
# Read Climate Division Data file.
#---------------------------------------------------------------------------
cdd_yyyy = {}
cdd_tmx_avg = {}
for s in range(len(seasons)):
    sm = seasons_mo[s]
    season = seasons[s]
    cdd_file = data_dir + '/cdd_' + sm + '_WA.dat'
    cdd = open(cdd_file)
    csvReader = csv.reader(cdd)
    yyyy = []
    tmx_avg = []
    for row in csvReader:
        (yyyy_c,val) = row[0].split()
        yyyy.append(int(yyyy_c))
        tmx_avg.append(float(val))
    cdd.close()
    cdd_yyyy[season] = yyyy
    cdd_tmx_avg[season] = tmx_avg
    
##---------------------------------------------------------------------------
## Load geo_em file.
##---------------------------------------------------------------------------
#print 'Loading ', geo_em
#nc = NetCDFFile(geo_em, 'r')
#lat = nc.variables['XLAT_M'][0,:,:]
#lon = nc.variables['XLONG_M'][0,:,:]
#hgt = nc.variables['HGT_M'][0,:,:]    
#lat = np.transpose(lat)
#lon = np.transpose(lon)
#hgt = np.transpose(hgt)
#nc.close()

#---------------------------------------------------------------------------
#
#---------------------------------------------------------------------------
sums_all = {}
cnts_all = {}
years_all = []
for m in range(len(models)):
    mod = models[m]

    print 'Loading slice for model ', mod

    files = get_extract_files(data_dirs, mod, sdt, edt)            
    files = list(sorted(set(files)))

    (seafiles, years) = get_seasonal_files_new(files)

    years_all.append(years)

    for y in range(len(years)):
        yyyy = years[y]
        print 'loading year ', yyyy
        for s in range(len(seasons)):
            season = seasons[s]
            if (seafiles[season,yyyy] == seafiles[season,yyyy]):
                (sums, cnts) = sum_wrfout_slice(seafiles[season,yyyy], \
                                                vars_mod, maski, maskj)
                for var in vars_mod:
                    key = (mod,season,var,yyyy)
                    if (var in sums):
                        sums_all[key] = sums[var]
                        cnts_all[key] = cnts[var]

years_all = list(sorted(set(years_all[0])))
pickle.dump((sums_all,cnts_all,years_all), open(pickle_file,'wb'), -1)

sys.exit()

#---------------------------------------------------------------------------
#
#---------------------------------------------------------------------------
var = 'T2MAX'
stat = 'avg'
fig, ax = plt.subplots( figsize=(10,6) )
lw_sm = 1.8
smooth_fact     = 5       #-- odd number, used for smoothing time series plots.

for s in range(len(seasons)):
    season = seasons[s]
    
    cdd_yyyy_c = cdd_yyyy[season]
    cdd_tmx_avg_c = (cdd_tmx_avg[season] - 32.0) * (5./9.)

    plt.plot(cdd_tmx_avg_c, alpha=0.6, label='Observed', color='black', \
             linestyle='None', marker='o', markersize=3)
    lab_c = 'Observed ' + str(smooth_fact) + '-pt smoothing'
    plt.plot(smooth(cdd_tmx_avg_c, smooth_fact), label=lab_c, color='black', \
             linewidth=lw_sm)

    for m in range(len(models)):
        mod = models[m]
        modp = []
        xlabs = []
        for y in range(len(cdd_yyyy_c)):
            yyyy = cdd_yyyy_c[y]
            xlabs.append(yyyy)
            key = (mod,season,var,yyyy)
            if key in sums_all:
                tc = sums_all[key] / cnts_all[key]
                tf = (tc * 9./5.) + 32.0
                modp.append(tf)
            else:
                modp.append(np.nan)

        plt.plot(modp, alpha = 0.25, label = mod.upper(), color = mod_cols[m], \
                 linewidth=lw_sm)
        lab_c = mod.upper() + ', ' + str(smooth_fact) + '-pt smoothing'
        plt.plot(smooth(modp, smooth_fact), label = lab_c, color = mod_cols[m],\
                 linewidth=lw_sm)

    #--- y-axis labeling.
    plt.ylim(ylims[15,90])
    plt.ylabel('Seasonal ' + labels[var+stat], fontsize=fs+1)
    plt.tick_params(axis='y', which='major', labelsize=fs+1)    

    #--- x-axis labels.
    xticks_c = range(0,len(xlabs), 4)
    xlabs_c = [ xlabs[i] for i in xticks_c ]    
    plt.xticks(xticks_c)
    plt.tick_params(axis='x', which='major', labelsize=fs-1)
    ax.set_xticklabels(xlabs_c, rotation=90)        
    plt.xlabel('Year', fontsize=fs+1)

    titlein = 'test'
    plt.title(titlein, fontsize=titlefs, fontweight='bold')

    plt.tight_layout()
    plt.grid()
    plt.legend(fontsize = fs-1, loc = 'best')

    plotfname = './test.png'
    print 'xli ', plotfname, ' &'
    plt.savefig(plotfname)
    plt.close()


    sys.exit()

'''
    #--- Loop over year...
    for y in range(len(years)):
#        sums = {}
#        cnts = {}
        yyyy = years[y]

        print 'loading year ', yyyy
        
        if (win_files[yyyy] == win_files[yyyy]):
            season = 'winter'
#            (sums, cnts) = sum_wrfout_slice(win_files[yyyy], vars_mod, \
#                                            maski, maskj, sums, cnts)
            (sums, cnts) = sum_wrfout_slice(win_files[yyyy], vars_mod, \
                                            maski, maskj)
            for var in vars_mod:
                key = (mod,season,var,yyyy)
                sums_all[key] = sums[var]
                cnts_all[key] = cnts[var]
        if (spr_files[yyyy] == spr_files[yyyy]):
            season = 'spring'
#            (sums, cnts) = sum_wrfout_slice(spr_files[yyyy], vars_mod, \
#                                            maski, maskj, sums, cnts)
            (sums, cnts) = sum_wrfout_slice(spr_files[yyyy], vars_mod, \
                                            maski, maskj)
            for var in vars_mod:
                key = (mod,season,var,yyyy)
                sums_all[key] = sums[var]
                cnts_all[key] = cnts[var]
        if (sum_files[yyyy] == sum_files[yyyy]):
            season = 'summer'
#            (sums, cnts) = sum_wrfout_slice(sum_files[yyyy], vars_mod, \
#                                            maski, maskj, sums, cnts)
            (sums, cnts) = sum_wrfout_slice(sum_files[yyyy], vars_mod, \
                                            maski, maskj)
            for var in vars_mod:
                key = (mod,season,var,yyyy)
                sums_all[key] = sums[var]
                cnts_all[key] = cnts[var]
        if (fal_files[yyyy] == fal_files[yyyy]):
            season = 'fall'
#            (sums, cnts) = sum_wrfout_slice(fal_files[yyyy], vars_mod, \
#                                            maski, maskj, sums, cnts)
            (sums, cnts) = sum_wrfout_slice(fal_files[yyyy], vars_mod, \
                                            maski, maskj)
            for var in vars_mod:
                key = (mod,season,var,yyyy)
                sums_all[key] = sums[var]
                cnts_all[key] = cnts[var]

years_all = list(sorted(set(years_all[0])))
pickle.dump((sums_all,cnts_all,years_all), open(pickle_file,'wb'), -1)
sys.exit()
'''
            
#---------------------------------------------------------------------------
#
#---------------------------------------------------------------------------
(sums_all,cnts_all,years_all) = pickle.load(open(pickle_file, 'rb'))

fig, ax = plt.subplots( figsize=(10,6) )

plotfname = './test.png'

season = 'summer'
var = 'T2MAX'
for m in range(len(models)):
    mod = models[m]
    vals = []
    for yyyy in years_all:
        key = (mod,season,var,yyyy)
        if key in sums_all:
            avg_c = sums_all[key] / float(cnts_all[key])
            if (var == 'PREC'):
                avg_pr = avg_c * mm2in
            elif (var == 'T2MAX' or var == 'T2MIN'):
                avg_pr = ((avg_c - 273.15) * (9./5.)) + 32.0
                print mod, yyyy, season, var, avg_pr
        else:
            avg_pr = np.nan
        vals.append(avg_pr)

    plt.plot(vals, label = mod.upper(), color = mod_cols[m], linewidth=2)

#    sys.exit()

plt.tight_layout()
plt.grid()
plt.legend(fontsize = fs-1, loc = 'best')

print 'xli ', plotfname, ' &'
plt.savefig(plotfname)
plt.close()

sys.exit()

<file_sep>/old/old_old/plot_wrfout_grid_avg_20181030.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re, math
import numpy as np
#import matplotlib.pyplot as plt
from make_cmap import *
from netCDF4 import Dataset as NetCDFFile
from utils_wrfout import *
from utils_date import *
import pickle
from collections import defaultdict
from make_cmap import *

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
plot_dir   = rcm_dir + '/plots'
pickle_dir = rcm_dir + '/pickle'
run_dirs   = rcm_dir + '/rundirs'
geo_em     = rcm_dir + '/data' + '/geo_em.d02.nc'
data_dirs  = ['/home/disk/r2d2/steed/cmip5/rcp8.5', \
              '/home/disk/vader/steed/cmip5/rcp8.5', \
              '/home/disk/jabba/steed/cmip5/rcp8.5']

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
#vars = ['PREC', 'T2MAX', 'T2MIN', 'SPDUV10MAX', 'SPDUV10MEAN', 'SPDUV10STD']
vars = ['PREC', 'T2MAX', 'T2MIN']
#vars = ['T2MAX', 'T2MIN']
#vars = ['SWDOWN']
#vars = ['PREC']

sdts = ['197001', '207001']
edts = ['200012', '209912']
#sdts = ['207001']
#edts = ['209912']

#models   = ['gfdl-cm3', 'miroc5', 'bcc-csm1.1', 'access1.3', 'canesm2',
#            'noresm1-m', 'ccsm4']
#models = ['ccsm4']
models = ['csiro-mk3.6.0', 'fgoals-g2']

dtfmt = "%Y%m%d%H"

#---------------------------------------------------------------------------
# Make a color map using make_cmap.
#---------------------------------------------------------------------------
levs_norm_c = []
for i in range(len(levs_temp)):
    x = float(levs_temp[i])
    norm_c = (x - min(levs_temp)) / (max(levs_temp) - min(levs_temp))
    levs_norm_c.append(norm_c)
cmap_temp = make_cmap(cmap_temp, bit = True, position = levs_norm_c)

levs_norm_c = []
for i in range(len(levs_pcp)):
    x = float(levs_pcp[i])
    norm_c = (x - min(levs_pcp)) / (max(levs_pcp) - min(levs_pcp))
    levs_norm_c.append(norm_c)
cmap_pcp = make_cmap(cmap_pcp, bit = True, position = levs_norm_c)

#---------------------------------------------------------------------------
# Make run directory, load geo_em file.
#---------------------------------------------------------------------------
rundir = run_dirs + '/plot_wrfout_grid_avg.' + str(os.getpid())
print 'making ', rundir
os.makedirs(rundir)

print 'Loading ', geo_em
(lat, lon, hgt) = load_geo_em(geo_em)

#---------------------------------------------------------------------------
# For each model, make a maps of every season...
#---------------------------------------------------------------------------
for m in range(len(models)):
    model = models[m]
    for n in range(len(sdts)):
        sdt = sdts[n]
        edt = edts[n]
        yyyy_s = sdt[0:4]
        yyyy_e = edt[0:4]        

        #--- Get list of extracted files for this model.
        files = get_extract_files(data_dirs, model, sdt, edt, 0)

        #--- Get files broken out by year and season.
        (spr_files, sum_files, fal_files, win_files, years) = \
                    get_seasonal_files(files)
       
        for var in vars:
            for season in seasons:
                file_plot = rundir + '/' + model + '_' + var + '_' + season + \
                            '_' + yyyy_s + '_' + yyyy_e + '.nc'

                if (var == 'T2MAX' or var == 'T2MIN'):
                    syscom = 'ncra'
                elif (var == 'PREC'):
                    syscom = 'ncra -y ttl'

                nyears = 0
                for y in range(len(years)):
                    yyyy = years[y]
                    if season == 'summer':
                        if (sum_files[yyyy] == sum_files[yyyy]):
                            nyears += 1
                            for f in range(len(sum_files[yyyy])):
                                syscom = syscom + ' ' + sum_files[yyyy][f]
                            else:
                                continue
                    elif season == 'spring':
                        if (spr_files[yyyy] == spr_files[yyyy]):
                            nyears += 1
                            for f in range(len(spr_files[yyyy])):
                                syscom = syscom + ' ' + spr_files[yyyy][f]
                            else:
                                continue
                    elif season == 'winter':
                        if (win_files[yyyy] == win_files[yyyy]):
                            nyears += 1
                            for f in range(len(win_files[yyyy])):
                                syscom = syscom + ' ' + win_files[yyyy][f]
                            else:
                                continue
                    elif season == 'fall':                        
                        if (fal_files[yyyy] == fal_files[yyyy]):
                            nyears += 1
                            for f in range(len(fal_files[yyyy])):
                                syscom = syscom + ' ' + fal_files[yyyy][f]
                            else:
                                continue
                        
                syscom = syscom + ' ' + file_plot
                print 'syscom = ', syscom        
                os.system(syscom)
        
                print 'Loading ', var, ' from ', file_plot
                (vardat, ntimes, dt) = load_wrfout(file_plot, [var])
        
                titlein = model.upper() + ', ' + var_lab[var] + ', ' + \
                          seasons_lab[season] + ', ' + yyyy_s + ' - ' + yyyy_e
        
                plotdat = vardat[var][:,:,0]
                if (var == 'PREC'):
                    plotdat = (plotdat / nyears) * mm2in
                    levs_c = levs_pcp
                    cmap_c = cmap_pcp
                else:
                    levs_c = levs_temp
                    cmap_c = cmap_temp
        
                for z in range(len(zooms)):
                    plotfname = rundir + '/' + model + '_' + sdt + '_' + \
                                edt + '_' + season + '_' + var + '_' + \
                                zooms[z] + '.png'
                    mapper(var, lat, lon, plotdat, levs_c, cmap_c, \
                           maplw, colorbar_labs[var], titlein, plotfname, \
                           zooms[z])
#            sys.exit()


#    sys.exit()
       
#    if (var == 'T2MAX' or var == 'T2MIN'):
#        if (os.path.exists(file_plot)):
#            print 'nc file ', file_plot, ' already exists-- using it'
#        else:
#            syscom = 'ncra'
#            for f in range(len(files[yyyy])):
#                print yyyy, files[yyyy][f]
#                syscom = syscom + ' ' + files[yyyy][f]
#            syscom = syscom + ' ' + file_plot
#            print 'syscom = ', syscom        
#            os.system(syscom)
#    elif (var == 'PREC'):
#        if (os.path.exists(file_plot)):
#            print 'nc file ', file_plot, ' already exists-- using it'
#        else:
#            syscom = 'ncra -y ttl'
#            for f in range(len(files[yyyy])):
#                print yyyy, files[yyyy][f]
#                syscom = syscom + ' ' + files[yyyy][f]
#            syscom = syscom + ' ' + file_plot
#            print 'syscom = ', syscom        
#            os.system(syscom)


#    #--- Loop over year, making maps of each 
#    for y in range(len(years)):
#        yyyy = years[y]
#
#        #--- Winter plots.
#        if (win_files[yyyy] == win_files[yyyy]):
#            iret = run_mapper('T2MAX', model, rundir, 'winter', win_files,\
#                              yyyy, lat, lon, levs_temp, cmap_temp)
#            iret = run_mapper('T2MIN', model, rundir, 'winter', win_files,\
#                              yyyy, lat, lon, levs_temp, cmap_temp)
#            iret = run_mapper('PREC', model, rundir, 'winter', win_files,\
#                              yyyy, lat, lon, levs_pcp, cmap_pcp)
#
#        #--- Summer plots.
#        if (sum_files[yyyy] == sum_files[yyyy]):
#            iret = run_mapper('T2MAX', model, rundir, 'summer', sum_files,\
#                              yyyy, lat, lon, levs_temp, cmap_temp)
#            iret = run_mapper('T2MIN', model, rundir, 'summer', sum_files,\
#                              yyyy, lat, lon, levs_temp, cmap_temp)
#            iret = run_mapper('PREC', model, rundir, 'summer', sum_files,\
#                              yyyy, lat, lon, levs_pcp, cmap_pcp)
#
#        #--- Spring plots.
#        if (spr_files[yyyy] == spr_files[yyyy]):
#            iret = run_mapper('T2MAX', model, rundir, 'spring', spr_files,\
#                              yyyy, lat, lon, levs_temp, cmap_temp)
#            iret = run_mapper('T2MIN', model, rundir, 'spring', spr_files,\
#                              yyyy, lat, lon, levs_temp, cmap_temp)
#            iret = run_mapper('PREC', model, rundir, 'spring', spr_files,\
#                              yyyy, lat, lon, levs_pcp, cmap_pcp)
#
#        #--- Fall plots.
#        if (fal_files[yyyy] == fal_files[yyyy]):
#            iret = run_mapper('T2MAX', model, rundir, 'fall', fal_files,\
#                              yyyy, lat, lon, levs_temp, cmap_temp)
#            iret = run_mapper('T2MIN', model, rundir, 'fall', fal_files,\
#                              yyyy, lat, lon, levs_temp, cmap_temp)
#            iret = run_mapper('PREC', model, rundir, 'fall', fal_files,\
#                              yyyy, lat, lon, levs_pcp, cmap_pcp)



sys.exit()

<file_sep>/utils_wrfout.py
#!/usr/bin/python
import os, sys, glob, re, math
import numpy as np
from netCDF4 import Dataset as NetCDFFile
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, cm, shiftgrid
from collections import defaultdict
from utils_cmap import *
import pickle
from utils_date import *
from utils_ghcnd_obs import *

dtfmt = "%Y%m%d%H"

#seasons    = ['winter']
#seasons    = ['annual', 'spring', 'summer', 'fall', 'winter']
seasons    = ['spring', 'summer', 'fall', 'winter', 'annual']
seasons_mo = ['ALL', 'MAM', 'JJA', 'SON', 'DJF']
seasons_lab = {
    'annual': 'Jan-Dec',    
    'spring': 'Mar-Apr-May',
    'summer': 'Jun-Jul-Aug',
    'fall':   'Sep-Oct-Nov',
    'winter': 'Dec-Jan-Feb'
    }

zooms = ['Z1', 'Z2']

mm2in           = 0.0393700787
min_season_days = 85
std_lapse       = 0.0065  #-- std. atmos. lapse rate
#smooth_fact     = 5       #-- odd number, used for smoothing time series plots.
smooth_fact     = 1       #-- odd number, used for smoothing time series plots.

fs      = 9
titlefs = 9
width   = 10
height  = 8
maplw   = 1.0
mvc     = -9999.0

#---------------------------------------------------------------------------
# Get model pickle file name
#---------------------------------------------------------------------------
def get_mod_pkl_name (pdir, model, sdt, edt):
    pf = pdir + '/extract_' + model + '_' + sdt + '_' + edt + '.pkl'
    return pf

#---------------------------------------------------------------------------
# Load geo_em file.
#---------------------------------------------------------------------------
def load_geo_em(geo_em):
    nc = NetCDFFile(geo_em, 'r')
    lat = nc.variables['XLAT_M'][0,:,:]
    lon = nc.variables['XLONG_M'][0,:,:]
    hgt = nc.variables['HGT_M'][0,:,:]    
    lat = np.transpose(lat)
    lon = np.transpose(lon)
    hgt = np.transpose(hgt)
    nc.close()
    return lat, lon, hgt

#---------------------------------------------------------------------------
# Get netcdf dimension
#---------------------------------------------------------------------------
def get_nc_dim(ncin, dimname):
    nc = NetCDFFile(ncin, 'r')
    dim = len(nc.dimensions[dimname])
    nc.close()
    return dim

#---------------------------------------------------------------------------
# Load wrfout file.
#---------------------------------------------------------------------------
def load_wrfout(wrfout, vars):
    nc = NetCDFFile(wrfout, 'r')
    ntimes = len(nc.dimensions['Time'])

    dataout = {}
    for v in range(len(vars)):
        var = vars[v]
        dat_tmp = nc.variables[var][:,:,:]
        dat_tmp = np.transpose(dat_tmp)
        dataout[var] = dat_tmp

    #--- Get date from global attributes and clean it up and trim it down.
    dt_all = nc.START_DATE
    dt = dt_all.replace('-', '')
    dt = dt.replace(':', '')
    dt = dt.replace('_', '')        
    dt = dt[0:10]
    nc.close()
    return dataout, ntimes, dt

#---------------------------------------------------------------------------
# Load SWE wrfout file.
#---------------------------------------------------------------------------
def load_wrfout_swe(wrfout, vars):
    nc = NetCDFFile(wrfout, 'r')
    ntimes = len(nc.dimensions['Time'])

    dataout = {}
    for v in range(len(vars)):
        var = vars[v]
        dat_tmp = nc.variables[var][:,:,:]
        dat_tmp = np.transpose(dat_tmp)
        dataout[var] = dat_tmp
    nc.close()
    return dataout, ntimes

#---------------------------------------------------------------------------
# Load wrfout file.
#---------------------------------------------------------------------------
def sum_wrfout_slice(files, vars, maski, maskj):    
    sums = {}
    cnts = {}

    for f in range(len(files)):
        print 'reading ', files[f]
        nc = NetCDFFile(files[f], 'r')
        ntimes = len(nc.dimensions['Time'])

        for v in range(len(vars)):
            var = vars[v]
            dat_tmp = nc.variables[var][:,:,:]
            dat_tmp = np.transpose(dat_tmp)

            sum_c = 0
            cnt_c = 0
            for n in range(len(maski)):
                sum_c = sum_c + sum(dat_tmp[maski[n], maskj[n],:])
                cnt_c = cnt_c + len(dat_tmp[maski[n], maskj[n],:])

            if (var in sums):
                sums[var] = sums[var] + sum_c
                cnts[var] = cnts[var] + cnt_c
            else:
                sums[var] = sum_c
                cnts[var] = cnt_c

    return sums, cnts

#---------------------------------------------------------------------------
# For ensemble standard deviation plots....
#---------------------------------------------------------------------------
def get_ensmean_std(var, models, years, season, \
                    sum_files, spr_files, win_files, fal_files, ann_files, \
                    pickle_file):
    nyears = 0
    nmodels = len(models)
    files = []
    dts_all = []
    for y in range(len(years)):
        yyyy = years[y]

        for m in range(nmodels):
            model = models[m]
            sum_files_c = sum_files[model]
            spr_files_c = spr_files[model]
            win_files_c = win_files[model]
            fal_files_c = fal_files[model]
            ann_files_c = ann_files[model]

            if season == 'summer':
                if (sum_files_c[yyyy] == sum_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(sum_files_c[yyyy])):
                        files.append(sum_files_c[yyyy][f])
                    else:
                        continue
            elif season == 'spring':
                if (spr_files_c[yyyy] == spr_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(spr_files_c[yyyy])):
                        files.append(spr_files_c[yyyy][f])
                    else:
                        continue
            elif season == 'winter':
                if (win_files_c[yyyy] == win_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(win_files_c[yyyy])):
                        files.append(win_files_c[yyyy][f])
                    else:
                        continue
            elif season == 'fall':                        
                if (fal_files_c[yyyy] == fal_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(fal_files_c[yyyy])):
                        files.append(fal_files_c[yyyy][f])
                    else:
                        continue
            elif season == 'annual':
                if (ann_files_c[yyyy] == ann_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(ann_files_c[yyyy])):
                        files.append(ann_files_c[yyyy][f])
                    else:
                        continue

    print 'files = ', files
    
    #--- Get sorted, unique set of dates from files found above.
    nfiles = len(files)
    dts_all = []
    for f in range(nfiles):
        dt_c = re.findall(r'\.(\d{10})\.', files[f])[0]        
        dts_all.append(dt_c)
    dts_all = list(sorted(set(dts_all)))

    #--- For each unique date, read in files for each model.
    for d in range(len(dts_all)):

        #--- First make sure all members have a file.
        files_c = []
        print dts_all[d]
        for f in range(nfiles):
            dt_c = re.findall(r'\.(\d{10})\.', files[f])[0]
            if dt_c == dts_all[d]:
                files_c.append(files[f])
        if len(files_c) != nmodels:
            print 'missing member(s) for date = ', dts_all[d]
            sys.exit()

        #--- Now read in files for each member.
        for n in range(len(files_c)):
            print 'loading ', files_c[n]
            (vardat, ntimes, dt) = load_wrfout(files_c[n], [var])
            plotdat = vardat[var][:,:,:]
            nx,ny,nt = plotdat.shape
            if n == 0:
                dat_all = np.ones((nx, ny, nt, nmodels)) * np.nan
                stdevs_tmp = np.ones((nx, ny, nt)) * np.nan                
            if n == 0 and d == 0:
                stdevs = np.ones((nx, ny, nt)) * np.nan
            dat_all[:,:,:,n] = plotdat

        #--- For each ntimes (these should be monthly files, so ntimes are
        #--- days and will be ~30 days), calculate a standard deviation.
        for nt in range(ntimes):
            dat_c = np.squeeze(dat_all[:,:,nt,:])
            stdevs_tmp[:,:,nt] = np.std(dat_c, axis=2)

        #--- Add this month's standard deviation grids to our final array,
        #--- stdevs.
        if d == 0:
            stdevs = stdevs_tmp
        else:
            stdevs = np.concatenate((stdevs, stdevs_tmp), axis = 2)

    #--- Calculate the mean of the daily standard deviations.
    stdevmean = np.mean(stdevs, axis=2)
        
    #--- Create pickle file.
    pickle.dump((stdevmean), open(pickle_file,'wb'), -1)

    return stdevmean

#---------------------------------------------------------------------------
# For ensemble mean plots, get system nco command.
#---------------------------------------------------------------------------
def get_ensmean_syscom(file_plot, var, models, years, season, \
                       sum_files, spr_files, \
                       win_files, fal_files, ann_files, ncocom):
    syscom = ncocom
    nyears = 0
    for m in range(len(models)):
        model = models[m]
        sum_files_c = sum_files[model]
        spr_files_c = spr_files[model]
        win_files_c = win_files[model]
        fal_files_c = fal_files[model]
        ann_files_c = ann_files[model]
        for y in range(len(years)):
            yyyy = years[y]
            if season == 'summer':
                if (sum_files_c[yyyy] == sum_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(sum_files_c[yyyy])):
                        syscom = syscom + ' ' + sum_files_c[yyyy][f]
                    else:
                        continue
            elif season == 'spring':
                if (spr_files_c[yyyy] == spr_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(spr_files_c[yyyy])):
                        syscom = syscom + ' ' + spr_files_c[yyyy][f]
                    else:
                        continue
            elif season == 'winter':
                if (win_files_c[yyyy] == win_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(win_files_c[yyyy])):
                        syscom = syscom + ' ' + win_files_c[yyyy][f]
                    else:
                        continue
            elif season == 'fall':                        
                if (fal_files_c[yyyy] == fal_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(fal_files_c[yyyy])):
                        syscom = syscom + ' ' + fal_files_c[yyyy][f]
                    else:
                        continue
            elif season == 'annual':
                if (ann_files_c[yyyy] == ann_files_c[yyyy]):
                    nyears += 1
                    for f in range(len(ann_files_c[yyyy])):
                        syscom = syscom + ' ' + ann_files_c[yyyy][f]
                    else:
                        continue

    return syscom, nyears

#---------------------------------------------------------------------------
# For ensemble mean plots, run system nco command.
#---------------------------------------------------------------------------
def run_ensmean_syscom(file_plot, var, ncocom, syscom, yyyy_s, yyyy_e, \
                       season, rundir, ):

    #--- If system command is too big (too many files to send
    #--- to nco), break it up into 1000 piece chunks.
    files_c = syscom.replace(ncocom, '')
    scsplit = files_c.split()
    nfiles = len(scsplit)

    niter = 1000
    if nfiles > niter:
        nt =  int(math.ceil(float(nfiles) / float(niter)))
        files_all = []
        for n in range(nt):
            file_c = rundir + '/' + var + '_' + season + '_' + \
                     yyyy_s + '_' + yyyy_e + '_subset' + str(n) + '.nc'
            if os.path.isfile(file_c):
                print 'this file already exists: ', file_c
                files_all.append(file_c)
                continue
            nb = n * niter
            ne = (n+1) * niter
            syscom = ncocom + ' ' + ' '.join(scsplit[nb:ne]) + ' ' + file_c
            print syscom
            iret = os.system(syscom)
            if iret != 0:
                print 'problem running nco! iret = ', iret
                return mvc
            files_all.append(file_c)
        syscom = ncocom + ' ' + ' '.join(files_all) + ' ' + file_plot
        print syscom                
        iret = os.system(syscom)
        if iret != 0:
            print 'problem running nco! iret = ', iret
            return mvc
    else:
        syscom = syscom + ' ' + file_plot
        print 'syscom = ', syscom
        iret = os.system(syscom)
        if iret != 0:
            print 'problem running nco! iret = ', iret
            return mvc

    return 1

#---------------------------------------------------------------------------
# Get plot file name.
#---------------------------------------------------------------------------
def get_plotfname(plot_dir, stn, sdt, edt, season, var, stat):
    plotfname = plot_dir + '/' + stn + '_' + sdt + '_' + edt + '_' + \
                season + '_' + var + '_' + stat + '.png'
    return plotfname

#---------------------------------------------------------------------------
# Get plot title.
#---------------------------------------------------------------------------
def get_title(season, stn, var, stat, years):
    titleout = station_name_dict[stn] + ' (' + stn.upper() + '), ' + \
               seasons_lab[season] + ' ' + labels[var+stat] + ', ' + \
               str(years[0]) + ' - ' + str(years[len(years)-1])
    return titleout

#---------------------------------------------------------------------------
# Get list of extracted wrfout files between sdt_yyyymm and edt_yyyymm.
#---------------------------------------------------------------------------
def get_extract_files(data_dirs, model, sdt_yyyymm, edt_yyyymm, snowrad):
    files_out = []
    for d in range(len(data_dirs)):
        path_c = data_dirs[d] + '/' + model + '/extract/*.nc'
        files = glob.glob(path_c)
        
        for f in range(len(files)):

            if snowrad == 0:
                if 'snowrad' in files[f]:
                    continue
            else:
                if 'snowrad' not in files[f]:
                    continue

            dt_c = re.findall(r'\.(\d{10})\.', files[f])
            if (dt_c):
                dt_c = dt_c[0]
                yyyymm_c = dt_c[0:6]
                if (yyyymm_c >= sdt_yyyymm and yyyymm_c <= edt_yyyymm):
                    files_out.append(files[f])
    if (len(files_out) == 0):
        sys.exit('get_extract_files:  no files found!')
    return list(sorted(set(files_out)))

#---------------------------------------------------------------------------
# Get seasonal stats.
#---------------------------------------------------------------------------
def get_seasonal_files(files):

    springs = defaultdict(list)
    summers = defaultdict(list)
    falls   = defaultdict(list)
    winters = defaultdict(list)
    annuals = defaultdict(list)
    spring_ndays = {}
    summer_ndays = {}
    fall_ndays   = {}
    winter_ndays = {}
    annual_ndays = {}    
    years_all = []

    #--- Loop over all files, binning them into seasons / years.
    for f in range(len(files)):
        dt_c = re.findall(r'\.(\d{10})\.', files[f])[0]
        if (dt_c):
            yyyy = dt_c[0:4]
            mm   = dt_c[4:6]
            years_all.append(yyyy)
            if (mm == '03' or mm == '04' or mm == '05'):
                if ((yyyy) in spring_ndays):
                    spring_ndays[yyyy] += 1
                else:
                    spring_ndays[yyyy] = 1
                springs[yyyy].append(files[f])
            elif (mm == '06' or mm == '07' or mm == '08'):
                if ((yyyy) in summer_ndays):
                    summer_ndays[yyyy] += 1
                else:
                    summer_ndays[yyyy] = 1
                summers[yyyy].append(files[f])
            elif (mm == '09' or mm == '10' or mm == '11'):
                if ((yyyy) in fall_ndays):
                    fall_ndays[yyyy] += 1
                else:
                    fall_ndays[yyyy] = 1
                falls[yyyy].append(files[f])
            elif (mm == '12'):
                yyyyw = str(int(yyyy) + 1)
                if ((yyyyw) in winter_ndays):
                    winter_ndays[yyyyw] += 1
                else:
                    winter_ndays[yyyyw] = 1
                winters[yyyyw].append(files[f])
            elif (mm == '01' or mm == '02'):
                if ((yyyy) in winter_ndays):
                    winter_ndays[yyyy] += 1
                else:
                    winter_ndays[yyyy] = 1
                winters[yyyy].append(files[f])

            if ((yyyy) in annual_ndays):
                annual_ndays[yyyy] += 1
            else:
                annual_ndays[yyyy] = 1
            annuals[yyyy].append(files[f])

    #--- Get list of unique, sorted years found above.
    years = list(sorted(set(years_all)))

    #--- Check number of data points per unique years and nan out
    #--- ones that do not have enough (< min_season_days).
    for y in range(len(years)):
        yyyy = years[y]
        if (yyyy in spring_ndays):
            if (spring_ndays[yyyy] < 3):
                springs[yyyy] = np.nan
        else:
            springs[yyyy] = np.nan
        if (yyyy in summer_ndays):
            if (summer_ndays[yyyy] < 3):
                summers[yyyy] = np.nan
        else:
            summers[yyyy] = np.nan
        if (yyyy in fall_ndays):            
            if (fall_ndays[yyyy] < 3):
                falls[yyyy] = np.nan
        else:
            falls[yyyy] = np.nan
        if (yyyy in winter_ndays):            
            if (winter_ndays[yyyy] < 3):
                winters[yyyy] = np.nan
        else:
            winters[yyyy] = np.nan
        if (yyyy in annual_ndays):            
            if (annual_ndays[yyyy] < 3):
                annuals[yyyy] = np.nan
        else:
            annuals[yyyy] = np.nan

    return springs, summers, falls, winters, annuals, years

#---------------------------------------------------------------------------
# Get seasonal files (new)
#---------------------------------------------------------------------------
def get_seasonal_files_new(files):

    seafiles = defaultdict(list)
    ndays = {}

#    springs = defaultdict(list)
#    summers = defaultdict(list)
#    falls   = defaultdict(list)
#    winters = defaultdict(list)
#    spring_ndays = {}
#    summer_ndays = {}
#    fall_ndays   = {}
#    winter_ndays = {}

    years_all = []

    #--- Loop over all files, binning them into seasons / years.
    for f in range(len(files)):
        dt_c = re.findall(r'\.(\d{10})\.', files[f])[0]
        if (dt_c):
            yyyy = dt_c[0:4]
            mm   = dt_c[4:6]

            if mm == '12':
                yyyy = str(int(yyyy) + 1)
            else:
                yyyy = yyyy
            years_all.append(yyyy)

            if (mm == '03' or mm == '04' or mm == '05'):
                season = 'spring'
            if (mm == '06' or mm == '07' or mm == '08'):
                season = 'summer'
            if (mm == '09' or mm == '10' or mm == '11'):
                season = 'fall'
            if (mm == '12' or mm == '01' or mm == '02'):
                season = 'winter'

            if ((yyyy) in ndays):
                ndays[season+yyyy] += 1
            else:
                ndays[season+yyyy] = 1
            seafiles[season,yyyy].append(files[f])

    #--- Get list of unique, sorted years found above.
    years = list(sorted(set(years_all)))

    return seafiles, years

'''
    #--- Check number of data points per unique years and nan out
    #--- ones that do not have enough (< min_season_days).
    for y in range(len(years)):
        yyyy = years[y]
        if (yyyy in spring_ndays):
            if (spring_ndays[yyyy] < 3):
                springs[yyyy] = np.nan
        else:
            springs[yyyy] = np.nan
        if (yyyy in summer_ndays):
            if (summer_ndays[yyyy] < 3):
                summers[yyyy] = np.nan
        else:
            summers[yyyy] = np.nan
        if (yyyy in fall_ndays):            
            if (fall_ndays[yyyy] < 3):
                falls[yyyy] = np.nan
        else:
            falls[yyyy] = np.nan
        if (yyyy in winter_ndays):            
            if (winter_ndays[yyyy] < 3):
                winters[yyyy] = np.nan
        else:
            winters[yyyy] = np.nan
'''
#---------------------------------------------------------------------------
# Get seasonal stats (totals or averages currently).
#---------------------------------------------------------------------------
def get_seasonal_stats(data_all, dts_all, models, stns, var, stat):

    out_sum   = {}
    out_avg   = {}
    out_max   = {}
    out_min   = {}
    years_all = []
    
    for m in range(len(models)):
        mod = models[m]
        data_all_c = data_all[mod]
        for s in range(len(stns)):
            stn = stns[s]
            ndays = {}
            years_mod_stn = []
            for d in range(len(dts_all)):
                yyyy = dts_all[d][0:4]
                mm = dts_all[d][4:6]
                years_mod_stn.append(yyyy)

                #--- Very first date of all model runs will have unusable
                #--- (all 0's) in the fields.  So don't try and use it.
                if dts_all[d] == '1970010100':
                    continue

                if ((var,stn,dts_all[d]) in data_all_c):
                    dat_c = data_all_c[var,stn,dts_all[d]]
                else:
                    ndays[season+yyyy] = np.nan
                    out_sum[season,mod,stn,yyyy] = np.nan
                    out_max[season,mod,stn,yyyy] = np.nan
                    out_min[season,mod,stn,yyyy] = np.nan
                    continue

                #--- annual is all days so get sum/max/min for every dts_all.
                (ndays, out_sum, out_max, out_min) = \
                        get_season_summaxmin(dat_c, mod, stn, 'annual',yyyy,mm,\
                                             ndays, out_sum, out_max, out_min)

                if (mm == '03' or mm == '04' or mm == '05'):
                    season = 'spring'
                if (mm == '06' or mm == '07' or mm == '08'):
                    season = 'summer'
                if (mm == '09' or mm == '10' or mm == '11'):
                    season = 'fall'
                if (mm == '12' or mm == '01' or mm == '02'):
                    season = 'winter'
                (ndays, out_sum, out_max, out_min) = \
                        get_season_summaxmin(dat_c, mod, stn, season, yyyy, mm,\
                                             ndays, out_sum, out_max, out_min)
                        
            #--- Get list of unique, sorted years found above.
            years = list(sorted(set(years_mod_stn)))

            #--- Check number of data points per unique years and nan out
            #--- ones that do not have enough (< min_season_days).
            for y in range(len(years)):
                yyyy = years[y]
                for s in range(len(seasons)):
                    season = seasons[s]
                    if ((season+yyyy) in ndays):
                        if (ndays[season+yyyy] < min_season_days):
                            out_sum[season,mod,stn,yyyy] = np.nan
                            out_max[season,mod,stn,yyyy] = np.nan
                            out_min[season,mod,stn,yyyy] = np.nan
                    else:
                        ndays[season+yyyy] = np.nan
                        out_sum[season,mod,stn,yyyy] = np.nan
                        out_max[season,mod,stn,yyyy] = np.nan
                        out_min[season,mod,stn,yyyy] = np.nan

            #--- if stat requested was 'avg', do averaging.
            if (stat == 'avg' or stat == 'max' or stat == 'min'):
                for y in range(len(years)):
                    yyyy = years[y]
                    for s in range(len(seasons)):
                        season = seasons[s]
                        out_avg[season,mod,stn,yyyy] = out_sum[\
                            season,mod,stn,yyyy] / ndays[season+yyyy]
                
            #--- Keep around all years found for this model and station.
            years_all.append(years_mod_stn)

    #--- Final, unique, sorted list of years found.
    years = list(sorted(set(years_all[0])))            

    if (stat == 'avg'):
        return out_avg, years
    elif (stat == 'max'):
        return out_max, years
    elif (stat == 'min'):
        return out_min, years
    else:
        return out_sum, years

#---------------------------------------------------------------------------
# Loop over each models, grabbing data from extract wrfout files.
#---------------------------------------------------------------------------
def load_extract_data(geo_em, stns, latpts, lonpts, elevs, models, \
                      data_dirs, sdt, edt, varsin, pickle_dir, load_pickle):

    #--- Create temporary list of variables, removing 'T2MEAN' if in original
    #--- list (we'll calculate T2MEAN if necessary after loading data).
    vars_tmp = list(varsin)
    if 'T2MEAN' in vars_tmp:
        i = vars_tmp.index('T2MEAN')
        del vars_tmp[i]

    #--- Load geo em file to get lat/lon/elev grids.  Also interpolate
    #--- model elevation to stns lat/lon point locations.
    print 'Loading:\n', geo_em
    (lat, lon, hgt) = load_geo_em(geo_em)
    elevs_mod  = []
    elevs_diff = []    
    for ns in range(len(stns)):
        dat_interp = bilinear_interpolate(lat, lon, hgt, \
                                          latpts[ns], lonpts[ns], 0)
        elevs_mod.append(dat_interp)
        elevs_diff.append(elevs[ns] - dat_interp)

    for m in range(len(models)):
        data_all = {}
        dts_all = []
        model = models[m]
        pickle_file = get_mod_pkl_name(pickle_dir, model, sdt, edt)

        #--- Get list of extracted files for this model.
        print 'model = ', model
        files = get_extract_files(data_dirs, model, sdt, edt, 0)
        files = list(sorted(set(files)))

        print 'Loading: '
        #--- Loop over all extract files, reading them in.
        for f in range(len(files)):
    
            (vardat, ntimes, dt) = load_wrfout(files[f], vars_tmp)
            print files[f], ntimes
                
            #--- Loop over each vars_tmp, interpolating data to stns for
            #--- each ntimes.
            for v in range(len(vars_tmp)):
    
                var = vars_tmp[v]
                vardat_c = vardat[var]
                    
                #--- Loop over each ntime, grabbing interpolated data for
                #--- variable var for each stns.
                for n in range(ntimes):
                    dt_c = time_increment(dt, n, dtfmt)
                    for ns in range(len(stns)):
                        dat_interp = bilinear_interpolate(lat, lon, \
                                                          vardat_c[:,:,n],\
                                                          latpts[ns], \
                                                          lonpts[ns], 0)
    
                        #--- Do lapse rate correction for temperature.
                        if (var == 'T2MAX' or var == 'T2MIN'):
                            dat_c = dat_interp - std_lapse * elevs_diff[ns]
                        else:
                            dat_c = dat_interp
    
                        data_all[var, stns[ns], dt_c] = dat_c
                        dts_all.append(dt_c)
    
        #--- Get sorted unique list of all dates seen.
        dts_unique = list(sorted(set(dts_all)))

        #--- If T2MEAN was requested, calculate it from T2MAX and T2MIN for
        #--- every station and date.
        if 'T2MEAN' in varsin:
            for ns in range(len(stns)):
                for d in range(len(dts_unique)):
                    keymax = ('T2MAX', stns[ns], dts_unique[d])
                    keymin = ('T2MIN', stns[ns], dts_unique[d])
                    if keymax in data_all and keymin in data_all:
                        keymean = ('T2MEAN', stns[ns], dts_unique[d])

#                        if data_all[keymax] < (273-60) or \
#                           data_all[keymax] > (273+60) or \
#                           data_all[keymin] < (273-60) or \
#                           data_all[keymin] > (273+60):
#                            print 'stns[ns] = ', stns[ns]
#                            print 'dts_unique[d] = ', dts_unique[d]
#                            print 'max = ', data_all[keymax]
#                            print 'min = ', data_all[keymin]
##                            sys.exit()
                        
                        data_all[keymean] = np.mean([data_all[keymax], \
                                                     data_all[keymin]])

        #--- Create pickle file.
        pickle.dump((model, data_all, dts_unique, varsin, stns), \
                    open(pickle_file,'wb'), -1)

    return data_all, dts_unique, models, varsin, stns

#---------------------------------------------------------------------------
# Interpolate from a sent-in grid to a sent in lat/lon point.
#---------------------------------------------------------------------------
def bilinear_interpolate(latgrid, longrid, datagrid, latpt, lonpt, deb):

    totdiff = abs(latgrid - latpt) + abs(longrid - lonpt)
    (i,j) = np.unravel_index(totdiff.argmin(), totdiff.shape)

    #--- Get lat/lon box in which latpt,lonpt resides.
    if (latpt >= latgrid[i,j] and lonpt >= longrid[i,j]):
        iif = i
        jjf = j
    elif (latpt < latgrid[i,j] and lonpt < longrid[i,j]):
        iif = i-1
        jjf = j-1
    elif (latpt >= latgrid[i,j] and lonpt < longrid[i,j]):
        iif = i-1
        jjf = j
    elif (latpt < latgrid[i,j] and lonpt >= longrid[i,j]):
        iif = i
        jjf = j-1

    (nx, ny) = np.shape(latgrid)

    if (deb == 1):
        print 'nx, ny  = ', nx, ny
        print 'iif,jjf = ', iif,jjf
        print 'latgrid[iif+1,jjf] = ', latgrid[iif+1,jjf]
        print 'latgrid[iif,jjf]   = ', latgrid[iif,jjf]

    if (iif >= (nx-1) or jjf >= (ny-1) or iif < 0 or jjf < 0):
        return mvc

    #--- Do bilinear interpolation to latpt,lonpt.
    dlat  = latgrid[iif,jjf+1] - latgrid[iif,jjf]
    dlon  = longrid[iif+1,jjf] - longrid[iif,jjf]
    dslat = latgrid[iif,jjf+1] - latpt
    dslon = longrid[iif+1,jjf] - lonpt

    wrgt = 1 - (dslon/dlon)
    wup  = 1 - (dslat/dlat)

    vll = datagrid[iif,jjf]
    vlr = datagrid[iif,jjf+1]
    vul = datagrid[iif+1,jjf]
    vur = datagrid[iif+1,jjf+1]

    if (deb > 1):
        print 'll lat, lon, val = ', latgrid[iif,jjf], longrid[iif,jjf], vll
        print 'lr lat, lon, val = ', latgrid[iif+1,jjf], longrid[iif+1,jjf], vlr
        print 'ur lat, lon, val = ', latgrid[iif+1,jjf+1], longrid[iif+1,jjf+1],vur
        print 'ul lat, lon, val = ', latgrid[iif,jjf+1], longrid[iif,jjf+1], vul
        print 'latpt, lonpt = ', latpt, lonpt
        
        print 'vll = ', vll
        print 'vlr = ', vlr
        print 'vul = ', vul
        print 'vur = ', vur

    datout = (1-wrgt) * ((1-wup) * vll + wup * vul) + \
        (wrgt) * ((1-wup) * vlr + wup * vur)

#    if (deb == 1):
#        print 'datout = ', datout
#        sys.exit()
#    if (datout == 0.0):
#        print 'nx, ny  = ', nx, ny
#        print 'iif,jjf = ', iif,jjf
#        print 'latgrid[iif+1,jjf] = ', latgrid[iif+1,jjf]
#        print 'latgrid[iif,jjf]   = ', latgrid[iif,jjf]
#        
#        print 'll lat, lon, val = ', latgrid[iif,jjf], longrid[iif,jjf], vll
#        print 'lr lat, lon, val = ', latgrid[iif+1,jjf], longrid[iif+1,jjf], vl#r
#        print 'ur lat, lon, val = ', latgrid[iif+1,jjf+1], longrid[iif+1,jjf+1],vur
#        print 'ul lat, lon, val = ', latgrid[iif,jjf+1], longrid[iif,jjf+1], vu#l
#        print 'latpt, lonpt = ', latpt, lonpt
#        
#        print 'vll = ', vll
#        print 'vlr = ', vlr
#        print 'vul = ', vul
#        print 'vur = ', vur
#
#        sys.exit()

    return datout

#----------------------------------------------------------------------------
# Add data to sums, max's and min's arrays.
#----------------------------------------------------------------------------
def get_season_summaxmin(dat_c, mod, stn, season, yyyyin, month, ndays, \
                         out_sum, out_max, out_min):

    #--- Look out for cases where data is NaN (e.g. the 31st of June) and
    #--- do nothing (return) in those cases.
    if np.isnan(dat_c):
        return ndays, out_sum, out_max, out_min
        
    #--- Put December winter data into next year's winter-- naming
    #--- winters by their Jan/Feb year rather than their Dec year.
    if month == '12':
        yyyy = str(int(yyyyin) + 1)
    else:
        yyyy = yyyyin
        
    #--- Count number of days for this season and year.
    if (season+yyyy) in ndays:
        ndays[season+yyyy] += 1
    else:
        ndays[season+yyyy] = 1

    key = (season,mod,stn,yyyy)

    #--- Get sum of sent in data.
    if key in out_sum:
        out_sum[key] = out_sum[key] + dat_c
    else:
        out_sum[key] = dat_c

    #--- Get maximum.
    if key in out_max:
        if dat_c > out_max[key]:
            out_max[key] = dat_c
    else:
        out_max[key] = dat_c
            
    #--- Get minimum.
    if key in out_min:
        if dat_c < out_min[key]:
            out_min[key] = dat_c
    else:
        out_min[key] = dat_c

    return ndays, out_sum, out_max, out_min
<file_sep>/old/old_old/plot_wrfout_grid_20180130.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re, math
import numpy as np
#import matplotlib.pyplot as plt
from make_cmap import *
from netCDF4 import Dataset as NetCDFFile
from utils_wrfout import *
from utils_date import *
import pickle
from collections import defaultdict
from make_cmap import *

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
plot_dir   = rcm_dir + '/plots'
pickle_dir = rcm_dir + '/pickle'
run_dirs   = rcm_dir + '/rundirs'
data_dir = '/home/disk/r2d2/steed/cmip5/rcp8.5'

geo_em = '/home/disk/a125/steed/run/geo_em.d02.nc'

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
vars = ['PREC', 'T2MAX', 'T2MIN', 'SPDUV10MAX', 'SPDUV10MEAN', 'SPDUV10STD']
#sdt = '202001'
#edt = '202912'
sdt = '197001'
edt = '209912'
models = ['gfdl-cm3']
dtfmt = "%Y%m%d%H"

#---------------------------------------------------------------------------
# Make a color map using make_cmap.
#---------------------------------------------------------------------------
#if (varname == 'tos'):
#    levs_c = levs_temp
#    cmap_c = cmap_temp
#elif(varname == 'ua'):
#    levs_c = levs_prec
#    cmap_c = cmap_prec
levs_c = levs_temp
cmap_c = cmap_temp
        
levs_norm_c = []
for i in range(len(levs_c)):
    x = float(levs_c[i])
    norm_c = (x - min(levs_c)) / (max(levs_c) - min(levs_c))
    levs_norm_c.append(norm_c)

my_cmap = make_cmap(cmap_c, bit = True, position = levs_norm_c)

#---------------------------------------------------------------------------
# Make run directory, load geo_em file.
#---------------------------------------------------------------------------
rundir = run_dirs + '/plot_wrfout_grid.' + str(os.getpid())
print 'making ', rundir
os.makedirs(rundir)

print 'Loading ', geo_em
(lat, lon) = load_geo_em(geo_em)

#---------------------------------------------------------------------------
# For each model, make a maps of every season...
#---------------------------------------------------------------------------
for m in range(len(models)):

    model = models[m]
    data_dir_c = data_dir + '/' + model + '/extract'

    #--- Get list of extracted files for this model.
    files = get_extract_files(data_dir_c, model, sdt, edt)

    #--- Get files broken out by year and season.
    (spr_files, sum_files, fal_files, win_files, years) = \
                get_seasonal_files(files)

    #--- Loop over year, making maps of each 
    for y in range(len(years)):
        yyyy = years[y]

        #--- Winter plots.
        if (win_files[yyyy] == win_files[yyyy]):
            file_plot = rundir + '/win_' + yyyy + '.nc'
            syscom = 'ncra'
            for f in range(len(win_files[yyyy])):
                print yyyy, win_files[yyyy][f]
                syscom = syscom + ' ' + win_files[yyyy][f]
            syscom = syscom + ' ' + file_plot
            print 'syscom = ', syscom
            os.system(syscom)

            var = 'T2MAX'
            print 'Loading ', var, ' from ', file_plot
            (vardat, ntimes, dt) = load_wrfout(file_plot, [var])

            colorbar_lab = 'Temperature (K)'
            titlein = model + ', Average Max Temperature (K), DJF ' + yyyy
            plotfname = rundir + '/win_' + var + '_' + yyyy + '.png'

            plotdat = vardat[var][:,:,0]
            mapper(lat, lon, plotdat, minlat, maxlat, minlon, maxlon, \
                   levs_c, my_cmap, maplw, colorbar_lab, titlein, plotfname)

        if (sum_files[yyyy] == sum_files[yyyy]):
            file_plot = rundir + '/sum_' + yyyy + '.nc'
            syscom = 'ncra'
            for f in range(len(sum_files[yyyy])):
                print yyyy, sum_files[yyyy][f]
                syscom = syscom + ' ' + sum_files[yyyy][f]
            syscom = syscom + ' ' + file_plot
            print 'syscom = ', syscom
            os.system(syscom)

            var = 'T2MAX'
            print 'Loading ', var, ' from ', file_plot
            (vardat, ntimes, dt) = load_wrfout(file_plot, [var])

            colorbar_lab = 'Temperature (K)'
            titlein = model + ', Average Max Temperature (K), JJA ' + yyyy
            plotfname = rundir + '/sum_' + var + '_' + yyyy + '.png'

            plotdat = vardat[var][:,:,0]
            mapper(lat, lon, plotdat, minlat, maxlat, minlon, maxlon, \
                   levs_c, my_cmap, maplw, colorbar_lab, titlein, plotfname)



    sys.exit()



#    #--- Loop over all extract files, reading them in.
#    springs = defaultdict(list)
#    summers = defaultdict(list)
#    falls   = defaultdict(list)
#    winters = defaultdict(list)
#    spring_ndays = {}
#    summer_ndays = {}
#    fall_ndays   = {}
#    winter_ndays = {}
#
#    for f in range(len(files)):
##        print files[f]
#        dt_c = re.findall(r'\.(\d{10})\.', files[f])[0]
#        if (dt_c):
#            yyyy = dt_c[0:4]
#            mm   = dt_c[4:6]
#            years_mod_stn.append(yyyy)
#
#            print dt_c, yyyy, mm
#            if (mm == '03' or mm == '04' or mm == '05'):
#                if ((yyyy) in spring_ndays):
#                    spring_ndays[yyyy] += 1
#                else:
#                    spring_ndays[yyyy] = 1
#                springs[yyyy].append(files[f])
#            elif (mm == '06' or mm == '07' or mm == '08'):
#                if ((yyyy) in summer_ndays):
#                    summer_ndays[yyyy] += 1
#                else:
#                    summer_ndays[yyyy] = 1
#                summers[yyyy].append(files[f])
#            elif (mm == '09' or mm == '10' or mm == '11'):
#                if ((yyyy) in fall_ndays):
#                    fall_ndays[yyyy] += 1
#                else:
#                    fall_ndays[yyyy] = 1
#                falls[yyyy].append(files[f])
#            elif (mm == '12'):
#                if ((yyyyw) in winter_ndays):
#                    winter_ndays[yyyyw] += 1
#                else:
#                    winter_ndays[yyyyw] = 1
#                yyyyw = str(int(yyyy) + 1)
#                winters[yyyyw].append(files[f])
#            elif (mm == '01' or mm == '02'):
#                if ((yyyy) in winter_ndays):
#                    winter_ndays[yyyy] += 1
#                else:
#                    winter_ndays[yyyy] = 1
#                winters[yyyy].append(files[f])
#                
#    print springs
#    print 'springs[2020] = ', springs['2020']
#    print 'summers[2020] = ', summers['2020']
#    print 'winters[2020] = ', winters['2020']
#
#    sys.exit()

##---------------------------------------------------------------------------
## Loop over each models, grabbing data from extract wrfout files.
##---------------------------------------------------------------------------
#pickle_file = pickle_dir + '/extract_' + sdt + '_' + edt + '.pickle'
#if os.path.isfile(pickle_file):
#    (data_all, dts_unique, models, vars, stns) = \
#               pickle.load(open(pickle_file, 'rb'))
#else:
#    print 'Loading ', geo_em
#    (lat, lon) = load_geo_em(geo_em)
#
#    data_all = {}
#    dts_all = []
#    for m in range(len(models)):
#
#        model = models[m]
#        data_dir_c = data_dir + '/' + model + '/extract'
#
#        #--- Get list of extracted files for this model.
#        files = get_extract_files(data_dir_c, model, sdt, edt)
#
#        #--- Loop over all extract files, reading them in.
#        for f in range(len(files)):
#
#            print 'Loading ', files[f]
#            (vardat, ntimes, dt) = load_wrfout(files[f], vars)
#
#            #--- Loop over each vars, interpolating data to stns for
#            #--- each ntimes.
#            for v in range(len(vars)):
#
#                var = vars[v]
#                vardat_c = vardat[var]
#                
#                #--- Loop over each ntime, grabbing interpolated data for
#                #--- variable var for each stns.
#                for n in range(ntimes):
#                    dt_c = time_increment(dt, n, dtfmt)
#                    for ns in range(len(stns)):
#                        dat_interp = bilinear_interpolate(lat, lon, \
#                                                          vardat_c[:,:,n],\
#                                                          latpts[ns], \
#                                                          lonpts[ns], 0)
#                        data_all[model, var, stns[ns], dt_c] = dat_interp
#                        dts_all.append(dt_c)
#
#    #--- Get sorted unique list of all dates seen.
#    dts_unique = list(sorted(set(dts_all)))
#    
#    #--- Create pickle file.
#    pickle.dump((data_all, dts_unique, models, vars, stns), \
#                open(pickle_file,'wb'), -1)
#
##---------------------------------------------------------------------------
## Get seasonal precipitation totals and make a time series plot of them.
##---------------------------------------------------------------------------
#(spr_pcp_tot, sum_pcp_tot, fal_pcp_tot, win_pcp_tot, years) = \
#          get_seasonal_stats(data_all, dts_unique, models, stns, 'PREC', 'tot')
#
#mod = 'gfdl-cm3'
#plotfname = plot_dir + '/' + mod + '_season_precip.png'
#titlein = mod + ', ' + str(years[0]) + ' - ' + str(years[len(years)-1])
#iret = ts(win_pcp_tot, spr_pcp_tot, sum_pcp_tot, fal_pcp_tot, years, \
#          stns, mod, titlein, plotfname, 'PREC')
#
##---------------------------------------------------------------------------
## Get seasonal temperature averages.
##---------------------------------------------------------------------------
#(spr_tmx_avg, sum_tmx_avg, fal_tmx_avg, win_tmx_avg, years) = \
#          get_seasonal_stats(data_all, dts_unique, models, stns, 'T2MAX', 'avg')
#(spr_tmn_avg, sum_tmn_avg, fal_tmn_avg, win_tmn_avg, years) = \
#          get_seasonal_stats(data_all, dts_unique, models, stns, 'T2MIN', 'avg')
#
#(spr_spdavg_avg, sum_spdavg_avg, fal_spdavg_avg, win_spdavg_avg, years) = \
#                 get_seasonal_stats(data_all, dts_unique, models, stns, \
#                                    'SPDUV10MEAN', 'avg')
#
#mod = 'gfdl-cm3'
#plotfname = plot_dir + '/' + mod + '_season_tmx_avg.png'
#titlein = mod + ', TMAX Average, ' + str(years[0]) + ' - ' + \
#          str(years[len(years)-1])
#iret = ts(win_tmx_avg, spr_tmx_avg, sum_tmx_avg, fal_tmx_avg, years, \
#          stns, mod, titlein, plotfname, 'T2MAX')
#
#mod = 'gfdl-cm3'
#plotfname = plot_dir + '/' + mod + '_season_tmn_avg.png'
#titlein = mod + ', TMIN Average, ' + str(years[0]) + ' - ' + \
#          str(years[len(years)-1])
#iret = ts(win_tmn_avg, spr_tmn_avg, sum_tmn_avg, fal_tmn_avg, years, \
#          stns, mod, titlein, plotfname, 'T2MIN')

sys.exit()

<file_sep>/old/old_old/plot_wrfout_grid_avg_snowrad_20180926.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re, math
import numpy as np
#import matplotlib.pyplot as plt
from make_cmap import *
from netCDF4 import Dataset as NetCDFFile
from utils_wrfout import *
from utils_date import *
import pickle
from collections import defaultdict
from make_cmap import *

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
plot_dir   = rcm_dir + '/plots'
pickle_dir = rcm_dir + '/pickle'
run_dirs   = rcm_dir + '/rundirs'
geo_em     = rcm_dir + '/data' + '/geo_em.d02.nc'
data_dirs  = ['/home/disk/r2d2/steed/cmip5/rcp8.5', \
              '/home/disk/vader/steed/cmip5/rcp8.5', \
              '/home/disk/jabba/steed/cmip5/rcp8.5']

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
#vars = ['SNOW', 'SWDOWN']
#vars = ['SNOW']
vars = ['SWDOWN']

sdts = ['197001', '207001']
edts = ['200012', '209912']
#sdts = ['207001']
#edts = ['209912']

#models   = ['gfdl-cm3', 'miroc5', 'bcc-csm1.1', 'access1.3']
#models   = ['gfdl-cm3', 'miroc5', 'access1.3']
#models = ['gfdl-cm3']
models = ['access1.3']
dtfmt = "%Y%m%d%H"

#---------------------------------------------------------------------------
# Make a color map using make_cmap.
#---------------------------------------------------------------------------
levs_norm_c = []
for i in range(len(levs_temp)):
    x = float(levs_temp[i])
    norm_c = (x - min(levs_temp)) / (max(levs_temp) - min(levs_temp))
    levs_norm_c.append(norm_c)
cmap_temp = make_cmap(cmap_temp, bit = True, position = levs_norm_c)

levs_norm_c = []
for i in range(len(levs_pcp)):
    x = float(levs_pcp[i])
    norm_c = (x - min(levs_pcp)) / (max(levs_pcp) - min(levs_pcp))
    levs_norm_c.append(norm_c)
cmap_pcp = make_cmap(cmap_pcp, bit = True, position = levs_norm_c)

levs_norm_c = []
for i in range(len(levs_swe)):
    x = float(levs_swe[i])
    norm_c = (x - min(levs_swe)) / (max(levs_swe) - min(levs_swe))
    levs_norm_c.append(norm_c)
cmap_swe = make_cmap(cmap_swe, bit = True, position = levs_norm_c)

levs_norm_c = []
for i in range(len(levs_swdown)):
    x = float(levs_swdown[i])
    norm_c = (x - min(levs_swdown)) / (max(levs_swdown) - min(levs_swdown))
    levs_norm_c.append(norm_c)
cmap_swdown = make_cmap(cmap_swdown, bit = True, position = levs_norm_c)

#---------------------------------------------------------------------------
# Make run directory, load geo_em file.
#---------------------------------------------------------------------------
rundir = run_dirs + '/plot_wrfout_grid_avg.' + str(os.getpid())
print 'making ', rundir
os.makedirs(rundir)

#rundir = '/home/disk/spock/jbaars/rcm/rundirs/plot_wrfout_grid_avg.4149'

print 'Loading ', geo_em
(lat, lon, hgt) = load_geo_em(geo_em)

#---------------------------------------------------------------------------
# For each model, make a maps short-wave down radiation (SWDOWN).
#---------------------------------------------------------------------------
var = 'SWDOWN'
for m in range(len(models)):
    model = models[m]
    for n in range(len(sdts)):
        sdt = sdts[n]
        edt = edts[n]
        yyyy_s = sdt[0:4]
        yyyy_e = edt[0:4]        

        #--- Get list of extracted files for this model.
        files = get_extract_files(data_dirs, model, sdt, edt, 1)

        #--- Get files broken out by year and season.
        (spr_files, sum_files, fal_files, win_files, years) = \
                    get_seasonal_files(files)
       
        for season in seasons:
            file_plot = rundir + '/' + model + '_' + var + '_' + season + \
                        '_' + yyyy_s + '_' + yyyy_e + '.nc'
            syscom = 'ncra'
            nyears = 0
            for y in range(len(years)):
                yyyy = years[y]

                if season == 'summer':
                    if (sum_files[yyyy] == sum_files[yyyy]):
                        nyears += 1
                    for f in range(len(sum_files[yyyy])):
                        syscom = syscom + ' ' + sum_files[yyyy][f]
                    else:
                        continue
                elif season == 'spring':
                    if (spr_files[yyyy] == spr_files[yyyy]):
                        nyears += 1
                        for f in range(len(spr_files[yyyy])):
                            syscom = syscom + ' ' + spr_files[yyyy][f]
                        else:
                            continue
                elif season == 'winter':
                    if (win_files[yyyy] == win_files[yyyy]):
                        nyears += 1
                        for f in range(len(win_files[yyyy])):
                            syscom = syscom + ' ' + win_files[yyyy][f]
                        else:
                            continue
                elif season == 'fall':                        
                    if (fal_files[yyyy] == fal_files[yyyy]):
                        nyears += 1
                        for f in range(len(fal_files[yyyy])):
                            syscom = syscom + ' ' + fal_files[yyyy][f]
                        else:
                            continue

            syscom = syscom + ' ' + file_plot
            print 'syscom = ', syscom        
            os.system(syscom)

            print 'Loading ', var, ' from ', file_plot
            nc = NetCDFFile(file_plot, 'r')
            ntimes = len(nc.dimensions['Time'])
            vardat = {}
            dat_tmp = nc.variables[var][:,:,:]
            dat_tmp = np.transpose(dat_tmp)
            vardat[var] = dat_tmp
            nc.close()
            
            titlein = model.upper() + ', ' + var_lab[var] + ', ' + \
                      seasons_lab[season] + ', ' + yyyy_s + ' - ' + yyyy_e
    
            plotdat = vardat[var][:,:,0]
            if (var == 'PREC'):
                plotdat = plotdat * mm2in
    
            for z in range(len(zooms)):
                plotfname = rundir + '/' + model + '_' + sdt + '_' + \
                            edt + '_' + season + '_' + var + '_' + \
                            zooms[z] + '.png'
                mapper(var, lat, lon, plotdat, levs_swdown, cmap_swdown, \
                       maplw, colorbar_labs[var], titlein, plotfname, \
                       zooms[z])
#            sys.exit()

##---------------------------------------------------------------------------
## For each model, make a maps Snow Water Equivalent.
##---------------------------------------------------------------------------
#var = 'SNOW'
#for m in range(len(models)):
#
#    model = models[m]
#
#    for n in range(len(sdts)):
#        sdt = sdts[n]
#        edt = edts[n]
#        yyyy_s = sdt[0:4]
#        yyyy_e = edt[0:4]        
#
#        #--- Get list of extracted files for this model.
#        files = []
#        for d in range(len(data_dirs)):
#            path_c = data_dirs[d] + '/' + model + '/extract/*0401*.nc'
#            files_c = glob.glob(path_c)
#        
#            for f in range(len(files_c)):
#                if 'snowrad' not in files_c[f]:
#                    continue
#
#                dt_c = re.findall(r'\.(\d{10})\.', files_c[f])
#                if (dt_c):
#                    dt_c = dt_c[0]
#                    yyyymm_c = dt_c[0:6]
#                    if (yyyymm_c >= sdt and yyyymm_c <= edt):
#                        files.append(files_c[f])
#
#        #--- Make plot for each zooms.
#        file_plot = rundir + '/' + model + '_' + var + '_apr1_' + \
#                    yyyy_s + '_' + yyyy_e + '.nc'
#        syscom = 'ncra'
#        for f in range(len(files)):
#            syscom = syscom + ' ' + files[f]
#            
#        syscom = syscom + ' ' + file_plot
#        print 'syscom = ', syscom        
#        os.system(syscom)
#    
#        print 'Loading ', var, ' from ', file_plot
#        nc = NetCDFFile(file_plot, 'r')
#        ntimes = len(nc.dimensions['Time'])
#        vardat = {}
#        dat_tmp = nc.variables[var][:,:,:]
#        dat_tmp = np.transpose(dat_tmp)
#        vardat[var] = dat_tmp
#        nc.close()
#        titlein = model.upper() + ', ' + var_lab[var] + ', ' + \
#                  'Apr 1st, ' + yyyy_s + ' - ' + yyyy_e
#    
#        plotdat = vardat[var][:,:,0]
#    
#        for z in range(len(zooms)):
#            plotfname = rundir + '/' + model + '_' + sdt + '_' + \
#                        edt + '_apr1_' + var + '_' + \
#                        zooms[z] + '.png'
#            mapper(var, lat, lon, plotdat, levs_swe, cmap_swe, \
#                   maplw, colorbar_labs[var], titlein, plotfname, \
#                   zooms[z])
#
##---------------------------------------------------------------------------
## Make map of difference.
##---------------------------------------------------------------------------
#for m in range(len(models)):
#    model = models[m]
#
#    syscom = 'ncdiff'
#    file_diff = rundir + '/' + model + '_' + var + '_apr1_' + 'diff.nc'
#
#    for n in range(len(sdts)):
#        sdt = sdts[n]
#        edt = edts[n]
#        yyyy_s = sdt[0:4]
#        yyyy_e = edt[0:4]        
#        file_plot = rundir + '/' + model + '_' + var + '_apr1_' + \
#                    yyyy_s + '_' + yyyy_e + '.nc'
#            if os.path.isfile(file_plot):
#                syscom = syscom + ' ' + file_plot
#
#    syscom = syscom + ' ' + file_diff
#
#    print 'syscom = ', syscom        
#    os.system(syscom)
#
#    print 'Loading ', var, ' from ', file_diff
#    nc = NetCDFFile(file_diff, 'r')
#    ntimes = len(nc.dimensions['Time'])
#    vardat = {}
#    dat_tmp = nc.variables[var][:,:,:]
#    dat_tmp = np.transpose(dat_tmp)
#    vardat[var] = dat_tmp
#    nc.close()
#
#    titlein = model.upper() + ', ' + var_lab[var] + ', ' + \
#              'Apr 1st, Difference, ' + yyyy_s + ' - ' + yyyy_e
#        
#    plotdat = vardat[var][:,:,0]
#    
#    for z in range(len(zooms)):
#        plotfname = rundir + '/' + model + '_' + sdt + '_' + \
#                    edt + '_apr1_diff_' + var + '_' + \
#                    zooms[z] + '.png'
#        mapper(var, lat, lon, plotdat, levs_swe, cmap_swe, \
#               maplw, colorbar_labs[var], titlein, plotfname, \
#               zooms[z])

sys.exit()

<file_sep>/old/old_old/plot_wrfout_stns_20180329.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re
import numpy as np
from make_cmap import *
from netCDF4 import Dataset as NetCDFFile
from utils_wrfout import *
from utils_date import *
import pickle

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
plot_dir   = rcm_dir + '/plots'
pickle_dir = rcm_dir + '/pickle'
data_dir = '/home/disk/r2d2/steed/cmip5/rcp8.5'

geo_em = '/home/disk/a125/steed/run/geo_em.d02.nc'

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
vars = ['PREC', 'T2MAX', 'T2MIN', 'SPDUV10MAX', 'SPDUV10MEAN', 'SPDUV10STD']
#sdt = '202001'
#edt = '202912'
#sdt = '197001'
#edt = '209912'
sdt = '199809'
edt = '200804'
models = ['gfdl-cm3', 'miroc5']
dtfmt = "%Y%m%d%H"

stns   = ['KSEA', 'KSMP', 'KYKM']
stn_cols = ['b', 'r', 'g']
latpts = [47.44472, 47.27667, 46.56417]
lonpts = [-122.31361, -121.33722, -120.53361]
elevs  = [130.0, 1207.0, 333.0]
    
##---------------------------------------------------------------------------
## Test.
##---------------------------------------------------------------------------
#x = range(0,len(vars),2)
#print 'x = ', x
#
##test = vars[x]
##print 'test = ', test
#
#test = [ vars[i] for i in x ]
#print 'test = ', test
#
#sys.exit()

#ndays = {}
#season = 'winter'
#yyyy = '1970'
#ndays[season+yyyy] = 26.0
#if (season+yyyy in ndays):
#    print 'hi'
#else:
#    print 'bye'
#sys.exit()

#season = 'winter'
#seasonsin = ['winter', 'spring']
#if (season in seasonsin):
#    print 'hi'
#else:
#    print 'bye'
#
#sys.exit()

#---------------------------------------------------------------------------
# Loop over each models, grabbing data from extract wrfout files.
#---------------------------------------------------------------------------
load_pickle = 1
pickle_file = pickle_dir + '/extract_' + sdt + '_' + edt + '.pickle'
if load_pickle and os.path.isfile(pickle_file):
    (data_all, dts_unique, models, vars, stns) = \
               pickle.load(open(pickle_file, 'rb'))
else:

    #--- Load geo em file to get lat/lon/elev grids.  Also interpolate model
    #--- elevation to stns lat/lon point locations.
    print 'Loading:\n', geo_em
    (lat, lon, hgt) = load_geo_em(geo_em)
    elevs_mod  = []
    elevs_diff = []    
    for ns in range(len(stns)):
        dat_interp = bilinear_interpolate(lat, lon, hgt, \
                                          latpts[ns], lonpts[ns], 0)
        elevs_mod.append(dat_interp)
        elevs_diff.append(elevs[ns] - dat_interp)

    data_all = {}
    dts_all = []
    for m in range(len(models)):

        model = models[m]
        data_dir_c = data_dir + '/' + model + '/extract'

        #--- Get list of extracted files for this model.
        files = get_extract_files(data_dir_c, model, sdt, edt)

        print 'Loading: '
        #--- Loop over all extract files, reading them in.
        for f in range(len(files)):

            print files[f]
            (vardat, ntimes, dt) = load_wrfout(files[f], vars)

            #--- Loop over each vars, interpolating data to stns for
            #--- each ntimes.
            for v in range(len(vars)):

                var = vars[v]
                vardat_c = vardat[var]
                
                #--- Loop over each ntime, grabbing interpolated data for
                #--- variable var for each stns.
                for n in range(ntimes):
                    dt_c = time_increment(dt, n, dtfmt)
                    for ns in range(len(stns)):
                        dat_interp = bilinear_interpolate(lat, lon, \
                                                          vardat_c[:,:,n],\
                                                          latpts[ns], \
                                                          lonpts[ns], 0)

                        #--- Do lapse rate correction for temperature.
                        if (var == 'T2MAX' or var == 'T2MIN'):
                            dat_c = dat_interp - std_lapse * elevs_diff[ns]
                        else:
                            dat_c = dat_interp

                        data_all[model, var, stns[ns], dt_c] = dat_c
                        dts_all.append(dt_c)

    #--- Get sorted unique list of all dates seen.
    dts_unique = list(sorted(set(dts_all)))
    
    #--- Create pickle file.
    pickle.dump((data_all, dts_unique, models, vars, stns), \
                open(pickle_file,'wb'), -1)

##---------------------------------------------------------------------------
## Get seasonal precipitation totals and make a time series plot of them.
##---------------------------------------------------------------------------
#var = 'PREC'
#(pcp_tot, years) = get_seasonal_stats(data_all, dts_unique, models, \
#                                      stns, var, 'tot')
#(pcp_max, years) = get_seasonal_stats(data_all, dts_unique, models, \
#                                      stns, var, 'max')
#mod = 'gfdl-cm3'
#for s in range(len(seasons)):
#    season = seasons[s]
#    plotfname = plot_dir + '/' + mod + '_' + sdt + '_' + edt + '_' + season + \
#                '_' + var + '_tot.png'
#    titlein = 'Total ' + seasons_lab[season] + ' Precipitation, ' + \
#              mod.upper() + ', ' + str(years[0]) + ' - ' + \
#              str(years[len(years)-1])
#    iret = ts(pcp_tot, years, stns, mod, titlein, plotfname, 'PREC', 'tot', \
#              stn_cols, [season])
#
#    plotfname = plot_dir + '/' + mod + '_' + sdt + '_' + edt + '_' + season + \
#                '_' + var + '_max.png'
#    titlein = 'Maximum 24-h ' + seasons_lab[season] + ' Precipitation, ' + \
#              mod.upper() + ', ' + str(years[0]) + ' - ' + \
#              str(years[len(years)-1])
#    iret = ts(pcp_max, years, stns, mod, titlein, plotfname, 'PREC', 'max', \
#              stn_cols, [season])
#
#---------------------------------------------------------------------------
# Get seasonal temperature maxes and mins.
#---------------------------------------------------------------------------
(tmx_max, years) = get_seasonal_stats(data_all, dts_unique, models, stns, \
                                      'T2MAX', 'max')
(tmn_min, years) = get_seasonal_stats(data_all, dts_unique, models, stns, \
                                      'T2MIN', 'min')

#mod = 'gfdl-cm3'
#for s in range(len(seasons)):
#    season = seasons[s]
#
#    #--- Max temperature.
#    var = 'T2MAX'
#    plotfname = plot_dir + '/' + mod + '_' + sdt + '_' + edt + '_' + season + \
#                '_' + var + '_max.png'
#    titlein = 'Maximum Seasonal Temperature, ' + seasons_lab[season] + ', ' +\
#              mod.upper() + ', ' + str(years[0]) + ' - ' + \
#              str(years[len(years)-1])
#    iret = ts(tmx_max, years, stns, mod, titlein, plotfname, var, 'max', \
#              stn_cols, [season])
#
#    #--- Min temperature.
#    var = 'T2MIN'
#    plotfname = plot_dir + '/' + mod + '_' + sdt + '_' + edt + '_' + season + \
#                '_' + var + '_min.png'
#    titlein = 'Minimum Seasonal Temperature, ' + seasons_lab[season] + ', ' +\
#              mod.upper() + ', ' + str(years[0]) + ' - ' + \
#              str(years[len(years)-1])
#    iret = ts(tmn_min, years, stns, mod, titlein, plotfname, var, 'min', \
#              stn_cols, [season])
#
##---------------------------------------------------------------------------
## Get seasonal temperature averages.
##---------------------------------------------------------------------------
#(tmx_avg, years) = get_seasonal_stats(data_all, dts_unique, models, stns, \
#                                      'T2MAX', 'avg')
#(tmn_avg, years) = get_seasonal_stats(data_all, dts_unique, models, stns, \
#                                      'T2MIN', 'avg')
#
#mod = 'gfdl-cm3'
#for s in range(len(seasons)):
#    season = seasons[s]
#
#    #--- Max temperature.
#    var = 'T2MAX'
#    plotfname = plot_dir + '/' + mod + '_' + sdt + '_' + edt + '_' + season + \
#                '_' + var + '_avg.png'
#    titlein = 'Average Maximum Temperature, ' + seasons_lab[season] + ', ' +\
#              mod.upper() + ', ' + str(years[0]) + ' - ' + \
#              str(years[len(years)-1])
#    iret = ts(tmx_avg, years, stns, mod, titlein, plotfname, var, 'avg', \
#              stn_cols, [season])
#
#    #--- Min temperature.
#    var = 'T2MIN'
#    plotfname = plot_dir + '/' + mod + '_' + sdt + '_' + edt + '_' + season + \
#                '_' + var + '_avg.png'
#    titlein = 'Average Minimum Temperature, ' + seasons_lab[season] + ', ' +\
#              mod.upper() + ', ' + str(years[0]) + ' - ' + \
#              str(years[len(years)-1])
#    iret = ts(tmn_avg, years, stns, mod, titlein, plotfname, var, 'avg', \
#              stn_cols, [season])
#
#sys.exit()

#---------------------------------------------------------------------------
# Wind Speed.
#---------------------------------------------------------------------------
mod = 'gfdl-cm3'
var = 'SPDUV10MEAN'
(spdavg_avg, years) = get_seasonal_stats(data_all, dts_unique, models, stns, \
                                         var, 'avg')
for s in range(len(seasons)):
    season = seasons[s]

    plotfname = plot_dir + '/' + mod + '_' + sdt + '_' + edt + '_' + season + \
                '_' + var + '_avg.png'
    titlein = 'Average Wind Speed, ' + seasons_lab[season] + ', ' +\
              mod.upper() + ', ' + str(years[0]) + ' - ' + \
              str(years[len(years)-1])
    iret = ts(spdavg_avg, years, stns, mod, titlein, plotfname, var, 'avg', \
              stn_cols, [season])

var = 'SPDUV10MAX'
(spdmax_max, years) = get_seasonal_stats(data_all, dts_unique, models, stns, \
                                         var, 'max')
for s in range(len(seasons)):
    season = seasons[s]

    plotfname = plot_dir + '/' + mod + '_' + sdt + '_' + edt + '_' + season + \
                '_' + var + '_max.png'
    titlein = 'Maximum Wind Speed, ' + seasons_lab[season] + ', ' +\
              mod.upper() + ', ' + str(years[0]) + ' - ' + \
              str(years[len(years)-1])
    iret = ts(spdmax_max, years, stns, mod, titlein, plotfname, var, 'max', \
              stn_cols, [season])


sys.exit()

<file_sep>/old/old_old/plot_wrfout_grid_avg_snowrad_20180801.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re, math
import numpy as np
#import matplotlib.pyplot as plt
from make_cmap import *
from netCDF4 import Dataset as NetCDFFile
from utils_wrfout import *
from utils_date import *
import pickle
from collections import defaultdict
from make_cmap import *

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
plot_dir   = rcm_dir + '/plots'
pickle_dir = rcm_dir + '/pickle'
run_dirs   = rcm_dir + '/rundirs'
geo_em     = rcm_dir + '/data' + '/geo_em.d02.nc'
data_dirs  = ['/home/disk/r2d2/steed/cmip5/rcp8.5', \
              '/home/disk/vader/steed/cmip5/rcp8.5', \
              '/home/disk/jabba/steed/cmip5/rcp8.5']

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
#vars = ['SNOW', 'SWDOWN']
vars = ['SNOW']

sdts = ['197001', '207001']
edts = ['200012', '209912']
#sdts = ['207001']
#edts = ['209912']

#models   = ['gfdl-cm3', 'miroc5', 'bcc-csm1.1', 'access1.3']
#models   = ['gfdl-cm3', 'miroc5', 'access1.3']
models = ['gfdl-cm3']
#models = ['access1.3']
dtfmt = "%Y%m%d%H"

#---------------------------------------------------------------------------
# Make a color map using make_cmap.
#---------------------------------------------------------------------------
levs_norm_c = []
for i in range(len(levs_temp)):
    x = float(levs_temp[i])
    norm_c = (x - min(levs_temp)) / (max(levs_temp) - min(levs_temp))
    levs_norm_c.append(norm_c)
cmap_temp = make_cmap(cmap_temp, bit = True, position = levs_norm_c)

levs_norm_c = []
for i in range(len(levs_pcp)):
    x = float(levs_pcp[i])
    norm_c = (x - min(levs_pcp)) / (max(levs_pcp) - min(levs_pcp))
    levs_norm_c.append(norm_c)
cmap_pcp = make_cmap(cmap_pcp, bit = True, position = levs_norm_c)

levs_norm_c = []
for i in range(len(levs_swe)):
    x = float(levs_swe[i])
    norm_c = (x - min(levs_swe)) / (max(levs_swe) - min(levs_swe))
    levs_norm_c.append(norm_c)
cmap_swe = make_cmap(cmap_swe, bit = True, position = levs_norm_c)

#---------------------------------------------------------------------------
# Make run directory, load geo_em file.
#---------------------------------------------------------------------------
rundir = run_dirs + '/plot_wrfout_grid_avg.' + str(os.getpid())
print 'making ', rundir
os.makedirs(rundir)

#rundir = '/home/disk/spock/jbaars/rcm/rundirs/plot_wrfout_grid_avg.4149'

print 'Loading ', geo_em
(lat, lon, hgt) = load_geo_em(geo_em)

#---------------------------------------------------------------------------
# For each model, make a maps of every season...
#---------------------------------------------------------------------------
for m in range(len(models)):

    model = models[m]

    for n in range(len(sdts)):
        sdt = sdts[n]
        edt = edts[n]
        yyyy_s = sdt[0:4]
        yyyy_e = edt[0:4]        

        #--- Get list of extracted files for this model.
        files = []
        for d in range(len(data_dirs)):
            path_c = data_dirs[d] + '/' + model + '/extract/*0401*.nc'
            files_c = glob.glob(path_c)
        
            for f in range(len(files_c)):
                if 'snowrad' not in files_c[f]:
                    continue

                dt_c = re.findall(r'\.(\d{10})\.', files_c[f])
                if (dt_c):
                    dt_c = dt_c[0]
                    yyyymm_c = dt_c[0:6]
                    if (yyyymm_c >= sdt and yyyymm_c <= edt):
                        files.append(files_c[f])

        #--- Make plot for each vars and zooms.
        for var in vars:
            file_plot = rundir + '/' + model + '_' + var + '_apr1_' + \
                        yyyy_s + '_' + yyyy_e + '.nc'
            syscom = 'ncra'
            for f in range(len(files)):
                syscom = syscom + ' ' + files[f]
                
            syscom = syscom + ' ' + file_plot
            print 'syscom = ', syscom        
            os.system(syscom)
        
            print 'Loading ', var, ' from ', file_plot
            nc = NetCDFFile(file_plot, 'r')
            ntimes = len(nc.dimensions['Time'])
            vardat = {}
            dat_tmp = nc.variables[var][:,:,:]
            dat_tmp = np.transpose(dat_tmp)
            vardat[var] = dat_tmp
            nc.close()

            titlein = model.upper() + ', ' + var_lab[var] + ', ' + \
                      'Apr 1st, ' + yyyy_s + ' - ' + yyyy_e
        
            plotdat = vardat[var][:,:,0]
        
            for z in range(len(zooms)):
                plotfname = rundir + '/' + model + '_' + sdt + '_' + \
                            edt + '_apr1_' + var + '_' + \
                            zooms[z] + '.png'
                mapper(var, lat, lon, plotdat, levs_swe, cmap_swe, \
                       maplw, colorbar_labs[var], titlein, plotfname, \
                       zooms[z])

#---------------------------------------------------------------------------
# Make map of difference.
#---------------------------------------------------------------------------
for m in range(len(models)):
    model = models[m]

    for var in vars:
        syscom = 'ncdiff'
        file_diff = rundir + '/' + model + '_' + var + '_apr1_' + 'diff.nc'
    
        for n in range(len(sdts)):
            sdt = sdts[n]
            edt = edts[n]
            yyyy_s = sdt[0:4]
            yyyy_e = edt[0:4]        

            file_plot = rundir + '/' + model + '_' + var + '_apr1_' + \
                        yyyy_s + '_' + yyyy_e + '.nc'
            if os.path.isfile(file_plot):
                syscom = syscom + ' ' + file_plot

        syscom = syscom + ' ' + file_diff

        print 'syscom = ', syscom        
        os.system(syscom)

        print 'Loading ', var, ' from ', file_diff
        nc = NetCDFFile(file_diff, 'r')
        ntimes = len(nc.dimensions['Time'])
        vardat = {}
        dat_tmp = nc.variables[var][:,:,:]
        dat_tmp = np.transpose(dat_tmp)
        vardat[var] = dat_tmp
        nc.close()

        titlein = model.upper() + ', ' + var_lab[var] + ', ' + \
                  'Apr 1st, Difference, ' + yyyy_s + ' - ' + yyyy_e
        
        plotdat = vardat[var][:,:,0]
        
        for z in range(len(zooms)):
            plotfname = rundir + '/' + model + '_' + sdt + '_' + \
                        edt + '_apr1_diff_' + var + '_' + \
                        zooms[z] + '.png'
            mapper(var, lat, lon, plotdat, levs_swe, cmap_swe, \
                   maplw, colorbar_labs[var], titlein, plotfname, \
                   zooms[z])


sys.exit()

<file_sep>/igtg_check_utils_miroc5.py
#!/usr/bin/python
import  os, os.path, time, glob, re, math
import sys, string, readline, h5py
from Scientific.IO.NetCDF import *
import numpy as np
#from datetime import datetime, timedelta, time
from datetime import datetime
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, cm, shiftgrid
#from rw_plotsettings import *
import pickle

colorbar_lab = 'm/s';
maplw = 0.8

#---------------------------------------------------------------------------
# Load 2-D lat/lon netcdf data file.
#---------------------------------------------------------------------------
def load_nc(filein, latlon2d):

    print 'reading ', filein
    f = NetCDFFile(filein, 'r')
    ndims = len(f.dimensions)
    dims  = f.dimensions
    dimnames = f.dimensions.keys()

    for d in range(ndims):
        print d, dimnames[d], dims[dimnames[d]]
        #--- Find ny dimension.
        if (dimnames[d] == 'lat' or dimnames[d] == 'rlat'):
            ny = dims[dimnames[d]]
        #--- Find nx dimension.
        if (dimnames[d] == 'lon' or dimnames[d] == 'rlon'):
            nx = dims[dimnames[d]]

    if (latlon2d == 1):
        lon_tmp = f.variables['lon'][:,:]
        lat_tmp = f.variables['lat'][:,:]
#        tos_tmp = f.variables['tos'][0,:,:]
        tos_tmp = f.variables['tos'][:,:]        
        lonout = np.transpose(lon_tmp)
        latout = np.transpose(lat_tmp)
        tosout = np.transpose(tos_tmp)        
    else:
        lon_tmp = f.variables['lon'][:]
        lat_tmp = f.variables['lat'][:]
        lonout = np.empty([nx,ny])
        latout = np.empty([nx,ny])
        for y in range(ny):
            latout[:,y] = lat_tmp[y]
        for x in range(nx):
            lonout[x,:] = lon_tmp[x]
        tosout = -9999

    f.close()

    return latout, lonout, tosout

#---------------------------------------------------------------------------
# Mapper.
#---------------------------------------------------------------------------
def mapper(i1, j1, i2, j2, lat1, lon1, lat2, lon2, tos_new, 
           minlat, maxlat, minlon, maxlon, title, plotfname):

    ur_lat = maxlat
    ur_lon = maxlon
    ll_lat = minlat
    ll_lon = minlon
    lat_ctr = ll_lat + ((ur_lat - ll_lat) * 0.5)
    lon_ctr = ll_lon + ((ur_lon - ll_lon) * 0.5)

    width   = 6.5
    height  = 6.5
    fs      = 9
    titlefs = 9

    fig = plt.figure(figsize=(width,height))
    # left, bottom, width, height:
    ax = fig.add_axes([0.00,0.05,0.99,0.91])

    #---
    map = Basemap(resolution='i',projection='lcc',\
                  llcrnrlon= ll_lon, llcrnrlat=ll_lat,\
                  urcrnrlon= ur_lon, urcrnrlat= ur_lat,\
                  lat_0=lat_ctr,lon_0=lon_ctr,lat_1=(ur_lat - ll_lat))

    #--- Draw coastlines, country boundaries, fill continents.
    map.drawcoastlines(linewidth = maplw)
    map.drawstates(linewidth = maplw)
    map.drawcountries(linewidth = maplw)

    #--- Draw the edge of the map projection region (the projection limb)
    map.drawmapboundary(linewidth = maplw)

    #--- Draw lat/lon grid lines every 30 degrees.
    map.drawmeridians(np.arange(0, 360, 30), linewidth = maplw)
    map.drawparallels(np.arange(-90, 90, 30), linewidth = maplw)

#    #--- Get lat and lon data in map's x/y coordinates.
#    x1,y1 = map(lon1, lat1)
#    x2,y2 = map(lon2, lat2)    

    #-- Add dots for grid points for 1st grid.
    nx,ny = np.shape(lon1)
    print 'nx, ny = ', nx, ny
    for ii in range(nx):
        for jj in range(ny):
            if (lat1[ii,jj] > minlat and lat1[ii,jj] < maxlat):
                lon_c = lon1[ii,jj]
                if (lon_c > 180.0):
                    lon_c = lon_c - 360
                if (lon_c > minlon and lon_c < maxlon):
                    if (ii == i1 and jj == j1):
                        mark = 'ro'
                    else:
                        mark = 'bo'                        
                    xs, ys = map(lon1[ii,jj], lat1[ii,jj])
                    plt.plot(xs, ys, mark, markersize = 5)
#                    plt.text(xs, ys, str(ii) + ',' + str(jj), fontsize=7)
                    plt.text(xs, ys, str(ii+1) + ',' + str(jj+1), fontsize=7)
                    
    #-- Add dots for grid points for 2nd grid.
    nx,ny = np.shape(lon2)
    print 'nx, ny = ', nx, ny
    for ii in range(nx):
        for jj in range(ny):
            if (lat2[ii,jj] > minlat and lat2[ii,jj] < maxlat):
                lon_c = lon2[ii,jj]
                if (lon_c > 180.0):
                    lon_c = lon_c - 360
                if (lon_c > minlon and lon_c < maxlon):
                    if (ii == i2 and jj == j2):
                        mark = 'rx'
                        ms = 2.0
                    else:
                        mark = 'kx'
                        ms = 1.2
#                    iip = ii - 1
#                    jjp = jj - 1
#                    xs, ys = map(lon2[iip,jjp], lat2[iip,jjp])
                    xs, ys = map(lon2[ii,jj], lat2[ii,jj])
                    plt.plot(xs, ys, mark, markersize = 6, mew=ms)
#                    plt.text(xs, ys, str(ii) + ',' + str(jj), fontsize=6)
                    strp = "%d,%d,%.3f" % (ii+1, jj+1, tos_new[ii+1,jj+1])
#                    strp = "%d,%d,%.3f" % (ii-1, jj-1, tos_new[ii-1,jj-1])
#                    strp = "%d,%d,%.3f" % (iip, jjp, tos_new[iip,jjp])
#                    strp = "%d,%d,%.3f" % (ii+2, jj+2, tos_new[ii+2,jj+2])
#                    strp = "%d,%d,%.3f" % (ii+6, jj+6, tos_new[ii+6,jj+6]) 
#                    strp = "%d,%d,%.3f" % (ii+3, jj+3, tos_new[ii+3,jj+3])
#                    strp = "%d,%d,%.3f" % (ii-2, jj-2, tos_new[ii-2,jj-2])
#                    strp = "%d,%d,%.3f" % (ii-3, jj-3, tos_new[ii-3,jj-3])
                    plt.text(xs, ys, strp, fontsize=6)

    plt.title(title, fontsize=titlefs, fontweight='bold')

    #--- Save plot.
    print 'creating ', plotfname
    plt.savefig(plotfname)

    plt.close()

<file_sep>/check_extract_files.py
#!/usr/bin/python
import  sys, os, os.path, time, glob, re
import numpy as np
from make_cmap import *
from netCDF4 import Dataset as NetCDFFile
from utils_wrfout import *
from utils_date import *
from utils_ghcnd_obs import *
import pickle
import csv
import json

#---------------------------------------------------------------------------
# Paths.
#---------------------------------------------------------------------------
rcm_dir    = '/home/disk/spock/jbaars/rcm'
py_dir     = rcm_dir + '/python'
#plot_dir   = rcm_dir + '/plots'
plot_dir   = rcm_dir + '/plots/new'
pickle_dir = rcm_dir + '/pickle'
data_dir   = rcm_dir + '/data'
ghcnd_dir  = data_dir + '/ghcnd/ghcnd_all'
data_dirs  = ['/home/disk/r2d2/steed/cmip5/rcp8.5', \
              '/home/disk/vader/steed/cmip5/rcp8.5', \
              '/home/disk/jabba/steed/cmip5/rcp8.5']

geo_em = data_dir + '/geo_em.d02.nc'
station_file = data_dir + '/station_file.txt'

#---------------------------------------------------------------------------
# Settings.
#---------------------------------------------------------------------------
#vars_mod = ['PREC', 'T2MAX', 'T2MIN']
vars_mod = ['T2MAX']

sdt = '197001'
edt = '209912'

#--- GHCND data downloaded through June 2019, so 2019 is a partial year, so
#--- only include obs through 2018.
edt_obs = '201812' 

var_obs_dict = {
    'PREC': 'PRCP',
    'T2MAX': 'TMAX',
    'T2MIN': 'TMIN',
    'T2MEAN': 'T2MEAN'    
    }
stats_dict = {
    'PREC': ['max', 'tot'],
    'T2MAX': ['max', 'avg'],
    'T2MIN': ['min', 'max', 'avg'],
    'T2MEAN': ['min', 'max', 'avg'],    
    }

models = ['access1.0', 'access1.3', 'bcc-csm1.1', 'canesm2', \
          'ccsm4', 'csiro-mk3.6.0', 'fgoals-g2', 'gfdl-cm3', \
          'giss-e2-h', 'miroc5', 'mri-cgcm3', 'noresm1-m']
models = ['access1.0']

temp_max = 273 + 60
temp_min = 273 - 60

for m in range(len(models)):
    data_all = {}
    dts_all = []
    model = models[m]

    #--- Get list of extracted files for this model.
    print 'model = ', model
    files = get_extract_files(data_dirs, model, sdt, edt, 0)
    files = list(sorted(set(files)))

    #--- Loop over all extract files, reading them in.
    for f in range(len(files)):
        (vardat, ntimes, dt) = load_wrfout(files[f], vars_mod)

        for v in range(len(vars_mod)):
            var = vars_mod[v]
            dat_c = vardat[var]
            x,y,z = dat_c.shape
            for k in range(z):
                max_c = np.amax(dat_c[:,:,k])
                if max_c < temp_min or max_c > temp_max:
                    print model, ', ', files[f], ', ', k
                          
                





'''
#---------------------------------------------------------------------------
# Load model data.
#---------------------------------------------------------------------------
load_mod_all = 1
pf_mod_stats = pickle_dir + '/mod_stats.pkl'
if load_mod_all == 0:
    #--- If not loading all model data, just load stats pickle file.
    if os.path.isfile(pf_mod_stats):
        print 'reading pickle file ', pf_mod_stats
        (mod,yy_mod) = pickle.load(open(pf_mod_stats, 'rb'))
    else:
        sys.exit('cannot see ' + pf_mod_stats)
else:

    (data_all, dts_unique, models, vars_all, stns) = \
               load_extract_data(geo_em, stns, latpts, lonpts, elevs, models, \
                                 data_dirs, sdt, edt, vars_mod, pickle_dir, 0)
    sys.exit()
    
    data_all = {}
    dts_all = []
    for m in range(len(models)):
        model = models[m]
        pf = get_mod_pkl_name(pickle_dir, model, sdt, edt)
        print 'loading ', pf
        (model, data_c, dts_unique, vars, stns) = pickle.load(open(pf, 'rb'))
        data_all[model] = data_c
        dts_all = dts_all + dts_unique
    mod = {}
    yy_mod = {}
    for var in vars_mod:
        stats = stats_dict[var]        
        for stat in stats:
            (mod[var,stat], yy_mod[var,stat]) = get_seasonal_stats(\
                data_all, dts_unique, models, stns, var, stat)
    pickle.dump((mod,yy_mod), open(pf_mod_stats,'wb'), -1)

sys.exit()

#---------------------------------------------------------------------------
# Make a time series plots of each station, variable, stat and season.
#---------------------------------------------------------------------------
#--- Do multiple models on a plot for one station.
for ss in range(len(stns)):
    stn = stns[ss]
    for v in range(len(vars_mod)):
        var = vars_mod[v]
        varo = var_obs_dict[var]
        stats = stats_dict[var]
        for st in range(len(stats)):
            stat = stats[st]
            for s in range(len(seasons)):
                season = seasons[s]
                plotfname = get_plotfname(plot_dir,stn,sdt,edt,season,var,stat)
                titlein = get_title(season,stn,var,stat,yy_mod[var,stat])
                iret = ts_mods(mod[var,stat],yy_mod[var,stat], \
                               obs[varo,stat],yy_obs[varo,stat], stn, \
                               models, titlein, plotfname, var, stat, \
                               mod_cols, [season])
#                sys.exit()
    sys.exit()
sys.exit()

'''
