"""
Functions are useful untilities for SITperturb experiments
 
Notes
-----
    Author : Zachary Labe
    Date   : 13 August 2017
    
Usage
-----
    [1] calcDecJan(varx,vary,lat,lon,level,levsq)
    [2] calcDecJanFeb(varx,vary,lat,lon,level,levsq)
    [3] calc_indttest(varx,vary)
    [4] calc_weightedAve(var,lats)
    [5] calc_spatialCorr(varx,vary,lats,lons,weight)
    [6] calc_RMSE(varx,vary,lats,lons,weight)
    [7] calc_spatialCorrHeight(varx,vary,lats,lons,weight)
    [8] calc_spatialCorrHeightLev(varx,vary,lats,lons,weight,levelq)
"""

def calcDecJan(varx,vary,lat,lon,level,levsq):
    """
    Function calculates average for December-January

    Parameters
    ----------
    varx : 4d array or 5d array
        [year,month,lat,lon] or [year,month,lev,lat,lon]
    vary : 4d array or 5d array
        [year,month,lat,lon] or [year,month,lev,lat,lon]
    lat : 1d numpy array
        latitudes
    lon : 1d numpy array
        longitudes
    level : string
        Height of variable (surface or profile)
    levsq : integer
        number of levels
        
    Returns
    -------
    varx_dj : 3d array or 4d array
        [year,lat,lon] or [year,lev,lat,lon]
    vary_dj : 3d array
        [year,lat,lon] or [year,lev,lat,lon]

    Usage
    -----
    varx_dj,vary_dj = calcDecJan(varx,vary,lat,lon,level,levsq)
    """
    print('\n>>> Using calcDecJan function!')
    
    ### Import modules
    import numpy as np
    
    ### Reshape for 3d variables
    if level == 'surface':    
        varxravel = np.reshape(varx.copy(),
                           (int(varx.shape[0]*12),
                            int(lat.shape[0]),int(lon.shape[0])))
        varyravel = np.reshape(vary.copy(),
                               (int(vary.shape[0]*12),
                                int(lat.shape[0]),int(lon.shape[0]))) 
                               
        varx_dj = np.empty((varx.shape[0]-1,lat.shape[0],lon.shape[0]))
        vary_dj = np.empty((vary.shape[0]-1,lat.shape[0],lon.shape[0]) )                 
        for i in range(0,varxravel.shape[0]-12,12):
            counter = 0
            if i >= 12:
                counter = i//12
            djappendh = np.append(varxravel[11+i,:,:],varxravel[12+i,:,:])
            djappendf = np.append(varyravel[11+i,:,:],varyravel[12+i,:,:])    
            varx_dj[counter,:,:] = np.nanmean(np.reshape(djappendh,
                                    (2,int(lat.shape[0]),int(lon.shape[0]))),
                                    axis=0)                   
            vary_dj[counter,:,:] = np.nanmean(np.reshape(djappendf,
                                    (2,int(lat.shape[0]),int(lon.shape[0]))),
                                    axis=0)
    ### Reshape for 4d variables
    elif level == 'profile':
        varxravel = np.reshape(varx.copy(),
                           (int(varx.shape[0]*12.),levsq,
                            int(lat.shape[0]),int(lon.shape[0])))
        varyravel = np.reshape(vary.copy(),
                               (int(vary.shape[0]*12.),levsq,
                                int(lat.shape[0]),int(lon.shape[0]))) 
                               
        varx_dj = np.empty((int(varx.shape[0]-1),levsq,
                            int(lat.shape[0]),int(lon.shape[0])))
        vary_dj = np.empty((int(vary.shape[0]-1),levsq,
                            int(lat.shape[0]),int(lon.shape[0])) )                 
        for i in range(0,varxravel.shape[0]-12,12):
            counter = 0
            if i >= 12:
                counter = i//12
            djappendh = np.append(varxravel[11+i,:,:,:],
                                  varxravel[12+i,:,:,:])
            djappendf = np.append(varyravel[11+i,:,:,:],
                                  varyravel[12+i,:,:,:])    
            varx_dj[counter,:,:] = np.nanmean(np.reshape(djappendh,
                                    (2,levsq,int(lat.shape[0]),
                                     int(lon.shape[0]))),axis=0)                   
            vary_dj[counter,:,:] = np.nanmean(np.reshape(djappendf,
                                    (2,levsq,int(lat.shape[0]),
                                     int(lon.shape[0]))),axis=0)                               
    else:
        print(ValueError('Selected wrong height - (surface or profile!)!'))    
                                
    print('Completed: Organized data by months (ON,DJ,FM)!')

    print('*Completed: Finished calcDecJan function!')
    return varx_dj,vary_dj

