Search code examples
pythonvariablesdatasetnetcdfpython-xarray

How to remove latitude and longitude (they're constants) from variables in netcdf dataset using xarray?


I have a NetCDF data set I am trying to remove latitude and longitude data from my data variables so they can be indexed properly. The shape of each data variable is always (x, 1, 1) with x being my number of data points and the 1's representing the static longitude and latitudes. I've tried xarray.Dataset.squeeze and xarray.Dataset.drop and xarray.DataArray.drop_vars targeting latitude and longitude. I also tried it with a pandas data frame and the latitude and longitudes still stay glued to my variables and prevent indexing properly. If you copy my code below you will see the variable 'wave_height' shows a shape of (3411,1,1). I want a function that makes this shape just (3411,).

url = 'https://dods.ndbc.noaa.gov/thredds/fileServer/data/stdmet/46077/46077h2021.nc'
reqSpectra = urllib.request.Request(url)
with urllib.request.urlopen(reqSpectra) as respS:
    ds_s = xr.open_dataset(io.BytesIO(respS.read()))

wh = ds_s.variables['wave_height']
wh

Solution

  • I would use NumPy's squeeze to remove extra dimensions.

    import urllib
    import xarray as xr
    import numpy as np
    import io
    # --------------------------------------------------------------------------
    url = 'https://dods.ndbc.noaa.gov/thredds/fileServer/data/stdmet/46077/46077h2021.nc'
    reqSpectra = urllib.request.Request(url)
    with urllib.request.urlopen(reqSpectra) as respS:
        ds_s = xr.open_dataset(io.BytesIO(respS.read()))
    # --------------------------------------------------------------------------
    wh = ds_s.variables['wave_height'];
    wh = np.squeeze(wh);
    wh