import concurrent.futures
import eccodes
import requests
import tempfile
input_files = []
output_file = 'merged.grib'
def download(hour):
hour_str = str(hour).zfill(3)
request = requests.get(
f'https://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_0p25.pl?dir=%2Fgfs.20240922%2F00%2Fatmos&file=gfs.t00z.pgrb2.0p25.f{hour_str}&var_SPFH=on&var_VGRD=on&lev_850_mb=on&lev_15_mb=on&lev_10_mb=on&lev_tropopause=on&subregion=&toplat=42&leftlon=25&rightlon=60&bottomlat=12')
if request.status_code == 200:
input_files.append(request.content)
print(f"Successfully downloaded data for hour {hour_str}")
else:
print(f"Failed to download data for hour {hour_str}, status code: {request.status_code}")
hours_1h = list(range(1, 121))
hours_3h = list(range(120, 385, 3))
hours = hours_1h + hours_3h
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor:
futures = [executor.submit(download, hour) for hour in hours]
def merge_grib_files(input_data, output_file):
with open(output_file, 'wb') as outfile:
for data in input_data:
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
temp_file.write(data)
temp_file.flush()
temp_file.seek(0)
with open(temp_file.name , 'rb') as infile:
while (gid := eccodes.codes_grib_new_from_file(infile)):
eccodes.codes_write(gid, outfile)
eccodes.codes_release(gid)
merge_grib_files(input_files, output_file)
this is the code for downloading and merging the data into one grib file, however I could not figure out any implementation before the merging to create interpolated data between missing time steps