###############################################################################
###############################################################################
###############################################################################

def calcDecJanFeb(varx,vary,lat,lon,level,levsq):
    """
    Function calculates average for December-January-February

    Parameters
    ----------
    varx : 4d array or 5d array
        [year,month,lat,lon] or [year,month,lev,lat,lon]
    vary : 4d array or 5d array
        [year,month,lat,lon] or [year,month,lev,lat,lon]
    lat : 1d numpy array
        latitudes
    lon : 1d numpy array
        longitudes
    level : string
        Height of variable (surface or profile)
    levsq : integer
        number of levels
        
    Returns
    -------
    varx_djf : 3d array or 4d array
        [year,lat,lon] or [year,lev,lat,lon]
    vary_djf : 3d array
        [year,lat,lon] or [year,lev,lat,lon]

    Usage
    -----
    varx_djf,vary_djf = calcDecJanFeb(varx,vary,lat,lon,level,levsq)
    """
    print('\n>>> Using calcDecJan function!')
    
    ### Import modules
    import numpy as np
    
    ### Reshape for 3d variables
    if level == 'surface':    
        varxravel = np.reshape(varx.copy(),
                           (int(varx.shape[0]*12),
                            int(lat.shape[0]),int(lon.shape[0])))
        varyravel = np.reshape(vary.copy(),
                               (int(vary.shape[0]*12),
                                int(lat.shape[0]),int(lon.shape[0]))) 
                               
        varx_djf = np.empty((varx.shape[0]-1,lat.shape[0],lon.shape[0]))
        vary_djf = np.empty((vary.shape[0]-1,lat.shape[0],lon.shape[0]) )                 
        for i in range(0,varxravel.shape[0]-12,12):
            counter = 0
            if i >= 12:
                counter = i//12
            djfappendh1 = np.append(varxravel[11+i,:,:],varxravel[12+i,:,:])
            djfappendf1 = np.append(varyravel[11+i,:,:],varyravel[12+i,:,:])  
            djfappendh = np.append(djfappendh1,varxravel[13+i,:,:])
            djfappendf = np.append(djfappendf1,varyravel[13+i,:,:]) 
            varx_djf[counter,:,:] = np.nanmean(np.reshape(djfappendh,
                                    (3,int(lat.shape[0]),int(lon.shape[0]))),
                                    axis=0)                   
            vary_djf[counter,:,:] = np.nanmean(np.reshape(djfappendf,
                                    (3,int(lat.shape[0]),int(lon.shape[0]))),
                                    axis=0)
    ### Reshape for 4d variables
    elif level == 'profile':
        varxravel = np.reshape(varx.copy(),
                           (int(varx.shape[0]*12.),levsq,
                            int(lat.shape[0]),int(lon.shape[0])))
        varyravel = np.reshape(vary.copy(),
                               (int(vary.shape[0]*12.),levsq,
                                int(lat.shape[0]),int(lon.shape[0]))) 
                               
        varx_djf = np.empty((int(varx.shape[0]-1),levsq,
                            int(lat.shape[0]),int(lon.shape[0])))
        vary_djf = np.empty((int(vary.shape[0]-1),levsq,
                            int(lat.shape[0]),int(lon.shape[0])) )                 
        for i in range(0,varxravel.shape[0]-12,12):
            counter = 0
            if i >= 12:
                counter = i//12
            djfappendh1 = np.append(varxravel[11+i,:,:,:],
                                  varxravel[12+i,:,:,:])
            djfappendf1 = np.append(varyravel[11+i,:,:,:],
                                  varyravel[12+i,:,:,:]) 
            djfappendh = np.append(djfappendh1,
                                  varxravel[13+i,:,:,:])
            djfappendf = np.append(djfappendf1,
                                  varyravel[13+i,:,:,:])  
            varx_djf[counter,:,:] = np.nanmean(np.reshape(djfappendh,
                                    (3,levsq,int(lat.shape[0]),
                                     int(lon.shape[0]))),axis=0)                   
            vary_djf[counter,:,:] = np.nanmean(np.reshape(djfappendf,
                                    (3,levsq,int(lat.shape[0]),
                                     int(lon.shape[0]))),axis=0)                               
    else:
        print(ValueError('Selected wrong height - (surface or profile!)!'))    
                                
    print('Completed: Organized data by months (DJF)!')

    print('*Completed: Finished calcDecJanFeb function!')
    return varx_djf,vary_djf

###############################################################################
###############################################################################
###############################################################################
    
