def GraphEvents(self): ''' DESCRIPTION: Gráfica de los eventos. ''' PathImg = 'Tesis_MscR/02_Docs/01_Tesis_Doc/Kap2/Img/'+self.DataBase+'/Series/' Name = self.NamesArch[self.irow]+'_Series' Var = ['Precipitación','Temperatura','Humedad Relativa','Presión'] BP.GraphIDEA(self.f['FechaCP'],self.f['PrecC'], self.f['TC'],self.f['HRC'],self.f['PresC'],Var,PathImg,0,V=0,Name=Name) return
def InterData(self): ''' DESCRIPTION: Función para interpolar los datos NaN que se puedan. ''' print(' Se interpolan los datos NaN') # Se interpolan los datos que se pueden interpolar for iv,v in enumerate(self.Variables): if self.flag[v]: self.f[self.Variables2[v]] = BP.NaNEl(self.f[v]) self.var = list(self.f) self.flag['Variables2'] = True return
def Cycle_An(self): ''' DESCRIPTION: Gráfica los diferentes ciclos climatológicos y meteorológicos. ''' PathImg = 'Tesis_MscR/02_Docs/01_Tesis_Doc/Kap2/Img/'+self.DataBase+'/Hidro_A/' Name = self.NamesArch[self.irow] Var = ['Precipitación','Temperatura','Humedad Relativa','Presión'] Variables = ['Prec','T','HR','Pres'] # Se carga la información Horaria DataH = LD.LoadData(self.ArchT,self.DataBase,irow=self.irow,TimeScale='Horarios') self.DataH = DataH if self.DataBase == 'Manizales': FechaC = DataH['FechaEs'] elif self.DataBase == 'Medellin': FechaC = DataH['FechasC'] elif self.DataBase == 'Amazonas' or self.DataBase == 'Wunder': if self.irow == 1 and self.DataBase == 'Amazonas': FechaC = DataH['FechasC'] else: FechaC = DataH['FechaC'] if self.DataBase == 'Amazonas': FechaCP = DUtil.Dates_str2datetime(FechaC,Date_Format='%Y/%m/%d-%H%M',flagQuick=True) FechaCP = FechaCP - timedelta(0,4*60*60) for Lab in Variables: VC = DMan.CompD(FechaCP,DataH[Lab+'H'],dtm=timedelta(0,60*60)) DataH[Lab+'H'] = VC['VC'] if Lab == 'PrecC': FechaC = VC['DatesC'] # Se carga la información Diaria DataD = LD.LoadData(self.ArchT,self.DataBase,irow=self.irow,TimeScale='Diarios') self.DataD = DataD if self.DataBase == 'Manizales': FechaCD = DataD['FechaEs'] FechaCD = np.array([i[:4]+'/'+i[5:] for i in FechaCD]) elif self.DataBase == 'Medellin' or self.DataBase == 'Amazonas': FechaCD = DataD['FechaEs'] elif self.DataBase == 'Wunder': FechaCD = DataD['FechaM'] self.FechaCD = FechaCD # Se carga la información CH = dict() CM = dict() for Lab in Variables: # Horario try: HASR = HA(DateH=FechaC,VarH=DataH[Lab+'H']) except KeyError: HASR = HA(DateH=FechaC,VarH=DataH[Lab+'CH']) CH[Lab] = HASR.CiclD(FlagG=False) # Diario try: HASR = HA(DateM=FechaCD,VarM=DataD[Lab+'M']) except KeyError: HASR = HA(DateM=FechaCD,VarM=DataD[Lab+'CM']) CM[Lab] = HASR.CiclA(FlagG=False) self.CH = CH self.CM = CM # Se grafica el ciclo diurno BP.Graph_CiclD(CH['Prec'],CH['T'],CH['HR'],CH['Pres'],Var,PathImg,Name=Name) BP.Graph_CiclA(CM['Prec'],CM['T'],CM['HR'],CM['Pres'],Var,PathImg,Name=Name) return
def EventSeparation(self,Ci=60,Cf=60,m=0.2,M=100,mP=-1,MP=-0.5,mT=0.5,MT=1): ''' DESCRIPTION: Función para separar los eventos. ''' print(' Se generan los diferentes diagramas de compuestos') if self.flag['VariablesF']: Var2 = self.VariablesF Var2['PrecC'] = 'PrecC' elif self.flag['Variables2']: Var2 = self.Variables2 else: Var2 = self.Variables_N Var = self.Variables Variable = dict() VariablePres = dict() VariableTemp = dict() for iv,v in enumerate(Var[1:]): if self.flag[v]: print(v) PrecC, Variable[v], FechaEv = BP.ExEv(self.f[Var2['PrecC']],self.f[Var2[v]],self.f['FechaC'],Ci=Ci,Cf=Cf,m=m,M=M,dt=int(self.dtm)) print(PrecC.shape) for iv,v in enumerate(Var): if v != 'PresC': if self.flag[v]: PresC, VariablePres[v], FechaEvPres = BP.ExEvGen(self.f[Var2['PresC']],self.f[Var2[v]],self.f['FechaC'],Ci=Ci,Cf=Cf,m=mP,M=MP,MaxMin='min') for iv,v in enumerate(Var): if v != 'TC': if self.flag[v]: TC, VariableTemp[v], FechaEvTemp = BP.ExEvGen(self.f[Var2['TC']],self.f[Var2[v]],self.f['FechaC'],Ci=Ci,Cf=Cf,m=mT,M=MT,MaxMin='max') xx = 0 FechaEv2=[] # Se realiza una limpeza de los datos adicional for i in range(len(PrecC)): q = ~np.isnan(PrecC[i]) N = np.sum(q) q2 = ~np.isnan(Variable['PresC'][i]) N2 = np.sum(q2) q3 = ~np.isnan(Variable['TC'][i]) N3 = np.sum(q3) a = len(q)*0.70 if N >= a and N2 >= a and N3 >= a: if xx == 0: PrecC2 = PrecC[i] PresC2 = Variable['PresC'][i] TC2 = Variable['TC'][i] HRC2 = Variable['HRC'][i] qC2 = Variable['qC'][i] WC2 = Variable['WC'][i] xx += 1 else: PrecC2 = np.vstack((PrecC2,PrecC[i])) PresC2 = np.vstack((PresC2,Variable['PresC'][i])) TC2 = np.vstack((TC2,Variable['TC'][i])) HRC2 = np.vstack((HRC2,Variable['HRC'][i])) qC2 = np.vstack((qC2,Variable['qC'][i])) WC2 = np.vstack((WC2,Variable['WC'][i])) FechaEv2.append(FechaEv[i]) xx += 1 self.f[self.VariablesComp['PrecC']] = PrecC2 self.f[self.VariablesComp['PresC']] = PresC2 self.f[self.VariablesComp['TC']] = TC2 self.f[self.VariablesComp['HRC']] = HRC2 self.f[self.VariablesComp['qC']] = qC2 self.f[self.VariablesComp['WC']] = WC2 self.f['FechaEv'] = FechaEv2 xx = 0 FechaEv2=[] # Se realiza una limpeza de los datos adicional de presión for i in range(len(PresC)): q = ~np.isnan(PresC[i]) N = np.sum(q) q2 = ~np.isnan(VariablePres['PrecC'][i]) N2 = np.sum(q2) q3 = ~np.isnan(VariablePres['TC'][i]) N3 = np.sum(q3) a = len(q)*0.70 if N >= a and N2 >= a and N3 >= a: if xx == 0: PresC2 = PresC[i] PrecC2 = VariablePres['PrecC'][i] TC2 = VariablePres['TC'][i] HRC2 = VariablePres['HRC'][i] qC2 = VariablePres['qC'][i] WC2 = VariablePres['WC'][i] xx += 1 else: PresC2 = np.vstack((PresC2,PresC[i])) PrecC2 = np.vstack((PrecC2,VariablePres['PrecC'][i])) TC2 = np.vstack((TC2,VariablePres['TC'][i])) HRC2 = np.vstack((HRC2,VariablePres['HRC'][i])) qC2 = np.vstack((qC2,VariablePres['qC'][i])) WC2 = np.vstack((WC2,VariablePres['WC'][i])) FechaEv2.append(FechaEvPres[i]) xx += 1 self.f[self.VariablesComp['PrecC_Pres']] = PrecC2 self.f[self.VariablesComp['PresC_Pres']] = PresC2 self.f[self.VariablesComp['TC_Pres']] = TC2 self.f[self.VariablesComp['HRC_Pres']] = HRC2 self.f[self.VariablesComp['qC_Pres']] = qC2 self.f[self.VariablesComp['WC_Pres']] = WC2 self.f['FechaEv_Pres'] = FechaEv2 xx = 0 FechaEv2=[] # Se realiza una limpeza de los datos adicional de presión for i in range(len(TC)): q = ~np.isnan(TC[i]) N = np.sum(q) q2 = ~np.isnan(VariableTemp['PresC'][i]) N2 = np.sum(q2) q3 = ~np.isnan(VariableTemp['PrecC'][i]) N3 = np.sum(q3) a = len(q)*0.70 if N >= a and N2 >= a and N3 >= a: if xx == 0: TC2 = TC[i] PrecC2 = VariableTemp['PrecC'][i] PresC2 = VariableTemp['PresC'][i] HRC2 = VariableTemp['HRC'][i] qC2 = VariableTemp['qC'][i] WC2 = VariableTemp['WC'][i] xx += 1 else: TC2 = np.vstack((TC2 ,TC[i])) PrecC2 = np.vstack((PrecC2,VariableTemp['PrecC'][i])) PresC2 = np.vstack((PresC2,VariableTemp['PresC'][i])) HRC2 = np.vstack((HRC2,VariableTemp['HRC'][i])) qC2 = np.vstack((qC2,VariableTemp['qC'][i])) WC2 = np.vstack((WC2,VariableTemp['WC'][i])) FechaEv2.append(FechaEvTemp[i]) xx += 1 self.f[self.VariablesComp['PrecC_Temp']] = PrecC2 self.f[self.VariablesComp['PresC_Temp']] = PresC2 self.f[self.VariablesComp['TC_Temp']] = TC2 self.f[self.VariablesComp['HRC_Temp']] = HRC2 self.f[self.VariablesComp['qC_Temp']] = qC2 self.f[self.VariablesComp['WC_Temp']] = WC2 self.f['FechaEv_Temp'] = FechaEv2 self.flag['VariablesComp'] = True return
def GraphValidation(self, Prec, Name='', NameArch='', PathImg='', ImgFolder='', Evmax=1, EvType='Tot', flags={ 'TC': False, 'PresC': False, 'HRC': False, 'qC': False, 'WC': False }, flagAver=False, flagBig=False, DataV=None, DataKeyV=['DatesEvst', 'DatesEvend'], vm={ 'vmax': [None], 'vmin': [0] }, GraphInfoV={ 'color': ['-.b', '-.g'], 'label': ['Inicio del Evento', 'Fin del Evento'] }): ''' DESCRIPTION: Con esta función se pretenden graficar los diferentes eventos de precipitación por separado. Es necesario haber corrido la función EventsPrecPresDetection antes. _________________________________________________________________________ INPUT: + ImgFolder: Carpeta donde se guardarán los datos. _________________________________________________________________________ OUTPUT: Se generan los gráficos ''' # Constants self.PathImg = PathImg dt = self.dt Labels = ['PresC', 'TC', 'HRC', 'qC', 'WC'] UnitsDict = { 'TC': 'Temperatura [°C]', 'PresC': 'Presión [hPa]', 'HRC': 'Humedad Relativa [%]', 'qC': 'Humedad Específica [g/kg]', 'WC': 'Relación de Mezcla [g/kg]' } ColorDict = {'TC': 'r', 'PresC': 'k', 'HRC': 'g', 'qC': 'g', 'WC': 'm'} LabelDict = { 'TC': 'Temperatura', 'PresC': 'Presión', 'HRC': 'Humedad Relativa', 'qC': 'Humedad Específica', 'WC': 'Relación de Mezcla de V.A.' } DataKeys = ['PrecC'] Units = ['Precipitación [mm]'] Color = ['b'] Label = ['Precipitación'] Vars = 'Prec' for iLab, Lab in enumerate(Labels): if flags[Lab]: DataKeys.append(Lab) Units.append(UnitsDict[Lab]) Color.append(ColorDict[Lab]) Label.append(LabelDict[Lab]) Vars += '_' + Lab[:-1] Vars += '_Events' self.EvSi = 0 self.EvNo = 0 EvTot = self.SeriesC for iM, M in enumerate(self.MT): if iM == 0: EvTot['PrecC'] = Prec[-int(self.Extract[0] / dt) + M:M + int(self.Extract[1] / dt)] else: EvTot['PrecC'] = np.vstack( (EvTot['PrecC'], Prec[-int(self.Extract[0] / dt) + M:M + int(self.Extract[1] / dt)])) if np.nanmax(Prec[M:M + int(2 * 60 / dt)]) > 0: self.EvSi += 1 else: self.EvNo += 1 if flagAver: # Se grafican los eventos en promedio dt = int(self.dt) Data = dict() Data['PrecC'] = np.nanmean(EvTot['PrecC'], axis=0) for iLab, Lab in enumerate(Labels): if flags[Lab]: Data[Lab] = np.nanmean(EvTot[Lab], axis=0) BP.EventsSeriesGen(EvTot['DatesP'][0], Data, self.PrecCount, DataKeyV=DataKeyV, DataKey=DataKeys, PathImg=self.PathImg + ImgFolder + 'Series/' + EvType + '/' + Vars + '/', Name=self.Names[self.irow], NameArch=self.NamesArch[self.irow], GraphInfo={ 'ylabel': Units, 'color': Color, 'label': Label }, GraphInfoV={ 'color': ['-.b', '-.g'], 'label': ['Inicio del Evento', 'Fin del Evento'] }, flagBig=flagBig, vm=vm, Ev=0, flagV=False, flagAverage=True, dt=dt) else: # Se grafican los eventos for iEv in range(len(EvTot['PrecC'])): if iEv <= Evmax: Data = dict() Data['PrecC'] = EvTot['PrecC'][iEv] for iLab, Lab in enumerate(Labels): if flags[Lab]: Data[Lab] = EvTot[Lab][iEv] if not (DataV is None): DataVer = dict() for iLab, Lab in enumerate(DataKeyV): DataVer[Lab] = DataV[Lab][iEv] else: DataVer = None BP.EventsSeriesGen(EvTot['DatesP'][iEv], Data, DataVer, DataKeyV=DataKeyV, DataKey=DataKeys, PathImg=self.PathImg + ImgFolder + 'Series/' + EvType + '/' + Vars + '/', Name=Name, NameArch=NameArch, GraphInfo={ 'ylabel': Units, 'color': Color, 'label': Label }, GraphInfoV=GraphInfoV, flagV=False, flagBig=flagBig, vm={ 'vmax': [None], 'vmin': [0] }, Ev=iEv, Date=EvTot['Dates'][iEv][0]) return
def __FindValue(self, Var='Pres', Oper='min', Comp='<', M=None, limits=[-5, 5]): ''' DESCRIPTION: Method that find the minimum or maximum value in the series and calculates the changes. _________________________________________________________________________ INPUT: :param Var: An str, str of the dictionary of the Attribute Series. :param VarC: An str, str of the dictionary of the Attribute Series when comparation. :param Oper: A str, Function that would search the value ('max','min'). :param Extract: A list, List with the limits of the extracted values in minutes. :param Comp: A str, string with the comparation that would be made. :param M: A int or None, Integer with the position of the maximum of minimum, if None it searches in the whole series. :param limits: A list or None, List with the limits of search from the value of M. _________________________________________________________________________ OUTPUT: ''' # Constants dt = self.dt Extract = self.Extract # Determine Operation O = self.Oper[Oper.lower()] Comp = DM.Oper_Det(Comp) flagRange = True if M is None: flagRange = False if flagRange: MVal = O(self.Series[Var][int(limits[0] / dt) + M:M + int(limits[1] / dt)]) # Validation of the data if Comp(MVal, self.Limits[Var]['MV']): # Se extrae el evento Event = self.Series[Var][-int(Extract[0] / dt) + M:int(Extract[1] / dt) + M].copy() if -int(self.Extract[0]) + M < 0: self.Series[Var][0:int(self.Erase) + M] = np.nan return -2 elif int(self.Extract[1]) + M > len(self.Series[Var]): self.Series[Var][-int(self.Erase) + M:len(self.Series[Var])] = np.nan return -2 else: self.Series[Var][-int(self.Erase) + M:int(self.Erase) + M] = np.nan q = sum(~np.isnan(Event)) if q < 0.9 * len(Event): return -2 MP = np.where(Event == MVal)[0][0] # Se calculan los cambios Changes = BP.C_Rates_Changes(Event, dt=dt, MP=MP, MaxMin=Oper) if Changes['VRateB'] < self.Limits[Var]['VRateB'] or np.isnan( Changes['VRateB']): return -2 try: VarC = list(Changes) for V in VarC: a = len(self.Changes[Var][V]) self.Changes[Var][V] = np.hstack( (self.Changes[Var][V], Changes[V])) self.SeriesC[Var + 'C'] = np.vstack( (self.SeriesC[Var + 'C'], Event)) except KeyError: self.Changes[Var] = Changes.copy() self.SeriesC[Var + 'C'] = Event.copy() self.M[Var].append(MP) else: return -2 else: # Finding the value MVal = O(self.Series[Var]) M = np.where(self.Series[Var] == MVal)[0][0] # Validation of the data if Comp(MVal, self.Limits[Var]['MV']): # Se extrae el evento Event = self.Series[Var][-int(Extract[0] / dt) + M:int(Extract[1] / dt) + M].copy() if -int(self.Extract[0]) + M < 0: self.Series[Var][0:int(self.Erase) + M] = np.nan return -2 elif int(self.Extract[1]) + M > len(self.Series[Var]): self.Series[Var][-int(self.Erase) + M:len(self.Series[Var])] = np.nan return -2 else: self.Series[Var][-int(self.Erase) + M:int(self.Erase) + M] = np.nan q = sum(~np.isnan(Event)) if q < 0.9 * len(Event): return -2 MP = np.where(Event == MVal)[0][0] # Se calculan los cambios Changes = BP.C_Rates_Changes(Event, dt=dt, MP=MP, MaxMin=Oper) if Changes['VRateB'] < self.Limits[Var]['VRateB'] or np.isnan( Changes['VRateB']): return -2 Dates = self.Series['Dates'].datetime[-int(Extract[0] / dt) + M:int(Extract[1] / dt) + M] try: VarC = list(Changes) for V in VarC: a = len(self.Changes[Var][V]) self.Changes[Var][V] = np.hstack( (self.Changes[Var][V], Changes[V])) self.SeriesC['DatesP'] = np.vstack( (self.SeriesC['DatesP'], Dates)) self.SeriesC['Dates'] = np.vstack( (self.SeriesC['Dates'], self.Series['Dates'].str[-int(Extract[0] / dt) + M:int(Extract[1] / dt) + M])) self.SeriesC[Var + 'C'] = np.vstack( (self.SeriesC[Var + 'C'], Event)) except KeyError: self.Changes[Var] = Changes.copy() self.SeriesC['DatesP'] = self.Series['Dates'].datetime[ -int(Extract[0] / dt) + M:int(Extract[1] / dt) + M] self.SeriesC['Dates'] = self.Series['Dates'].str[ -int(Extract[0] / dt) + M:int(Extract[1] / dt) + M] self.SeriesC[Var + 'C'] = Event.copy() self.MT.append(M) self.M[Var].append(MP) else: return -1 return 1
def LoadStations(self): ''' DESCRIPTION: This method loads the information of the different stations. _________________________________________________________________ INPUT: ''' # ---------------- # Constants # ---------------- lenArch = len(self.SC.Arch) lenData = int((self.DateE-self.DateI).seconds/60) self.DataSt = {} self.PrecCount = {} self.Changes = {} self.IDC = [] dt = 1 # Horas atras TBef = 1 # minutos hacia adelante TAft = 30 Labels = ['FechaC','FechaCP','Prec','Pres_F','T_F','HR_F','W_F','q_F'] LabCh = ['Pres_F','T_F','HR_F'] opers = {'Pres_F':np.nanmin,'T_F':np.nanmax,'HR_F':np.nanmin} MaxMin = {'Pres_F':'min','T_F':'max','HR_F':'min'} self.vmax = {} self.vmin = {} for Lab in Labels[1:]: self.vmax[Lab] = [] self.vmin[Lab] = [] # ---------------- # Extract Data # ---------------- for iar in range(lenArch): self.DataSt[self.ID[iar]] = {} self.SC.LoadData(irow=iar) xi = np.where(self.SC.f['FechaCP'] == self.DateI)[0] xf = np.where(self.SC.f['FechaCP'] == self.DateE)[0] if len(xi) == 0 or len(xf) == 0: for Lab in Labels: self.DataSt[self.ID[iar]][Lab] = np.array(lenData) continue for iLab,Lab in enumerate(Labels): self.DataSt[self.ID[iar]][Lab] = self.SC.f[Lab][xi:xf+1] if iLab > 1: self.vmax[Lab].append(np.nanmax(self.SC.f[Lab][xi:xf+1])) self.vmin[Lab].append(np.nanmin(self.SC.f[Lab][xi:xf+1])) # Changes if np.nanmax(self.DataSt[self.ID[iar]]['Prec']) == 0 or np.isnan(np.nanmax(self.DataSt[self.ID[iar]]['Prec'])): continue xMax = np.where(self.DataSt[self.ID[iar]]['Prec'] == np.nanmax(self.DataSt[self.ID[iar]]['Prec']))[0][0] # Precipitation self.PrecCount[self.ID[iar]] = HyMF.PrecCount(self.DataSt[self.ID[iar]]['Prec'],self.DataSt[self.ID[iar]]['FechaC'],dt=1,M=xMax) try: flag = np.isnan(self.PrecCount[self.ID[iar]]['DatesEvst'][0]) except TypeError: flag = False if flag: continue # Change First xEv = np.where(self.DataSt[self.ID[iar]]['FechaCP'] == self.PrecCount[self.ID[iar]]['DatesEvst'][0])[0][0] Bef = xEv-int(60/dt*TBef) Aft = xEv+int(60/dt*(TAft/60)) self.Changes[self.ID[iar]] = dict() for Var in LabCh: oper = opers[Var] Min = oper(self.DataSt[self.ID[iar]][Var][Bef:Aft]) xMin1 = np.where(self.DataSt[self.ID[iar]][Var][:Aft]==Min)[0][-1] self.Changes[self.ID[iar]][Var] = BP.C_Rates_Changes(self.DataSt[self.ID[iar]][Var],dt=dt, MP=xMin1,MaxMin=MaxMin[Var],flagTop=False) self.IDC.append(self.ID[iar]) self.vmax2 = self.vmax.copy() self.vmin2 = self.vmin.copy() for Lab in Labels[2:]: self.vmax[Lab] = np.nanmax(self.vmax[Lab])+0.1 if Lab == 'Prec': self.vmin[Lab] = np.nanmin(self.vmin[Lab]) else: self.vmin[Lab] = np.nanmin(self.vmin[Lab])-0.1 return