Tuesday - Session B

This session is on the JEDI Unified Forward Operator (UFO) code and quality control (QC) filters. This tutorial has two main parts. First, you will first review how to clone, build and customize a bundle. Second, you will add in a new QC filter to JEDI.

This activity has no prerequisites besides having access to either a JupyterLab or SSH session.

Filters

Filters are an essential component in a data assimilation workflow. Filters can change quality control flags (i.e., to reject or retain observations) and observation error variances (e.g., one might wish to increase observation error variances to decrease the observation weight in the analysis instead of rejecting observations altogether).

In JEDI, filters are customizable and generic. This means that you can use the same code (written in C++) to accomplish diffferent tasks (specified by you in a YAML file). This tutorial introduces the YAML file format, explains how to specify a QC filter in YAML, and explains the backend code used to actually perform the filtering. We will implement a relatively simple filter in C++.

1) Access your AWS instance

Connect to your assigned compute node. You will use the same method as yesterday.

2) Clone and customize the ufo-bundle

JEDI bundles are convenient ways for users to build all of the JEDI components that are needed for a particular application. Open a new terminal and navigate to the ~/jedi/src directory. Yesterday, you worked with the fv3-bundle, which was already checked out from GitHub. Today, we are going to with with the ufo-bundle instead. The UFO bundle contains the ufo project and all of its dependencies, including the Object Oriented Prediction System (OOPS), the Interface for Observational Data Assimilation (IODA), the Unified Forward Operator (UFO), and the System-Agnostic Background Error Representation (SABER).

To clone the UFO bundle, run this command:

Change into the new ufo-bundle directory and look around. When a bundle is first cloned, it is very small, having only a CMakeLists.txt file and a few supplemental configuration and documentation files. It does not yet have any source code. This is because the bundle’s projects have not yet been cloned.

Open the CMakeLists.txt file in a text editor and examine it. It can be divided into three parts.

  • The first part (lines 1-22) is a preamble that you normally never have to change. The code here tells ecbuild that we are declaring a bundle, that requires a certain minimum version of the cmake program, and that requires C, C++ and Fortran compilers.
  • The second part (lines 23-36) has several ecbuild_bundle declarations that tell ecbuild the components of the bundle and where to find them. By default, the bundle depends on the develop branch of several repositories.
  • The final part (lines 37-48) is again mostly fixed. It concerns switches for generating documentation and setup of testing.

Modify the ecbuild_bundle declarations and switch from using the develop branches to the feature/academy branches for the oops, saber, ioda and ufo sub-projects. Leave the other ecbuild_bundle declarations untouched. Once this is done, save and close the file.

3) Enter the JEDI container, run ecbuild and then compile the bundle

This again follows the instruction from yesterday. You should already have downloaded the singularity container. Enter the container using:

singularity shell -e jedi-gnu-openmpi-dev_latest.sif

Once inside the container, create a build directory at ~/jedi/build-ufo and change into it (mkdir ~/jedi/build-ufo && cd ~/jedi/build-ufo).

Now, build and test JEDI.

ecbuild jedi/src/ufo-bundle

make -j4

ctest

This will again take some time. Ecbuild must download a fresh copy of the bundle components and all necessary data files for JEDI to run. GNU Make then must compile all of the source codes into executables and libraries. CTest will then validate the compiled programs and ensure that no tests fail.

While this process is happening, read ahead.

4) What Are YAML Files?

Programmers and computers typically store data as complex “objects” (structures and classes). In a computer’s memory, these objects may have very complicated storage involving pointers, references, dictionaries, and similar constructs. However, when we need to store these complex structures to a disk or send them across a network, we have to translate these complex structures into a series of bytes (a.k.a. we serialize an object into a byte stream).

There are lots of ways of doing this. However, JEDI wanted to employ a consistent, well-documented format that is easy for people to edit and for machines to read. So, we chose to use the YAML Ain’t Markup Language (YAML) format to store the configuration data for the JEDI project.

YAML was developed in 2001 and has been implemented for use with several programming languages.

Let’s take a look at a YAML file for a brief overview.

---
# Comments are indicated with the '#' symbol.
 name: "Your name here" # A string
 a-boolean-value: true
 an-integer-value: 3
 pi: 3.14159
 list-of-some-jedi-components:
   - saber
   - oops
   - ioda
   - ufo
 dictionary-of-things-to-see-around-monterey:
 - aquarium:
   types-of-animals:
   - jellyfish
   - turtles
   - fish
   free: false
 - cannery-row:
   historic: true
   shops: true
   has-monument: true
   food:
   - "Lalla Oceanside Grill":
     address: "654 Cannery Row"
   - "Schooners Coastal Kitchen & Bar":
     address: "400 Cannery Row"
   - "Chart House":
     address: "444 Cannery Row"
 - wharf:
   address: "#1 Old, Fishermans Wharf, Monterey, CA 93940"
   # TODO: Explore this area and add more details.

