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

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

gcc-4.4 fix to include cstring

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