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