def data_collection():

    rawDataPath = '../../Data/'
    derivedDataPath = '../../DataOutput/'
    forecastVar = 'conc'
    iceType = 'extent'
    hemStr = 'N'
    siiVersion = 'v3.0'

    startYr = 1990
    forecastYr = 2010
    startMonth = 2
    stopMonth = 11
    regionMask = np.load(derivedDataPath + 'Regions/regionMaskA100km')

    numFeat = 18
    numSamples = (forecastYr - startYr) * (stopMonth + 1 - startMonth)

    #data collection
    #start off with just the summer months and without ice thickness

    feat = []
    groundTruth = []
    for year in range(startYr, forecastYr):
        for index, month in enumerate(range(startMonth, stopMonth + 1)):

            _, extent = ff.get_ice_extentN(rawDataPath, month, year, year,
                                           iceType, siiVersion, hemStr)
            groundTruth.append(extent[0])

            #set month to array
            sample = np.zeros(numFeat)
            sample[0] = (month - 1)  #gridded months are actually indexed at 0
            #divide by 100 to put in the same magnitude as the other values
            sample[1] = np.ma.mean(
                ff.get_pmas_month(rawDataPath, year, month - 1))

            #set region features to array
            iceConc = ff.get_gridvar(derivedDataPath, forecastVar, month,
                                     array(year), hemStr)

            for regInd in range(16):

                regionData = iceConc.data
                regionData[iceConc.mask == True] = 0  #get rid of nan
                desiredRegion = regionMask == regInd
                sample[regInd + 2] = np.ma.mean(
                    np.multiply(regionData, desiredRegion))
                #sample[regInd+1] = np.ma.mean(np.multiply(regionData, desiredRegion))

            feat.append(sample)

    feat = np.reshape(feat, (numSamples, numFeat))
    groundTruth = np.reshape(groundTruth, (numSamples, 1))

    return feat, groundTruth
region = 0
hemStr = 'N'
anomObs = 1

rawDataPath = '../../Data/'
derivedDataPath = '../../DataOutput/'

yrForecast = 2015
randSeedNum = 50

#thick, forecastThickMean = ff.get_ice_thickness(rawDataPath, startYr, yrForecast, forecastMonth)

yrsTrain, extentTrain = ff.get_ice_extentN(rawDataPath,
                                           predMonth,
                                           startYr,
                                           yrForecast - 1,
                                           icetype=iceType,
                                           version=siiVersion,
                                           hemStr=hemStr)

extentDetrendTrain, lineTrain = ff.get_varDT(yrsTrain, extentTrain)

varTrain = ff.get_gridvar(derivedDataPath, forecastVar, forecastMonth,
                          yrsTrain, hemStr)
varForecast = ff.get_gridvar(derivedDataPath, forecastVar, forecastMonth,
                             array(yrForecast), hemStr)
