512772d3111418239702bdd59e8ece22dcb623a5
[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.9 2000/12/06 15:17:51 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>
38 #include <fstream>
39 #include <iostream>
40 #include <vector>
41 #include "ctsupport.h"
42 #include "fnetorderstream.h"
43 #include "array2d.h"
44
45 using namespace std;
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 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     string& setLabelString (const char* const str)
80         { m_strLabel = str; return (m_strLabel); }
81
82     string& setLabelString (const 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 string& getDateString () const;
90
91     void print (ostream& 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     string m_strLabel;
108     kfloat64 m_calcTime;
109
110     mutable 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   Array2dFile (int nx, int ny, int pixelSize, int pixelFormat = PIXEL_INVALID);
130   Array2dFile (void);
131   ~Array2dFile ();
132
133   void setArraySize (int nx, int ny, int pixelSize, int pixelFormat = PIXEL_INVALID);
134
135   void setArraySize (int nx, int ny);
136
137   unsigned int getNumLabels (void) const
138       { return m_labels.size(); }
139
140   const Array2dFileLabel& labelGet (int label_num) const;
141
142   void labelAdd (const Array2dFileLabel& label);
143
144   void labelAdd (const char* const m_strLabel, double calc_time=0.);
145
146   void labelAdd (int type, const char* const m_strLabel, double calc_time=0.);
147
148   void labelsCopy (Array2dFile& file, const char* const idStr = NULL);
149
150   void setPixelFormat (int type)
151       { m_pixelFormat = type; }
152
153   void setPixelSize (int size)
154       { m_pixelSize = size; }
155
156   kuint32 nx (void) const
157       { return m_nx; }
158
159   kuint32 ny (void) const
160       { return m_ny; }
161
162   void setAxisIncrement (double axisIncX, double axisIncY);
163
164   void setAxisExtent (double minX, double maxX, double minY, double maxY);
165
166   void getPixelValueRange (double& pvmin, double& pvmax) const;
167       
168   void doPixelOffsetScale (double offset, double scale);
169
170   void arrayDataClear (void);
171
172   bool fileRead (const char* const filename);
173
174   bool fileRead (const string& filename);
175
176   bool fileWrite (const char* const filename);
177
178   bool fileWrite (const string& filename);
179
180   const string& getFilename (void) const 
181       {  return m_filename; }
182
183   void printLabels (ostream& os) const;
184
185   typedef vector<Array2dFileLabel*>::iterator labelIterator;
186   typedef vector<Array2dFileLabel*>::const_iterator constLabelIterator;
187
188  protected:
189   typedef vector<Array2dFileLabel*> labelContainer;
190
191   static const kuint16 m_signature;
192   kuint16 m_headersize;
193   string  m_filename;
194
195   kuint16 m_pixelSize;
196   kuint16 m_pixelFormat;
197   kuint16 m_axisIncrementKnown;
198   kfloat64 m_axisIncrementX, m_axisIncrementY;
199   kuint16 m_axisExtentKnown;
200   kfloat64 m_minX, m_maxX, m_minY, m_maxY;
201   kfloat64 m_offsetPV, m_scalePV;
202   kuint32 m_nx;
203   kuint32 m_ny;
204   kuint32 m_arraySize;
205   labelContainer m_labels;
206   kuint16 m_numFileLabels;
207   unsigned char** m_arrayData;
208
209 private:
210   void init (void);
211
212   bool headerRead (frnetorderstream& fs);
213
214   bool headerWrite (frnetorderstream& fs);
215
216   bool arrayDataRead (frnetorderstream& fs);
217
218   bool arrayDataWrite (frnetorderstream& fs);
219
220   bool labelsRead (frnetorderstream& fs);
221
222   bool labelsWrite (frnetorderstream& fs);
223
224   bool labelSeek (int label_num);
225
226   void allocArray (void);
227   
228   void freeArray (void);
229
230   Array2dFile (const Array2dFile& rhs);        // copy constructor
231   Array2dFile& operator= (const Array2dFile&); // assignment operator
232
233 };
234
235
236
237 #endif