#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Dec  1 15:01:55 2020

@author: jabadgeley
"""
import numpy as np

# acceleration due to gravity at sea level (m/s^2)
gravity = 9.81

# latent heat of vaporization (J/kg at 0 degrees Celsius)
latent_heat_vaporization = 2.5e6

# specific heat of dry air (J/kgK)
specific_heat_dry_air = 1004.

# gas constant for dry air (J/kgK)
gas_constant_dry_air = 287.

# gas constant for water vapor (J/kgK)
gas_constant_water_vapor = 461.

# ratio: gas constant dry air / gas constant water vapor (epsilon = Rd/Rv)
epsilon = gas_constant_dry_air / gas_constant_water_vapor

# dry adiabatic lapse rate (K/m)
adiabatic_lapse_rate_dry = gravity / specific_heat_dry_air

# reference pressure (Pa)
reference_pressure = 100000.

# centimeters to inches conversion
cm2in = 1/2.54  

# ice core locations dictionary
icecores = {'EDML':(360.07,-75.0),
            'DF':(39.7,-77.32),
            'DB':(94.92, -77.08),
            'Vostok':(106.87,-78.47),
            'EDC':(123.35,-75.1),
            'Law':(112.83,-66.73), 
            'Talos':(159.18,-72.82),
            'Taylor':(158.72,-77.8), 
            'Siple':(-148.82,-81.67),
            'Byrd':(-119.52,-80.02), 
            'WDC':(-112.14,-79.46),
            'Herc':(-105.,-86.),
            'SP':[-95.1,-89.98],
            'WAIS 2':['WDC','Byrd'],
            'WAIS 3':['WDC','Byrd','Herc'],
            'EAIS 3': ['DF', 'EDC', 'Vostok'],
            'EAIS 4': ['DF', 'EDC', 'Vostok', 'EDML'],
            'AIS All': ['WDC', 'SP', 'EDML', 'EDC', 
                        'Talos', 'DF', 'Taylor',
                        'Vostok', 'Byrd', 'Siple',
                        'Law', 'DB'
                        ]}

# values for the ensemble
ensemble_values = {'model_groups':['PMIP3','PMIP3','PMIP3','PMIP3','PMIP3',
                                   'PMIP4','PMIP4','PMIP4','PMIP4',
                                   'New_Model_Output'],
                   'model_names':['CNRM','FGOALS','GISS','IPSL','MRI',
                                  'AWI','INM','MIROC','MPI','CAM5'],
                   'experiments_recent':['piControl','piControl',
                                         'piControl','piControl',
                                         'piControl','piControl',
                                         'piControl','piControl',
                                         'piControl','PI_control_gtopo30'],
                   'experiments_past':['lgm','lgm','lgm','lgm','lgm','lgm',
                                       'lgm','lgm','lgm',
                                       'LGM_Si_real_avg_SAT_TMP373_RVP'], #full_lgm_no_AA_120m_sea_level_gtopo30
                   'subexperiments_recent':['r1i1p1','r1i1p1','r1i1p1',
                                            'r1i1p1','r1i1p1','r1i1p1f1',
                                            'r1i1p1f1','r1i1p1f2','r1i1p1f1',
                                            ''],
                   'subexperiments_past':['r1i1p1','r1i1p1','r1i1p150',
                                          'r1i1p1','r1i1p1','r1i1p1f1',
                                          'r1i1p1f1','r1i1p1f2','r1i1p1f1',''],
                   'frequencies':['Aclim','Aclim','Aclim','Aclim','Aclim', 
                                  'Aclim','Aclim','Aclim','Aclim','monthly'],
                   'huss':['huss','hus','huss','hus','huss','huss','huss',
                           'huss','huss',''],
                   'hurs':['hurs','hur','hurs','hur','hurs','hurs','hurs',
                           'hurs','hurs',''],
                   'titles':['PMIP3: CNRM', 'PMIP3: FGOALS', 'PMIP3: GISS',
                             'PMIP3: IPSL', 'PMIP3: MRI', 
                             'PMIP4: AWI', 'PMIP4: INM', 
                             'PMIP4: MIROC', 'PMIP4: MPI', 'CAM5']
                   }

# regrid to these lat and lons
regrid_data = {'lat':np.linspace(-90,90,181),
               'lon':np.linspace(0,359,360)}

# The following regrid_data_irregular was computed with:
# import cartopy.crs as ccrs
# import numpy as np
# Sample locations
#sample_xs = np.linspace(-2600000, 2600000, 27)
#sample_ys = np.linspace(-2600000, 2600000, 27)
#meshxs, meshys = np.meshgrid(sample_xs, sample_ys)
#points_meshlatlon = ccrs.Geodetic().transform_points(src_crs = ccrs.SouthPolarStereo(),
#                                                     x=meshxs, y=meshys)
#points_latlon = points_meshlatlon[...,:2].reshape([-1,2])
#lats = []
#lons = []
#for ii, lon in enumerate(points_latlon[:,0]):
#    lat = points_latlon[ii,1]
#    if Ant_poly.intersects(Point(lon, lat)):
#        lons += [lon]
#        lats += [lat]
#regrid_data_irregular = {}
#regrid_data_irregular['lat'] = lats
#regrid_data_irregular['lon'] = lons
regrid_data_irregular = {'lat':[-71.88563091955609,
                     -71.46239999061652,
                     -70.88665451705731,
                     -70.17267271384074,
                     -69.33599972822388,
                     -68.39229845573956,
                     -67.3565270104634,
                     -73.13081092575067,
                     -72.49647721365037,
                     -71.71508511194318,
                     -70.8059133908041,
                     -69.78772502062775,
                     -68.67782580903652,
                     -67.49160884705043,
                     -66.24244233890475,
                     -74.787438394019,
                     -74.08334405003902,
                     -73.22350177108659,
                     -72.23195358717996,
                     -71.13110091544634,
                     -69.94074739852175,
                     -68.67782580903652,
                     -67.3565270104634,
                     -76.42444652319007,
                     -75.63632752958513,
                     -74.68477714637953,
                     -73.59979489875644,
                     -72.40783364466198,
                     -71.13110091544634,
                     -69.78772502062775,
                     -68.39229845573956,
                     -66.956511734001,
                     -74.89082910278142,
                     -76.08100660895126,
                     -78.03020965262921,
                     -78.03020965262921,
                     -77.13983143379339,
                     -76.08100660895126,
                     -74.89082910278142,
                     -73.59979489875644,
                     -72.23195358717996,
                     -70.8059133908041,
                     -69.33599972822388,
                     -67.83327887811996,
                     -76.08100660895126,
                     -77.38761590170691,
                     -78.57103749719082,
                     -79.58665840023174,
                     -80.37904340231306,
                     -79.58665840023174,
                     -78.57103749719082,
                     -77.38761590170691,
                     -76.08100660895126,
                     -74.68477714637953,
                     -73.22350177108659,
                     -71.71508511194318,
                     -70.17267271384074,
                     -68.60605411873061,
                     -67.0226472817909,
                     -75.63632752958513,
                     -77.13983143379339,
                     -78.57103749719082,
                     -79.89600629374526,
                     -81.06438901494425,
                     -82.00463805800558,
                     -82.00463805800558,
                     -81.06438901494425,
                     -79.89600629374526,
                     -78.57103749719082,
                     -77.13983143379339,
                     -75.63632752958513,
                     -74.08334405003902,
                     -72.49647721365037,
                     -70.88665451705731,
                     -69.26174772724161,
                     -67.62758875765805,
                     -76.42444652319007,
                     -78.03020965262921,
                     -79.58665840023174,
                     -81.06438901494425,
                     -82.41375297869291,
                     -83.55041894290635,
                     -84.342020551227,
                     -84.63195091295835,
                     -84.342020551227,
                     -83.55041894290635,
                     -82.41375297869291,
                     -81.06438901494425,
                     -79.58665840023174,
                     -78.03020965262921,
                     -76.42444652319007,
                     -74.787438394019,
                     -73.13081092575067,
                     -71.46239999061652,
                     -69.78772502062775,
                     -68.11083463273563,
                     -66.4348092805271,
                     -73.59979489875644,
                     -75.31199659582853,
                     -77.01776892210273,
                     -78.71040041310147,
                     -80.37904340231306,
                     -82.00463805800558,
                     -83.55041894290635,
                     -84.93856013100968,
                     -85.99764394431013,
                     -86.41990455796258,
                     -85.99764394431013,
                     -84.93856013100968,
                     -83.55041894290635,
                     -82.00463805800558,
                     -80.37904340231306,
                     -78.71040041310147,
                     -77.01776892210273,
                     -75.31199659582853,
                     -73.59979489875644,
                     -71.88563091955609,
                     -70.17267271384074,
                     -68.46329303716269,
                     -66.75935285325657,
                     -73.88809340336006,
                     -75.63632752958513,
                     -77.38761590170691,
                     -79.13948464619006,
                     -80.8881373330936,
                     -82.62691915587232,
                     -84.342020551227,
                     -85.99764394431013,
                     -87.46809500735466,
                     -88.20953302062978,
                     -87.46809500735466,
                     -85.99764394431013,
                     -84.342020551227,
                     -82.62691915587232,
                     -80.8881373330936,
                     -79.13948464619006,
                     -77.38761590170691,
                     -75.63632752958513,
                     -73.88809340336006,
                     -72.14470321157323,
                     -70.40755910911066,
                     -68.67782580903652,
                     -66.95651173400098,
                     -73.98540615757254,
                     -75.74614564519538,
                     -77.51341051571873,
                     -79.28642261353893,
                     -81.06438901494425,
                     -82.84650396865064,
                     -84.63195091295835,
                     -86.41990455796258,
                     -88.20953302062978,
                     -90.0,
                     -88.20953302062978,
                     -86.41990455796258,
                     -84.63195091295835,
                     -82.84650396865064,
                     -81.06438901494425,
                     -79.28642261353893,
                     -77.51341051571873,
                     -75.74614564519538,
                     -73.98540615757254,
                     -72.23195358717996,
                     -70.48653109120028,
                     -68.74986180003033,
                     -67.0226472817909,
                     -73.88809340336006,
                     -75.63632752958513,
                     -77.38761590170691,
                     -80.8881373330936,
                     -82.62691915587232,
                     -84.342020551227,
                     -85.99764394431013,
                     -87.46809500735466,
                     -88.20953302062978,
                     -87.46809500735466,
                     -85.99764394431013,
                     -84.342020551227,
                     -82.62691915587232,
                     -80.8881373330936,
                     -79.13948464619006,
                     -77.38761590170691,
                     -75.63632752958513,
                     -73.88809340336006,
                     -72.14470321157323,
                     -70.40755910911066,
                     -68.67782580903652,
                     -73.59979489875644,
                     -75.31199659582853,
                     -83.55041894290635,
                     -84.93856013100968,
                     -85.99764394431013,
                     -86.41990455796258,
                     -85.99764394431013,
                     -84.93856013100968,
                     -83.55041894290635,
                     -82.00463805800558,
                     -80.37904340231306,
                     -78.71040041310147,
                     -77.01776892210273,
                     -75.31199659582853,
                     -73.59979489875644,
                     -71.88563091955609,
                     -70.17267271384074,
                     -68.46329303716269,
                     -74.787438394019,
                     -83.55041894290635,
                     -84.342020551227,
                     -84.63195091295835,
                     -84.342020551227,
                     -83.55041894290635,
                     -82.41375297869291,
                     -81.06438901494425,
                     -79.58665840023174,
                     -78.03020965262921,
                     -76.42444652319007,
                     -74.787438394019,
                     -73.13081092575067,
                     -71.46239999061652,
                     -72.49647721365037,
                     -74.08334405003902,
                     -81.06438901494425,
                     -82.00463805800558,
                     -82.62691915587232,
                     -82.84650396865064,
                     -82.62691915587232,
                     -82.00463805800558,
                     -81.06438901494425,
                     -79.89600629374526,
                     -78.57103749719082,
                     -77.13983143379339,
                     -75.63632752958513,
                     -74.08334405003902,
                     -72.49647721365037,
                     -70.88665451705731,
                     -68.60605411873061,
                     -70.17267271384074,
                     -71.71508511194318,
                     -80.37904340231306,
                     -80.8881373330936,
                     -81.06438901494425,
                     -80.8881373330936,
                     -80.37904340231306,
                     -79.58665840023174,
                     -78.57103749719082,
                     -77.38761590170691,
                     -76.08100660895126,
                     -74.68477714637953,
                     -73.22350177108659,
                     -71.71508511194318,
                     -70.17267271384074,
                     -68.60605411873061,
                     -78.03020965262921,
                     -78.71040041310147,
                     -79.13948464619006,
                     -79.28642261353893,
                     -79.13948464619006,
                     -78.71040041310147,
                     -78.03020965262921,
                     -77.13983143379339,
                     -76.08100660895126,
                     -74.89082910278142,
                     -73.59979489875644,
                     -72.23195358717996,
                     -70.8059133908041,
                     -69.33599972822388,
                     -67.83327887811996,
                     -76.42444652319007,
                     -77.01776892210273,
                     -77.38761590170691,
                     -77.51341051571873,
                     -77.38761590170691,
                     -77.01776892210273,
                     -76.42444652319007,
                     -75.63632752958513,
                     -74.68477714637953,
                     -73.59979489875644,
                     -72.40783364466198,
                     -71.13110091544634,
                     -69.78772502062775,
                     -68.39229845573956,
                     -75.31199659582853,
                     -75.63632752958513,
                     -75.74614564519538,
                     -75.63632752958513,
                     -75.31199659582853,
                     -74.787438394019,
                     -74.08334405003902,
                     -73.22350177108659,
                     -72.23195358717996,
                     -71.13110091544634,
                     -69.94074739852175,
                     -68.67782580903652,
                     -67.3565270104634,
                     -65.98861668340835,
                     -73.59979489875644,
                     -73.88809340336006,
                     -73.98540615757254,
                     -73.88809340336006,
                     -73.59979489875644,
                     -73.13081092575067,
                     -72.49647721365037,
                     -71.71508511194318,
                     -70.8059133908041,
                     -69.78772502062775,
                     -68.67782580903652,
                     -72.14470321157323,
                     -72.23195358717996,
                     -72.14470321157323,
                     -71.88563091955609,
                     -71.46239999061652,
                     -70.88665451705731],
                'lon': [168.6900675259798,
                     163.30075576600638,
                     158.19859051364818,
                     153.434948822922,
                     149.03624346792648,
                     145.00797980144134,
                     141.34019174590992,
                     161.565051177078,
                     156.03751102542182,
                     150.94539590092285,
                     146.30993247402023,
                     142.12501634890182,
                     138.3664606634298,
                     135.0,
                     131.98721249581666,
                     159.44395478041653,
                     153.434948822922,
                     147.9946167919165,
                     143.13010235415598,
                     138.81407483429035,
                     135.0,
                     131.63353933657018,
                     128.6598082540901,
                     156.80140948635182,
                     150.25511870305778,
                     144.46232220802563,
                     139.39870535499554,
                     135.0,
                     131.18592516570965,
                     127.87498365109822,
                     124.99202019855868,
                     122.47119229084849,
                     -135.0,
                     -140.19442890773482,
                     -153.434948822922,
                     153.434948822922,
                     146.30993247402023,
                     140.19442890773482,
                     135.0,
                     130.6012946450045,
                     126.86989764584402,
                     123.69006752597979,
                     120.96375653207352,
                     118.61045966596524,
                     -129.8055710922652,
                     -135.0,
                     -141.34019174590992,
                     -149.03624346792648,
                     158.19859051364818,
                     149.03624346792648,
                     141.34019174590992,
                     135.0,
                     129.8055710922652,
                     125.53767779197437,
                     122.0053832080835,
                     119.05460409907714,
                     116.56505117707799,
                     114.44395478041653,
                     112.61986494804043,
                     -119.74488129694222,
                     -123.69006752597979,
                     -128.6598082540901,
                     -135.0,
                     -143.13010235415598,
                     -153.434948822922,
                     153.434948822922,
                     143.13010235415598,
                     135.0,
                     128.6598082540901,
                     123.69006752597979,
                     119.74488129694222,
                     116.56505117707799,
                     113.96248897457819,
                     111.80140948635182,
                     109.98310652189998,
                     108.43494882292202,
                     -113.1985905136482,
                     -116.56505117707799,
                     -120.96375653207352,
                     -126.86989764584402,
                     -135.0,
                     -146.30993247402023,
                     -161.565051177078,
                     180.0,
                     161.565051177078,
                     146.30993247402023,
                     135.0,
                     126.86989764584402,
                     120.96375653207352,
                     116.56505117707799,
                     113.1985905136482,
                     110.55604521958347,
                     108.43494882292202,
                     106.69924423399362,
                     105.25511870305779,
                     104.03624346792648,
                     102.9946167919165,
                     -102.52880770915152,
                     -104.03624346792648,
                     -105.94539590092286,
                     -108.43494882292202,
                     -111.80140948635182,
                     -116.56505117707799,
                     -123.69006752597979,
                     -135.0,
                     -153.434948822922,
                     180.0,
                     153.434948822922,
                     135.0,
                     123.69006752597979,
                     116.56505117707799,
                     111.80140948635182,
                     108.43494882292202,
                     105.94539590092286,
                     104.03624346792648,
                     102.52880770915152,
                     101.30993247402021,
                     100.30484646876603,
                     99.46232220802563,
                     98.74616226255522,
                     -96.3401917459099,
                     -97.1250163489018,
                     -98.13010235415598,
                     -99.46232220802563,
                     -101.30993247402021,
                     -104.03624346792648,
                     -108.43494882292202,
                     -116.56505117707799,
                     -135.0,
                     180.0,
                     135.0,
                     116.56505117707799,
                     108.43494882292202,
                     104.03624346792648,
                     101.30993247402021,
                     99.46232220802563,
                     98.13010235415598,
                     97.1250163489018,
                     96.3401917459099,
                     95.71059313749964,
                     95.1944289077348,
                     94.76364169072617,
                     94.39870535499554,
                     -90.0,
                     -90.0,
                     -90.0,
                     -90.0,
                     -90.0,
                     -90.0,
                     -90.0,
                     -90.0,
                     -90.0,
                     0.0,
                     90.0,
                     90.0,
                     90.0,
                     90.0,
                     90.0,
                     90.0,
                     90.0,
                     90.0,
                     90.0,
                     90.0,
                     90.0,
                     90.0,
                     90.0,
                     -83.6598082540901,
                     -82.8749836510982,
                     -81.86989764584403,
                     -78.69006752597979,
                     -75.96375653207353,
                     -71.56505117707799,
                     -63.43494882292201,
                     -45.0,
                     0.0,
                     45.0,
                     63.43494882292201,
                     71.56505117707799,
                     75.96375653207353,
                     78.69006752597979,
                     80.53767779197439,
                     81.86989764584403,
                     82.8749836510982,
                     83.6598082540901,
                     84.28940686250036,
                     84.8055710922652,
                     85.23635830927383,
                     -77.47119229084849,
                     -75.96375653207353,
                     -56.309932474020215,
                     -45.0,
                     -26.56505117707799,
                     0.0,
                     26.56505117707799,
                     45.0,
                     56.309932474020215,
                     63.43494882292201,
                     68.19859051364818,
                     71.56505117707799,
                     74.05460409907715,
                     75.96375653207353,
                     77.47119229084849,
                     78.69006752597979,
                     79.69515353123397,
                     80.53767779197439,
                     -69.44395478041653,
                     -33.690067525979785,
                     -18.43494882292201,
                     0.0,
                     18.43494882292201,
                     33.690067525979785,
                     45.0,
                     53.13010235415598,
                     59.03624346792648,
                     63.43494882292201,
                     66.80140948635182,
                     69.44395478041653,
                     71.56505117707799,
                     73.30075576600639,
                     -66.03751102542181,
                     -63.43494882292201,
                     -36.86989764584402,
                     -26.56505117707799,
                     -14.036243467926479,
                     0.0,
                     14.036243467926479,
                     26.56505117707799,
                     36.86989764584402,
                     45.0,
                     51.34019174590991,
                     56.309932474020215,
                     60.25511870305777,
                     63.43494882292201,
                     66.03751102542181,
                     68.19859051364818,
                     -65.55604521958347,
                     -63.43494882292201,
                     -60.94539590092286,
                     -21.80140948635181,
                     -11.309932474020215,
                     0.0,
                     11.309932474020215,
                     21.80140948635181,
                     30.96375653207352,
                     38.659808254090095,
                     45.0,
                     50.19442890773481,
                     54.46232220802562,
                     57.9946167919165,
                     60.94539590092286,
                     63.43494882292201,
                     65.55604521958347,
                     -26.56505117707799,
                     -18.43494882292201,
                     -9.462322208025617,
                     0.0,
                     9.462322208025617,
                     18.43494882292201,
                     26.56505117707799,
                     33.690067525979785,
                     39.80557109226519,
                     45.0,
                     49.398705354995535,
                     53.13010235415598,
                     56.309932474020215,
                     59.03624346792648,
                     61.38954033403478,
                     -23.19859051364819,
                     -15.945395900922858,
                     -8.13010235415598,
                     0.0,
                     8.13010235415598,
                     15.945395900922858,
                     23.19859051364819,
                     29.74488129694223,
                     35.53767779197438,
                     40.60129464500447,
                     45.0,
                     48.81407483429036,
                     52.1250163489018,
                     55.00797980144134,
                     -14.036243467926479,
                     -7.125016348901798,
                     0.0,
                     7.125016348901798,
                     14.036243467926479,
                     20.556045219583467,
                     26.56505117707799,
                     32.005383208083494,
                     36.86989764584402,
                     41.18592516570964,
                     45.0,
                     48.3664606634298,
                     51.34019174590991,
                     53.97262661489639,
                     -12.528807709151511,
                     -6.34019174590991,
                     0.0,
                     6.34019174590991,
                     12.528807709151511,
                     18.43494882292201,
                     23.962488974578186,
                     29.054604099077146,
                     33.690067525979785,
                     37.8749836510982,
                     41.6335393365702,
                     -5.710593137499643,
                     0.0,
                     5.710593137499643,
                     11.309932474020215,
                     16.69924423399362,
                     21.80140948635181]}