changeset 447:7ef40014236c

updated weather indicator calculations
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 04 Feb 2014 15:00:39 -0500
parents a65a14c90834
children 0b15fdeb903e
files python/pavement.py
diffstat 1 files changed, 22 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/python/pavement.py	Tue Feb 04 02:13:27 2014 -0500
+++ b/python/pavement.py	Tue Feb 04 15:00:39 2014 -0500
@@ -119,16 +119,21 @@
 
     return (somme_eau, somme_neige, neigeMTQ_sup_seuil, somme_abrasif, somme_sel, somme_lc, somme_lrg, somme_lrd, premiere_neige, autres_abrasifs)
 
-def ecWeatherIndicators(data, startDate, endDate, snowThreshold):
+def ecWeatherIndicators(data, startDate, endDate, snowThreshold, minProportionMeasures = 0.):
     '''Computes the indicators from Environment Canada files
     (loaded as a recarray using csv2rec in data),
-    between start and end dates (datetime.datetime objects)'''
+    between start and end dates (datetime.datetime objects)
+
+    minProportionMeasures is proportion of measures necessary to consider the indicators'''
+    from matplotlib.mlab import find
     nbre_jours_T_negatif,nbre_jours_gel_degel,pluie_tot,neige_tot,ecart_type_T = 0,0,0,0,0
     compteur,nbre_jours_gel_consecutifs=0,0
     tmoys = []
     seuils_T = [20,15,10,5]
     deltas_T = [0,0,0,0]
-    for i in range(int((endDate - startDate).days)+1):
+    startIndex = find(data['date'] == startDate)
+    nDays = (endDate - startDate).days+1
+    for i in range(startIndex, startIndex+int(nDays)):
         if data['tmax'][i] != '' and data['tmax'][i] != None:
             tmax = float(data['tmax'][i].replace(',','.'))
         else:
@@ -159,14 +164,15 @@
             else:
                 compteur = 0
         nbre_jours_gel_consecutifs = max(nbre_jours_gel_consecutifs,compteur)
-    ecart_type_T = np.std(tmoys)
-    if neige_tot < snowThreshold:
-        neigeEC_sup_seuil = 0
+    if float(len(tmoys))/nDays >= minProportionMeasures:
+        ecart_type_T = np.std(tmoys)
+        if neige_tot < snowThreshold:
+            neigeEC_sup_seuil = 0
+        else:
+            neigeEC_sup_seuil = 1
+        return (nbre_jours_T_negatif,nbre_jours_gel_degel, deltas_T, nbre_jours_gel_consecutifs, pluie_tot, neige_tot, neigeEC_sup_seuil, ecart_type_T)
     else:
-        neigeEC_sup_seuil = 1
-
-    return (nbre_jours_T_negatif,nbre_jours_gel_degel, deltas_T, nbre_jours_gel_consecutifs, pluie_tot, neige_tot, neigeEC_sup_seuil, ecart_type_T)
-
+        return [None]*2+[[None]*len(seuils_T)]+[None]*5
 
 class RTSS:
     'class for data related to a RTSS, including agregating pavement marking measurements'
@@ -191,7 +197,7 @@
         return int(self.id[:2])
 
     def getTestAttributes(self):
-        return [self.paintingType, self.color]
+        return [self.paintingType, self.color, self.paintingDate.year]
 
     def plot(self, measure, options = 'o', dayRatio = 1., **kwargs):
         from matplotlib.pyplot import plot
@@ -199,15 +205,14 @@
              self.data[measure], options, **kwargs)
 
     def getMarkingMeasures(self, dataLabel):
-        from numpy import isnan
-        nonZeroIndices = ~isnan(self.data[dataLabel])
+        nonZeroIndices = ~np.isnan(self.data[dataLabel])
         return self.data[nonZeroIndices]['jours'], self.data[nonZeroIndices][dataLabel]
 
     def plotMarkingMeasures(self, measure, options = 'o', dayRatio = 1., **kwargs):
         for i in range(1,7):
             self.plot('{}_{}'.format(measure, i), options, dayRatio, **kwargs)
 
-    def computeMarkingMeasureVariations(self, dataLabel, lanePositions, weatherData, snowThreshold):
+    def computeMarkingMeasureVariations(self, dataLabel, lanePositions, weatherData, snowThreshold, minProportionMeasures = 0.):
         '''Computes for each successive measurement
         lanePositions = None
         measure variation, initial measure, time duration, weather indicators
@@ -217,14 +222,13 @@
         measure variation, initial measure, time duration, lane position1, weather indicators
         measure variation, initial measure, time duration, lane position2, weather indicators
         ...'''
-        from numpy import isnan
         variationData = []
         if lanePositions == None:
-            nonZeroIndices = ~isnan(self.data[dataLabel])
+            nonZeroIndices = ~np.isnan(self.data[dataLabel])
             days = self.data[nonZeroIndices]['jours']
             dates = self.data[nonZeroIndices]['date_mesure']
             measures = self.data[nonZeroIndices][dataLabel]
             for i in range(1, len(dates)):
-                nDaysTNegative, nDaysThawFreeze, deltaTemp, nConsecutiveFrozenDays, totalRain, totalSnow, snowAboveThreshold, stdevTemp = ecWeatherIndicators(weatherData, dates[i-1], dates[i], snowThreshold)
-                variationData.append([measures[i-1]-measures[i], measures[i-1], days[i]-days[i-1], nDaysTNegative, nDaysThawFreeze] + deltaTemp + [nConsecutiveFrozenDays, totalRain, totalSnow, snowAboveThreshold, stdevTemp])
+                nDaysTNegative, nDaysThawFreeze, deltaTemp, nConsecutiveFrozenDays, totalRain, totalSnow, snowAboveThreshold, stdevTemp = ecWeatherIndicators(weatherData, dates[i-1], dates[i], snowThreshold, minProportionMeasures)
+                variationData.append([measures[i-1]-measures[i], measures[i-1], days[i]-days[i-1], days[i-1], nDaysTNegative, nDaysThawFreeze] + deltaTemp + [nConsecutiveFrozenDays, totalRain, totalSnow, snowAboveThreshold, stdevTemp])
         return variationData