| [3063] | 1 | #include "machdefs.h"
 | 
|---|
 | 2 | 
 | 
|---|
 | 3 | #include <stdio.h>
 | 
|---|
 | 4 | #include <string.h>
 | 
|---|
 | 5 | #include <iostream>
 | 
|---|
 | 6 | #include <typeinfo>
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | #include "generaldata.h"
 | 
|---|
 | 9 | 
 | 
|---|
 | 10 | #include "fitsblkrw.h"
 | 
|---|
 | 11 | #include "fitshandler.h"
 | 
|---|
 | 12 | 
 | 
|---|
| [3237] | 13 | namespace SOPHYA {
 | 
|---|
 | 14 | 
 | 
|---|
| [3063] | 15 | DECL_TEMP_SPEC  /* equivalent a template <> , pour SGI-CC en particulier */
 | 
|---|
 | 16 | int FitsHandler<GeneralFitData>::CheckReadability(FitsInOutFile& is)
 | 
|---|
 | 17 | {
 | 
|---|
 | 18 |   if (is.CurrentHDUType() == IMAGE_HDU ) return 0;
 | 
|---|
 | 19 |   string key = "SOPCLSNM"; 
 | 
|---|
 | 20 |   string clsnm = is.KeyValue(key);
 | 
|---|
 | 21 |   if( clsnm == "SOPHYA::GeneralFitData") return 2;
 | 
|---|
 | 22 |   return 0;
 | 
|---|
 | 23 | }
 | 
|---|
 | 24 | 
 | 
|---|
 | 25 | DECL_TEMP_SPEC  /* equivalent a template <> , pour SGI-CC en particulier */
 | 
|---|
 | 26 | int FitsHandler<GeneralFitData>::CheckHandling(AnyDataObj & o) 
 | 
|---|
 | 27 | {  
 | 
|---|
 | 28 |  if (typeid(o) == typeid(GeneralFitData)) return 2;
 | 
|---|
 | 29 |  GeneralFitData * po = dynamic_cast< GeneralFitData * >(& o); 
 | 
|---|
 | 30 |  if (po != NULL) return 2;
 | 
|---|
 | 31 |  return 0;
 | 
|---|
 | 32 | }
 | 
|---|
 | 33 | 
 | 
|---|
 | 34 | DECL_TEMP_SPEC  /* equivalent a template <> , pour SGI-CC en particulier */
 | 
|---|
 | 35 | void FitsHandler<GeneralFitData>::Write(FitsInOutFile& os)
 | 
|---|
 | 36 | {
 | 
|---|
 | 37 |   if(dobj==NULL)   
 | 
|---|
 | 38 |     throw NullPtrError("FitsHandler<GeneralFitData>::Write() NULL dobj pointer ");
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 |   //--- Le type d'objet et son pointeur
 | 
|---|
 | 41 |   GeneralFitData* gd = dynamic_cast< GeneralFitData *> (dobj);
 | 
|---|
 | 42 |   long nvar = gd->mNVar;
 | 
|---|
 | 43 |   if(nvar<=0)
 | 
|---|
 | 44 |     throw NullPtrError("FitsHandler<GeneralFitData>::Write() no variables ");
 | 
|---|
 | 45 | 
 | 
|---|
 | 46 |   //--- Les noms de colonnes
 | 
|---|
 | 47 |   int tbltyp = os.GetDef_TableType();
 | 
|---|
 | 48 |   vector<string> colnames, tform, tunit;
 | 
|---|
 | 49 |   // La variable Y et son Erreur
 | 
|---|
 | 50 |   if(tbltyp==ASCII_TBL) tform.push_back("D15.8"); else tform.push_back("D");
 | 
|---|
 | 51 |   colnames.push_back("y");
 | 
|---|
 | 52 |   tunit.push_back("");
 | 
|---|
 | 53 |   if(tbltyp==ASCII_TBL) tform.push_back("D15.8"); else tform.push_back("D");
 | 
|---|
 | 54 |   colnames.push_back("ey");
 | 
|---|
 | 55 |   tunit.push_back("");
 | 
|---|
 | 56 |   // Le flag de validite
 | 
|---|
 | 57 |   if(tbltyp==ASCII_TBL) tform.push_back("I9"); else tform.push_back("J");
 | 
|---|
 | 58 |   colnames.push_back("ok");
 | 
|---|
 | 59 |   tunit.push_back("");
 | 
|---|
 | 60 |   // Les variables X
 | 
|---|
 | 61 |   for(int i=0;i<nvar;i++) {
 | 
|---|
 | 62 |     char str[16]; sprintf(str,"x%d",i);
 | 
|---|
 | 63 |     if(tbltyp==ASCII_TBL) tform.push_back("D15.8"); else tform.push_back("D");
 | 
|---|
 | 64 |     colnames.push_back(str);
 | 
|---|
 | 65 |     tunit.push_back("");
 | 
|---|
 | 66 |   }
 | 
|---|
 | 67 |   // Les erreurs sur les variables X
 | 
|---|
 | 68 |   if(gd->HasXErrors()) {
 | 
|---|
 | 69 |     for(int i=0;i<nvar;i++) {
 | 
|---|
 | 70 |       char str[16]; sprintf(str,"ex%d",i);
 | 
|---|
 | 71 |       if(tbltyp==ASCII_TBL) tform.push_back("D15.8"); else tform.push_back("D");
 | 
|---|
 | 72 |       colnames.push_back(str);
 | 
|---|
 | 73 |       tunit.push_back("");
 | 
|---|
 | 74 |     }
 | 
|---|
 | 75 |   }
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 |   //--- On cree la table  
 | 
|---|
 | 78 |   string extname = os.NextExtensionName();
 | 
|---|
 | 79 |   os.CreateTable(os.GetDef_TableType(),extname,colnames,tform,tunit);
 | 
|---|
 | 80 | 
 | 
|---|
 | 81 |   //--- Ecriture des donnees des colonnes
 | 
|---|
 | 82 |   long n = gd->mNDataAlloc;
 | 
|---|
 | 83 |   if(n>0) {
 | 
|---|
 | 84 |     // Y
 | 
|---|
 | 85 |     FitsBlockRW<r_8>::WriteColumnData(os,1,1,1,gd->mF,n);
 | 
|---|
 | 86 |     // EY
 | 
|---|
 | 87 |     FitsBlockRW<r_8>::WriteColumnData(os,2,1,1,gd->mErr,n);
 | 
|---|
 | 88 |     // Le flag de validite
 | 
|---|
 | 89 |     int_4 *ival = new int_4[n];
 | 
|---|
 | 90 |     for(int_4 i=0;i<n;i++) ival[i] =  gd->mOK[i];
 | 
|---|
 | 91 |     FitsBlockRW<int_4>::WriteColumnData(os,3,1,1,ival,n);
 | 
|---|
 | 92 |     delete [] ival;
 | 
|---|
 | 93 |     // les variables X
 | 
|---|
 | 94 |     r_8 *val = new r_8[n];
 | 
|---|
 | 95 |     for(int k=0;k<nvar;k++) {
 | 
|---|
 | 96 |       for(int_4 i=0;i<n;i++) val[i] = gd->mXP[nvar*i+k];
 | 
|---|
 | 97 |       FitsBlockRW<r_8>::WriteColumnData(os,4+k,1,1,val,n);
 | 
|---|
 | 98 |     }
 | 
|---|
 | 99 |     // les erreurs sur les variables X
 | 
|---|
 | 100 |     if(gd->HasXErrors()) {
 | 
|---|
 | 101 |       for(int k=0;k<nvar;k++) {
 | 
|---|
 | 102 |         for(int_4 i=0;i<n;i++) val[i] = gd->mErrXP[nvar*i+k];
 | 
|---|
 | 103 |         FitsBlockRW<r_8>::WriteColumnData(os,4+nvar+k,1,1,val,n);
 | 
|---|
 | 104 |       }
 | 
|---|
 | 105 |     }
 | 
|---|
 | 106 |     delete [] val;
 | 
|---|
 | 107 |   }
 | 
|---|
 | 108 | 
 | 
|---|
 | 109 |   //--- Ecriture des clefs fits
 | 
|---|
 | 110 |   MuTyV mtv;
 | 
|---|
 | 111 | 
 | 
|---|
 | 112 |   mtv = "SOPHYA::GeneralFitData";
 | 
|---|
 | 113 |   os.WriteKey("SOPCLSNM",mtv," SOPHYA class name");
 | 
|---|
 | 114 | 
 | 
|---|
 | 115 |   mtv = "GeneralFitData";
 | 
|---|
 | 116 |   os.WriteKey("CONTENT",mtv," name of SOPHYA object");
 | 
|---|
 | 117 | 
 | 
|---|
 | 118 |   mtv = gd->mNVar;
 | 
|---|
 | 119 |   os.WriteKey("NVAR",mtv," number of variables X");
 | 
|---|
 | 120 | 
 | 
|---|
 | 121 |   mtv = gd->mNDataAlloc;
 | 
|---|
 | 122 |   os.WriteKey("NALLOC",mtv," number of allocated data");
 | 
|---|
 | 123 |   mtv = gd->mNData;
 | 
|---|
 | 124 |   os.WriteKey("NDATA",mtv," number of filled data");
 | 
|---|
 | 125 |   mtv = gd->mNDataGood;
 | 
|---|
 | 126 |   os.WriteKey("NGOOD",mtv," number of good filled data");
 | 
|---|
 | 127 | 
 | 
|---|
 | 128 |   mtv = (int_4)((gd->HasXErrors())? 1: 0);
 | 
|---|
 | 129 |   os.WriteKey("HASXERR",mtv," erreurs sur X?");
 | 
|---|
 | 130 | 
 | 
|---|
 | 131 |   return;
 | 
|---|
 | 132 | }
 | 
|---|
 | 133 | 
 | 
|---|
 | 134 | DECL_TEMP_SPEC  /* equivalent a template <> , pour SGI-CC en particulier */
 | 
|---|
 | 135 | void FitsHandler<GeneralFitData>::Read(FitsInOutFile& is)
 | 
|---|
 | 136 | {
 | 
|---|
 | 137 |  int hdutyp = is.CurrentHDUType();
 | 
|---|
 | 138 |  if( (hdutyp != BINARY_TBL ) && (hdutyp != ASCII_TBL) )
 | 
|---|
 | 139 |     throw FitsIOException("FitsHandler<GeneralFitData>::Read() Not a binary or ascii table HDU");
 | 
|---|
 | 140 | 
 | 
|---|
 | 141 |   //--- Nb de lignes et de colonnes 
 | 
|---|
| [3167] | 142 |   vector<string> colnames; vector<int> coltypes; vector<LONGLONG> repcnt, width;
 | 
|---|
| [3063] | 143 |   is.GetColInfo(colnames,coltypes,repcnt,width);
 | 
|---|
 | 144 |   long ncol = colnames.size();
 | 
|---|
 | 145 |   if(ncol<=0)
 | 
|---|
 | 146 |     throw FitsIOException("FitsHandler<GeneralFitData>::Read() bad number of table columns");
 | 
|---|
| [3167] | 147 |   int_8 nbrows = is.GetNbRows();
 | 
|---|
| [3063] | 148 |   if(nbrows<=0)
 | 
|---|
 | 149 |     throw FitsIOException("FitsHandler<GeneralFitData>::Read() number of rows is zero, no reading");
 | 
|---|
 | 150 | 
 | 
|---|
 | 151 |   //--- Lecture entete FITS
 | 
|---|
 | 152 |   DVList dvl; is.GetHeaderRecords(dvl,true,false);
 | 
|---|
 | 153 | 
 | 
|---|
 | 154 |   int_4 nvar = dvl.GetI("NVAR",-1);
 | 
|---|
 | 155 |   if(nvar<=0)
 | 
|---|
 | 156 |     throw FitsIOException("FitsHandler<GeneralFitData>::Read() number of variables is zero, no reading");
 | 
|---|
 | 157 | 
 | 
|---|
 | 158 |   int_4 ndatalloc = dvl.GetI("NALLOC",-1);
 | 
|---|
 | 159 |   if(ndatalloc<=0)
 | 
|---|
 | 160 |     throw FitsIOException("FitsHandler<GeneralFitData>::Read() number of allocated data is zero, no reading");
 | 
|---|
 | 161 |   int_4 ndata = dvl.GetI("NDATA",-1);
 | 
|---|
 | 162 |   int_8 ndatagood = dvl.GetI("NGOOD",-1);
 | 
|---|
 | 163 | 
 | 
|---|
 | 164 |   int_4 dum = dvl.GetI("HASXERR",0);
 | 
|---|
 | 165 |   uint_2 ok_ex = (dum==0) ? 0: 1;
 | 
|---|
 | 166 | 
 | 
|---|
 | 167 |   int mynvar = 3 + nvar + ((ok_ex>0)? nvar: 0);
 | 
|---|
 | 168 |   if(ncol!=mynvar)
 | 
|---|
 | 169 |     throw FitsIOException("FitsHandler<GeneralFitData>::Read() inconsistent header/mvar, no reading");
 | 
|---|
 | 170 | 
 | 
|---|
 | 171 |   //--- Creation de l'objet
 | 
|---|
 | 172 |   if(dobj == NULL) dobj = new GeneralFitData;
 | 
|---|
 | 173 |   dobj->Alloc(nvar,ndatalloc,ok_ex);
 | 
|---|
 | 174 | 
 | 
|---|
 | 175 |   //--- remplissage des variables privees restantes
 | 
|---|
 | 176 |   dobj->mNData = ndata;
 | 
|---|
 | 177 |   dobj->mNDataGood = ndatagood;
 | 
|---|
 | 178 | 
 | 
|---|
 | 179 |   //--- remplissage de la structure de donnees
 | 
|---|
 | 180 |   if(ndata>0) {
 | 
|---|
 | 181 |     // Y
 | 
|---|
 | 182 |     FitsBlockRW<r_8>::ReadColumnData(is,1,1,1,dobj->mF,ndata);
 | 
|---|
 | 183 |     // EY
 | 
|---|
 | 184 |     FitsBlockRW<r_8>::ReadColumnData(is,2,1,1,dobj->mErr,ndata);
 | 
|---|
 | 185 |     // Le flag de validite
 | 
|---|
 | 186 |     int_4 *ival = new int_4[ndata];
 | 
|---|
 | 187 |     FitsBlockRW<int_4>::ReadColumnData(is,3,1,1,ival,ndata);
 | 
|---|
 | 188 |     for(int_4 i=0;i<ndata;i++) dobj->mOK[i] = (uint_2)ival[i];
 | 
|---|
 | 189 |     delete [] ival;
 | 
|---|
 | 190 |     // les variables X
 | 
|---|
 | 191 |     r_8 *val = new r_8[ndata];
 | 
|---|
 | 192 |     for(int k=0;k<nvar;k++) {
 | 
|---|
 | 193 |       FitsBlockRW<r_8>::ReadColumnData(is,4+k,1,1,val,ndata);
 | 
|---|
 | 194 |       for(int_4 i=0;i<ndata;i++) dobj->mXP[nvar*i+k] = val[i];
 | 
|---|
 | 195 |     }
 | 
|---|
 | 196 |     // les erreurs sur les variables X
 | 
|---|
 | 197 |     if(dobj->HasXErrors()) {
 | 
|---|
 | 198 |       for(int k=0;k<nvar;k++) {
 | 
|---|
 | 199 |         FitsBlockRW<r_8>::ReadColumnData(is,4+nvar+k,1,1,val,ndata);
 | 
|---|
 | 200 |         for(int_4 i=0;i<ndata;i++) dobj->mErrXP[nvar*i+k] = val[i];
 | 
|---|
 | 201 |       }
 | 
|---|
 | 202 |     }
 | 
|---|
 | 203 |     delete [] val;
 | 
|---|
 | 204 |   }
 | 
|---|
 | 205 | 
 | 
|---|
 | 206 |   return;
 | 
|---|
 | 207 | }
 | 
|---|
| [3237] | 208 | 
 | 
|---|
 | 209 | } // FIN namespace SOPHYA 
 | 
|---|