source: trunk/external-alma/atnf/PKSIO/PKSSDwriter.cc @ 3067

Last change on this file since 3067 was 1757, checked in by Kana Sugimoto, 14 years ago

New Development: Yes

JIRA Issue: Yes (CAS-2211)

Ready for Test: Yes

Interface Changes: Yes

What Interface Changed: ASAP 3.0.0 interface changes

Test Programs:

Put in Release Notes: Yes

Module(s): all the CASA sd tools and tasks are affected.

Description: Merged ATNF-ASAP 3.0.0 developments to CASA (alma) branch.

Note you also need to update casa/code/atnf.


File size: 9.3 KB
Line 
1//#---------------------------------------------------------------------------
2//# PKSSDwriter.cc: Class to write Parkes multibeam data to an SDFITS file.
3//#---------------------------------------------------------------------------
4//# livedata - processing pipeline for single-dish, multibeam spectral data.
5//# Copyright (C) 2000-2009, Australia Telescope National Facility, CSIRO
6//#
7//# This file is part of livedata.
8//#
9//# livedata is free software: you can redistribute it and/or modify it under
10//# the terms of the GNU General Public License as published by the Free
11//# Software Foundation, either version 3 of the License, or (at your option)
12//# any later version.
13//#
14//# livedata is distributed in the hope that it will be useful, but WITHOUT
15//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16//# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17//# more details.
18//#
19//# You should have received a copy of the GNU General Public License along
20//# with livedata.  If not, see <http://www.gnu.org/licenses/>.
21//#
22//# Correspondence concerning livedata may be directed to:
23//#        Internet email: mcalabre@atnf.csiro.au
24//#        Postal address: Dr. Mark Calabretta
25//#                        Australia Telescope National Facility, CSIRO
26//#                        PO Box 76
27//#                        Epping NSW 1710
28//#                        AUSTRALIA
29//#
30//# http://www.atnf.csiro.au/computing/software/livedata.html
31//# $Id: PKSSDwriter.cc,v 19.17 2009-09-29 07:33:38 cal103 Exp $
32//#---------------------------------------------------------------------------
33
34#include <atnf/PKSIO/MBrecord.h>
35#include <atnf/PKSIO/PKSSDwriter.h>
36
37#include <casa/Logging/LogIO.h>
38
39#include <casa/stdio.h>
40#include <casa/Quanta/MVTime.h>
41
42#include <string>
43#include <cstring>
44
45// Class name
46const string className = "PKSSDwriter" ;
47
48//--------------------------------------------------- PKSSDwriter::PKSSDwriter
49
50// Default constructor.
51
52PKSSDwriter::PKSSDwriter()
53{
54}
55
56//-------------------------------------------------- PKSSDwriter::~PKSSDwriter
57
58// Destructor.
59
60PKSSDwriter::~PKSSDwriter()
61{
62  close();
63}
64
65//-------------------------------------------------------- PKSSDwriter::create
66
67// Create the SDFITS file and and write static data.
68
69Int PKSSDwriter::create(
70        const String sdName,
71        const String observer,
72        const String project,
73        const String antName,
74        const Vector<Double> antPosition,
75        const String obsMode,
76        const String bunit,
77        const Float  equinox,
78        const String dopplerFrame,
79        const Vector<uInt> nChan,
80        const Vector<uInt> nPol,
81        const Vector<Bool> haveXPol,
82        const Bool   haveBase)
83{
84  const string methodName = "create()" ;
85  LogIO os( LogOrigin( className, methodName, WHERE ) ) ;
86
87  double antPos[3];
88  antPos[0] = antPosition(0);
89  antPos[1] = antPosition(1);
90  antPos[2] = antPosition(2);
91
92  cNIF = nChan.nelements();
93  if (nPol.nelements() != cNIF || haveXPol.nelements() != cNIF) {
94    os << LogIO::SEVERE << "Inconsistent number of IFs for nChan, nPol, and/or haveXPol." << LogIO::POST ;
95    return 1;
96  }
97
98  cNChan.assign(nChan);
99  cNPol.assign(nPol);
100
101  cHaveXPol.resize(cNIF);
102  for (uInt iIF = 0; iIF < cNIF; iIF++) {
103    // Convert Bool -> uInt.
104    cHaveXPol(iIF) = haveXPol(iIF) ? 1 : 0;
105  }
106
107  cHaveBase = haveBase;
108
109  // Storage in the trivial cNChan, cNPol, and cHaveXPol arrays should always
110  // be contiguous so the pointer returned by getStorage() shouldn't need to
111  // be deleted via freeStorage() (i.e. deleteIt always returned False).  This
112  // storage will, of course, be deleted when the PKSwriter object is deleted.
113  Bool deleteIt;
114  Int status = cSDwriter.create((char *)sdName.chars(),
115        (char *)observer.chars(), (char *)project.chars(),
116        (char *)antName.chars(), antPos, (char *)obsMode.chars(),
117        (char *)bunit.chars(), equinox, (char *)dopplerFrame.chars(), cNIF,
118        (int *)cNChan.getStorage(deleteIt),
119        (int *)cNPol.getStorage(deleteIt),
120        (int *)cHaveXPol.getStorage(deleteIt), (int)cHaveBase, 1);
121  //logMsg(cSDwriter.getMsg());
122  //cSDwriter.clearMsg();
123  if (status) {
124    cSDwriter.deleteFile();
125    close();
126  }
127
128  return status;
129}
130
131//--------------------------------------------------------- PKSSDwriter::write
132
133// Write the next data record.
134
135Int PKSSDwriter::write(
136        const PKSrecord &pksrec)
137{
138  const string methodName = "write()" ;
139  LogIO os( LogOrigin( className, methodName, WHERE ) ) ;
140
141  // Do basic checks.
142  Int IFno = pksrec.IFno;
143  uInt iIF = IFno - 1;
144  if (IFno < 1 || Int(cNIF) < IFno) {
145    os << LogIO::SEVERE
146       << "Invalid IF number " << IFno
147       << " (maximum " << cNIF << ")." << LogIO::POST ;
148    return 1;
149  }
150
151  uInt nChan = pksrec.spectra.nrow();
152  if (nChan != cNChan(iIF)) {
153    os << LogIO::SEVERE << "Wrong number of channels for IF " << IFno << "," << endl
154         << "got " << nChan << " should be " << cNChan(iIF) << "." << endl;
155    os << LogIO::POST ;
156    return 1;
157  }
158
159  uInt nPol = pksrec.spectra.ncolumn();
160  if (nPol != cNPol(iIF)) {
161    os << LogIO::SEVERE << "Wrong number of polarizations for IF " << IFno << "," << endl
162       << "got " << nPol << " should be " << cNPol(iIF) << "." << endl;
163    os << LogIO::POST ;
164    return 1;
165  }
166
167  // Extract calendar information frrom mjd.
168  MVTime time(pksrec.mjd);
169  Int year  = time.year();
170  Int month = time.month();
171  Int day   = time.monthday();
172
173  // Transfer data to a single-IF MBrecord.
174  MBrecord mbrec(1);
175
176  // Start with basic beam- and IF-independent bookkeeping information.
177  mbrec.scanNo  = pksrec.scanNo;
178  mbrec.cycleNo = pksrec.cycleNo;
179
180  sprintf(mbrec.datobs, "%4.4d-%2.2d-%2.2d", year, month, day);
181  mbrec.utc      = fmod(pksrec.mjd, 1.0) * 86400.0;
182  mbrec.exposure = float(pksrec.interval);
183
184  strncpy(mbrec.srcName, (char *)pksrec.srcName.chars(), 17);
185  mbrec.srcRA    = pksrec.srcDir(0);
186  mbrec.srcDec   = pksrec.srcDir(1);
187  if (pksrec.restFreq.shape()==0) {
188     mbrec.restFreq = 0;
189  }
190  else {
191     mbrec.restFreq = pksrec.restFreq(0);
192  }
193  strncpy(mbrec.obsType, (char *)pksrec.obsType.chars(), 16);
194
195  // Now beam-dependent parameters.
196  mbrec.beamNo   = pksrec.beamNo;
197  mbrec.ra       = pksrec.direction(0);
198  mbrec.dec      = pksrec.direction(1);
199  mbrec.raRate   = pksrec.scanRate(0);
200  mbrec.decRate  = pksrec.scanRate(1);
201
202  // Now IF-dependent parameters.
203  mbrec.nIF      = 1;
204  mbrec.IFno[0]  = IFno;
205  mbrec.nChan[0] = nChan;
206  mbrec.nPol[0]  = nPol;
207
208  mbrec.fqRefPix[0] = (nChan/2) + 1;
209  mbrec.fqRefVal[0] = pksrec.refFreq;
210  mbrec.fqDelt[0]   = pksrec.freqInc;
211
212  // Now the data itself.
213  for (uInt i = 0; i < pksrec.tsys.nelements(); i++) {
214    mbrec.tsys[0][i] = pksrec.tsys(i);
215  }
216
217  for (uInt ipol = 0; ipol < nPol; ipol++) {
218    mbrec.calfctr[0][ipol] = pksrec.calFctr(ipol);
219  }
220
221  if (cHaveXPol(iIF)) {
222    mbrec.xcalfctr[0][0] = pksrec.xCalFctr.real();
223    mbrec.xcalfctr[0][1] = pksrec.xCalFctr.imag();
224  } else {
225    mbrec.xcalfctr[0][0] = 0.0f;
226    mbrec.xcalfctr[0][1] = 0.0f;
227  }
228
229  if (cHaveBase) {
230    mbrec.haveBase = 1;
231
232    for (uInt ipol = 0; ipol < nPol; ipol++) {
233      mbrec.baseLin[0][ipol][0] = pksrec.baseLin(0,ipol);
234      mbrec.baseLin[0][ipol][1] = pksrec.baseLin(1,ipol);
235
236      for (uInt j = 0; j < pksrec.baseSub.nrow(); j++) {
237        mbrec.baseSub[0][ipol][j] = pksrec.baseSub(j,ipol);
238      }
239      for (uInt j = pksrec.baseSub.nrow(); j < 24; j++) {
240        mbrec.baseSub[0][ipol][j] = 0.0f;
241      }
242    }
243
244  } else {
245    mbrec.haveBase = 0;
246  }
247
248  Bool delSpectra = False;
249  const Float *specstor = pksrec.spectra.getStorage(delSpectra);
250  mbrec.spectra[0] = (float *)specstor;
251
252  Bool delFlagged = False;
253  const uChar *flagstor = pksrec.flagged.getStorage(delFlagged);
254  mbrec.flagged[0] = (unsigned char *)flagstor;
255
256  Bool delXPol = False;
257  const Complex *xpolstor;
258  if (cHaveXPol(iIF)) {
259    xpolstor = pksrec.xPol.getStorage(delXPol);
260  } else {
261    xpolstor = 0;
262  }
263  mbrec.xpol[0] = (float *)xpolstor;
264
265  // Finish off with system calibration parameters.
266  mbrec.extraSysCal = 1;
267  mbrec.refBeam     = pksrec.refBeam;
268  for (uInt i = 0; i < pksrec.tcal.nelements(); i++) {
269    mbrec.tcal[0][i] = pksrec.tcal(i);
270  }
271  strncpy(mbrec.tcalTime, (char *)pksrec.tcalTime.chars(), 16);
272  mbrec.azimuth   = pksrec.azimuth;
273  mbrec.elevation = pksrec.elevation;
274  mbrec.parAngle  = pksrec.parAngle;
275  mbrec.focusAxi  = pksrec.focusAxi;
276  mbrec.focusTan  = pksrec.focusTan;
277  mbrec.focusRot  = pksrec.focusRot;
278  mbrec.temp      = pksrec.temperature;
279  mbrec.pressure  = pksrec.pressure;
280  mbrec.humidity  = pksrec.humidity;
281  mbrec.windSpeed = pksrec.windSpeed;
282  mbrec.windAz    = pksrec.windAz;
283
284  Int status = cSDwriter.write(mbrec);
285  //logMsg(cSDwriter.getMsg());
286  //cSDwriter.clearMsg();
287  if (status) {
288    status = 1;
289  }
290
291  pksrec.spectra.freeStorage(specstor, delSpectra);
292  pksrec.flagged.freeStorage(flagstor, delFlagged);
293  pksrec.xPol.freeStorage(xpolstor, delXPol);
294
295  return status;
296}
297
298//------------------------------------------------------- PKSSDwriter::history
299
300// Write a history record.
301
302Int PKSSDwriter::history(const String text)
303{
304  return cSDwriter.history((char *)text.chars());
305}
306
307Int PKSSDwriter::history(const char *text)
308{
309  return cSDwriter.history((char *)text);
310}
311
312//--------------------------------------------------------- PKSSDwriter::close
313
314// Close the SDFITS file.
315
316void PKSSDwriter::close()
317{
318  cSDwriter.close();
319  //logMsg(cSDwriter.getMsg());
320  //cSDwriter.clearMsg();
321}
Note: See TracBrowser for help on using the repository browser.