def graph_pred_truth(predictions, forMonth, year, resolution):

    """
    Purpose: Graph prediction and truth grids
    """
    
    #Graph predictions
    m = Basemap(projection='npstere',boundinglat=65,lon_0=0, resolution='l')
    
    dx_res = resolution * 1000
    nx = int((m.xmax-m.xmin)/dx_res)+1; ny = int((m.ymax-m.ymin)/dx_res)+1
    #grid_str=str(int(dx_res/1000))+'km'
    lonsG, latsG, xptsG, yptsG = m.makegrid(nx, ny, returnxy=True)
    
    # Get lon/lats pf the ice concentration data on polar sterographic grid
    datapath = '../../../Data/'
    lats, lons = ff.get_psnlatslons(datapath)
    xpts, ypts =m(lons, lats)
    
    monthStr = get_month_str(forMonth)
    yearStr = str(year)
    
    fig_title = 'Comparison for ' + monthStr + ' ' + yearStr
    fig = figure(figsize=(6,6))
    fig.suptitle(fig_title, fontsize=18)
    
    ax = fig.add_subplot(221)
    ax.set_title('Predictions for ' + monthStr + ' ' + yearStr)
    im1 = m.pcolormesh(xptsG , yptsG, predictions, cmap=cm.Blues_r, vmin=0, vmax=1,shading='flat', zorder=2)
    
    truthGrid = get_conc_grid(forMonth, year, resolution)
    ax = fig.add_subplot(223)
    ax.set_title('Truth for ' + monthStr + ' ' + yearStr)
    im2 = m.pcolormesh(xptsG , yptsG, truthGrid, cmap=cm.Blues_r, vmin=0, vmax=1,shading='flat', zorder=2)
    
    #Calculate ice extent and ice areas
    areaStr   = "Ice Area: \n\nIce Extent: "
    
    ax = fig.add_subplot(222)
    ax.axis('off')
    ax.text(0.05, 0.95, areaStr,  fontsize=12,
            verticalalignment='top')
    
    ax = fig.add_subplot(224)
    ax.axis('off')
    ax.text(0.05, 0.95, areaStr,  fontsize=12,
            verticalalignment='top')
    
    plt.show()
def get_thick_grid(month, year, resolution):

    """
    Purpose: Retrieve the concentration grids using given month, year, and resolution
    """
    
    # File paths
    datapath = '../../../Data/'
    
    # Get map projection and create regularly spaced grid from this projection
    m = Basemap(projection='npstere',boundinglat=65,lon_0=0, resolution='l')
    
    dx_res = resolution*1000. # 100 km
    nx = int((m.xmax-m.xmin)/dx_res)+1; ny = int((m.ymax-m.ymin)/dx_res)+1
    lonsG, latsG, xptsG, yptsG = m.makegrid(nx, ny, returnxy=True)
    
    # Get lon/lats on polar sterographic grid
    lats, lons = ff.get_psnlatslons(datapath)
    xpts, ypts =m(lons, lats)
    
    xptsP, yptsP, thickness=ff.get_pmas_month(m, datapath, year, month)
    iceThicknessG = griddata((xptsP, yptsP),thickness, (xptsG, yptsG), method='linear')
    
    return iceThicknessG
def main(year, month, alg=0, poleStr='A', outputGrid=0):

    m = Basemap(projection='npstere', boundinglat=65, lon_0=0, resolution='l')

    #datapath='/Users/aapetty/GitRepos/GitHub/SeaIcePrediction/Data/'
    #dataoutpath='/Users/aapetty/GitRepos/GitHub/SeaIcePrediction/DataOutput/IceConcA/'
    #figpath='/Users/aapetty/GitRepos/GitHub/SeaIcePrediction/Figures/Arctic/IceConc/'

    datapath = '../../Data'
    dataoutpath = '../../DataOutput/IceConcA/'
    figpath = '../../Figures/Arctic/IceConc'

    dx_res = 100000.
    nx = int((m.xmax - m.xmin) / dx_res) + 1
    ny = int((m.ymax - m.ymin) / dx_res) + 1
    grid_str = str(int(dx_res / 1000)) + 'km'
    lonsG, latsG, xptsG, yptsG = m.makegrid(nx, ny, returnxy=True)

    if (outputGrid == 1):
        xptsG.dump(dataoutpath + 'xpts' + grid_str + poleStr)
        yptsG.dump(dataoutpath + 'ypts' + grid_str + poleStr)

    lats, lons = ff.get_psnlatslons(datapath)
    xpts, ypts = m(lons, lats)
    f = Dataset(datapath + '/OTHER/NIC_valid_ice_mask.N25km.01.1972-2007.nc',
                'r')
    ice_flag = f.variables['valid_ice_flag'][:]
    region_mask = ff.get_region_mask_sect(datapath, m, xypts_return=0)

    if (year > 2015):
        ice_conc = ff.get_month_concSN_NRT(datapath,
                                           year,
                                           month,
                                           alg=alg,
                                           pole=poleStr,
                                           lowerConc=1,
                                           maxConc=1,
                                           mask=1,
                                           monthMean=1)
        #ice_conc=ma.masked_where(ice_conc<=0.15, ice_conc)
    else:
        ice_conc = ff.get_month_concSN(datapath,
                                       year,
                                       month,
                                       alg=alg,
                                       pole=poleStr,
                                       lowerConc=1,
                                       maxConc=1,
                                       mask=1)

    #ice_conc = ice_conc.filled(0)

    #ice_conc = ma.masked_where(ice_conc>1., ice_conc)
    #ice_conc = ma.where(ice_conc>1.,0, ice_conc)
    #ice_conc = ma.where(ice_conc<0.15,0, ice_conc)
    ice_conc = ma.where((ice_flag >= 1.5), 0, ice_conc)

    # get mean conc around the pole hole (time varying)
    pmask = ff.get_pmask(year, month)
    concHole = ma.mean(ice_conc[(lats > pmask - 0.5) & (lats < pmask)])
    ice_conc = where((lats >= pmask - 0.5), concHole, ice_conc)

    #ice_conc[where(region_mask>18)]=0

    ice_concG = griddata((xpts.flatten(), ypts.flatten()),
                         ice_conc.flatten(), (xptsG, yptsG),
                         method='linear')
    ice_conc_ma = ma.masked_where(np.isnan(ice_concG), ice_concG)

    plot_conc(figpath,
              m,
              xptsG,
              yptsG,
              ice_conc_ma,
              year,
              month,
              grid_str,
              poleStr='A')

    ice_conc_ma.dump(dataoutpath + 'ice_conc' + grid_str + str(month) +
                     str(year) + poleStr)