def calc_indttest(varx,vary):
    """
    Function calculates statistical difference for 2 independent
    sample t-test

    Parameters
    ----------
    varx : 3d array
    vary : 3d array
    
    Returns
    -------
    stat = calculated t-statistic
    pvalue = two-tailed p-value

    Usage
    -----
    stat,pvalue = calc_ttest(varx,vary)
    """
    print('\n>>> Using calc_ttest function!')
    
    ### Import modules
    import numpy as np
    import scipy.stats as sts
    
    ### 2-independent sample t-test
    stat,pvalue = sts.ttest_ind(varx,vary,nan_policy='omit')
    
    ### Significant at 95% confidence level
    pvalue[np.where(pvalue >= 0.05)] = np.nan
    pvalue[np.where(pvalue < 0.05)] = 1.
    
    print('*Completed: Finished calc_ttest function!')
    return stat,pvalue

###############################################################################
###############################################################################
###############################################################################

def calc_weightedAve(var,lats):
    """
    Area weights sit array 5d [ens,year,month,lat,lon] into [ens,year,month]
    
    Parameters
    ----------
    var : 5d,4d,3d array of a gridded variable
    lats : 2d array of latitudes
    
    Returns
    -------
    meanvar : weighted average for 3d,2d,1d array

    Usage
    -----
    meanvar = calc_weightedAve(var,lats)
    """
    print('\n>>> Using calc_weightedAve function!')
    
    ### Import modules
    import numpy as np
    
    ### Calculate weighted average for various dimensional arrays
    if var.ndim == 5:
        meanvar = np.empty((var.shape[0],var.shape[1],var.shape[2]))
        for ens in range(var.shape[0]):
            for i in range(var.shape[1]):
                for j in range(var.shape[2]):
                    varq = var[ens,i,j,:,:]
                    mask = np.isfinite(varq) & np.isfinite(lats)
                    varmask = varq[mask]
                    areamask = np.cos(np.deg2rad(lats[mask]))
                    meanvar[ens,i,j] = np.nansum(varmask*areamask) \
                                        /np.sum(areamask)  
    elif var.ndim == 4:
        meanvar = np.empty((var.shape[0],var.shape[1]))
        for i in range(var.shape[0]):
            for j in range(var.shape[1]):
                varq = var[i,j,:,:]
                mask = np.isfinite(varq) & np.isfinite(lats)
                varmask = varq[mask]
                areamask = np.cos(np.deg2rad(lats[mask]))
                meanvar[i,j] = np.nansum(varmask*areamask)/np.sum(areamask)
    elif var.ndim == 3:
        meanvar = np.empty((var.shape[0]))
        for i in range(var.shape[0]):
            varq = var[i,:,:]
            mask = np.isfinite(varq) & np.isfinite(lats)
            varmask = varq[mask]
            areamask = np.cos(np.deg2rad(lats[mask]))
            meanvar[i] = np.nansum(varmask*areamask)/np.sum(areamask)
    elif var.ndim == 2:
        meanvar = np.empty((var.shape[0]))
        varq = var[:,:]
        mask = np.isfinite(varq) & np.isfinite(lats)
        varmask = varq[mask]
        areamask = np.cos(np.deg2rad(lats[mask]))
        meanvar = np.nansum(varmask*areamask)/np.sum(areamask)
    else:
        print(ValueError('Variable has the wrong dimensions!'))
     
    print('Completed: Weighted variable average!')
    
    print('*Completed: Finished calc_weightedAve function!')
    return meanvar

###############################################################################
###############################################################################
###############################################################################
    
def calc_spatialCorr(varx,vary,lats,lons,weight):
    """
    Calculates spatial correlation from pearson correlation coefficient
    
    Parameters
    ----------
    varx : 2d array
    vary : 2d array
    lats : 1d array
    lons : 1d array of latitude
    weight : string (yes or no)
    
    Returns
    -------
    corrcoef : 1d array of correlation coefficient (pearson r)
    
    Usage
    -----
    corrcoef = calc_spatialCorr(varx,vary,lats,lons)
    """
    
    print('\n>>> Using calc_spatialCorr function!')
    ### Import modules
    import numpy as np
    
    if weight == 'yes': # Computed weighted correlation coefficient   
        ### mask 
        mask = 'yes'
        if mask == 'yes':
            latq = np.where(lats > 40)[0]
            lats = lats[latq]
            varx = varx[latq,:]
            vary = vary[latq,:]
            print('MASKING LATITUDES!')
        
        ### Create 2d meshgrid for weights 
        lon2,lat2 = np.meshgrid(lons,lats)
        
        ### Create 2d array of weights based on latitude
        gw = np.cos(np.deg2rad(lat2))
        
        def m(x, w):
            """Weighted Mean"""
    
            wave = np.sum(x * w) / np.sum(w)
            print('Completed: Computed weighted average!') 
            return wave
        
        def cov(x, y, w):
            """Weighted Covariance"""
            
            wcov = np.sum(w * (x - m(x, w)) * (y - m(y, w))) / np.sum(w)
            print('Completed: Computed weighted covariance!')
            return wcov
        
        def corr(x, y, w):
            """Weighted Correlation"""
            
            wcor = cov(x, y, w) / np.sqrt(cov(x, x, w) * cov(y, y, w))
            print('Completed: Computed weighted correlation!')
            return wcor
        
        corrcoef = corr(varx,vary,gw)
        
    elif weight == 'no':   
        ### Correlation coefficient from numpy function (not weighted)
        corrcoef= np.corrcoef(varx.ravel(),vary.ravel())[0][1]
        print('Completed: Computed NON-weighted correlation!')
        
    else:
        ValueError('Wrong weighted arguement in function!')
    
    print('*Completed: Finished calc_SpatialCorr function!')
    return corrcoef

