AMBHAS
rain_disagg.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003 Created on Tue May 24 18:07:28 2011
00004 
00005 @author: Sat Kumar Tomer
00006 @website: www.ambhas.com
00007 @email: satkumartomer@gmail.com
00008 """
00009 
00010 # import required libraries
00011 from __future__ import division
00012 import numpy as np
00013 from ambhas.errlib import rmse
00014 from scipy.optimize import fmin
00015 from scipy.stats import poisson
00016 
00017 class RainDisagg:
00018     
00019     
00020     def __init__(self,rf):
00021         self.rf = rf
00022             
00023         # calculate the length of rainfall series
00024         len_rf = len(rf)
00025                   
00026         # take only that much length of rainfall series which is multiplication of 32 
00027         rf_1 = rf[0:len_rf-np.mod(len_rf,32)]
00028         
00029         # summing rainfall for 2 days
00030         rf_2 = np.sum(rf_1.reshape(-1,2), axis=1)
00031         
00032         # summing rainfall for 4 days
00033         rf_4 = np.sum(rf_2.reshape(-1,2), axis=1)
00034         
00035         # summing rainfall for 8 days
00036         rf_8 = np.sum(rf_4.reshape(-1,2), axis=1)
00037         
00038         # summing rainfall for 16 days
00039         rf_16 = np.sum(rf_8.reshape(-1,2), axis=1)
00040         
00041         # summing rainfall for 32 days
00042         rf_32 = np.sum(rf_16.reshape(-1,2), axis=1)
00043         
00044         #generate moments (q varies from 0 to 5)
00045         # row => time scale
00046         # column => moments
00047         M1 = np.zeros((6,11))
00048         for i in range(11):
00049             M1[0,i] = np.mean(rf_1**(i/2))
00050             M1[1,i] = np.mean(rf_2**(i/2))
00051             M1[2,i] = np.mean(rf_4**(i/2))
00052             M1[3,i] = np.mean(rf_8**(i/2))
00053             M1[4,i] = np.mean(rf_16**(i/2))
00054             M1[5,i] = np.mean(rf_32**(i/2))
00055         
00056         self.M1 = M1
00057         self.logM = np.log(M1)
00058         
00059         # l is inverse of time scale
00060         l = [32, 16, 8, 4, 2, 1]
00061         self.log_lambda = np.log(l)
00062         
00063         
00064         # calculate the tau from the slope of log(M) vs log(l)
00065         tau_obs = np.zeros(10,)
00066         for i in range(10):
00067             tau_obs[i] = -np.polyfit(np.log(l), np.log(M1[:,i+1]),1)[0]
00068         self.tau_obs = tau_obs
00069         
00070         # fit the log-poisson distribution
00071         self.lp = fmin(self.fun_poisson,np.array([0.4, 0.2]))
00072         
00073         # calculate the parameter of log poisson distribution form the parameters
00074         # of tau function
00075         self.A = np.exp(self.lp[0]*(1-self.lp[1]))
00076 
00077     def tau_predict(self):
00078         q = np.arange(0.5,5.5,0.5)
00079         c = abs(self.lp[0])
00080         beta = abs(self.lp[1])
00081         b = 2
00082         tau_pred = q-c*(q*(1-beta)+beta**q-1)/(np.log(b))
00083         self.tau_pred = tau_pred
00084         self.q = q
00085 
00086     #define the log-poisson function
00087     def fun_poisson(self,par):
00088         q = np.arange(0.5,5.5,0.5)
00089         c = abs(par[0])
00090         beta = abs(par[1])
00091         b = 2
00092         tau_pred = q-c*(q*(1-beta)+beta**q-1)/(np.log(b))
00093         f = rmse(tau_pred,self.tau_obs)
00094         return f
00095     
00096     def disaggregate(self,rf):
00097         len_rf = len(rf)        
00098         # generating rainfall from t h to t/2 h
00099         rf_pre = np.zeros((1,len_rf*2))
00100         for j in range(1):
00101             for i in xrange(0,len_rf*2,2):
00102                 W = self.A*(self.lp[1])**poisson.rvs(1, size=2)
00103                 W[W<0] = 1e-6
00104                 rf_pre[j,i] = rf[int(i/2)]*W[0]/(W[0]+W[1])
00105                 rf_pre[j,i+1] = rf[int(i/2)]*W[1]/(W[0]+W[1])              
00106                              
00107         
00108         rf_pre = np.mean(rf_pre, axis=0)
00109         
00110         # rounding up the simulated rainfall to the least count of raingauge 
00111         for i in xrange(0,len_rf*2,2):
00112             if np.mod(rf_pre[i],0.5) !=0:
00113                 TB = np.mod(rf_pre[i],0.5)
00114             else:
00115                 TB = 0
00116             
00117             rf_pre[i] -= TB
00118             rf_pre[i+1] += TB
00119             
00120             
00121         
00122         return rf_pre
00123 
 All Classes Namespaces Files Functions Variables