Line data Source code
1 : //# BLParameterParser.h: this code perses baseline fitting parameters
2 : //# Copyright (C) 2015
3 : //# National Astronomical Observatory of Japan
4 : //#
5 : //# This library is free software; you can redistribute it and/or modify it
6 : //# under the terms of the GNU Library General Public License as published by
7 : //# the Free Software Foundation; either version 2 of the License, or (at your
8 : //# option) any later version.
9 : //#
10 : //# This library is distributed in the hope that it will be useful, but WITHOUT
11 : //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 : //# License for more details.
14 : //#
15 : //# You should have received a copy of the GNU Library General Public License
16 : //# along with this library; if not, write to the Free Software Foundation,
17 : //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 : //#
19 : //# Correspondence concerning AIPS++ should be addressed as follows:
20 : //# Internet email: casa-feedback@nrao.edu.
21 : //# Postal address: AIPS++ Project Office
22 : //# National Radio Astronomy Observatory
23 : //# 520 Edgemont Road
24 : //# Charlottesville, VA 22903-2475 USA
25 : //#
26 : //# $Id$
27 : #ifndef _CASA_BLPARAMETER_PARSER_H_
28 : #define _CASA_BLPARAMETER_PARSER_H_
29 :
30 : #include <string>
31 : #include <map>
32 : #include <vector>
33 : //#include<cstdint> //with c++11 support
34 : #include <climits>
35 : #include <sstream>
36 :
37 : #include <casacore/casa/aipstype.h>
38 : #include <casacore/casa/Logging/LogIO.h>
39 : #include <casacore/casa/Logging/LogOrigin.h>
40 : #include <casacore/casa/Arrays/Vector.h>
41 : #include <libsakura/sakura.h>
42 : #include <singledish/SingleDish/BaselineTable.h>
43 :
44 : namespace casa { //# NAMESPACE CASA - BEGIN
45 :
46 : struct LineFinderParameter {
47 4576 : LineFinderParameter(bool const use_lf=false, float const thresh=5.0,
48 2288 : std::vector<size_t> const &edges=std::vector<size_t>(2,0),
49 : size_t const chavglim=0)
50 4576 : {
51 4576 : use_line_finder=use_lf;
52 4576 : threshold = thresh;
53 4576 : chan_avg_limit = chavglim;
54 13728 : for (size_t iedge=0; iedge < 2; ++ iedge) {
55 9152 : edge[iedge] = edges[iedge % edges.size()] ;
56 : }
57 4576 : }
58 : bool use_line_finder;
59 : float threshold;
60 : size_t edge[2];
61 : size_t chan_avg_limit;
62 : };
63 :
64 : struct BLParameterSet {
65 2288 : BLParameterSet(string const blmask="", uint16_t const nfit_max=0,
66 : float const clipthres=3.0,
67 : LineFinderParameter lf_param=LineFinderParameter(),
68 : size_t const bl_type = BaselineType_kNumElements,
69 : uint16_t const fit_order = USHRT_MAX, //UINT16_MAX,
70 : size_t const num_piece = USHRT_MAX, //SIZE_MAX,
71 : std::vector<size_t> const &nwaves = std::vector<size_t>()
72 : )
73 2288 : {
74 2288 : baseline_mask = blmask;
75 2288 : num_fitting_max = nfit_max;
76 2288 : clip_threshold_sigma = clipthres;
77 2288 : line_finder = lf_param;
78 2288 : baseline_type = bl_type;
79 2288 : order = fit_order;
80 2288 : npiece = num_piece;
81 2288 : nwave = nwaves;
82 2288 : }
83 :
84 0 : void PrintSummary() {
85 0 : casacore::LogIO os(casacore::LogOrigin("BLParameterSet","PrintSummary"));
86 0 : os << "- mask: " << baseline_mask << casacore::LogIO::POST;
87 0 : os << "- clip: iteration=" << num_fitting_max
88 0 : << ", threshold=" << clip_threshold_sigma << casacore::LogIO::POST;
89 : os
90 : << "- line finder: "
91 0 : << (line_finder.use_line_finder==1 ? "true" : "false")
92 0 : << ", threshold=" << line_finder.threshold
93 : << ", edge=[" << line_finder.edge[0] << ","
94 : << line_finder.edge[1] << "], chan_average="
95 0 : << line_finder.chan_avg_limit << casacore::LogIO::POST;
96 : os << "- baseline: type=" << baseline_type
97 0 : << ", order=" << order << ", npiece=" << npiece
98 0 : << casacore::LogIO::POST;
99 0 : }
100 :
101 : string baseline_mask;
102 : uint16_t num_fitting_max;
103 : float clip_threshold_sigma;
104 : LineFinderParameter line_finder;
105 : size_t baseline_type;
106 : uint16_t order; //initialize with invalid parameters
107 : size_t npiece;
108 : std::vector<size_t> nwave;
109 : };
110 :
111 :
112 : class BLParameterParser {
113 : public:
114 :
115 : explicit BLParameterParser(string const file_name);
116 : ~BLParameterParser();
117 :
118 : //Returns false if there is no fitting parameters for the row and pol.
119 : //Else, returns a baseline fitting parameter of a certain
120 : //row and pol IDs in MS
121 : bool GetFitParameter(size_t const rowid,size_t const polid,
122 : BLParameterSet &bl_param);
123 :
124 : //Returns the name of file that stores
125 : inline string get_file_name(){return blparam_file_;};
126 : //Returns a list of baseline type in the file
127 93 : inline std::vector<size_t> get_function_types(){return baseline_types_;};
128 : //Returns the maximum fit order of specified baseline type to construct BaselineContext
129 : uint16_t get_max_order(size_t const bltype);
130 :
131 : protected:
132 : //private:
133 : void initialize();
134 : void Clearup();
135 : // parse a file
136 : void parse(string const file_name);
137 : // split string by separator character
138 : void SplitLine(string const &linestr, char const separator,
139 : std::vector<string> &strvec);
140 : // convert a line of string to a BLParameterSet data structure
141 : void ConvertLineToParam(string const &linestr, size_t &rowid,
142 : size_t &polid, BLParameterSet ¶mset);
143 : //Returns order or npiece in BLParameterSet structure depending on datatype.
144 : uint16_t GetTypeOrder(BLParameterSet const &bl_param);
145 :
146 : // Member variables
147 : string blparam_file_;
148 : std::map<const std::pair<size_t, size_t>, BLParameterSet*> bl_parameters_;
149 : std::vector<size_t> baseline_types_;
150 : uint16_t max_orders_[BaselineType_kNumElements];
151 : // The enum for columns in fitting parameter file
152 : typedef enum {BLParameters_kRow = 0,
153 : BLParameters_kPol,
154 : BLParameters_kMask,
155 : BLParameters_kNumIteration,
156 : BLParameters_kClipThreshold,
157 : BLParameters_kLineFinder,
158 : BLParameters_kLFThreshold,
159 : BLParameters_kLeftEdge,
160 : BLParameters_kRightEdge,
161 : BLParameters_kChanAverageLim,
162 : BLParameters_kBaselineType,
163 : BLParameters_kOrder,
164 : BLParameters_kNPiece,
165 : BLParameters_kNWave,
166 : BLParameters_kNumElements
167 : } BLParameters;
168 :
169 : //
170 : template<typename DataType>
171 1588 : inline DataType ConvertString(string const &svalue)
172 : {
173 : DataType out_value;
174 3176 : std::istringstream istr(svalue.data());
175 1588 : istr >> out_value;
176 1588 : return out_value;
177 1588 : };
178 :
179 : }; // class BLParameterParser -END
180 :
181 : struct BLIdSet {};
182 :
183 : class BLTableParser : public BLParameterParser {
184 : public:
185 : explicit BLTableParser(string const file_name);
186 : ~BLTableParser();
187 : bool GetFitParameterIdx(double const time, double const interval,
188 : size_t const scanid, size_t const beamid,
189 : size_t const antid, size_t const spwid, size_t &idx);
190 : void GetFitParameterByIdx(size_t const idx, size_t const ipol,
191 : bool &apply,
192 : casacore::Vector<double> &coeff,
193 : casacore::Vector<size_t> &boundary,
194 : std::vector<bool> &masklist,
195 : BLParameterSet &bl_param);
196 : private:
197 : void initialize();
198 : void parse();
199 : //Returns order or npiece in BLParameterSet structure depending on datatype.
200 : uint16_t GetTypeOrder(size_t const &baseline_type,
201 : casacore::uInt const irow, casacore::uInt const ipol);
202 : BaselineTable *bt_;
203 : std::map<string, std::vector<double> > sortedTimes_;
204 : std::map<string, std::vector<casacore::uInt> > timeSortedIdx_;
205 : std::map<string, size_t> numRows_;
206 : };
207 :
208 : } //# NAMESPACE CASA - END
209 :
210 : #endif /* _CASA_BLPARAMETER_PARSER_H_ */
|