###############################################################################
###############################################################################
###############################################################################

def calc_RMSE(varx,vary,lats,lons,weight):
        """
        Calculates root mean square weighted average
        
        Parameters
        ----------
        varx : 2d array
        vary : 2d array
        lons : 1d array of latitude
        weight : string (yes or no)
        
        Returns
        -------
        rmse : 1d array
        
        Usage
        -----
        rmse = calc_RMSE(varx,vary,lats,lons)
        """
        
        print('\n>>> Using calc_RMSE function!')
        ### Import modules
        import numpy as np
        from sklearn.metrics import mean_squared_error
        
        if weight == 'yes': # Computed weighted correlation coefficient   
            ### mask
            mask = 'yes'
            if mask == 'yes':
                latq = np.where(lats > 40)[0]
                lats = lats[latq]
                varx = varx[latq,:]
                vary = vary[latq,:]
                print('MASKING LATITUDES!')
            
            ### Create 2d meshgrid for weights 
            lon2,lat2 = np.meshgrid(lons,lats)
            
            ### Create 2d array of weights based on latitude
            gw = np.cos(np.deg2rad(lat2))
            
            ### Calculate rmse 
            sq_err = (varx - vary)**2
            rmse = np.sqrt((np.sum(sq_err*gw))/np.sum(gw))
     
        elif weight == 'no':   
            ### Root mean square error from sklearn (not weighted)
            rmse = np.sqrt(mean_squared_error(varx.ravel(),vary.ravel()))
            print('Completed: Computed NON-weighted correlation!')
            
        else:
            ValueError('Wrong weighted arguement in function!')
        
        print('*Completed: Finished calc_RMSE function!')
        return rmse
    
###############################################################################
###############################################################################
###############################################################################
    
def calc_spatialCorrHeight(varx,vary,levs,lons,weight):
    """
    Calculates spatial correlation from pearson correlation coefficient for
    grids over vertical height (17 pressure coordinate levels)
    
    Parameters
    ----------
    varx : 2d array
    vary : 2d array
    levs : 1d array of levels
    lons : 1d array of latitude
    weight : string (yes or no)
    
    Returns
    -------
    corrcoef : 1d array of correlation coefficient (pearson r)
    
    Usage
    -----
    corrcoef = calc_spatialCorrHeight(varx,vary,lats,lons)
    """
    
    print('\n>>> Using calc_spatialCorrHeight function!')
    ### Import modules
    import numpy as np
    
    if weight == 'yes': # Computed weighted correlation coefficient   
        
        ### Create 2d meshgrid for weights 
        lon2,lev2 = np.meshgrid(lons,levs)
        
        ### Create 2d array of weights based on latitude
        gwq = np.array([0.25,0.25,0.25,0.25,0.25,0.25,0.4,0.5,0.5,0.5,
              0.5,0.5,0.5,0.7,0.7,0.7,1.])
        gw,gw2 = np.meshgrid(lons,gwq)
        
        def m(x, w):
            """Weighted Mean"""
    
            wave = np.sum(x * w) / np.sum(w)
            print('Completed: Computed weighted average (17 P Levels)!') 
            return wave
        
        def cov(x, y, w):
            """Weighted Covariance"""
            
            wcov = np.sum(w * (x - m(x, w)) * (y - m(y, w))) / np.sum(w)
            print('Completed: Computed weighted covariance (17 P Levels)!')
            return wcov
        
        def corr(x, y, w):
            """Weighted Correlation"""
            
            wcor = cov(x, y, w) / np.sqrt(cov(x, x, w) * cov(y, y, w))
            print('Completed: Computed weighted correlation (17 P Levels)!')
            return wcor
        
        corrcoef = corr(varx,vary,gw)
        
    elif weight == 'no':   
        ### Correlation coefficient from numpy function (not weighted)
        corrcoef= np.corrcoef(varx.ravel(),vary.ravel())[0][1]
        print('Completed: Computed NON-weighted correlation!')
        
    else:
        ValueError('Wrong weighted argument in function!')
    
    print('*Completed: Finished calc_SpatialCorrHeight function!')
    return corrcoef

