| Bsoft 2.0.5 Bernard's software package

rwASCII.cpp
Go to the documentation of this file.
1
9 #include "rwASCII.h"
10 #include "Complex.h"
11 #include "linked_list.h"
12 #include "utilities.h"
13 #include <fstream>
14
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17
34 int readASCII(Bimage* p, int readdata)
35 {
36 ifstream fimg;
37 fimg.open(p->file_name());
38 if ( fimg.fail() ) return -1;
39
40 long i, j, k, l, m = 1;
41 double v[16], min[16], max[16];
42 long order[16];
43 char aline[MAXLINELEN];
44 Vector3<double> ori;
45
46 if ( verbose & VERB_DEBUG )
47 cout << "DEBUG readASCII: Start reading text image" << endl;
48
49 // Initialize order and extrema arrays
50 for ( i=0; i<16; i++ ) {
51 order[i] = -1;
52 min[i] = 1e30;
53 max[i] = -1e30;
54 v[i] = 0;
55 }
56
57 // The first line contains the column labels
58 fimg.getline( aline, MAXLINELEN);
59 for ( size_t i=0; i<strlen(aline); i++ ) {
60 if ( isspace(aline[i]) || ispunct(aline[i]) ) aline[i]=' ';
61 aline[i] = toupper(aline[i]);
62 }
63
64 if ( verbose & VERB_DEBUG )
65 cout << "DEBUG readASCII: Header line: " << aline << endl;
66
67 Bstring headerline(aline);
68 Bstring* one;
69 Bstring* label = headerline.split();
70 m = count_list((char *)label);
71
72 if ( verbose & VERB_DEBUG ) {
73 cout << "DEBUG readASCII: " << m << " column labels:";
74 for ( one=label; one; one=one->next ) cout << *one << " ";
75 cout << endl;
76 }
77
78 if ( verbose & VERB_DEBUG ) {
79 if ( p->fourier_type() == NoTransform ) cout << "DEBUG readASCII: An image" << endl;
80 else cout << "DEBUG readASCII: A transform" << endl;
81 }
82
83 // Associate each label with an index and a value variable
84 for ( one=label, i=0; one && i<m; one=one->next, i++ ) {
85 if ( order[0] < 0 && (*one)[0] == 'X' ) order[0] = i;
86 if ( order[0] < 0 && (*one)[0] == 'H' ) {
87 order[0] = i;
88 p->compound_type(TComplex);
89 p->fourier_type(Standard);
90 p->channels(2);
91 }
92 if ( order[1] < 0 && (*one)[0] == 'Y' ) order[1] = i;
93 if ( order[1] < 0 && (*one)[0] == 'K' ) order[1] = i;
94 if ( order[2] < 0 && (*one)[0] == 'Z' ) order[2] = i;
95 if ( order[2] < 0 && (*one)[0] == 'L' ) order[2] = i;
96 if ( order[3] < 0 && (*one)[0] == 'N' ) order[3] = i;
97 if ( order[4] < 0 && (*one)[0] == 'R' ) order[4] = i;
98 if ( order[4] < 0 && (*one)[0] == 'A' ) order[4] = i;
99 if ( order[4] < 0 && *one == "VX" ) {
100 order[4] = i;
101 p->compound_type(TVector3);
102 p->channels(3);
103 }
104 if ( order[4] < 0 && *one == "Red" ) {
105 order[4] = i;
106 p->compound_type(TRGB);
107 p->channels(3);
108 }
109 if ( order[4] < 0 && *one == "Cyn" ) {
110 order[4] = i;
111 p->compound_type(TCMYK);
112 p->channels(4);
113 }
114 if ( order[5] < 0 && (*one)[0] == 'I' ) order[5] = i;
115 if ( order[5] < 0 && (*one)[0] == 'P' ) order[5] = i;
116 if ( order[5] < 0 && *one == "VY" ) order[5] = i;
117 if ( order[5] < 0 && *one == "Grn" ) order[5] = i;
118 if ( order[5] < 0 && *one == "Mag" ) order[5] = i;
119 if ( order[6] < 0 && (*one)[0] == 'P' ) order[6] = i;
120 if ( order[6] < 0 && *one == "VZ" ) order[6] = i;
121 if ( order[6] < 0 && *one == "Blu" ) order[6] = i;
122 if ( order[6] < 0 && *one == "Yel" ) order[6] = i;
123 if ( order[7] < 0 && (*one)[0] == 'P' ) order[7] = i;
124 if ( order[7] < 0 && *one == "Alf" ) {
125 order[7] = i;
126 p->compound_type(TRGBA);
127 p->channels(4);
128 }
129 if ( order[7] < 0 && *one == "blK" ) order[7] = i;
130 if ( order[8] < 0 && (*one)[0] == 'F' ) order[8] = i;
131 }
132
133 if ( verbose & VERB_DEBUG )
134 cout << "DEBUG readASCII: order: " << order[0] << " " << order[1] << " " << order[2] << " " <<
135 order[3] << " " << order[4] << " " << order[5] << " " << order[6] << " " << order[7] << " " << order[8] << " (m=" << m << ")" << endl;
136
137 // Pass once through the file to get the number of data points and extrema
138 i = 0;
139 while ( !fimg.eof() ) {
140 fimg.getline(aline, MAXLINELEN);
141 if ( sscanf(aline, "%lf %lf %lf %lf %lf %lf %lf %lf %lf", &v[0], &v[1], &v[2],
142 &v[3], &v[4], &v[5], &v[6], &v[7], &v[8]) < m ) {
143 cout << "Missing data at line " << i << endl;
144 } else {
145 for ( j=0; j<9; j++ ) {
146 if ( min[j] > v[j] ) min[j] = v[j];
147 if ( max[j] < v[j] ) max[j] = v[j];
148 }
149 /* if ( verbose & VERB_DEBUG ) {
150 cout << "DEBUG readASCII: " << i;
151 for ( j=0; j<m; j++ ) cout << " " << v[j];
152 cout << endl;
153 }*/
154 i++;
155 }
156 }
157
158 fimg.close();
159
160 // Set some parameters for the image
161 p->data_type(Float);
162 p->channels(1);
163 if ( order[3] > -1 ) p->images((long) (max[order[3]] + 1));
164 else p->images(1);
165
166 if ( p->fourier_type() == NoTransform ) {
167 if ( order[0] > -1 ) {
168 p->sizeX((long) (max[order[0]] - min[order[0]] + 1));
169 ori[0] = min[order[0]];
170 }
171 if ( order[1] > -1 ) {
172 p->sizeY((long) (max[order[1]] - min[order[1]] + 1));
173 ori[1] = min[order[1]];
174 }
175 if ( order[2] > -1 ) {
176 p->sizeZ((long) (max[order[2]] - min[order[2]] + 1));
177 ori[2] = min[order[2]];
178 }
179 p->origin(ori);
180 } else {
181 if ( order[0] > -1 ) {
182 p->sizeX((int) fabs(min[order[0]]));
183 if ( p->sizeX() < fabs(max[order[0]]) ) p->sizeX((long) fabs(max[order[0]]));
184 p->sizeX(p->sizeX() + 1);
185 }
186 if ( order[1] > -1 ) {
187 p->sizeY((int) fabs(min[order[1]]));
188 if ( p->sizeY() < fabs(max[order[1]]) ) p->sizeY((long) fabs(max[order[1]]));
189 p->sizeY(p->sizeY() + 1);
190 }
191 if ( order[2] > -1 ) {
192 p->sizeZ((int) fabs(min[order[2]]));
193 if ( p->sizeZ() < fabs(max[order[2]]) ) p->sizeZ((long) fabs(max[order[2]]));
194 p->sizeZ(p->sizeZ() + 1);
195 }
196 }
197
198 for ( i=4; i<8; i++ ) {
199 if ( order[i] > -1 ) {
200 if ( p->minimum() > min[order[i]] ) p->minimum(min[order[i]]);
201 if ( p->maximum() < max[order[i]] ) p->maximum(max[order[i]]);
202 }
203 }
204
205 p->size(p->size().max(1));
206
207 if ( verbose & VERB_DEBUG )
208 cout << "DEBUG readASCII: size=" << p->size() << " channels=" << p->channels() << " images=" << p->images() << endl;
209
210 if ( !readdata ) return 0;
211
212 // Allocate memory for the data
213 p->data_alloc();
214 float* fom = NULL;
215 if ( order[8] > -1 ) {
216 p->next = new Bimage(Float, TSimple, p->size(), p->images());
217 fom = (float *) p->next->data_pointer();
218 }
219
220 // Rewind and read the data into the image
221 fimg.open(p->file_name());
222 if ( fimg.fail() ) return -1;
223
224 fimg.getline( aline, MAXLINELEN); // Header labels
225
226 long c(0), x(0), y(0), z(0), n(0);
227 i = 0;
228 while ( !fimg.eof() ) {
229 fimg.getline(aline, MAXLINELEN);
230 if ( sscanf(aline, "%lf %lf %lf %lf %lf %lf %lf %lf %lf", &v[0], &v[1], &v[2],
231 &v[3], &v[4], &v[5], &v[6], &v[7], &v[8]) < m ) {
232 cout << "Missing data at line " << i << endl;
233 } else {
234 if ( order[0] > -1 ) x = (long) v[order[0]];
235 if ( order[1] > -1 ) y = (long) v[order[1]];
236 if ( order[2] > -1 ) z = (long) v[order[2]];
237 if ( order[3] > -1 ) n = (long) v[order[3]];
238 if ( x < 0 ) x += p->sizeX(); // Wrapping for structure factors
239 if ( y < 0 ) y += p->sizeY();
240 if ( z < 0 ) z += p->sizeZ();
241 j = ((n*p->sizeZ() + z)*p->sizeY() + y)*p->sizeX() + x;
242 for ( c=0, k=j*p->channels(), l=4; c<p->channels(); c++, k++, l++ ) p->set(k, v[order[l]]);
243 if ( order[8] > -1 ) fom[j] = v[order[8]];
244 i++;
245 if ( verbose & VERB_DEBUG ) {
246 cout << "DEBUG readASCII: " << i;
247 for ( j=0; j<m; j++ ) cout << " " << v[j];
248 for ( c=0, k=j*p->channels(); c<p->channels(); c++, k++ ) cout << " " << (*p)[k];
249 cout << endl;
250 }
251 }
252 }
253
254 if ( verbose & VERB_PROCESS )
255 cout << "Data points read: " << i << endl << endl;
256
257 fimg.close();
258
259 string_kill(label);
260
261 return 0;
262 }
263
279 int writeASCII(Bimage* p)
280 {
281 ofstream fimg;
282 fimg.open(p->file_name());
283 if ( fimg.fail() ) return -1;
284
285 long i(0);
286 long order[10] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
287 char label[128];
288 memset(label, ' ', 128*sizeof(char));
289
290 // Get the data order and labels
291 if ( p->sizeX() > 1 ) {
292 order[0] = i;
293 strncpy(label + 10*i, "X", 1);
294 if ( p->fourier_type() ) strncpy(label + 10*i, "H", 1);
295 i++;
296 }
297 if ( p->sizeY() > 1 ) {
298 order[1] = i;
299 strncpy(label + 10*i, "Y", 1);
300 if ( p->fourier_type() ) strncpy(label + 10*i, "K", 1);
301 i++;
302 }
303 if ( p->sizeZ() > 1 ) {
304 order[2] = i;
305 strncpy(label + 10*i, "Z", 1);
306 if ( p->fourier_type() ) strncpy(label + 10*i, "L", 1);
307 i++;
308 }
309 if ( p->images() > 1 ) {
310 order[3] = i;
311 strncpy(label + 10*i, "Nimage", 6);
312 i++;
313 }
314
315 switch ( p->compound_type() ) {
316 case TSimple: strncpy(label + 10*i, "Real", 4); i++; break;
317 case TComplex: strncpy(label + 10*i, "Real", 4); i++;
318 strncpy(label + 10*i, "Imag", 4); i++; break;
319 case TVector3: strncpy(label + 10*i, "VX", 4); i++;
320 strncpy(label + 10*i, "VY", 4); i++;
321 strncpy(label + 10*i, "VX", 4); i++; break;
322 case TRGB: strncpy(label + 10*i, "Red", 4); i++;
323 strncpy(label + 10*i, "Grn", 4); i++;
324 strncpy(label + 10*i, "Blu", 4); i++; break;
325 case TRGBA: strncpy(label + 10*i, "Red", 4); i++;
326 strncpy(label + 10*i, "Grn", 4); i++;
327 strncpy(label + 10*i, "Blu", 4); i++;
328 strncpy(label + 10*i, "Alf", 4); i++; break;
329 case TCMYK: strncpy(label + 10*i, "Cyn", 4); i++;
330 strncpy(label + 10*i, "Mag", 4); i++;
331 strncpy(label + 10*i, "Yel", 4); i++;
332 strncpy(label + 10*i, "blK", 4); i++; break;
333 default: strncpy(label + 10*i, "Real", 4); i++; break;
334 }
335
336 if ( p->next ) {
337 order[7] = i;
338 strncpy(label + 10*i, "FOM", 3);
339 i++;
340 }
341 strncpy(label + 10*i, "\0", 1);
342
343 if ( verbose & VERB_DEBUG ) {
344 cout << "DEBUG writeASCII: label: " << label << endl;
345 cout << "DEBUG writeASCII: order: " << order[0] << " " << order[1] << " " << order[2] << " " <<
346 order[3] << " " << order[4] << " " << order[5] << " " << order[6] << " " << order[7] << " " << order[8] << " (m=" << i << ")" << endl;
347 }
348
349 fimg << label << endl;
350
351 long c, x, y, z, n;
352 float* fom = (float *) p->next->data_pointer();
353
354 for ( i=n=0; n<p->images(); n++ ) {
355 for ( z=0; z<p->sizeZ(); z++ ) {
356 for ( y=0; y<p->sizeY(); y++ ) {
357 for ( x=0; x<p->sizeX(); x++ ) {
358 if ( order[0] > -1 ) fimg << " " << x;
359 if ( order[1] > -1 ) fimg << " " << y;
360 if ( order[2] > -1 ) fimg << " " << z;
361 if ( order[3] > -1 ) fimg << " " << n;
362 for ( c=0; c<p->channels(); c++, i++ ) fimg << " " << (*p)[i];
363 if ( order[4] > -1 ) fimg << " " << fom[i];
364 fimg << endl;
365 }
366 }
367 }
368 }
369
370 fimg.close();
371
372 return 0;
373 }
374
rwASCII.h
Header file for reading and writing ASCII files.
utilities.h
Header file for general utilities.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:351
Complex.h
Class for complex numbers.
count_list
long count_list(char *list)
Counts the number of items in a linked list.
Definition: linked_list.cpp:235
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
MAXLINELEN
#define MAXLINELEN
Definition: utilities.h:44
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
TComplex
Definition: Bimage.h:59
TCMYK
Definition: Bimage.h:64
TVector3
Definition: Bimage.h:60
TSimple
Definition: Bimage.h:58
Float
Definition: utilities.h:157
Vector3::max
Type max()
Definition: Vector3.h:175
Bimage
General image parameter class.
Definition: Bimage.h:163
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:256
linked_list.h
Header file for generalized linked list functions.
Bimage::maximum
double maximum()
Definition: Bimage.h:535
Bstring::next
Bstring * next
Definition: Bstring.h:31
writeASCII
int writeASCII(Bimage *p)
Writing an ASCII or text image format.
Definition: rwASCII.cpp:279
Standard
Definition: Bimage.h:41
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
Bimage::channels
long channels()
Definition: Bimage.h:371
verbose
int verbose
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1096
TRGB
Definition: Bimage.h:62
Bimage::next
Bimage * next
Definition: Bimage.h:165
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:261
Bstring
Definition: Bstring.h:29
readASCII
int readASCII(Bimage *p, int readdata)
Reading an ASCII or text image format.
Definition: rwASCII.cpp:34
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
Vector3< double >
TRGBA
Definition: Bimage.h:63
Bimage::images
long images()
Definition: Bimage.h:491
NoTransform
Definition: Bimage.h:40
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
Bimage::minimum
double minimum()
Definition: Bimage.h:534
Bimage::fourier_type
FourierType fourier_type()
Definition: Bimage.h:367
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563
Bstring::split
Bstring * split()
Definition: Bstring.cpp:597
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:760