casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RegionTextParser.h
Go to the documentation of this file.
1 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
3 //# License for more details.
4 //#
5 //# You should have received a copy of the GNU Library General Public License
6 //# along with this library; if not, write to the Free Software Foundation,
7 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
8 //#
9 //# Correspondence concerning AIPS++ should be addressed as follows:
10 //# Internet email: aips2-request@nrao.edu.
11 //# Postal address: AIPS++ Project Office
12 //# National Radio Astronomy Observatory
13 //# 520 Edgemont Road
14 //# Charlottesville, VA 22903-2475 USA
15 //#
16 
17 #ifndef IMAGES_ASCIIANNOTATIONFILEPARSER_H
18 #define IMAGES_ASCIIANNOTATIONFILEPARSER_H
19 
20 #include <casa/aips.h>
21 #include <casa/Arrays/Vector.h>
22 #include <casa/Containers/Record.h>
23 #include <casa/Logging/LogIO.h>
24 #include <casa/OS/RegularFile.h>
25 #include <casa/Utilities/Regex.h>
29 
30 
32 
33 namespace casa {
34 
35 // <summary>
36 // Parse and store regions and annotations from an ascii region file
37 // </summary>
38 // <author>Dave Mehringer</author>
39 // <use visibility=export>
40 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
41 // </reviewed>
42 // <prerequisite>
43 
44 // </prerequisite>
45 
46 // <etymology>
47 // This is a class designed to parse and store regions and annotations from an ascii region file
48 // </etymology>
49 
50 // <synopsis>
51 // This class is for parsing and storing regions and annotations from an ascii region (CRTF) file.
52 // The format spec can be found at
53 // https://casa.nrao.edu/casadocs-devel/stable/imaging/image-analysis/region-file-format
54 // </synopsis>
55 
57 
58 public:
59 
61  static const casacore::Regex MAGIC;
62 
63  // because of nonstandard access patterns, be careful when using ParamValue and ParamSet
64  // outside this class. These should probably be made into full fledged classes at some
65  // point.
66  struct ParamValue {
73  std::shared_ptr<std::pair<casacore::MFrequency, casacore::MFrequency> > freqRange;
76  std::vector<casacore::Int> intVec;
77  };
78 
79  using ParamSet = std::map<AnnotationBase::Keyword, ParamValue>;
80 
81  RegionTextParser() = delete;
82 
83  // <group>
84  // differentiating between the filename and simple text constructors
85  // <src>globalOverrideChans</src> override all spectral selections in the file
86  // or text by using this channel selection<src>
87  // <src>globalOverrideStokes</src> override all correlation selections in the file
88  // or text by using this polarization selection<src>
89  // <src>prependRegion</src> allows one to specify region(s) that will be prepended to
90  // any text in <src>filename</src> or <src>text</src>
91  // <src>requireImageRegion</src> is passed to the AnnRegion constructors
92  // to indicate whether to rethrow ToLCRegionConversionError exception when
93  // the region is outside the image lattice, or create the object even if
94  // the ImageRegion has no lattice region. The default (true) rethrows the exception.
95  // CAS-12631: added for CARTA, which can import regions outside an image.
97  const casacore::String& filename, const casacore::CoordinateSystem& csys,
98  const casacore::IPosition& imShape, const casacore::Int requireAtLeastThisVersion,
99  const casacore::String& prependRegion="",
100  const casacore::String& globalOverrideChans="",
101  const casacore::String& globalOverrrideStokes="",
102  casacore::Bool verbose=true,
103  casacore::Bool requireImageRegion=true
104  );
105 
107  const casacore::CoordinateSystem& csys,
108  const casacore::IPosition& imShape,
109  const casacore::String& text,
110  const casacore::String& prependRegion="",
111  const casacore::String& globalOverrideChans="",
112  const casacore::String& globalOverrrideStokes="",
113  casacore::Bool verbose=true,
114  casacore::Bool requireImageRegion=true
115  );
116  //</group>
117 
119 
120  RegionTextParser& operator=(const RegionTextParser&) = delete;
121 
123 
124  std::vector<AsciiAnnotationFileLine> getLines() const;
125 
126  // get the parameter set from a line of <src>text</src>. <src>preamble</src> is prepended to exception messages.
127  static ParamSet getParamSet(
128  casacore::Bool& spectralParmsUpdated,
129  casacore::LogIO& log, const casacore::String& text, const casacore::String& preamble,
130  const casacore::CoordinateSystem& csys,
131  std::shared_ptr<std::pair<casacore::MFrequency, casacore::MFrequency> > overridingFreqRange,
132  std::shared_ptr<casacore::Vector<casacore::Stokes::StokesTypes> > overridingCorrRange
133  );
134 
135 private:
136 
139  const static casacore::String sNPair;
142 
144  std::unique_ptr<casacore::LogIO> _log;
146  std::vector<AsciiAnnotationFileLine> _lines;
152 
153  std::shared_ptr<std::pair<casacore::MFrequency, casacore::MFrequency> > _overridingFreqRange;
154 
155  std::shared_ptr<casacore::Vector<casacore::Stokes::StokesTypes> > _overridingCorrRange;
156 
157  void _parse(const casacore::String& contents, const casacore::String& fileDesc, casacore::Bool requireImageRegion);
158 
160 
161  // extract s1 and s2 from a string of the form "[s1, s2]"
163 
164  void _addLine(const AsciiAnnotationFileLine& line);
165 
168  std::vector<casacore::Quantity>& quantities,
169  casacore::String& textString,
170  casacore::String& consumeMe, const casacore::String& preamble
171  ) const;
172 
174  casacore::Bool& spectralParmsUpdated, ParamSet& newParams,
175  casacore::String& consumeMe, const casacore::String& preamble
176  ) const;
177 
178  void _createAnnotation(
179  const AnnotationBase::Type annType,
180  //const casacore::Vector<casacore::MDirection> dirs,
182  const std::pair<casacore::Quantity, casacore::Quantity>& qFreqs,
183  const std::vector<casacore::Quantity>& quantities,
184  const casacore::String& textString,
185  const ParamSet& currentParamSet,
186  const casacore::Bool annOnly, const casacore::Bool isDifference,
187  const casacore::String& preamble, casacore::Bool requireImageRegion
188  );
189 
190  std::pair<casacore::Quantity, casacore::Quantity> _quantitiesFromFrequencyString(
191  const casacore::String& freqString, const casacore::String& preamble
192  ) const;
193 
194  static casacore::String _doLabel(casacore::String& consumeMe, const casacore::String& logPreamble);
195 
196  static casacore::String _getKeyValue(casacore::String& consumeMe, const casacore::String& preamble);
197 
199  casacore::String& consumeMe, const casacore::String& preamble
200  ) const;
201 
203  casacore::String& consumeMe, const casacore::String& preamble
204  ) const;
205 
207  casacore::String& consumeMe, const casacore::String& preamble
208  ) const;
209 
210  std::pair<casacore::Quantity, casacore::Quantity> _extractSingleQuantityPair(
211  const casacore::String& pair, const casacore::String& preamble
212  ) const;
213 
214  void _setInitialGlobals();
215 
217  const casacore::String& stokes, const casacore::String& preamble
218  );
219 
221  casacore::String& consumeMe, const casacore::String& preamble
222  ) const;
223 
225  std::pair<casacore::Quantity, casacore::Quantity>& quantities, casacore::String& string,
226  casacore::String& consumeMe, const casacore::String& preamble,
227  const casacore::Bool requireQuotesAroundString
228  ) const;
229 
231  const casacore::String& pair, const casacore::String& preamble
232  ) const;
233 
234  void _determineVersion(
235  const casacore::String& chunk, const casacore::String& filename,
236  const casacore::Int requireAtLeastThisVersion
237  );
238 
239  // set the casacore::Stokes/polarizations/correlations that will override all global and per line correlation
240  // specifications. If multiple ranges are specified, an exception will be thrown.
241  void _setOverridingCorrelations(const casacore::String& globalOverrideStokes);
242 
243  // set the (single) channel range that will override all global and per line frequency
244  // specifications. If multiple ranges are specified, an exception will be thrown.
245  void _setOverridingChannelRange(const casacore::String& globalOverrideChans);
246 
247 };
248 }
249 
250 #endif
A Vector of integers, for indexing into Array&lt;T&gt; objects.
Definition: IPosition.h:119
A 1-D Specialization of the Array class.
casacore::IPosition _imShape
int Int
Definition: aipstype.h:50
LatticeExprNode log(const LatticeExprNode &expr)
casacore::Vector< casacore::Quantity > _extractQuantitiesFromPair(const casacore::String &pair, const casacore::String &preamble) const
casacore::Vector< casacore::Stokes::StokesTypes > stokes
casacore::CoordinateSystem _csys
static const casacore::Regex startOnePair
ostream-like interface to creating log messages.
Definition: LogIO.h:167
static const casacore::Regex startNPair
const_iterator end() const
static const casacore::Int CURRENT_VERSION
std::vector< float > RGB
std::vector< AsciiAnnotationFileLine > getLines() const
void _determineVersion(const casacore::String &chunk, const casacore::String &filename, const casacore::Int requireAtLeastThisVersion)
std::pair< casacore::Quantity, casacore::Quantity > _extractSingleQuantityPair(const casacore::String &pair, const casacore::String &preamble) const
static casacore::String _getKeyValue(casacore::String &consumeMe, const casacore::String &preamble)
std::shared_ptr< casacore::Vector< casacore::Stokes::StokesTypes > > _overridingCorrRange
static const casacore::String sNPair
double Double
Definition: aipstype.h:55
AnnotationBase::LineStyle lineStyleVal
casacore::Vector< casacore::Quantity > _extractNQuantityPairs(casacore::String &consumeMe, const casacore::String &preamble) const
Regular expression class.
Definition: Regex.h:198
static casacore::Vector< casacore::Stokes::StokesTypes > _stokesFromString(const casacore::String &stokes, const casacore::String &preamble)
casacore::Vector< AnnotationBase::Keyword > _globalKeysToApply
AnnotationBase::Type _getAnnotationType(casacore::Vector< casacore::Quantity > &qDirs, std::vector< casacore::Quantity > &quantities, casacore::String &textString, casacore::String &consumeMe, const casacore::String &preamble) const
std::shared_ptr< std::pair< casacore::MFrequency, casacore::MFrequency > > freqRange
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
void _createAnnotation(const AnnotationBase::Type annType, const casacore::Vector< casacore::Quantity > &qDirs, const std::pair< casacore::Quantity, casacore::Quantity > &qFreqs, const std::vector< casacore::Quantity > &quantities, const casacore::String &textString, const ParamSet &currentParamSet, const casacore::Bool annOnly, const casacore::Bool isDifference, const casacore::String &preamble, casacore::Bool requireImageRegion)
casacore::Array< casacore::String > _extractTwoPairs(casacore::uInt &end, const casacore::String &string) const
std::unique_ptr< casacore::LogIO > _log
because of nonstandard access patterns, be careful when using ParamValue and ParamSet outside this cl...
static ParamSet getParamSet(casacore::Bool &spectralParmsUpdated, casacore::LogIO &log, const casacore::String &text, const casacore::String &preamble, const casacore::CoordinateSystem &csys, std::shared_ptr< std::pair< casacore::MFrequency, casacore::MFrequency > > overridingFreqRange, std::shared_ptr< casacore::Vector< casacore::Stokes::StokesTypes > > overridingCorrRange)
get the parameter set from a line of text.
std::pair< casacore::Quantity, casacore::Quantity > _quantitiesFromFrequencyString(const casacore::String &freqString, const casacore::String &preamble) const
casacore::Vector< casacore::Quantity > _extractTwoQuantityPairs(casacore::String &consumeMe, const casacore::String &preamble) const
RegionTextParser & operator=(const RegionTextParser &)=delete
void _parse(const casacore::String &contents, const casacore::String &fileDesc, casacore::Bool requireImageRegion)
static casacore::Vector< casacore::String > _extractSinglePair(const casacore::String &string)
extract s1 and s2 from a string of the form &quot;[s1, s2]&quot;
static const casacore::String bTwoPair
casacore::Int getFileVersion() const
casacore::Vector< casacore::Quantity > _extractTwoQuantityPairsAndSingleQuantity(casacore::String &consumeMe, const casacore::String &preamble) const
std::map< AnnotationBase::Keyword, ParamValue > ParamSet
std::shared_ptr< std::pair< casacore::MFrequency, casacore::MFrequency > > _overridingFreqRange
Represents a line in an ascii region file &lt;author&gt;Dave Mehringer&lt;/author&gt;
static const casacore::String sOnePair
Parse and store regions and annotations from an ascii region file &lt;author&gt;Dave Mehringer&lt;/author&gt; ...
String: the storage and methods of handling collections of characters.
Definition: String.h:223
std::vector< AsciiAnnotationFileLine > _lines
void _extractQuantityPairAndString(std::pair< casacore::Quantity, casacore::Quantity > &quantities, casacore::String &string, casacore::String &consumeMe, const casacore::String &preamble, const casacore::Bool requireQuotesAroundString) const
Subchunk class Provides the ordered pair identifying the current subchunk The first component is the chunk(goes from 0 to nChunks-1 during the outer loop iteration).The second component is the subchunk number which is zero after VisibilityIterator
static casacore::String _doLabel(casacore::String &consumeMe, const casacore::String &logPreamble)
ParamSet _getCurrentParamSet(casacore::Bool &spectralParmsUpdated, ParamSet &newParams, casacore::String &consumeMe, const casacore::String &preamble) const
void _addLine(const AsciiAnnotationFileLine &line)
void _setOverridingCorrelations(const casacore::String &globalOverrideStokes)
set the casacore::Stokes/polarizations/correlations that will override all global and per line correl...
std::vector< casacore::Int > intVec
AnnotationBase::FontStyle fontStyleVal
casacore::Vector< casacore::Quantity > _extractQuantityPairAndSingleQuantity(casacore::String &consumeMe, const casacore::String &preamble) const
void _setOverridingChannelRange(const casacore::String &globalOverrideChans)
set the (single) channel range that will override all global and per line frequency specifications...
Interconvert pixel and world coordinates.
unsigned int uInt
Definition: aipstype.h:51
static const casacore::Regex MAGIC