The file starts with three dashes. These dashes indicate the start of a new YAML document. YAML supports multiple documents, and compliant parsers will recognize each set of dashes as the beginning of a new one.

Comments are started with a space and a hashtag (” #”) and extend to the end of the line.

Next, we see the construct that makes up most of a typical YAML document: a key-value pair. “name” is a key that points to a string value: “Your name here”. YAML allows for several types of values: strings, integers, floating-point numbers, boolean values and dates are all acceptable.

Strings can optionally be enclosed in quotes. Quotes include both single and double quotes.

You can also add in arrays / lists. Each element in a list is denoted by an opening dash.

YAML elements can also be nested. This lets you emulate a group / folder structure. Nesting is accomplished by adding levels of spaces (no tabs allowed).

See this link for more examples.

5) How Do We Invoke Filters Using a YAML Configuration File?

Example YAML code for filters can be found in the UFO repository in ufo/test/testinput. The qc*.yaml files provide many examples of how to use QC filters.

Let’s look at the DifferenceCheck filter to see how a relatively basic filter works.

See qc_differencecheck.yaml:

window_begin: 2018-04-14T21:00:00Z
window_end: 2018-04-15T03:00:00Z

Observations:
  ObsTypes:
  - ObsSpace:
      name: test data
      ObsDataIn:
        obsfile: Data/filters_testdata.nc4
      simulate:
        variables: [variable1]
    ObsFilters:
    - Filter: Difference Check    # test minvalue with one var (compare var3-var4 with min)
      value: var3@MetaData        # var3@MetaData = 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
      reference: var4@MetaData    # var4@MetaData = 0, 0, 0, 0, 0, 1, 2, 3, 4, 5
      minvalue: 0.0
    passedBenchmark: 5
  - ObsSpace:
      name: test data
      ObsDataIn:
        obsfile: Data/filters_testdata.nc4
      simulate:
        variables: [variable1, variable2, variable3]
    ObsFilters:
    - Filter: Difference Check     # test same minvalue with three vars (compare var3-var4 with min)
      value: var3@MetaData         # var3@MetaData = 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
      reference: var4@MetaData     # var4@MetaData = 0, 0, 0, 0, 0, 1, 2, 3, 4, 5
      minvalue: 1.0
    passedBenchmark: 15
  - ObsSpace:
      name: test data
      ObsDataIn:
        obsfile: Data/filters_testdata.nc4
      simulate:
        variables: [variable1]
    ObsFilters:
    - Filter: Difference Check     # test maxvalue (compare var3-var4 with max)
      filter variables:
      - name: variable1
      value: var3@MetaData         # var3@MetaData = 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
      reference: var4@MetaData     # var4@MetaData = 0, 0, 0, 0, 0, 1, 2, 3, 4, 5
      maxvalue: -3.0
    passedBenchmark: 3
  - ObsSpace:
      name: test data
      ObsDataIn:
        obsfile: Data/filters_testdata.nc4
      simulate:
        variables: [variable1]
    ObsFilters:
    - Filter: Difference Check        # test min and maxvalue (compare var3-var4 with min and max)
      filter variables:
      - name: variable1
      value: variable2@ObsValue       # variable2@ObsValue = 10, 12, 14, 16, 18, 20, 22, 24, 26, 28
      reference: variable1@ObsValue   # variable1@ObsValue = 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
      minvalue: 2.0
      maxvalue: 6.0
    passedBenchmark: 5
  - ObsSpace:
      name: test data
      ObsDataIn:
        obsfile: Data/filters_testdata.nc4
      simulate:
        variables: [variable1]
    ObsFilters:
    - Filter: Difference Check         # test threshold (compare abs(variable2 - variable1) with threshold)
      filter variables:
      - name: variable1
      value: variable2@ObsValue        # variable2@ObsValue = 10, 12, 14, 16, 18, 20, 22, 24, 26, 28
      reference: variable1@ObsValue    # variable1@ObsValue = 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
      threshold: 3
    passedBenchmark: 4
  - ObsSpace:
      name: test data
      ObsDataIn:
        obsfile: Data/filters_testdata.nc4
      simulate:
        variables: [variable1]
    ObsFilters:
    - Filter: Difference Check        # test min and maxvalue (equal), equivalent to previous test
      filter variables:
      - name: variable1
      value: variable2@ObsValue       # variable2@ObsValue = 10, 12, 14, 16, 18, 20, 22, 24, 26, 28
      reference: variable1@ObsValue   # variable1@ObsValue = 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
      minvalue: -3
      maxvalue: 3
    passedBenchmark: 4

