2017-11-13 1 views
0

Ich versuche, einen Wert zu berechnen, der eine Bedingung zu der Tageszeit gibt. Die Vektoren/Reihen sind die Netto-Sonnenstrahlung für eine gegebene Stunde und die Zeit, zu der die Daten gesammelt wurden. Wenn der Wert während des Tageslichts erhalten wurde, muss dieser Wert mit 0,1 multipliziert werden, und wenn der Wert in der Nacht erhalten wurde, multipliziere ich den Wert mit 0,5. Die Tagesstunden ändern sich merklich Monat für Monat (Woche für Woche) wie das Tageslicht Wörterbuch unten zeigt:Python Boolesche Maske mit einem Wörterbuch

Ich versuche eine boolesche Maske (withindaylight) zu erstellen, die mir helfen würde, die Berechnung/Multiplikation anzuwenden OHNE eine for-Schleife über jeden Wert in der ‚Zeit‘ Vektor verwendet und es gegen Tageslicht Wörterbuch überprüft, welche, was ich momentan mache:

def Ghr(time, Rn): 
    #soil heatflux cal 
    #time is a single vale of the time vector 
    mon = time.strftime('%b') #strips the month 

    #sunrise -sunset hours from 1st of the month (sr,ss) to end of the month 
    #Jan-1st SR 8:17, Jan-31st SR 07:47 
    #Jan-1st SS 16:03, Jan-31st SS 16:52 

    daylight = {'Jan':('08:00', '16:25'), 
      'Feb':('07:20', '17:20'), 
      'Mar':('06:45', '18:40'), 
      'Apr':('06:05', '20:05'), 
      'May':('05:10', '20:55'), 
      'Jun':('04:50', '21:25'), 
      'Jul':('05:10', '21:15'), 
      'Aug':('05:50', '20:30'), 
      'Sep':('06:45', '19:25'), 
      'Oct':('07:00', '17:30'), 
      'Nov':('07:25', '16:15'), 
      'Dec':('08:05', '16:00')} 

    #strips the hour and minute from the daylight dictionary 
    #then withindaylight is the boolean after checking the 
    #time the data was collected against the these stripped values 

    daybegin = dt.strptime(daylight[mon][0], '%H:%M').time() 
    dayend= dt.strptime(daylight[mon][1], '%H:%M').time() 

    withindaylight = daybegin <= time.time() <= dayend 


    #I want to apply the boolean mask such that it produces the following, 
    #but returns a vector and not just a single value 

    if withindaylight: 
     return .1*Rn #I want to return a series and not just a single value 

    else: 
     return .5*Rn 

Antwort

0

es gab einige Dinge, die mit meinem Code geschehen benötigt:

  1. Wie m ake sicher, dass jede Instanz von meiner ‚Zeit‘ wurde als der Schlüssel aus dem ‚Tageslicht‘, um eine korrekte Monat abgebildet ...

  2. Wie das Tupel in eine separate Liste

  3. Umwandlung des Tupels Halte extrahieren die Zeichenfolge in ein Zeitobjekt in einer pd.Series

  4. zu einer Maske Umwandlung und die Vermehrung der Anwendung

ich diese Probleme mit dem folgenden Code gelöst:

def Ghr(time, Rn): 
    """Soil Heat flux calculator""" 

    #sunrise -sunset hours from 1st of the month (sr,ss) to end of the month 
    #Jan-1st SR 8:17, Jan-31st SR 07:47 
    #Jan-1st SS 16:03, Jan-31st SS 16:52 

    daylight = {'Jan':('08:00', '16:25'), 
      'Feb':('07:20', '17:20'), 
      'Mar':('06:45', '18:40'), 
      'Apr':('06:05', '20:05'), 
      'May':('05:10', '20:55'), 
      'Jun':('04:50', '21:25'), 
      'Jul':('05:10', '21:15'), 
      'Aug':('05:50', '20:30'), 
      'Sep':('06:45', '19:25'), 
      'Oct':('07:00', '17:30'), 
      'Nov':('07:25', '16:15'), 
      'Dec':('08:05', '16:00')} 

    #this maps the month of 'time' as to the dictionary daylight 
    #then unzips the tuple to daystart and dayend lists which 
    #are then converted to pandas.Series objects for use in boolean mask 
    daystart, dayend = zip(*time.dt.strftime('%b').map(daylight)) 

    #dt.strftime extracts the 'Mon' ('%b') from the DateTime object and 
    #maps it to the dictionary and saves the morning and dusk hours respectively 

    #conversion to pandas series 
    daystart=pd.Series(pd.to_datetime(daystart).time) 
    dayend=pd.Series(pd.to_datetime(dayend).time) 
    t=pd.Series(time.dt.time) 
    #the above converts the strings to datetime objects and saves it 
    #to a pandas Series, t is just a to_Series conversion 


    light_mask = t.between(daystart, dayend) 
    #this mask will be used to designate which values get multiplied by 
    #either .1 or .5 


    #True values get multiplied by .1 (The inversion is because 
    #pd.Series.where() only changes the values which are False) 
    Ghr = Rn.where(np.invert(light_mask), Rn*.1) 

    #False values multiplied by *.5 
    Ghr = Rn.where(light_mask, Ghr*.5) 

    return Ghr 
Verwandte Themen