6. Read netCDF Input

6.1. Description

The METreadnc module is used to read netCDF input files and create a list of Xarray Datasets or a Pandas dataframe.

6.2. Requirements

METreadnc requires the following Python packages that must be installed prior to us:

  • xarray

  • netcdf4

  • pandas

  • pyyaml

Install the same versions of Python packages as those specified in the nco_requirements.txt or requirements.txt in the METcalcpy repository (https://github.com/dtcenter/METcalcpy).

6.3. Setting up

  • set up a base directory, where the METdataio source code reside

bash:
  export BASE_DIR=/path/to/METdataio

csh:
  setenv BASE_DIR /path/to/METdataio
  • replace /path/to with an actual path

  • set up a working directory, where the YAML config file will be located (if using from the command line)

bash:
 export WORKING_DIR=/path/to/working_dir

csh:
 setenv WORKING_DIR /path/to/working_dir
  • set the PYTHONPATH:

bash
export PYTHONPATH=$BASE_DIR:/$BASE_DIR/METdataio:$BASE_DIR/METdataio/METreadnc:$BASE_DIR/METcalcpy:$BASE_DIR/METcalcpy/metcalcpy

csh
setenv PYTHONPATH $BASE_DIR:/$BASE_DIR/METdataio:$BASE_DIR/METdataio/METreadnc:$BASE_DIR/METcalcpy:$BASE_DIR/METcalcpy/metcalcpy

6.4. Invoking METreadnc from within your code

  • Import the appropriate module

import METreadnc.util.read_netcdf as read_netcdf
  • Create a file reader object:

file_reader = read_netcdf.ReadNetCDF()
  • To read the netCDF file(s) into a list of Xarray Datasets:

    Invoke the read_into_xarray method with the name of a single file or a list of files

ds = file_reader.read_into_xarray(infile)

The variable infile represents a single file (string) or a list of file names. Specify the full path to the file(s).

  • To read the netCDF file(s) into a Pandas DataFrame:

    Invoke the read_into_pandas method with the name of a single file or a list of files. Specify the full path to the file(s).

df = file_reader_read_into_pandas(infile)

The variable infile represents a single file (string) or a list of file names. Specify the full path to the file(s).