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

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

Update from livedata CVS repository

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
40//--------------------------------------------------- PKSSDwriter::PKSSDwriter
41
42// Default constructor.
43
44PKSSDwriter::PKSSDwriter()
45{
[1452]46 // By default, messages are written to stderr.
47 initMsg();
[1325]48}
49
50//-------------------------------------------------- PKSSDwriter::~PKSSDwriter
51
52// Destructor.
53
54PKSSDwriter::~PKSSDwriter()
55{
56 close();
57}
58
[1452]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
[1325]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,
[1399]83 const String bunit,
[1325]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{
[1452]91 // Clear the message stack.
92 clearMsg();
93
[1325]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(),
[1399]125 (char *)antName.chars(), antPos, (char *)obsMode.chars(),
126 (char *)bunit.chars(), equinox, (char *)dopplerFrame.chars(), cNIF,
[1325]127 (int *)cNChan.getStorage(deleteIt),
128 (int *)cNPol.getStorage(deleteIt),
129 (int *)cHaveXPol.getStorage(deleteIt), (int)cHaveBase, 1);
[1452]130 logMsg(cSDwriter.getMsg());
131 cSDwriter.clearMsg();
[1325]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(
[1452]145 const PKSrecord &pksrec)
[1325]146{
147 // Do basic checks.
[1452]148 Int IFno = pksrec.IFno;
[1325]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
[1452]157 uInt nChan = pksrec.spectra.nrow();
[1325]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
[1452]166 uInt nPol = pksrec.spectra.ncolumn();
[1325]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.
[1452]176 MVTime time(pksrec.mjd);
[1325]177 Int year = time.year();
178 Int month = time.month();
179 Int day = time.monthday();
180
[1452]181 // Transfer data to a single-IF MBrecord.
182 MBrecord mbrec(1);
[1325]183
184 // Start with basic beam- and IF-independent bookkeeping information.
[1452]185 mbrec.scanNo = pksrec.scanNo;
186 mbrec.cycleNo = pksrec.cycleNo;
[1325]187
188 sprintf(mbrec.datobs, "%4.4d-%2.2d-%2.2d", year, month, day);
[1452]189 mbrec.utc = fmod(pksrec.mjd, 1.0) * 86400.0;
190 mbrec.exposure = float(pksrec.interval);
[1325]191
[1452]192 strncpy(mbrec.srcName, (char *)pksrec.srcName.chars(), 17);
193 mbrec.srcRA = pksrec.srcDir(0);
194 mbrec.srcDec = pksrec.srcDir(1);
[1325]195
[1452]196 mbrec.restFreq = pksrec.restFreq;
[1325]197
[1452]198 strncpy(mbrec.obsType, (char *)pksrec.obsType.chars(), 16);
[1325]199
200 // Now beam-dependent parameters.
[1452]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);
[1325]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;
[1452]214 mbrec.fqRefVal[0] = pksrec.refFreq;
215 mbrec.fqDelt[0] = pksrec.freqInc;
[1325]216
217 // Now the data itself.
[1452]218 for (uInt i = 0; i < pksrec.tsys.nelements(); i++) {
219 mbrec.tsys[0][i] = pksrec.tsys(i);
[1325]220 }
221
222 for (uInt ipol = 0; ipol < nPol; ipol++) {
[1452]223 mbrec.calfctr[0][ipol] = pksrec.calFctr(ipol);
[1325]224 }
225
226 if (cHaveXPol(iIF)) {
[1452]227 mbrec.xcalfctr[0][0] = pksrec.xCalFctr.real();
228 mbrec.xcalfctr[0][1] = pksrec.xCalFctr.imag();
[1325]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++) {
[1452]238 mbrec.baseLin[0][ipol][0] = pksrec.baseLin(0,ipol);
239 mbrec.baseLin[0][ipol][1] = pksrec.baseLin(1,ipol);
[1325]240
[1452]241 for (uInt j = 0; j < pksrec.baseSub.nrow(); j++) {
242 mbrec.baseSub[0][ipol][j] = pksrec.baseSub(j,ipol);
[1325]243 }
[1635]244 for (uInt j = pksrec.baseSub.nrow(); j < 24; j++) {
[1325]245 mbrec.baseSub[0][ipol][j] = 0.0f;
246 }
247 }
248
249 } else {
250 mbrec.haveBase = 0;
251 }
252
253 Bool delSpectra = False;
[1452]254 const Float *specstor = pksrec.spectra.getStorage(delSpectra);
[1325]255 mbrec.spectra[0] = (float *)specstor;
256
257 Bool delFlagged = False;
[1452]258 const uChar *flagstor = pksrec.flagged.getStorage(delFlagged);
[1325]259 mbrec.flagged[0] = (unsigned char *)flagstor;
260
261 Bool delXPol = False;
262 const Complex *xpolstor;
263 if (cHaveXPol(iIF)) {
[1452]264 xpolstor = pksrec.xPol.getStorage(delXPol);
[1325]265 } else {
266 xpolstor = 0;
267 }
268 mbrec.xpol[0] = (float *)xpolstor;
269
270 // Finish off with system calibration parameters.
271 mbrec.extraSysCal = 1;
[1452]272 mbrec.refBeam = pksrec.refBeam;
273 for (uInt i = 0; i < pksrec.tcal.nelements(); i++) {
274 mbrec.tcal[0][i] = pksrec.tcal(i);
[1325]275 }
[1452]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;
[1325]288
289 Int status = cSDwriter.write(mbrec);
[1452]290 logMsg(cSDwriter.getMsg());
291 cSDwriter.clearMsg();
[1325]292 if (status) {
293 status = 1;
294 }
295
[1452]296 pksrec.spectra.freeStorage(specstor, delSpectra);
297 pksrec.flagged.freeStorage(flagstor, delFlagged);
298 pksrec.xPol.freeStorage(xpolstor, delXPol);
[1325]299
300 return status;
301}
302
[1399]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
[1325]317//--------------------------------------------------------- PKSSDwriter::close
318
319// Close the SDFITS file.
320
321void PKSSDwriter::close()
322{
323 cSDwriter.close();
[1452]324 logMsg(cSDwriter.getMsg());
325 cSDwriter.clearMsg();
[1325]326}
Note: See TracBrowser for help on using the repository browser.