TECA
The Toolkit for Extreme Climate Analysis
teca_time_axis_convolution.h
1 #ifndef teca_time_axis_convolution_h
2 #define teca_time_axis_convolution_h
3 
4 #include "teca_config.h"
5 #include "teca_shared_object.h"
6 #include "teca_algorithm.h"
7 #include "teca_metadata.h"
8 
9 #include <string>
10 #include <vector>
11 
12 TECA_SHARED_OBJECT_FORWARD_DECL(teca_time_axis_convolution)
13 
14 /// An algorithm that applies a convolution along the time axis
15 /** Supports constant, Gaussian, and explicitly provided convolution kernels of
16  * arbitrary width applied over forward, backward, or centered stencils.
17  *
18  * A number of options are provided for specifying kernel weights.
19  *
20  * * User provided kernels can be explicitly specified via the combination of
21  * ::set_kernel_weights and ::set_kernel_name.
22  *
23  * * The kernel can be generated at run time by providing a kernel name, width,
24  * and flag selecting either a high or low pass filter via the combination of
25  * ::set_kernel_name, ::set_kernel_width, and ::set_use_high_pass. In this
26  * case default kernel parameters are used.
27  *
28  * * The kernel can be generated with explicitly provided kernel parameters.
29  */
31 {
32 public:
33  TECA_ALGORITHM_STATIC_NEW(teca_time_axis_convolution)
34  TECA_ALGORITHM_DELETE_COPY_ASSIGN(teca_time_axis_convolution)
35  TECA_ALGORITHM_CLASS_NAME(teca_time_axis_convolution)
36 
38 
39  // report/initialize to/from Boost program options objects.
40  TECA_GET_ALGORITHM_PROPERTIES_DESCRIPTION()
41  TECA_SET_ALGORITHM_PROPERTIES()
42 
43  /** @name stencil_type
44  * Select which time steps are convolved. The default stencil is backward.
45  */
46  ///@{
47  enum {
48  backward, /// convolve with time steps before the active step
49  centered, /// convolve with time steps centered on the active step
50  forward /// convolve with time steps after the active step
51  };
52 
53  TECA_ALGORITHM_PROPERTY(int, stencil_type)
54 
55  /// convolve with time steps after the active step
56  void set_stencil_type_to_forward()
57  { this->stencil_type = forward; }
58 
59  /// convolve with time steps before the active step
61  { this->stencil_type = backward; }
62 
63  /// convolve with time steps centered on the active step
65  { this->stencil_type = centered; }
66 
67  /// set the stencil type from a string
68  int set_stencil_type(const std::string &type);
69 
70  /// get the stencil as a string
71  std::string get_stencil_type_name();
72  ///@}
73 
74  /** @name use_high_pass
75  * When set the internally generated weights will be converted to a
76  * high_pass filter during weight generation. This option has no affect on
77  * user provided kernel weights.
78  */
79  ///@{
80  TECA_ALGORITHM_PROPERTY(int, use_high_pass)
81  ///@}
82 
83  /** @name kernel_width
84  * The number of samples to use when generating kernel weights. This option
85  * has no affect on user provided kernel weights.
86  */
87  ///@{
88  TECA_ALGORITHM_PROPERTY(unsigned int, kernel_width)
89  ///@}
90 
91  /** @name kernel_weights
92  * Set the kernel weights explicitly. The number of weights defines the
93  * stencil width and must be odd for a centered stencil.
94  */
95  ///@{
96  TECA_ALGORITHM_VECTOR_PROPERTY(double, kernel_weight)
97 
98  /// generate constant convolution kernel weights with the given filter width
99  int set_constant_kernel_weights(unsigned int width);
100 
101  /** generate Gaussian convolution kernel weights with the given filter
102  * width.
103  *
104  * @param[in] width the number of samples in the generated kernel
105  * @param[in] high_pass transform the weights for use in a high pass filter.
106  * @param[in] a peak height of the Gaussian
107  * @param[in] B center of the Gaussian (note coordinates range from -1 to 1.
108  * @param[in] c width of the Gaussian
109  */
110  int set_gaussian_kernel_weights(unsigned int width, int high_pass = 0,
111  double a = 1.0, double B = 0.0, double c = 0.55);
112 
113  /** set the kernel weights from a string name the kernel type. Default
114  * kernel weight generator parameters are used.
115  *
116  * @param[in] name the name of the kernel to generate. (gaussian, or constant)
117  * @param[in] width the width of the kernel.
118  * @param[in] high_pass convert the weights for use as a high pass filter.
119  */
120  int set_kernel_weights(const std::string &name,
121  unsigned int width, int high_pass);
122  ///@}
123 
124  /** @name kernel_name
125  * set the name of the user provided kernel, or the kernel to generate.
126  */
127  ///@{
128  TECA_ALGORITHM_PROPERTY(std::string, kernel_name)
129  ///@}
130 
131  /** @name variable_postfix
132  * a string to be appended to the name of each output variable setting this
133  * to an empty string will result in the damped array replacing the input
134  * array in the output. default is an empty string ""
135  */
136  ///@{
137  TECA_ALGORITHM_PROPERTY(std::string, variable_postfix)
138  ///@}
139 
140 protected:
142 
143 private:
144  using teca_algorithm::get_output_metadata;
145 
146  teca_metadata get_output_metadata(unsigned int port,
147  const std::vector<teca_metadata> &input_md) override;
148 
149  std::vector<teca_metadata> get_upstream_request(
150  unsigned int port, const std::vector<teca_metadata> &input_md,
151  const teca_metadata &request) override;
152 
153  const_p_teca_dataset execute(unsigned int port,
154  const std::vector<const_p_teca_dataset> &input_data,
155  const teca_metadata &request) override;
156 
157 private:
158  int stencil_type;
159  std::vector<double> kernel_weights;
160  std::string kernel_name;
161  std::string variable_postfix;
162  int use_high_pass;
163  unsigned int kernel_width;
164 };
165 
166 #endif
teca_time_axis_convolution::set_stencil_type_to_backward
void set_stencil_type_to_backward()
convolve with time steps before the active step
Definition: teca_time_axis_convolution.h:60
teca_metadata
A generic container for meta data in the form of name=value pairs.
Definition: teca_metadata.h:21
teca_time_axis_convolution::centered
@ centered
convolve with time steps before the active step
Definition: teca_time_axis_convolution.h:49
teca_time_axis_convolution
An algorithm that applies a convolution along the time axis.
Definition: teca_time_axis_convolution.h:30
teca_shared_object.h
teca_time_axis_convolution::set_stencil_type_to_centered
void set_stencil_type_to_centered()
convolve with time steps centered on the active step
Definition: teca_time_axis_convolution.h:64
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