def extract_wx_data(time_obj, wx_path): """ This function was writen to reenstate wx file parsing for tha greed upon NOAA weather data format for any study area within the USA. This is THE function that should be used for reading weather data, the others will not be supported. It expects data in the format as retrieved from this URL: [http://gis.ncdc.noaa.gov/map/viewer/#app=cdo&cfg=cdo&theme=hourly&layers=1&node=gi] Please see the readme for more detailed instructions on data download. Inputs: time_obj A datetime object representing the image data aquisition datetime wx_path filepath to the weather data. (hourly data) Returns: an array with specific ordering of climate variables. """ # format weather (daily and hourly) as a time series object wx = time_series.time_series("wx_data") tdo = textio.read_DS3505(wx_path, has_headers = True) wx.from_tdo(tdo) time_lable = "YR--MODAHRMN" time_format = "%Y%m%d%H%M" start_time = "200001010000" wx.define_time(time_lable, time_format, start_time) # bin the data into days pull out the one we want. wx.make_subsets("%j", cust_center_time = time_obj) day_name = time_obj.strftime("%Y-%m-%d") wx.interrogate() # if it cant find a subset in wx with the input dates name, wx data is for wrong time. try: wx_day = wx[day_name] except: raise Exception("wx data has no entries for date of landsat acquisition ({0})".format(time_obj)) # get min/max temperatures and convert to Celcius (statistical operations clean up NoData) print("Centered statistics around {0}".format(wx_day.center_time)) Tstats = wx_day.column_stats("TEMP") temp_C_min = (Tstats["TEMP_min_v"] - 32) * (5.0/9) # F --> C temp_C_max = (Tstats["TEMP_max_v"] - 32) * (5.0/9) # F --> C # get instantaneous variables at input @param time_obj by interpolating between nearest values temp_C_mid = (wx_day.interp_col(time_obj, "TEMP") - 32) * (5.0/9) # F --> C P_air = wx_day.interp_col(time_obj, "STP" ) # in millibars wind_speed = wx_day.interp_col(time_obj, "SPD" ) * 0.51444 # knots --> meters / second dewp_C = (wx_day.interp_col(time_obj, "DEWP") - 32) * (5.0/9) # F --> C # this format is for legacy support, just an array of values, not a dict. print("Temperature is {0}C".format(temp_C_mid)) return [temp_C_min, temp_C_max, temp_C_mid, P_air, wind_speed, dewp_C]
padded_name.ljust(28, " "), str(len(self.time)).ljust(5," "), self.time_dom[0], self.time_dom[-1])) if self.subsetted: for subset in self.subsets: subset.interrogate() return # testing code if __name__ == "__main__": filepath = r"test_data\weather_dat.txt" # define filepath with text data tdo = textio.read_DS3505(filepath) # build a "text data object" (tdo) print(tdo.headers) # print the headers print(tdo.row_data[0]) # print the first row ts = time_series('weather_data') # initialize a time series named "weather_data" ts.from_tdo(tdo) # populate it with the contents of the tdo print ts.headers # view the headers of this time series timecol = "YR--MODAHRMN" # identify the column with time information in it fmt = "%Y%m%d%H%M" # specify the format of strings in this column ts.define_time(timecol, fmt) # converts text data into datetimes ts.interrogate() # print a heads up summary of the time series
print("{0} \t {1} \t {2} \t {3}".format( padded_name.ljust(28, " "), str(len(self.time)).ljust(5, " "), self.time_dom[0], self.time_dom[-1])) if self.subsetted: for subset in self.subsets: subset.interrogate() return # testing code if __name__ == "__main__": filepath = r"test_data\weather_dat.txt" # define filepath with text data tdo = textio.read_DS3505(filepath) # build a "text data object" (tdo) print(tdo.headers) # print the headers print(tdo.row_data[0]) # print the first row ts = time_series( 'weather_data') # initialize a time series named "weather_data" ts.from_tdo(tdo) # populate it with the contents of the tdo print ts.headers # view the headers of this time series timecol = "YR--MODAHRMN" # identify the column with time information in it fmt = "%Y%m%d%H%M" # specify the format of strings in this column ts.define_time(timecol, fmt) # converts text data into datetimes ts.interrogate() # print a heads up summary of the time series