[1795] | 1 | //
|
---|
| 2 | // C++ Implementation: PKSFiller
|
---|
| 3 | //
|
---|
| 4 | // Description:
|
---|
| 5 | //
|
---|
| 6 | //
|
---|
| 7 | // Author: Malte Marquarding <asap@atnf.csiro.au>, (C) 2010
|
---|
| 8 | //
|
---|
| 9 | // Copyright: See COPYING file that comes with this distribution
|
---|
| 10 | //
|
---|
| 11 | //
|
---|
| 12 | #include <casa/iostream.h>
|
---|
| 13 | #include <casa/iomanip.h>
|
---|
| 14 |
|
---|
| 15 | #include <casa/Exceptions.h>
|
---|
| 16 | #include <casa/OS/Path.h>
|
---|
| 17 | #include <casa/OS/File.h>
|
---|
| 18 | #include <casa/Quanta/Unit.h>
|
---|
| 19 | #include <casa/Arrays/ArrayMath.h>
|
---|
| 20 | #include <casa/Arrays/ArrayLogical.h>
|
---|
| 21 | #include <casa/Utilities/Regex.h>
|
---|
[1816] | 22 | #include <casa/Logging/LogIO.h>
|
---|
[1795] | 23 |
|
---|
| 24 | #include <measures/Measures/MDirection.h>
|
---|
| 25 | #include <measures/Measures/MeasConvert.h>
|
---|
| 26 |
|
---|
| 27 | #include <atnf/PKSIO/PKSrecord.h>
|
---|
| 28 | #include <atnf/PKSIO/PKSreader.h>
|
---|
| 29 |
|
---|
| 30 | #ifdef HAS_ALMA
|
---|
| 31 | #include <casa/System/ProgressMeter.h>
|
---|
| 32 | #endif
|
---|
| 33 | #include <casa/Logging/LogIO.h>
|
---|
| 34 |
|
---|
| 35 | #include <time.h>
|
---|
| 36 |
|
---|
| 37 | #include "STDefs.h"
|
---|
| 38 | #include "STAttr.h"
|
---|
| 39 |
|
---|
| 40 | #include "PKSFiller.h"
|
---|
| 41 | #include "STHeader.h"
|
---|
| 42 |
|
---|
| 43 | using namespace casa;
|
---|
| 44 |
|
---|
| 45 | namespace asap {
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | PKSFiller::PKSFiller( CountedPtr< Scantable > stbl ) :
|
---|
| 49 | FillerBase(stbl),
|
---|
[1815] | 50 | reader_(0)
|
---|
[1795] | 51 | {
|
---|
| 52 | setReferenceRegex(".*(e|w|_R)$");
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | PKSFiller::~PKSFiller()
|
---|
| 56 | {
|
---|
| 57 | close();
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | bool PKSFiller::open( const std::string& filename)
|
---|
| 61 | {
|
---|
| 62 | Bool haveBase, haveSpectra;
|
---|
| 63 |
|
---|
| 64 | String inName(filename);
|
---|
| 65 | Path path(inName);
|
---|
| 66 | inName = path.expandedName();
|
---|
| 67 |
|
---|
| 68 | File file(inName);
|
---|
| 69 | filename_ = inName;
|
---|
| 70 |
|
---|
| 71 | // Create reader and fill in values for arguments
|
---|
| 72 | String format;
|
---|
| 73 | Vector<Bool> beams, ifs;
|
---|
| 74 | Vector<uInt> nchans,npols;
|
---|
| 75 |
|
---|
[1832] | 76 | String antenna("0");
|
---|
[1815] | 77 |
|
---|
| 78 | reader_ = getPKSreader(inName, antenna, 0, 0, format, beams, ifs,
|
---|
| 79 | nchans, npols, haveXPol_, haveBase, haveSpectra);
|
---|
| 80 | if (reader_.null() == True) {
|
---|
[1795] | 81 | return False;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | if (!haveSpectra) {
|
---|
| 85 | reader_ = 0;
|
---|
| 86 | throw(AipsError("No spectral data in file."));
|
---|
| 87 | }
|
---|
[1816] | 88 |
|
---|
| 89 | LogIO os( LogOrigin( "PKSFiller", "open()", WHERE ) ) ;
|
---|
[1795] | 90 | nBeam_ = beams.nelements();
|
---|
| 91 | nIF_ = ifs.nelements();
|
---|
| 92 | // Get basic parameters.
|
---|
| 93 | if ( anyEQ(haveXPol_, True) ) {
|
---|
| 94 | os << "Cross polarization present" << LogIO::POST;
|
---|
| 95 | for (uInt i=0; i< npols.nelements();++i) {
|
---|
| 96 | if (npols[i] < 3) npols[i] += 2;// Convert Complex -> 2 Floats
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
[1815] | 99 | STHeader header;
|
---|
| 100 | header.nchan = max(nchans);
|
---|
| 101 | header.npol = max(npols);
|
---|
| 102 | header.nbeam = nBeam_;
|
---|
[1795] | 103 |
|
---|
[1815] | 104 | Int status = reader_->getHeader(header.observer, header.project,
|
---|
| 105 | header.antennaname, header.antennaposition,
|
---|
| 106 | header.obstype,
|
---|
| 107 | header.fluxunit,
|
---|
| 108 | header.equinox,
|
---|
| 109 | header.freqref,
|
---|
| 110 | header.utc, header.reffreq,
|
---|
| 111 | header.bandwidth);
|
---|
[1795] | 112 |
|
---|
| 113 | if (status) {
|
---|
| 114 | reader_ = 0;
|
---|
| 115 | throw(AipsError("Failed to get header."));
|
---|
| 116 | }
|
---|
[1816] | 117 | os << "Found " << header.antennaname << " data." << LogIO::POST;
|
---|
[1815] | 118 | if ((header.obstype).matches("*SW*")) {
|
---|
[1795] | 119 | // need robust way here - probably read ahead of next timestamp
|
---|
| 120 | os << "Header indicates frequency switched observation.\n"
|
---|
| 121 | << "setting # of IFs = 1 " << LogIO::POST;
|
---|
| 122 |
|
---|
| 123 | nIF_ = 1;
|
---|
[1815] | 124 | header.obstype = String("fswitch");
|
---|
[1795] | 125 | }
|
---|
| 126 | // Determine Telescope and set brightness unit
|
---|
| 127 | Bool throwIt = False;
|
---|
[1815] | 128 | Instrument inst = STAttr::convertInstrument(header.antennaname, throwIt);
|
---|
[1795] | 129 |
|
---|
| 130 | if (inst==ATMOPRA || inst==TIDBINBILLA) {
|
---|
[1815] | 131 | header.fluxunit = "K";
|
---|
[1795] | 132 | } else {
|
---|
| 133 | // downcase for use with Quanta
|
---|
[1815] | 134 | if (header.fluxunit == "JY") {
|
---|
| 135 | header.fluxunit = "Jy";
|
---|
[1795] | 136 | }
|
---|
| 137 | }
|
---|
| 138 | STAttr stattr;
|
---|
[1815] | 139 | header.poltype = stattr.feedPolType(inst);
|
---|
| 140 | header.nif = nIF_;
|
---|
| 141 | header.epoch = "UTC";
|
---|
| 142 | // *** header.frequnit = "Hz"
|
---|
[1795] | 143 | // Apply selection criteria.
|
---|
| 144 | Vector<Int> ref;
|
---|
| 145 |
|
---|
| 146 | Vector<Int> start(nIF_, 1);
|
---|
| 147 | Vector<Int> end(nIF_, 0);
|
---|
| 148 | Bool getPt = False;
|
---|
| 149 | reader_->select(beams, ifs, start, end, ref, True, haveXPol_[0], False, getPt);
|
---|
[1815] | 150 | setHeader(header);
|
---|
[1795] | 151 | //For MS, add the location of POINTING of the input MS so one get
|
---|
| 152 | //pointing data from there, if necessary.
|
---|
| 153 | //Also find nrow in MS
|
---|
| 154 | nInDataRow = 0;
|
---|
| 155 | if (format == "MS2") {
|
---|
| 156 | Path datapath(inName);
|
---|
| 157 | String ptTabPath = datapath.absoluteName();
|
---|
| 158 | Table inMS(ptTabPath);
|
---|
| 159 | nInDataRow = inMS.nrow();
|
---|
| 160 | ptTabPath.append("/POINTING");
|
---|
| 161 | table_->table().rwKeywordSet().define("POINTING", ptTabPath);
|
---|
[1815] | 162 | if (header.antennaname.matches("GBT")) {
|
---|
[1795] | 163 | String GOTabPath = datapath.absoluteName();
|
---|
| 164 | GOTabPath.append("/GBT_GO");
|
---|
| 165 | table_->table().rwKeywordSet().define("GBT_GO", GOTabPath);
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
[1815] | 168 | String freqFrame = header.freqref;
|
---|
[1795] | 169 | //translate frequency reference frame back to
|
---|
| 170 | //MS style (as PKSMS2reader converts the original frame
|
---|
| 171 | //in FITS standard style)
|
---|
| 172 | if (freqFrame == "TOPOCENT") {
|
---|
| 173 | freqFrame = "TOPO";
|
---|
| 174 | } else if (freqFrame == "GEOCENER") {
|
---|
| 175 | freqFrame = "GEO";
|
---|
| 176 | } else if (freqFrame == "BARYCENT") {
|
---|
| 177 | freqFrame = "BARY";
|
---|
| 178 | } else if (freqFrame == "GALACTOC") {
|
---|
| 179 | freqFrame = "GALACTO";
|
---|
| 180 | } else if (freqFrame == "LOCALGRP") {
|
---|
| 181 | freqFrame = "LGROUP";
|
---|
| 182 | } else if (freqFrame == "CMBDIPOL") {
|
---|
| 183 | freqFrame = "CMB";
|
---|
| 184 | } else if (freqFrame == "SOURCE") {
|
---|
| 185 | freqFrame = "REST";
|
---|
| 186 | }
|
---|
| 187 | // set both "FRAME" and "BASEFRAME"
|
---|
| 188 | table_->frequencies().setFrame(freqFrame, false);
|
---|
[1815] | 189 | table_->frequencies().setFrame(freqFrame, true);
|
---|
[1795] | 190 |
|
---|
| 191 | return true;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | void PKSFiller::close( )
|
---|
| 195 | {
|
---|
[1815] | 196 | if (reader_.null() != False) {
|
---|
| 197 | reader_ = 0;
|
---|
[1795] | 198 | }
|
---|
| 199 | table_ = 0;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | void PKSFiller::fill( )
|
---|
| 203 | {
|
---|
| 204 | int status = 0;
|
---|
[1816] | 205 | LogIO os( LogOrigin( "PKSFiller", "fill()", WHERE ) ) ;
|
---|
[1795] | 206 |
|
---|
| 207 | /**
|
---|
| 208 | Int beamNo, IFno, refBeam, scanNo, cycleNo;
|
---|
| 209 | Float azimuth, elevation, focusAxi, focusRot, focusTan,
|
---|
| 210 | humidity, parAngle, pressure, temperature, windAz, windSpeed;
|
---|
| 211 | Double bandwidth, freqInc, interval, mjd, refFreq, srcVel;
|
---|
| 212 | String fieldName, srcName, tcalTime, obsType;
|
---|
| 213 | Vector<Float> calFctr, sigma, tcal, tsys;
|
---|
| 214 | Matrix<Float> baseLin, baseSub;
|
---|
| 215 | Vector<Double> direction(2), scanRate(2), srcDir(2), srcPM(2), restFreq(1);
|
---|
| 216 | Matrix<Float> spectra;
|
---|
| 217 | Matrix<uChar> flagtra;
|
---|
| 218 | Complex xCalFctr;
|
---|
| 219 | Vector<Complex> xPol;
|
---|
| 220 | **/
|
---|
| 221 |
|
---|
| 222 | Double min = 0.0;
|
---|
| 223 | Double max = nInDataRow;
|
---|
| 224 | #ifdef HAS_ALMA
|
---|
| 225 | ProgressMeter fillpm(min, max, "Data importing progress");
|
---|
| 226 | #endif
|
---|
| 227 | PKSrecord pksrec;
|
---|
| 228 | pksrec.srcType=-1;
|
---|
| 229 | int n = 0;
|
---|
| 230 | bool isGBTFITS = false ;
|
---|
[1815] | 231 | if ((table_->getAntennaName().find( "GBT" ) != String::npos)
|
---|
[1795] | 232 | && File(filename_).isRegular()) {
|
---|
| 233 | FILE *fp = fopen( filename_.c_str(), "r" ) ;
|
---|
| 234 | fseek( fp, 640, SEEK_SET ) ;
|
---|
| 235 | char buf[81] ;
|
---|
| 236 | fread( buf, 80, 1, fp ) ;
|
---|
| 237 | buf[80] = '\0' ;
|
---|
| 238 | if ( strstr( buf, "NRAO_GBT" ) != NULL ) {
|
---|
| 239 | isGBTFITS = true ;
|
---|
| 240 | }
|
---|
| 241 | fclose( fp ) ;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | while ( status == 0 ) {
|
---|
| 245 |
|
---|
| 246 | status = reader_->read(pksrec);
|
---|
| 247 | if ( status != 0 ) break;
|
---|
| 248 | n += 1;
|
---|
| 249 | Regex filterrx(".*[SL|PA]$");
|
---|
| 250 | Regex obsrx("^AT.+");
|
---|
[1815] | 251 | if ( table_->getAntennaName().matches(obsrx) &&
|
---|
[1795] | 252 | pksrec.obsType.matches(filterrx)) {
|
---|
| 253 | //cerr << "ignoring paddle scan" << endl;
|
---|
| 254 | continue;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | Vector<Double> sratedbl(pksrec.scanRate.nelements());
|
---|
| 258 | convertArray(sratedbl, pksrec.scanRate);
|
---|
[1816] | 259 | setScanRate(sratedbl);
|
---|
[1795] | 260 |
|
---|
| 261 | Regex rx(getReferenceRegex());
|
---|
| 262 | Regex rx2("_S$");
|
---|
| 263 | Int match = pksrec.srcName.matches(rx);
|
---|
| 264 | std::string srcname;
|
---|
| 265 | if (match) {
|
---|
| 266 | srcname = pksrec.srcName;
|
---|
| 267 | } else {
|
---|
| 268 | srcname = pksrec.srcName.before(rx2);
|
---|
| 269 | }
|
---|
| 270 | Int srctype = match;
|
---|
| 271 | if ( pksrec.srcType != -1 ) {
|
---|
| 272 | srctype = pksrec.srcType ;
|
---|
| 273 | }
|
---|
| 274 | setTime(pksrec.mjd, pksrec.interval);
|
---|
| 275 | setSource(srcname, srctype, pksrec.fieldName,
|
---|
| 276 | pksrec.srcDir, pksrec.srcPM, pksrec.srcVel);
|
---|
| 277 |
|
---|
| 278 | if (nBeam_ > 1 ) {
|
---|
| 279 | setReferenceBeam(pksrec.refBeam-1);
|
---|
| 280 | }
|
---|
[1805] | 281 |
|
---|
[1795] | 282 | setMolecule(pksrec.restFreq);
|
---|
| 283 | setTcal(pksrec.tcalTime, pksrec.tcal);
|
---|
| 284 | setWeather(pksrec.temperature, pksrec.pressure,
|
---|
| 285 | pksrec.humidity, pksrec.windSpeed,
|
---|
| 286 | pksrec.windAz);
|
---|
| 287 | setFocus(pksrec.parAngle, pksrec.focusAxi, pksrec.focusTan,
|
---|
| 288 | pksrec.focusRot);
|
---|
| 289 | setDirection(pksrec.direction, pksrec.azimuth, pksrec.elevation);
|
---|
| 290 |
|
---|
[1805] | 291 | setFrequency(Double(pksrec.spectra.nrow()/2),
|
---|
| 292 | pksrec.refFreq, pksrec.freqInc);
|
---|
| 293 |
|
---|
[1816] | 294 | // Note: this is only one value for all polarisations!
|
---|
| 295 | setFlagrow(pksrec.flagrow);
|
---|
[1795] | 296 | // Turn the (nchan,npol) matrix and possible complex xPol vector
|
---|
| 297 | // into 2-4 rows in the scantable
|
---|
| 298 | Vector<Float> tsysvec(1);
|
---|
| 299 | // Why is spectra.ncolumn() == 3 for haveXPol_ == True
|
---|
| 300 | uInt npol = (pksrec.spectra.ncolumn()==1 ? 1: 2);
|
---|
| 301 | uInt polno =0;
|
---|
| 302 | for ( uInt i=0; i< npol; ++i ) {
|
---|
| 303 | tsysvec = pksrec.tsys(i);
|
---|
| 304 | if (isGBTFITS) {
|
---|
| 305 | polno = pksrec.polNo ;
|
---|
| 306 | } else {
|
---|
| 307 | polno = i;
|
---|
| 308 | }
|
---|
[1828] | 309 | setIndex(pksrec.scanNo-1, pksrec.cycleNo-1, pksrec.IFno-1, polno,
|
---|
| 310 | pksrec.beamNo-1);
|
---|
[1795] | 311 | setSpectrum(pksrec.spectra.column(i), pksrec.flagged.column(i), tsysvec);
|
---|
| 312 | commitRow();
|
---|
| 313 | }
|
---|
| 314 | if ( haveXPol_[0] ) {
|
---|
| 315 | // no tsys given for xpol, so emulate it
|
---|
| 316 | tsysvec = sqrt(pksrec.tsys[0]*pksrec.tsys[1]);
|
---|
| 317 | // add real part of cross pol
|
---|
| 318 | polno = 2;
|
---|
| 319 | Vector<Float> r(real(pksrec.xPol));
|
---|
| 320 | // make up flags from linears
|
---|
| 321 | /// @fixme this has to be a bitwise or of both pols
|
---|
| 322 | /// pksrec.flagged.column(0) | pksrec.flagged.column(1);
|
---|
| 323 |
|
---|
[1831] | 324 | setIndex(pksrec.scanNo-1, pksrec.cycleNo-1, pksrec.IFno-1, polno,
|
---|
| 325 | pksrec.beamNo-1);
|
---|
[1795] | 326 | setSpectrum(r, pksrec.flagged.column(0), tsysvec);
|
---|
| 327 | commitRow();
|
---|
| 328 |
|
---|
| 329 | // ad imaginary part of cross pol
|
---|
| 330 | polno = 3;
|
---|
| 331 | Vector<Float> im(imag(pksrec.xPol));
|
---|
[1830] | 332 | setIndex(pksrec.scanNo-1, pksrec.cycleNo-1, pksrec.IFno-1, polno,
|
---|
| 333 | pksrec.beamNo-1);
|
---|
[1795] | 334 | setSpectrum(im, pksrec.flagged.column(0), tsysvec);
|
---|
| 335 | commitRow();
|
---|
| 336 | }
|
---|
| 337 | #ifdef HAS_ALMA
|
---|
| 338 | fillpm._update(n);
|
---|
| 339 | #endif
|
---|
| 340 | }
|
---|
| 341 | if (status > 0) {
|
---|
| 342 | close();
|
---|
| 343 | throw(AipsError("Reading error occured, data possibly corrupted."));
|
---|
| 344 | }
|
---|
| 345 | #ifdef HAS_ALMA
|
---|
| 346 | fillpm.done();
|
---|
| 347 | #endif
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | }//namespace asap
|
---|