2

I have an application with wxpython in which I can read and show NetCDF4 file and some information like x y coordinates.

what I need is the numerical value or pixel value for the variable air dep which corresponds to the coordinates cursor x y : x = col & y = row :(X 89.98774193548385, Y 35.8141935483871).

My NetCDF file has some variables and I need just this information for variable "air_dep".

The code is shown below:

In [1]: import numpy as np^M
   ...: import netCDF4^M
   ...: from netCDF4 import Dataset^M
   ...: ^M
   ...: fic='air.departure.sig995.2012.nc'^M
   ...: ^M
   ...: path='D:/data/'^M
   ...: ^M
   ...: nc = netCDF4.Dataset(path+fic,'r')

In [2]:

In [2]: nc
Out[2]:
<class 'netCDF4._netCDF4.Dataset'>
root group (NETCDF4 data model, file format HDF5):
    description: The departure of the NCEP/NCAR Reanalysis air temperature from its value at Darwin, Australia. Data is from NMC initialized reanalysis
(4x/day).  These are the 0.9950 sigma level values.
    dimensions(sizes): lat(73), lon(144), time(366)
    variables(dimensions): float32 lat(lat), float32 lon(lon), float64 time(time), float64 air_dep(time,lat,lon)
    groups:

In [3]: nc.variables.keys()
Out[3]: odict_keys(['lat', 'lon', 'time', 'air_dep'])

I know the code is in wxpython but i just edit the code without GUI and ploting python3.6.

That's what I have, I can just show the x y but what I need is the numeric value and lat-lon at this point! i'm new in netCDF4

enter image description here

2
  • 2
    Please reduce your example to plain Python code without all the GUI elements and plotting. Commented Jul 5, 2019 at 12:26
  • @bugmenot123 i'm edit the code without GUI Commented Jul 7, 2019 at 19:26

1 Answer 1

3

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

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
1
  • 1
    Comments are not for extended discussion; this conversation has been moved to chat.
    – Ian Turton
    Commented Jul 8, 2019 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.