SingleDishMS.h

Go to the documentation of this file.
00001 //# SingleDishMS.h: this defines a class that handles single dish MSes
00002 //#
00003 //# Copyright (C) 2014,2015
00004 //# National Astronomical Observatory of Japan
00005 //#
00006 //# This library is free software; you can redistribute it and/or modify it
00007 //# under the terms of the GNU Library General Public License as published by
00008 //# the Free Software Foundation; either version 2 of the License, or (at your
00009 //# option) any later version.
00010 //#
00011 //# This library is distributed in the hope that it will be useful, but WITHOUT
00012 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00014 //# License for more details.
00015 //#
00016 //# You should have received a copy of the GNU Library General Public License
00017 //# along with this library; if not, write to the Free Software Foundation,
00018 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00019 //#
00020 //# Correspondence concerning AIPS++ should be addressed as follows:
00021 //#        Internet email: aips2-request@nrao.edu.
00022 //#        Postal address: AIPS++ Project Office
00023 //#                        National Radio Astronomy Observatory
00024 //#                        520 Edgemont Road
00025 //#                        Charlottesville, VA 22903-2475 USA
00026 //#
00027 //# $Id$
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 { //# NAMESPACE CASA - BEGIN
00045 
00046 class SingleDishMS {
00047 public:
00048   // Default constructor
00049   SingleDishMS();
00050   // Construct from MS name string
00051   SingleDishMS(string const& ms_name);
00052 
00053   // Destructor
00054   ~SingleDishMS();
00055 
00056   /* 
00057    * Return the name of the MeasurementSet
00058    */
00059   string name() const {
00060     return msname_;
00061   }
00062 
00063   bool close();
00064 
00065   /*
00066    * Formats selection parameters for single dish processing.
00067    * @param [in] selection A Record consists of selection key and values
00068    * @param [in] verbose If true, print summary of selection logger
00069    */
00070   void setSelection(Record const& selection, bool const verbose = true);
00071 
00072   /*
00073    * Set time averaging parameters.
00074    */
00075   void setAverage(Record const& average, bool const verbose = true);
00076 
00077   // Multiply a scale factor to selected spectra
00078   void scale(float const factor, string const& in_column_name,
00079       string const& out_ms_name);
00080 
00081   // Invoke baseline subtraction
00082   // (polynomial, write results in new MS)
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   //Cubicspline  
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   //Sinusoid  
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   // variable fitting parameters stored in a text file
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   // apply baseline table
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   // fit line profile
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   // smooth data with arbitrary smoothing kernel
00153   // smoothing kernels currently supported include gaussian and boxcar
00154   void smooth(string const &kernelType, float const kernelWidth,
00155       string const &columnName, string const&outMsName);
00156 
00157 private:
00161   /*
00162    *  Initializes member variables: in_column_
00163    */
00164   void initialize();
00165   /*
00166    * Formats selection parameters for single dish processing.
00167    * @param [in] selection A Record consists of selection key and values
00168    */
00169   void format_selection(Record &selection);
00170 
00171   // retrieve a field by name from Record as casa::String.
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   // check column 'in' is in input MS and set to 'out' if it exists.
00184   // if not, out is set to MS::UNDEFINED_COLUMN
00185   bool set_column(MSMainEnums::PredefinedColumns const &in,
00186       MSMainEnums::PredefinedColumns &out);
00187   // Convert a Complex Array to Float Array
00188   void convertArrayC2F(Array<Float> &from, Array<Complex> const &to);
00189   // Split a string with given delimiter
00190   std::vector<string> split_string(string const &s, char delim);
00191   // examine if a file with specified name exists
00192   bool file_exists(string const &filename);
00193   /* Convert spw selection string to vectors of spwid and channel
00194      ranges by parsing msseltoindex output. Also creates some
00195      placeholder vectors to store mask and the muber of channels
00196      in each selected SPW.
00197      [in] in_spw: input SPW selection string
00198      [out] spw : a vector of selected SPW IDs. the number of
00199                  elements is the number of selected SPWs
00200      [out] chan : a vector of selected SPW IDs and channel ranges
00201                   returned by msseltoindex. [[SPWID, start, end, stride], ...]
00202      [out] nchan : a vector of length spw.size() initialized by zero
00203      [out] mask : an uninitialized vector of length spw.size() 
00204      [out] nchan_set: a vector of length spw.size() initilazed by false
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   /* Go through chunk and set valudes to nchan and mask selection
00210      vectors of the SPW if not already done.
00211     [in] rec_spw: a vector of selected SPW IDs. the number of
00212                  elements is the number of selected SPWs
00213     [in] data_spw: a vector of SPW IDs in current chunk. the
00214                    number of elements is equals to the number
00215                    of rows in the chunk.
00216     [in] rec_chan: a vector of selected SPW IDs and channel ranges
00217                   returned by msseltoindex. [[SPWID, start, end, stride], ...]
00218     [in] num_chan: a vector of the number of channels in current chunk.
00219                    the number of elements is equals to the number
00220                    of rows in the chunk.
00221     [out] nchan: a vector of length spw.size(). the number of channels 
00222                  in the corresponding SPW is set when the loop traverses
00223                  the SPW for the first time.
00224     [out] mask: a vector of length spw.size().
00225     [in,out] nchan_set: a boolean vector of length spw.size().
00226                         the value indicates if nchan, and mask of
00227                         the corresponding SPW is already set.
00228     [out] new_nchan: returns true if this is the first time finding
00229                      SPWs with the same number of channels.
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   // Create a set of baseline contexts (if necessary)
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   // Destroy a set of baseline contexts
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[/*num_data*/],
00324       bool mask[/*num_data*/], 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[/*num_data*/],
00329       bool const in_mask[/*num_data*/], float const threshold,
00330       int const avg_limit, int const minwidth, vector<int> const& edge,
00331       bool const invert, bool out_mask[/*num_data*/]);
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   // retrieve a spectrum at the row and plane (polarization) from data cube
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[/*num_data*/]);
00367   // set a spectrum at the row and plane (polarization) to data cube
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[/*num_data*/]);
00370   // get data cube (npol*nchan*nvirow) in in_column_ from visbuffer
00371   // and convert it to float cube
00372   void get_data_cube_float(vi::VisBuffer2 const &vb, Cube<Float> &data_cube);
00373   // get flag cube (npol*nchan*nvirow) from visbuffer
00374   void get_flag_cube(vi::VisBuffer2 const &vb, Cube<Bool> &flag_cube);
00375   // retrieve a flag at the row and plane (polarization) from flag cube
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[/*num_flag*/]);
00379   // set a flag at the row and plane (polarization) to flag cube
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[/*num_data*/]);
00382 
00383   // flag all channels in a supectrum in cube at the row and plane (polarization)
00384   void flag_spectrum_in_cube(Cube<Bool> &flag_cube, size_t const row,
00385       size_t const plane);
00386 
00387   // return true if all channels are flagged
00388   bool allchannels_flagged(size_t const num_flag, bool const* flag);
00389   // returns the number of channels with true in input mask
00390   size_t NValidMask(size_t const num_mask, bool const* mask);
00391 
00395   // multiply a scaling factor to a float array
00396   void do_scale(float const factor, size_t const num_data,
00397       float data[/*num_data*/]);
00398 
00402   // the name of input MS
00403   string msname_;
00404   // columns to read and save data
00405   MSMainEnums::PredefinedColumns in_column_;  //, out_column_;
00406   // Record of selection
00407   Record selection_;
00408   // Record of average
00409   Record average_;
00410   // SDMSManager
00411   SDMSManager *sdh_;
00412   // pointer to accessor function
00413   void (*visCubeAccessor_)(vi::VisBuffer2 const &vb, Cube<Float> &cube);
00414   // smoothing flag
00415   Bool doSmoothing_;
00416 
00417   string bloutputname_csv;
00418   string bloutputname_text;
00419   string bloutputname_table;
00420 
00421   //split the name  
00422   void split_bloutputname(string str);
00423 
00424   //max number of rows to get in each iteration
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 // class SingleDishMS -END
00432 
00433 } //# NAMESPACE CASA - END
00434 
00435 #endif /* _CASA_SINGLEDISH_MS_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1