def check_TSI(start=1860,end=2100,compress=True,graph=False): # open data files from open import open_pkl data = open_pkl(king_sol,'TSI.year1860-2100.month01-12.sf.pkl') # this is in months inclusive of BOTH end years months = data['sf'] months.shape = (months.shape[0]/12,12) if compress == True: # define empty array tsi = np.array([]) i = 1 # exclude end winters as they do not have fully representative values for year in range(start,end): tmp = np.array([months[i][0],months[i][1],months[i-1][11]]) tsi = np.append(tsi,np.mean(tmp)) i += 1 pass # print 'The TSI distribution runs from '+str(start)+' to '+str(end-1) if graph == True: from plots import line line(range(start,end),tsi,'TSI distribution from '+str(start)+'-'+str(end-1),'years','TSI',king_gra+'long_run/tsi_'+str(start)+'-'+str(end)+'month12-14.png') return tsi
typ = 'Max-Clim' diff = np.mean(scmax,0) - np.mean(all_data,0) test_dat1,test_dat2 = np.zeros(scmax.shape), scmax - np.mean(all_data,0) if mncl == True: typ = 'Min-Clim' diff = np.mean(scmin,0) - np.mean(all_data,0) test_dat1,test_dat2 = np.zeros(scmin.shape), scmin - np.mean(all_data,0) # print result for clarification print 'Type: '+typ # get lat-lon coordinates of these arrays and append to a matrix from open import open_pkl opt = [] data_xy = open_pkl(dat,str(run)+'.pj.year-2010.month-02.b.thpv2_003.duration_ge_5_day.pkl') Lon,Lat = data_xy['lon']['lon'],data_xy['lat']['lat'] Lon = np.append(Lon,360+Lon[0]) X,Y = meshgrid(Lon,Lat) opt.append(X) opt.append(Y) # obtain final value for diff so values overlap a = zeros((len(diff),1)) diff = np.concatenate((diff,a),axis=-1) for lat in range(0, len(diff)): diff[lat][-1] = diff[lat][0] opt.append(diff) if sig == True: try:
def diff1(king,king_dat,output,high=True,type='thpv2',v='v',monte=False,sttest=True,tcrit=0.87,graph=True,filled=False,sig=False,monthly=1,trials=1000,cutoff=0.90): ################################################################# # METHOD: 25/11/13 # # # # take high/low solar data # # compute average blocking frequency for each lat lon point # # then difference from all-time data (climatological data) # # plot this difference using basemap and compare to Woolings # # Note: this function returns the difference data # # # ################################################################# try: from open import open_pkl #read in x-y data opt = [] data_xy = open_pkl(king_dat,'era40.gga'+v+'.year-2002.month-01.b.'+type+'_003.duration_ge_5_day.pkl') Lon,Lat = data_xy['lon']['lon'],data_xy['lat']['lat'] Lon = np.append(Lon,360+Lon[0]) #print Lon X,Y = meshgrid(Lon,Lat) opt.append(X) opt.append(Y) # #read in data if high == True: data = read_list(king+'high_era40_blocking_'+type+'.list',king_dat) #generate listname listnm = 'era40_blocking_'+str(type)+'_high_blk' stype = 'high' elif high != True: data = read_list(king+'low_era40_blocking_'+type+'.list',king_dat) #generate listname listnm = 'era40_blocking_'+str(type)+'_low_blk' stype = 'low' clim = read_list(king+'era40_blocking_'+type+'.list',king_dat) #checks of data read in if len(data) != len(clim) or len(data[0]) != len(clim[0]): print "Error: Array lengths don't match\nData: "+str(len(data)),str(len(data[0]))+"\nClim: "+str(len(clim)),str(len(clim[0])) return 0 #compute difference array diff = zeros(shape=(len(data),len(data[0])+1)) test = [] for lat in range(0,len(data)): for lon in range(0,len(data[0])): diff[lat][lon] = -clim[lat][lon] + data[lat][lon] #final value for diff for lat in range(0, len(data)): diff[lat][-1] = -clim[lat][0] + data[lat][0] opt.append(diff) if sig == True: try: from sig_test import sig_test regions = sig_test(list_name='era40_blocking_thpv2.list',list_dir=king,monte=monte,sttest=sttest,tcrit=tcrit,data_dir=king_dat,high=high,trials=1000,cutoff=cutoff) extra = regions[:,0] extra.shape = (regions[:,0].shape[0],1) regions = np.concatenate((regions,extra),1) except ValueError as err: print "Value Error: " + str(err) if graph == True: #generate output filename if monte == True: sgtype = 'monte' if sttest == True: sgtype = 'ttest' output = str(output)+'blk'+str(listnm)+str(sgtype)+str(int(100*(1-cutoff)+1))+'.png' from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt # use low resolution coastlines. fig,ax = plt.subplots() # fig = plt.figure() map = fig.add_subplot() map = Basemap(boundinglat=Lat[-1],lon_0=0,projection='npaeqd',resolution='l',round=True) lon,lat = np.array(opt[0]),np.array(opt[1]) x,y = map(lon,lat) # draw coastlines, country boundaries, fill continents. map.drawcoastlines(linewidth=0.25) map.drawcountries(linewidth=0.25) # draw the edge of the map projection region (the projection limb) map.drawmapboundary() #if sig == True: map.fill() if filled == False: a = np.array(range(-100,102,2))/float(100) p = map.contour(x,y,np.array(opt[2]),a,colors='k') elif filled == True: a = np.array(range(-100,102,2))/float(100) c = map.contour(x,y,np.array(opt[2]),10,linestyles='solid',colors='black') p = map.contourf(x,y,-np.array(opt[2]),10,cmap=cm.RdBu,vmin=np.array(opt[2]).min(),vmax=np.array(opt[2]).max(),alpha=0.5,) cb = fig.colorbar(p, ax=ax) t = ax.set_title('blk'+stype+'-clim') if sig == True: # masking the array regions = np.ma.array(regions) interior = regions < 0.5 regions[interior] = np.ma.masked s = map.contourf(x,y,regions,1,cmap=cm.gray_r) t = ax.set_title('blk'+str(stype)+'-clim (sig '+str(int(100*(1.0-cutoff)+1))+'%)') fig.savefig(str(output)) return opt except IOError as err: print "File error: " + str(err) except ValueError as err: print "Value Error: " + str(err)
# METHOD: 25/11/13 # # # # take high/low solar data # # compute average blocking frequency for each lat lon point # # then difference from all-time data (climatological data) # # plot this difference using basemap and compare to Woolings # # Note: this function returns the difference data # # # ################################################################# try: if arr != True from open import open_pkl #read in x-y data opt = [] data_xy = open_pkl(king_dat,'era40.gga'+v+'.year-2002.month-01.b.'+type+'_003.duration_ge_5_day.pkl') Lon,Lat = data_xy['lon']['lon'],data_xy['lat']['lat'] Lon = np.append(Lon,360+Lon[0]) #print Lon X,Y = meshgrid(Lon,Lat) opt.append(X) opt.append(Y) # #read in data from open import stdata all_data = stdata('[DIR]/era40_blocking_thpv2.list',directory='/media/jonathan/KINGSTON/blocking/data/pkl_files/blocking/',monthly='thpv2') from solar import years yrs = np.array(years()['SCmin'])-1957 if high == True: yrs = np.array(years()['SCmax'])-1957 data = np.mean(all_data[yrs],axis=0)
def jet(dat_dir=king_dat,strength='jsi',Atlantic=True,start=1957,end=2001,a=305,b=90,compress=False,select=True,mnmx='SCmax'): # want to select high/low solar if select == True: from solar import years yrs = years()[mnmx] # call data in from open import open_pkl strength = strength Atlantic = True if Atlantic == True: sea = 'Atlantic' elif Atlantic == False: sea = 'Pacific' # use import pickle extractor database = open_pkl(dat_dir,'era40.year'+str(start)+'-'+str(end)+'.month11-15.'+str(strength)+'.JI_001.'+str(sea)+'.pkl') data = database[str(database['info']['name'])] data.shape = data.shape[-1] # compress days into months, first get the days database days = database['time']['day_of_year'] # firstly create a loop for specific number of years # identify empty data array jdat = np.array([]) # empty arrays jet = np.array([]) tmp = np.array([]) # counters i,dat=1,0 # check if first year is leap year year = start last = b if year % 4 == 0: if year % 100 == 0 and year % 400 != 0: last = b+1 # loop over days sum = 0 for day in days: tmp = np.append(tmp,data[dat]) if i == len(days): # final append before breaking if compress == True: jet = np.append(jet,np.mean(tmp)) else: if year in yrs: jet = np.append(jet,tmp) tmp = np.array([]) break if day == last: #once the winter end has been reached append this data sum += len(tmp) if compress == True: jet = np.append(jet,np.mean(tmp)) else: if year in yrs: jet = np.append(jet,tmp) tmp = np.array([]) # check if next year will be leap year year += 1 last = b if (year+1) % 4 == 0: if (year+1) % 100 == 0 and (year+1) % 400 != 0: pass else: last = b+1 i += 1 dat += 1 return jet
def plots(SC='SCmax',compress=False,select=True,two_hist=True,wrttime=False,hist=False,strength='jsi',start=1957,end='2001',sig=False,sea='Atlantic'): # get data data = jet(strength=strength) # plot data wrt time to compare to blocking cycle import matplotlib.pyplot as plt from solar import years yrs = np.array(years()[SC])-start all_yrs = np.array(range(1957,2002))-start if wrttime == True: fig = plt.figure() x = all_yrs+start#linspace(0, 5, 10) y = data axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # left, bottom, width, height (range 0 to 1) axes.plot(x, y, 'r') axes.set_xlabel('time (winters)') axes.set_ylabel(strength) axes.set_title('Jet '+strength+' wrt time'); fig.savefig(king_gra+'jet_'+strength+'_wrt_time') if two_hist == True: if select == False: from open import open_pkl database = open_pkl(king_dat,'era40.year'+str(start)+'-'+str(end)+'.month11-15.jsi.JI_001.'+str(sea)+'.pkl') sdata = database[str(database['info']['name'])] sdata.shape = sdata.shape[-1] database = open_pkl(king_dat,'era40.year'+str(start)+'-'+str(end)+'.month11-15.jli.JI_001.'+str(sea)+'.pkl') ldata = database[str(database['info']['name'])] ldata.shape = ldata.shape[-1] elif compress == False: sdata = jet(strength='jsi',mnmx=SC) ldata = jet(strength='jli',mnmx=SC) else: sdata = jet(strength='jsi',compress=True) ldata = jet(strength='jli',compress=True) from matplotlib.colors import LogNorm fig = plt.figure() # plt.subplots # plt.scatter(ldata,sdata) plt.hist2d(ldata,sdata, bins=40, cmap='Greens', norm=LogNorm()) ax = plt.gca() ax.set_xlabel('jli') ax.set_ylabel('jsi') plt.title("Jet jsi v jli for "+SC) plt.colorbar() # plt.xlim(-15, 15) # plt.ylim(-15, 15) plt.show() fig.savefig(king_gra+'jet_jsi_v_jli_'+SC) if hist == True: # plot a histogram of values based on high/low solar max = np.array(years()['SCmax'])-start min = np.array(years()['SCmin'])-start # years() # # print years # print data[yrs] # print np.mean(data[yrs]),np.mean(data[max]),np.mean(data[min]) plt.subplots() plt.hist(data[all_yrs],20) plt.subplots() plt.hist(data[max],20) plt.subplots() plt.hist(data[min],20) #try a ttest for the means if sig == True: import sig_test data.shape = (data.shape[-1],1,1) print sig_test.ttest(data,data[min],tcrit=1.34)[0][0]