UFO accesses observation data via functions and subroutines in the ObsSpace (Observation Space) class. The above YAML files specifies several instances of ObsSpace. Each space has a name, a path to the input data and a list of variables to be simulated.

Paired with the ObsSpaces are the filters (ObsFilters) that act on each space. When specifying a filter, you must provide its name and any other configuration information that it requires. The above YAML file invokes the Difference Check filter. Its options are as follows:

Parameter Description
value The variable that we are comparing
reference The variable that we are comparing against
minvalue The minimum difference of (value - reference) for a valid datum
maxvalue The maximum difference of (value - reference) for a valid datum
threshold A shortcut for expressing minvalue = -threshold, maxvalue = threshold

Since we are using this YAML file in a test, we also encode the expected number number of passed locations using the passedBenchmark option.

6) How Do We Implement a Filter?

The C++ code for all filters can be found in the UFO repository in src/ufo/filters [link].

The full source code for the DifferenceCheck filter is available under your JupyterLab session’s Examples directory (accessible from the left sidebar). If you are not using JupyterLab, then the header is [here] and the full source code is [here].

Defining the filter - the header file

Annotated excerpt from DifferenceCheck.h:

namespace ufo {

/// DifferenceCheck filter

class DifferenceCheck : public FilterBase,
                        private util::ObjectCounter<DifferenceCheck> {
 public:
  static const std::string classname() {return "ufo::DifferenceCheck";}

  /// !!! This Constructor function initializes an instance of the
  /// !!! filter based on options specified in the YAML configuration file.
  DifferenceCheck(ioda::ObsSpace &, const eckit::Configuration &,
                  boost::shared_ptr<ioda::ObsDataVector<int> >,
                  boost::shared_ptr<ioda::ObsDataVector<float> >);
  ~DifferenceCheck();

 private:
  void print(std::ostream &) const override;
  /// !!! This is the function that does all of the work in the filter. !!!
  void applyFilter(const std::vector<bool> &, const Variables &,
                   std::vector<std::vector<bool>> &) const override;
  int qcFlag() const override {return QCflags::diffref;}
  const Variable ref_;
  const Variable val_;
};

}  // namespace ufo

Implementing the filter - the source code file

Excerpt from DifferenceCheck.cpp:

// -----------------------------------------------------------------------------

/** !!! This is the constructor function.
 *
 *  When we instantiate a new DifferenceFilter object, we read in configuration
 *  from the YAML files (stored in the _config_ variable).
 *
 *  We look for two keys:
 *  - refererence: the name of the variable used for the reference.
 *  - value: the name of the variable that contains our data.
**/
DifferenceCheck::DifferenceCheck(ioda::ObsSpace & obsdb, const eckit::Configuration & config,
                                 boost::shared_ptr<ioda::ObsDataVector<int> > flags,
                                 boost::shared_ptr<ioda::ObsDataVector<float> > obserr)
  : FilterBase(obsdb, config, flags, obserr),
    ref_(config_.getString("reference")), val_(config_.getString("value"))
{
  oops::Log::trace() << "DifferenceCheck contructor starting" << std::endl;
  /// Here we tell OOPS and IODA that our filter requires these variables to work.
  /// I.e. these variables have to be available in memory.
  allvars_ += ref_;
  allvars_ += val_;
}

// -----------------------------------------------------------------------------

