source: Sophya/trunk/SophyaExt/FitsIOServer/fitsswapper.cc@ 2846

Last change on this file since 2846 was 2782, checked in by ansari, 20 years ago

Ajout classe Swapper sur fichiers FITS - CMV+Reza 30 Mai 2005

File size: 5.2 KB
Line 
1#include "fitsswapper.h"
2#include "sopnamsp.h"
3#include <stdio.h>
4#include <iostream>
5
6
7template <class T>
8FITSDataSwapper<T>::FITSDataSwapper()
9{
10 SetInStream(NULL, 0);
11 SetOutStream(NULL, 0);
12}
13
14template <class T>
15FITSDataSwapper<T>::FITSDataSwapper(fitsfile * is, int coli, fitsfile * os, int colo)
16{
17 SetInStream(is, coli);
18 if (os == NULL) SetOutStream(is, coli);
19 else SetOutStream(os, colo);
20}
21
22template <class T>
23void FITSDataSwapper<T>::SetInStream(fitsfile * is, int col)
24{
25 fitsis = is;
26 colis = col;
27 hduis = 0;
28 int status=0, hdutype=0;
29 if (is != NULL) {
30 if ( fits_get_hdu_num(fitsis, &hduis) <= 0)
31 throw IOExc("FITSDataSwapper<T>::SetInStream() fits_get_hdu_num Error");
32 if (fits_get_hdu_type(fitsis, &hdutype, &status)) {
33 fits_report_error(stdout,status); fflush(stdout);
34 throw IOExc("FITSDataSwapper<T>::SetInStream() fits_get_hdu_type Error");
35 }
36 // cout << "DBG-SetInStream() : hduis= " << hduis
37 // << " hdutype=" << hdutype << " BINARY_TBL= " << BINARY_TBL << endl;
38 if ( (hdutype != ASCII_TBL) && (hdutype != BINARY_TBL) )
39 throw IOExc("FITSDataSwapper<T>::SetInStream() current HDU not of BINARY_TBL or ASCII_TBL type");
40 }
41}
42
43template <class T>
44void FITSDataSwapper<T>::SetOutStream(fitsfile * os, int col)
45{
46 fitsos = os;
47 colos = col;
48 hduos = 0;
49 rowos = 0;
50 int status=0, hdutype=0;
51 if (os != NULL) {
52 if ( fits_get_hdu_num(fitsos, &hduos) <= 0 )
53 throw IOExc("FITSDataSwapper<T>::SetOutStream() fits_get_hdu_num Error");
54 if (fits_get_hdu_type(fitsos, &hdutype, &status)) {
55 fits_report_error(stdout,status); fflush(stdout);
56 throw IOExc("FITSDataSwapper<T>::SetOutStream() fits_get_hdu_type Error");
57 }
58 // cout << "DBG-SetOutStream() : hduos= " << hduos
59 // << " hdutype=" << hdutype << " BINARY_TBL= " << BINARY_TBL << endl;
60 if ( (hdutype != ASCII_TBL) && (hdutype != BINARY_TBL) )
61 throw IOExc("FITSDataSwapper<T>::SetOutStream() current HDU not of BINARY_TBL or ASCII_TBL type");
62 }
63}
64
65template <class T>
66FITSDataSwapper<T>& FITSDataSwapper<T>::operator = (FITSDataSwapper<T> const & a)
67{
68 fitsos = a.fitsos;
69 colos = a.colos;
70 hduos = a.hduos;
71 rowos = a.rowos;
72 fitsis = a.fitsis;
73 colis = a.colis;
74 hduis = a.hduis;
75 return *this;
76}
77
78
79static inline int _ConvertToFitsDataType(const r_8 * d) { return TDOUBLE; }
80static inline int _ConvertToFitsDataType(const r_4 * d) { return TFLOAT; }
81static inline int _ConvertToFitsDataType(const uint_2 * d) { return TUSHORT; }
82static inline int _ConvertToFitsDataType(const int_2 * d) { return TSHORT; }
83static inline int _ConvertToFitsDataType(const int_4 * d) { return (sizeof(long)==4) ? TLONG: TINT; }
84#ifdef TLONGLONG
85static inline int _ConvertToFitsDataType(const int_8 * d) { return TLONGLONG; }
86#else
87static inline int _ConvertToFitsDataType(const int_8 * d) { throw NotAvailableOperation("FITSDataSwapper<int_8> ; Unsupported data type ")}
88#endif
89
90template <class T>
91int_8 FITSDataSwapper<T>::WriteToSwap(const T * d, size_t sz, int_8 idx, int_8 oswp, bool osw)
92{
93 if (fitsos == NULL) throw IOExc("FITSDataSwapper<T>::WriteToSwap() null output stream pointer (fitsos)" );
94 int hdutype=0, status=0;
95 if(fits_movabs_hdu(fitsos,hduos,&hdutype,&status)) {
96 fits_report_error(stdout,status); fflush(stdout);
97 throw IOExc("FITSDataSwapper<T>::WriteToSwap() fits_movabs_hdu Error");
98 }
99 int ftype = _ConvertToFitsDataType(d);
100 long row;
101 if (osw) row = oswp;
102 else row = rowos;
103 T * ncd = const_cast<T *>(d);
104 if( fits_write_col(fitsos, ftype, colos+1, row+1, 1, sz, ncd, &status) ) {
105 fits_report_error(stdout,status); fflush(stdout);
106 throw IOExc("FITSDataSwapper<T>::WriteToSwap() fits_write_col Error");
107 }
108 if (!osw) rowos += sz; // On met a jour la position d'ecriture courante si pas de reecriture
109 return row;
110}
111
112template <class T>
113void FITSDataSwapper<T>::ReadFromSwap(int_8 idx, int_8 swp, T* d, size_t sz)
114{
115 if (fitsis == NULL) throw IOExc("FITSDataSwapper<T>::ReadFromSwap() null input stream pointer (fitsis)" );
116 int hdutype=0, status=0;
117 if(fits_movabs_hdu(fitsis,hduos,&hdutype,&status)) {
118 fits_report_error(stdout,status); fflush(stdout);
119 throw IOExc("FITSDataSwapper<T>::ReadFromSwap() fits_movabs_hdu Error");
120 }
121 int ftype = _ConvertToFitsDataType(d);
122 if( fits_read_col(fitsis, ftype, colis+1, swp+1, 1, sz, NULL, d, NULL, &status) ) {
123 fits_report_error(stdout,status); fflush(stdout);
124 throw IOExc("FITSDataSwapper<T>::ReadFromSwap() fits_read_col Error");
125 }
126 return;
127}
128
129
130///////////////////////////////////////////////////////////////
131///////////////////////////////////////////////////////////////
132#ifdef __CXX_PRAGMA_TEMPLATES__
133
134#pragma define_template FITSDataSwapper<uint_2>
135#pragma define_template FITSDataSwapper<int_2>
136#pragma define_template FITSDataSwapper<int_4>
137#pragma define_template FITSDataSwapper<int_8>
138#pragma define_template FITSDataSwapper<r_4>
139#pragma define_template FITSDataSwapper<r_8>
140#endif
141
142#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
143template class FITSDataSwapper<uint_2>;
144template class FITSDataSwapper<int_2>;
145template class FITSDataSwapper<int_4>;
146template class FITSDataSwapper<int_8>;
147template class FITSDataSwapper<r_4>;
148template class FITSDataSwapper<r_8>;
149#endif
Note: See TracBrowser for help on using the repository browser.