NetCDF Python: How to Read Numerical Values of Variables in NetCDF4

netcdfpython 3

I need to undestand how I can get the numerical value of one channel IR_108 ( variable) in NetCDF4 and some other data:

import numpy as np
from netCDF4 import Dataset

 path = '/home/data/Mmultic3kmNC4_msg04_201905080200.
    ...: nc'

In [78]: f=netCDF4.Dataset(path,'r')

 f.variables.keys()
Out[80]: odict_keys(['time', 'dtime', 'IR_016', 'commentaires', 'satellite', 'geos', 'ImageNavigation', 'GeosCoordinateSystem', 'Y', 'X', 'Albedo_to_Native_count_IR_016', 'IR_039', 'Temp_to_Native_count_IR_039', 'IR_087', 'Temp_to_Native_count_IR_087', 'IR_097', 'Temp_to_Native_count_IR_097', 'IR_108', 'Temp_to_Native_count_IR_108', 'IR_120', 'Temp_to_Native_count_IR_120', 'IR_134', 'Temp_to_Native_count_IR_134', 'VIS006', 'Albedo_to_Native_count_VIS006', 'VIS008', 'Albedo_to_Native_count_VIS008', 'WV_062', 'Temp_to_Native_count_WV_062', 'WV_073', 'Temp_to_Native_count_WV_073'])

In [81]: IR=f.variables['IR_108']

In [82]: IR
Out[82]: 
<class 'netCDF4._netCDF4.Variable'>
int16 IR_108(ny, nx)
    standard_name: toa_brightness_temperature
    long_name: 10.8 microns infra-red channel MSG Imager
    units: K
    scale_factor: 0.01
    add_offset: 273.15
    valid_min: -27315
    valid_max: 32767
    _FillValue: -32768
    available: yes
    comment: channel 9
    nuc: 931.122
    alpha: 0.9983
    beta: 0.6256
    CalibrationParametersOrigin: rttov
    _CoordinateSystems: GeosCoordinateSystem
    grid_mapping: GeosCoordinateSystem
    resolution: 3.0
    Offset : radiance = offset + CN x slope: -10.456819486590666
    Slope : radiance = offset + CN x slope: 0.2050356762076601
unlimited dimensions: 
current shape = (3712, 3712)
filling on

print(IR.dimensions)
('ny', 'nx')

fisrt question The dimensions (nx,ny) is the row and column? or the numerical value corresponds to coordinates x;y ?

What I would like to understand from the information on the variable IR_108 how I can have the numerical values in this channel and also the geographical coordinates X Y or lat and lon.

The idea that I have points x, y ( 2150,525) which corresponds to the line and column and I will have to extract from these x y the numerical value of the variable in this pixel. For example, the pixel 2150 row 525 corresponds to the value IR_108 = 4.7.

Best Answer

All the X,Y coordinates are stored in arrays and can be extracted by:

x=f.variables['X'][:]
y=f.variables['Y'][:]

Let's say we need to extract the value of IR_108 at a specific location (X=500000, Y=250000). Get the array indices for the data point nearest this position:

idx=(np.abs(x-500000)).argmin()
idy=(np.abs(y-250000)).argmin()

The grid coords for these indices may be double-checked by getting the values of X by x[idx] and Y by y[idy]. Then the value of IR_108 at this location is:

val_IR_108 = f.variables['IR_108'][idy,idx]

Which is 281.56 on y,x indexing.

Related Question