source: trunk/external-alma/asdm2ASAP/ASDMReader.cc@ 2252

Last change on this file since 2252 was 2252, checked in by Takeshi Nakazato, 13 years ago

New Development: No

JIRA Issue: Yes CAS-1913

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No

Module(s): Module Names change impacts.

Description: Describe your changes here...

Rnage of SRCDIRECTION (in rad) is limited between -pi and pi.

File size: 70.4 KB
RevLine 
[2197]1#include <iostream>
2#include <sstream>
3
4#include <measures/Measures/MEpoch.h>
5#include <measures/Measures/MPosition.h>
6#include <measures/Measures/MDirection.h>
7#include <measures/Measures/MCDirection.h>
8#include <measures/Measures/MeasFrame.h>
9#include <measures/Measures/MeasConvert.h>
[2208]10#include <casa/Logging/LogMessage.h>
[2252]11#include <casa/BasicSL/Constants.h>
[2197]12
13#include "ASDMReader.h"
14#include <atnf/PKSIO/SrcType.h>
15
16using namespace std ;
17//using namespace casa ;
18using namespace asdm ;
19using namespace sdmbin ;
20
21// sec to day
22double s2d = 1.0 / 86400.0 ;
23
24ASDMReader::ASDMReader()
25 : asdm_(0),
26 sdmBin_(0),
27 vmsData_(0),
28 antennaId_( -1 ),
29 antennaName_( "" ),
30 row_(-1),
[2208]31 apc_(AP_CORRECTED),
32 className_("ASDMReader")
[2197]33{
34 configDescIdList_.resize(0) ;
35 feedIdList_.resize(0) ;
36 fieldIdList_.resize(0) ;
37 mainRow_.resize(0) ;
38 ifno_.clear() ;
[2208]39 corrMode_.reset() ;
40 timeSampling_.reset() ;
[2197]41}
42
43ASDMReader::~ASDMReader()
44{
[2208]45 close() ;
46 logsink_ = 0 ;
[2197]47}
48
49bool ASDMReader::open( const string &filename, const casa::Record &rec )
50{
[2227]51 casa::String funcName = "open" ;
[2208]52
53 // return value
54 bool status = true ;
55
56 // set default
57 timeSampling_.reset() ;
58 corrMode_.reset() ;
[2215]59 resolutionType_.reset() ;
[2208]60 apc_ = AP_CORRECTED ;
61
[2197]62 // parsing ASDM options
63 if ( rec.isDefined( "asdm" ) ) {
64 casa::Record asdmrec = rec.asRecord( "asdm" ) ;
[2208]65
66 // antenna
[2197]67 if ( asdmrec.isDefined( "antenna" ) ) {
68 if ( asdmrec.type( asdmrec.fieldNumber( "antenna" ) ) == casa::TpInt ) {
69 antennaId_ = asdmrec.asInt( "antenna" ) ;
70 }
71 else {
72 antennaName_ = asdmrec.asString( "antenna" ) ;
73 }
74 }
75 else {
76 antennaId_ = 0 ;
77 }
[2208]78
79 // ATM phase correction
[2197]80 if ( asdmrec.isDefined( "apc" ) ) {
81 if ( asdmrec.asBool( "apc" ) )
82 apc_ = AP_CORRECTED ;
83 else
84 apc_ = AP_UNCORRECTED ;
85 }
[2208]86
87 // time sampling
88 String timeSampling = "all" ; // take all time sampling by default
89 if ( asdmrec.isDefined( "sampling" ) ) {
90 timeSampling = asdmrec.asString( "sampling" ) ;
91 }
92 if ( timeSampling == "all" ) {
93 timeSampling_.set( INTEGRATION ) ;
94 timeSampling_.set( SUBINTEGRATION ) ;
95 }
96 else if ( timeSampling == "integration" ) {
97 timeSampling_.set( INTEGRATION ) ;
98 }
99 else if ( timeSampling == "subintegration" ) {
100 timeSampling_.set( SUBINTEGRATION ) ;
101 }
102 else {
103 //throw AipsError( "Unrecognized option for sampling" ) ;
104 logsink_->postLocally( LogMessage( "Unrecognized option for time sampling: "+String::toString(timeSampling), LogOrigin(className_,funcName,WHERE), LogMessage::WARN ) ) ;
105 status = false ;
106 }
107
[2215]108 // spectral resolution type
[2216]109 string resolutionType = "all" ;
[2215]110 if ( asdmrec.isDefined( "srt" ) ) {
[2216]111 resolutionType = string( asdmrec.asString( "srt" ) ) ;
[2215]112 }
[2216]113 string resolutionTypes[3] ;
114 Int numType = split( resolutionType, resolutionTypes, 3, "," ) ;
115 for ( Int it = 0 ; it < numType ; it++ ) {
116 if ( resolutionTypes[it] == "all" ) {
117 resolutionType_.reset() ;
118 resolutionType_.set( FULL_RESOLUTION ) ;
119 resolutionType_.set( BASEBAND_WIDE ) ;
120 resolutionType_.set( CHANNEL_AVERAGE ) ;
121 break ;
122 }
123 else if ( resolutionTypes[it] == "fr" ) {
124 resolutionType_.set( FULL_RESOLUTION ) ;
125 }
126 else if ( resolutionTypes[it] == "bw" ) {
127 resolutionType_.set( BASEBAND_WIDE ) ;
128 }
129 else if ( resolutionTypes[it] == "ca" ) {
130 resolutionType_.set( CHANNEL_AVERAGE ) ;
131 }
[2215]132 }
[2216]133 if ( resolutionType_.size() == 0 ) {
[2215]134 logsink_->postLocally( LogMessage( "Unrecognized option for spectral resolution type: "+String::toString(resolutionType), LogOrigin(className_,funcName,WHERE), LogMessage::WARN ) ) ;
135 status = false ;
136 }
137
[2208]138 // input correlation mode
[2248]139 string corrMode = "ao,ca" ;
[2208]140 if ( asdmrec.isDefined( "corr" ) ) {
[2216]141 corrMode = string( asdmrec.asString( "corr" ) ) ;
[2208]142 //logsink_->postLocally( LogMessage("corrMode = "+String(corrMode),LogOrigin(className_,funcName,WHERE)) ) ;
[2216]143 }
144 string corrModes[3] ;
145 Int numCorr = split( corrMode, corrModes, 3, "," ) ;
146 for ( Int ic = 0 ; ic < numCorr ; ic++ ) {
147 if ( corrModes[ic] == "ao" ) {
148 corrMode_.set( AUTO_ONLY ) ;
[2208]149 }
[2216]150 else if ( corrModes[ic] == "ca" ) {
151 corrMode_.set( CROSS_AND_AUTO ) ;
[2208]152 }
153 }
[2216]154 if ( corrMode_.size() == 0 ) {
155 logsink_->postLocally( LogMessage( "Invalid option for correlation mode: "+String::toString(corrMode), LogOrigin(className_,funcName,WHERE), LogMessage::WARN ) ) ;
156 status = false ;
157 }
[2208]158
159// logsink_->postLocally( LogMessage( "### asdmrec summary ###", LogOrigin(className_,funcName,WHERE) ) ) ;
160// ostringstream oss ;
161// asdmrec.print( oss ) ;
162// logsink_->postLocally( LogMessage( oss.str(), LogOrigin(className_,funcName,WHERE) ) ) ;
163// logsink_->postLocally( LogMessage( "#######################", LogOrigin(className_,funcName,WHERE) ) ) ;
[2197]164 }
165
166 // create ASDM object
167 asdm_ = new ASDM() ;
168 asdm_->setFromFile( filename ) ;
169
170 if ( antennaId_ == -1 ) {
171 AntennaTable &atab = asdm_->getAntenna() ;
172 vector<AntennaRow *> rows = atab.get() ;
173 int idx = -1 ;
174 for ( casa::uInt irow = 0 ; irow < rows.size() ; irow++ ) {
175 if ( casa::String(rows[irow]->getName()) == antennaName_ ) {
176 idx = rows[irow]->getAntennaId().getTagValue() ;
177 break ;
178 }
179 }
180 if ( idx == -1 ) {
181 close() ;
182 throw (casa::AipsError( antennaName_ + " not found." )) ;
183 }
184 else {
185 antennaId_ = idx ;
186 }
187 }
188
189 // set antenna name
190 if ( antennaName_.size() == 0 ) {
191 AntennaTable &atab = asdm_->getAntenna() ;
192 Tag tag( antennaId_, TagType::Antenna ) ;
193 AntennaRow *arow = atab.getRowByKey( tag ) ;
194 if ( arow == 0 ) {
195 close() ;
196 throw (casa::AipsError( tag.toString() + " not found." )) ;
197 }
198 else {
199 antennaName_ = casa::String( arow->getName() ) ;
200 }
201 }
202
203 // create SDMBinData object
204 sdmBin_ = new SDMBinData( asdm_, filename ) ;
205
206 // get Main rows
207 //mainRow_ = casa::Vector<MainRow *>(asdm_->getMain().get()) ;
208
209 // set up IFNO
210 setupIFNO() ;
211
212 // process Station table
213 processStation() ;
214
[2220]215 logsink_->postLocally( LogMessage( "antennaId_ = "+String::toString(antennaId_), LogOrigin(className_,funcName,WHERE) ) ) ;
216 logsink_->postLocally( LogMessage( "antennaName_ = "+antennaName_, LogOrigin(className_,funcName,WHERE) ) ) ;
[2197]217
218 return true ;
219}
220
[2208]221// void ASDMReader::fill()
222// {
223// }
[2197]224
225void ASDMReader::close()
226{
227 clearMainRow() ;
228
229 if ( sdmBin_ )
230 delete sdmBin_ ;
231 sdmBin_ = 0 ;
232
233 if ( asdm_ )
234 delete asdm_ ;
235 asdm_ = 0 ;
236
237 return ;
238}
239
240void ASDMReader::fillHeader( casa::Int &nchan,
241 casa::Int &npol,
242 casa::Int &nif,
243 casa::Int &nbeam,
244 casa::String &observer,
245 casa::String &project,
246 casa::String &obstype,
247 casa::String &antennaname,
248 casa::Vector<casa::Double> &antennaposition,
249 casa::Float &equinox,
250 casa::String &freqref,
251 casa::Double &reffreq,
252 casa::Double &bandwidth,
253 casa::Double &utc,
254 casa::String &fluxunit,
255 casa::String &epoch,
256 casa::String &poltype )
257{
[2227]258 casa::String funcName = "fillHeader" ;
[2197]259
260 ExecBlockTable &ebtab = asdm_->getExecBlock() ;
261 // at the moment take first row of ExecBlock table
262 ExecBlockRow *ebrow = ebtab.get()[0] ;
263 casa::String telescopeName( ebrow->getTelescopeName() ) ;
264 AntennaTable &atab = asdm_->getAntenna() ;
265 AntennaRow *arow = atab.getRowByKey( Tag( antennaId_, TagType::Antenna ) ) ;
266 //StationTable &stab = asdm_->getStation() ;
267 //StationRow *srow = stab.getRowByKey( arow->getStationId() ) ;
268 StationRow *srow = arow->getStationUsingStationId() ;
269 casa::String stationName( srow->getName() ) ;
270
271 // antennaname
272 // <telescopeName>//<antennaName>@stationName
273 antennaname = telescopeName + "//" + antennaName_ + "@" + stationName ;
[2208]274 //logsink_->postLocally( LogMessage("antennaName = "+antennaname,LogOrigin(className_,funcName,WHERE)) ) ;
[2197]275
276 // antennaposition
277 //vector<Length> antpos = arow->getPosition() ;
278 vector<Length> antpos = srow->getPosition() ;
279 antennaposition.resize( 3 ) ;
280 for ( casa::uInt i = 0 ; i < 3 ; i++ )
281 antennaposition[i] = casa::Double( antpos[i].get() ) ;
282
283 // observer
284 observer = ebrow->getObserverName() ;
285
286 // project
287 // T.B.D. (project UID?)
288 project = "T.B.D. (" + ebrow->getProjectId().toString() + ")" ;
289
290 // utc
291 // start time of the project
292 utc = casa::Double( ebrow->getStartTime().getMJD() ) ;
293
294
295 SpectralWindowTable &spwtab = asdm_->getSpectralWindow() ;
296 vector<SpectralWindowRow *> spwrows = spwtab.get() ;
[2220]297 int nspwrow = spwrows.size() ;
[2197]298
299 // nif
[2220]300 //nif = spwrows.size() ;
301 nif = getNumIFs() ;
[2197]302
303 // nchan
304 int refidx = -1 ;
305 vector<int> nchans ;
[2220]306 for ( int irow = 0 ; irow < nspwrow ; irow++ ) {
[2197]307 nchans.push_back( spwrows[irow]->getNumChan() ) ;
308 if ( refidx == -1 && nchans[irow] != 1 && nchans[irow] != 4 )
309 refidx = irow ;
310 }
311 nchan = casa::Int( *max_element( nchans.begin(), nchans.end() ) ) ;
312
[2208]313 //logsink_->postLocally( LogMessage("refidx = "+String::toString(refidx),LogOrigin(className_,funcName,WHERE)) ) ;
[2197]314
315 // bandwidth
316 vector<double> bws ;
[2220]317 for ( int irow = 0 ; irow < nspwrow ; irow++ ) {
[2197]318 if ( nchans[irow] != 4 ) { // exclude WVR data
319 bws.push_back( spwrows[irow]->getTotBandwidth().get() ) ;
320 }
321 }
322 bandwidth = casa::Double( *max_element( bws.begin(), bws.end() ) ) ;
323
324 // reffreq
325 reffreq = casa::Double( spwrows[refidx]->getRefFreq().get() ) ;
326
327 // freqref
328 if ( spwrows[refidx]->isMeasFreqRefExists() ) {
329 string mfr = CFrequencyReferenceCode::name( spwrows[refidx]->getMeasFreqRef() ) ;
[2225]330// if (mfr == "TOPO") {
331// freqref = "TOPOCENT";
332// } else if (mfr == "GEO") {
333// freqref = "GEOCENTR";
334// } else if (mfr == "BARY") {
335// freqref = "BARYCENT";
336// } else if (mfr == "GALACTO") {
337// freqref = "GALACTOC";
338// } else if (mfr == "LGROUP") {
339// freqref = "LOCALGRP";
340// } else if (mfr == "CMB") {
341// freqref = "CMBDIPOL";
342// } else if (mfr == "REST") {
343// freqref = "SOURCE";
344// }
345 freqref = String( mfr ) ;
[2197]346 }
347 else {
348 // frequency reference is TOPOCENT by default
[2225]349 //freqref = "TOPOCENT" ;
350 freqref = "TOPO" ;
[2197]351 }
352
353
354 PolarizationTable &ptab = asdm_->getPolarization() ;
355 vector<PolarizationRow *> prows = ptab.get() ;
356 vector<int> npols ;
357 refidx = -1 ;
358 for ( unsigned int irow = 0 ; irow < prows.size() ; irow++ ) {
359 npols.push_back( prows[irow]->getNumCorr() ) ;
360 if ( refidx == -1 && npols[irow] != 1 )
361 refidx = irow ;
362 }
363 if ( refidx == -1 )
364 refidx = 0 ;
365
366 // npol
367 npol = casa::Int( *max_element( npols.begin(), npols.end() ) ) ;
368
369 // poltype
370 vector<StokesParameter> corrType = prows[refidx]->getCorrType() ;
371 if ( corrType[0] == I ||
372 corrType[0] == Q ||
373 corrType[0] == U ||
374 corrType[0] == V )
375 poltype = "stokes" ;
376 else if ( corrType[0] == RR ||
377 corrType[0] == RL ||
378 corrType[0] == LR ||
379 corrType[0] == LL )
380 poltype = "circular" ;
381 else if ( corrType[0] == XX ||
382 corrType[0] == XY ||
383 corrType[0] == YX ||
384 corrType[0] == YY )
385 poltype = "linear" ;
386 else if ( corrType[0] == PLINEAR ||
387 corrType[0] == PANGLE ) {
388 poltype = "linpol" ;
389 }
390 else {
391 poltype = "notype" ;
392 }
393
394
395 FeedTable &ftab = asdm_->getFeed() ;
396 vector<FeedRow *> frows = ftab.get() ;
397 vector<int> nbeams ;
398 for ( unsigned int irow = 0 ; irow < frows.size() ; irow++ ) {
399 if ( frows[irow]->isFeedNumExists() )
400 nbeams.push_back( frows[irow]->getFeedNum() ) ;
401 else
402 nbeams.push_back( 1 ) ;
403 }
404
405 // nbeam
406 nbeam = casa::Int( *max_element( nbeams.begin(), nbeams.end() ) ) ;
407
408 // fluxunit
409 // tentatively set 'K'? or empty?
410 fluxunit = "K" ;
411
412 // equinox
413 // tentatively set 2000.0
414 equinox = 2000.0 ;
415
416 // epoch
417 // tentatively set "UTC"
418 epoch = "UTC" ;
419
420 // obstype
421 // at the moment take observingMode attribute in SBSummary table
422 SBSummaryRow *sbrow = ebrow->getSBSummaryUsingSBSummaryId() ;
423 vector<string> obsmode = sbrow->getObservingMode() ;
424 obstype = "" ;
425 for ( unsigned int imode = 0 ; imode < obsmode.size() ; imode++ ) {
426 obstype += casa::String(obsmode[imode]) ;
427 if ( imode != obsmode.size()-1 )
428 obstype += "#" ;
429 }
430}
431
432void ASDMReader::selectConfigDescription()
433{
[2227]434 casa::String funcName = "selectConfigDescription" ;
[2208]435
[2197]436 vector<ConfigDescriptionRow *> cdrows = asdm_->getConfigDescription().get() ;
437 vector<Tag> cdidTags ;
438 for ( unsigned int irow = 0 ; irow < cdrows.size() ; irow++ ) {
[2208]439 //logsink_->postLocally( LogMessage("correlationMode["+String::toString(irow)+"] = "+String::toString(cdrows[irow]->getCorrelationMode()),LogOrigin(className_,funcName,WHERE)) ) ;
[2197]440 if ( cdrows[irow]->getCorrelationMode() != CROSS_ONLY ) {
441 cdidTags.push_back( cdrows[irow]->getConfigDescriptionId() ) ;
442 }
443 }
444
445 configDescIdList_.resize( cdidTags.size() ) ;
446 for ( unsigned int i = 0 ; i < cdidTags.size() ; i++ ) {
447 configDescIdList_[i] = casa::uInt( cdidTags[i].getTagValue() ) ;
448 }
449}
450
451void ASDMReader::selectFeed()
452{
453 feedIdList_.resize(0) ;
454 vector<FeedRow *> frows = asdm_->getFeed().get() ;
455 Tag atag( antennaId_, TagType::Antenna ) ;
456 for ( unsigned int irow = 0 ; irow < frows.size() ; irow++ ) {
457 casa::uInt feedId = (casa::uInt)(frows[irow]->getFeedId() ) ;
458 if ( casa::anyEQ( feedIdList_, feedId ) )
459 continue ;
460 if ( frows[irow]->getAntennaId() == atag ) {
461 unsigned int oldsize = feedIdList_.size() ;
462 feedIdList_.resize( oldsize+1, true ) ;
463 feedIdList_[oldsize] = feedId ;
464 }
465 }
466}
467
468casa::Vector<casa::uInt> ASDMReader::getFieldIdList()
469{
[2227]470 casa::String funcName = "getFieldIdList" ;
[2208]471
[2197]472 vector<FieldRow *> frows = asdm_->getField().get() ;
473 fieldIdList_.resize( frows.size() ) ;
474 for ( unsigned int irow = 0 ; irow < frows.size() ; irow++ ) {
[2208]475 //logsink_->postLocally( LogMessage("fieldId["+String::toString(irow)+"]="+String(frows[irow]->getFieldId().toString()),LogOrigin(className_,funcName,WHERE)) ) ;
[2197]476 fieldIdList_[irow] = frows[irow]->getFieldId().getTagValue() ;
477 }
478
479 return fieldIdList_ ;
480}
481
482casa::uInt ASDMReader::getNumMainRow()
483{
484 casa::uInt nrow = casa::uInt( mainRow_.size() ) ;
485
486 return nrow ;
487}
488
489void ASDMReader::select()
490{
[2208]491 // selection by input CorrelationMode
492 EnumSet<CorrelationMode> esCorrs ;
493 sdmBin_->select( corrMode_ ) ;
494
495 // selection by TimeSampling
496 sdmBin_->select( timeSampling_ ) ;
497
[2215]498 // selection by SpectralResolutionType
499 sdmBin_->select( resolutionType_ ) ;
500
[2208]501 // selection by AtmPhaseCorrection and output CorrelationMode
[2197]502 EnumSet<AtmPhaseCorrection> esApcs ;
503 esApcs.set( apc_ ) ;
[2208]504 // always take only autocorrelation data
[2197]505 Enum<CorrelationMode> esCorr = AUTO_ONLY ;
506 sdmBin_->selectDataSubset( esCorr, esApcs ) ;
507
508 // select valid configDescriptionId
509 selectConfigDescription() ;
510
511 // select valid feedId
512 selectFeed() ;
513}
514
515casa::Bool ASDMReader::setMainRow( casa::uInt irow )
516{
517 casa::Bool status = true ;
518 row_ = irow ;
519
520 unsigned int cdid = mainRow_[row_]->getConfigDescriptionId().getTagValue() ;
521 if ( (int)count(configDescIdList_.begin(),configDescIdList_.end(),cdid) == 0 )
522 status = false ;
523 else {
524 status = (casa::Bool)(sdmBin_->acceptMainRow( mainRow_[row_] )) ;
525 }
526 return status ;
527}
528
529casa::Bool ASDMReader::setMainRow( casa::uInt configDescId, casa::uInt fieldId )
530{
531 clearMainRow() ;
532
533 Tag configDescTag( (unsigned int)configDescId, TagType::ConfigDescription ) ;
534 Tag fieldTag( (unsigned int)fieldId, TagType::Field ) ;
535 mainRow_ = casa::Vector<MainRow *>( *(asdm_->getMain().getByContext( configDescTag, fieldTag ) ) ) ;
536
537 return true ;
538}
539
540void ASDMReader::clearMainRow()
541{
542 mainRow_.resize(0) ;
543}
544
545void ASDMReader::setupIFNO()
546{
[2227]547 casa::String funcName = "setupIFNO" ;
[2208]548
[2197]549 vector<SpectralWindowRow *> spwrows = asdm_->getSpectralWindow().get() ;
550 unsigned int nrow = spwrows.size() ;
551 ifno_.clear() ;
552 casa::uInt idx = 0 ;
553 casa::uInt wvridx = 0 ;
554 for ( unsigned int irow = 0 ; irow < nrow ; irow++ ) {
555 casa::uInt index ;
556 if ( isWVR( spwrows[irow] ) ) {
[2208]557 //logsink_->postLocally( LogMessage(spwrows[irow]->getSpectralWindowId().toString()+" is WVR",LogOrigin(className_,funcName,WHERE)) ) ;
[2197]558 index = wvridx ;
559 }
560 else {
561 index = ++idx ;
562 }
563 ifno_.insert( pair<Tag,casa::uInt>(spwrows[irow]->getSpectralWindowId(),index) ) ;
[2208]564 //logsink_->postLocally( LogMessage(spwrows[irow]->getSpectralWindowId().toString()+": IFNO="+String::toString(index),LogOrigin(className_,funcName,WHERE)) ) ;
[2197]565 }
566}
567
568bool ASDMReader::isWVR( SpectralWindowRow *row )
569{
570 BasebandName bbname = row->getBasebandName() ;
571 int nchan = row->getNumChan() ;
572 if ( bbname == NOBB && nchan == 4 )
573 return true ;
574 else
575 return false ;
576}
577
[2208]578// casa::Vector<casa::uInt> ASDMReader::getDataDescIdList( casa::uInt cdid )
579// {
580// Tag cdTag( (unsigned int)cdid, TagType::ConfigDescription ) ;
581// ConfigDescriptionRow *cdrow = asdm_->getConfigDescription().getRowByKey( cdTag ) ;
582// vector<Tag> ddTags = cdrow->getDataDescriptionId() ;
583// casa::Vector<casa::uInt> ddidList( ddTags.size() ) ;
584// for ( unsigned int idd = 0 ; idd < ddTags.size() ; idd++ ) {
585// ddidList[idd] = ddTags[idd].getTagValue() ;
586// }
587// return ddidList ;
588// }
[2197]589
[2208]590// casa::Vector<casa::uInt> ASDMReader::getSwitchCycleIdList( casa::uInt cdid )
591// {
592// Tag cdTag( (unsigned int)cdid, TagType::ConfigDescription ) ;
593// ConfigDescriptionRow *cdrow = asdm_->getConfigDescription().getRowByKey( cdTag ) ;
594// vector<Tag> scTags = cdrow->getSwitchCycleId() ;
595// casa::Vector<casa::uInt> scidList( scTags.size() ) ;
596// for ( unsigned int idd = 0 ; idd < scTags.size() ; idd++ ) {
597// scidList[idd] = scTags[idd].getTagValue() ;
598// }
599// return scidList ;
600// }
[2197]601
[2208]602// casa::Vector<casa::uInt> ASDMReader::getFeedIdList( casa::uInt cdid )
603// {
[2227]604// casa::String funcName = "getFeedIdList" ;
[2208]605//
606// Tag cdTag( (unsigned int)cdid, TagType::ConfigDescription ) ;
607// ConfigDescriptionRow *cdrow = asdm_->getConfigDescription().getRowByKey( cdTag ) ;
608// casa::Vector<casa::uInt> feedIdList ;
609// vector<int> feedIds = cdrow->getFeedId() ;
610// for ( unsigned int ife = 0 ; ife < feedIds.size() ; ife++ ) {
611// logsink_->postLocally( LogMessage("feedIds["+String::toString(ife)+"]="+String::toString(feedIds[ife]),LogOrigin(className_,funcName,WHERE)) ) ;
612// if ( casa::anyEQ( feedIdList, casa::uInt( feedIds[ife] ) ) )
613// continue ;
614// if ( casa::anyEQ( feedIdList_, casa::uInt( feedIds[ife] ) ) ) {
615// casa::uInt oldsize = feedIdList.size() ;
616// feedIdList.resize( oldsize+1, true ) ;
617// feedIdList[oldsize] = casa::uInt( feedIds[ife] ) ;
618// }
619// }
620// logsink_->postLocally( LogMessage("feedIdList.size() = "+String::toString(feedIdList.size()),LogOrigin(className_,funcName,WHERE)) ) ;
621// return feedIdList ;
622// }
[2197]623
624casa::Bool ASDMReader::setData()
625{
[2227]626 casa::String funcName = "setData" ;
[2208]627
628 //logsink_->postLocally( LogMessage("try to retrieve binary data",LogOrigin(className_,funcName,WHERE)) ) ;
[2197]629
[2208]630// EnumSet<AtmPhaseCorrection> esApcs ;
631// esApcs.set( apc_ ) ;
632// // always take only autocorrelation data
633// Enum<CorrelationMode> esCorr = AUTO_ONLY ;
634// vmsData_ = sdmBin_->getDataCols( esCorr, esApcs ) ;
635
636 // 2011/07/06 TN
637 // Workaround to avoid unwanted message from SDMBinData::getDataCols()
638 ostringstream oss ;
639 streambuf *buforg = cout.rdbuf(oss.rdbuf()) ;
[2197]640 vmsData_ = sdmBin_->getDataCols() ;
[2208]641 cout.rdbuf(buforg) ;
[2197]642
[2208]643// logsink_->postLocally( LogMessage("oss.str() = "+oss.str(),LogOrigin(className_,funcName,WHERE)) ) ;
644// cout << "This is test: oss.str()=" << oss.str() << endl ;
[2197]645
646
[2208]647 //logsink_->postLocally( LogMessage("processorId = "+String::toString(vmsData_->processorId),LogOrigin(className_,funcName,WHERE)) ) ;
648 //logsink_->postLocally( LogMessage("v_time.size() = "+String::toString(vmsData_->v_time.size()),LogOrigin(className_,funcName,WHERE)) ) ;
649 //logsink_->postLocally( LogMessage(" v_time[0] = "+String::toString(vmsData_->v_time[0]),LogOrigin(className_,funcName,WHERE)) ) ;
650 //logsink_->postLocally( LogMessage("v_interval.size() = "+String::toString(vmsData_->v_interval.size()),LogOrigin(className_,funcName,WHERE)) ) ;
651 //logsink_->postLocally( LogMessage(" v_interval[0] = "+String::toString(vmsData_->v_interval[0]),LogOrigin(className_,funcName,WHERE)) ) ;
652 //logsink_->postLocally( LogMessage("v_atmPhaseCorrection.size() = "+String::toString(vmsData_->v_atmPhaseCorrection.size()),LogOrigin(className_,funcName,WHERE)) ) ;
653 //logsink_->postLocally( LogMessage("binNum = "+String::toString(vmsData_->binNum),LogOrigin(className_,funcName,WHERE)) ) ;
654 //logsink_->postLocally( LogMessage("v_projectPath.size() = "+String::toString(vmsData_->v_projectPath.size()),LogOrigin(className_,funcName,WHERE)) ) ;
655 //logsink_->postLocally( LogMessage("v_antennaId1.size() = "+String::toString(vmsData_->v_antennaId1.size()),LogOrigin(className_,funcName,WHERE)) ) ;
656 //logsink_->postLocally( LogMessage("v_antennaId2.size() = "+String::toString(vmsData_->v_antennaId2.size()),LogOrigin(className_,funcName,WHERE)) ) ;
657 //logsink_->postLocally( LogMessage("v_feedId1.size() = "+String::toString(vmsData_->v_feedId1.size()),LogOrigin(className_,funcName,WHERE)) ) ;
658 //logsink_->postLocally( LogMessage("v_feedId2.size() = "+String::toString(vmsData_->v_feedId2.size()),LogOrigin(className_,funcName,WHERE)) ) ;
659 //logsink_->postLocally( LogMessage("v_dataDescId.size() = "+String::toString(vmsData_->v_dataDescId.size()),LogOrigin(className_,funcName,WHERE)) ) ;
660 //logsink_->postLocally( LogMessage("v_timeCentroid.size() = "+String::toString(vmsData_->v_timeCentroid.size()),LogOrigin(className_,funcName,WHERE)) ) ;
661 //logsink_->postLocally( LogMessage("v_exposure.size() = "+String::toString(vmsData_->v_exposure.size()),LogOrigin(className_,funcName,WHERE)) ) ;
662 //logsink_->postLocally( LogMessage("v_numData.size() = "+String::toString(vmsData_->v_numData.size()),LogOrigin(className_,funcName,WHERE)) ) ;
663 //logsink_->postLocally( LogMessage("vv_dataShape.size() = "+String::toString(vmsData_->vv_dataShape.size()),LogOrigin(className_,funcName,WHERE)) ) ;
664 //logsink_->postLocally( LogMessage("v_m_data.size() = "+String::toString(vmsData_->v_m_data.size()),LogOrigin(className_,funcName,WHERE)) ) ;
665 //logsink_->postLocally( LogMessage("v_phaseDir.size() = "+String::toString(vmsData_->v_phaseDir.size()),LogOrigin(className_,funcName,WHERE)) ) ;
666 //logsink_->postLocally( LogMessage("v_stateId.size() = "+String::toString(vmsData_->v_stateId.size()),LogOrigin(className_,funcName,WHERE)) ) ;
667 //logsink_->postLocally( LogMessage("v_msState.size() = "+String::toString(vmsData_->v_msState.size()),LogOrigin(className_,funcName,WHERE)) ) ;
668 //logsink_->postLocally( LogMessage("v_flag.size() = "+String::toString(vmsData_->v_flag.size()),LogOrigin(className_,funcName,WHERE)) ) ;
[2197]669
670 dataIdList_.clear() ;
671 unsigned int numTotalData = vmsData_->v_m_data.size() ;
672 for ( unsigned int idata = 0 ; idata < numTotalData ; idata++ ) {
673 if ( vmsData_->v_antennaId1[idata] == (int)antennaId_
674 && vmsData_->v_antennaId2[idata] == (int)antennaId_ )
675 dataIdList_.push_back( idata ) ;
676 }
677 numData_ = dataIdList_.size() ;
[2208]678 //logsink_->postLocally( LogMessage("numData_ = "+String::toString(numData_),LogOrigin(className_,funcName,WHERE)) ) ;
679 //logsink_->postLocally( LogMessage("dataSize = "+String::toString(mainRow_[row_]->getDataSize()),LogOrigin(className_,funcName,WHERE)) ) ;
[2197]680
681 return true ;
682}
683
684casa::uInt ASDMReader::getIFNo( unsigned int idx )
685{
686 Tag ddTag( vmsData_->v_dataDescId[dataIdList_[idx]], TagType::DataDescription ) ;
687 DataDescriptionRow *ddrow = asdm_->getDataDescription().getRowByKey( ddTag ) ;
688 Tag spwid = ddrow->getSpectralWindowId() ;
689 map<Tag,casa::uInt>::iterator iter = ifno_.find( spwid ) ;
690 if ( iter != ifno_.end() )
691 return iter->second ;
692 else {
693 return 0 ;
694 }
695}
696
697int ASDMReader::getNumPol( unsigned int idx )
698{
699 Tag ddTag( vmsData_->v_dataDescId[dataIdList_[idx]], TagType::DataDescription ) ;
700 DataDescriptionRow *ddrow = asdm_->getDataDescription().getRowByKey( ddTag ) ;
701 PolarizationRow *polrow = ddrow->getPolarizationUsingPolOrHoloId() ;
702 return polrow->getNumCorr() ;
703}
704
705void ASDMReader::getFrequency( unsigned int idx,
706 double &refpix,
707 double &refval,
[2227]708 double &incr,
709 string &freqref )
[2197]710{
[2227]711 casa::String funcName = "getFrequency" ;
[2208]712
[2197]713 Tag ddTag( vmsData_->v_dataDescId[dataIdList_[idx]], TagType::DataDescription ) ;
714 DataDescriptionRow *ddrow = asdm_->getDataDescription().getRowByKey( ddTag ) ;
715 //Tag spwid = ddrow->getSpectralWindowId() ;
716 SpectralWindowRow *spwrow = ddrow->getSpectralWindowUsingSpectralWindowId() ;
717 int nchan = spwrow->getNumChan() ;
[2227]718 freqref = "TOPO" ;
719 if ( spwrow->isMeasFreqRefExists() )
720 freqref = CFrequencyReferenceCode::toString( spwrow->getMeasFreqRef() ) ;
[2197]721 if ( nchan == 1 ) {
[2208]722 //logsink_->postLocally( LogMessage("channel averaged data",LogOrigin(className_,funcName,WHERE)) ) ;
[2197]723 refpix = 0.0 ;
724 incr = spwrow->getTotBandwidth().get() ;
725 if ( spwrow->isChanFreqStartExists() ) {
726 refval = spwrow->getChanFreqStart().get() ;
727 }
728 else if ( spwrow->isChanFreqArrayExists() ) {
729 refval = spwrow->getChanFreqArray()[0].get() ;
730 }
731 else {
732 throw (casa::AipsError( "Either chanFreqArray or chanFreqStart must exist." )) ;
733 }
734 }
735 else if ( nchan % 2 ) {
736 // odd
[2208]737 //logsink_->postLocally( LogMessage("odd case",LogOrigin(className_,funcName,WHERE)) ) ;
[2197]738 refpix = 0.5 * ( (double)nchan - 1.0 ) ;
739 int ic = ( nchan - 1 ) / 2 ;
740 if ( spwrow->isChanWidthExists() ) {
741 incr = spwrow->getChanWidth().get() ;
742 }
743 else if ( spwrow->isChanWidthArrayExists() ) {
744 incr = spwrow->getChanWidthArray()[0].get() ;
745 }
746 else {
747 throw (casa::AipsError( "Either chanWidthArray or chanWidth must exist." )) ;
748 }
749 if ( spwrow->isChanFreqStepExists() ) {
750 if ( spwrow->getChanFreqStep().get() < 0.0 )
751 incr *= -1.0 ;
752 }
753 else if ( spwrow->isChanFreqArrayExists() ) {
754 vector<Frequency> chanFreqArr = spwrow->getChanFreqArray() ;
755 if ( chanFreqArr[0].get() > chanFreqArr[1].get() )
756 incr *= -1.0 ;
757 }
758 else {
759 throw (casa::AipsError( "Either chanFreqArray or chanFreqStep must exist." )) ;
760 }
761 if ( spwrow->isChanFreqStartExists() ) {
762 refval = spwrow->getChanFreqStart().get() + refpix * incr ;
763 }
764 else if ( spwrow->isChanFreqArrayExists() ) {
765 refval = spwrow->getChanFreqArray()[ic].get() ;
766 }
767 else {
768 throw (casa::AipsError( "Either chanFreqArray or chanFreqStart must exist." )) ;
769 }
770 }
771 else {
772 // even
[2208]773 //logsink_->postLocally( LogMessage("even case",LogOrigin(className_,funcName,WHERE)) ) ;
[2197]774 refpix = 0.5 * ( (double)nchan - 1.0 ) ;
775 int ic = nchan / 2 ;
776 if ( spwrow->isChanWidthExists() ) {
777 incr = spwrow->getChanWidth().get() ;
778 }
779 else if ( spwrow->isChanWidthArrayExists() ) {
780 incr = spwrow->getChanWidthArray()[0].get() ;
781 }
782 else {
783 throw (casa::AipsError( "Either chanWidthArray or chanWidth must exist." )) ;
784 }
785 if ( spwrow->isChanFreqStepExists() ) {
786 if ( spwrow->getChanFreqStep().get() < 0.0 )
787 incr *= -1.0 ;
788 }
789 else if ( spwrow->isChanFreqArrayExists() ) {
790 vector<Frequency> chanFreqArr = spwrow->getChanFreqArray() ;
791 if ( chanFreqArr[0].get() > chanFreqArr[1].get() )
792 incr *= -1.0 ;
793 }
794 else {
795 throw (casa::AipsError( "Either chanFreqArray or chanFreqStep must exist." )) ;
796 }
797 if ( spwrow->isChanFreqStartExists() ) {
798 refval = spwrow->getChanFreqStart().get() + refpix * incr ;
799 }
800 else if ( spwrow->isChanFreqArrayExists() ) {
801 vector<Frequency> freqs = spwrow->getChanFreqArray() ;
802 refval = 0.5 * ( freqs[ic-1].get() + freqs[ic].get() ) ;
803 }
804 else {
805 throw (casa::AipsError( "Either chanFreqArray or chanFreqStart must exist." )) ;
806 }
807 }
808}
809
810vector<double> ASDMReader::getRestFrequency( unsigned int idx )
811{
812 vector<double> rf( 0 ) ;
813 unsigned int index = dataIdList_[idx] ;
814 //ArrayTimeInterval tint( vmsData_->v_time[index]*s2d, vmsData_->v_interval[index]*s2d ) ;
815 double startSec = vmsData_->v_time[index] - 0.5 * vmsData_->v_interval[index] ;
816 ArrayTimeInterval tint( startSec*s2d, vmsData_->v_interval[index]*s2d ) ;
817 Tag ddtag( vmsData_->v_dataDescId[index], TagType::DataDescription ) ;
818 Tag spwtag = asdm_->getDataDescription().getRowByKey(ddtag)->getSpectralWindowId() ;
819 Tag ftag( vmsData_->v_fieldId[index], TagType::Field ) ;
820 FieldRow *frow = asdm_->getField().getRowByKey( ftag ) ;
821 if ( frow->isSourceIdExists() ) {
[2208]822 //logsink_->postLocally( LogMessage("sourceId exists",LogOrigin(className_,funcName,WHERE)) ) ;
[2197]823 int sid = frow->getSourceId() ;
824 SourceRow *srow = asdm_->getSource().getRowByKey( sid, tint, spwtag ) ;
825 if ( srow->isRestFrequencyExists() ) {
[2208]826 //logsink_->postLocally( LogMessage("restFrequency exists",LogOrigin(className_,funcName,WHERE)) ) ;
[2197]827 vector<Frequency> restfreq = srow->getRestFrequency() ;
828 rf.resize( restfreq.size() ) ;
829 for ( unsigned int i = 0 ; i < restfreq.size() ; i++ )
830 rf[i] = restfreq[i].get() ;
831 }
832 }
833 return rf ;
834}
835
836double ASDMReader::getTime( unsigned int idx )
837{
838 double tsec = vmsData_->v_time[dataIdList_[idx]] ;
839 return tsec * s2d ;
840}
841
842double ASDMReader::getInterval( unsigned int idx )
843{
844 return vmsData_->v_interval[dataIdList_[idx]] ;
845}
846
847string ASDMReader::getSourceName( unsigned int idx )
848{
849 unsigned int index = dataIdList_[idx] ;
850 //ArrayTimeInterval tint( vmsData_->v_time[index]*s2d, vmsData_->v_interval[index]*s2d ) ;
851 double startSec = vmsData_->v_time[index] - 0.5 * vmsData_->v_interval[index] ;
852 ArrayTimeInterval tint( startSec*s2d, vmsData_->v_interval[index]*s2d ) ;
853 Tag ddtag( vmsData_->v_dataDescId[index], TagType::DataDescription ) ;
854 Tag spwtag = asdm_->getDataDescription().getRowByKey(ddtag)->getSpectralWindowId() ;
855 Tag ftag( vmsData_->v_fieldId[index], TagType::Field ) ;
856 FieldRow *frow = asdm_->getField().getRowByKey( ftag ) ;
857 string srcname ;
858 if ( frow->isSourceIdExists() ) {
[2208]859 //logsink_->postLocally( LogMessage("sourceId exists",LogOrigin(className_,funcName,WHERE)) ) ;
[2197]860 int sid = frow->getSourceId() ;
861 SourceRow *srow = asdm_->getSource().getRowByKey( sid, tint, spwtag ) ;
862 srcname = srow->getSourceName() ;
863 }
864 else {
865 srcname = frow->getFieldName() ;
866 }
867 return srcname ;
868}
869
870string ASDMReader::getFieldName( unsigned int idx )
871{
872 int fid = vmsData_->v_fieldId[dataIdList_[idx]] ;
873 Tag ftag( fid, TagType::Field ) ;
874 FieldRow *frow = asdm_->getField().getRowByKey( ftag ) ;
875 ostringstream oss ;
876 oss << frow->getFieldName() << "__" << fid ;
877 return oss.str() ;
878}
879
880int ASDMReader::getSrcType( unsigned int scan,
881 unsigned int subscan )
882{
883 int srctype = SrcType::NOTYPE ;
884 Tag ebtag = mainRow_[row_]->getExecBlockId() ;
885 ScanRow *scanrow = asdm_->getScan().getRowByKey( ebtag, (int)scan ) ;
886 ScanIntent scanIntent = scanrow->getScanIntent()[0] ;
887 SubscanRow *subrow = asdm_->getSubscan().getRowByKey( ebtag, (int)scan, (int)subscan ) ;
888 SubscanIntent subIntent = subrow->getSubscanIntent() ;
889 SwitchingMode swmode = subrow->getSubscanMode() ;
890 if ( scanIntent == OBSERVE_TARGET ) {
891 // on sky scan
892 if ( swmode == NO_SWITCHING || swmode == POSITION_SWITCHING ) {
893 // position switching
894 // tentatively set NO_SWITCHING = POSITION_SWITCHING
895 if ( subIntent == ON_SOURCE ) {
896 srctype = SrcType::PSON ;
897 }
898 else if ( subIntent == OFF_SOURCE ) {
899 srctype = SrcType::PSOFF ;
900 }
901 }
902 else if ( swmode == FREQUENCY_SWITCHING ) {
903 // frequency switching
904 if ( subIntent == ON_SOURCE ) {
905 srctype = SrcType::FSON ;
906 }
907 else if ( subIntent == OFF_SOURCE ) {
908 srctype = SrcType::FSOFF ;
909 }
910 }
911 else if ( swmode == NUTATOR_SWITCHING ) {
912 // nutator switching
913 if ( subIntent == ON_SOURCE ) {
914 srctype = SrcType::PSON ;
915 }
916 else if ( subIntent == OFF_SOURCE ) {
917 srctype = SrcType::PSOFF ;
918 }
919 }
920 else {
921 // other switching mode
922 if ( subIntent == ON_SOURCE ) {
923 srctype = SrcType::SIG ;
924 }
925 else if ( subIntent == OFF_SOURCE ) {
926 srctype = SrcType::REF ;
927 }
928 }
929 }
[2250]930 else if ( scanIntent == CALIBRATE_ATMOSPHERE ) {
931 if ( swmode == NO_SWITCHING || swmode == POSITION_SWITCHING ) {
932 // position switching
933 // tentatively set NO_SWITCHING = POSITION_SWITCHING
934 if ( subIntent == ON_SOURCE ) {
935 srctype = SrcType::PONCAL ;
936 }
937 else if ( subIntent == OFF_SOURCE ) {
938 srctype = SrcType::POFFCAL ;
939 }
940 }
941 else if ( swmode == FREQUENCY_SWITCHING ) {
942 // frequency switching
943 if ( subIntent == ON_SOURCE ) {
944 srctype = SrcType::FONCAL ;
945 }
946 else if ( subIntent == OFF_SOURCE ) {
947 srctype = SrcType::FOFFCAL ;
948 }
949 }
950 else if ( swmode == NUTATOR_SWITCHING ) {
951 // nutator switching
952 if ( subIntent == ON_SOURCE ) {
953 srctype = SrcType::PONCAL ;
954 }
955 else if ( subIntent == OFF_SOURCE ) {
956 srctype = SrcType::POFFCAL ;
957 }
958 }
959 else {
960 // other switching mode
961 if ( subIntent == ON_SOURCE ) {
962 srctype = SrcType::CAL ;
963 }
964 else if ( subIntent == OFF_SOURCE ) {
965 srctype = SrcType::CAL ;
966 }
967 }
968 }
[2197]969 else {
970 // off sky (e.g. calibrator device) scan
971 if ( subIntent == ON_SOURCE ) {
972 srctype = SrcType::SIG ;
973 }
974 else if ( subIntent == OFF_SOURCE ) {
975 srctype = SrcType::REF ;
976 }
977 else if ( subIntent == HOT ) {
978 srctype = SrcType::HOT ;
979 }
980 else if ( subIntent == AMBIENT ) {
981 srctype = SrcType::SKY ;
982 }
983 else {
984 srctype = SrcType::CAL ;
985 }
986 }
987
988 return srctype ;
989}
990
991unsigned int ASDMReader::getSubscanNo( unsigned int idx )
992{
[2208]993 //logsink_->postLocally( LogMessage("subscan"+String::toString(vmsData_->v_msState[dataIdList_[idx]].subscanNum)+": obsmode="+String::toString(vmsData_->v_msState[dataIdList_[idx]].obsMode),LogOrigin(className_,funcName,WHERE)) ) ;
[2197]994 return vmsData_->v_msState[dataIdList_[idx]].subscanNum ;
995}
996
997vector<double> ASDMReader::getSourceDirection( unsigned int idx )
998{
999 vector<double> dir( 2, 0.0 ) ;
1000 unsigned int index = dataIdList_[idx] ;
1001 //ArrayTimeInterval tint( vmsData_->v_time[index]*s2d, vmsData_->v_interval[index]*s2d ) ;
1002 double startSec = vmsData_->v_time[index] - 0.5 * vmsData_->v_interval[index] ;
1003 ArrayTimeInterval tint( startSec*s2d, vmsData_->v_interval[index]*s2d ) ;
1004 Tag ddtag( vmsData_->v_dataDescId[index], TagType::DataDescription ) ;
1005 Tag spwtag = asdm_->getDataDescription().getRowByKey(ddtag)->getSpectralWindowId() ;
1006 Tag ftag( vmsData_->v_fieldId[index], TagType::Field ) ;
1007 FieldRow *frow = asdm_->getField().getRowByKey( ftag ) ;
1008 string srcname ;
1009 if ( frow->isSourceIdExists() ) {
[2208]1010 //logsink_->postLocally( LogMessage("sourceId exists",LogOrigin(className_,funcName,WHERE)) ) ;
[2197]1011 int sid = frow->getSourceId() ;
1012 SourceRow *srow = asdm_->getSource().getRowByKey( sid, tint, spwtag ) ;
1013 vector<Angle> srcdir = srow->getDirection() ;
[2252]1014 dir[0] = limitedAngle( srcdir[0].get() ) ;
1015 dir[1] = limitedAngle( srcdir[1].get() ) ;
[2197]1016 if ( srow->isDirectionCodeExists() ) {
1017 DirectionReferenceCode dircode = srow->getDirectionCode() ;
[2252]1018 //logsink_->postLocally( LogMessage("dircode="+CDirectionReferenceCode::toString(dircode),LogOrigin(className_,funcName,WHERE)) ) ;
[2197]1019 if ( dircode != J2000 ) {
1020 // if not J2000, need direction conversion
[2252]1021 string ref = CDirectionReferenceCode::toString( dircode ) ;
1022 double mjd = vmsData_->v_time[index] * s2d ;
1023 Tag atag( antennaId_, TagType::Antenna ) ;
1024 AntennaRow *arow = asdm_->getAntenna().getRowByKey( atag ) ;
1025 StationRow *srow = arow->getStationUsingStationId() ;
1026 vector<Length> antposL = srow->getPosition() ;
1027 casa::Vector<casa::Double> antpos( 3 ) ;
1028 for ( int i = 0 ; i < 3 ; i++ )
1029 antpos[i] = antposL[i].get() ;
1030 dir = toJ2000( dir, ref, mjd, antpos ) ;
[2197]1031 }
1032 }
1033 }
1034 return dir ;
1035}
1036
[2225]1037void ASDMReader::getSourceDirection( unsigned int idx,
1038 vector<double> &dir,
1039 string &ref )
1040{
1041 dir.resize( 2 ) ;
1042 dir[0] = 0.0 ;
1043 dir[1] = 0.0 ;
1044 ref = "J2000" ;
1045 unsigned int index = dataIdList_[idx] ;
1046 //ArrayTimeInterval tint( vmsData_->v_time[index]*s2d, vmsData_->v_interval[index]*s2d ) ;
1047 double startSec = vmsData_->v_time[index] - 0.5 * vmsData_->v_interval[index] ;
1048 ArrayTimeInterval tint( startSec*s2d, vmsData_->v_interval[index]*s2d ) ;
1049 Tag ddtag( vmsData_->v_dataDescId[index], TagType::DataDescription ) ;
1050 Tag spwtag = asdm_->getDataDescription().getRowByKey(ddtag)->getSpectralWindowId() ;
1051 Tag ftag( vmsData_->v_fieldId[index], TagType::Field ) ;
1052 FieldRow *frow = asdm_->getField().getRowByKey( ftag ) ;
1053 string srcname ;
1054 if ( frow->isSourceIdExists() ) {
1055 //logsink_->postLocally( LogMessage("sourceId exists",LogOrigin(className_,funcName,WHERE)) ) ;
1056 int sid = frow->getSourceId() ;
1057 SourceRow *srow = asdm_->getSource().getRowByKey( sid, tint, spwtag ) ;
1058 vector<Angle> srcdir = srow->getDirection() ;
1059 if ( srow->isDirectionCodeExists() ) {
1060 ref = CDirectionReferenceCode::toString( srow->getDirectionCode() ) ;
1061 }
[2252]1062 dir[0] = limitedAngle( srcdir[0].get() ) ;
1063 dir[1] = limitedAngle( srcdir[1].get() ) ;
[2225]1064 }
1065}
1066
1067void ASDMReader::getSourceDirection( vector<double> &dir, string &ref )
1068{
1069 dir.resize( 2 ) ;
1070 ref = "J2000" ; // default is J2000
1071 SourceTable &tab = asdm_->getSource() ;
1072 SourceRow *row = tab.get()[0] ;
1073 vector<Angle> dirA = row->getDirection() ;
1074 dir[0] = dirA[0].get() ;
1075 dir[1] = dirA[1].get() ;
1076 if ( row->isDirectionCodeExists() ) {
1077 ref = CDirectionReferenceCode::toString( row->getDirectionCode() ) ;
1078 }
1079}
1080
[2197]1081vector<double> ASDMReader::getSourceProperMotion( unsigned int idx )
1082{
1083 vector<double> pm( 2, 0.0 ) ;
1084 unsigned int index = dataIdList_[idx] ;
1085 //ArrayTimeInterval tint( vmsData_->v_time[index]*s2d, vmsData_->v_interval[index]*s2d ) ;
1086 double startSec = vmsData_->v_time[index] - 0.5 * vmsData_->v_interval[index] ;
1087 ArrayTimeInterval tint( startSec*s2d, vmsData_->v_interval[index]*s2d ) ;
1088 Tag ddtag( vmsData_->v_dataDescId[index], TagType::DataDescription ) ;
1089 Tag spwtag = asdm_->getDataDescription().getRowByKey(ddtag)->getSpectralWindowId() ;
1090 Tag ftag( vmsData_->v_fieldId[index], TagType::Field ) ;
1091 FieldRow *frow = asdm_->getField().getRowByKey( ftag ) ;
1092 string srcname ;
1093 if ( frow->isSourceIdExists() ) {
[2208]1094 //logsink_->postLocally( LogMessage("sourceId exists",LogOrigin(className_,funcName,WHERE)) ) ;
[2197]1095 int sid = frow->getSourceId() ;
1096 SourceRow *srow = asdm_->getSource().getRowByKey( sid, tint, spwtag ) ;
1097 vector<AngularRate> srcpm = srow->getProperMotion() ;
1098 pm[0] = srcpm[0].get() ;
1099 pm[1] = srcpm[1].get() ;
1100 }
1101 return pm ;
1102}
1103
1104double ASDMReader::getSysVel( unsigned int idx )
1105{
1106 double sysvel = 0.0 ;
1107 unsigned int index = dataIdList_[idx] ;
1108 //ArrayTimeInterval tint( vmsData_->v_time[index]*s2d, vmsData_->v_interval[index]*s2d ) ;
1109 double startSec = vmsData_->v_time[index] - 0.5 * vmsData_->v_interval[index] ;
1110 ArrayTimeInterval tint( startSec*s2d, vmsData_->v_interval[index]*s2d ) ;
1111 Tag ddtag( vmsData_->v_dataDescId[index], TagType::DataDescription ) ;
1112 Tag spwtag = asdm_->getDataDescription().getRowByKey(ddtag)->getSpectralWindowId() ;
1113 Tag ftag( vmsData_->v_fieldId[index], TagType::Field ) ;
1114 FieldRow *frow = asdm_->getField().getRowByKey( ftag ) ;
1115 string srcname ;
1116 if ( frow->isSourceIdExists() ) {
[2208]1117 //logsink_->postLocally( LogMessage("sourceId exists",LogOrigin(className_,funcName,WHERE)) ) ;
[2197]1118 int sid = frow->getSourceId() ;
1119 SourceRow *srow = asdm_->getSource().getRowByKey( sid, tint, spwtag ) ;
1120 if ( srow->isSysVelExists() ) {
1121 vector<Speed> sysvelV = srow->getSysVel() ;
1122 if ( sysvelV.size() > 0 )
1123 sysvel = sysvelV[0].get() ;
1124 }
1125 }
1126 return sysvel ;
1127}
1128
1129unsigned int ASDMReader::getFlagRow( unsigned int idx )
1130{
1131 return vmsData_->v_flag[dataIdList_[idx]] ;
1132}
1133
1134vector<unsigned int> ASDMReader::getDataShape( unsigned int idx )
1135{
1136 return vmsData_->vv_dataShape[dataIdList_[idx]] ;
1137}
1138
1139float * ASDMReader::getSpectrum( unsigned int idx )
1140{
1141 map<AtmPhaseCorrection, float*> data = vmsData_->v_m_data[dataIdList_[idx]] ;
1142 //map<AtmPhaseCorrection, float*>::iterator iter = data.find(AP_UNCORRECTED) ;
1143 map<AtmPhaseCorrection, float*>::iterator iter = data.find(apc_) ;
1144 float *autoCorr = iter->second ;
1145 return autoCorr ;
1146}
1147
1148// bool * ASDMReader::getFlagChannel( unsigned int idx )
1149// {
1150// return 0 ;
1151// }
1152
1153vector< vector<float> > ASDMReader::getTsys( unsigned int idx )
1154{
1155 vector< vector<float> > defaultTsys( 1, vector<float>( 1, 1.0 ) ) ;
[2251]1156 SysCalRow *scrow = getSysCalRow( idx ) ;
[2249]1157 if ( scrow != 0 && scrow->isTsysSpectrumExists() ) {
[2197]1158 vector< vector<Temperature> > tsysSpec = scrow->getTsysSpectrum() ;
1159 unsigned int numReceptor = tsysSpec.size() ;
1160 unsigned int numChan = tsysSpec[0].size() ;
1161 vector< vector<float> > tsys( numReceptor, vector<float>( numChan, 1.0 ) ) ;
1162 for ( unsigned int ir = 0 ; ir < numReceptor ; ir++ ) {
1163 for ( unsigned int ic = 0 ; ic < numChan ; ic++ ) {
1164 tsys[ir][ic] = tsysSpec[ir][ic].get() ;
1165 }
1166 }
1167 return tsys ;
1168 }
1169// else if ( scrow->isTsysExists() ) {
1170// vector<Temperature> tsysScalar = scrow->getTsys() ;
1171// unsigned int numReceptor = tsysScalar.size() ;
1172// vector< vector<float> > tsys( numReceptor ) ;
1173// for ( unsigned int ir = 0 ; ir < numReceptor ; ir++ )
1174// tsys[ir] = vector<float>( 1, tsysScalar[ir].get() ) ;
1175// return tsys ;
1176// }
1177 else {
1178 return defaultTsys ;
1179 }
1180}
1181
1182vector< vector<float> > ASDMReader::getTcal( unsigned int idx )
1183{
1184 vector< vector<float> > defaultTcal( 1, vector<float>( 1, 1.0 ) ) ;
[2251]1185 SysCalRow *scrow = getSysCalRow( idx ) ;
[2249]1186 if ( scrow != 0 && scrow->isTcalSpectrumExists() ) {
[2197]1187 vector< vector<Temperature> > tcalSpec = scrow->getTcalSpectrum() ;
1188 unsigned int numReceptor = tcalSpec.size() ;
1189 unsigned int numChan = tcalSpec[0].size() ;
1190 vector< vector<float> > tcal( numReceptor, vector<float>( numChan, 1.0 ) ) ;
1191 for ( unsigned int ir = 0 ; ir < numReceptor ; ir++ ) {
1192 for ( unsigned int ic = 0 ; ic < numChan ; ic++ ) {
1193 tcal[ir][ic] = tcalSpec[ir][ic].get() ;
1194 }
1195 }
1196 return tcal ;
1197 }
1198// else if ( scrow->isTcalExists() ) {
1199// vector<Temperature> tcalScalar = scrow->getTcal() ;
1200// unsigned int numReceptor = tcalScalar.size() ;
1201// vector< vector<float> > tcal( numReceptor, vector<float>( 1, 1.0 ) ) ;
1202// for ( unsigned int ir = 0 ; ir < numReceptor ; ir++ )
1203// tcal[ir][0] = tcalScalar[ir][0].get() ;
1204// return tcal ;
1205// }
1206 else {
1207 return defaultTcal ;
1208 }
1209}
1210
[2251]1211void ASDMReader::getTcalAndTsys( unsigned int idx,
1212 vector< vector<float> > &tcal,
1213 vector< vector<float> > &tsys )
1214{
1215 String funcName = "getTcalAndTsys" ;
1216
1217 vector< vector<float> > defaultT( 1, vector<float>( 1, 1.0 ) ) ;
1218 SysCalRow *scrow = getSysCalRow( idx ) ;
1219 if ( scrow == 0 ) {
1220 tcal = defaultT ;
1221 tsys = defaultT ;
1222 }
1223 else {
1224 if ( scrow->isTsysSpectrumExists() ) {
1225 vector< vector<Temperature> > tsysSpec = scrow->getTsysSpectrum() ;
1226 unsigned int numReceptor = tsysSpec.size() ;
1227 unsigned int numChan = tsysSpec[0].size() ;
1228 tsys.resize( numReceptor ) ;
1229 for ( unsigned int ir = 0 ; ir < numReceptor ; ir++ ) {
1230 tsys[ir].resize( numChan ) ;
1231 for ( unsigned int ic = 0 ; ic < numChan ; ic++ ) {
1232 tsys[ir][ic] = tsysSpec[ir][ic].get() ;
1233 }
1234 }
1235 }
1236 else {
1237 tsys = defaultT ;
1238 }
1239 if ( scrow->isTcalSpectrumExists() ) {
1240 vector< vector<Temperature> > tcalSpec = scrow->getTcalSpectrum() ;
1241 unsigned int numReceptor = tcalSpec.size() ;
1242 unsigned int numChan = tcalSpec[0].size() ;
1243 tcal.resize( numReceptor ) ;
1244 for ( unsigned int ir = 0 ; ir < numReceptor ; ir++ ) {
1245 tcal[ir].resize( numReceptor ) ;
1246 for ( unsigned int ic = 0 ; ic < numChan ; ic++ ) {
1247 tcal[ir][ic] = tcalSpec[ir][ic].get() ;
1248 }
1249 }
1250 }
1251 else {
1252 tcal = defaultT ;
1253 }
1254 }
1255}
1256
[2197]1257vector<float> ASDMReader::getOpacity( unsigned int idx )
1258{
1259 vector<float> tau(0) ;
1260 CalAtmosphereTable &atmtab = asdm_->getCalAtmosphere() ;
1261 unsigned int nrow = atmtab.size() ;
1262 if ( nrow > 0 ) {
1263 unsigned int index = dataIdList_[idx] ;
1264 //ArrayTimeInterval tint( vmsData_->v_time[index]*s2d, vmsData_->v_interval[index]*s2d ) ;
1265 double startSec = vmsData_->v_time[index] - 0.5 * vmsData_->v_interval[index] ;
1266 ArrayTimeInterval tint( startSec*s2d, vmsData_->v_interval[index]*s2d ) ;
1267 //int feedid = vmsData_->v_feedId1[index] ;
1268 //Tag atag( antennaId_, TagType::Antenna ) ;
1269 //Tag ddtag( vmsData_->v_dataDescId[index], TagType::DataDescription ) ;
1270 //DataDescriptionRow *ddrow = asdm_->getDataDescription().getRowByKey(ddtag) ;
1271 //Tag spwtag = ddrow->getSpectralWindowId() ;
1272 //SpectralWindowRow *spwrow = ddrow->getSpectralWindowUsingSpectralWindowId() ;
1273 //BasebandName bbname = spwrow->getBasebandName() ;
1274 //FeedRow *frow = asdm_->getFeed().getRowByKey( atag, spwtag, tint, feedid ) ;
1275 //int nfeed = frow->getNumReceptor() ;
1276 //vector<ReceiverRow *> rrows = frow->getReceivers() ;
1277 vector<CalAtmosphereRow *> atmrows = atmtab.get() ;
1278 //ReceiverBand rb = rrows[0]->getFrequencyBand() ;
1279 int row0 = -1 ;
1280 double eps = tint.getStart().getMJD() ;
1281 for ( unsigned int irow = 0 ; irow < nrow ; irow++ ) {
1282 CalAtmosphereRow *atmrow = atmrows[irow] ;
1283 if ( casa::String(atmrow->getAntennaName()) != antennaName_
1284 //|| atmrow->getReceiverBand() != rb
1285 //|| atmrow->getBasebandName() != bbname
1286 || atmrow->getCalDataUsingCalDataId()->getCalType() != CAL_ATMOSPHERE )
1287 continue ;
1288 else {
1289 double dt = tint.getStart().getMJD() - atmrow->getEndValidTime().getMJD() ;
1290 if ( dt >= 0 && dt < eps ) {
1291 eps = dt ;
1292 row0 = (int)irow ;
1293 }
1294 }
1295 }
1296 if ( row0 != -1 ) {
1297 CalAtmosphereRow *atmrow = atmrows[row0] ;
1298 tau = atmrow->getTau() ;
1299 }
1300 }
[2229]1301 else {
1302 tau.resize( 1 ) ;
1303 tau[0] = 0.0 ;
1304 }
[2197]1305 return tau ;
1306}
1307
1308void ASDMReader::getWeatherInfo( unsigned int idx,
1309 float &temperature,
1310 float &pressure,
1311 float &humidity,
1312 float &windspeed,
1313 float &windaz )
1314{
[2235]1315 casa::String funcName = "getWeatherInfo" ;
1316
[2197]1317 temperature = 0.0 ;
1318 pressure = 0.0 ;
1319 humidity = 0.0 ;
1320 windspeed = 0.0 ;
1321 windaz = 0.0 ;
1322
[2208]1323 //logsink_->postLocally( LogMessage("weatherStationId_ = "+String::toString(weatherStationId_),LogOrigin(className_,funcName,WHERE)) ) ;
[2197]1324
1325 WeatherTable &wtab = asdm_->getWeather() ;
1326 if ( wtab.size() == 0 || weatherStationId_ == -1 )
1327 return ;
1328
1329 unsigned int index = dataIdList_[idx] ;
1330 //Tag anttag( antennaId_, TagType::Antenna ) ;
1331 //Tag sttag = (asdm_->getAntenna().getRowByKey( anttag ))->getStationId() ;
1332 Tag sttag( (unsigned int)weatherStationId_, TagType::Station ) ;
1333 //ArrayTimeInterval tint( vmsData_->v_time[index]*s2d, vmsData_->v_interval[index]*s2d ) ;
1334 double startSec = vmsData_->v_time[index] - 0.5 * vmsData_->v_interval[index] ;
1335 ArrayTimeInterval tint( startSec*s2d, vmsData_->v_interval[index]*s2d ) ;
1336 //WeatherRow *wrow = wtab.getRowByKey( sttag, tint ) ;
1337 vector<WeatherRow *> *wrows = wtab.getByContext( sttag ) ;
1338 WeatherRow *wrow = (*wrows)[0] ;
1339 unsigned int nrow = wrows->size() ;
[2208]1340 //logsink_->postLocally( LogMessage("There are "+String::toString(nrow)+" rows for given context: stationId "+String::toString(weatherStationId_),LogOrigin(className_,funcName,WHERE)) ) ;
[2235]1341 ArrayTime startTime = getMidTime( tint ) ;
[2197]1342 if ( startTime < (*wrows)[0]->getTimeInterval().getStart() ) {
1343 temperature = (*wrows)[0]->getTemperature().get() ;
[2235]1344 pressure = (*wrows)[0]->getPressure().get() ;
[2197]1345 humidity = (*wrows)[0]->getRelHumidity().get() ;
1346 windspeed = (*wrows)[0]->getWindSpeed().get() ;
1347 windaz = (*wrows)[0]->getWindDirection().get() ;
1348 }
[2235]1349 else if ( startTime > getEndTime( (*wrows)[nrow-1]->getTimeInterval() ) ) {
[2197]1350 temperature = (*wrows)[nrow-1]->getTemperature().get() ;
1351 pressure = (*wrows)[nrow-1]->getPressure().get() ;
1352 humidity = (*wrows)[nrow-1]->getRelHumidity().get() ;
1353 windspeed = (*wrows)[nrow-1]->getWindSpeed().get() ;
1354 windaz = (*wrows)[nrow-1]->getWindDirection().get() ;
1355 }
1356 else {
1357 for ( unsigned int irow = 1 ; irow < wrows->size() ; irow++ ) {
1358 wrow = (*wrows)[irow-1] ;
[2235]1359 ArrayTime tStart = wrow->getTimeInterval().getStart() ;
1360 ArrayTime tEnd = (*wrows)[irow]->getTimeInterval().getStart() ;
1361 if ( startTime >= tStart && startTime <= tEnd ) {
[2197]1362 temperature = wrow->getTemperature().get() ;
1363 pressure = wrow->getPressure().get() ;
1364 humidity = wrow->getRelHumidity().get() ;
1365 windspeed = wrow->getWindSpeed().get() ;
1366 windaz = wrow->getWindDirection().get() ;
1367 break ;
1368 }
1369 }
1370 }
1371
[2235]1372 // Pa -> hPa
1373 pressure /= 100.0 ;
1374
[2197]1375 return ;
1376}
1377
1378void ASDMReader::processStation()
1379{
1380 antennaPad_.resize(0) ;
1381 weatherStation_.resize(0) ;
1382 StationTable &stab = asdm_->getStation() ;
1383 vector<StationRow *> srows = stab.get() ;
1384 for ( unsigned int irow = 0 ; irow < srows.size() ; irow++ ) {
1385 StationType stype = srows[irow]->getType() ;
1386 Tag stag = srows[irow]->getStationId() ;
1387 if ( stype == ANTENNA_PAD )
1388 antennaPad_.push_back( stag ) ;
1389 else if ( stype == WEATHER_STATION )
1390 weatherStation_.push_back( stag ) ;
1391 }
1392
1393 weatherStationId_ = getClosestWeatherStation() ;
1394}
1395
1396int ASDMReader::getClosestWeatherStation()
1397{
1398 if ( weatherStation_.size() == 0 )
1399 return -1 ;
1400
1401 Tag atag( antennaId_, TagType::Antenna ) ;
1402 Tag stag = (asdm_->getAntenna().getRowByKey( atag ))->getStationId() ;
1403 vector<double> apos( 3 ) ;
1404 StationTable &stab = asdm_->getStation() ;
1405 StationRow *srow = stab.getRowByKey( stag ) ;
1406 vector<Length> pos = srow->getPosition() ;
1407 apos[0] = pos[0].get() ;
1408 apos[1] = pos[1].get() ;
1409 apos[2] = pos[2].get() ;
1410
1411 double eps = 1.0e20 ;
1412 int retval = -1 ;
1413 for ( unsigned int ir = 0 ; ir < weatherStation_.size() ; ir++ ) {
1414 srow = stab.getRowByKey( weatherStation_[ir] ) ;
1415 vector<Length> wpos = srow->getPosition() ;
1416 double dist = (apos[0]-wpos[0].get())*(apos[0]-wpos[0].get())
1417 + (apos[1]-wpos[1].get())*(apos[1]-wpos[1].get())
1418 + (apos[2]-wpos[2].get())*(apos[2]-wpos[2].get()) ;
1419 if ( dist < eps ) {
1420 retval = (int)(weatherStation_[ir].getTagValue()) ;
1421 }
1422 }
1423
1424 return retval ;
1425}
1426
1427void ASDMReader::getPointingInfo( unsigned int idx,
1428 vector<double> &dir,
1429 double &az,
1430 double &el,
1431 vector<double> &srate )
1432{
[2208]1433 String funcName = "getPointingInfo" ;
1434
[2197]1435 dir.resize(0) ;
1436 az = -1.0 ;
1437 el = -1.0 ;
1438 srate.resize(0) ;
1439
1440 Tag atag( antennaId_, TagType::Antenna ) ;
1441 unsigned int index = dataIdList_[idx] ;
1442 vector<PointingRow *> *prows = asdm_->getPointing().getByContext( atag ) ;
1443 PointingRow *prow ;
1444 PointingRow *qrow ;
1445 //ArrayTimeInterval tint( vmsData_->v_time[index]*s2d, vmsData_->v_interval[index]*s2d ) ;
1446 double startSec = vmsData_->v_time[index] - 0.5 * vmsData_->v_interval[index] ;
1447 ArrayTimeInterval tint( startSec*s2d, vmsData_->v_interval[index]*s2d ) ;
1448
1449 unsigned int nrow = prows->size() ;
[2208]1450 //logsink_->postLocally( LogMessage("There are " << nrow << " rows for given context: antennaId "+String::toString(antennaId_),LogOrigin(className_,funcName,WHERE)) ) ;
[2197]1451
1452// for ( unsigned int irow = 0 ; irow < nrow ; irow++ ) {
1453// prow = (*prows)[irow] ;
1454// ArrayTimeInterval ati = prow->getTimeInterval() ;
1455// ArrayTime pst = ati.getStart() ;
1456// ArrayTime pet( ati.getStartInMJD()+ati.getDurationInDays() ) ;
[2208]1457// logsink_->postLocally( LogMessage("start: "+pst.toFITS(),LogOrigin(className_,funcName,WHERE)) ) ;
1458// logsink_->postLocally( LogMessage("end: "+pet.toFITS(),LogOrigin(className_,funcName,WHERE)) ) ;
[2197]1459// }
1460
1461 if ( nrow == 0 )
1462 return ;
1463
1464 srate.resize(2) ;
1465 srate[0] = 0.0 ;
1466 srate[1] = 0.0 ;
1467 az = 0.0 ;
1468 el = 0.0 ;
1469 double tcen = 0.0 ;
1470
1471 //
1472 // shape of pointingDirection is (numSample,2) if usePolynomial = False, while it is
1473 // (numTerm,2) if usePolynomial = True.
1474 //
1475 // In the former case, typical sampled time interval is 48msec, which is very small
1476 // compared with typical integration time (~a few sec).
1477 // Scan rate for this case is always [0,0] (or get slope?).
1478 //
1479 // In the later case, scan rate is (pointingDirection[1][0],pointingDirection[1][1])
1480 //
1481 // PointingDirection seems to store AZEL
1482 //
1483 ArrayTimeInterval pTime0 = (*prows)[0]->getTimeInterval() ;
1484 ArrayTimeInterval pTime1 = (*prows)[nrow-1]->getTimeInterval() ;
1485 if ( tint.getStartInMJD()+tint.getDurationInDays() < pTime0.getStartInMJD() ) {
[2208]1486 logsink_->postLocally( LogMessage( "ArrayTimeInterval out of bounds: no data for given position (tint < ptime)", LogOrigin(className_,funcName,WHERE), LogMessage::WARN ) ) ;
[2197]1487 prow = (*prows)[0] ;
1488 //vector< vector<Angle> > dirA = prow->getPointingDirection() ;
1489 vector< vector<Angle> > dirA = prow->getTarget() ;
1490 vector< vector<Angle> > offA = prow->getOffset() ;
1491 //az = dirA[0][0].get() ;
1492 //el = dirA[0][1].get() ;
1493 az = dirA[0][0].get() + offA[0][0].get() ;
1494 el = dirA[0][1].get() + offA[0][1].get() ;
1495 if ( prow->getUsePolynomials() && prow->getNumTerm() > 1 ) {
1496 //srate[0] = dirA[1][0].get() ;
1497 //srate[1] = dirA[1][1].get() ;
1498 srate[0] = dirA[1][0].get() + offA[1][0].get() ;
1499 srate[1] = dirA[1][1].get() + offA[1][1].get() ;
1500 }
1501 }
1502 else if ( tint.getStartInMJD() > pTime1.getStartInMJD()+pTime1.getDurationInDays() ) {
[2208]1503 logsink_->postLocally( LogMessage( "ArrayTimeInterval out of bounds: no data for given position (tint > ptime)", LogOrigin(className_,funcName,WHERE), LogMessage::WARN ) ) ;
[2197]1504 prow = (*prows)[nrow-1] ;
1505 int numSample = prow->getNumSample() ;
1506 //vector< vector<Angle> > dirA = prow->getPointingDirection() ;
1507 vector< vector<Angle> > dirA = prow->getTarget() ;
1508 vector< vector<Angle> > offA = prow->getOffset() ;
1509 if ( prow->getUsePolynomials() ) {
1510 //az = dirA[0][0].get() ;
1511 //el = dirA[0][1].get() ;
1512 az = dirA[0][0].get() + offA[0][0].get() ;
1513 el = dirA[0][1].get() + offA[0][1].get() ;
1514 if ( prow->getNumTerm() > 1 ) {
1515 //srate[0] = dirA[1][0].get() ;
1516 //srate[1] = dirA[1][1].get() ;
1517 srate[0] = dirA[1][0].get() + offA[1][0].get() ;
1518 srate[1] = dirA[1][1].get() + offA[1][0].get() ;
1519 }
1520 }
1521 else {
1522 //az = dirA[numSample-1][0].get() ;
1523 //el = dirA[numSample-1][1].get() ;
1524 az = dirA[numSample-1][0].get() + offA[numSample-1][0].get() ;
1525 el = dirA[numSample-1][1].get() + offA[numSample-1][1].get() ;
1526 }
1527 }
1528 else {
1529 ArrayTime startTime = tint.getStart() ;
1530 ArrayTime endTime( tint.getStartInMJD()+tint.getDurationInDays() ) ;
1531 int row0 = -1 ;
1532 int row1 = -1 ;
1533 int row2 = -1 ;
1534 double dt0 = getMidTime( tint ).getMJD() ;
1535 for ( unsigned int irow = 0 ; irow < nrow ; irow++ ) {
1536 prow = (*prows)[irow] ;
1537 double dt = getMidTime( tint ).getMJD() - getMidTime( prow->getTimeInterval() ).getMJD() ;
1538 if ( dt > 0 && dt < dt0 ) {
1539 dt0 = dt ;
1540 row2 = irow ;
1541 }
1542 if ( prow->getTimeInterval().contains( startTime ) )
1543 row0 = irow ;
1544 else if ( prow->getTimeInterval().contains( endTime ) )
1545 row1 = irow ;
1546 if ( row0 != -1 && row1 != -1 )
1547 break ;
1548 }
[2208]1549 //logsink_->postLocally( LogMessage("row0 = "+String::toString(row0)+", row1 = "+String::toString(row1)+", row2 = "+String::toString(row2),LogOrigin(className_,funcName,WHERE)) ) ;
[2197]1550 if ( row0 == -1 && row1 == -1 ) {
1551 prow = (*prows)[row2] ;
1552 qrow = (*prows)[row2+1] ;
1553 double t0 = getEndTime( prow->getTimeInterval() ).getMJD() ;
1554 double t1 = qrow->getTimeInterval().getStartInMJD() ;
1555 double t = startTime.getMJD() ;
1556 //vector< vector<Angle> > dirA = prow->getPointingDirection() ;
1557 //vector< vector<Angle> > dirB = qrow->getPointingDirection() ;
1558 vector< vector<Angle> > dirA = prow->getTarget() ;
1559 vector< vector<Angle> > offA = prow->getOffset() ;
1560 vector< vector<Angle> > dirB = qrow->getTarget() ;
1561 vector< vector<Angle> > offB = qrow->getOffset() ;
1562 //double da0 = dirA[0][0].get() ;
1563 //double db0 = dirB[0][0].get() ;
1564 //double da1 = dirA[0][1].get() ;
1565 //double db1 = dirB[0][1].get() ;
1566 double da0 = dirA[0][0].get() + offA[0][0].get() ;
1567 double db0 = dirB[0][0].get() + offB[0][0].get() ;
1568 double da1 = dirA[0][1].get() + offA[0][1].get() ;
1569 double db1 = dirB[0][1].get() + offB[0][1].get() ;
1570 if ( prow->getUsePolynomials() && qrow->getUsePolynomials() ) {
1571 double dt = ( t - t0 ) / ( t1 - t0 ) ;
1572 az = da0 + ( db0 - da0 ) * dt ;
1573 el = da1 + ( db1 - da1 ) * dt ;
1574 if ( prow->getNumTerm() > 0 && qrow->getNumTerm() > 1 ) {
1575 //double ra0 = dirA[1][0].get() ;
1576 //double rb0 = dirB[1][0].get() ;
1577 //double ra1 = dirA[1][1].get() ;
1578 //double rb1 = dirB[1][1].get() ;
1579 double ra0 = dirA[1][0].get() + offA[1][0].get() ;
1580 double rb0 = dirB[1][0].get() + offB[1][0].get() ;
1581 double ra1 = dirA[1][1].get() + offA[1][1].get() ;
1582 double rb1 = dirB[1][1].get() + offB[1][1].get() ;
1583 srate[0] = ra0 + ( rb0 - ra0 ) * dt ;
1584 srate[1] = ra1 + ( rb1 - ra1 ) * dt ;
1585 }
1586 }
1587 else if ( !(qrow->getUsePolynomials()) ) {
1588 double dt = ( t - t0 ) / ( t1 - t0 ) ;
1589 az = da0 + ( db0 - da0 ) * dt ;
1590 el = da1 + ( db1 - da1 ) * dt ;
1591 }
1592 //else {
1593 // nothing to do
1594 //}
1595 }
1596 else {
1597 int ndir = 0 ;
1598 if ( row0 == -1 ) {
1599 row0 = row1 ;
1600 }
1601 else if ( row1 == -1 ) {
1602 row1 = row0 ;
1603 }
1604 prow = (*prows)[row0] ;
1605 qrow = (*prows)[row1] ;
1606 if ( prow->getUsePolynomials() && qrow->getUsePolynomials() ) {
[2208]1607 //logsink_->postLocally( LogMessage("usePolynomial = True",LogOrigin(className_,funcName,WHERE)) ) ;
[2197]1608 if ( row0 == row1 ) {
1609 prow = (*prows)[row0] ;
1610 //vector< vector<Angle> > dirA = prow->getPointingDirection() ;
1611 vector< vector<Angle> > dirA = prow->getTarget() ;
1612 vector< vector<Angle> > offA = prow->getOffset() ;
1613 //az = dirA[0][0].get() ;
1614 //el = dirA[0][1].get() ;
1615 az = dirA[0][0].get() + offA[0][0].get() ;
1616 el = dirA[0][1].get() + offA[0][1].get() ;
1617 if ( prow->getNumTerm() > 1 ) {
1618 //srate[0] = dirA[1][0].get() ;
1619 //srate[1] = dirA[1][1].get() ;
1620 srate[0] = dirA[1][0].get() + offA[1][0].get() ;
1621 srate[1] = dirA[1][1].get() + offA[1][0].get() ;
1622 }
1623 }
1624 else {
1625 prow = (*prows)[row0] ;
1626 qrow = (*prows)[row1] ;
1627 //vector< vector<Angle> > dirA = qrow->getPointingDirection() ;
1628 //vector< vector<Angle> > dirB = qrow->getPointingDirection() ;
1629 vector< vector<Angle> > dirA = qrow->getTarget() ;
1630 vector< vector<Angle> > offA = prow->getOffset() ;
1631 vector< vector<Angle> > dirB = qrow->getTarget() ;
1632 vector< vector<Angle> > offB = qrow->getOffset() ;
1633 double t0 = getMidTime( prow->getTimeInterval() ).getMJD() ;
1634 double t1 = getMidTime( qrow->getTimeInterval() ).getMJD() ;
1635 double t = startTime.getMJD() ;
1636 double dt = ( t - t0 ) / ( t1 - t0 ) ;
1637 //double da0 = dirA[0][0].get() ;
1638 //double db0 = dirB[0][0].get() ;
1639 //double da1 = dirA[0][1].get() ;
1640 //double db1 = dirB[0][1].get() ;
1641 double da0 = dirA[0][0].get() + offA[0][0].get() ;
1642 double db0 = dirB[0][0].get() + offB[0][0].get() ;
1643 double da1 = dirA[0][1].get() + offA[0][1].get() ;
1644 double db1 = dirB[0][1].get() + offB[0][1].get() ;
1645 az = da0 + ( db0 - da0 ) * dt ;
1646 el = da1 + ( db1 - db0 ) * dt ;
1647 if ( prow->getNumTerm() > 0 && qrow->getNumTerm() > 1 ) {
1648 //double ra0 = dirA[1][0].get() ;
1649 //double rb0 = dirB[1][0].get() ;
1650 //double ra1 = dirA[1][1].get() ;
1651 //double rb1 = dirB[1][1].get() ;
1652 double ra0 = dirA[1][0].get() + offA[1][0].get() ;
1653 double rb0 = dirB[1][0].get() + offB[1][0].get() ;
1654 double ra1 = dirA[1][1].get() + offA[1][1].get() ;
1655 double rb1 = dirB[1][1].get() + offB[1][1].get() ;
1656 srate[0] = ra0 + ( rb0 - ra0 ) * dt ;
1657 srate[1] = ra1 + ( rb1 - ra1 ) * dt ;
1658 }
1659 }
1660 }
1661 else if ( prow->getUsePolynomials() == qrow->getUsePolynomials() ) {
[2208]1662 //logsink_->postLocally( LogMessage("numSample == numTerm",LogOrigin(className_,funcName,WHERE)) ) ;
[2197]1663 for ( int irow = row0 ; irow <= row1 ; irow++ ) {
1664 prow = (*prows)[irow] ;
1665 int numSample = prow->getNumSample() ;
[2208]1666 //logsink_->postLocally( LogMessage("numSample = "+String::toString(numSample),LogOrigin(className_,funcName,WHERE)) ) ;
[2197]1667 //vector< vector<Angle> > dirA = prow->getPointingDirection() ;
1668 vector< vector<Angle> > dirA = prow->getTarget() ;
1669 vector< vector<Angle> > offA = prow->getOffset() ;
1670 if ( prow->isSampledTimeIntervalExists() ) {
[2208]1671 //logsink_->postLocally( LogMessage("sampledTimeIntervalExists",LogOrigin(className_,funcName,WHERE)) ) ;
[2197]1672 vector<ArrayTimeInterval> stime = prow->getSampledTimeInterval() ;
1673 for ( int isam = 0 ; isam < numSample ; isam++ ) {
1674 //if ( tint.overlaps( stime[isam] ) ) {
1675 if ( tint.contains( stime[isam] ) ) {
1676 //az += dirA[isam][0].get() ;
1677 //el += dirA[isam][1].get() ;
1678 az += dirA[isam][0].get() + offA[isam][0].get() ;
1679 el += dirA[isam][1].get() + offA[isam][1].get() ;
1680 ndir++ ;
1681 }
1682 }
1683 }
1684 else {
1685 double sampleStart = prow->getTimeInterval().getStartInMJD() ;
1686 double sampleInterval = prow->getTimeInterval().getDurationInDays() / (double)numSample ;
[2208]1687 //logsink_->postLocally( LogMessage("sampleStart = "+String::toString(sampleStart),LogOrigin(className_,funcName,WHERE)) )
1688 //logsink_->postLocally( LogMessage("sampleInterval = "+String::toString(sampleInterval),LogOrigin(className_,funcName,WHERE)) ) ;
1689 //logsink_->postLocally( LogMessage("tint = "+tint.toString(),LogOrigin(className_,funcName,WHERE)) ) ;
[2197]1690 for ( int isam = 0 ; isam < numSample ; isam++ ) {
1691 ArrayTimeInterval stime( sampleStart+isam*sampleInterval, sampleInterval ) ;
1692 //if ( tint.overlaps( stime ) ) {
1693 if ( tint.contains( stime ) ) {
1694 //az += dirA[isam][0].get() ;
1695 //el += dirA[isam][1].get() ;
1696 az += dirA[isam][0].get() + offA[isam][0].get() ;
1697 el += dirA[isam][1].get() + offA[isam][1].get() ;
1698 tcen += getMidTime( stime ).getMJD() ;
1699 ndir++ ;
1700 }
1701 }
1702 }
1703 }
1704 if ( ndir > 1 ) {
1705 az /= (double)ndir ;
1706 el /= (double)ndir ;
1707 tcen /= (double)ndir ;
1708 }
1709 }
1710 //else {
1711 // nothing to do
1712 //}
1713 }
1714
1715 AntennaRow *arow = asdm_->getAntenna().getRowByKey( Tag( antennaId_, TagType::Antenna ) ) ;
1716 StationRow *srow = arow->getStationUsingStationId() ;
1717 vector<Length> antposL = srow->getPosition() ;
1718 casa::Vector<casa::Double> antpos( 3 ) ;
1719 for ( int i = 0 ; i < 3 ; i++ )
1720 antpos[i] = antposL[i].get() ;
[2208]1721 //logsink_->postLocally( LogMessage("tcen = "+String::toString(tcen),LogOrigin(className_,funcName,WHERE)) ) ;
1722 //logsink_->postLocally( LogMessage("antpos = "+String::toString(antpos),LogOrigin(className_,funcName,WHERE)) ) ;
[2197]1723 toJ2000( dir, az, el, tcen, antpos ) ;
1724
1725 }
1726
1727 return ;
1728}
1729
1730ArrayTime ASDMReader::getMidTime( const ArrayTimeInterval &t )
1731{
1732 return ArrayTime( t.getStartInMJD() + 0.5 * t.getDurationInDays() ) ;
1733}
1734
1735ArrayTime ASDMReader::getEndTime( const ArrayTimeInterval &t )
1736{
1737 return ArrayTime( t.getStartInMJD() + t.getDurationInDays() ) ;
1738}
1739
1740ArrayTime ASDMReader::getStartTime( const ArrayTimeInterval &t )
1741{
1742 return ArrayTime( t.getStartInMJD() ) ;
1743}
1744
1745void ASDMReader::toJ2000( vector<double> &dir,
1746 double az,
1747 double el,
1748 double mjd,
1749 casa::Vector<casa::Double> antpos )
1750{
[2208]1751 String funcName = "toJ2000" ;
1752
[2225]1753 vector<double> azel( 2 ) ;
[2197]1754 azel[0] = az ;
1755 azel[1] = el ;
[2225]1756 dir = toJ2000( azel, "AZELGEO", mjd, antpos ) ;
1757}
[2208]1758
[2225]1759vector<double> ASDMReader::toJ2000( vector<double> dir,
1760 casa::String dirref,
1761 double mjd,
1762 casa::Vector<casa::Double> antpos )
1763{
[2227]1764 casa::String funcName = "toJ2000" ;
[2225]1765
1766 vector<double> newd( dir ) ;
1767 if ( dirref != "J2000" ) {
1768 casa::MEpoch me( casa::Quantity( (casa::Double)mjd, "d" ), casa::MEpoch::UTC ) ;
1769 casa::Vector<casa::Quantity> qantpos( 3 ) ;
1770 qantpos[0] = casa::Quantity( antpos[0], "m" ) ;
1771 qantpos[1] = casa::Quantity( antpos[1], "m" ) ;
1772 qantpos[2] = casa::Quantity( antpos[2], "m" ) ;
1773 casa::MPosition mp( casa::MVPosition( qantpos ),
1774 casa::MPosition::ITRF ) ;
1775 //ostringstream oss ;
1776 //mp.print( oss ) ;
1777 //logsink_->postLocally( LogMessage(oss.str(),LogOrigin(className_,funcName,WHERE)) ) ;
1778
1779 casa::MeasFrame mf( me, mp ) ;
1780 casa::MDirection::Types dirtype ;
1781 casa::Bool b = casa::MDirection::getType( dirtype, dirref ) ;
1782 if ( b ) {
1783 casa::MDirection::Convert toj2000( dirtype,
1784 casa::MDirection::Ref( casa::MDirection::J2000, mf ) ) ;
1785 casa::Vector<casa::Double> cdir = toj2000( dir ).getAngle( "rad" ).getValue() ;
1786 //logsink_->postLocally( LogMessage("cdir = "+String::toString(cdir),LogOrigin(className_,funcName,WHERE)) ) ;
1787 newd[0] = (double)(cdir[0]) ;
1788 newd[1] = (double)(cdir[1]) ;
1789 }
1790 }
1791 return newd ;
[2197]1792}
[2208]1793
1794void ASDMReader::setLogger( CountedPtr<LogSinkInterface> &logsink )
1795{
1796 logsink_ = logsink ;
1797}
[2218]1798
1799string ASDMReader::getFrame()
1800{
[2227]1801 casa::String funcName = "getFrame" ;
[2218]1802
1803 // default is TOPO
1804 string frame = "TOPO" ;
1805
1806 SpectralWindowTable &spwtab = asdm_->getSpectralWindow() ;
1807 vector<SpectralWindowRow *> rows = spwtab.get() ;
1808 vector<FrequencyReferenceCode> measFreqRef( rows.size() ) ;
1809 int nref = 0 ;
1810 for ( unsigned int irow = 0 ; irow < rows.size() ; irow++ ) {
1811 int nchan = rows[irow]->getNumChan() ;
1812 if ( nchan != 4 ) {
1813 if ( rows[irow]->isMeasFreqRefExists() ) {
1814 measFreqRef[nref] = rows[irow]->getMeasFreqRef() ;
1815 nref++ ;
1816 }
1817 }
1818 }
1819 if ( nref != 0 ) {
1820 frame = CFrequencyReferenceCode::toString( measFreqRef[0] ) ;
1821 }
1822
[2220]1823 //logsink_->postLocally( LogMessage("frame = "+String::toString(frame),LogOrigin(className_,funcName,WHERE)) ) ;
[2218]1824
1825 return frame ;
1826}
[2220]1827
1828int ASDMReader::getNumIFs()
1829{
[2227]1830 casa::String funcName = "getNumIFs" ;
[2220]1831
1832 int nif = 0 ;
1833 vector<SpectralWindowRow *> rows = asdm_->getSpectralWindow().get() ;
1834 unsigned int nrow = rows.size() ;
1835 // check if all rows have freqGroup attribute
1836 bool freqGroupExists = true ;
1837 bool countedWvr = false ;
1838 for ( unsigned int irow = 0 ; irow < nrow ; irow++ ) {
1839 freqGroupExists &= rows[irow]->isFreqGroupExists() ;
1840 if ( rows[irow]->getNumChan() == 4 ) {
1841 if ( !countedWvr ) {
1842 countedWvr = true ;
1843 nif++ ;
1844 }
1845 }
1846 else {
1847 nif++ ;
1848 }
1849 }
1850
1851 if ( freqGroupExists ) {
1852 vector<int> freqGroup(0) ;
1853 for ( unsigned int irow = 0 ; irow < nrow ; irow++ ) {
1854 int fg = rows[irow]->getFreqGroup() ;
1855 if ( (int)count( freqGroup.begin(), freqGroup.end(), fg ) == 0 ) {
1856 freqGroup.push_back( fg ) ;
1857 }
1858 }
1859 nif = freqGroup.size() ;
1860 }
1861
1862 //logsink_->postLocally( LogMessage("nif = "+String::toString(nif),LogOrigin(className_,funcName,WHERE)) ) ;
1863
1864 return nif ;
1865}
[2251]1866
1867SysCalRow *ASDMReader::getSysCalRow( unsigned int idx )
1868{
1869 String funcName = "getSysCalRow" ;
1870
1871 SysCalRow *row = 0 ;
1872 unsigned int index = dataIdList_[idx] ;
1873 Tag anttag( antennaId_, TagType::Antenna ) ;
1874 int feedid = vmsData_->v_feedId1[index] ;
1875 //ArrayTimeInterval tint( vmsData_->v_time[index]*s2d, vmsData_->v_interval[index]*s2d ) ;
1876 double startSec = vmsData_->v_time[index] - 0.5 * vmsData_->v_interval[index] ;
1877 ArrayTimeInterval tint( startSec*s2d, vmsData_->v_interval[index]*s2d ) ;
1878 Tag ddtag( vmsData_->v_dataDescId[index], TagType::DataDescription ) ;
1879 Tag spwtag = asdm_->getDataDescription().getRowByKey(ddtag)->getSpectralWindowId() ;
1880 //int nchan = asdm_->getSpectralWindow().getRowByKey(spwtag)->getNumChan() ;
1881 vector< vector<float> > defaultTsys( 1, vector<float>( 1, 1.0 ) ) ;
1882 SysCalTable &sctab = asdm_->getSysCal() ;
1883 //vector<SysCalRow *> rows = sctab.get() ;
1884 vector<SysCalRow *> *rows = sctab.getByContext( anttag, spwtag, feedid ) ;
1885 //if ( nrow == 0 ) {
1886 if ( rows == 0 ) {
1887 //logsink_->postLocally( LogMessage("no rows in SysCal table",LogOrigin(className_,funcName,WHERE)) ) ;
1888 row = 0 ;
1889 }
1890 else {
1891 unsigned int nrow = rows->size() ;
1892 //logsink_->postLocally( LogMessage("nrow = "+String::toString(nrow),LogOrigin(className_,funcName,WHERE)) ) ;
1893 int scindex = -1 ;
1894 if ( nrow == 1 ) {
1895 scindex = 0 ;
1896 }
1897 else if ( getEndTime( tint ) <= getStartTime( (*rows)[0]->getTimeInterval() ) )
1898 scindex = 0 ;
1899 else {
1900 for ( unsigned int irow = 0 ; irow < nrow-1 ; irow++ ) {
1901 ArrayTime t = getMidTime( tint ) ;
1902 if ( t > getStartTime( (*rows)[irow]->getTimeInterval() )
1903 && t <= getStartTime( (*rows)[irow+1]->getTimeInterval() ) ) {
1904 scindex = irow ;
1905 break ;
1906 }
1907 }
1908 if ( scindex == -1 )
1909 scindex = nrow-1 ;
1910 }
1911 //logsink_->postLocally( LogMessage("irow = "+String::toString(scindex),LogOrigin(className_,funcName,WHERE)) ) ;
1912 row = (*rows)[scindex] ;
1913 }
1914 return row ;
1915}
[2252]1916
1917double ASDMReader::limitedAngle( double angle )
1918{
1919 if ( angle > C::pi )
1920 while ( angle > C::pi ) angle -= C::_2pi ;
1921 else if ( angle < -C::pi )
1922 while ( angle < -C::pi ) angle += C::_2pi ;
1923 return angle ;
1924}
Note: See TracBrowser for help on using the repository browser.