r256: *** empty log message ***
[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.10 2000/12/16 02:44:26 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 using namespace std;
48
49 class Array2dFileLabel
50 {
51 public:
52     enum {
53       L_EMPTY = 0,
54       L_HISTORY = 1,
55       L_USER = 2,
56     };
57
58     Array2dFileLabel(); 
59
60     Array2dFileLabel(const char* const str, double ctime = 0.);
61
62     Array2dFileLabel(const int type, const char* const str, double ctime = 0.);
63
64     ~Array2dFileLabel();
65
66     const string& getLabelString (void) const
67         { return m_strLabel; }
68
69     kfloat64 getCalcTime (void) const
70         { return m_calcTime; }
71
72     void setCalcTime (kfloat64 calcTime)
73         { m_calcTime = calcTime; }
74
75     void setLabelType (int labelType)
76         { m_labelType = labelType; }
77
78     int getLabelType (void) const
79         { return m_labelType; }
80
81     string& setLabelString (const char* const str)
82         { m_strLabel = str; return (m_strLabel); }
83
84     string& setLabelString (const string& str)
85         { m_strLabel = str; return (m_strLabel); }
86
87     void setDateTime (int year, int month, int day, int hour, int minute, int second);
88
89     void getDateTime (int& year, int& month, int& day, int& hour, int& minute, int& second) const;
90
91     const string& getDateString () const;
92
93     void print (std::ostream& os) const;
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     string m_strLabel;
110     kfloat64 m_calcTime;
111
112     mutable 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
131   Array2dFile (int nx, int ny, int pixelSize, int pixelFormat = PIXEL_INVALID);
132   Array2dFile (void);
133   ~Array2dFile ();
134
135   void setArraySize (int nx, int ny, int pixelSize, int pixelFormat = PIXEL_INVALID);
136
137   void setArraySize (int nx, int ny);
138
139   unsigned int getNumLabels (void) const
140       { return m_labels.size(); }
141
142   const Array2dFileLabel& labelGet (int label_num) const;
143
144   void labelAdd (const Array2dFileLabel& label);
145
146   void labelAdd (const char* const m_strLabel, double calc_time=0.);
147
148   void labelAdd (int type, const char* const m_strLabel, double calc_time=0.);
149
150   void labelsCopy (Array2dFile& file, const char* const idStr = NULL);
151
152   void setPixelFormat (int type)
153       { m_pixelFormat = type; }
154
155   void setPixelSize (int size)
156       { m_pixelSize = size; }
157
158   kuint32 nx (void) const
159       { return m_nx; }
160
161   kuint32 ny (void) const
162       { return m_ny; }
163
164   void setAxisIncrement (double axisIncX, double axisIncY);
165
166   void setAxisExtent (double minX, double maxX, double minY, double maxY);
167
168   void getPixelValueRange (double& pvmin, double& pvmax) const;
169       
170   void doPixelOffsetScale (double offset, double scale);
171
172   void arrayDataClear (void);
173
174   bool fileRead (const char* const filename);
175
176   bool fileRead (const string& filename);
177
178   bool fileWrite (const char* const filename);
179
180   bool fileWrite (const string& filename);
181
182   const string& getFilename (void) const 
183       {  return m_filename; }
184
185   void printLabels (ostream& os) const;
186
187   typedef vector<Array2dFileLabel*>::iterator labelIterator;
188   typedef vector<Array2dFileLabel*>::const_iterator constLabelIterator;
189
190  protected:
191   typedef vector<Array2dFileLabel*> labelContainer;
192
193   static const kuint16 m_signature;
194   kuint16 m_headersize;
195   string  m_filename;
196
197   kuint16 m_pixelSize;
198   kuint16 m_pixelFormat;
199   kuint16 m_axisIncrementKnown;
200   kfloat64 m_axisIncrementX, m_axisIncrementY;
201   kuint16 m_axisExtentKnown;
202   kfloat64 m_minX, m_maxX, m_minY, m_maxY;
203   kfloat64 m_offsetPV, m_scalePV;
204   kuint32 m_nx;
205   kuint32 m_ny;
206   kuint32 m_arraySize;
207   labelContainer m_labels;
208   kuint16 m_numFileLabels;
209   unsigned char** m_arrayData;
210
211 private:
212   void init (void);
213
214   bool headerRead (frnetorderstream& fs);
215
216   bool headerWrite (frnetorderstream& fs);
217
218   bool arrayDataRead (frnetorderstream& fs);
219
220   bool arrayDataWrite (frnetorderstream& fs);
221
222   bool labelsRead (frnetorderstream& fs);
223
224   bool labelsWrite (frnetorderstream& fs);
225
226   bool labelSeek (int label_num);
227
228   void allocArray (void);
229   
230   void freeArray (void);
231
232   Array2dFile (const Array2dFile& rhs);        // copy constructor
233   Array2dFile& operator= (const Array2dFile&); // assignment operator
234
235 };
236
237
238
239 #endif