ludmila gomez saez
ludmila gomez saez

Reputation: 1

Issue Reading GRIB2 Files with Polar Stereographic Projection in Python

I'm working with GRIB2 files from the GFS Wave Model and I have encountered an issue when loading and reading the data using the xarray and cfgrib libraries. I've been able to read several files without difficulty, but the files for the product gfswave.t00z.arctic.9km.f*.grib2 use polar stereographic projection coordinates, which leads to the following error: GeocalculusError: Problem with calculation of geographic attributes.

How can I read this type of files? I work in Windows with Python 3.10.

Python code to read grib data and join all timestemp into a netcdf file :

import xarray as xr 
import os 
import numpy as np 
import pandas as pd 
import glob 
import cfgrib

pattern_name = ['gfswave.t00z.global.0p25.f*.grib2' , 'gfswave.t00z.arctic.9km.f*.grib2' , 'gfswave.t00z.gsouth.0p25.f*.grib2', 'gfswave.t00z.global.0p16.f*.grib2' ] 

for pattern in pattern_name: 
    search_pattern = os.path.join(directory , pattern)
    grid2_files= glob.glob(search_pattern)
    grid2_files.sort()

    datasets = []
    for i, file in enumerate(grid2_files):
        ds = xr.open_dataset(file, engine='cfgrib') 

        initial_time = ds['time'].values #initial time of forecast

        hour = int(file.split('.')[-2][1:])  # hour or step

        time_value = initial_time + np.timedelta64(hour, 'h')#add hour to initialtime

        ds = ds.assign_coords(time=time_value) #asing value to time variable

        datasets.append(ds)

    concatenated_ds = xr.concat(datasets, dim='time') #concatenated all files

    encoding = {var: {"zlib": True, "complevel": 4} for var in concatenated_ds.data_vars} # reduce NetCDF

    output_file = os.path.join(path_save, pattern[:-9] + '.nc')

    concatenated_ds.to_netcdf(output_file, encoding=encoding) #save 

Upvotes: 0

Views: 18

Answers (0)

Related Questions