TECA
The Toolkit for Extreme Climate Analysis
teca_apply_binary_mask.h
1 #ifndef teca_apply_binary_mask_h
2 #define teca_apply_binary_mask_h
3 
4 #include "teca_shared_object.h"
5 #include "teca_algorithm.h"
6 #include "teca_metadata.h"
7 
8 #include <string>
9 #include <vector>
10 
11 TECA_SHARED_OBJECT_FORWARD_DECL(teca_apply_binary_mask)
12 
13 /// Applies a mask to a given list of variables
14 /**
15  * Given a mask variable, this routine applies the mask to a list of input
16  * variables.
17  *
18  * The mask variable can either be binary, or it can represent a probability
19  * ranging from 0 to 1. For mask variable `mask` and input variable `var`, this
20  * algorithm computes `mask * var` and sends the resulting array downstream; this
21  * masking operation is applied for all variables in the input list.
22  *
23  * A potential use-case for this algorithm is masking quantities like
24  * precipitation by the probability of atmospheric river presence; the average
25  * of this masked precipitation variable gives the average precipitation due to
26  * atmospheric rivers.
27  *
28  * The output variable names are given a prefix to distinguish them from the
29  * upstream versions. E.g., if the algorithm property `output_variable_prefix` is set
30  * to 'ar_', and the variable being masked is 'precip', then the output array
31  * name is 'ar_precip'.
32  */
34 {
35 public:
36  TECA_ALGORITHM_STATIC_NEW(teca_apply_binary_mask)
37  TECA_ALGORITHM_DELETE_COPY_ASSIGN(teca_apply_binary_mask)
38  TECA_ALGORITHM_CLASS_NAME(teca_apply_binary_mask)
40 
41  // report/initialize to/from Boost program options
42  // objects.
43  TECA_GET_ALGORITHM_PROPERTIES_DESCRIPTION()
44  TECA_SET_ALGORITHM_PROPERTIES()
45 
46  /** @name mask_variable
47  * set the name of the variable containing the mask values
48  */
49  ///@{
50  TECA_ALGORITHM_PROPERTY(std::string, mask_variable)
51  ///@}
52 
53  /** @name masked_variable
54  * A list of of variables to apply the mask to. If empty no arrays will be
55  * requested, and no variables will be masked
56  */
57  ///@{
58  TECA_ALGORITHM_VECTOR_PROPERTY(std::string, masked_variable)
59  ///@}
60 
61  /** @name output_variable_prefix
62  * A prefix for the names of the variables that have been masked. If this
63  * is empty masked data replaces its input, otherwise input data is
64  * preserved and masked data is added.
65  */
66  ///@{
67  TECA_ALGORITHM_PROPERTY(std::string, output_variable_prefix)
68  ///@}
69 
70  /** helper that constructs and returns the result variable names taking
71  * into account he list of masked_variables and the output_variable_prefix.
72  * use this to know what variables will be produced.
73  */
74  void get_output_variable_names(std::vector<std::string> &names);
75 
76 protected:
78 
79 private:
80  using teca_algorithm::get_output_metadata;
81 
82  teca_metadata get_output_metadata(
83  unsigned int port,
84  const std::vector<teca_metadata> &input_md) override;
85 
86  std::vector<teca_metadata> get_upstream_request(
87  unsigned int port, const std::vector<teca_metadata> &input_md,
88  const teca_metadata &request) override;
89 
90  const_p_teca_dataset execute(unsigned int port,
91  const std::vector<const_p_teca_dataset> &input_data,
92  const teca_metadata &request) override;
93 
94  // helper that given and input variable name constructs the result variable
95  // name taking into account the output_variable_prefix
96  std::string get_output_variable_name(std::string input_var);
97 
98 private:
99  std::string mask_variable;
100  std::vector<std::string> masked_variables;
101  std::string output_variable_prefix;
102 };
103 
104 #endif
teca_metadata
A generic container for meta data in the form of name=value pairs.
Definition: teca_metadata.h:21
teca_apply_binary_mask
Applies a mask to a given list of variables.
Definition: teca_apply_binary_mask.h:33
teca_shared_object.h
teca_error::TECA_EXPORT
p_teca_error_handler error_handler TECA_EXPORT
The global error handler instance.
teca_algorithm
The interface to TECA pipeline architecture.
Definition: teca_algorithm.h:237