ValueError: Could not convert tuple of form (dims, data[, attrs, encoding]): #ISSUE #GRIP2 #Weather-Data #cfgrib #xarrary #Python #

3.3k views Asked by At

ValueError: Could not convert tuple of form (dims, data[, attrs, encoding]):

I have a problem building this dataset and I couldn't know how to solve it, I try many solutions and it gives me the same error

import cfgrib
import glob
import xarray as xr
import pandas as pd 


folder = input('Folder Path:')
val = input('Variable Name:')

files = glob.glob(rf'{folder}/*.grib2')

data_conc=[]

for file in files:

     data = xr.open_dataset(file,engine='cfgrib',
     backend_kwargs={'filter_by_keys':{'typeOfLevel': 'hybrid'}})
 
 
     lon  = data.variables['longitude'].values
     lat  = data.variables['latitude'].values
     hyb  = data.variables['hybrid'].values
     time = pd.Timestamp(data.time.values) + pd.to_timedelta(data.step.values,'H')
 
     X = xr.Dataset(
                    data_vars = { 
                    'data':(['Alt','lat','lon'], data[val].values)
                                 },
                    coords = {
                              'Alt':(['lat','lon'],hyb),
                              'lat':(['lat','lon'],lat),
                              'lon':(['lat','lon'],on),
                              'time':time
                               },
     
                    )

 
     data_conc.append(X) 
1

There are 1 answers

0
Behzad Sadrfaridpour On

You may have find your answer by now. If not, try to reshape the coordinate values and see if that helps.

import numpy as np
     lon  = np.reshape(data.variables['longitude'].values, (1, -1))
     lat  = np.reshape(data.variables['latitude'].values, (1, -1))
     hyb  = np.reshape(data.variables['hybrid'].values, (1, -1))