Bsoft  2.1.0
Bernard's software package
rwHKL.cpp
Go to the documentation of this file.
1 
9 #include "rwHKL.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 
36 int readHKL(Bimage* p, int readdata)
37 {
38  ifstream fhkl;
39  long i, j, m(1), n(0);
40 
41  fhkl.open(p->file_name());
42  if ( fhkl.fail() ) return -1;
43 
44  char aline[MAXLINELEN];
45 
46  fhkl.getline(aline, MAXLINELEN); // First line with column headers
47  for ( i=0; i<strlen(aline); i++ ) {
48  if ( isspace(aline[i]) || ispunct(aline[i]) ) aline[i]=' ';
49  aline[i] = toupper(aline[i]);
50  }
51 
52  Bstring headerline(aline);
53  Bstring* one;
54  Bstring* header = headerline.split();
55  m = count_list((char *)header);
56 
57  int ih(0); // Find the H index header item
58  int ik(0); // Find the K index header item
59  int il(0); // Find the L index header item
60  int iamp(0); // Find AMP, PHI, FOM and FREE columns
61  int isigamp(0);
62  int iphi(0);
63  int isigphi(0);
64  int ifom(0);
65  int ifre(0);
66 
67  for ( one=header, i=0; one && i<m; one=one->next, i++ ) {
68  if ( *one == "H" || *one == "IH" ) ih = i;
69  if ( *one == "K" || *one == "IK" ) ik = i;
70  if ( *one == "L" || *one == "IL" ) il = i;
71  if ( one->contains("AMP") ) iamp = i;
72  if ( one->contains("SIGAMP") ) isigamp = i;
73  if ( one->contains("PH") ) iphi = i;
74  if ( one->contains("SIGPH") ) isigphi = i;
75  if ( one->contains("FOM") ) ifom = i;
76  if ( one->contains("FRE") ) ifre = i;
77  }
78 
79  if ( verbose & VERB_PROCESS ) {
80  cout << "Headers: ";
81  for ( one=header; one; one=one->next ) cout << " " << *one;
82  cout << endl;
83  }
84 
85  float v[64], fommax(0); // Variable input list
86  char* aptr;
87  int k, hmin(0), hmax(0), kmin(0), kmax(0), lmin(0), lmax(0);
88  while ( fhkl.getline(aline, MAXLINELEN) ) {
89  if ( strlen(aline) > 10 ) {
90  memset(v,0,64*sizeof(float));
91  aptr = aline;
92  i = 0;
93  while ( strlen(aptr) > 1 ) {
94  if ( sscanf(aptr, "%f%n", &v[i], &k) < 1 && i < m ) {
95  fhkl.getline(aline, MAXLINELEN);
96  aptr = aline;
97  sscanf(aptr, "%f%n", &v[i], &k);
98  }
99  aptr += k;
100  i++;
101  }
102  if ( i ) {
103  if ( hmin > v[ih] ) hmin = (int) v[ih];
104  if ( ik < m && kmin > v[ik] ) kmin = (int) v[ik];
105  if ( il < m && lmin > v[il] ) lmin = (int) v[il];
106  if ( hmax < v[ih] ) hmax = (int) v[ih];
107  if ( ik < m && kmax < v[ik] ) kmax = (int) v[ik];
108  if ( il < m && lmax < v[il] ) lmax = (int) v[il];
109  if ( ifom < m && fommax < v[ifom] ) fommax = v[ifom];
110  n++;
111  }
112  }
113  }
114 
115  fhkl.close();
116 
117  if ( verbose & VERB_DEBUG )
118  cout << "DEBUG rwHKL: First pass done" << endl;
119 
120  p->images(1);
121  p->size(1,1,1);
122  if ( p->sizeX() < (long)2*(hmax + 1) ) p->sizeX(2*(hmax + 1));
123  if ( p->sizeX() < (long)2*(-hmin) ) p->sizeX(-2*hmin);
124  if ( p->sizeY() < (long)2*(kmax + 1) ) p->sizeY(2*(kmax + 1));
125  if ( p->sizeY() < (long)2*(-kmin) ) p->sizeY(-2*kmin);
126  if ( p->sizeZ() < (long)2*(lmax + 1) ) p->sizeZ(2*(lmax + 1));
127  if ( p->sizeZ() < (long)2*(-lmin) ) p->sizeZ(-2*lmin);
128 // if ( p->FOM_maximum() < 1 ) p->FOM_maximum(1); // Preserve fractional FOM values
129  if ( kmin == kmax ) p->sizeY(1);
130  if ( lmin == lmax ) p->sizeZ(1);
131 
132  int x(0), y(0), z(0);
133  p->data_type(Float);
135  p->channels(2);
137 
138 
139  if ( !readdata ) return 0;
140 
141  Complex<float>* data = (Complex<float>*) p->data_alloc();
142  float* fom = NULL;
143  if ( ifom ) {
144  p->next = new Bimage(Float, TSimple, p->size(), p->images());
145  fom = (float *) p->next->data_pointer();
146  }
147 
148  if ( verbose & VERB_DEBUG )
149  cout << "DEBUG rwHKL: Memory allocated" << endl;
150 
151 
152  fhkl.open(p->file_name());
153  if ( fhkl.fail() ) return -1;
154 
155  fhkl.getline(aline,128); // Ignore the header line
156 
157  while ( fhkl.getline(aline, MAXLINELEN) ) {
158  if ( verbose & VERB_DEBUG )
159  cout << aline << endl;
160  if ( strlen(aline) > 10 ) {
161  memset(v,'\0',64*sizeof(float));
162  aptr = aline;
163  i = 0;
164  while ( strlen(aptr) > 1 ) {
165  if ( sscanf(aptr, "%f%n", &v[i], &k) < 1 && i < m ) {
166  fhkl.getline(aline, MAXLINELEN);
167  aptr = aline;
168  sscanf(aptr, "%f%n", &v[i], &k);
169  }
170  aptr += k;
171  i++;
172  }
173  if ( i ) {
174  x = (int) v[ih];
175  if ( x < 0 ) x += p->sizeX();
176  if ( ik ) {
177  y = (int) v[ik];
178  if ( y < 0 ) y += p->sizeY();
179  }
180  if ( il ) {
181  z = (int) v[il];
182  if ( z < 0 ) z += p->sizeZ();
183  }
184  j = (z*p->sizeY() + y)*p->sizeX() + x;
185  if ( iphi ) {
186  v[iphi] *= M_PI/180.0;
187  data[j] = Complex<float>(v[iamp]*cos(v[iphi]), v[iamp]*sin(v[iphi]));
188  }
189  if ( p->next && ifom ) fom[j] = v[ifom]/fommax;
190  }
191  }
192  }
193 
194  if ( verbose & VERB_DEBUG )
195  cout << "DEBUG rwHKL: File read" << endl;
196 
197  fhkl.close();
198 
199  string_kill(header);
200 
201  return 0;
202 }
203 
211 {
212  if ( p->compound_type() < TComplex ) p->simple_to_complex();
213  p->change_type(Float);
214 
215  ofstream fhkl;
216  long i, x, y, z, nw(0), freeflag(0);
217  long h, k, l;
218  float fom1;
219  float* fom = NULL;
220  if ( p->next ) fom = (float *) p->next->data_pointer();
221  Complex<float>* data = (Complex<float> *) p->data_pointer();
222 
223  fhkl.open(p->file_name());
224  if ( fhkl.fail() ) return -1;
225 
226  fhkl << " H K L AMP PHI FOM FREE" << endl;
227 
228  for ( i=z=0; z<p->sizeZ(); z++ ) {
229  l = z;
230  if ( l > (p->sizeZ() - 1)/2 ) l -= p->sizeZ();
231  for ( y=0; y<p->sizeY(); y++ ) {
232  k = y;
233  if ( k > (p->sizeY() - 1)/2 ) k -= p->sizeY();
234  for ( x=0; x<p->sizeX(); x++, i++ ) {
235  h = x;
236  if ( h > (p->sizeX() - 1)/2 ) h -= p->sizeX();
237  fom1 = 1;
238  if ( data[i].amp() < 1e-10 ) fom1 = 0;
239  if ( fom ) fom1 = fom[i];
240  if ( fom1 > 1e-10 ) {
241  fhkl << h << " " << k << " " << l << " " << data[i].amp() << " "
242  << data[i].phi()*180/M_PI << " " << fom1 << " " << freeflag << endl;
243  nw++;
244  }
245  }
246  }
247  }
248 
249  fhkl.close();
250 
251  if ( verbose & VERB_DEBUG ) {
252  cout << "DEBUG writeHKL: Number of structure factors written: " << nw << endl;
253  }
254 
255  return 0;
256 }
257 
MAXLINELEN
#define MAXLINELEN
Definition: utilities.h:45
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:768
Complex
Definition: Complex.h:23
Bstring
Definition: Bstring.h:29
Bimage::next
Bimage * next
Definition: Bimage.h:179
Bimage::simple_to_complex
void simple_to_complex()
A simple image is converted to a complex image.
Definition: Bimage_complex.cpp:22
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
Complex.h
Class for complex numbers.
TComplex
@ TComplex
Definition: Bimage.h:60
writeHKL
int writeHKL(Bimage *p)
Writing a HKL structure factor file format. This function writes a HKL structure factor file.
Definition: rwHKL.cpp:210
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
count_list
long count_list(char *list)
Counts the number of items in a linked list.
Definition: linked_list.cpp:235
Standard
@ Standard
Definition: Bimage.h:42
Bimage::images
long images()
Definition: Bimage.h:504
TSimple
@ TSimple
Definition: Bimage.h:59
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
Bimage::fourier_type
FourierType fourier_type()
Definition: Bimage.h:380
rwHKL.h
Header file for reading and writing HKL reflection files as images.
verbose
int verbose
kmax
long kmax(long i, long km, long m)
Definition: Bimage_filter.cpp:22
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:364
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:271
Complex::phi
double phi()
Definition: Complex.h:105
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Bstring::next
Bstring * next
Definition: Bstring.h:31
linked_list.h
Header file for generalized linked list functions.
Bstring::split
Bstring * split()
Definition: Bstring.cpp:605
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
kmin
long kmin(long i, long hk)
Definition: Bimage_filter.cpp:17
Complex::amp
double amp()
Definition: Complex.h:104
Float
@ Float
Definition: utilities.h:161
Bimage::channels
long channels()
Definition: Bimage.h:384
readHKL
int readHKL(Bimage *p, int readdata)
Reading a HKL structure factor file format. This function reads a HKL file typically produced with MT...
Definition: rwHKL.cpp:36