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

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

gcc-4.4 fix to include cstring

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