Exemple #1
0
    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
Exemple #2
0
    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