Skip to content

Swat

Intense Rainfall Events

Date Amman Petra ERA5 Weather-Type

Weather-Type

Cumulative Rainfall

Code

# -*- coding: utf-8 -*-

import matplotlib
matplotlib.use('Agg') # Must be before importing matplotlib.pyplot or pylab!
import matplotlib.pyplot as P
from mpl_toolkits.basemap import Basemap,maskoceans
from netCDF4 import Dataset,num2date
from scipy.io import netcdf
import numpy as N
import datetime
from matplotlib.offsetbox import AnchoredText
import string

params = {'legend.fontsize': 8,'font.family': 'serif'}
P.rcParams.update(params)

P.style.use('seaborn-talk')


def ma(a,n=3):
    ret=N.cumsum(a,dtype=float)
    ret[n:]=ret[n:]-ret[:-n]
    return ret[n-1:]/n
orte = {'amman':'320359','petra':'304356'}

amm = N.genfromtxt('../data/swat/amman/weatherdata-320359.csv',names=True,delimiter=',',dtype=None,encoding=None)
pet = N.genfromtxt('../data/swat/petra/weatherdata-304356.csv',names=True,delimiter=',',dtype=None,encoding=None)
era = N.genfromtxt('../data/era5/iera5_prcp_daily_35-37E_31-33N_n_5lan_su_max.dat',usecols=(0,1),delimiter=';',dtype=None,encoding=None,comments='#')
gwl = N.genfromtxt('../data/gwls/jor.txt',usecols=(0,1),delimiter=';',dtype=None,encoding=None)

gwls = {}

for g in list(set(gwl[:,1])):

    gwls[g] = []

rint = {}

for i in N.arange(5,65,5):

    i = '%02i'%i

    rint[i] = []

names = amm.dtype.names

date0 = []

for d in amm["Date"]:

    d = d.replace('"','')
    d = d.split('/')

    date0.append('%4i-%02i-%02i'%(int(d[2]),int(d[0]),int(d[1])))

date0 = N.array(date0)

date1 = []

for d in pet["Date"]:

    d = d.replace('"','')
    d = d.split('/')

    date1.append('%4i-%02i-%02i'%(int(d[2]),int(d[0]),int(d[1])))

date1 = N.array(date1)

date2 = []
jahr2 = []

for d in era:

    d = str(d[0])

    date2.append('%4s-%2s-%2s'%(d[0:4],d[4:6],d[6:8]))
    jahr2.append(int(d[0:4]))

date2 = N.array(date2)
jahr2 = N.array(jahr2)

date3 = []

for d in gwl:

    d = d[0]

    d = d.split('-')

    date3.append('%4i-%02i-%02i'%(int(d[0]),int(d[1]),int(d[2])))

date3 = N.array(date3)

nd = len(date2)

f = open('./include/rx30.md','w')
f.write('Date|Amman|Petra|ERA5|Weather-Type\n')
f.write('---|---|---|---|---\n')

for d in range(nd):

    print (date2[d])

    id = N.where(date2[d]==date0)[0]

    if(len(id)==1):

        nied0 = amm["Precipitation"][id[0]] 
        nied0 = float(nied0.replace('"',''))

    else:

        nied0 = -999.9

    id = N.where(date2[d]==date1)[0]

    if(len(id)==1):

        nied1 = pet["Precipitation"][id[0]]
        nied1 = float(nied1.replace('"',''))

    else:

        nied1 = -999.9

    nied2 = era[d][1]

    id = N.where(date2[d]==date3)[0]

    if(len(id)==1):

        gwl4 = gwl[id[0],1]

        gwls[gwl4].append(nied2)

        for g in rint:

            if((nied1>=int(g))&(nied1<(int(g)+2))):

                rint[g].append(gwl4)

    else:

        gwl4 = '---'

    if((nied0>30.)|(nied1>30)):    

        f.write('%s|%04.1f|%04.1f|%04.1f|%s\n'%(date2[d],nied0,nied1,nied2,gwl4))

f.close()

Plotting

P.figure(figsize=(8,5))
ax = P.subplot(111)

gws = list(gwls.keys())

diag = {'x':[],'y':[]}

for rs in rint:

    tmp = []

    for gw in gws: 

        id = N.where(N.array(rint[rs])==gw)[0]        

        tmp.append(len(id))

    tmp = N.array(tmp)
    ind = N.argsort(tmp)

    id = N.where(tmp>0)[0]

    diag['x'].append(int(rs))
    diag['y'].append(len(id))

    k = -1

    for i in id:

        k = k+1

        if(k>(len(id)-2)):

            P.text(int(rs)+1.5,0.3+k,gws[i],fontsize=8,color='w',horizontalalignment='center',weight='bold')

        else:

            P.text(int(rs)+1.5,0.3+k,gws[i],fontsize=8,color='w',horizontalalignment='center')

P.bar(N.array(diag['x'])+2.5,diag['y'],5,color='dodgerblue',ec='k',lw=1.0)

P.xticks([5,10,15,20,25,30,35,40,45,50,55,60])
P.xlim(0,70)
P.yticks([0,2,4,6,8,10])
P.ylim(0,10)


P.xlabel('Daily Rainfall Intensity (mm/d)',fontsize=14,weight='bold')
P.ylabel('Number of Contributing Weather-Types',fontsize=12,weight='bold')

at = AnchoredText('Amman',prop=dict(size=14),frameon=True,loc='upper right')
at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
ax.add_artist(at)

P.savefig('./img/swat.png',dpi=160,bbox_inches='tight')

Cumulative

jo = N.arange(1981,2022,1)
nj = len(jo)
do = N.arange(1,366,1)
nd = len(do)

nied2 = []

for d in era:

    nied2.append(d[1])

nied2 = N.array(nied2)

tmp = N.zeros((nj,nd),float)
tmp[:,:] = N.nan

for j in range(nj):

    id = N.where(jahr2==jo[j])[0]

    if(len(id)<nd): nd = len(id)

    id = id[:nd]

    tmp[j,:nd] = nied2[id]

P.figure(figsize=(9,5))
ax = P.subplot(111)

P.bar(ma(do,7),ma(N.nanmax(tmp,0),7),3,color='dodgerblue',ec='k',alpha=0.5,zorder=5)

P.ylabel('Max. Rainfall (mm/d)',fontsize=14,weight='bold')
P.yticks([0,20,40,60,80,100,120],fontsize=12,weight='bold')
P.ylim(0,120)


P.twinx()

for j in range(nj):

    if(jo[j]==2021):

        P.plot(do,N.cumsum(tmp[j,:]),'k',lw=0.7,zorder=10)

    else:

        P.plot(do,N.cumsum(tmp[j,:]),'k',alpha=0.3,lw=0.7,zorder=10)

P.ylabel('Cum. Rainfall (mm)',fontsize=14,weight='bold')
P.xticks([1,31,61,91,121,151,181,211,241,271,301,331],['1.Jan','1.Feb','1.Mar','1.Apr','1.May','1.Jun','1.Jul','1.Aug','1.Sep','1.Oct','1.Nov','1.Dec'],fontsize=10,weight='bold')
P.yticks([0,200,400,600,800,1000,1200],fontsize=12,weight='bold')

P.xlim(1,365)
P.ylim(0,1200)

at = AnchoredText('ERA5: Jordan',prop=dict(size=14),frameon=True,loc='upper left')
at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
ax.add_artist(at)


P.savefig('./img/cum.png',dpi=160,bbox_inches='tight')