r140: *** 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.4 2000/07/09 08:16:17 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 #include <sys/types.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <string>
35 #include <fstream>
36 #include <iostream>
37 #include <vector>
38 #include "ctsupport.h"
39 #include "fnetorderstream.h"
40 #include "array2d.h"
41
42 using namespace std;
43
44 class Array2dFileLabel
45 {
46 public:
47     static const int L_EMPTY = 0;
48     static const int L_HISTORY = 1;
49     static const int L_USER = 2;
50
51     Array2dFileLabel(); 
52
53     Array2dFileLabel(const char* const str, double ctime = 0.);
54
55     Array2dFileLabel(const int type, const char* const str, double ctime = 0.);
56
57     ~Array2dFileLabel();
58
59     const string& getLabelString (void) const
60         { return m_strLabel; }
61
62     kfloat64 getCalcTime (void) const
63         { return m_calcTime; }
64
65     void setCalcTime (kfloat64 calcTime)
66         { m_calcTime = calcTime; }
67
68     void setLabelType (int labelType)
69         { m_labelType = labelType; }
70
71     int getLabelType (void) const
72         { return m_labelType; }
73
74     string& setLabelString (const char* const str)
75         { m_strLabel = str; return (m_strLabel); }
76
77     string& setLabelString (const string& str)
78         { m_strLabel = str; return (m_strLabel); }
79
80     void setDateTime (int year, int month, int day, int hour, int minute, int second);
81
82     void getDateTime (int& year, int& month, int& day, int& hour, int& minute, int& second) const;
83
84     const string& getDateString () const;
85
86     void print (ostream& os) const;
87
88     Array2dFileLabel (const Array2dFileLabel& rhs);
89
90     Array2dFileLabel& operator= (const Array2dFileLabel& rhs);
91
92 private:
93     void init (void);
94
95     kuint16 m_labelType;
96     kuint16 m_year;
97     kuint16 m_month;
98     kuint16 m_day;
99     kuint16 m_hour;
100     kuint16 m_minute;
101     kuint16 m_second;
102     string m_strLabel;
103     kfloat64 m_calcTime;
104
105     mutable string m_strDate;
106 };
107
108
109 class Array2dFile 
110 {
111 public:
112   static const int PIXEL_INVALID = 0;
113   static const int PIXEL_INT8 = 1;
114   static const int PIXEL_UINT8 = 2;
115   static const int PIXEL_INT16 = 3;
116   static const int PIXEL_UINT16 = 4;
117   static const int PIXEL_INT32 = 5;
118   static const int PIXEL_UINT32 = 6;
119   static const int PIXEL_FLOAT32 = 7;
120   static const int PIXEL_FLOAT64 = 8;
121
122   Array2dFile (int nx, int ny, int pixelSize, int pixelFormat = PIXEL_INVALID);
123   Array2dFile (void);
124   ~Array2dFile ();
125
126   void setArraySize (int nx, int ny, int pixelSize, int pixelFormat = PIXEL_INVALID);
127
128   void setArraySize (int nx, int ny);
129
130   unsigned int getNumLabels (void) const
131       { return m_labels.size(); }
132
133   const Array2dFileLabel& labelGet (int label_num) const;
134
135   void labelAdd (const Array2dFileLabel& label);
136
137   void labelAdd (const char* const m_strLabel, double calc_time=0.);
138
139   void labelAdd (int type, const char* const m_strLabel, double calc_time=0.);
140
141   void labelsCopy (Array2dFile& file, const char* const idStr = NULL);
142
143   void setPixelFormat (int type)
144       { m_pixelFormat = type; }
145
146   void setPixelSize (int size)
147       { m_pixelSize = size; }
148
149   kuint32 nx (void) const
150       { return m_nx; }
151
152   kuint32 ny (void) const
153       { return m_ny; }
154
155   void setAxisIncrement (double axisIncX, double axisIncY);
156
157   void setAxisExtent (double minX, double maxX, double minY, double maxY);
158
159   void getPixelValueRange (double& pvmin, double& pvmax) const;
160       
161   void doPixelOffsetScale (double offset, double scale);
162
163   void arrayDataClear (void);
164
165   bool fileRead (const char* const filename);
166
167   bool fileRead (const string& filename);
168
169   bool fileWrite (const char* const filename);
170
171   bool fileWrite (const string& filename);
172
173   const string& getFilename (void) const 
174       {  return m_filename; }
175
176   void printLabels (ostream& os) const;
177
178   typedef vector<Array2dFileLabel*>::iterator labelIterator;
179   typedef vector<Array2dFileLabel*>::const_iterator constLabelIterator;
180
181  protected:
182   typedef vector<Array2dFileLabel*> labelContainer;
183
184   static const kuint16 m_signature = ('I'*256+'F');
185   kuint16 m_headersize;
186   string  m_filename;
187
188   kuint16 m_pixelSize;
189   kuint16 m_pixelFormat;
190   kuint16 m_axisIncrementKnown;
191   kfloat64 m_axisIncrementX, m_axisIncrementY;
192   kuint16 m_axisExtentKnown;
193   kfloat64 m_minX, m_maxX, m_minY, m_maxY;
194   kfloat64 m_offsetPV, m_scalePV;
195   kuint32 m_nx;
196   kuint32 m_ny;
197   kuint32 m_arraySize;
198   labelContainer m_labels;
199   kuint16 m_numFileLabels;
200   unsigned char** m_arrayData;
201
202 private:
203   void init (void);
204
205   bool headerRead (frnetorderstream& fs);
206
207   bool headerWrite (frnetorderstream& fs);
208
209   bool arrayDataRead (frnetorderstream& fs);
210
211   bool arrayDataWrite (frnetorderstream& fs);
212
213   bool labelsRead (frnetorderstream& fs);
214
215   bool labelsWrite (frnetorderstream& fs);
216
217   bool labelSeek (int label_num);
218
219   void allocArray (void);
220   
221   void freeArray (void);
222
223   Array2dFile (const Array2dFile& rhs);        // copy constructor
224   Array2dFile& operator= (const Array2dFile&); // assignment operator
225
226 };
227
228
229
230 #endif