source: trunk/external/atnf/PKSIO/PKSSDwriter.cc @ 1720

Last change on this file since 1720 was 1720, checked in by Malte Marquarding, 14 years ago

Update from livedata CVS repository

File size: 9.4 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/stdio.h>
38#include <casa/Quanta/MVTime.h>
39
40//--------------------------------------------------- PKSSDwriter::PKSSDwriter
41
42// Default constructor.
43
44PKSSDwriter::PKSSDwriter()
45{
46  // By default, messages are written to stderr.
47  initMsg();
48}
49
50//-------------------------------------------------- PKSSDwriter::~PKSSDwriter
51
52// Destructor.
53
54PKSSDwriter::~PKSSDwriter()
55{
56  close();
57}
58
59//-------------------------------------------------------- PKSSDwriter::setMsg
60
61// Set message disposition.  If fd is non-zero messages will be written
62// to that file descriptor, else stored for retrieval by getMsg().
63
64Int PKSSDwriter::setMsg(FILE *fd)
65{
66  PKSmsg::setMsg(fd);
67  cSDwriter.setMsg(fd);
68
69  return 0;
70}
71
72//-------------------------------------------------------- PKSSDwriter::create
73
74// Create the SDFITS file and and write static data.
75
76Int PKSSDwriter::create(
77        const String sdName,
78        const String observer,
79        const String project,
80        const String antName,
81        const Vector<Double> antPosition,
82        const String obsMode,
83        const String bunit,
84        const Float  equinox,
85        const String dopplerFrame,
86        const Vector<uInt> nChan,
87        const Vector<uInt> nPol,
88        const Vector<Bool> haveXPol,
89        const Bool   haveBase)
90{
91  // Clear the message stack.
92  clearMsg();
93
94  double antPos[3];
95  antPos[0] = antPosition(0);
96  antPos[1] = antPosition(1);
97  antPos[2] = antPosition(2);
98
99  cNIF = nChan.nelements();
100  if (nPol.nelements() != cNIF || haveXPol.nelements() != cNIF) {
101    cerr << "PKSSDwriter::create: "
102         << "Inconsistent number of IFs for nChan, nPol, and/or haveXPol."
103         << endl;
104    return 1;
105  }
106
107  cNChan.assign(nChan);
108  cNPol.assign(nPol);
109
110  cHaveXPol.resize(cNIF);
111  for (uInt iIF = 0; iIF < cNIF; iIF++) {
112    // Convert Bool -> uInt.
113    cHaveXPol(iIF) = haveXPol(iIF) ? 1 : 0;
114  }
115
116  cHaveBase = haveBase;
117
118  // Storage in the trivial cNChan, cNPol, and cHaveXPol arrays should always
119  // be contiguous so the pointer returned by getStorage() shouldn't need to
120  // be deleted via freeStorage() (i.e. deleteIt always returned False).  This
121  // storage will, of course, be deleted when the PKSwriter object is deleted.
122  Bool deleteIt;
123  Int status = cSDwriter.create((char *)sdName.chars(),
124        (char *)observer.chars(), (char *)project.chars(),
125        (char *)antName.chars(), antPos, (char *)obsMode.chars(),
126        (char *)bunit.chars(), equinox, (char *)dopplerFrame.chars(), cNIF,
127        (int *)cNChan.getStorage(deleteIt),
128        (int *)cNPol.getStorage(deleteIt),
129        (int *)cHaveXPol.getStorage(deleteIt), (int)cHaveBase, 1);
130  logMsg(cSDwriter.getMsg());
131  cSDwriter.clearMsg();
132  if (status) {
133    cSDwriter.deleteFile();
134    close();
135  }
136
137  return status;
138}
139
140//--------------------------------------------------------- PKSSDwriter::write
141
142// Write the next data record.
143
144Int PKSSDwriter::write(
145        const PKSrecord &pksrec)
146{
147  // Do basic checks.
148  Int IFno = pksrec.IFno;
149  uInt iIF = IFno - 1;
150  if (IFno < 1 || Int(cNIF) < IFno) {
151    cerr << "PKSDwriter::write: "
152         << "Invalid IF number " << IFno
153         << " (maximum " << cNIF << ")." << endl;
154    return 1;
155  }
156
157  uInt nChan = pksrec.spectra.nrow();
158  if (nChan != cNChan(iIF)) {
159    cerr << "PKSDwriter::write: "
160         << "Wrong number of channels for IF " << IFno << "," << endl
161         << "                   "
162         << "got " << nChan << " should be " << cNChan(iIF) << "." << endl;
163    return 1;
164  }
165
166  uInt nPol = pksrec.spectra.ncolumn();
167  if (nPol != cNPol(iIF)) {
168    cerr << "PKSDwriter::write: "
169         << "Wrong number of polarizations for IF " << IFno << "," << endl
170         << "                   "
171         << "got " << nPol << " should be " << cNPol(iIF) << "." << endl;
172    return 1;
173  }
174
175  // Extract calendar information from mjd.
176  MVTime time(pksrec.mjd);
177  Int year  = time.year();
178  Int month = time.month();
179  Int day   = time.monthday();
180
181  // Transfer data to a single-IF MBrecord.
182  MBrecord mbrec(1);
183
184  // Start with basic beam- and IF-independent bookkeeping information.
185  mbrec.scanNo  = pksrec.scanNo;
186  mbrec.cycleNo = pksrec.cycleNo;
187
188  sprintf(mbrec.datobs, "%4.4d-%2.2d-%2.2d", year, month, day);
189  mbrec.utc      = fmod(pksrec.mjd, 1.0) * 86400.0;
190  mbrec.exposure = float(pksrec.interval);
191
192  strncpy(mbrec.srcName, (char *)pksrec.srcName.chars(), 17);
193  mbrec.srcRA    = pksrec.srcDir(0);
194  mbrec.srcDec   = pksrec.srcDir(1);
195
196  mbrec.restFreq = pksrec.restFreq;
197
198  strncpy(mbrec.obsType, (char *)pksrec.obsType.chars(), 16);
199
200  // Now beam-dependent parameters.
201  mbrec.beamNo   = pksrec.beamNo;
202  mbrec.ra       = pksrec.direction(0);
203  mbrec.dec      = pksrec.direction(1);
204  mbrec.raRate   = pksrec.scanRate(0);
205  mbrec.decRate  = pksrec.scanRate(1);
206
207  // Now IF-dependent parameters.
208  mbrec.nIF      = 1;
209  mbrec.IFno[0]  = IFno;
210  mbrec.nChan[0] = nChan;
211  mbrec.nPol[0]  = nPol;
212
213  mbrec.fqRefPix[0] = (nChan/2) + 1;
214  mbrec.fqRefVal[0] = pksrec.refFreq;
215  mbrec.fqDelt[0]   = pksrec.freqInc;
216
217  // Now the data itself.
218  for (uInt i = 0; i < pksrec.tsys.nelements(); i++) {
219    mbrec.tsys[0][i] = pksrec.tsys(i);
220  }
221
222  for (uInt ipol = 0; ipol < nPol; ipol++) {
223    mbrec.calfctr[0][ipol] = pksrec.calFctr(ipol);
224  }
225
226  if (cHaveXPol(iIF)) {
227    mbrec.xcalfctr[0][0] = pksrec.xCalFctr.real();
228    mbrec.xcalfctr[0][1] = pksrec.xCalFctr.imag();
229  } else {
230    mbrec.xcalfctr[0][0] = 0.0f;
231    mbrec.xcalfctr[0][1] = 0.0f;
232  }
233
234  if (cHaveBase) {
235    mbrec.haveBase = 1;
236
237    for (uInt ipol = 0; ipol < nPol; ipol++) {
238      mbrec.baseLin[0][ipol][0] = pksrec.baseLin(0,ipol);
239      mbrec.baseLin[0][ipol][1] = pksrec.baseLin(1,ipol);
240
241      for (uInt j = 0; j < pksrec.baseSub.nrow(); j++) {
242        mbrec.baseSub[0][ipol][j] = pksrec.baseSub(j,ipol);
243      }
244      for (uInt j = pksrec.baseSub.nrow(); j < 24; j++) {
245        mbrec.baseSub[0][ipol][j] = 0.0f;
246      }
247    }
248
249  } else {
250    mbrec.haveBase = 0;
251  }
252
253  Bool delSpectra = False;
254  const Float *specstor = pksrec.spectra.getStorage(delSpectra);
255  mbrec.spectra[0] = (float *)specstor;
256
257  Bool delFlagged = False;
258  const uChar *flagstor = pksrec.flagged.getStorage(delFlagged);
259  mbrec.flagged[0] = (unsigned char *)flagstor;
260
261  Bool delXPol = False;
262  const Complex *xpolstor;
263  if (cHaveXPol(iIF)) {
264    xpolstor = pksrec.xPol.getStorage(delXPol);
265  } else {
266    xpolstor = 0;
267  }
268  mbrec.xpol[0] = (float *)xpolstor;
269
270  // Finish off with system calibration parameters.
271  mbrec.extraSysCal = 1;
272  mbrec.refBeam     = pksrec.refBeam;
273  for (uInt i = 0; i < pksrec.tcal.nelements(); i++) {
274    mbrec.tcal[0][i] = pksrec.tcal(i);
275  }
276  strncpy(mbrec.tcalTime, (char *)pksrec.tcalTime.chars(), 16);
277  mbrec.azimuth   = pksrec.azimuth;
278  mbrec.elevation = pksrec.elevation;
279  mbrec.parAngle  = pksrec.parAngle;
280  mbrec.focusAxi  = pksrec.focusAxi;
281  mbrec.focusTan  = pksrec.focusTan;
282  mbrec.focusRot  = pksrec.focusRot;
283  mbrec.temp      = pksrec.temperature;
284  mbrec.pressure  = pksrec.pressure;
285  mbrec.humidity  = pksrec.humidity;
286  mbrec.windSpeed = pksrec.windSpeed;
287  mbrec.windAz    = pksrec.windAz;
288
289  Int status = cSDwriter.write(mbrec);
290  logMsg(cSDwriter.getMsg());
291  cSDwriter.clearMsg();
292  if (status) {
293    status = 1;
294  }
295
296  pksrec.spectra.freeStorage(specstor, delSpectra);
297  pksrec.flagged.freeStorage(flagstor, delFlagged);
298  pksrec.xPol.freeStorage(xpolstor, delXPol);
299
300  return status;
301}
302
303//------------------------------------------------------- PKSSDwriter::history
304
305// Write a history record.
306
307Int PKSSDwriter::history(const String text)
308{
309  return cSDwriter.history((char *)text.chars());
310}
311
312Int PKSSDwriter::history(const char *text)
313{
314  return cSDwriter.history((char *)text);
315}
316
317//--------------------------------------------------------- PKSSDwriter::close
318
319// Close the SDFITS file.
320
321void PKSSDwriter::close()
322{
323  cSDwriter.close();
324  logMsg(cSDwriter.getMsg());
325  cSDwriter.clearMsg();
326}
Note: See TracBrowser for help on using the repository browser.