/** !!! This function does the actual work.
 *
 * We read in three keys from the YAML configuration: minvalue, maxvalue, and threshold.
 *
 * When applying this filter, we loop over all possible locations.
 * For each location, we check the difference between the two variables (reference and value).
 * If the difference is outside of the bounds specified by minvalue, maxvalue and threshold,
 * then we flag that location. This flag gets passed back to the calling function, which then
 * sets the appropriate QC flag.
**/
void DifferenceCheck::applyFilter(const std::vector<bool> & apply,
                                  const Variables & filtervars,
                                  std::vector<std::vector<bool>> & flagged) const {
  oops::Log::trace() << "DifferenceCheck priorFilter" << std::endl;

  const float missing = util::missingValue(missing);
  const size_t nlocs = obsdb_.nlocs();

// min/max value setup
  float vmin = config_.getFloat("minvalue", missing);
  float vmax = config_.getFloat("maxvalue", missing);

// check for threshold and if exists, set vmin and vmax appropriately
  const float thresh = config_.getFloat("threshold", missing);
  if (thresh != missing) {
    vmin = -thresh;
    vmax = thresh;
  }

// Get reference values and values to compare (as floats)
  std::vector<float> ref, val;
  data_.get(ref_, ref);
  data_.get(val_, val);
  ASSERT(ref.size() == val.size());
// Loop over all obs
  for (size_t jobs = 0; jobs < nlocs; ++jobs) {
    if (apply[jobs]) {
      // check to see if one of the reference or value is missing
      if (val[jobs] == missing || ref[jobs] == missing) {
        for (size_t jv = 0; jv < filtervars.nvars(); ++jv) {
          flagged[jv][jobs] = true;
        }
      } else {
// Check if difference is within min/max value range and set flag
        float diff = val[jobs] - ref[jobs];
        for (size_t jv = 0; jv < filtervars.nvars(); ++jv) {
          if (vmin != missing && diff < vmin) flagged[jv][jobs] = true;
          if (vmax != missing && diff > vmax) flagged[jv][jobs] = true;
        }
      }
    }
  }
}

}  // namespace ufo

7) Try To Add a New Filter

We are going to re-implement a simplified version of the Bounds Check filter. This filter checks that observation data are within certain user-specified bounds.

7a) The backend logic

In your JEDI bundle, navigate into the ufo/src/ufo/filters directory. Copy the DifferenceCheck.cc and DifferenceCheck.h files to PracticalBoundsCheck.cc and PracticalBoundsCheck.h, respectively.

Open these files in your editor of choice.

In PracticalBoundsCheck.h:

  • Rename all references of DifferenceCheck to PracticalBoundsCheck. Don’t forget the text near #ifndef UFO_FILTERS_!
  • Change the line int qcFlag() const override {return QCflags::diffref;} to return a different flag: QCflags::bounds. This QC flag is conveniently already defined in ufo/filters/QCflags.h.
  • Remove the lines defining const Variable ref_; and const Variable val_;.

In PracticalBoundsCheck.cc:

  • Rename all references of DifferenceCheck to PracticalBoundsCheck.
  • In PracticalBoundsCheck::PracticalBoundsCheck(...), remove references to ref_ and val_.
  • In PracticalBoundsCheck::applyFilter(...), replace the function body with something like this:
const float missing = util::missingValue(missing);
ufo::Variables testvars;
testvars += ufo::Variables(filtervars, "ObsValue");

const float vmin = config_.getFloat("minvalue", missing);
const float vmax = config_.getFloat("maxvalue", missing);

// Sanity checks
  if (filtervars.nvars() == 0) {
    oops::Log::error() << "No variables will be filtered out in filter "
                       << config_ << std::endl;
    ABORT("No variables specified to be filtered out in filter");
  }

// Loop over all variables to filter
    for (size_t jv = 0; jv < testvars.nvars(); ++jv) {
      //  get test data for this variable
      std::vector<float> testdata;
      data_.get(testvars.variable(jv), testdata);
      //  apply the filter
      for (size_t jobs = 0; jobs < obsdb_.nlocs(); ++jobs) {
        if (apply[jobs]) {
          ASSERT(testdata[jobs] != missing);
          if (vmin != missing && testdata[jobs] < vmin) flagged[jv][jobs] = true;
          if (vmax != missing && testdata[jobs] > vmax) flagged[jv][jobs] = true;
        }
      }
    }
  • Feel free to customize the function further.

7b) Add your new filter to the build system

  • Edit src/ufo/filters/CMakeLists.txt and add in PracticalBoundsCheck.cc and PracticalBoundsCheck.h.

  • UFO needs to be told that another filter is available. The list of known filters is located in ufo/src/ufo/instantiateObsFilterFactory.h.

    To add in the new filter, first add #include "ufo/filters/PracticalBoundsCheck.h to the top of instantiateObsFilterFactory.h.

    At the end of instantiateObsFilterFactory.h, follow the pattern and add in:

    static oops::FilterMaker<MODEL, oops::ObsFilter<MODEL, ufo::PracticalBoundsCheck> >
         makerChk16_("Practical Bounds Check");
    
  • Re-run make ufo.

  • The filter is added!

7c) Add in the YAML that describes this filter to a test

This is covered in the tomorrow’s tutorial.