casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NRO2MSReader.h
Go to the documentation of this file.
1 /*
2  * NROReader.h
3  *
4  * Created on: May 9, 2016
5  * Author: wataru kawasaki
6  */
7 
8 #ifndef SINGLEDISH_FILLER_NRO2MSREADER_H_
9 #define SINGLEDISH_FILLER_NRO2MSREADER_H_
10 
11 #define STRING2CHAR(s) const_cast<char *>((s).c_str())
12 
17 
20 #include <string>
21 #include <memory>
22 
23 using namespace std;
24 
25 namespace casa { //# NAMESPACE CASA - BEGIN
26 
27 // forward declaration
28 class NROOptionalTables;
29 
31 public:
33 
34  NRO2MSReader(std::string const &scantable_name);
35  virtual ~NRO2MSReader();
36 
37  // get number of rows
38  virtual size_t getNumberOfRows();
39 
40  virtual casacore::MDirection::Types getDirectionFrame() const;
41 
42  virtual casacore::Bool isFloatData() const {
43  return true;
44  }
45 
46  virtual casacore::String getDataUnit() const {
47  return "K";
48  }
49 
50  // to get OBSERVATION table
52  POST_START;
53  casacore::Bool return_value = (*this.*get_observation_row_)(record);
54  POST_END;
55  return return_value;
56  }
57 
58  // to get ANTENNA table
60  POST_START;
61  casacore::Bool return_value = (*this.*get_antenna_row_)(record);
62  POST_END;
63  return return_value;
64  }
65 
66  // to get PROCESSOR table
68  POST_START;
69  casacore::Bool return_value = (*this.*get_processor_row_)(record);
70  POST_END;
71  return return_value;
72  }
73 
74  // to get SOURCE table
76  POST_START;
77  casacore::Bool return_value = (*this.*get_source_row_)(record);
78  POST_END;
79  return return_value;
80  }
81 
82  // to get FIELD table
84  POST_START;
85  casacore::Bool return_value = (*this.*get_field_row_)(record);
86  POST_END;
87  return return_value;
88  }
89 
90  // to get SOURCE table
92  POST_START;
93  casacore::Bool return_value = (*this.*get_spw_row_)(record);
94  POST_END;
95  return return_value;
96  }
97 
98  // for DataAccumulator
99  virtual casacore::Bool getData(size_t irow, sdfiller::DataRecord &record);
100 
101  int getNROArraySize() const {
102 // return obs_header_.ARYNM0; //obs_header_.NBEAM * obs_header_.NPOL * obs_header_.NSPWIN;
103  return NRO_ARYMAX;
104  }
105  int getNRONumBeam() const {
106  return obs_header_.NBEAM;
107  }
108  int getNRONumPol() const {
109  return obs_header_.NPOL;
110  }
111  int getNRONumSpw() const {
112  return obs_header_.NSPWIN;
113  }
114 
115  bool isNROArrayUsed(int array_id) const {
116  return array_mapper_[array_id].isUsed();
117  }
118  int getNROArrayBeamId(int array_id) const {
119 // assert(array_id >= 0 && array_id < getNROArraySize());
120  return array_mapper_[array_id].getBeamId();
121  }
123 // assert(array_id >= 0 && array_id < getNROArraySize());
124  return array_mapper_[array_id].getPol();
125  }
126  int getNROArraySpwId(int array_id) const {
127 // assert(array_id >= 0 && array_id < getNROArraySize());
128  return array_mapper_[array_id].getSpwId();
129  }
130 
131 protected:
132  void initializeSpecific();
133  void finalizeSpecific();
134 
135 private:
136  FILE *fp_;
138  void readObsHeader();
139  void readScanData(int const irow, sdfiller::NRODataScanData &data);
140  void checkEndian();
141  template<typename T>
142  void convertEndian(T &value) {
143  char volatile *first = reinterpret_cast<char volatile *>(&value) ;
144  char volatile *last = first + sizeof(T) ;
145  std::reverse(first, last) ;
146  }
147 
149 
150  template<typename T>
151  void readHeader(T &v) {
152  if ((int)fread(&v, 1, sizeof(T), fp_) != sizeof(T)) {
153  cout << "read failed." << endl;
154  }
155  if (!same_endian_) {
156  convertEndian(v);
157  }
158  }
159 
160  template<typename T>
161  void readHeader(T *v, size_t numArray) {
162  for (size_t i = 0; i < numArray; ++i) {
163  readHeader<T>(v[i]);
164  }
165  }
166 
167  void readHeader(string &v, size_t strLength) {
168  v.resize(strLength);
169  if (fread(STRING2CHAR(v), 1, strLength, fp_) != strLength) {
170  cout << "read failed." << endl;
171  }
172  v.resize(strlen(v.c_str())); // remove trailing null characters
173  }
174 
175  void readHeader(string *v, size_t strLength, size_t numArray) {
176  for (size_t i = 0; i < numArray; ++i) {
177  readHeader(v[i], strLength);
178  }
179  }
180 
181  struct NROArrayData {
182  int beam_id=-1;
184  string pol_name="";
185  int spw_id=-1;
186  bool is_used=false;
187  void set(int16_t const arr_data, string const *pol_data) {
188  // indices in NOSTAR data are 1-base
189  if (arr_data < 1101) {
190  is_used = false;
191  beam_id = -1;
192  spw_id = -1;
193  pol_name = "";
194  stokes_type = casacore::Stokes::Undefined;
195  return;
196  }
197  is_used = true;
198  beam_id = static_cast<int>(arr_data/1000) - 1;
199  int pol_id = static_cast<int>((arr_data % 1000)/100) - 1;
200  spw_id = static_cast<int>(arr_data % 100) -1;
201  pol_name = pol_data[pol_id];
202  stokes_type = casacore::Stokes::type(pol_name);
203  if (stokes_type == casacore::Stokes::Undefined) {
204  throw casacore::AipsError("Got unsupported polarization type\n");
205  }
206  }
207  int getBeamId() const {
208  if (beam_id < 0)
209  throw casacore::AipsError("Array data is not set yet\n");
210  return beam_id;}
212  if (stokes_type == casacore::Stokes::Undefined)
213  throw casacore::AipsError("Array data is not set yet\n");
214  return stokes_type;}
215  int getSpwId() const {
216  if (spw_id < 0)
217  throw casacore::AipsError("Array data is not set yet\n");
218  return spw_id;}
219  string getPolName() const {
220  if (pol_name.size() == 0)
221  throw casacore::AipsError("Array data is not set yet\n");
222  return pol_name;}
223  bool isUsed() const {
224  return is_used;
225  }
226  };
227 
228  std::vector<NROArrayData> array_mapper_;
229 
230  void constructArrayTable();
231  bool checkScanArray(string const scan_array, NROArrayData const *header_array);
232  // Returns the first array ID whose SPW ID is spwid.
233  int getFirstArrayIdWithSpwID(int spwid) {
234  for (int iarr = 0; iarr < obs_header_.ARYNM0 ; ++iarr) {
235  if (spwid == array_mapper_[iarr].spw_id) {
236  return iarr;
237  }
238  }
239  // no array with spwid found
240  throw casacore::AipsError("Internal ERROR: Could not find array ID corresponds to an SPW ID\n");
241  }
242 
247  int const len_obs_header_ = 15136;
248  double getMJD(string const &time);
249  double getIntMiddleTimeSec(sdfiller::NRODataScanData const &data);
250  double getIntStartTimeSec(int const scanno);
251  double getIntEndTimeSec(int const scanno);
252  void getFullTimeRange();
253  double getMiddleOfTimeRangeSec();
254 
255  casacore::Double const posx_ = -3.8710235e6;
256  casacore::Double const posy_ = 3.4281068e6;
257  casacore::Double const posz_ = 3.7240395e6;
258 
259  double getRestFrequency(int const spwno);
260  string convertVRefName(string const &vref0);
261  void shiftFrequency(string const &vdef,
262  double const v,
263  std::vector<double> &freqs);
264 
265  std::vector<double> getSpectrum(int const irow, sdfiller::NRODataScanData const &data);
266 // casacore::Int getPolNo(string const &rx);
267 
274 
275  casacore::Bool getAntennaRowImpl(sdfiller::AntennaRecord &record);
276  casacore::Bool getFieldRowImpl(sdfiller::FieldRecord &record);
277  casacore::Bool getObservationRowImpl(sdfiller::ObservationRecord &record);
278  casacore::Bool getProcessorRowImpl(sdfiller::ProcessorRecord &record);
279  casacore::Bool getSourceRowImpl(sdfiller::SourceRecord &record);
280  casacore::Bool getSpectralWindowRowImpl(sdfiller::SpectralWindowRecord &record);
281 
282  template<class _Record>
285  return false;
286  }
287 };
288 
289 // OptionalTables class for NRO data
291 public:
292  static void Generate(casacore::Table &table, NRO2MSReader const &reader) {
293  // generate NRO_ARRAY table
294  Generate_NRO_ARRAY(table, reader);
295  }
296 
297 private:
298  static void Generate_NRO_ARRAY(casacore::Table &table, NRO2MSReader const &reader) {
299  casacore::String const nro_tablename = "NRO_ARRAY";
300 
306  casacore::String tabname = table.tableName() + "/" + nro_tablename;
308  table.rwKeywordSet().defineTable(nro_tablename,
309  casacore::Table(newtab, reader.getNROArraySize()));
310 
311  casacore::Table nro_table = table.rwKeywordSet().asTable(nro_tablename);
312  casacore::ScalarColumn<int> arr(nro_table, "ARRAY");
313  casacore::ScalarColumn<int> bea(nro_table, "BEAM");
314  casacore::ScalarColumn<int> pol(nro_table, "POLARIZATION");
315  casacore::ScalarColumn<int> spw(nro_table, "SPECTRAL_WINDOW");
316  for (int iarr = 0; iarr < reader.getNROArraySize(); ++iarr) {
317  arr.put(iarr, iarr);
318  if (reader.isNROArrayUsed(iarr)) {
319  bea.put(iarr, reader.getNROArrayBeamId(iarr));
320  pol.put(iarr, reader.getNROArrayPol(iarr));
321  spw.put(iarr, reader.getNROArraySpwId(iarr));
322  } else {
323  // array is not used, fill with -1
324  bea.put(iarr, -1);
325  pol.put(iarr, -1);
326  spw.put(iarr, -1);
327  }
328  }
329  }
330 };
331 
332 } //# NAMESPACE CASA - END
333 
334 #endif /* SINGLEDISH_FILLER_NRO2MSREADER_H_ */
void readHeader(string &v, size_t strLength)
Definition: NRO2MSReader.h:167
virtual casacore::Bool isFloatData() const
Definition: NRO2MSReader.h:42
Templated class to define columns of scalars in tables.
Definition: ScaColData.h:39
int getNRONumSpw() const
Definition: NRO2MSReader.h:111
#define POST_END
Definition: FillerUtil.h:18
int getNRONumPol() const
Definition: NRO2MSReader.h:108
Create a new table - define shapes, data managers, etc.
Definition: SetupNewTab.h:346
#define NRO_ARYMAX
Definition: NROData.h:11
ColumnDesc & addColumn(const ColumnDesc &)
Add a column to the table description.
Definition: TableDesc.h:568
Main interface class to a read/write table.
Definition: Table.h:153
NROOptionalTables OptionalTables
Definition: NRO2MSReader.h:32
new table, which gets marked for delete
Definition: Table.h:176
struct Node * first
Definition: malloc.h:330
Table asTable(const RecordFieldId &) const
Get the table from the given field.
void readHeader(string *v, size_t strLength, size_t numArray)
Definition: NRO2MSReader.h:175
casacore::Stokes::StokesTypes getPol() const
Definition: NRO2MSReader.h:211
ABSTRACT TOOL CLASSES A PlotTool is a higher level event handler for a PlotCanvas The idea is to take common tasks which may require multiple events and put them in one place PlotTools also provide additional functionality in that they can be active and blocking non blocking The PlotCanvas will only send events to active and will not send events to later tools or event handlers if the latest tool was blocking In this way a single tool can be used to handle ALL user interaction via the GUI at one time
Definition: PlotTool.h:43
virtual casacore::Bool getFieldRow(sdfiller::FieldRecord &record)
to get FIELD table
Definition: NRO2MSReader.h:83
int getNRONumBeam() const
Definition: NRO2MSReader.h:105
std::vector< NROArrayData > array_mapper_
Definition: NRO2MSReader.h:228
void defineTable(const RecordFieldId &, const Table &value, RecordType type=Variable)
const String & tableName() const
Get the table name.
Definition: Table.h:1219
StokesTypes
The Stokes types are defined by this enum.
Definition: Stokes.h:66
virtual casacore::Bool getAntennaRow(sdfiller::AntennaRecord &record)
to get ANTENNA table
Definition: NRO2MSReader.h:59
ABSTRACT CLASSES Deliberately vague to be general enough to allow for many different types of data
Definition: PlotData.h:48
Types
Types of known MDirections Warning: The order defines the order in the translation matrix FromTo in ...
Definition: MDirection.h:188
int getNROArraySize() const
Definition: NRO2MSReader.h:101
static void Generate_NRO_ARRAY(casacore::Table &table, NRO2MSReader const &reader)
Definition: NRO2MSReader.h:298
virtual casacore::String getDataUnit() const
Definition: NRO2MSReader.h:46
casacore::Vector< casacore::Double > time_range_sec_
Definition: NRO2MSReader.h:246
OptionalTables class for NRO data.
Definition: NRO2MSReader.h:290
static void Generate(casacore::Table &table, NRO2MSReader const &reader)
Definition: NRO2MSReader.h:292
int getNROArrayBeamId(int array_id) const
Definition: NRO2MSReader.h:118
double Double
Definition: aipstype.h:55
static StokesTypes type(Int stokesNumber)
convert Int to StokesTypes, returns Stokes::Undefined if it is an invalid type
#define STRING2CHAR(s)
Definition: NRO2MSReader.h:11
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
casacore::Bool noMoreRowImpl(_Record &)
Definition: NRO2MSReader.h:283
TableRecord & rwKeywordSet()
Get read/write access to the table keyword set.
void readHeader(T *v, size_t numArray)
Definition: NRO2MSReader.h:161
virtual casacore::Bool getSourceRow(sdfiller::SourceRecord &record)
to get SOURCE table
Definition: NRO2MSReader.h:75
void readHeader(T &v)
Definition: NRO2MSReader.h:151
sdfiller::NRODataObsHeader obs_header_
Definition: NRO2MSReader.h:137
void set(int16_t const arr_data, string const *pol_data)
Definition: NRO2MSReader.h:187
virtual casacore::Bool getObservationRow(sdfiller::ObservationRecord &record)
to get OBSERVATION table
Definition: NRO2MSReader.h:51
casacore::Stokes::StokesTypes getNROArrayPol(int array_id) const
Definition: NRO2MSReader.h:122
bool isNROArrayUsed(int array_id) const
Definition: NRO2MSReader.h:115
int getFirstArrayIdWithSpwID(int spwid)
Returns the first array ID whose SPW ID is spwid.
Definition: NRO2MSReader.h:233
Base class for all Casacore library errors.
Definition: Error.h:134
void convertEndian(T &value)
Definition: NRO2MSReader.h:142
void put(uInt rownr, const T &value)
Put the value in a particular cell (i.e.
Definition: ScalarColumn.h:198
virtual casacore::Bool getProcessorRow(sdfiller::ProcessorRecord &record)
to get PROCESSOR table
Definition: NRO2MSReader.h:67
int getNROArraySpwId(int array_id) const
Definition: NRO2MSReader.h:126
String: the storage and methods of handling collections of characters.
Definition: String.h:223
Define the structure of a Casacore table.
Definition: TableDesc.h:187
#define POST_START
Definition: FillerUtil.h:17
Access to a scalar table column with arbitrary data type.
Definition: MSFitsOutput.h:41
undefined value = 0
Definition: Stokes.h:68
virtual casacore::Bool getSpectralWindowRow(sdfiller::SpectralWindowRecord &record)
to get SOURCE table
Definition: NRO2MSReader.h:91
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.