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

Last change on this file since 1635 was 1635, checked in by Malte Marquarding, 15 years ago

Update from livedata CVS

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