00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef _CASA_SINGLEDISH_MS_H_
00029 #define _CASA_SINGLEDISH_MS_H_
00030
00031 #include <iostream>
00032 #include <string>
00033
00034 #include <casa/aipstype.h>
00035 #include <casa/Containers/Record.h>
00036 #include <libsakura/sakura.h>
00037 #include <ms/MeasurementSets/MeasurementSet.h>
00038 #include <msvis/MSVis/VisBuffer2.h>
00039 #include <scimath/Mathematics/FFTServer.h>
00040 #include <singledish/SingleDish/SDMSManager.h>
00041
00042 #define SinusoidWaveNumber_kUpperLimit -999
00043
00044 namespace casa {
00045
00046 class SingleDishMS {
00047 public:
00048
00049 SingleDishMS();
00050
00051 SingleDishMS(string const& ms_name);
00052
00053
00054 ~SingleDishMS();
00055
00056
00057
00058
00059 string name() const {
00060 return msname_;
00061 }
00062
00063 bool close();
00064
00065
00066
00067
00068
00069
00070 void setSelection(Record const& selection, bool const verbose = true);
00071
00072
00073
00074
00075 void setAverage(Record const& average, bool const verbose = true);
00076
00077
00078 void scale(float const factor, string const& in_column_name,
00079 string const& out_ms_name);
00080
00081
00082
00083 void subtractBaseline(string const& in_column_name,
00084 string const& out_ms_name,
00085 string const& out_bloutput_name,
00086 bool const& do_subtract,
00087 string const& in_spw,
00088 string const& blfunc,
00089 int const order,
00090 float const clip_threshold_sigma,
00091 int const num_fitting_max,
00092 bool const linefinding,
00093 float const threshold,
00094 int const avg_limit,
00095 int const minwidth,
00096 vector<int> const& edge);
00097
00098
00099 void subtractBaselineCspline(string const& in_column_name,
00100 string const& out_ms_name,
00101 string const& out_bloutput_name,
00102 bool const& do_subtract,
00103 string const& in_spw,
00104 int const npiece,
00105 float const clip_threshold_sigma,
00106 int const num_fitting_max,
00107 bool const linefinding,
00108 float const threshold,
00109 int const avg_limit,
00110 int const minwidth,
00111 vector<int> const& edge);
00112
00113
00114 void subtractBaselineSinusoid(string const& in_column_name,
00115 string const& out_ms_name,
00116 string const& out_bloutput_name,
00117 bool const& do_subtract,
00118 string const& in_spw,
00119 string const& addwn0,
00120 string const& rejwn0,
00121 bool const applyfft,
00122 string const fftmethod,
00123 string const fftthresh,
00124 float const clip_threshold_sigma,
00125 int const num_fitting_max,
00126 bool const linefinding,
00127 float const threshold,
00128 int const avg_limit,
00129 int const minwidth,
00130 vector<int> const& edge);
00131
00132
00133 void subtractBaselineVariable(string const& in_column_name,
00134 string const& out_ms_name,
00135 string const& out_bloutput_name,
00136 bool const& do_subtract,
00137 string const& in_spw,
00138 string const& param_file);
00139
00140
00141 void applyBaselineTable(string const& in_column_name,
00142 string const& in_bltable_name, string const& in_spw,
00143 string const& out_ms_name);
00144
00145
00146 void fitLine(string const& in_column_name, string const& in_spw,
00147 string const& in_pol, string const& fitfunc, string const& in_nfit,
00148 bool const linefinding, float const threshold, int const avg_limit,
00149 int const minwidth, vector<int> const& edge,
00150 string const& tempfile_name, string const& temp_out_ms_name);
00151
00152
00153
00154 void smooth(string const &kernelType, float const kernelWidth,
00155 string const &columnName, string const&outMsName);
00156
00157 private:
00161
00162
00163
00164 void initialize();
00165
00166
00167
00168
00169 void format_selection(Record &selection);
00170
00171
00172 String get_field_as_casa_string(Record const &in_data,
00173 string const &field_name);
00174
00175 bool prepare_for_process(string const &in_column_name,
00176 string const&out_ms_name);
00177
00178 bool prepare_for_process(string const &in_column_name,
00179 string const&out_ms_name, Block<Int> const &sortColumns,
00180 bool const addDefaultSortCols = false);
00181 void finalize_process();
00182
00183
00184
00185 bool set_column(MSMainEnums::PredefinedColumns const &in,
00186 MSMainEnums::PredefinedColumns &out);
00187
00188 void convertArrayC2F(Array<Float> &from, Array<Complex> const &to);
00189
00190 std::vector<string> split_string(string const &s, char delim);
00191
00192 bool file_exists(string const &filename);
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 void parse_spw(string const &in_spw, Vector<Int> &spw, Matrix<Int> &chan,
00207 Vector<size_t> &nchan, Vector<Vector<Bool> > &mask,
00208 Vector<bool> &nchan_set);
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 void get_nchan_and_mask(Vector<Int> const &rec_spw,
00232 Vector<Int> const &data_spw, Matrix<Int> const &rec_chan,
00233 size_t const num_chan, Vector<size_t> &nchan, Vector<Vector<Bool> > &mask,
00234 Vector<bool> &nchan_set, bool &new_nchan);
00235 void get_mask_from_rec(Int spwid, Matrix<Int> const &rec_chan,
00236 Vector<Bool> &mask, bool initialize = true);
00237 void get_masklist_from_mask(size_t const num_chan, bool const *mask,
00238 Vector<uInt> &masklist);
00239
00240 void get_baseline_context(size_t const bltype,
00241 uint16_t order,
00242 size_t num_chan,
00243 Vector<size_t> const &nchan,
00244 Vector<bool> const &nchan_set,
00245 Vector<size_t> &ctx_indices,
00246 std::vector<LIBSAKURA_SYMBOL(LSQFitContextFloat) *> &bl_contexts);
00247 void get_baseline_context(size_t const bltype,
00248 uint16_t order,
00249 size_t num_chan,
00250 size_t ispw,
00251 Vector<size_t> &ctx_indices,
00252 std::vector<size_t> &ctx_nchans,
00253 std::vector<LIBSAKURA_SYMBOL(LSQFitContextFloat) *> &bl_contexts);
00254
00255 void destroy_baseline_contexts(std::vector<LIBSAKURA_SYMBOL(LSQFitContextFloat) *> &bl_contexts);
00256 void check_sakura_status(string const &name, LIBSAKURA_SYMBOL(Status) const status);
00257 void check_baseline_status(LIBSAKURA_SYMBOL(LSQFitStatus) const bl_status);
00258 template<typename T, typename U>
00259 void set_matrix_for_bltable(size_t const num_pol, size_t const num_data_max,
00260 std::vector<std::vector<T> > const &in_data, Array<U> &out_data) {
00261 for (size_t ipol = 0; ipol < num_pol; ++ipol) {
00262 for (size_t i = 0; i < num_data_max; ++i) {
00263 out_data[i][ipol] = static_cast<U>(0);
00264 }
00265 size_t num_data = in_data[ipol].size();
00266 for (size_t i = 0; i < num_data; ++i) {
00267 out_data[i][ipol] = static_cast<U>(in_data[ipol][i]);
00268 }
00269 }
00270 }
00271 template<typename T, typename U>
00272 void set_array_for_bltable(size_t const ipol, size_t const num_data,
00273 T const *in_data, Array<U> &out_data) {
00274 for (size_t i = 0; i < num_data; ++i) {
00275 out_data[i][ipol] = static_cast<U>(in_data[i]);
00276 }
00277 }
00278 size_t get_num_coeff_bloutput(size_t const bltype,
00279 size_t order,
00280 size_t &num_coeff_max);
00281 vector<int> string_to_list(string const &wn_str, char const delim);
00282 void get_effective_nwave(std::vector<int> const &addwn,
00283 std::vector<int> const &rejwn,
00284 int const wn_ulimit,
00285 std::vector<int> &effwn);
00286 void finalise_effective_nwave(std::vector<int> const &blparam_eff_base,
00287 std::vector<int> const &blparam_exclude,
00288 int const &blparam_upperlimit,
00289 size_t const &num_chan,
00290 float const *spec, bool const *mask,
00291 bool const &applyfft,
00292 string const &fftmethod, string const &fftthresh,
00293 std::vector<size_t> &blparam_eff);
00294 void parse_fftthresh(string const& fftthresh_str,
00295 string& fftthresh_attr,
00296 float& fftthresh_sigma,
00297 int& fftthresh_top);
00298 void select_wavenumbers_via_fft(size_t const num_chan,
00299 float const *spec,
00300 bool const *mask,
00301 string const &fftmethod,
00302 string const &fftthresh_attr,
00303 float const fftthresh_sigma,
00304 int const fftthresh_top,
00305 int const blparam_upperlimit,
00306 std::vector<int> &blparam_fft);
00307 void exec_fft(size_t const num_chan,
00308 float const *in_spec,
00309 bool const *in_mask,
00310 bool const get_real_imag,
00311 bool const get_ampl_only,
00312 std::vector<float> &fourier_spec);
00313 void interpolate_constant(int const num_chan,
00314 float const *in_spec,
00315 bool const *in_mask,
00316 Vector<Float> &spec);
00317 void merge_wavenumbers(std::vector<int> const &blparam_eff_base,
00318 std::vector<int> const &blparam_fft,
00319 std::vector<int> const &blparam_exclude,
00320 std::vector<size_t> &blparam_eff);
00321
00322 list<pair<size_t, size_t>> findLineAndGetRanges(size_t const num_data,
00323 float const data[],
00324 bool mask[], float const threshold,
00325 int const avg_limit, int const minwidth, vector<int> const& edge,
00326 bool const invert);
00327
00328 void findLineAndGetMask(size_t const num_data, float const data[],
00329 bool const in_mask[], float const threshold,
00330 int const avg_limit, int const minwidth, vector<int> const& edge,
00331 bool const invert, bool out_mask[]);
00332
00333 template<typename Func0, typename Func1, typename Func2, typename Func3>
00334 void doSubtractBaseline(string const& in_column_name,
00335 string const& out_ms_name,
00336 string const& out_bloutput_name,
00337 bool const& do_subtract,
00338 string const& in_spw,
00339 LIBSAKURA_SYMBOL(Status)& status,
00340 std::vector<LIBSAKURA_SYMBOL(LSQFitContextFloat) *> &bl_contexts,
00341 size_t const bltype,
00342 vector<int> const& blparam,
00343 vector<int> const& blparam_exclude,
00344 bool const& applyfft,
00345 string const& fftmethod,
00346 string const& fftthresh,
00347 float const clip_threshold_sigma,
00348 int const num_fitting_max,
00349 bool const linefinding,
00350 float const threshold,
00351 int const avg_limit,
00352 int const minwidth,
00353 vector<int> const& edge,
00354 Func0 func0,
00355 Func1 func1,
00356 Func2 func2,
00357 Func3 func3,
00358 LogIO os);
00359
00363
00364 void get_spectrum_from_cube(Cube<Float> &data_cube, size_t const row,
00365 size_t const plane, size_t const num_data,
00366 float out_data[]);
00367
00368 void set_spectrum_to_cube(Cube<Float> &data_cube, size_t const row,
00369 size_t const plane, size_t const num_data, float in_data[]);
00370
00371
00372 void get_data_cube_float(vi::VisBuffer2 const &vb, Cube<Float> &data_cube);
00373
00374 void get_flag_cube(vi::VisBuffer2 const &vb, Cube<Bool> &flag_cube);
00375
00376 void get_flag_from_cube(Cube<Bool> &flag_cube, size_t const row,
00377 size_t const plane, size_t const num_flag,
00378 bool out_flag[]);
00379
00380 void set_flag_to_cube(Cube<Bool> &flag_cube, size_t const row,
00381 size_t const plane, size_t const num_flag, bool in_flag[]);
00382
00383
00384 void flag_spectrum_in_cube(Cube<Bool> &flag_cube, size_t const row,
00385 size_t const plane);
00386
00387
00388 bool allchannels_flagged(size_t const num_flag, bool const* flag);
00389
00390 size_t NValidMask(size_t const num_mask, bool const* mask);
00391
00395
00396 void do_scale(float const factor, size_t const num_data,
00397 float data[]);
00398
00402
00403 string msname_;
00404
00405 MSMainEnums::PredefinedColumns in_column_;
00406
00407 Record selection_;
00408
00409 Record average_;
00410
00411 SDMSManager *sdh_;
00412
00413 void (*visCubeAccessor_)(vi::VisBuffer2 const &vb, Cube<Float> &cube);
00414
00415 Bool doSmoothing_;
00416
00417 string bloutputname_csv;
00418 string bloutputname_text;
00419 string bloutputname_table;
00420
00421
00422 void split_bloutputname(string str);
00423
00424
00425 constexpr static Int kNRowBlocking = 1000;
00426
00427 public:
00428 static bool importAsap(string const &infile, string const &outfile, bool const parallel=false);
00429 static bool importNRO(string const &infile, string const &outfile, bool const parallel=false);
00430 };
00431
00432
00433 }
00434
00435 #endif