0

I have downloaded the dataset from https://cds.climate.copernicus.eu/. I am looking at different meteorological parameters and the dataset is split in west and east domain. Hence, I have several grib file, one each per parameter. Now I would like to extract the timeseries of the parameter for a certain location (latl/lon) in order to feed it to my model.

I have tried grib_to_netcdf and the following error:

grib_to_netcdf: Version 2.30.2
grib_to_netcdf: Processing input file 'westdomain.grib'.
grib_to_netcdf: Found 23256 GRIB fields in 1 file.
grib_to_netcdf: Ignoring key(s): method, type, stream, refdate, hdate
grib_to_netcdf: Creating netCDF file 'westdomain.nc'
grib_to_netcdf: NetCDF library version: 4.9.2 of May 19 2023 11:45:38 $
grib_to_netcdf: Creating large (64 bit) file format.
ECCODES ERROR   :  Grid type = lambert
ECCODES ERROR   :  First GRIB is not on a regular lat/lon grid or on a regular Gaussian grid. Exiting.

Hence, I have moved from the terminal to jupyter notebook and tried to open it with cfgrib.open_dataset(). This is how the dataset looks:

xarray.Dataset {
dimensions:
    time = 3880 ;
    y = 1269 ;
    x = 1069 ;

variables:
    datetime64[ns] time(time) ;
        time:long_name = initial time of forecast ;
        time:standard_name = forecast_reference_time ;
    timedelta64[ns] step() ;
        step:long_name = time since forecast_reference_time ;
        step:standard_name = forecast_period ;
    float64 surface() ;
        surface:long_name = original GRIB coordinate for key: level(surface) ;
        surface:units = 1 ;
    float64 latitude(y, x) ;
        latitude:units = degrees_north ;
        latitude:standard_name = latitude ;
        latitude:long_name = latitude ;
    float64 longitude(y, x) ;
        longitude:units = degrees_east ;
        longitude:standard_name = longitude ;
        longitude:long_name = longitude ;
    datetime64[ns] valid_time(time) ;
        valid_time:standard_name = time ;
        valid_time:long_name = time ;
    float32 ssr(time, y, x) ;
        ssr:GRIB_paramId = 176 ;
        ssr:GRIB_dataType = fc ;
        ssr:GRIB_numberOfPoints = 1356561 ;
        ssr:GRIB_typeOfLevel = surface ;
        ssr:GRIB_stepUnits = 1 ;
        ssr:GRIB_stepType = accum ;
        ssr:GRIB_gridType = lambert ;
        ssr:GRIB_DxInMetres = 2500.0 ;
        ssr:GRIB_DyInMetres = 2500.0 ;
        ssr:GRIB_LaDInDegrees = 72.0 ;
        ssr:GRIB_Latin1InDegrees = 72.0 ;
        ssr:GRIB_Latin2InDegrees = 72.0 ;
        ssr:GRIB_LoVInDegrees = 324.0 ;
        ssr:GRIB_NV = 0 ;
        ssr:GRIB_Nx = 1069 ;
        ssr:GRIB_Ny = 1269 ;
        ssr:GRIB_cfName = surface_net_downward_shortwave_flux ;
        ssr:GRIB_cfVarName = ssr ;
        ssr:GRIB_gridDefinitionDescription = Lambert conformal ;
        ssr:GRIB_iScansNegatively = 0 ;
        ssr:GRIB_jPointsAreConsecutive = 0 ;
        ssr:GRIB_jScansPositively = 1 ;
        ssr:GRIB_latitudeOfFirstGridPointInDegrees = 55.81 ;
        ssr:GRIB_latitudeOfSouthernPoleInDegrees = 0.0 ;
        ssr:GRIB_longitudeOfFirstGridPointInDegrees = 302.903 ;
        ssr:GRIB_longitudeOfSouthernPoleInDegrees = 0.0 ;
        ssr:GRIB_missingValue = 3.4028234663852886e+38 ;
        ssr:GRIB_name = Surface net short-wave (solar) radiation ;
        ssr:GRIB_shortName = ssr ;
        ssr:GRIB_units = J m**-2 ;
        ssr:long_name = Surface net short-wave (solar) radiation ;
        ssr:units = J m**-2 ;
        ssr:standard_name = surface_net_downward_shortwave_flux ;

// global attributes:
    :GRIB_edition = 2 ;
    :GRIB_centre = enmi ;
    :GRIB_centreDescription = Oslo ;
    :GRIB_subCentre = 255 ;
    :Conventions = CF-1.7 ;
    :institution = Oslo ;
    :history = 2023-07-11T09:53 GRIB to CDM+CF via cfgrib-0.9.10.4/ecCodes-2.30.2 with {"source": "../../", "filter_by_keys": {}, "encode_cf": ["parameter", "time", "geography", "vertical"]} ;
}

