1 year ago
#119291
arkipelago
How to properly parse NOAA GFS Grib2 files using GPS Coordinates?
I'm currently trying to download GFS files from NOAA to get irradiance forecasts. I've done this same process for NOAA's HRRR files, but for some reason, parsing out GFS files isn't giving me the proper result.
For a given point on the West coast of the United States, I'm getting positive values for irradiance way past sunset for this time of year (around 5 pm local time), which I'm not expecting.
I understand that the GFS forecasts from NOAA in particular have a different convention for lat-long coordinates, so I'm wondering if that is the issue here. Minimal reproducible code is below.
import math
import numpy as np
import xarray as xr
import pandas as pd
from datetime import datetime, timedelta, date
import time
import pytz
latitude = 46.9 # latitude of point
longitude = -118.6 % 360 # longitude of point when converting to 0-360 scale used by NOAA GFS. This gives a value of 241.4
# Function for finding the NOAA coordinates nearest to the site's coordinates
def find_nearest(array,value):
idx = np.searchsorted(array, value, side="left")
if idx > 0 and (idx == len(array) or math.fabs(value - array[idx-1]) < math.fabs(value - array[idx])):
return float(array[idx-1])
else:
return float(array[idx])
variables = ['dswrfsfc'] # irradiance
GFS_URL = f'http://nomads.ncep.noaa.gov/dods/gfs_0p25/gfs20220131/gfs_0p25_00z' # GFS run for 1/31/22
dataset = xr.open_dataset(GFS_URL)[variables]
# Finding the nearest lat long coordinates for this point
GFS_latitude = find_nearest(dataset.lat,latitude)
GFS_longitude = find_nearest(dataset.lon,longitude)
# Parsing dataset for specific lat long coordinates and converting to pandas dataframe
parsed_dataset = dataset.sel(lon=GFS_longitude,lat=GFS_latitude).to_dataframe()
# localizing timezone since timestamps from NOAA are in UTC
local_tz = pytz.timezone('US/Pacific')
parsed_dataset.index = parsed_dataset.index.tz_localize('UTC')
parsed_dataset.index = parsed_dataset.index.tz_convert(local_tz)
parsed_dataset.head(12)
python
weather
noaa
gfs
weatherdata
0 Answers
Your Answer