source: Sophya/trunk/SophyaExt/FitsIOServer/fitsgenedata.cc@ 3167

Last change on this file since 3167 was 3167, checked in by ansari, 19 years ago

Passage a la version ll (LONGLONG) des routines fits , cmv+Reza 02/02/2007

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