def main(argv): try: job = GetWeatherDataJob() lat = float(FLAGS.latitude) lon = float(FLAGS.longitude) UtilFunctions.validate_lat_lon(lat, lon) # get weather data job.get_weather_data(start_date=FLAGS.start_date, end_date=FLAGS.end_date, lat=float(FLAGS.latitude), lon=float(FLAGS.longitude)) except Exception as err: if FLAGS.job_status_blob_sas_url: writer = JobStatusWriter(FLAGS.job_status_blob_sas_url) writer.set_success(False) writer.flush() JobError.write_to_status_file(err, FLAGS.job_status_blob_sas_url) sys.exit(1)
def __get_weather_forecast_data_for_day(self, day, lat, lon): ''' Gets weather data for a given day and pushes it to eventhub ''' try: # get data for given date range. start_time = time.time() LOG.info("Getting data for " + day.strftime("%m/%d/%Y, %H:%M:%S")) weather_data = NoaaGfsWeather(day, day) LOG.info("Successfully got data for " + day.strftime("%m/%d/%Y, %H:%M:%S")) # get the data into a pandas data frame, so we can filter and process weather_data_df = weather_data.to_pandas_dataframe() LOG.info("Took {} seconds to get the data.".format(time.time() - start_time)) # out of the lat longs available get the nearest points LOG.info( "Finding the nearest latitude and longitude from the available data" ) (nearest_lat, nearest_lon) = UtilFunctions.find_nearest_lat_longs_in_data( weather_data_df, lat, lon) LOG.info("nearest lat, lon: [" + str(nearest_lat) + "," + str(nearest_lon) + "]") # filter the data to this lat and lon LOG.info("Filtering the data to nearest lat, lon") filtered_weather_data = weather_data_df[ (weather_data_df['latitude'] == nearest_lat) & (weather_data_df['longitude'] == nearest_lon)] LOG.info(filtered_weather_data) # push the data to eventhub LOG.info("Pushing data to eventhub") wdl_id = self.__push_weather_data_to_farmbeats( filtered_weather_data) LOG.info("Successfully pushed data") # Update the status for the job if FLAGS.job_status_blob_sas_url: msg = "Weather data pushed for start_date: {} to end_date: {}\n for nearest_lat: {}, nearest_lon: {}\n provided lat:{}, lon:{}".format( FLAGS.start_date, FLAGS.end_date, nearest_lat, nearest_lon, FLAGS.latitude, FLAGS.longitude) writer = JobStatusWriter(FLAGS.job_status_blob_sas_url) output_writer = writer.get_output_writer() output_writer.set_prop("WeatherDataLocationId: ", wdl_id) output_writer.set_prop("Message: ", msg) writer.set_success(True) writer.flush() except Exception as err: # Update the status in failure if FLAGS.job_status_blob_sas_url: writer = JobStatusWriter(FLAGS.job_status_blob_sas_url) writer.set_success(False) writer.flush() raise JobError(str(err), JobConstants.INTERNAL_ERROR, False)
def validate_lat_lon(lat, lon): ''' validates, if provided latitude and longitude are in range. ''' if (lat > 90.0 or lat < -90.0 or lon > 180.0 or lon < -180.0): raise JobError( "Bad request: Please ensure that latitude[-90, 90]/longitude[-180, 180]", '400', False)
def __get_weather_data_model_id(self, name): ''' returns the weather data model id, given the name ''' wsms = self.fb_api.get_weather_data_model_api().weather_data_model_get_all(names=[name], includes=[JobConstants.WEATHER_MEASURES, JobConstants.PROPERTIES]).to_dict() if (wsms): return wsms[JobConstants.ITEMS][0][JobConstants.ID] else: raise JobError("weather data model {} not found!".format(wsms), JobConstants.INTERNAL_ERROR, False)
def daterange(start_date, end_date): ''' Generator to give one day at a time. ''' if (start_date > end_date): raise JobError( "Bad request: Please ensure that start_date is prior to end_date", '400', False) for n in range(int((end_date - start_date).days) + 1): yield start_date + timedelta(n)