MCS  0.3.3-alpha7
Writers.cc
1 // ----------------------------------------------------------------------^
2 // Copyright (C) 2004, 2005, 2006, 2007, 2008 Giorgio Calderone
3 // (mailto: <gcalderone@ifc.inaf.it>)
4 //
5 // This file is part of MCS.
6 //
7 // MCS is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // MCS is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with MCS; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 //
21 // ----------------------------------------------------------------------$
22 #include "mcs.hh"
23 using namespace mcs;
24 
25 //CFITSIO include
26 #if ENABLE_CFITSIO
27 #include <fitsio.h>
28 
29 #define CHECK_FITS_ERROR if (status) throw MCS_ERROR(MSG_FITS_ERROR, fitsError(status).c_str())
30 
31 mcs::FITSWriter::FITSWriter()
32 { fptr = NULL; }
33 
34 mcs::FITSWriter::~FITSWriter()
35 { close(); }
36 
37 
38 void mcs::FITSWriter::open(string fn, bool compressed)
39 {
40  int status = 0;
41  fitsfile *fptr;
42 
43  fn = "!" + fn;
44  if (compressed) fn += ".gz";
45 
46  fits_create_file(&fptr, fn.c_str(), &status);
47  CHECK_FITS_ERROR;
48  this->fptr = fptr;
49 }
50 
51 void mcs::FITSWriter::close()
52 {
53  fitsfile *fptr = (fitsfile*) this->fptr;
54  int status = 0;
55 
56  if (fptr) {
57  fits_close_file(fptr, &status);
58  CHECK_FITS_ERROR;
59  }
60 
61  fptr = NULL;
62 }
63 
64 
65 void mcs::FITSWriter::newBinTable(Record& meta, string HDUName)
66 {
67  fitsfile *fptr = (fitsfile*) this->fptr;
68  int status = 0;
69  int i;
70  string s;
71 
72  char** ttype = NULL;
73  char** tform = NULL;
74  char* buf = NULL;
75  char* p1;
76  char* p2;
77  Record& rec = *this;
78 
79  Record::operator=(meta);
80  setNull();
81 
82 
83 #define FIELD_NAME_LEN 50
84 #define FIELD_TYPE_LEN 10
85  ttype = (char**) malloc(sizeof(char*) * count());
86  tform = (char**) malloc(sizeof(char*) * count());
87  buf = (char*) malloc((FIELD_NAME_LEN + FIELD_TYPE_LEN + 2) *
88  count());
89  memset(buf, 0, (FIELD_NAME_LEN + FIELD_TYPE_LEN + 2) * count());
90 
91  p1 = buf;
92  p2 = buf + FIELD_NAME_LEN * count();
93 
94  for (i=0; i<count(); i++) {
95  ttype[i] = p1;
96  p1 = stpcpy(p1, rec[i].name().c_str());
97  p1++;
98 
99  if (! Types2S_FITS(rec[i].type(), rec[i].maxLength(),
100  rec[i].isUnsigned(), s)) {
101  free(ttype);
102  free(tform);
103  free(buf);
104  throw MCS_ERROR(MSG_TYPE_NOT_HANDLED, i, rec[i].type());
105  }
106 
107  tform[i] = p2;
108  p2 = stpcpy(p2, s.c_str());
109  p2++;
110  }
111 
112  fits_create_tbl(fptr, BINARY_TBL, 0, count(),
113  ttype, tform,
114  NULL, (char*) HDUName.c_str(), &status);
115  CHECK_FITS_ERROR;
116 
117  free(ttype);
118  free(tform);
119  free(buf);
120 }
121 
122 
123 
124 
125 
126 void mcs::FITSWriter::newImage(Types bitpix,
127  unsigned int naxis, unsigned int naxes,
128  void* buf, string name)
129 {
130  //fitsfile *fptr = (fitsfile*) this->fptr;
131  //int status = 0;
132  int bitpix_fits;
133 
134  switch(bitpix) {
135  case TINY:
136  bitpix_fits = BYTE_IMG;
137  break;
138  case SMALL:
139  bitpix_fits = SHORT_IMG;
140  break;
141  case INT:
142  bitpix_fits = LONG_IMG;
143  break;
144  case BIGINT:
145  bitpix_fits = LONGLONG_IMG;
146  break;
147  case FLOAT:
148  bitpix_fits = FLOAT_IMG;
149  break;
150  case DOUBLE:
151  bitpix_fits = DOUBLE_IMG;
152  break;
153  default:
154  throw; //ERROR
155  }
156 
157  //fits_create_img(fptr, bitpix_fits, &naxis, &naxes, &status);
158 }
159 
160 #endif //ENABLE_CFITSIO
A dynamic array of Data objects.
Definition: mcs.hh:4170
#define MCS_ERROR(A, rest...)
Facility to easily pass all necessary parameter to an Event constructor.
Definition: mcs.hh:964
bool Types2S_FITS(Types dbt, int len, bool isunsigned, string &fits)
Convert a MCS type into a FITSIO type code.
Definition: Data.cc:603
Record & operator=(Record &from)
Assignment operator, will copy all data objects.
Definition: Record.cc:269
Definition: mcs.hh:55
Main include file for all MCS based applications.
Namespace for MCS library.
Types
Enumeration of base type for Data.
Definition: mcs.hh:54
Definition: mcs.hh:58

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha7
Documentation generated on Mon May 28 07:39:41 UTC 2018