###############################################################################
###############################################################################
###############################################################################
    
def calc_spatialCorrHeightLev(varx,vary,levs,lons,weight,levelq):
    """
    Calculates spatial correlation from pearson correlation coefficient for
    grids over vertical height (17 pressure coordinate levels). Change the 
    weighting for different level correlations
    
    Parameters
    ----------
    varx : 2d array
    vary : 2d array
    levs : 1d array of levels
    lons : 1d array of latitude
    weight : string (yes or no)
    levelq : string (all, tropo, strato)
    
    Returns
    -------
    corrcoef : 1d array of correlation coefficient (pearson r)
    
    Usage
    -----
    corrcoef = calc_spatialCorrHeight(varx,vary,lats,lons,levels)
    """
    
    print('\n>>> Using calc_spatialCorrHeightLev function!')
    ### Import modules
    import numpy as np
    
    if weight == 'yes': # Computed weighted correlation coefficient   
        
        ### Create 2d meshgrid for weights 
        lon2,lev2 = np.meshgrid(lons,levs)
        
        if levelq == 'all':
            ### Create 2d array of weights based on latitude
            gwq = np.array([0.25,0.25,0.25,0.25,0.25,0.25,0.4,0.5,0.5,0.5,
                  0.5,0.5,0.5,0.7,0.7,0.7,1.])
            gw,gw2 = np.meshgrid(lons,gwq)
        elif levelq == 'tropo':
           gwq = np.array([1.0,1.0,1.0,1.0,0.5,0.5,0.5,0.2,0.2,0.,0.,0.,
                           0.,0.,0.,0.,0.])
           gw,gw2 = np.meshgrid(lons,gwq)
        elif levelq == 'strato':
           gwq = np.array([0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.5,1.,1.,1.,1.
                           ,1.,1.])
           gw,gw2 = np.meshgrid(lons,gwq)
        
        def m(x, w):
            """Weighted Mean"""
    
            wave = np.sum(x * w) / np.sum(w)
            print('Completed: Computed weighted average (17 P Levels)!') 
            return wave
        
        def cov(x, y, w):
            """Weighted Covariance"""
            
            wcov = np.sum(w * (x - m(x, w)) * (y - m(y, w))) / np.sum(w)
            print('Completed: Computed weighted covariance (17 P Levels)!')
            return wcov
        
        def corr(x, y, w):
            """Weighted Correlation"""
            
            wcor = cov(x, y, w) / np.sqrt(cov(x, x, w) * cov(y, y, w))
            print('Completed: Computed weighted correlation (17 P Levels)!')
            return wcor
        
        corrcoef = corr(varx,vary,gw)
        
    elif weight == 'no':   
        ### Correlation coefficient from numpy function (not weighted)
        corrcoef= np.corrcoef(varx.ravel(),vary.ravel())[0][1]
        print('Completed: Computed NON-weighted correlation!')
        
    else:
        ValueError('Wrong weighted argument in function!')
    
    print('*Completed: Finished calc_SpatialCorrHeightLev function!')
    return corrcoef
