VTK  9.1.0
PIOData.h
Go to the documentation of this file.
1#if !defined(_PIODATA_H)
2#define _PIODATA_H
3
4#include <fstream>
5#include <iostream>
6#include <list>
7#include <map>
8#include <set>
9#include <sstream>
10#include <string.h>
11#include <string>
12#include <valarray>
13
15{
16 bool operator()(const char* p, const char* q) const { return strcmp(p, q) < 0; };
17};
18
19// Class Declarations
20
21class PIO_DATA;
22class PIO_FIELD;
23
25{
26public:
27 char* pio_name;
28 int index;
29 int64_t length;
30 int64_t position;
31 int64_t chksum;
32 size_t cdata_len;
34 friend class PIO_DATA;
35
36protected:
37 double* data;
38 char* cdata;
39}; // End class PIO_FIELD
40
41// Typedefs for the mapping between the names of the PIO blocks in the PIO file and
42// the PIO_FIELD's used to store the data values in the PIO blocks.
43typedef std::multimap<const char*, PIO_FIELD*, Cstring_less> VAR_MAP;
44typedef VAR_MAP::iterator VMI;
45typedef VAR_MAP::const_iterator CVMI;
46typedef std::pair<VMI, VMI> VMP;
47typedef std::pair<CVMI, CVMI> CVMP;
48
50{
51public:
52 PIO_DATA(const char* piofile = 0, const std::list<std::string>* fields_to_read = 0,
53 bool _defer_read_data = true, const std::set<const char*, Cstring_less>* rdata = 0,
54 const std::set<const char*, Cstring_less>* cdata = 0);
56 bool GetPIOfileTime(const char*, double&);
57 void print(std::ostream&);
58 void print(const char*);
59 bool set_scalar_field(std::valarray<int>&, const char*);
60 bool set_scalar_field(std::valarray<int64_t>&, const char*);
61 bool set_scalar_field(std::valarray<uint64_t>&, const char*);
62 bool set_scalar_field(std::valarray<double>&, const char*);
63 bool set_vector_field(std::valarray<std::valarray<double>>&, const char*);
64 inline bool good_read() { return (pio_field != 0) ? true : false; }
65 VAR_MAP VarMMap; // Multimap from pio_name to a PIO_FIELD class
66 const char* get_name() const { return name; }
67 bool get_reverse_endian() const { return reverse_endian; }
68 int get_PIO_VERSION() const { return PIO_VERSION; }
69 int get_PIO_NAME_LENGTH() const { return PIO_NAME_LENGTH; }
70 int get_PIO_HEADER_LENGTH() const { return PIO_HEADER_LENGTH; }
71 int get_PIO_INDEX_LENGTH() const { return PIO_INDEX_LENGTH; }
72 const char* get_pio_dandt() const { return pio_dandt; }
73 int get_pio_num() const { return pio_num; }
74 int get_pio_num_with_size(int64_t n) const;
75 int get_pio_signature() const { return pio_signature; }
76 PIO_FIELD* get_pio_field() const { return pio_field; }
77 void GetPIOData(PIO_FIELD&, const double*&, const char*&);
78 void GetPIOData(PIO_FIELD&, const double*&);
79 void GetPIOData(PIO_FIELD&, const char*&);
80 const double* GetPIOData(PIO_FIELD&);
81 void GetPIOData(const char*, const double*&, const char*&);
82 void GetPIOData(const char*, const double*&);
83 void GetPIOData(const char*, const char*&);
84 const double* GetPIOData(const char*);
85 double GetPIOData(const char*, int);
86 void AddRealData(const char* _name)
87 {
88 if (RealData.find(_name) == RealData.end())
89 RealData.insert(strdup(_name));
90 }
91 void AddCharData(const char* _name)
92 {
93 if (CharData.find(_name) == CharData.end())
94 CharData.insert(strdup(_name));
95 }
96 void FreePIOData(PIO_FIELD& pio_field);
97 bool verbose;
98
99private:
100 std::set<const char*, Cstring_less> RealData;
101 std::set<const char*, Cstring_less> CharData;
102 const char* name;
103 std::istream* Infile;
104 bool reverse_endian;
105 int PIO_VERSION;
106 int PIO_NAME_LENGTH;
107 int PIO_HEADER_LENGTH;
108 int PIO_INDEX_LENGTH;
109 const char* pio_dandt; // Date and Time
110 int pio_num;
111 int64_t pio_position;
112 int pio_signature;
113 PIO_FIELD* pio_field;
114 bool defer_read_data;
115 size_t matident_len;
116 size_t timertype_len;
117
118 char* buf;
119 size_t size_buf;
120 void ReadPioFieldData(PIO_FIELD& pio_field);
121 bool read(const char*, const std::list<std::string>* fields_to_read = 0);
122 bool read(const std::list<std::string>* fields_to_read = 0);
123 inline void byte_flip(char* word, int64_t size)
124 {
125 if (size_buf < (size_t)size)
126 {
127 if (buf)
128 delete[] buf;
129 size_buf = size;
130 buf = new char[size_buf];
131 }
132 memcpy((void*)buf, (const void*)word, size);
133 for (int64_t i = 0; i < size; ++i)
134 word[i] = buf[size - 1 - i];
135 } // End byte_flip
136
137 template <class T>
138 inline T read_pio_word(T& val)
139 {
140 double word;
141 this->Infile->read((char*)&word, sizeof(word));
142 if (reverse_endian)
143 byte_flip((char*)&word, sizeof(word));
144 val = T(word);
145 return val;
146 } // End read_pio_word
147
148 inline bool read_pio_bool()
149 {
150 double word;
151 this->Infile->read((char*)&word, sizeof(word));
152 if (reverse_endian)
153 byte_flip((char*)&word, sizeof(word));
154 return (word != 0) ? true : false;
155 } // End read_pio_bool
156
157 inline void fstr2Cstr(char* s, size_t len) const
158 {
159 s[len] = '\0';
160 size_t i = len - 1;
161 do
162 {
163 if (s[i--] == ' ')
164 s[i + 1] = '\0';
165 } while (i != 0);
166 } // End fstr2Cstr
167
168 inline char* read_pio_char_string(size_t len)
169 {
170 if (size_buf <= len)
171 {
172 if (buf)
173 delete[] buf;
174 size_buf = len + 1;
175 buf = new char[size_buf];
176 }
177 this->Infile->read(buf, len);
178 buf[len] = '\0';
179 fstr2Cstr(buf, len);
180 char* val = new char[strlen(buf) + 1];
181 strcpy(val, buf);
182 return val;
183 } // End read_pio_char_string
184
185 inline void insert_VAR_MAP_pairs()
186 {
187 for (int i = 0; i < pio_num; ++i)
188 {
189 if (pio_field[i].read_field_data)
190 {
191#if !defined __SUNPRO_CC
192 VarMMap.insert(std::make_pair(pio_field[i].pio_name, pio_field + i));
193#else
194 VAR_MAP::value_type type(pio_field[i].pio_name, pio_field + i);
195 VarMMap.insert(type);
196#endif
197 }
198 }
199 } // End insert_VAR_MAP_pairs
200
201 inline bool read_field(const char* pio_name, const std::list<std::string>* fields_to_read)
202 {
203 std::string spio_name = std::string(pio_name);
204 if (fields_to_read == 0)
205 return true;
206 else
207 {
208 for (std::list<std::string>::const_iterator pos = fields_to_read->begin();
209 pos != fields_to_read->end(); ++pos)
210 {
211 if (spio_name == *pos)
212 return true;
213 }
214 }
215 return false;
216 }
217}; // End class PIO_DATA
218
219// Locations of various data items from the input arrays, amhc_i, amhc_r8,
220// amch_l, and controller_r8
221enum
222{
223 Ntime = 0, // time = controller_r8[Ntime];
224 Nnumdim = 42, // numdim = amhc_i[Nnumdim]
225 Nmesh0 = 16, // N[0] = amhc_i[Nmesh0]
226 Nmesh1 = 17, // N[1] = amhc_i[Nmesh1]
227 Nmesh2 = 29, // N[2] = amhc_i[Nmesh2]
228 Nd0 = 21, // d[0] = amhc_r8[Nd0]
229 Nd1 = 22, // d[1] = amhc_r8[Nd1]
230 Nd2 = 38, // d[2] = amhc_r8[Nd2]
231 NZero0 = 19, // Zero[0] = amhc_r8[NZero0]
232 NZero1 = 20, // Zero[1] = amhc_r8[NZero1]
233 NZero2 = 35, // Zero[2] = amhc_r8[NZero2]
234 Ncylin = 1, // cylindrically (axisymmetric) symmetric
235 // geometry if amhc_l[Ncylin]!=0
236 Nsphere = 8 // spherically symmetirc geometry if
237 // amhc_l[Nsphere]!=0
239
240// Prototypes
241bool GetPIOfileTime(const char*, double&);
242bool IsPIOfile(const char*);
243#endif
@ NZero2
Definition: PIOData.h:233
@ Nsphere
Definition: PIOData.h:236
@ Nmesh2
Definition: PIOData.h:227
@ Nmesh1
Definition: PIOData.h:226
@ NZero1
Definition: PIOData.h:232
@ Nmesh0
Definition: PIOData.h:225
@ Nd1
Definition: PIOData.h:229
@ Nd2
Definition: PIOData.h:230
@ Nd0
Definition: PIOData.h:228
@ Nnumdim
Definition: PIOData.h:224
@ Ncylin
Definition: PIOData.h:234
@ NZero0
Definition: PIOData.h:231
@ Ntime
Definition: PIOData.h:223
bool IsPIOfile(const char *)
bool GetPIOfileTime(const char *, double &)
std::pair< VMI, VMI > VMP
Definition: PIOData.h:46
std::pair< CVMI, CVMI > CVMP
Definition: PIOData.h:47
VAR_MAP::iterator VMI
Definition: PIOData.h:44
std::multimap< const char *, PIO_FIELD *, Cstring_less > VAR_MAP
Definition: PIOData.h:43
VAR_MAP::const_iterator CVMI
Definition: PIOData.h:45
PIO_DATA(const char *piofile=0, const std::list< std::string > *fields_to_read=0, bool _defer_read_data=true, const std::set< const char *, Cstring_less > *rdata=0, const std::set< const char *, Cstring_less > *cdata=0)
void print(std::ostream &)
void AddCharData(const char *_name)
Definition: PIOData.h:91
int get_pio_signature() const
Definition: PIOData.h:75
void GetPIOData(PIO_FIELD &, const double *&)
int get_PIO_INDEX_LENGTH() const
Definition: PIOData.h:71
void GetPIOData(PIO_FIELD &, const double *&, const char *&)
void AddRealData(const char *_name)
Definition: PIOData.h:86
void GetPIOData(const char *, const double *&)
PIO_FIELD * get_pio_field() const
Definition: PIOData.h:76
bool GetPIOfileTime(const char *, double &)
void GetPIOData(const char *, const char *&)
double GetPIOData(const char *, int)
const double * GetPIOData(PIO_FIELD &)
const char * get_name() const
Definition: PIOData.h:66
bool good_read()
Definition: PIOData.h:64
int get_pio_num() const
Definition: PIOData.h:73
void GetPIOData(PIO_FIELD &, const char *&)
bool verbose
Definition: PIOData.h:97
void FreePIOData(PIO_FIELD &pio_field)
bool set_vector_field(std::valarray< std::valarray< double > > &, const char *)
bool set_scalar_field(std::valarray< double > &, const char *)
bool get_reverse_endian() const
Definition: PIOData.h:67
int get_pio_num_with_size(int64_t n) const
int get_PIO_HEADER_LENGTH() const
Definition: PIOData.h:70
int get_PIO_NAME_LENGTH() const
Definition: PIOData.h:69
void GetPIOData(const char *, const double *&, const char *&)
bool set_scalar_field(std::valarray< uint64_t > &, const char *)
bool set_scalar_field(std::valarray< int64_t > &, const char *)
void print(const char *)
const double * GetPIOData(const char *)
VAR_MAP VarMMap
Definition: PIOData.h:65
bool set_scalar_field(std::valarray< int > &, const char *)
const char * get_pio_dandt() const
Definition: PIOData.h:72
int get_PIO_VERSION() const
Definition: PIOData.h:68
int64_t chksum
Definition: PIOData.h:31
int index
Definition: PIOData.h:28
bool read_field_data
Definition: PIOData.h:33
char * pio_name
Definition: PIOData.h:27
int64_t length
Definition: PIOData.h:29
char * cdata
Definition: PIOData.h:38
int64_t position
Definition: PIOData.h:30
double * data
Definition: PIOData.h:37
size_t cdata_len
Definition: PIOData.h:32
@ type
Definition: vtkX3D.h:522
bool operator()(const char *p, const char *q) const
Definition: PIOData.h:16