def download_forecast(forecastTime, grid, h, forecastHour, path): forecastTime = forecastTime[0:4 + 2 + 2] + "%2F" + forecastTime[-2:] url = "https://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_" + grid + ".pl?file=gfs.t" + ( "%02d" % forecastHour ) + "z.pgrb2." + grid + ".f" + ("%03d" % h) + "&" + GfsData( ).g_grib_url_levels + "&" + GfsData( ).g_grib_url_meteovars + "&leftlon=0&rightlon=360&toplat=90&bottomlat=-90&dir=%2Fgfs." + forecastTime Verbose.print_text(1, url) Forecast.__download(url, path)
def meteoParams(): meteoParamsPrecipitation = [[(h, ) + p for p in GfsData().parameters_humidity] for h in [6, 12, 18]] meteoParamsOther = [[(h, ) + p for p in GfsData().parameters_other] for h in [6, 12, 18]] meteoParamsWind = [[(h, ) + p for p in GfsData().parameters_wind] for h in [6, 12, 18]] return meteoParamsOther, meteoParamsWind, meteoParamsPrecipitation
def compute_cells_forecasts(models_directory, problem_formulation, meteo_matrix): Verbose.print_arguments() predict = Predict(models_directory, ModelType.CELLS, problem_formulation) predict.set_meteo_data(meteo_matrix, GfsData().parameters_vector_all) predict.set_trained_cells() predict.trainedModel.load_all_weights() predict.set_prediction_population() NN_X = predict.get_X(range(meteo_matrix.shape[0])) return predict.trainedModel.model.predict(NN_X)
def readWeatherData(meteoFiles, crops): global g_distinct_latitudes global g_distinct_longitudes # ====================================================================================== # Retrieve grid info # # inputs: # - meteoFiles[0] # outputs: # - g_distinct_latitudes # - g_distinct_longitudes # ====================================================================================== if g_distinct_latitudes.size == 0 or g_distinct_longitudes.size == 0: validDate, g_distinct_latitudes, g_distinct_longitudes = GribReader(meteoFiles[0]).getInfos() # Convert longitudes for i in range(g_distinct_longitudes.shape[0]): g_distinct_longitudes[i] = Utils.convert_longitude(g_distinct_longitudes[i]) # ====================================================================================== # Read grib files into a single matrix # # params: # - crops # inputs: # - meteoFiles # - parameters_vector_all # outputs: # - meteo_matrix # ====================================================================================== meteo_matrix = ForecastData.get_meteo_array_of_day(meteoFiles, GfsData().parameters_vector_all, crops).transpose() return g_distinct_latitudes, g_distinct_longitudes, meteo_matrix
def __compute_spots_forecasts(self, models_directory, problem_formulation, lats, lons, meteo_matrix, filename): Verbose.print_arguments() predict = Predict(models_directory, ModelType.SPOTS, problem_formulation) predict.set_meteo_data(meteo_matrix, GfsData().parameters_vector_all) #============================================================= # depend de GribReader.get_values_array(self, params, crops): forecastCellsLine = {} line = 0 for crop in self.crops: for iLat in range(crop[0], crop[1]): for iLon in range(crop[2], crop[3]): forecastCellsLine[(iLat, iLon)] = line line += 1 #============================================================= # Compute or load cells_and_spots #============================================================= filename_cells_and_spots = "Forecast_cellsAndSpots_" + "_".join( [str(crop[d]) for crop in self.crops for d in range(4)]) if not BinObj.exists(filename_cells_and_spots): Verbose.print_text( 0, "Generating precomputation file because of new crop, it may crash on the server... To be computed on my computer" ) cells_and_spots = {} # find forecast cell for each spot # C'est comme le cellsAndSpots de Train sauf que les cellules sont les cells de forecast (32942 cells) spots_data = SpotsData() spots = spots_data.getSpots(range(80)) for kc, cell_spots in enumerate(spots): for ks, spot in enumerate(cell_spots): iCell = (np.abs(lats - spot.lat).argmin(), np.abs(lons - spot.lon).argmin()) cellLine = forecastCellsLine[iCell] kcks_spot = ( (kc, ks), spot.toDict()) # (training cell, ks) if not cellLine in cells_and_spots: cells_and_spots[cellLine] = [kcks_spot] else: cells_and_spots[cellLine] += [kcks_spot] BinObj.save(cells_and_spots, filename_cells_and_spots) else: cells_and_spots = BinObj.load(filename_cells_and_spots) #============================================================= # Create a model with 1 cell of 1 spot #============================================================= predict.set_trained_spots() #============================================================= # Compute prediction for each spot, one by one #============================================================= spots_and_prediction = [] for kcslst, cslst in enumerate(cells_and_spots.items()): meteoLine, spotsLst = cslst for cs in spotsLst: modelContent = ModelContent() modelContent.add(cs[0][0], cs[0][1]) predict.trainedModel.load_all_weights( modelContent) # TODO do not reload shared weights predict.set_prediction_population() NN_X = predict.get_X([meteoLine]) prediction = predict.trainedModel.model.predict(NN_X)[0][0] spots_and_prediction += [ Spot((cs[1]['name'], cs[1]['lat'], cs[1]['lon']), cs[1]['id'], cs[1]['nbFlights'], prediction) ] #[cs[1]] #============================================================= # Export all results #============================================================= Forecast.__export_spots_forecasts(spots_and_prediction, filename)
def compute_prediction_file_cells( cells_forecasts, prediction_filename_for_tiler, distinct_latitudes, distinct_longitudes, meteo_matrix, crops, meteo_params, grid_desc_predictions, export_for_tiler=True): Verbose.print_arguments() # ====================================================================================== # Retrieve geopotential height at 12h indices in meteoParams # # inputs: # - meteoParams # - parameters_geopotential_height # outputs: # - geopotential_height_indices # ====================================================================================== geopotential_height_indices = [] for geopotential_height_param in GfsData().parameters_geopotential_height: for kParam, param in enumerate(meteo_params): if param == (12,) + geopotential_height_param: geopotential_height_indices += [kParam] break assert (len(geopotential_height_indices) == len(GfsData().parameters_geopotential_height)) # ====================================================================================== # Retrieve wind at 12h # # inputs: # - meteoParams # - parameters_wind # outputs: # - wind_indices # ====================================================================================== wind_indices = [] for wind_param in GfsData().parameters_wind: for kParam, param in enumerate(meteo_params): if param == (12,) + wind_param: wind_indices += [kParam] break assert (len(wind_indices) == len(GfsData().parameters_wind)) # ======================================================================== # Create and fill prediction grid # # params: # - grid_desc_predictions # inputs: # - distinct_latitudes # - distinct_longitudes # - meteo_matrix # - geopotential_height_indices # - predictions # outputs: # - prediction_grid # ======================================================================== prediction_grid = GridLatLon(grid_desc_predictions[0], grid_desc_predictions[1], grid_desc_predictions[2], grid_desc_predictions[3]) p = 0 for crop in crops: # number of data rectangles to read in the GRIB grid lat_range = (crop[0], crop[1]) lon_range = (crop[2], crop[3]) Verbose.print_text(1, "lats: " + str(distinct_latitudes[lat_range[0]]) +" , "+ str(distinct_latitudes[lat_range[1]-1])) Verbose.print_text(1, "lons: " + str(distinct_longitudes[lon_range[0]]) +" , "+ str(distinct_longitudes[lon_range[1]-1])) for ilat in range(lat_range[0], lat_range[1]): for ilon in range(lon_range[0], lon_range[1]): predictions_data_sample = () # Add geopential heights (not predicted, simply meteo variables) predictions_data_sample += (meteo_matrix[p, geopotential_height_indices[0]], # 0 meteo_matrix[p, geopotential_height_indices[1]], # 1 meteo_matrix[p, geopotential_height_indices[2]], # 2 meteo_matrix[p, geopotential_height_indices[3]], # 3 meteo_matrix[p, geopotential_height_indices[4]]) # 4 predictions_data_sample += (cells_forecasts[0][p,0,0], # flyability at 1000 # 5 cells_forecasts[0][p,0,1], # flyability at 900 # 6 cells_forecasts[0][p,0,2], # flyability at 800 # 7 cells_forecasts[0][p,0,3], # flyability at 700 # 8 cells_forecasts[0][p,0,4], # flyability at 600 # 9 np.mean(cells_forecasts[1][p,0,0:5]),# crossability # 10 cells_forecasts[2][p,0,0], # wind at 1000 # 11 cells_forecasts[2][p,0,1], # wind at 900 # 12 cells_forecasts[2][p,0,2], # wind at 800 # 13 cells_forecasts[2][p,0,3], # wind at 700 # 14 cells_forecasts[2][p,0,4], # wind at 600 # 15 np.mean(cells_forecasts[3][p,0,0:5]), # humidity # 16 0.0) # Attractiveness # 17 predictions_data_sample += (meteo_matrix[p, wind_indices[0 + 0]], # U 1000 12h # 18 meteo_matrix[p, wind_indices[2 + 0]], # U 900 12h # 19 meteo_matrix[p, wind_indices[4 + 0]], # U 800 12h # 20 meteo_matrix[p, wind_indices[6 + 0]], # U 700 12h # 21 meteo_matrix[p, wind_indices[8 + 0]], # U 600 12h # 22 meteo_matrix[p, wind_indices[0 + 1]], # V 1000 12h # 23 meteo_matrix[p, wind_indices[2 + 1]], # V 900 12h # 24 meteo_matrix[p, wind_indices[4 + 1]], # V 800 12h # 25 meteo_matrix[p, wind_indices[6 + 1]], # V 700 12h # 26 meteo_matrix[p, wind_indices[8 + 1]]) # V 600 12h # 27 # Push the sample prediction_grid.add(distinct_latitudes[ilat], distinct_longitudes[ilon], predictions_data_sample) p += 1 # ======================================================================== # Export predictions for tiler # # params: # - predictionFilenameForTiler # inputs: # - prediction_grid # ======================================================================== accessors = [lambda x, ix=ixVal: sum([xx[ix] for xx in x]) / len(x) for ixVal in range(len(predictions_data_sample))] if export_for_tiler: prediction_grid.export_data_for_tiler(prediction_filename_for_tiler, accessors) else: prediction_grid.export_json(prediction_filename_for_tiler, accessors)