Skip to content

Transition

Generated weather-type transistions per month.


Transition

Fig.: Long-term monthly characteristics of weather-type transitions: persistence (horizontal lines).


Code

Importing

from sklearn import tree #For our Decision Tree
import pandas as pd # For our DataFrame
import pydotplus # To create our Decision Tree Graph
from IPython.display import Image  # To Display a image of our graph
import numpy as N
import pylab as P
import random
import datetime
from scipy import signal,stats

Functioning

def mov_sum(a,n=3):
    ret=N.cumsum(a,dtype=float)
    ret[n:]=ret[n:]-ret[:-n]
    return ret[n-1:]

Setting

P.style.use('bmh')
P.rcParams["font.family"] = "serif"
P.rcParams["legend.fontsize"] = 8
jz = 'ja'

Reading

file = '../csv/gen_1961-1990/gen_1961-1990.txt' 
dat0=N.genfromtxt(file,names=True,comments='#',dtype=None,encoding='utf-8')

file = '../csv/gen_1990-2019/gen_1990-2019.txt' 
dat1=N.genfromtxt(file,names=True,comments='#',dtype=None,encoding='utf-8')

go = N.array(['WA','WZ','WS','WW','SWA','SWZ','NWA','NWZ','HM','BM','TM','NA','NZ','HNA','HNZ','HB','TRM','NEA','NEZ','HFA','HFZ','HNFA','HNFZ','SEA','SEZ','SA','SZ','TB','TRW'])

#go = list(set(dat0['gw']))
ng = len(go)
nd = len(dat0)
mo = N.arange(1,13,1)
nm = len(mo)

print (go)

Processing

gw = N.array(dat0['gw'])

nn = N.zeros(ng,float)

for g in range(ng):

    id = N.where(gw==go[g])[0]

    nn[g] = len(id)

id = N.argsort(nn)

go = go[id]
ng = len(go)

trans0 = N.zeros((ng,ng,nm),float)
trans1 = N.zeros((ng,ng,nm),float)

for d in range(1,nd):

    id0 = N.where(go==dat0['gw'][d-1])[0]
    id1 = N.where(go==dat0['gw'][d])[0]
    im0 = N.where(mo==dat0['mo'][d-1])[0]   

    trans0[id0,id1,im0] = trans0[id0,id1,im0] + 1   

    id0 = N.where(go==dat1['gw'][d-1])[0]
    id1 = N.where(go==dat1['gw'][d])[0]
    im0 = N.where(mo==dat1['mo'][d-1])[0]       

    trans1[id0,id1,im0] = trans1[id0,id1,im0] + 1

#vmax = N.max(trans1)

#print (vmax)

Plotting

fig = P.figure(figsize=(12,12))

for k in ['1961-1990','1990-2019']:

    if(k=='1961-1990'): 

         P.subplot(211) 
         trans = trans0

    if(k=='1990-2019'): 

         P.subplot(212)
         trans = trans1

    gg = N.arange(ng)
    mm = N.arange(nm)

    for m in range(nm+1):

        P.plot(m+N.zeros(ng),N.arange(ng),'k-o',zorder=10)

    TRW2TRM = 0
    TRM2TRM = 0

    for m in range(nm):

        for x in range(ng):
            for y in range(ng):

                if((go[x]=='TRW')&(go[y]=='TRM')):  

                    TRW2TRM = TRW2TRM + trans[x,y,m] 

                    P.plot([m,m+1],[x,y],'b',alpha=0.6,lw=trans[x,y,m]/100.,zorder=5)

                elif((go[x]=='TRM')&(go[y]=='TRM')):    

                    TRM2TRM = TRM2TRM + trans[x,y,m] 

                    P.plot([m,m+1],[x,y],'b',alpha=0.6,lw=trans[x,y,m]/1000.,zorder=5)

                else:

                    if(x==y): P.plot([m,m+1],[x,y],'r',alpha=0.6,lw=trans[x,y,m]/1000.,zorder=5)
                    if(x!=y): P.plot([m,m+1],[x,y],'k',alpha=0.2,lw=trans[x,y,m]/100.)

    print (k,TRW2TRM)
    print (k,TRM2TRM)

    P.xlim(-0.5,12.5)
    P.ylim(-1,29)


    P.xticks(mm+0.5,['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],weight='bold')
    P.yticks(gg,go,fontsize=10,weight='bold')
    P.grid()

    P.tick_params(direction='out')

    P.title('%s: TRW-TRM = %.1f; TRM-TRM = %.1f per Year'%(k,TRW2TRM/1000.,TRM2TRM/1000.),fontsize=16,weight='bold')

P.tight_layout()
P.savefig('./img/transition.png',dpi=240,transparent=False,bbox_inches='tight',pad_inches=0.0)