#๐Ÿ”’ Grib time interpolation for gfs forecast system

5 messages ยท Page 1 of 1 (latest)

languid bluff
#

Hi, i'm using the global forecast system(gfs) grib data, the model run has 120 hourly steps first, then transitions to 3 hour steps until 384, What I want to do is to linear interpolate the missing values between the 3 hours,
so pseduocode

pygrib open(file)

get data of time 120, 123 for example

interpolate 121,122

im not the best at the interpolation part and it has been quite challenging especially:
outputting the interpolation file the same structure as original dataset, including atmospheric layers for each variable, xarray is quite disasterous when handling layers, can anyone help?

round basinBOT
#

@languid bluff

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

languid bluff
#
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

round basinBOT
#

@languid bluff

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.