79ae2f26f74b1ea476f82ea4bb3ab4cddfd50b1d
[ctsim.git] / include / array2dfile.h
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **      Name:         array2dfile.h
5 **      Purpose:      2-dimension array file class
6 **      Programmer:   Kevin Rosenberg
7 **      Date Started: June 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: array2dfile.h,v 1.17 2001/01/02 07:47:36 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 #ifndef ARRAY2DFILE_H
29 #define ARRAY2DFILE_H
30
31 #ifndef MSVC
32 #include <unistd.h>
33 #endif
34
35 #include <sys/types.h>
36 #include <cstring>
37 #include <string>\r
38 #include <iosfwd>
39 #include <iomanip>\r
40 #include <fstream>\r
41 #include <iostream>\r
42 #include <vector>
43 #include "ctsupport.h"
44 #include "fnetorderstream.h"
45 #include "array2d.h"
46
47 class Array2dFileLabel
48 {
49 public:
50     enum {
51       L_EMPTY = 0,
52       L_HISTORY = 1,
53       L_USER = 2,
54     };
55
56     Array2dFileLabel(); 
57
58     Array2dFileLabel(const char* const str, double ctime = 0.);
59
60     Array2dFileLabel(const int type, const char* const str, double ctime = 0.);
61
62     ~Array2dFileLabel();
63
64     const std::string& getLabelString (void) const
65         { return m_strLabel; }
66
67     kfloat64 getCalcTime (void) const
68         { return m_calcTime; }
69
70     void setCalcTime (kfloat64 calcTime)
71         { m_calcTime = calcTime; }
72
73     void setLabelType (int labelType)
74         { m_labelType = labelType; }
75
76     int getLabelType (void) const
77         { return m_labelType; }
78
79         std::string& setLabelString (const char* const str)
80         { m_strLabel = str; return (m_strLabel); }
81
82         std::string& setLabelString (const std::string& str)
83         { m_strLabel = str; return (m_strLabel); }
84
85     void setDateTime (int year, int month, int day, int hour, int minute, int second);
86
87     void getDateTime (int& year, int& month, int& day, int& hour, int& minute, int& second) const;
88
89     const std::string& getDateString () const;
90
91     void print (std::ostream& os) const;
92     void printBrief (std::ostream& os) const;\r
93     void printBrief (std::ostringstream& os) const;\r
94
95     Array2dFileLabel (const Array2dFileLabel& rhs);
96
97     Array2dFileLabel& operator= (const Array2dFileLabel& rhs);
98
99 private:
100     void init (void);
101
102     kuint16 m_labelType;
103     kuint16 m_year;
104     kuint16 m_month;
105     kuint16 m_day;
106     kuint16 m_hour;
107     kuint16 m_minute;
108     kuint16 m_second;
109         std::string m_strLabel;
110     kfloat64 m_calcTime;
111
112     mutable std::string m_strDate;
113 };
114
115
116 class Array2dFile 
117 {
118 public:
119   enum {
120     PIXEL_INVALID = 0,
121     PIXEL_INT8 = 1,
122     PIXEL_UINT8 = 2,
123     PIXEL_INT16 = 3,
124     PIXEL_UINT16 = 4,
125     PIXEL_INT32 = 5,
126     PIXEL_UINT32 = 6,
127     PIXEL_FLOAT32 = 7,
128     PIXEL_FLOAT64 = 8,
129   };
130 \r
131   enum {\r
132     DATA_TYPE_INVALID = 0,\r
133     DATA_TYPE_REAL,\r
134     DATA_TYPE_COMPLEX,\r
135   };\r
136
137   Array2dFile (int nx, int ny, int pixelSize, int pixelFormat = PIXEL_INVALID, int dataType = DATA_TYPE_REAL);
138   Array2dFile (void);
139   ~Array2dFile ();
140
141   void setArraySize (int nx, int ny, int pixelSize, int pixelFormat = PIXEL_INVALID, int dataType = DATA_TYPE_REAL);
142
143   void setArraySize (int nx, int ny);
144
145   unsigned int getNumLabels (void) const
146       { return m_labels.size(); }
147
148   const Array2dFileLabel& labelGet (int label_num) const;
149
150   void labelAdd (const Array2dFileLabel& label);
151
152   void labelAdd (const char* const m_strLabel, double calc_time=0.);
153
154   void labelAdd (int type, const char* const m_strLabel, double calc_time=0.);
155
156   void labelsCopy (const Array2dFile& file, const char* const idStr = NULL);
157
158   void setPixelFormat (int type)
159       { m_pixelFormat = type; }
160
161   void setPixelSize (int size)
162       { m_pixelSize = size; }
163
164   kuint32 nx (void) const
165       { return m_nx; }
166
167   kuint32 ny (void) const
168       { return m_ny; }
169 \r
170   bool isComplex() const\r
171   { return m_dataType == DATA_TYPE_COMPLEX; }\r
172 \r
173   bool isReal() const\r
174   { return m_dataType == DATA_TYPE_REAL; }\r
175 \r
176   int dataType () const\r
177   { return static_cast<int>(m_dataType); }\r
178 \r
179   void setDataType (int dataType)\r
180   { m_dataType = dataType; }\r
181
182   void setAxisIncrement (double axisIncX, double axisIncY);
183
184   bool reallocRealToComplex ();\r
185 \r
186   bool reallocComplexToReal ();\r
187
188   void getPixelValueRange (double& pvmin, double& pvmax) const;\r
189   void setAxisExtent (double minX, double maxX, double minY, double maxY);
190       
191   void doPixelOffsetScale (double offset, double scale);
192
193   void arrayDataClear (void);
194
195   bool fileRead (const char* const filename);
196
197   bool fileRead (const std::string& filename);
198
199   bool fileWrite (const char* const filename);
200
201   bool fileWrite (const std::string& filename);
202
203   const std::string& getFilename (void) const 
204       {  return m_filename; }
205
206   void printLabels (std::ostream& os) const;\r
207   void printLabelsBrief (std::ostream& os) const;\r
208   void printLabelsBrief (std::ostringstream& os) const;\r
209 \r
210   unsigned int nLabels() const\r
211   { return m_labels.size(); }\r
212
213   typedef std::vector<Array2dFileLabel*>::iterator labelIterator;
214   typedef std::vector<Array2dFileLabel*>::const_iterator constLabelIterator;
215
216  protected:
217          typedef std::vector<Array2dFileLabel*> labelContainer;
218
219   static const kuint16 m_signature;
220   kuint16 m_headersize;
221   std::string  m_filename;
222
223   kuint16 m_pixelSize;
224   kuint16 m_pixelFormat;
225   kuint16 m_axisIncrementKnown;
226   kfloat64 m_axisIncrementX, m_axisIncrementY;
227   kuint16 m_axisExtentKnown;
228   kfloat64 m_minX, m_maxX, m_minY, m_maxY;
229   kfloat64 m_offsetPV, m_scalePV;
230   kuint32 m_nx;
231   kuint32 m_ny;
232   kuint32 m_arraySize;
233   labelContainer m_labels;
234   kuint16 m_numFileLabels;\r
235   kuint16 m_dataType;
236   unsigned char** m_arrayData;
237   unsigned char** m_imaginaryArrayData;\r
238
239 private:
240   void init (void);
241
242   bool headerRead (frnetorderstream& fs);
243
244   bool headerWrite (frnetorderstream& fs);
245
246   bool arrayDataRead (frnetorderstream& fs);
247
248   bool arrayDataWrite (frnetorderstream& fs);
249
250   bool labelsRead (frnetorderstream& fs);
251
252   bool labelsWrite (frnetorderstream& fs);
253
254   bool labelSeek (int label_num);
255
256   void allocArrays ();
257   void freeArrays ();
258 \r
259   void allocArray (unsigned char**& rppData);\r
260   void freeArray (unsigned char**& rppData);\r
261
262   Array2dFile (const Array2dFile& rhs);        // copy constructor
263   Array2dFile& operator= (const Array2dFile&); // assignment operator
264
265 };
266
267
268
269 #endif