BLParameterParser.h

Go to the documentation of this file.
00001 //# BLParameterParser.h: this code perses baseline fitting parameters
00002 //# Copyright (C) 2015
00003 //# National Astronomical Observatory of Japan
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id$
00027 #ifndef _CASA_BLPARAMETER_PARSER_H_
00028 #define _CASA_BLPARAMETER_PARSER_H_
00029 
00030 #include <string>
00031 #include <map>
00032 #include <vector>
00033 //#include<cstdint> //with c++11 support
00034 #include <climits>
00035 #include <sstream>
00036 
00037 #include <casa/aipstype.h>
00038 #include <casa/Logging/LogIO.h>
00039 #include <casa/Logging/LogOrigin.h>
00040 #include <libsakura/sakura.h>
00041 #include <singledish/SingleDish/BaselineTable.h>
00042 
00043 namespace casa { //# NAMESPACE CASA - BEGIN
00044 
00045 struct LineFinderParameter {
00046   LineFinderParameter(bool const use_lf=false, float const thresh=5.0,
00047                       std::vector<size_t> const &edges=std::vector<size_t>(2,0),
00048                       size_t const chavglim=0)
00049   {
00050     use_line_finder=use_lf;
00051     threshold = thresh;
00052     chan_avg_limit = chavglim;
00053     for (size_t iedge=0; iedge < 2; ++ iedge) {
00054         edge[iedge] = edges[iedge % edges.size()] ;
00055     }
00056   }
00057   bool use_line_finder;
00058   float threshold;
00059   size_t edge[2];
00060   size_t chan_avg_limit;
00061 };
00062 
00063 struct BLParameterSet {
00064   BLParameterSet(string const blmask="", uint16_t const nfit_max=0,
00065                  float const clipthres=3.0,
00066                  LineFinderParameter lf_param=LineFinderParameter(),
00067                  size_t const bl_type = BaselineType_kNumElements,
00068                  uint16_t const fit_order = USHRT_MAX, //UINT16_MAX,
00069                  size_t const num_piece = USHRT_MAX, //SIZE_MAX,
00070                  std::vector<size_t> const &nwaves = std::vector<size_t>()
00071                  )
00072   {
00073     baseline_mask = blmask;
00074     num_fitting_max = nfit_max;
00075     clip_threshold_sigma = clipthres;
00076     line_finder = lf_param;
00077     baseline_type = bl_type;
00078     order = fit_order;
00079     npiece = num_piece;
00080     nwave = nwaves;
00081   }
00082 
00083   void PrintSummary() {
00084     LogIO os(LogOrigin("BLParameterSet","PrintSummary"));
00085     os << "- mask: " << baseline_mask << LogIO::POST;
00086     os << "- clip: iteration=" << num_fitting_max
00087        << ", threshold=" << clip_threshold_sigma << LogIO::POST;
00088     os 
00089       << "- line finder: "
00090       << (line_finder.use_line_finder==1 ? "true" : "false")
00091       << ", threshold=" << line_finder.threshold
00092       << ", edge=[" << line_finder.edge[0] << ","
00093       << line_finder.edge[1] << "], chan_average="
00094       << line_finder.chan_avg_limit << LogIO::POST;
00095     os << "- baseline: type=" << baseline_type
00096        << ", order=" << order << ", npiece=" << npiece
00097        << LogIO::POST;
00098   }
00099 
00100   string baseline_mask;
00101   uint16_t num_fitting_max;
00102   float clip_threshold_sigma;
00103   LineFinderParameter line_finder;
00104   size_t baseline_type;
00105   uint16_t order; //initialize with invalid parameters
00106   size_t npiece;
00107   std::vector<size_t> nwave;
00108 };
00109 
00110 
00111 class BLParameterParser {
00112 public:
00113 
00114   explicit BLParameterParser(string const file_name);
00115   ~BLParameterParser();
00116 
00117   //Returns false if there is no fitting parameters for the row and pol.
00118   //Else, returns a baseline fitting parameter of a certain
00119   //row and pol IDs in MS
00120   bool GetFitParameter(size_t const rowid,size_t const polid,
00121                        BLParameterSet &bl_param);
00122 
00123   //Returns the name of file that stores 
00124   inline string get_file_name(){return blparam_file_;};
00125   //Returns a list of baseline type in the file
00126   inline std::vector<size_t> get_function_types(){return baseline_types_;};
00127   //Returns the maximum fit order of specified baseline type to construct BaselineContext
00128   uint16_t get_max_order(size_t const bltype);
00129 
00130 protected:
00131   //private:
00132   void initialize();
00133   void Clearup();
00134   // parse a file
00135   void parse(string const file_name);
00136   // split string by separator character
00137   void SplitLine(string const &linestr, char const separator,
00138                  std::vector<string> &strvec);
00139   // convert a line of string to a BLParameterSet data structure
00140   void ConvertLineToParam(string const &linestr, size_t &rowid,
00141                           size_t &polid, BLParameterSet &paramset);
00142   //Returns order or npiece in BLParameterSet structure depending on datatype.
00143   uint16_t GetTypeOrder(BLParameterSet const &bl_param);
00144 
00145   // Member variables
00146   string blparam_file_;
00147   std::map<const std::pair<size_t, size_t>, BLParameterSet*> bl_parameters_;
00148   std::vector<size_t> baseline_types_;
00149   uint16_t max_orders_[BaselineType_kNumElements];
00150   // The enum for columns in fitting parameter file
00151   typedef enum {BLParameters_kRow = 0,
00152                 BLParameters_kPol,
00153                 BLParameters_kMask,
00154                 BLParameters_kNumIteration,
00155                 BLParameters_kClipThreshold,
00156                 BLParameters_kLineFinder,
00157                 BLParameters_kLFThreshold,
00158                 BLParameters_kLeftEdge,
00159                 BLParameters_kRightEdge,
00160                 BLParameters_kChanAverageLim,
00161                 BLParameters_kBaselineType,
00162                 BLParameters_kOrder,
00163                 BLParameters_kNPiece,
00164                 BLParameters_kNWave,
00165                 BLParameters_kNumElements
00166   } BLParameters;
00167 
00168   //
00169   template<typename DataType>
00170   inline DataType ConvertString(string const &svalue)
00171   {
00172     DataType out_value;
00173     std::istringstream istr(svalue.data());
00174     istr >> out_value;
00175     return out_value;
00176   };
00177 
00178 }; // class BLParameterParser -END
00179 
00180 struct BLIdSet {};
00181 
00182 class BLTableParser : public BLParameterParser {
00183  public:
00184   explicit BLTableParser(string const file_name);
00185   ~BLTableParser();
00186   bool GetFitParameterIdx(double const time, double const interval, 
00187                           size_t const scanid, size_t const beamid, 
00188                           size_t const antid, size_t const spwid, size_t &idx);
00189   void GetFitParameterByIdx(size_t const idx, size_t const ipol, 
00190                             bool &apply, std::vector<float> &coeff, 
00191                             std::vector<double> &boundary, 
00192                             BLParameterSet &bl_param);
00193  private:
00194   void initialize();
00195   void parse();
00196   //Returns order or npiece in BLParameterSet structure depending on datatype.
00197   uint16_t GetTypeOrder(size_t const &baseline_type, 
00198                         uInt const irow, uInt const ipol);
00199   BaselineTable *bt_;
00200   std::map<string, std::vector<double> > sortedTimes_;
00201   std::map<string, std::vector<uInt> > timeSortedIdx_;
00202   std::map<string, size_t> numRows_;
00203 };
00204 
00205 } //# NAMESPACE CASA - END
00206   
00207 #endif /* _CASA_BLPARAMETER_PARSER_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1