years, extentYr = ff.get_ice_extentN(rawDataPath,
                                     predMonth,
                                     yrForecast,
                                     yrForecast,
                                     icetype=iceType,
def main(endYear, fmonth, pmonth):
    rawDataPath = '../../Data/'
    derivedDataPath = '../../DataOutput/'
    saveDataPath = derivedDataPath + '/Arctic/'
    figPath = '../../Figures/' + '/Arctic/Predictions/'

    fvars = ['conc']
    iceType = 'extent'
    hemStr = 'N'
    siiVersion = 'v3.0'
    startYear = 1980
    startYearP = 1990
    #endYear=2017
    weight = 1
    region = 0

    varStrsOut = ''.join(fvars)

    forecastVarsM = np.array([]).reshape(0, 7)

    for year in range(startYearP, endYear + 1):
        outStr = 'forecastDump' + iceType + varStrsOut + 'fm' + str(
            fmonth) + 'pm' + str(pmonth) + 'R' + str(region) + str(
                startYear) + str(year) + 'W' + str(weight) + 'SII' + siiVersion

        forecastVars = load(saveDataPath + outStr)

        forecastVarsM = np.vstack([forecastVarsM, forecastVars])

    years, extent = ff.get_ice_extentN(rawDataPath,
                                       pmonth,
                                       startYear,
                                       year,
                                       icetype=iceType,
                                       version=siiVersion,
                                       hemStr=hemStr)

    yearsP = np.arange(startYearP, endYear + 1)
    """Plot forecast data """
    rcParams['xtick.major.size'] = 2
    rcParams['ytick.major.size'] = 2
    rcParams['axes.linewidth'] = .5
    rcParams['lines.linewidth'] = .5
    rcParams['patch.linewidth'] = .5
    rcParams['axes.labelsize'] = 8
    rcParams['xtick.labelsize'] = 8
    rcParams['ytick.labelsize'] = 8
    rcParams['legend.fontsize'] = 8
    rcParams['font.size'] = 7
    rc('font', **{'family': 'sans-serif', 'sans-serif': ['Arial']})

    fig = figure(figsize=(3.5, 2.2))
    ax1 = subplot(1, 1, 1)
    im1 = plot(years, extent, 'k')
    #im2 = plot(Years[start_year_pred-start_year:], lineT[start_year_pred-start_year:]+ExtentG, 'r')

    #im3 = plot(years[-1], extent[-1], marker='o', markersize=2, color='k')
    im3 = plot(yearsP,
               forecastVarsM[:, 2],
               marker='x',
               markersize=1,
               alpha=0.5,
               color='k')
    im3 = plot(yearsP,
               forecastVarsM[:, 3],
               marker='o',
               markersize=1,
               alpha=0.5,
               color='r')
    ax1.errorbar(yearsP,
                 forecastVarsM[:, 3],
                 yerr=forecastVarsM[:, 6],
                 color='r',
                 fmt='',
                 linestyle='',
                 alpha=0.5,
                 lw=0.6,
                 capsize=0.5,
                 zorder=2)

    im3 = plot(yearsP[-1],
               forecastVarsM[-1, 2],
               marker='x',
               markersize=2,
               color='k')
    im3 = plot(yearsP[-1],
               forecastVarsM[-1, 3],
               marker='o',
               markersize=2,
               color='r')
    ax1.errorbar(yearsP[-1],
                 forecastVarsM[-1, 3],
                 yerr=forecastVarsM[-1, 6],
                 color='r',
                 fmt='',
                 linestyle='',
                 lw=0.6,
                 capsize=0.5,
                 zorder=2)

    #errorbar(YearsP, array(lineTP)+array(ExtentG) , yerr=prederror, color='r',fmt='',linestyle='',lw=0.4,capsize=0.5, zorder = 2)
    #if (np.isfinite(forecastVars[4])):

    #ax1.errorbar(yearsP, extentPredAbs , yerr=[1.96*x for x in perr], color='r',fmt='',linestyle='',lw=0.3,capsize=0.5, zorder = 2)

    forecastStr = '%.2f' % (forecastVarsM[-1, 3])
    linearStr = '%.2f' % (forecastVarsM[-1, 2])
    observedStr = '%.2f' % (extent[-1])

    ax1.annotate('Year: ' + str(year) + '\nObserved: ' + observedStr +
                 r' M km$^2$' + '\nTrend: ' + linearStr + r' M km$^2$',
                 xy=(0.7, 1.02),
                 xycoords='axes fraction',
                 horizontalalignment='left',
                 verticalalignment='top')

    ax1.annotate('\nForecast: ' + forecastStr + r' M km$^2$',
                 xy=(0.7, 0.85),
                 xycoords='axes fraction',
                 color='r',
                 horizontalalignment='left',
                 verticalalignment='top')

    ax1.annotate('June forecasts of September sea ice',
                 fontsize=5,
                 xy=(0.02, 0.02),
                 xycoords='axes fraction',
                 horizontalalignment='left',
                 verticalalignment='bottom')

    ax1.set_ylabel(iceType + r' (Million km$^2$)')
    #ax1.set_xlabel('Years')
    ax1.set_xlim(1980, 2020)
    ax1.set_xticks(np.arange(1980, 2021, 10))
    ax1.set_xticks(np.arange(1980, 2021, 5), minor=True)
    #ax1.set_xticklabels([])
    ax1.set_ylim(3, 8)

    ax1.spines['right'].set_visible(False)
    ax1.spines['top'].set_visible(False)

    plt.tight_layout()
    #subplots_adjust(left=0.15, right=0.90, bottom=0.17, top=0.96, hspace=0)

    savefig(figPath + '/forecast' + outStr + 'multi.png', dpi=300)
    close(fig)
Beispiel #4
0
def main(year,
         fmonth,
         pmonth,
         fvars=['conc'],
         iceType='extent',
         hemStr='N',
         siiVersion='v3.0',
         startYear=1979,
         weight=1,
         region=0,
         numYearsReq=5,
         plotForecast=1,
         plotSkill=1,
         outSkill=1,
         outLine=1,
         outWeights=1):
    """ 
	Main sea ice forecast script. Can be run here (and looped )

	Args:
		startYear=1980 # Start of forecast training
		endYear=2018 # End of forecast
		fmonth=11 #6=June, 9=Sep #  Forecast month
		pmonth=2 #9=SEP # Predicted month

		fvars: forecast variables (e.g. 'conc'). Need to be in brackets.
		weight: Spatially weighting the data (1=True)
		iceType: Ice type being forecast ('extent' or 'area')
		hemStr: Hemipshere (N or S)
		siiVersion: version of the NSIDC sea ice index (if used as the observed ice state)
		startYear: Start year of training data (default = 1980)
		region: Region being forecast (0 is pan Arctic/Antartctic)
			#0 implies pan-Arctic or Antarctic
			#2 Weddell Sea
			#3 Indian Ocean
			#4 Pacific Ocean
			#5 Ross Sea
			#6 Amundsen/BHausen Sea
			#A Alaskan

		numYearsReq: (defaul 5) Number of years required in a grid cell for it to count towards the training data
		plotSkill: =1 for plotting the skill
		outSkill: =1 far saving the skill values
		outLine: =1 for saving the forecast time series
		outWeights: =1 for saving the weightings

	Returns:
		A dumped Python array including the folowing variables:
			Observed ice extent
			Observed detrended ice extent from linear trend persistence (LTP)
			LTP extent
			Absolute forecast of sea ice extent (added LTP)
			Detrended (forecast) ice extent from LTP
			Forecast anomaly from observed
			Estimated forecast error (1 SD)

	"""
    repoPath = '/Users/aapetty/GitRepos/GitHub/SeaIcePrediction/'
    rawDataPath = repoPath + '/Data/'
    derivedDataPath = repoPath + '/DataOutput/'

    if (hemStr == 'S'):
        saveDataPath = derivedDataPath + '/Antarctic/'
        figPath = repoPath + '/Figures/Antarctic/YearlyPredictions/'
    elif (hemStr == 'N'):
        saveDataPath = derivedDataPath + '/Arctic/'
        figPath = repoPath + '/Figures/Arctic/YearlyPredictions/'

    if (region == 'A'):
        saveDataPath = derivedDataPath + '/Alaska/'
        figPath = repoPath + '/Figures/Alaska/YearlyPredictions/'

    print('Forecast year:', year)
    print('Forecast data month:', fmonth, 'Predicted month:', pmonth)
    print('Variables:', fvars)
    print('Hemisphere:', hemStr)
    print('Ice type predicted', iceType)
    print('Weighted:', weight)

    varStrsOut = ''.join(fvars)
    outStr = 'forecastDump' + iceType + varStrsOut + 'fm' + str(
        fmonth) + 'pm' + str(pmonth) + 'R' + str(region) + str(
            startYear) + str(year) + 'W' + str(weight) + 'SII' + siiVersion

    if (year < 2018):
        anomObsT = 1
    else:
        anomObsT = 0

    forecastVals = ff.CalcForecastMultiVar(rawDataPath,
                                           derivedDataPath,
                                           year,
                                           startYear,
                                           fvars,
                                           fmonth,
                                           pmonth=pmonth,
                                           region=region,
                                           anomObs=anomObsT,
                                           numYearsReq=numYearsReq,
                                           weight=weight,
                                           outWeights=outWeights,
                                           icetype=iceType,
                                           hemStr=hemStr,
                                           siiVersion=siiVersion)

    print('Observed ice state:', forecastVals[0])
    print('Linear trend presistence:', forecastVals[2])
    print('Forecast ice state:', forecastVals[3])
    print('Detrended observed ice state :', forecastVals[1])
    print('Detrended observed ice state :', forecastVals[4])
    print('Forecast anomaly :', forecastVals[5])

    array(forecastVals).dump(saveDataPath + outStr)

    if (plotForecast == 1):
        if (region == 0):
            years, extent = ff.get_ice_extentN(rawDataPath,
                                               pmonth,
                                               startYear,
                                               year,
                                               icetype=iceType,
                                               version=siiVersion,
                                               hemStr=hemStr)

            ff.plotForecastOneYear(figPath,
                                   years,
                                   extent,
                                   year,
                                   forecastVals,
                                   outStr,
                                   iceType,
                                   minval=2,
                                   maxval=8)

        elif (region == 'A'):
            poleStr = 'A'

            extent = loadtxt(derivedDataPath + '/Extent/' + 'ice_' + iceType +
                             '_M' + str(pmonth) + 'R' + str(region) + '_' +
                             str(startYear) + '2017' + poleStr)
            extent = extent[0:year - startYear + 1]
            years = np.arange(startYear, startYear + size(extent), 1)

            ff.plotForecastOneYear(figPath,
                                   years,
                                   extent,
                                   year,
                                   forecastVals,
                                   outStr,
                                   iceType,
                                   minval=0,
                                   maxval=2)
def main(endYear, fmonth, pmonth, fvars=['conc'], iceType='extent', hemStr='N', siiVersion='v3.0', startYear=1979, minval=3, maxval=8, region=0):
	
	monthStrs=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'August', 'September']
	textStr=monthStrs[fmonth-1]+' forecasts of '+monthStrs[pmonth-1]+' Arctic sea ice '+iceType

	repoPath='/Users/aapetty/GitRepos/GitHub/SeaIcePrediction/'
	rawDataPath = repoPath+'/Data/' 
	derivedDataPath = repoPath+'/DataOutput/'
	if (hemStr=='S'):
		saveDataPath=derivedDataPath+'/Antarctic/'
		figPath=repoPath+'/Figures/Forecasts/'
	elif (region=='A'):
		saveDataPath=derivedDataPath+'/Alaska/'
		figPath=repoPath+'/Figures/Forecasts/'

	else:
		saveDataPath=derivedDataPath+'/Arctic/'
		figPath=repoPath+'/Figures/Forecasts/'
	
	
	startYearP=1990
	#endYear=2017
	weight=1

	varStrsOut=''.join(fvars)

	forecastVarsM=np.array([]).reshape(0,7)


	for year in range(startYearP, endYear+1):
		outStr='forecastDump'+iceType+varStrsOut+'fm'+str(fmonth)+'pm'+str(pmonth)+'R'+str(region)+str(startYear)+str(year)+'W'+str(weight)+'SII'+siiVersion

		forecastVars=load(saveDataPath+outStr)

		forecastVarsM=np.vstack([forecastVarsM, forecastVars])

	if (region==0):
		years, extent = ff.get_ice_extentN(rawDataPath, pmonth, startYear, year, 
			icetype=iceType, version=siiVersion, hemStr=hemStr)
	elif (region=='A'):
		poleStr='A'

		extent=loadtxt(derivedDataPath+'/Extent/'+'ice_'+iceType+'_M'+str(pmonth)+'R'+str(region)+'_'+str(startYear)+'2017'+poleStr)
		#extent=extent[0:year-startYear+1]
		years=np.arange(startYear, 2017+1, 1)



	yearsP=np.arange(startYearP, endYear+1)

	#extentyr, extentObsDT, extTrendP, extentForrAbs, extentForrDT, anom, prstd[0]
	print ('Prediction int:', forecastVars[-1])

	skill = '%.2f' %(1 - (ff.rms(forecastVarsM[0:-1, 5]))/(ff.rms(forecastVarsM[0:-1, 1])))

	"""Plot forecast data """
	rcParams['xtick.major.size'] = 2
	rcParams['ytick.major.size'] = 2
	rcParams['axes.linewidth'] = .5
	rcParams['lines.linewidth'] = .5
	rcParams['patch.linewidth'] = .5
	rcParams['axes.labelsize'] = 8
	rcParams['xtick.labelsize']=8
	rcParams['ytick.labelsize']=8
	rcParams['legend.fontsize']=8
	rcParams['font.size']=7
	rc('font',**{'family':'sans-serif','sans-serif':['Arial']})


	fig = figure(figsize=(4.,2.2))
	ax1=subplot(1, 1, 1)
	im1 = plot(years, extent, 'k')
	#im2 = plot(Years[start_year_pred-start_year:], lineT[start_year_pred-start_year:]+ExtentG, 'r')

	#im3 = plot(years[-1], extent[-1], marker='o', markersize=2, color='k')
	im3 = plot(yearsP, forecastVarsM[:, 2], marker='x', markersize=1, alpha=0.5, color='k')
	im3 = plot(yearsP, forecastVarsM[:, 3], marker='o', markersize=1, alpha=0.5, color='r')
	#ax1.errorbar(yearsP, forecastVarsM[:, 3] , yerr=forecastVarsM[:, 6], color='r',fmt='',linestyle='',alpha=0.5, lw=0.6,capsize=0.5, zorder = 2)

	im3 = plot(yearsP[-1], forecastVarsM[-1, 2], marker='x', markersize=2, color='k')
	im3 = plot(yearsP[-1], forecastVarsM[-1, 3], marker='o', markersize=2, color='r')
	ax1.errorbar(yearsP[-1], forecastVarsM[-1, 3] , yerr=forecastVarsM[-1, 6], color='r',fmt='',linestyle='',lw=0.6,capsize=0.5, zorder = 2)

	#errorbar(YearsP, array(lineTP)+array(ExtentG) , yerr=prederror, color='r',fmt='',linestyle='',lw=0.4,capsize=0.5, zorder = 2)
	#if (np.isfinite(forecastVars[4])):

	#ax1.errorbar(yearsP, extentPredAbs , yerr=[1.96*x for x in perr], color='r',fmt='',linestyle='',lw=0.3,capsize=0.5, zorder = 2)

	forecastStr='%.2f' %(forecastVarsM[-1, 3])
	linearStr='%.2f' %(forecastVarsM[-1, 2])
	#observedStr='%.2f' %(extent[-1])

	ax1.annotate('Year: '+str(year)+'\nLinear trend forecast: '+linearStr+r' M km$^2$',
			xy=(0.8, 1.02), xycoords='axes fraction', horizontalalignment='left', verticalalignment='top')

	ax1.annotate('NASA GSFC forecast: '+forecastStr+r' M km$^2$',
			xy=(0.8, 0.9), xycoords='axes fraction', color='r', horizontalalignment='left', verticalalignment='top')

	#ax1.annotate('Year: '+str(year)+'\nObserved: '+observedStr+r' M km$^2$'+'\nTrend: '+linearStr+r' M km$^2$',
	#		xy=(1., 1.02), xycoords='axes fraction', horizontalalignment='left', verticalalignment='top')

	#ax1.annotate(,
	#		xy=(0.8, 0.85), xycoords='axes fraction', color='r', horizontalalignment='left', verticalalignment='top')

	#ax1.annotate('NASA GSFC forecast: '+forecastStr+r' M km$^2$'+'\nSkill:'+skill,
	#		xy=(0.8, 0.85), xycoords='axes fraction', color='r', horizontalalignment='left', verticalalignment='top')

	ax1.annotate(textStr, fontsize=5, 
			xy=(0.02, 0.02), xycoords='axes fraction', horizontalalignment='left', verticalalignment='bottom')

	ax1.set_ylabel('Ice '+iceType+r' (Million km$^2$)')
	#ax1.set_xlabel('Years')
	ax1.set_xlim(1980, 2020)
	ax1.set_xticks(np.arange(1980, 2021, 10))
	ax1.set_xticks(np.arange(1980, 2021, 5), minor=True)
	#ax1.set_xticklabels([])
	ax1.set_ylim(minval, maxval-0.1)
	ax1.set_yticks(np.arange(minval, maxval, 1))

	ax1.spines['right'].set_visible(False)
	ax1.spines['top'].set_visible(False)

	ax1.yaxis.grid(which='major', linestyle='-', linewidth='0.1', color='k')
	#plt.tight_layout()
	#subplots_adjust(left=0.15, right=0.90, bottom=0.17, top=0.96, hspace=0)
	subplots_adjust(left=0.11, right=0.74, bottom=0.1, top=0.96, hspace=0)

	savefig(figPath+'/'+outStr+hemStr+'multi.png', dpi=300)
	savefig(figPath+'/'+outStr+hemStr+'multi.jpg', dpi=300)
	close(fig)
#set region features to array
iceConc = ff.get_gridvar(derivedDataPath, forecastVar, month, array(year),
                         hemStr)

for regInd in range(16):
    regionData = iceConc.data
    regionData[iceConc.mask == True] = 0  #get rid of nan
    desiredRegion = regionMask == regInd
    sample[regInd +
           2] = 100 * np.ma.mean(np.multiply(regionData, desiredRegion))

sample = np.reshape(sample, (1, -1))
feat = pcaConc.transform(sample)
#feat=sample

_, extent = ff.get_ice_extentN(rawDataPath, fmonth, year, year, iceType,
                               siiVersion, hemStr)
gTruth = extent[0]

#Ensemble Model run

output = mlpConc.predict(feat)  #THIS OUTPUT REPRESENTS SEA ICE FOR JULY
debug = output

#Testing out penalizing small features as inaccuracies
for i in range(np.size(output)):
    if (output[0][i] < 0.09):
        output[0][i] = 0
        debug[0][i] = 0

month = month + 1
sample = np.zeros(numFeat)