[((9848, 9892), 'scipy.stats.ttest_ind', 'sts.ttest_ind', (['varx', 'vary'], {'nan_policy': '"""omit"""'}), "(varx, vary, nan_policy='omit')\n", (9861, 9892), True, 'import scipy.stats as sts\n'), ((1888, 1945), 'numpy.empty', 'np.empty', (['(varx.shape[0] - 1, lat.shape[0], lon.shape[0])'], {}), '((varx.shape[0] - 1, lat.shape[0], lon.shape[0]))\n', (1896, 1945), True, 'import numpy as np\n'), ((1960, 2017), 'numpy.empty', 'np.empty', (['(vary.shape[0] - 1, lat.shape[0], lon.shape[0])'], {}), '((vary.shape[0] - 1, lat.shape[0], lon.shape[0]))\n', (1968, 2017), True, 'import numpy as np\n'), ((6092, 6149), 'numpy.empty', 'np.empty', (['(varx.shape[0] - 1, lat.shape[0], lon.shape[0])'], {}), '((varx.shape[0] - 1, lat.shape[0], lon.shape[0]))\n', (6100, 6149), True, 'import numpy as np\n'), ((6165, 6222), 'numpy.empty', 'np.empty', (['(vary.shape[0] - 1, lat.shape[0], lon.shape[0])'], {}), '((vary.shape[0] - 1, lat.shape[0], lon.shape[0]))\n', (6173, 6222), True, 'import numpy as np\n'), ((10947, 10999), 'numpy.empty', 'np.empty', (['(var.shape[0], var.shape[1], var.shape[2])'], {}), '((var.shape[0], var.shape[1], var.shape[2]))\n', (10955, 10999), True, 'import numpy as np\n'), ((13952, 13975), 'numpy.meshgrid', 'np.meshgrid', (['lons', 'lats'], {}), '(lons, lats)\n', (13963, 13975), True, 'import numpy as np\n'), ((16497, 16520), 'numpy.meshgrid', 'np.meshgrid', (['lons', 'lats'], {}), '(lons, lats)\n', (16508, 16520), True, 'import numpy as np\n'), ((18266, 18289), 'numpy.meshgrid', 'np.meshgrid', (['lons', 'levs'], {}), '(lons, levs)\n', (18277, 18289), True, 'import numpy as np\n'), ((18369, 18474), 'numpy.array', 'np.array', (['[0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.7,\n    0.7, 0.7, 1.0]'], {}), '([0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5,\n    0.5, 0.7, 0.7, 0.7, 1.0])\n', (18377, 18474), True, 'import numpy as np\n'), ((18486, 18508), 'numpy.meshgrid', 'np.meshgrid', (['lons', 'gwq'], {}), '(lons, gwq)\n', (18497, 18508), True, 'import numpy as np\n'), ((20869, 20892), 'numpy.meshgrid', 'np.meshgrid', (['lons', 'levs'], {}), '(lons, levs)\n', (20880, 20892), True, 'import numpy as np\n'), ((2188, 2251), 'numpy.append', 'np.append', (['varxravel[(11 + i), :, :]', 'varxravel[(12 + i), :, :]'], {}), '(varxravel[(11 + i), :, :], varxravel[(12 + i), :, :])\n', (2197, 2251), True, 'import numpy as np\n'), ((2263, 2326), 'numpy.append', 'np.append', (['varyravel[(11 + i), :, :]', 'varyravel[(12 + i), :, :]'], {}), '(varyravel[(11 + i), :, :], varyravel[(12 + i), :, :])\n', (2272, 2326), True, 'import numpy as np\n'), ((6395, 6458), 'numpy.append', 'np.append', (['varxravel[(11 + i), :, :]', 'varxravel[(12 + i), :, :]'], {}), '(varxravel[(11 + i), :, :], varxravel[(12 + i), :, :])\n', (6404, 6458), True, 'import numpy as np\n'), ((6472, 6535), 'numpy.append', 'np.append', (['varyravel[(11 + i), :, :]', 'varyravel[(12 + i), :, :]'], {}), '(varyravel[(11 + i), :, :], varyravel[(12 + i), :, :])\n', (6481, 6535), True, 'import numpy as np\n'), ((6550, 6599), 'numpy.append', 'np.append', (['djfappendh1', 'varxravel[(13 + i), :, :]'], {}), '(djfappendh1, varxravel[(13 + i), :, :])\n', (6559, 6599), True, 'import numpy as np\n'), ((6618, 6667), 'numpy.append', 'np.append', (['djfappendf1', 'varyravel[(13 + i), :, :]'], {}), '(djfappendf1, varyravel[(13 + i), :, :])\n', (6627, 6667), True, 'import numpy as np\n'), ((9951, 9975), 'numpy.where', 'np.where', (['(pvalue >= 0.05)'], {}), '(pvalue >= 0.05)\n', (9959, 9975), True, 'import numpy as np\n'), ((9997, 10020), 'numpy.where', 'np.where', (['(pvalue < 0.05)'], {}), '(pvalue < 0.05)\n', (10005, 10020), True, 'import numpy as np\n'), ((11509, 11547), 'numpy.empty', 'np.empty', (['(var.shape[0], var.shape[1])'], {}), '((var.shape[0], var.shape[1]))\n', (11517, 11547), True, 'import numpy as np\n'), ((14061, 14077), 'numpy.deg2rad', 'np.deg2rad', (['lat2'], {}), '(lat2)\n', (14071, 14077), True, 'import numpy as np\n'), ((16618, 16634), 'numpy.deg2rad', 'np.deg2rad', (['lat2'], {}), '(lat2)\n', (16628, 16634), True, 'import numpy as np\n'), ((21008, 21113), 'numpy.array', 'np.array', (['[0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.7,\n    0.7, 0.7, 1.0]'], {}), '([0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5,\n    0.5, 0.7, 0.7, 0.7, 1.0])\n', (21016, 21113), True, 'import numpy as np\n'), ((21133, 21155), 'numpy.meshgrid', 'np.meshgrid', (['lons', 'gwq'], {}), '(lons, gwq)\n', (21144, 21155), True, 'import numpy as np\n'), ((3574, 3643), 'numpy.append', 'np.append', (['varxravel[(11 + i), :, :, :]', 'varxravel[(12 + i), :, :, :]'], {}), '(varxravel[(11 + i), :, :, :], varxravel[(12 + i), :, :, :])\n', (3583, 3643), True, 'import numpy as np\n'), ((3688, 3757), 'numpy.append', 'np.append', (['varyravel[(11 + i), :, :, :]', 'varyravel[(12 + i), :, :, :]'], {}), '(varyravel[(11 + i), :, :, :], varyravel[(12 + i), :, :, :])\n', (3697, 3757), True, 'import numpy as np\n'), ((7926, 7995), 'numpy.append', 'np.append', (['varxravel[(11 + i), :, :, :]', 'varxravel[(12 + i), :, :, :]'], {}), '(varxravel[(11 + i), :, :, :], varxravel[(12 + i), :, :, :])\n', (7935, 7995), True, 'import numpy as np\n'), ((8042, 8111), 'numpy.append', 'np.append', (['varyravel[(11 + i), :, :, :]', 'varyravel[(12 + i), :, :, :]'], {}), '(varyravel[(11 + i), :, :, :], varyravel[(12 + i), :, :, :])\n', (8051, 8111), True, 'import numpy as np\n'), ((8158, 8210), 'numpy.append', 'np.append', (['djfappendh1', 'varxravel[(13 + i), :, :, :]'], {}), '(djfappendh1, varxravel[(13 + i), :, :, :])\n', (8167, 8210), True, 'import numpy as np\n'), ((8263, 8315), 'numpy.append', 'np.append', (['djfappendf1', 'varyravel[(13 + i), :, :, :]'], {}), '(djfappendf1, varyravel[(13 + i), :, :, :])\n', (8272, 8315), True, 'import numpy as np\n'), ((11937, 11959), 'numpy.empty', 'np.empty', (['var.shape[0]'], {}), '(var.shape[0])\n', (11945, 11959), True, 'import numpy as np\n'), ((13722, 13741), 'numpy.where', 'np.where', (['(lats > 40)'], {}), '(lats > 40)\n', (13730, 13741), True, 'import numpy as np\n'), ((14165, 14178), 'numpy.sum', 'np.sum', (['(x * w)'], {}), '(x * w)\n', (14171, 14178), True, 'import numpy as np\n'), ((14181, 14190), 'numpy.sum', 'np.sum', (['w'], {}), '(w)\n', (14187, 14190), True, 'import numpy as np\n'), ((14424, 14433), 'numpy.sum', 'np.sum', (['w'], {}), '(w)\n', (14430, 14433), True, 'import numpy as np\n'), ((16239, 16258), 'numpy.where', 'np.where', (['(lats > 40)'], {}), '(lats > 40)\n', (16247, 16258), True, 'import numpy as np\n'), ((16747, 16766), 'numpy.sum', 'np.sum', (['(sq_err * gw)'], {}), '(sq_err * gw)\n', (16753, 16766), True, 'import numpy as np\n'), ((16766, 16776), 'numpy.sum', 'np.sum', (['gw'], {}), '(gw)\n', (16772, 16776), True, 'import numpy as np\n'), ((18594, 18607), 'numpy.sum', 'np.sum', (['(x * w)'], {}), '(x * w)\n', (18600, 18607), True, 'import numpy as np\n'), ((18610, 18619), 'numpy.sum', 'np.sum', (['w'], {}), '(w)\n', (18616, 18619), True, 'import numpy as np\n'), ((18867, 18876), 'numpy.sum', 'np.sum', (['w'], {}), '(w)\n', (18873, 18876), True, 'import numpy as np\n'), ((21204, 21304), 'numpy.array', 'np.array', (['[1.0, 1.0, 1.0, 1.0, 0.5, 0.5, 0.5, 0.2, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,\n    0.0, 0.0]'], {}), '([1.0, 1.0, 1.0, 1.0, 0.5, 0.5, 0.5, 0.2, 0.2, 0.0, 0.0, 0.0, 0.0, \n    0.0, 0.0, 0.0, 0.0])\n', (21212, 21304), True, 'import numpy as np\n'), ((21324, 21346), 'numpy.meshgrid', 'np.meshgrid', (['lons', 'gwq'], {}), '(lons, gwq)\n', (21335, 21346), True, 'import numpy as np\n'), ((21616, 21629), 'numpy.sum', 'np.sum', (['(x * w)'], {}), '(x * w)\n', (21622, 21629), True, 'import numpy as np\n'), ((21632, 21641), 'numpy.sum', 'np.sum', (['w'], {}), '(w)\n', (21638, 21641), True, 'import numpy as np\n'), ((21889, 21898), 'numpy.sum', 'np.sum', (['w'], {}), '(w)\n', (21895, 21898), True, 'import numpy as np\n'), ((12286, 12308), 'numpy.empty', 'np.empty', (['var.shape[0]'], {}), '(var.shape[0])\n', (12294, 12308), True, 'import numpy as np\n'), ((21396, 21496), 'numpy.array', 'np.array', (['[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 1.0, 1.0, 1.0, 1.0,\n    1.0, 1.0]'], {}), '([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 1.0, 1.0, \n    1.0, 1.0, 1.0, 1.0])\n', (21404, 21496), True, 'import numpy as np\n'), ((21508, 21530), 'numpy.meshgrid', 'np.meshgrid', (['lons', 'gwq'], {}), '(lons, gwq)\n', (21519, 21530), True, 'import numpy as np\n'), ((11197, 11214), 'numpy.isfinite', 'np.isfinite', (['varq'], {}), '(varq)\n', (11208, 11214), True, 'import numpy as np\n'), ((11217, 11234), 'numpy.isfinite', 'np.isfinite', (['lats'], {}), '(lats)\n', (11228, 11234), True, 'import numpy as np\n'), ((11314, 11336), 'numpy.deg2rad', 'np.deg2rad', (['lats[mask]'], {}), '(lats[mask])\n', (11324, 11336), True, 'import numpy as np\n'), ((11377, 11406), 'numpy.nansum', 'np.nansum', (['(varmask * areamask)'], {}), '(varmask * areamask)\n', (11386, 11406), True, 'import numpy as np\n'), ((11448, 11464), 'numpy.sum', 'np.sum', (['areamask'], {}), '(areamask)\n', (11454, 11464), True, 'import numpy as np\n'), ((11686, 11703), 'numpy.isfinite', 'np.isfinite', (['varq'], {}), '(varq)\n', (11697, 11703), True, 'import numpy as np\n'), ((11706, 11723), 'numpy.isfinite', 'np.isfinite', (['lats'], {}), '(lats)\n', (11717, 11723), True, 'import numpy as np\n'), ((11795, 11817), 'numpy.deg2rad', 'np.deg2rad', (['lats[mask]'], {}), '(lats[mask])\n', (11805, 11817), True, 'import numpy as np\n'), ((11850, 11879), 'numpy.nansum', 'np.nansum', (['(varmask * areamask)'], {}), '(varmask * areamask)\n', (11859, 11879), True, 'import numpy as np\n'), ((11878, 11894), 'numpy.sum', 'np.sum', (['areamask'], {}), '(areamask)\n', (11884, 11894), True, 'import numpy as np\n'), ((12049, 12066), 'numpy.isfinite', 'np.isfinite', (['varq'], {}), '(varq)\n', (12060, 12066), True, 'import numpy as np\n'), ((12069, 12086), 'numpy.isfinite', 'np.isfinite', (['lats'], {}), '(lats)\n', (12080, 12086), True, 'import numpy as np\n'), ((12150, 12172), 'numpy.deg2rad', 'np.deg2rad', (['lats[mask]'], {}), '(lats[mask])\n', (12160, 12172), True, 'import numpy as np\n'), ((12199, 12228), 'numpy.nansum', 'np.nansum', (['(varmask * areamask)'], {}), '(varmask * areamask)\n', (12208, 12228), True, 'import numpy as np\n'), ((12227, 12243), 'numpy.sum', 'np.sum', (['areamask'], {}), '(areamask)\n', (12233, 12243), True, 'import numpy as np\n'), ((12350, 12367), 'numpy.isfinite', 'np.isfinite', (['varq'], {}), '(varq)\n', (12361, 12367), True, 'import numpy as np\n'), ((12370, 12387), 'numpy.isfinite', 'np.isfinite', (['lats'], {}), '(lats)\n', (12381, 12387), True, 'import numpy as np\n'), ((12443, 12465), 'numpy.deg2rad', 'np.deg2rad', (['lats[mask]'], {}), '(lats[mask])\n', (12453, 12465), True, 'import numpy as np\n'), ((12485, 12514), 'numpy.nansum', 'np.nansum', (['(varmask * areamask)'], {}), '(varmask * areamask)\n', (12494, 12514), True, 'import numpy as np\n'), ((12513, 12529), 'numpy.sum', 'np.sum', (['areamask'], {}), '(areamask)\n', (12519, 12529), True, 'import numpy as np\n')]