Example #4
0
import numpy as np
from pylab import *
import numpy.ma as ma
from mpl_toolkits.basemap import Basemap, shiftgrid
from glob import glob
from netCDF4 import Dataset
import forecast_funcs as ff

dataPath = '/Users/aapetty/GitRepos/GitHub/SeaIcePrediction/Data/'
outPath='/Users/aapetty/GitRepos/GitHub/SeaIcePrediction/DataOutput/Extent/'
figPath='/Users/aapetty/GitRepos/GitHub/SeaIcePrediction/Figures/'

m = Basemap(projection='npstere',boundinglat=66,lon_0=0, resolution='l'  )


lats, lons = ff.get_psnlatslons(dataPath)
areaF=reshape(fromfile(file=open(dataPath+'/OTHER/psn25area_v3.dat', 'rb'), dtype='<i4')/1000., [448, 304])/1e6
region_mask, xpts, ypts = ff.get_region_mask_sect(dataPath, m, xypts_return=1)



start_year=1979
end_year=2017
month = 9 # 9 = Sept
alg=0 #0=NASA TEAM, 1=BOOTSTRAP

ice_ext_mean=[]
ice_area_mean=[]

regions=[12, 13]
def get_conc_grid(month, year, resolution):
    
    """
    Purpose: Retrieve the concentration grids using given month, year, and resolution
    """
    
    poleStr='A'# 'A: Arctic, AA: Antarctic
    alg=0 #0=Nasa team
    
    # File paths
    datapath = '../../../Data'
    
    # Get map projection and create regularly spaced grid from this projection
    m = Basemap(projection='npstere',boundinglat=65,lon_0=0, resolution='l')
    
    dx_res = resolution * 1000
    nx = int((m.xmax-m.xmin)/dx_res)+1; ny = int((m.ymax-m.ymin)/dx_res)+1
    lonsG, latsG, xptsG, yptsG = m.makegrid(nx, ny, returnxy=True)  
    
    # Get lon/lats pf the ice concentration data on polar sterographic grid
    lats, lons = ff.get_psnlatslons(datapath)
    xpts, ypts =m(lons, lats)
    
    f = Dataset(datapath+'/OTHER/NIC_valid_ice_mask.N25km.01.1972-2007.nc', 'r')
    ice_flag = f.variables['valid_ice_flag'][:]
    
    if (year>2015):
        ice_conc = ff.get_month_concSN_NRT(datapath, year, month, alg=alg, pole=poleStr, monthMean=1)
        # Mask values below 0.15
        ice_conc=ma.masked_where(ice_conc<=0.15, ice_conc) #I don't get this...
    else:
        ice_conc = ff.get_month_concSN(datapath, year, month, alg=alg, pole=poleStr)
    
    # fill ice concentration data with zeroes
    ice_conc = ice_conc.filled(0)
    ice_conc = where((ice_flag >=1.5), 0, ice_conc)
    
    # Note the pole hole due to the incomplete satellite orbit
    if (year<1987):
        pmask=84.
    elif((year==1987)&(month<=5)):
        pmask=84.
    elif ((year==1987)&(month>5)):
        pmask=86.5
    elif ((year>1987)&(year<2008)):
        pmask=86.5
    else:
        pmask=88.5
    
    # Grid data
    ice_concG = griddata((xpts.flatten(), ypts.flatten()),ice_conc.flatten(), (xptsG, yptsG), method='linear')
    ice_conc_ma=ma.masked_where(np.isnan(ice_concG), ice_concG)
    #ice_conc_ma=ma.masked_where((latsG>pmask), ice_conc_ma)
    
    test = np.where(latsG>pmask)
    gridData = ice_conc_ma.data
    gridData[ice_conc_ma.mask == True] = 0
    
    #WARNING: Super ghetto solution to not just set the hole to 1
    arr1 = np.arange(np.min(test[0]), np.max(test[0]))
    arr2 = np.ones((1, np.size(arr1)), dtype=np.int ) * (np.min(test[0])-1)
    tup  = (arr1, arr2)
    gridMean = sum(gridData[tup])/np.size(arr1)
    gridData[test] = gridMean #fix
    
    return gridData