[1654] | 1 | /* Writer de table Fits (binaire ou ASCII) */
|
---|
| 2 | #include "machdefs.h"
|
---|
| 3 | #include <stdlib.h>
|
---|
| 4 | #include <stdio.h>
|
---|
| 5 | #include "pexceptions.h"
|
---|
| 6 | #include "fabtwriter.h"
|
---|
| 7 |
|
---|
| 8 | /*!
|
---|
| 9 | \class SOPHYA::FitsABTWriter
|
---|
| 10 | \ingroup FitsIOServer
|
---|
| 11 | Class for writing a FITS ASCII or BINARY table
|
---|
[1659] | 12 | \verbatim
|
---|
| 13 | //-----------------------------------------------------------
|
---|
| 14 | -- Exemple 1: Writing element by element
|
---|
| 15 | FitsABTWriter fbtw(fitswrit,BINARY_TBL);
|
---|
[1654] | 16 | fbtw.SetExtName("MY_OWN_EXTENSION");
|
---|
[1660] | 17 | fbtw.AddCol("vars",NULL,"km",TSHORT); // col=0
|
---|
| 18 | fbtw.AddCol("vars2",NULL,"km",TSHORT); // col=1
|
---|
| 19 | fbtw.AddCol("varl",NULL,"Degre",TINT32BIT); // col=2
|
---|
| 20 | fbtw.AddCol("varf",NULL,"",TFLOAT); // col=3
|
---|
| 21 | fbtw.AddCol("vard","","arcmin",TDOUBLE); // col=4
|
---|
[1654] | 22 | fbtw.SetDebug(3);
|
---|
| 23 | for(long i=0;i<1000;i++) {
|
---|
| 24 | double x = i;
|
---|
| 25 | fbtw.Write(0,i,1000.*x); // if overflow, managed by cfitsio
|
---|
| 26 | // Write(int,long,double) is called
|
---|
| 27 | fbtw.Write(1,i,(short)(1000.*x));
|
---|
| 28 | // if overflow, managed by language
|
---|
| 29 | // Write(int,long,short) is called
|
---|
| 30 | fbtw.Write(2,i,x);
|
---|
| 31 | fbtw.Write(3,i,x);
|
---|
| 32 | fbtw.Write(4,i,x);
|
---|
| 33 | }
|
---|
| 34 | cout<<"Number of Overflows when writing: "
|
---|
| 35 | <<fbtw.GetNOverFlow()<<endl;
|
---|
[1659] | 36 |
|
---|
| 37 | //-----------------------------------------------------------
|
---|
| 38 | -- Exemple 2: Writing into TVector
|
---|
| 39 | ...
|
---|
| 40 | TVector<double> datad(100);
|
---|
| 41 | TVector<float> dataf(100);
|
---|
| 42 | TVector<int_4> datal(100);
|
---|
| 43 | for(long i=0;i<9990;i+=100) {
|
---|
| 44 | long i2=i+100-1; if(i2>=9990) i2=9990-1;
|
---|
| 45 | for(long j=0;j<100;j++) datad(i) = ...;
|
---|
| 46 | for(long j=0;j<100;j++) dataf(i) = ...;
|
---|
| 47 | for(long j=0;j<100;j++) datal(i) = ...;
|
---|
| 48 | fbtw.Write(1,i,datal);
|
---|
| 49 | fbtw.Write(2,i,dataf);
|
---|
| 50 | fbtw.Write(3,i,datad);
|
---|
| 51 | }
|
---|
[1654] | 52 | \endverbatim
|
---|
| 53 | */
|
---|
| 54 |
|
---|
| 55 | //////////////////////////////////////////////////////////////
|
---|
| 56 | /*!
|
---|
| 57 | Constructor.
|
---|
[1659] | 58 | \param fname : FITS file name to be written
|
---|
| 59 | \param hdutype : type of extension to be created (BINARY_TBL or ASCII_TBL)
|
---|
| 60 | \param lp : debug level
|
---|
[1654] | 61 | */
|
---|
| 62 | FitsABTWriter::FitsABTWriter(string fname,int hdutype,int lp)
|
---|
| 63 | {
|
---|
| 64 | createfits(fname.c_str(),hdutype,lp);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /*! See FitsABTWriter() */
|
---|
| 68 | FitsABTWriter::FitsABTWriter(const char* cfname,int hdutype,int lp)
|
---|
| 69 | {
|
---|
| 70 | createfits(cfname,hdutype,lp);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /*! See FitsABTWriter() */
|
---|
| 74 | void FitsABTWriter::createfits(const char *cfname,int hdutype,int lp)
|
---|
| 75 | {
|
---|
| 76 | FirstTime = true;
|
---|
| 77 | FitsPtr = NULL;
|
---|
| 78 | HduType = hdutype;
|
---|
| 79 | SetDebug(lp);
|
---|
| 80 | FitsFN = cfname;
|
---|
| 81 | NOverFlow = 0;
|
---|
| 82 |
|
---|
| 83 | if(DbgLevel)
|
---|
| 84 | cout<<"FitsABTWriter::createfits FitsFN="<<FitsFN
|
---|
| 85 | <<" HduType="<<HduType<<endl;
|
---|
| 86 |
|
---|
| 87 | if(FitsFN.size() <= 0 )
|
---|
| 88 | throw ParmError("FitsABTWriter::createfits: Fits file name error\n");
|
---|
| 89 |
|
---|
| 90 | if(HduType!=BINARY_TBL && HduType!=ASCII_TBL)
|
---|
| 91 | throw TypeMismatchExc("FitsABTWriter::createfits: Only BINARY or ASCII table permitted\n");
|
---|
| 92 |
|
---|
| 93 | // create new FITS file
|
---|
| 94 | int sta=0;
|
---|
| 95 | if(fits_create_file(&FitsPtr,FitsFN.c_str(),&sta)) {
|
---|
| 96 | printerror(sta);
|
---|
| 97 | throw NullPtrError("FitsABTWriter::createfits: Error creating Fits file\n");
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[1657] | 100 | // create d'un Primary HDU
|
---|
[1654] | 101 | //long naxes[1] = {0};
|
---|
| 102 | //if(fits_create_img(FitsPtr,BYTE_IMG,0,naxes,&sta)) {
|
---|
| 103 | // printerror(sta);
|
---|
| 104 | // throw NullPtrError("FitsABTWriter::createfits: Error creating Primary extension\n");
|
---|
| 105 | //}
|
---|
| 106 |
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /*! Destructor */
|
---|
| 110 | FitsABTWriter::~FitsABTWriter()
|
---|
| 111 | {
|
---|
| 112 | int sta = 0;
|
---|
| 113 | if(fits_close_file(FitsPtr,&sta)) printerror(sta);
|
---|
| 114 | FitsPtr = NULL;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | //////////////////////////////////////////////////////////////
|
---|
| 118 | /*!
|
---|
| 119 | Add a new column to the FITS table
|
---|
[1659] | 120 | \param label : column label
|
---|
[1660] | 121 | \param tform : fits tform definition ("1I","1J","1E","1J",...)
|
---|
| 122 | (can be automatically set as "datatype"
|
---|
| 123 | if BINARY_TBL and tform="" or tform=NULL)
|
---|
[1659] | 124 | \param tunit : fits tunit definition (optional)
|
---|
[1660] | 125 | \param datatype : TBYTE TSHORT TINT32BIT TLONG TFLOAT TDOUBLE
|
---|
| 126 | TUSHORT TULONG. That parameter is only use in case
|
---|
| 127 | of a BINARY_TBL table when tform is not defined).
|
---|
[1659] | 128 | \return The number of the new added column in the table.
|
---|
| 129 | \warning col = [0,ncol-1]
|
---|
[1654] | 130 | */
|
---|
[1660] | 131 | int FitsABTWriter::addcol(const char* label,const char* tform
|
---|
| 132 | ,const char* tunit,int datatype)
|
---|
[1654] | 133 | {
|
---|
| 134 | if(!FirstTime)
|
---|
| 135 | throw AllocationError("FitsABTWriter::addcol: table already created\n");
|
---|
| 136 |
|
---|
[1660] | 137 | // Gestion auto du tform pour les tables binaires avec le datatype (si non-definie)
|
---|
| 138 | bool tformauto = false;
|
---|
| 139 | if(HduType==BINARY_TBL) {
|
---|
| 140 | if(tform==NULL) tformauto = true;
|
---|
| 141 | if(strlen(tform)<=0) tformauto = true;
|
---|
| 142 | }
|
---|
| 143 | if(tformauto) {
|
---|
| 144 | char str[8];
|
---|
| 145 | if(datatype==TBYTE) strcpy(str,"1B");
|
---|
| 146 | else if(datatype==TSHORT) strcpy(str,"1I");
|
---|
[1657] | 147 | else if(datatype==TINT32BIT) strcpy(str,"1J");
|
---|
[1660] | 148 | else if(datatype==TLONG) strcpy(str,"1J");
|
---|
[1657] | 149 | else if(datatype==TFLOAT) strcpy(str,"1E");
|
---|
| 150 | else if(datatype==TDOUBLE) strcpy(str,"1D");
|
---|
[1660] | 151 | else if(datatype==TUSHORT) strcpy(str,"1U");
|
---|
| 152 | else if(datatype==TULONG) strcpy(str,"1V");
|
---|
| 153 | else
|
---|
| 154 | throw ParmError("FitsABTWriter::addcol: datatype not allowed\n");
|
---|
[1654] | 155 | TForm.push_back(str);
|
---|
[1660] | 156 | } else {
|
---|
| 157 | if(tform) TForm.push_back(tform); else TForm.push_back("");
|
---|
| 158 | datatype = 0;
|
---|
| 159 | }
|
---|
| 160 | Label.push_back(label);
|
---|
| 161 | if(tunit) TUnit.push_back(tunit); else TUnit.push_back("");
|
---|
[1654] | 162 |
|
---|
| 163 | int n = (int) Label.size() -1;
|
---|
| 164 |
|
---|
| 165 | if(DbgLevel)
|
---|
| 166 | cout<<"FitsABTWriter::addcol["<<n<<"] Label="<<Label[n]
|
---|
| 167 | <<" TForm="<<TForm[n]
|
---|
[1660] | 168 | <<" TUnit="<<TUnit[n]
|
---|
| 169 | <<" datatype="<<datatype<<endl;
|
---|
[1654] | 170 |
|
---|
| 171 | return n;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | /*! Create the table. Done at the first write request. */
|
---|
| 175 | void FitsABTWriter::createtbl(void)
|
---|
| 176 | {
|
---|
| 177 | if(!FirstTime) return;
|
---|
| 178 |
|
---|
| 179 | int tfields = Label.size();
|
---|
| 180 | if(tfields<=0)
|
---|
| 181 | throw ParmError("FitsABTWriter::createtbl: Zero column asked !\n");
|
---|
| 182 |
|
---|
| 183 | long nrows = 0;
|
---|
| 184 | char *extname = NULL;
|
---|
| 185 | char **ttype = (char **) malloc(tfields*sizeof(char *));
|
---|
| 186 | char **tform = (char **) malloc(tfields*sizeof(char *));
|
---|
| 187 | char **tunit = (char **) malloc(tfields*sizeof(char *));
|
---|
| 188 |
|
---|
| 189 | if(ExtName.size()>0) {
|
---|
| 190 | extname = (char *) malloc((strlen(ExtName.c_str())+1)*sizeof(char));
|
---|
| 191 | strcpy(extname,ExtName.c_str());
|
---|
| 192 | }
|
---|
| 193 | for(int i=0;i<tfields;i++) {
|
---|
| 194 | ttype[i] = (char *) malloc((strlen(Label[i].c_str())+1)*sizeof(char));
|
---|
| 195 | strcpy(ttype[i],Label[i].c_str());
|
---|
| 196 | tform[i] = (char *) malloc((strlen(TForm[i].c_str())+1)*sizeof(char));
|
---|
| 197 | strcpy(tform[i],TForm[i].c_str());
|
---|
| 198 | tunit[i] = (char *) malloc((strlen(TUnit[i].c_str())+1)*sizeof(char));
|
---|
| 199 | strcpy(tunit[i],TUnit[i].c_str());
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | // append a new empty binary/ascii table onto the FITS file
|
---|
| 203 | int sta=0;
|
---|
| 204 | if(fits_create_tbl(FitsPtr,HduType,nrows,tfields,ttype,tform,tunit,extname,&sta)) {
|
---|
| 205 | printerror(sta);
|
---|
| 206 | throw NullPtrError("FitsABTWriter::createtbl: Error creating Table extension\n");
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | // menage
|
---|
| 210 | if(extname) delete [] extname;
|
---|
| 211 | for(int i=0;i<tfields;i++) {
|
---|
| 212 | if(ttype[i]) delete [] ttype[i];
|
---|
| 213 | if(tform[i]) delete [] tform[i];
|
---|
| 214 | if(tunit[i]) delete [] tunit[i];
|
---|
| 215 | }
|
---|
| 216 | if(ttype) delete [] ttype;
|
---|
| 217 | if(tform) delete [] tform;
|
---|
| 218 | if(tunit) delete [] tunit;
|
---|
| 219 |
|
---|
| 220 | FirstTime = false;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | //////////////////////////////////////////////////////////////
|
---|
| 224 | /*!
|
---|
| 225 | Write a short data to FITS file.
|
---|
[1659] | 226 | \param col : column number [0,ncol[
|
---|
| 227 | \param row : row number [0,nrow[
|
---|
| 228 | \param val : value to be written
|
---|
| 229 | \warning that routine write a SHORT value into column "col"
|
---|
[1654] | 230 | which could have been defined with an other type.
|
---|
| 231 | Cast is performed by the cfitsio package.
|
---|
[1659] | 232 | \verbatim
|
---|
[1654] | 233 | WARNING: suppose that the column has be defined to be TSHORT
|
---|
[1660] | 234 | and suppose that you wanted to write a double value "val"
|
---|
| 235 | - If you write dummy.Write(col,row,(short)(val))
|
---|
| 236 | the Write(int,long,short) routine is called and
|
---|
[1654] | 237 | the cast is performed by the C++ language.
|
---|
| 238 | - If you write dummy.Write(col,row,val) where val is double
|
---|
[1660] | 239 | the Write(int,long,double) routine is called and
|
---|
[1654] | 240 | the cast is performed by the cfistio package.
|
---|
| 241 | \endverbatim
|
---|
| 242 | */
|
---|
| 243 | void FitsABTWriter::Write(int col,long row,short val)
|
---|
| 244 | {
|
---|
| 245 | if(FirstTime) createtbl();
|
---|
| 246 | int sta=0;
|
---|
[1657] | 247 | if(fits_write_col(FitsPtr,TSHORT,col+1,row+1,1,1,&val,&sta))
|
---|
[1654] | 248 | printerrorwrite("short",col,row,sta);
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | /*! Write long data to FITS file (see below) */
|
---|
[1657] | 252 | void FitsABTWriter::Write(int col,long row,int_4 val)
|
---|
[1654] | 253 | {
|
---|
| 254 | if(FirstTime) createtbl();
|
---|
| 255 | int sta=0;
|
---|
[1657] | 256 | // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
|
---|
| 257 | int T = (sizeof(long)==4) ? TLONG: TINT;
|
---|
| 258 | if(fits_write_col(FitsPtr,T,col+1,row+1,1,1,&val,&sta))
|
---|
[1654] | 259 | printerrorwrite("long",col,row,sta);
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | /*! Write float data to FITS file (see below) */
|
---|
| 263 | void FitsABTWriter::Write(int col,long row,float val)
|
---|
| 264 | {
|
---|
| 265 | if(FirstTime) createtbl();
|
---|
| 266 | int sta=0;
|
---|
[1657] | 267 | if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,1,&val,&sta))
|
---|
[1654] | 268 | printerrorwrite("float",col,row,sta);
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | /*! Write double data to FITS file (see below) */
|
---|
| 272 | void FitsABTWriter::Write(int col,long row,double val)
|
---|
| 273 | {
|
---|
| 274 | if(FirstTime) createtbl();
|
---|
| 275 | int sta=0;
|
---|
[1657] | 276 | if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,1,&val,&sta))
|
---|
[1654] | 277 | printerrorwrite("double",col,row,sta);
|
---|
| 278 | }
|
---|
| 279 |
|
---|
[1657] | 280 | //////////////////////////////////////////////////////////////
|
---|
| 281 | /*!
|
---|
| 282 | Write a vector of long data to FITS file.
|
---|
[1659] | 283 | \param col : column number [0,ncol[
|
---|
| 284 | \param row : starting row number [0,nrow[
|
---|
| 285 | \param val : vector to be written
|
---|
| 286 | \return "N" = number of the next row to be written,
|
---|
| 287 | that is "N-1" is the number of the last row written.
|
---|
[1657] | 288 | */
|
---|
| 289 | /*! Write a vector of long data to FITS file (see below) */
|
---|
| 290 | long FitsABTWriter::Write(int col,long row,TVector<int_4>& val)
|
---|
| 291 | {
|
---|
| 292 | if(FirstTime) createtbl();
|
---|
| 293 | long nel = val.Size();
|
---|
| 294 | int sta=0;
|
---|
| 295 | // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
|
---|
| 296 | int T = (sizeof(long)==4) ? TLONG: TINT;
|
---|
| 297 | if(fits_write_col(FitsPtr,T,col+1,row+1,1,nel,val.Data(),&sta))
|
---|
| 298 | printerrorwrite("long",col,row,sta);
|
---|
| 299 | return row+nel;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | /*! Write a vector of float data to FITS file (see below) */
|
---|
| 303 | long FitsABTWriter::Write(int col,long row,TVector<float>& val)
|
---|
| 304 | {
|
---|
| 305 | if(FirstTime) createtbl();
|
---|
| 306 | long nel = val.Size();
|
---|
| 307 | int sta=0;
|
---|
| 308 | if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,nel,val.Data(),&sta))
|
---|
| 309 | printerrorwrite("float",col,row,sta);
|
---|
| 310 | return row+nel;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | /*! Write a vector of double data to FITS file (see below) */
|
---|
| 314 | long FitsABTWriter::Write(int col,long row,TVector<double>& val)
|
---|
| 315 | {
|
---|
| 316 | if(FirstTime) createtbl();
|
---|
| 317 | long nel = val.Size();
|
---|
| 318 | int sta=0;
|
---|
| 319 | if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,nel,val.Data(),&sta))
|
---|
| 320 | printerrorwrite("double",col,row,sta);
|
---|
| 321 | return row+nel;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | //////////////////////////////////////////////////////////////
|
---|
[1660] | 325 | void FitsABTWriter::printerrorwrite(const char* type,int col,long row,int sta)
|
---|
[1654] | 326 | {
|
---|
| 327 | if(sta==NUM_OVERFLOW) {NOverFlow++; return;}
|
---|
| 328 | printerror(sta);
|
---|
| 329 | char str[256];
|
---|
| 330 | sprintf(str,"FitsABTWriter::Write_%s: Error Writing Fits c=%d r=%ld sta=%d"
|
---|
| 331 | ,type,col,row,sta);
|
---|
| 332 | throw NotAvailableOperation(str);
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | /////////////////////////////////////////////////
|
---|
| 336 | void FitsABTWriter::printerror(int sta) const
|
---|
| 337 | {
|
---|
| 338 | int stat = sta;
|
---|
| 339 | fits_report_error(stdout,stat);
|
---|
| 340 | fflush(stdout);
|
---|
| 341 | return;
|
---|
| 342 | }
|
---|