[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");
|
---|
[1657] | 17 | fbtw.AddCol("vars",TSHORT,"","km"); // col=0
|
---|
| 18 | fbtw.AddCol("vars2",TSHORT,"","km"); // col=1
|
---|
| 19 | fbtw.AddCol("varl",TINT32BIT,"","Degre"); // col=2
|
---|
| 20 | fbtw.AddCol("varf",TFLOAT,"",""); // col=3
|
---|
| 21 | fbtw.AddCol("vard",TDOUBLE,"","arcmin"); // 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
|
---|
| 121 | \param datatype : TSHORT TINT32BIT TFLOAT or TDOUBLE
|
---|
| 122 | \param tform : fits tform definition
|
---|
| 123 | (can be automatically set if BINARY_TBL and tform="")
|
---|
| 124 | \param tunit : fits tunit definition (optional)
|
---|
| 125 | \return The number of the new added column in the table.
|
---|
| 126 | \warning col = [0,ncol-1]
|
---|
[1654] | 127 | */
|
---|
| 128 | int FitsABTWriter::addcol(const char* label,int datatype
|
---|
| 129 | ,const char* tform,const char* tunit)
|
---|
| 130 | {
|
---|
| 131 | if(!FirstTime)
|
---|
| 132 | throw AllocationError("FitsABTWriter::addcol: table already created\n");
|
---|
| 133 |
|
---|
[1657] | 134 | if( datatype!=TSHORT && datatype!=TINT32BIT
|
---|
[1654] | 135 | && datatype!=TFLOAT && datatype!=TDOUBLE )
|
---|
| 136 | throw ParmError("FitsABTWriter::addcol: datatype not allowed\n");
|
---|
| 137 |
|
---|
| 138 | Label.push_back(label);
|
---|
| 139 | DataType.push_back(datatype);
|
---|
| 140 | // Gestion auto du tform par defaut pour les tables binaires
|
---|
| 141 | if(HduType==BINARY_TBL && strlen(tform)<=0) {
|
---|
| 142 | char str[16];
|
---|
[1657] | 143 | if(datatype==TSHORT) strcpy(str,"1I");
|
---|
| 144 | else if(datatype==TINT32BIT) strcpy(str,"1J");
|
---|
| 145 | else if(datatype==TFLOAT) strcpy(str,"1E");
|
---|
| 146 | else if(datatype==TDOUBLE) strcpy(str,"1D");
|
---|
[1654] | 147 | TForm.push_back(str);
|
---|
| 148 | } else TForm.push_back(tform);
|
---|
| 149 | TUnit.push_back(tunit);
|
---|
| 150 |
|
---|
| 151 | int n = (int) Label.size() -1;
|
---|
| 152 |
|
---|
| 153 | if(DbgLevel)
|
---|
| 154 | cout<<"FitsABTWriter::addcol["<<n<<"] Label="<<Label[n]
|
---|
| 155 | <<" datatype="<<datatype
|
---|
| 156 | <<" TForm="<<TForm[n]
|
---|
| 157 | <<" TUnit="<<TUnit[n]<<endl;
|
---|
| 158 |
|
---|
| 159 | return n;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | /*! Create the table. Done at the first write request. */
|
---|
| 163 | void FitsABTWriter::createtbl(void)
|
---|
| 164 | {
|
---|
| 165 | if(!FirstTime) return;
|
---|
| 166 |
|
---|
| 167 | int tfields = Label.size();
|
---|
| 168 | if(tfields<=0)
|
---|
| 169 | throw ParmError("FitsABTWriter::createtbl: Zero column asked !\n");
|
---|
| 170 |
|
---|
| 171 | long nrows = 0;
|
---|
| 172 | char *extname = NULL;
|
---|
| 173 | char **ttype = (char **) malloc(tfields*sizeof(char *));
|
---|
| 174 | char **tform = (char **) malloc(tfields*sizeof(char *));
|
---|
| 175 | char **tunit = (char **) malloc(tfields*sizeof(char *));
|
---|
| 176 |
|
---|
| 177 | if(ExtName.size()>0) {
|
---|
| 178 | extname = (char *) malloc((strlen(ExtName.c_str())+1)*sizeof(char));
|
---|
| 179 | strcpy(extname,ExtName.c_str());
|
---|
| 180 | }
|
---|
| 181 | for(int i=0;i<tfields;i++) {
|
---|
| 182 | ttype[i] = (char *) malloc((strlen(Label[i].c_str())+1)*sizeof(char));
|
---|
| 183 | strcpy(ttype[i],Label[i].c_str());
|
---|
| 184 | tform[i] = (char *) malloc((strlen(TForm[i].c_str())+1)*sizeof(char));
|
---|
| 185 | strcpy(tform[i],TForm[i].c_str());
|
---|
| 186 | tunit[i] = (char *) malloc((strlen(TUnit[i].c_str())+1)*sizeof(char));
|
---|
| 187 | strcpy(tunit[i],TUnit[i].c_str());
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | // append a new empty binary/ascii table onto the FITS file
|
---|
| 191 | int sta=0;
|
---|
| 192 | if(fits_create_tbl(FitsPtr,HduType,nrows,tfields,ttype,tform,tunit,extname,&sta)) {
|
---|
| 193 | printerror(sta);
|
---|
| 194 | throw NullPtrError("FitsABTWriter::createtbl: Error creating Table extension\n");
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | // menage
|
---|
| 198 | if(extname) delete [] extname;
|
---|
| 199 | for(int i=0;i<tfields;i++) {
|
---|
| 200 | if(ttype[i]) delete [] ttype[i];
|
---|
| 201 | if(tform[i]) delete [] tform[i];
|
---|
| 202 | if(tunit[i]) delete [] tunit[i];
|
---|
| 203 | }
|
---|
| 204 | if(ttype) delete [] ttype;
|
---|
| 205 | if(tform) delete [] tform;
|
---|
| 206 | if(tunit) delete [] tunit;
|
---|
| 207 |
|
---|
| 208 | FirstTime = false;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | //////////////////////////////////////////////////////////////
|
---|
| 212 | /*!
|
---|
| 213 | Write a short data to FITS file.
|
---|
[1659] | 214 | \param col : column number [0,ncol[
|
---|
| 215 | \param row : row number [0,nrow[
|
---|
| 216 | \param val : value to be written
|
---|
| 217 | \warning that routine write a SHORT value into column "col"
|
---|
[1654] | 218 | which could have been defined with an other type.
|
---|
| 219 | Cast is performed by the cfitsio package.
|
---|
[1659] | 220 | \verbatim
|
---|
[1654] | 221 | WARNING: suppose that the column has be defined to be TSHORT
|
---|
| 222 | and suppose that you wanted to write a double value
|
---|
| 223 | - If you write dummy.Write(col,row,(short(val))
|
---|
| 224 | you call the Write(int,long,short) routine and
|
---|
| 225 | the cast is performed by the C++ language.
|
---|
| 226 | - If you write dummy.Write(col,row,val) where val is double
|
---|
| 227 | you call theWrite(int,long,double) routine and
|
---|
| 228 | the cast is performed by the cfistio package.
|
---|
| 229 | \endverbatim
|
---|
| 230 | */
|
---|
| 231 | void FitsABTWriter::Write(int col,long row,short val)
|
---|
| 232 | {
|
---|
| 233 | if(FirstTime) createtbl();
|
---|
| 234 | int sta=0;
|
---|
[1657] | 235 | if(fits_write_col(FitsPtr,TSHORT,col+1,row+1,1,1,&val,&sta))
|
---|
[1654] | 236 | printerrorwrite("short",col,row,sta);
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | /*! Write long data to FITS file (see below) */
|
---|
[1657] | 240 | void FitsABTWriter::Write(int col,long row,int_4 val)
|
---|
[1654] | 241 | {
|
---|
| 242 | if(FirstTime) createtbl();
|
---|
| 243 | int sta=0;
|
---|
[1657] | 244 | // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
|
---|
| 245 | int T = (sizeof(long)==4) ? TLONG: TINT;
|
---|
| 246 | if(fits_write_col(FitsPtr,T,col+1,row+1,1,1,&val,&sta))
|
---|
[1654] | 247 | printerrorwrite("long",col,row,sta);
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | /*! Write float data to FITS file (see below) */
|
---|
| 251 | void FitsABTWriter::Write(int col,long row,float val)
|
---|
| 252 | {
|
---|
| 253 | if(FirstTime) createtbl();
|
---|
| 254 | int sta=0;
|
---|
[1657] | 255 | if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,1,&val,&sta))
|
---|
[1654] | 256 | printerrorwrite("float",col,row,sta);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | /*! Write double data to FITS file (see below) */
|
---|
| 260 | void FitsABTWriter::Write(int col,long row,double val)
|
---|
| 261 | {
|
---|
| 262 | if(FirstTime) createtbl();
|
---|
| 263 | int sta=0;
|
---|
[1657] | 264 | if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,1,&val,&sta))
|
---|
[1654] | 265 | printerrorwrite("double",col,row,sta);
|
---|
| 266 | }
|
---|
| 267 |
|
---|
[1657] | 268 | //////////////////////////////////////////////////////////////
|
---|
| 269 | /*!
|
---|
| 270 | Write a vector of long data to FITS file.
|
---|
[1659] | 271 | \param col : column number [0,ncol[
|
---|
| 272 | \param row : starting row number [0,nrow[
|
---|
| 273 | \param val : vector to be written
|
---|
| 274 | \return "N" = number of the next row to be written,
|
---|
| 275 | that is "N-1" is the number of the last row written.
|
---|
[1657] | 276 | */
|
---|
| 277 | /*! Write a vector of long data to FITS file (see below) */
|
---|
| 278 | long FitsABTWriter::Write(int col,long row,TVector<int_4>& val)
|
---|
| 279 | {
|
---|
| 280 | if(FirstTime) createtbl();
|
---|
| 281 | long nel = val.Size();
|
---|
| 282 | int sta=0;
|
---|
| 283 | // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
|
---|
| 284 | int T = (sizeof(long)==4) ? TLONG: TINT;
|
---|
| 285 | if(fits_write_col(FitsPtr,T,col+1,row+1,1,nel,val.Data(),&sta))
|
---|
| 286 | printerrorwrite("long",col,row,sta);
|
---|
| 287 | return row+nel;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | /*! Write a vector of float data to FITS file (see below) */
|
---|
| 291 | long FitsABTWriter::Write(int col,long row,TVector<float>& val)
|
---|
| 292 | {
|
---|
| 293 | if(FirstTime) createtbl();
|
---|
| 294 | long nel = val.Size();
|
---|
| 295 | int sta=0;
|
---|
| 296 | if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,nel,val.Data(),&sta))
|
---|
| 297 | printerrorwrite("float",col,row,sta);
|
---|
| 298 | return row+nel;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | /*! Write a vector of double data to FITS file (see below) */
|
---|
| 302 | long FitsABTWriter::Write(int col,long row,TVector<double>& val)
|
---|
| 303 | {
|
---|
| 304 | if(FirstTime) createtbl();
|
---|
| 305 | long nel = val.Size();
|
---|
| 306 | int sta=0;
|
---|
| 307 | if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,nel,val.Data(),&sta))
|
---|
| 308 | printerrorwrite("double",col,row,sta);
|
---|
| 309 | return row+nel;
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | //////////////////////////////////////////////////////////////
|
---|
[1654] | 313 | void FitsABTWriter::printerrorwrite(char* type,int col,long row,int sta)
|
---|
| 314 | {
|
---|
| 315 | if(sta==NUM_OVERFLOW) {NOverFlow++; return;}
|
---|
| 316 | printerror(sta);
|
---|
| 317 | char str[256];
|
---|
| 318 | sprintf(str,"FitsABTWriter::Write_%s: Error Writing Fits c=%d r=%ld sta=%d"
|
---|
| 319 | ,type,col,row,sta);
|
---|
| 320 | throw NotAvailableOperation(str);
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | /////////////////////////////////////////////////
|
---|
| 324 | void FitsABTWriter::printerror(int sta) const
|
---|
| 325 | {
|
---|
| 326 | int stat = sta;
|
---|
| 327 | fits_report_error(stdout,stat);
|
---|
| 328 | fflush(stdout);
|
---|
| 329 | return;
|
---|
| 330 | }
|
---|