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
RevLine 
[1325]1//#---------------------------------------------------------------------------
2//# PKSSDwriter.cc: Class to write Parkes multibeam data to an SDFITS file.
3//#---------------------------------------------------------------------------
[1720]4//# livedata - processing pipeline for single-dish, multibeam spectral data.
5//# Copyright (C) 2000-2009, Australia Telescope National Facility, CSIRO
[1325]6//#
[1720]7//# This file is part of livedata.
[1325]8//#
[1720]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.
[1325]13//#
[1720]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.
[1325]18//#
[1720]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/>.
[1325]21//#
[1720]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 $
[1325]32//#---------------------------------------------------------------------------
33
[1452]34#include <atnf/PKSIO/MBrecord.h>
[1325]35#include <atnf/PKSIO/PKSSDwriter.h>
36
[1452]37#include <casa/stdio.h>
[1325]38#include <casa/Quanta/MVTime.h>
39
[1734]40#include <cstring>
41
[1325]42//--------------------------------------------------- PKSSDwriter::PKSSDwriter
43
44// Default constructor.
45
46PKSSDwriter::PKSSDwriter()
47{
[1452]48  // By default, messages are written to stderr.
49  initMsg();
[1325]50}
51
52//-------------------------------------------------- PKSSDwriter::~PKSSDwriter
53
54// Destructor.
55
56PKSSDwriter::~PKSSDwriter()
57{
58  close();
59}
60
[1452]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
[1325]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,
[1399]85        const String bunit,
[1325]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{
[1452]93  // Clear the message stack.
94  clearMsg();
95
[1325]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(),
[1399]127        (char *)antName.chars(), antPos, (char *)obsMode.chars(),
128        (char *)bunit.chars(), equinox, (char *)dopplerFrame.chars(), cNIF,
[1325]129        (int *)cNChan.getStorage(deleteIt),
130        (int *)cNPol.getStorage(deleteIt),
131        (int *)cHaveXPol.getStorage(deleteIt), (int)cHaveBase, 1);
[1452]132  logMsg(cSDwriter.getMsg());
133  cSDwriter.clearMsg();
[1325]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(
[1452]147        const PKSrecord &pksrec)
[1325]148{
149  // Do basic checks.
[1452]150  Int IFno = pksrec.IFno;
[1325]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
[1452]159  uInt nChan = pksrec.spectra.nrow();
[1325]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
[1452]168  uInt nPol = pksrec.spectra.ncolumn();
[1325]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.
[1452]178  MVTime time(pksrec.mjd);
[1325]179  Int year  = time.year();
180  Int month = time.month();
181  Int day   = time.monthday();
182
[1452]183  // Transfer data to a single-IF MBrecord.
184  MBrecord mbrec(1);
[1325]185
186  // Start with basic beam- and IF-independent bookkeeping information.
[1452]187  mbrec.scanNo  = pksrec.scanNo;
188  mbrec.cycleNo = pksrec.cycleNo;
[1325]189
190  sprintf(mbrec.datobs, "%4.4d-%2.2d-%2.2d", year, month, day);
[1452]191  mbrec.utc      = fmod(pksrec.mjd, 1.0) * 86400.0;
192  mbrec.exposure = float(pksrec.interval);
[1325]193
[1452]194  strncpy(mbrec.srcName, (char *)pksrec.srcName.chars(), 17);
195  mbrec.srcRA    = pksrec.srcDir(0);
196  mbrec.srcDec   = pksrec.srcDir(1);
[1325]197
[1452]198  mbrec.restFreq = pksrec.restFreq;
[1325]199
[1452]200  strncpy(mbrec.obsType, (char *)pksrec.obsType.chars(), 16);
[1325]201
202  // Now beam-dependent parameters.
[1452]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);
[1325]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;
[1452]216  mbrec.fqRefVal[0] = pksrec.refFreq;
217  mbrec.fqDelt[0]   = pksrec.freqInc;
[1325]218
219  // Now the data itself.
[1452]220  for (uInt i = 0; i < pksrec.tsys.nelements(); i++) {
221    mbrec.tsys[0][i] = pksrec.tsys(i);
[1325]222  }
223
224  for (uInt ipol = 0; ipol < nPol; ipol++) {
[1452]225    mbrec.calfctr[0][ipol] = pksrec.calFctr(ipol);
[1325]226  }
227
228  if (cHaveXPol(iIF)) {
[1452]229    mbrec.xcalfctr[0][0] = pksrec.xCalFctr.real();
230    mbrec.xcalfctr[0][1] = pksrec.xCalFctr.imag();
[1325]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++) {
[1452]240      mbrec.baseLin[0][ipol][0] = pksrec.baseLin(0,ipol);
241      mbrec.baseLin[0][ipol][1] = pksrec.baseLin(1,ipol);
[1325]242
[1452]243      for (uInt j = 0; j < pksrec.baseSub.nrow(); j++) {
244        mbrec.baseSub[0][ipol][j] = pksrec.baseSub(j,ipol);
[1325]245      }
[1635]246      for (uInt j = pksrec.baseSub.nrow(); j < 24; j++) {
[1325]247        mbrec.baseSub[0][ipol][j] = 0.0f;
248      }
249    }
250
251  } else {
252    mbrec.haveBase = 0;
253  }
254
255  Bool delSpectra = False;
[1452]256  const Float *specstor = pksrec.spectra.getStorage(delSpectra);
[1325]257  mbrec.spectra[0] = (float *)specstor;
258
259  Bool delFlagged = False;
[1452]260  const uChar *flagstor = pksrec.flagged.getStorage(delFlagged);
[1325]261  mbrec.flagged[0] = (unsigned char *)flagstor;
262
263  Bool delXPol = False;
264  const Complex *xpolstor;
265  if (cHaveXPol(iIF)) {
[1452]266    xpolstor = pksrec.xPol.getStorage(delXPol);
[1325]267  } else {
268    xpolstor = 0;
269  }
270  mbrec.xpol[0] = (float *)xpolstor;
271
272  // Finish off with system calibration parameters.
273  mbrec.extraSysCal = 1;
[1452]274  mbrec.refBeam     = pksrec.refBeam;
275  for (uInt i = 0; i < pksrec.tcal.nelements(); i++) {
276    mbrec.tcal[0][i] = pksrec.tcal(i);
[1325]277  }
[1452]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;
[1325]290
291  Int status = cSDwriter.write(mbrec);
[1452]292  logMsg(cSDwriter.getMsg());
293  cSDwriter.clearMsg();
[1325]294  if (status) {
295    status = 1;
296  }
297
[1452]298  pksrec.spectra.freeStorage(specstor, delSpectra);
299  pksrec.flagged.freeStorage(flagstor, delFlagged);
300  pksrec.xPol.freeStorage(xpolstor, delXPol);
[1325]301
302  return status;
303}
304
[1399]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
[1325]319//--------------------------------------------------------- PKSSDwriter::close
320
321// Close the SDFITS file.
322
323void PKSSDwriter::close()
324{
325  cSDwriter.close();
[1452]326  logMsg(cSDwriter.getMsg());
327  cSDwriter.clearMsg();
[1325]328}
Note: See TracBrowser for help on using the repository browser.