RegionTextParser.h

Go to the documentation of this file.
00001 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00002 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00003 //# License for more details.
00004 //#
00005 //# You should have received a copy of the GNU Library General Public License
00006 //# along with this library; if not, write to the Free Software Foundation,
00007 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00008 //#
00009 //# Correspondence concerning AIPS++ should be addressed as follows:
00010 //#        Internet email: aips2-request@nrao.edu.
00011 //#        Postal address: AIPS++ Project Office
00012 //#                        National Radio Astronomy Observatory
00013 //#                        520 Edgemont Road
00014 //#                        Charlottesville, VA 22903-2475 USA
00015 //#
00016 
00017 #ifndef IMAGES_ASCIIANNOTATIONFILEPARSER_H
00018 #define IMAGES_ASCIIANNOTATIONFILEPARSER_H
00019 
00020 #include <casa/aips.h>
00021 #include <casa/Arrays/Vector.h>
00022 #include <casa/Containers/Record.h>
00023 #include <casa/Logging/LogIO.h>
00024 #include <casa/OS/RegularFile.h>
00025 #include <casa/Utilities/Regex.h>
00026 #include <coordinates/Coordinates/SpectralCoordinate.h>
00027 #include <imageanalysis/Annotations/AnnotationBase.h>
00028 #include <imageanalysis/IO/AsciiAnnotationFileLine.h>
00029 
00030 
00031 #include <coordinates/Coordinates/CoordinateSystem.h>
00032 
00033 namespace casa {
00034 
00035 // <summary>
00036 // Parse and store regions and annotations from an ascii region file
00037 // </summary>
00038 // <author>Dave Mehringer</author>
00039 // <use visibility=export>
00040 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00041 // </reviewed>
00042 // <prerequisite>
00043 
00044 // </prerequisite>
00045 
00046 // <etymology>
00047 // This is a class designed to parse and store regions and annotations from an ascii region file
00048 // </etymology>
00049 
00050 // <synopsis>
00051 // This class is for parsing and storing regions and annotations from an ascii region file .
00052 // See the region file format proposal attached to CAS-2285 (https://bugs.nrao.edu/browse/CAS-2285)
00053 // </synopsis>
00054 // <note>
00055 // This class will create AnnotationBase pointers via new(). It is assumed the caller will
00056 // make use of these pointers so they are not deleted upon deletion of the object. It is
00057 // the caller's responsibility to delete them. To do so, call getLines() and loop through
00058 // the returned Vector of AsciiRegionLines. For objects of type AsciiRegionLines::ANNOTATION,
00059 // get the pointer and delete it.
00060 
00061 class RegionTextParser {
00062 
00063 public:
00064 
00065         static const Int CURRENT_VERSION;
00066         static const Regex MAGIC;
00067 
00068         // because of nonstandard access patterns, be careful when using ParamValue and ParamSet
00069         // outside this class. These should probably be made into full fledged classes at some
00070         // point.
00071         struct ParamValue {
00072                 Double doubleVal;
00073                 Int intVal;
00074                 String stringVal;
00075                 Bool boolVal;
00076                 AnnotationBase::LineStyle lineStyleVal;
00077                 AnnotationBase::FontStyle fontStyleVal;
00078                 // Vector<MFrequency> freqRange;
00079                 SHARED_PTR<std::pair<MFrequency, MFrequency> > freqRange;
00080                 Vector<Stokes::StokesTypes> stokes;
00081                 AnnotationBase::RGB color;
00082                 vector<Int> intVec;
00083         };
00084         /*
00085         struct GlobalOverrideChans {
00086                 // the "classic" channel specification
00087                 String chanSpec;
00088                 // the number of spectral planes in the image
00089                 uInt nChannels;
00090                 // the image's spectral coordinate
00091                 SpectralCoordinate specCoord;
00092         };
00093         */
00094 
00095         using ParamSet = std::map<AnnotationBase::Keyword, ParamValue>;
00096 
00097         RegionTextParser() = delete;
00098 
00099 
00100 
00101 
00102         // <group>
00103         // differentiating between the filename and simple text constructors
00104         // <src>globalOverrideChans</src> override all spectral selections in the file
00105         // or text by using this channel selection<src>
00106         // <src>globalOverrideStokes</src> override all correlation selections in the file
00107         // or text by using this polarization selection<src>
00108         // <src>prependRegion</src> allows one to specify region(s) that will be prepended to
00109         // any text in <src>filename</src> or <src>text</src>
00110         RegionTextParser(
00111                 const String& filename, const CoordinateSystem& csys,
00112                 const IPosition& imShape, const Int requireAtLeastThisVersion,
00113                 const String& prependRegion="",
00114                 const String& globalOverrideChans="", const String& globalOverrrideStokes=""
00115         );
00116 
00117         RegionTextParser(
00118                 const CoordinateSystem& csys, const IPosition& imShape, const String& text,
00119                 const String& prependRegion="",
00120                 const String& globalOverrideChans="", const String& globalOverrrideStokes=""
00121         );
00122         //</group>
00123 
00124         ~RegionTextParser();
00125 
00126         RegionTextParser& operator=(const RegionTextParser&) = delete;
00127 
00128         Int getFileVersion() const;
00129 
00130         vector<AsciiAnnotationFileLine> getLines() const;
00131 
00132         // get the parameter set from a line of <src>text</src>. <src>preamble</src> is prepended to exception messages.
00133         static ParamSet getParamSet(
00134                 Bool& spectralParmsUpdated,
00135                 LogIO& log, const String& text, const String& preamble,
00136                 const CoordinateSystem& csys,
00137                 SHARED_PTR<std::pair<MFrequency, MFrequency> > overridingFreqRange,
00138                 SHARED_PTR<Vector<Stokes::StokesTypes> > overridingCorrRange
00139         );
00140 
00141 private:
00142 
00143         const static String sOnePair;
00144         const static String bTwoPair;
00145         const static String sNPair;
00146         const static Regex startOnePair;
00147         const static Regex startNPair;
00148 
00149         CoordinateSystem _csys;
00150         std::unique_ptr<LogIO> _log;
00151         ParamSet _currentGlobals;
00152         vector<AsciiAnnotationFileLine> _lines;
00153         Vector<AnnotationBase::Keyword> _globalKeysToApply;
00154         Int _fileVersion;
00155         IPosition _imShape;
00156         uInt _regions;
00157 
00158         SHARED_PTR<std::pair<MFrequency, MFrequency> > _overridingFreqRange;
00159         SHARED_PTR<Vector<Stokes::StokesTypes> > _overridingCorrRange;
00160 
00161         /*
00162         RegionTextParser() {}
00163 
00164         RegionTextParser& operator=(const RegionTextParser&);
00165         */
00166 
00167         void _parse(const String& contents, const String& fileDesc);
00168 
00169         Array<String> _extractTwoPairs(uInt& end, const String& string) const;
00170 
00171         // extract s1 and s2 from a string of the form "[s1, s2]"
00172         static Vector<String> _extractSinglePair(const String& string);
00173 
00174         void _addLine(const AsciiAnnotationFileLine& line);
00175 
00176         AnnotationBase::Type _getAnnotationType(
00177                 Vector<Quantity>& qDirs,
00178                 vector<Quantity>& qunatities,
00179                 String& textString,
00180                 String& consumeMe, const String& preamble
00181         ) const;
00182 
00183         ParamSet _getCurrentParamSet(
00184                 Bool& spectralParmsUpdated, ParamSet& newParams,
00185                 String& consumeMe, const String& preamble
00186         ) const;
00187 
00188         void _createAnnotation(
00189                 const AnnotationBase::Type annType,
00190                 //const Vector<MDirection> dirs,
00191                 const Vector<Quantity>& qDirs,
00192                 const std::pair<Quantity, Quantity>& qFreqs,
00193                 const vector<Quantity>& quantities,
00194                 const String& textString,
00195                 const ParamSet& currentParamSet,
00196                 const Bool annOnly, const Bool isDifference,
00197                 const String& preamble
00198         );
00199 
00200         std::pair<Quantity, Quantity> _quantitiesFromFrequencyString(
00201                 const String& freqString, const String& preamble
00202         ) const;
00203 
00204         static String _doLabel(String& consumeMe, const String& logPreamble);
00205 
00206         static String _getKeyValue(String& consumeMe, const String& preamble);
00207 
00208         Vector<Quantity> _extractQuantityPairAndSingleQuantity(
00209                 String& consumeMe, const String& preamble
00210         ) const;
00211 
00212         Vector<Quantity> _extractNQuantityPairs(
00213                         String& consumeMe, const String& preamble
00214         ) const;
00215 
00216         Vector<Quantity> _extractTwoQuantityPairs(
00217                 String& consumeMe, const String& preamble
00218         ) const;
00219 
00220         std::pair<Quantity, Quantity> _extractSingleQuantityPair(
00221                 const String& pair, const String& preamble
00222         ) const;
00223 
00224         void _setInitialGlobals();
00225 
00226         static Vector<Stokes::StokesTypes> _stokesFromString(
00227                 const String& stokes, const String& preamble
00228         );
00229 
00230         Vector<Quantity> _extractTwoQuantityPairsAndSingleQuantity(
00231                 String& consumeMe, const String& preamble
00232         ) const;
00233 
00234         void _extractQuantityPairAndString(
00235                 std::pair<Quantity, Quantity>& quantities, String& string,
00236                 String& consumeMe, const String& preamble,
00237                 const Bool requireQuotesAroundString
00238         ) const;
00239 
00240         Vector<Quantity> _extractQuantitiesFromPair(
00241                 const String& pair, const String& preamble
00242         ) const;
00243 
00244         void _determineVersion(
00245                 const String& chunk, const String& filename,
00246                 const Int requireAtLeastThisVersion
00247         );
00248 
00249         // set the Stokes/polarizations/correlations that will override all global and per line correlation
00250         // specifications. If multiple ranges are specified, an exception will be thrown.
00251         void _setOverridingCorrelations(const String& globalOverrideStokes);
00252 
00253         // set the (single) channel range that will override all global and per line frequency
00254         // specifications. If multiple ranges are specified, an exception will be thrown.
00255         void _setOverridingChannelRange(const String& globalOverrideChans);
00256 
00257 };
00258 }
00259 
00260 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1