Skip to main content
Mod Moved Comments To Chat
added 1002 characters in body
Source Link
pan
  • 567
  • 5
  • 11

UPDATE: Following the comments below, I have updated the above script to get the col and row of a pixels and retunr the lat, lon and nc value.

def ExtractVarsFromNetcdf(x_coord, y_coord, ncdir, varnames):
    """   
    @params:
        x_coord    - Required : the x coordinate of the point
        x_coord    - Required : the y coordinate of the point
        ncdir      - Required : The directory of the netcdf file.
        varnames   - Required : The netcdf variables
    """

    with Dataset(ncdir, "r") as nc:

        # Get the nc lat and lon from the point's x, y
        lon = nc.variables["lon"][int(round(x_coord))]
        lat = nc.variables["lat"][int(round(y_coord))]

        # Return a np.array with the netcdf data
        nc_data = np.ma.getdata(
            [nc.variables[varname][:, x_coord, y_coord] for varname in varnames]
        )

        return nc_data, lon, lat

UPDATE: Following the comments below, I have updated the above script to get the col and row of a pixels and retunr the lat, lon and nc value.

def ExtractVarsFromNetcdf(x_coord, y_coord, ncdir, varnames):
    """   
    @params:
        x_coord    - Required : the x coordinate of the point
        x_coord    - Required : the y coordinate of the point
        ncdir      - Required : The directory of the netcdf file.
        varnames   - Required : The netcdf variables
    """

    with Dataset(ncdir, "r") as nc:

        # Get the nc lat and lon from the point's x, y
        lon = nc.variables["lon"][int(round(x_coord))]
        lat = nc.variables["lat"][int(round(y_coord))]

        # Return a np.array with the netcdf data
        nc_data = np.ma.getdata(
            [nc.variables[varname][:, x_coord, y_coord] for varname in varnames]
        )

        return nc_data, lon, lat
Source Link
pan
  • 567
  • 5
  • 11

The process is quite simple:

  1. get the netcdf's latitudes and longitudes as lists;
  2. use these lists to get the column and row of the netcdf's grid that correspond to your point's longitude and latitude, respectively;
  3. use these column and row values to get the netcdf data for your variable.

Below I attach a function that implements the aforementioned workflow and I use for extracting CMEMS netcdf data. It can get data for more than one variables at a time.

def ExtractVarsFromNetcdf(point, ncdir, varnames):
    """   
    @params:
        point      - Required : shapely point
        ncdir      - Required : The directory of the netcdf file.
        varnames   - Required : The netcdf variables
    """

    with Dataset(ncdir, "r") as nc:

        # Get the nc row, col for the point's lat, lon
        col = np.argmin(np.abs(nc.variables["lon"][:] - point.x))
        row = np.argmin(np.abs(nc.variables["lat"][:] - point.y))

        # Return a np.array with the netcdf data
        nc_data = np.ma.getdata(
            [nc.variables[varname][:, row, col] for varname in varnames]
        )

        return nc_data