Displaying differences for changeset
 
display as  

src/java/python/weatherExtraction.py

@@ -47,6 +47,7 @@
             'gust': 'm/s',
             'gusttm': 'min into day NOT HH:MM as other max/min',
             'gustdir': 'deg',
+            'et': 'mm',
         },
         'hourly': {
             'tmean': 'C',
@@ -62,6 +63,7 @@
             'gust': 'm/s',
             'gusttm': 'minutes into day',
             'gustdir': 'Degrees',
+            'et': 'mm',
         }
     }
 
@@ -795,9 +797,10 @@
     def WEgetDataNCWCD_live(self, arg_dict):
         stations = arg_dict['station_list']
         climate_data = arg_dict.get('climate_data', ["tmax", "tmin", "pp", "sr", "rhave", "windspeed_avg"])
+        time_type = arg_dict.get('time', 'daily')
         units = arg_dict.get('units', 'metric')
         
-        ncwcd_finder = NCWCDDataFinder(stations, arg_dict['start_date'], arg_dict['end_date'], climate_data)
+        ncwcd_finder = NCWCDDataFinder(stations, arg_dict['start_date'], arg_dict['end_date'], time_type, climate_data)
         data = ncwcd_finder.query()
 
         return self.WEprocessPointData(data, units, 'daily', climate_data)
@@ -851,20 +854,29 @@
                     s = station_data[0][1]
                     e = station_data[-1][1]
 
+                    ishourly = len(s) > 10
+                    tzindex = None
+                    if ishourly:
+                        tzindex = s.rindex('-')
+                        s = s[:tzindex]
+                        e = e[:tzindex]
+
                     if not fmt:
                         fmt = '%Y-%m-%d'
-                        if len(s) > 10:
-                            fmt += ' %H:%M:%S'
-                        
+                        if ishourly:
+                            # timestamp format (ie, 2015-12-30T00:00:00-07:00)
+                            fmt += 'T%H:%M:%S'
+                            
                     if isinstance(s, basestring):
-                        s = datetime.datetime.strptime(s, fmt).date()
-                        e = datetime.datetime.strptime(e, fmt).date()
+                        s = datetime.datetime.strptime(s, fmt)
+                        e = datetime.datetime.strptime(e, fmt)
                     if not start_date:
                         start_date = s
                         end_date = e 
                     else:
                         start_date = min(start_date, s)
                         end_date = max(end_date, e)
+                    break
 
         if start_date:
             header = ['station', 'date',]
@@ -889,9 +901,12 @@
                     # Fill in missing data.
                     missing_row = [None,]*6
                     cur_date = start_date
-                    first_date = datetime.datetime.strptime(station_data[0][1], fmt).date()
+                    date_str = station_data[0][1]
+                    if ishourly:
+                        date_str = date_str[:tzindex]
+                    first_date = datetime.datetime.strptime(date_str, fmt)
                     while cur_date < first_date:
-                        ret.append([station, cur_date.strftime('%Y-%m-%d')] + missing_row)
+                        ret.append([station, cur_date.strftime(fmt)] + missing_row)
                         cur_date += one_day
 
                     for row in station_data:
@@ -899,9 +914,9 @@
                             "station data has wrong number of columns, expected {0}, got {1} for {2} [{3}]".format(len(climate_data)+2, len(row), row, climate_data)
 
                         # Fill in any missing rows
-                        row_date = datetime.datetime.strptime(row[1], fmt).date()
+                        row_date = datetime.datetime.strptime(row[1][:tzindex], fmt)
                         while cur_date < row_date:
-                            ret.append([station, cur_date.strftime('%Y-%m-%d')] + missing_row)
+                            ret.append([station, cur_date.strftime(fmt)] + missing_row)
                             cur_date += one_day
                             
                         for itype in range(len(climate_data)):
@@ -916,7 +931,7 @@
 
                 # Pad at end
                 while cur_date <= end_date:
-                    ret.append([station, cur_date.strftime('%Y-%m-%d')] + missing_row)
+                    ret.append([station, cur_date.strftime(fmt)] + missing_row)
                     cur_date += one_day
         return ret
             
@@ -951,7 +966,7 @@
 
 
 class NCWCDDataFinder(object):
-    def __init__(self, station_ids, start_date, end_date, climate_data):
+    def __init__(self, station_ids, start_date, end_date, time_type, climate_data):
         """ Sets the attributes of the weather station associated with the data finder """
         # NCWCD returns all stations, so just pick the first one.
         if len(station_ids):
@@ -963,6 +978,7 @@
                 self.station_names = station_ids
         self.start_date = start_date
         self.end_date = end_date
+        self.time_type = time_type
         self.climate_data = climate_data
         
     def makeRequest(self, url, query_string = None):
@@ -979,7 +995,7 @@
     def parse(self, response):
         """ Insert any valid weather station data into the database """
         parser = NCWCDParser(self) 
-        return parser.parse(response, self.climate_data)
+        return parser.parse(response, self.climate_data, self.time_type)
     
     def query(self):
         """ Completes a query to find weather data related to the weather station """
@@ -999,7 +1015,7 @@
                               start_date=start_date.strftime("%m/%d/%Y"),
                               end_date=end_date.strftime("%m/%d/%Y"),
                               # 2: daily data
-                              data_type=2,
+                              data_type=2 if self.time_type=='daily' else 1,
                               stations=self.station_ids[0])
         #print 'NWCD URL: ' + url
         response = self.makeRequest(url)
@@ -1013,7 +1029,7 @@
         self.data = None
         self.data_finder = data_finder
 
-    def parse(self, raw_data, climate_data):
+    def parse(self, raw_data, climate_data, time_type):
         root = ET.fromstring(raw_data)
         # Return individual stations
         ret = {}
@@ -1033,7 +1049,9 @@
 
                 name = station_name #entry.findtext('Name')
                 timestamp = entry.findtext('TimeStamp', None)
-                date = datetime.datetime.strptime(timestamp.split('T')[0], "%Y-%m-%d").date()
+                if time_type == 'daily':
+                    # convert to YYYY-MM-DD
+                    timestamp = timestamp.split('T')[0]
                 tmp_air_max = entry.findtext('TemperatureAir_Max', None)
                 tmp_air_max_time = entry.findtext('Time_TemperatureAir_Max', None)
                 tmp_air_min = entry.findtext('TemperatureAir_Min', None)
@@ -1085,7 +1103,7 @@
                 # Return as dictionary
                 choices = {
                     'name': name,
-                    'date': date.strftime("%Y-%m-%d"),
+                    'date': timestamp,
                     'tmp_air_max': tmp_air_max,
                     'tmp_air_max_time': tmp_air_max_time,
                     'tmp_air_min': tmp_air_min,

test/service_tests/NCWCD_V1_0/ncwcd-req.json

@@ -10,7 +10,7 @@
         "value": "metric"
     }, {
         "name": "start_date",
-        "value": "1980-1-1"
+        "value": "2015-2-1"
     }, {
         "name": "end_date",
         "value": "2015-12-31"