I tried to extract the timeseries for ssr for a certain with ds.ssr.sel(latitude=latitude, longitude=longitude, method='nearest') but I get the following error:

I get the following error: ---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[19], line 8
      5 longitude = 15.64022
      7 # Extract the ssr time series for the specified latitude and longitude
----> 8 ssr_timeseries = ds.ssr.sel(latitude=latitude, longitude=longitude, method='nearest')
     10 # Print the ssr time series
     11 print(ssr_timeseries)

File /opt/anaconda3/envs/pvlib/lib/python3.10/site-packages/xarray/core/dataarray.py:1550, in DataArray.sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
   1440 def sel(
   1441     self: T_DataArray,
   1442     indexers: Mapping[Any, Any] | None = None,
   (...)
   1446     **indexers_kwargs: Any,
   1447 ) -> T_DataArray:
   1448     """Return a new DataArray whose data is given by selecting index
   1449     labels along the specified dimension(s).
   1450 
   (...)
   1548     Dimensions without coordinates: points
   1549     """
-> 1550     ds = self._to_temp_dataset().sel(
   1551         indexers=indexers,
   1552         drop=drop,
   1553         method=method,
   1554         tolerance=tolerance,
   1555         **indexers_kwargs,
   1556     )
   1557     return self._from_temp_dataset(ds)

File /opt/anaconda3/envs/pvlib/lib/python3.10/site-packages/xarray/core/dataset.py:2653, in Dataset.sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
   2592 """Returns a new dataset with each array indexed by tick labels
   2593 along the specified dimension(s).
   2594 
   (...)
   2650 DataArray.sel
   2651 """
   2652 indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "sel")
-> 2653 query_results = map_index_queries(
   2654     self, indexers=indexers, method=method, tolerance=tolerance
   2655 )
   2657 if drop:
   2658     no_scalar_variables = {}

File /opt/anaconda3/envs/pvlib/lib/python3.10/site-packages/xarray/core/indexing.py:182, in map_index_queries(obj, indexers, method, tolerance, **indexers_kwargs)
    179     options = {"method": method, "tolerance": tolerance}
    181 indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "map_index_queries")
--> 182 grouped_indexers = group_indexers_by_index(obj, indexers, options)
    184 results = []
    185 for index, labels in grouped_indexers:

File /opt/anaconda3/envs/pvlib/lib/python3.10/site-packages/xarray/core/indexing.py:144, in group_indexers_by_index(obj, indexers, options)
    142     grouped_indexers[index_id][key] = label
    143 elif key in obj.coords:
--> 144     raise KeyError(f"no index found for coordinate {key!r}")
    145 elif key not in obj.dims:
    146     raise KeyError(f"{key!r} is not a valid dimension or coordinate")

KeyError: "no index found for coordinate 'latitude'"

Anyone familiar on how to get a lat/lon location if the grid is Lambert Conformal Conic grid? Or any advice on how to change the grid type?

Kim
  • 1
  • 1

0 Answers0