You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import xarray as xr
import numpy as np
file_path = '/home/ubuntu/DATA/nam20250105.t00z.awphys00.tm00.grib2'
# Open the GRIB2 file using the cfgrib engine
try:
ds = xr.open_dataset(file_path, engine='cfgrib')
# Print the dataset to see all dimensions, coordinates, and attributes
print(ds)
# Access specific properties/attributes
# Example: Global attributes
print(f"\nGlobal attributes: {ds.attrs}")
print(f"File history: {ds.attrs.get('history')}")
# Access specific variables and their attributes
# The variable name will depend on your GRIB file content (e.g., 't2m' for 2m temperature)
# You can see variable names in the print(ds) output
variable_name = 't2m' # Example variable name
if variable_name in ds.variables:
print(f"\nProperties of variable '{variable_name}': {ds[variable_name].attrs}")
print(f"Units of {variable_name}: {ds[variable_name].attrs.get('units')}")
print(f"Long name of {variable_name}: {ds[variable_name].attrs.get('long_name')}")
# Access coordinates (lat/lon)
lats = ds[variable_name].latitude.values
lons = ds[variable_name].longitude.values
print(f"\nLatitude range: {np.min(lats)} to {np.max(lats)}")
print(f"Longitude range: {np.min(lons)} to {np.max(lons)}")
except Exception as e:
print(f"Error reading GRIB file: {e}")
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to read a NAM file with the code:
However, it returns the error:
Any suggestion on what I might be doing wrong. Thanks.
Beta Was this translation helpful? Give feedback.
All reactions