casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
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 <imageanalysis/Annotations/AnnotationBase.h>
00027 #include <imageanalysis/IO/AsciiAnnotationFileLine.h>
00028 
00029 
00030 #include <coordinates/Coordinates/CoordinateSystem.h>
00031 
00032 namespace casa {
00033 
00034 // <summary>
00035 // Parse and store regions and annotations from an ascii region file
00036 // </summary>
00037 // <author>Dave Mehringer</author>
00038 // <use visibility=export>
00039 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00040 // </reviewed>
00041 // <prerequisite>
00042 
00043 // </prerequisite>
00044 
00045 // <etymology>
00046 // This is a class designed to parse and store regions and annotations from an ascii region file
00047 // </etymology>
00048 
00049 // <synopsis>
00050 // This class is for parsing and storing regions and annotations from an ascii region file .
00051 // See the region file format proposal attached to CAS-2285 (https://bugs.nrao.edu/browse/CAS-2285)
00052 // </synopsis>
00053 // <note>
00054 // This class will create AnnotationBase pointers via new(). It is assumed the caller will
00055 // make use of these pointers so they are not deleted upon deletion of the object. It is
00056 // the caller's responsibility to delete them. To do so, call getLines() and loop through
00057 // the returned Vector of AsciiRegionLines. For objects of type AsciiRegionLines::ANNOTATION,
00058 // get the pointer and delete it.
00059 
00060 class RegionTextParser {
00061 
00062 public:
00063 
00064         static const Int CURRENT_VERSION;
00065         static const Regex MAGIC;
00066 
00067         // because of nonstandard access patterns, be careful when using ParamValue and ParamSet
00068         // outside this class. These should probably be made into full fledged classes at some
00069         // point.
00070         struct ParamValue {
00071                 Double doubleVal;
00072                 Int intVal;
00073                 String stringVal;
00074                 Bool boolVal;
00075                 AnnotationBase::LineStyle lineStyleVal;
00076                 AnnotationBase::FontStyle fontStyleVal;
00077                 Vector<MFrequency> freqRange;
00078                 Vector<Stokes::StokesTypes> stokes;
00079                 AnnotationBase::RGB color;
00080                 vector<Int> intVec;
00081         };
00082 
00083         typedef std::map<AnnotationBase::Keyword, ParamValue> ParamSet;
00084 
00085         // <group>
00086         // differentiating between the filename and simple text constructors
00087         RegionTextParser(
00088                 const String& filename, const CoordinateSystem& csys,
00089                 const IPosition& imShape,
00090                 const Int requireAtLeastThisVersion
00091         );
00092 
00093         RegionTextParser(
00094                 const CoordinateSystem& csys, const IPosition& imShape, const String& text
00095         );
00096         //</group>
00097 
00098         ~RegionTextParser();
00099 
00100         Int getFileVersion() const;
00101 
00102         Vector<AsciiAnnotationFileLine> getLines() const;
00103 
00104         // get the parameter set from a line of <src>text</src>. <src>preamble</src> is prepended to exception messages.
00105         static ParamSet getParamSet(
00106                 Bool& spectralParmsUpdated,
00107                 LogIO& log, const String& text, const String& preamble,
00108                 const CoordinateSystem& csys
00109         );
00110 
00111 private:
00112 
00113         const static String sOnePair;
00114         const static String bTwoPair;
00115         const static String sNPair;
00116         const static Regex startOnePair;
00117         const static Regex startNPair;
00118 
00119         CoordinateSystem _csys;
00120         std::auto_ptr<LogIO> _log;
00121         ParamSet _currentGlobals;
00122         Vector<AsciiAnnotationFileLine> _lines;
00123         Vector<AnnotationBase::Keyword> _globalKeysToApply;
00124         Int _fileVersion;
00125         IPosition _imShape;
00126         uInt _regions;
00127 
00128         RegionTextParser() {}
00129 
00130         RegionTextParser& operator=(const RegionTextParser&);
00131 
00132         void _parse(const String& contents, const String& fileDesc);
00133 
00134         Array<String> _extractTwoPairs(uInt& end, const String& string) const;
00135 
00136         // extract s1 and s2 from a string of the form "[s1, s2]"
00137         static Vector<String> _extractSinglePair(const String& string);
00138 
00139         void _addLine(const AsciiAnnotationFileLine& line);
00140 
00141         AnnotationBase::Type _getAnnotationType(
00142                 Vector<Quantity>& qDirs,
00143                 Vector<Quantity>& qunatities,
00144                 String& textString,
00145                 String& consumeMe, const String& preamble
00146         ) const;
00147 
00148         ParamSet _getCurrentParamSet(
00149                 Bool& spectralParmsUpdated, ParamSet& newParams,
00150                 String& consumeMe, const String& preamble
00151         ) const;
00152 
00153         void _createAnnotation(
00154                 const AnnotationBase::Type annType,
00155                 //const Vector<MDirection> dirs,
00156                 const Vector<Quantity>& qDirs,
00157                 const Vector<Quantity>& qFreqs,
00158                 const Vector<Quantity>& quantities,
00159                 const String& textString,
00160                 const ParamSet& currentParamSet,
00161                 const Bool annOnly, const Bool isDifference,
00162                 const String& preamble
00163         );
00164 
00165         Vector<Quantity> _quantitiesFromFrequencyString(
00166                 const String& freqString,
00167                 const String& preamble
00168         ) const;
00169 
00170         static String _doLabel(String& consumeMe, const String& logPreamble);
00171 
00172         static String _getKeyValue(String& consumeMe, const String& preamble);
00173 
00174         Vector<Quantity> _extractQuantityPairAndSingleQuantity(
00175                 String& consumeMe, const String& preamble
00176         ) const;
00177 
00178         Vector<Quantity> _extractNQuantityPairs(
00179                         String& consumeMe, const String& preamble
00180         ) const;
00181 
00182         Vector<Quantity> _extractTwoQuantityPairs(
00183                 String& consumeMe, const String& preamble
00184         ) const;
00185 
00186         Vector<Quantity> _extractSingleQuantityPair(
00187                 const String& pair, const String& preamble
00188         ) const;
00189 
00190         void _setInitialGlobals();
00191 
00192         static Vector<Stokes::StokesTypes> _stokesFromString(
00193                 const String& stokes, const String& preamble
00194         );
00195 
00196         Vector<Quantity> _extractTwoQuantityPairsAndSingleQuantity(
00197                 String& consumeMe, const String& preamble
00198         ) const;
00199 
00200         void _extractQuantityPairAndString(
00201                 Vector<Quantity>& quantities, String& string,
00202                 String& consumeMe, const String& preamble,
00203                 const Bool requireQuotesAroundString
00204         ) const;
00205 
00206         Vector<Quantity> _extractQuantitiesFromPair(
00207                 const String& pair, const String& preamble
00208         ) const;
00209 
00210         void _determineVersion(
00211                 const String& chunk, const String& filename,
00212                 const Int requireAtLeastThisVersion
00213         );
00214 
00215 
00216 };
00217 }
00218 
00219 #endif