Bsoft  2.1.0
Bernard's software package
mg_tomo_rec.cpp
Go to the documentation of this file.
1 
9 #include "mg_tomo_rec.h"
10 #include "mg_reconstruct.h"
11 #include "mg_tomography.h"
12 #include "mg_select.h"
13 #include "mg_ctf.h"
14 #include "Complex.h"
15 #include "linked_list.h"
16 #include "utilities.h"
17 #include "timer.h"
18 
19 #include <fstream>
20 
21 #include <sys/stat.h>
22 
23 // Declaration of global variables
24 extern int verbose; // Level of output to the screen
25 
26 // Internal function prototypes
27 int img_backtransform_z_lines(fstream* ftemp, Bimage* p);
28 int img_write_data_block_with_type(fstream* ftemp, Bimage* p,
29  double avg, double std, double cutmin, double cutmax);
30 
31 Bimage* mg_tomo_rec_prepare(Bmicrograph* mg, int ft_size, long thickness,
32  double edge_width, double marker_radius,
33  int fill_type, double fill,
34  int action, double wiener, Vector3<long> tile_size, fft_plan plan)
35 {
36  if ( verbose & VERB_FULL )
37  cout << "Reading image " << mg->img_num << " (micrograph " << mg->id << ")" << endl;
38 
39 // Bimage* p = mg_erase_markers(mg, marker_radius);
40 
41  Bimage* p = read_img(mg->fmg, 1, mg->img_num);
42  if ( !p ) {
43  error_show("mg_erase_markers", __FILE__, __LINE__);
44  return NULL;
45  }
46 
47  p->change_type(Float);
48 
49  p->sampling(mg->pixel_size);
50 
51  p->origin(mg->origin[0], mg->origin[1], 0);
52 
53  p->statistics();
54  p->rescale_to_avg_std(0,1);
55 
56 // Vector3<long> tile_size(128,128,1);
57 // Vector3<long> tile_size(64,64,1);
58  if ( action ) {
59  if ( verbose & VERB_PROCESS )
60  cout << "Correcting for the CTF" << endl;
61  if ( !mg->ctf ) {
62  cerr << "Error: The CTF parameters are not specified! Abort!" << endl;
63  bexit(-1);
64  }
65  img_ttf_apply(p, *(mg->ctf), action, wiener,
66  tile_size, mg->tilt_angle, mg->tilt_axis, 0, 0);
67  }
68 
69  if ( edge_width )
70  micrograph_clear_extraneous_areas(mg, p, thickness, edge_width);
71 
72  if ( marker_radius )
73  img_erase_markers(p, mg->mark, marker_radius);
74 
75 // p->calculate_background();
76 // p->statistics();
77 // p->rescale_to_avg_std(0,1);
78 
79 // p->pad(ft_size, fill_type, fill);
80  p->pad(ft_size);
81 
82  p->fft(plan, 1);
83 
85 
86  return p;
87 }
88 
89 
125  double scale, Vector3<long> size, int interp_type, int pad_factor,
126  double edge_width, double marker_radius,
127  int fill_type, double fill, int action, double wiener)
128 {
129  double ti = timer_start();
130 
132  Bfield* field;
133  Bmicrograph* mg = NULL;
134 
135  if ( nmg < 1 ) {
136  cerr << "Error: No micrographs selected!" << endl;
137  return NULL;
138  }
139 
140  for ( field = project->field; field; field = field->next ) {
141  for ( mg = field->mg; mg; mg = mg->next ) if ( mg->select )
142  if ( mg->fmg.length() ) break;
143  if ( mg ) break;
144  }
145 
146  if ( !mg ) {
147  cerr << "No selected micrograph found!" << endl << endl;
148  return NULL;
149  }
150 
151  if ( !mg->fmg.length() ) {
152  cerr << "No file name for micrograph " << mg->id << endl << endl;
153  return NULL;
154  }
155 
156  Bimage* p = read_img(mg->fmg, 0, mg->img_num);
157 
158  if ( size.volume() < 1 )
159  size = Vector3<long>(p->sizeX()*scale, p->sizeY()*scale, p->sizeX()*scale/10);
160 
161  int ft_size = part_ft_size(p->sizeX(), scale, pad_factor);
162  double thickness(size[2]/scale);
163 
164  delete p;
165 
166  if ( hi_res < 2*mg->pixel_size[0]/scale )
167  hi_res = 2*mg->pixel_size[0]/scale;
168  double rec_scale = 2*mg->pixel_size[0]/(scale*hi_res);
169  if ( rec_scale > 1 ) rec_scale = 1;
170  double voxel_size(mg->pixel_size[0]/(scale*rec_scale));
171  Vector3<double> vscale(rec_scale*scale, rec_scale*scale, rec_scale*scale);
172  Vector3<long> rec_size = size*rec_scale;
173  long ds = rec_size.volume();
174 
175  long memreq1 = 2*size.volume()*sizeof(float);
176  long memreq2 = (ft_size*ft_size + 5*rec_size.volume())*sizeof(float);
177 
178 // if ( verbose & VERB_PROCESS ) {
179  if ( verbose ) {
180  cout << "3D reciprocal space reconstruction:" << endl;
181  cout << "Reconstruction size: " << size << endl;
182  cout << "Scale: " << scale << endl;
183  cout << "Integration size: " << rec_size << endl;
184  cout << "Integration scale: " << rec_scale << endl;
185  cout << "Voxel size: " << voxel_size << " A" << endl;
186  cout << "Resolution: " << hi_res << " A" << endl;
187  cout << "Interpolation type: " << interp_type << endl;
188  cout << "Fourier transform size: " << ft_size << " x " << ft_size << endl;
189  cout << "Padding factor: " << pad_factor << endl;
190  cout << "Edge smooting width: " << edge_width << endl;
191  if ( marker_radius )
192  cout << "Erase markers using radius: " << marker_radius << endl;
193  if ( action )
194  cout << "CTF correction: " << action << " (wiener=" << wiener << ")" << endl;
195  cout << endl;
196  }
197 
198  if ( memreq1 > memreq2) memory_check(memreq1);
199  else memory_check(memreq2);
200 
201  Vector3<long> tile_size(128,128,1);
202  fft_plan plan = fft_setup_plan(ft_size, ft_size, 1, FFTW_FORWARD, 0);
203 
204  // Header structure for the final reconstruction
205  Bimage* prec = new Bimage(Float, TComplex, rec_size, 1);
206  prec->fourier_type(Standard);
207  prec->sampling(voxel_size, voxel_size, voxel_size);
208  prec->next = new Bimage(Float, TSimple, prec->size(), prec->images());
209  prec->next->next = new Bimage(Float, TSimple, prec->size(), prec->images());
210  prec->next->next->next = new Bimage(Float, TSimple, prec->size(), prec->images());
211  float* fom = (float *) prec->next->data_pointer();
212  float* weight = (float *) prec->next->next->data_pointer();
213  float* weight2 = (float *) prec->next->next->next->data_pointer();
214 
215  double pad_ratio(ft_size*1.0/prec->sizeX());
216 // if ( pad_ratio > 1 ) pad_ratio *= pad_ratio*0.6;
217 // if ( pad_ratio > 1 ) pad_ratio *= 0.25;
218 
219  if ( verbose )
220  cout << "Transforming and packing " << nmg << " micrographs:" << endl;
221 
222  for ( field = project->field; field; field = field->next ) {
223  for ( mg = field->mg; mg; mg = mg->next ) if ( mg->select ) {
224  p = mg_tomo_rec_prepare(mg, ft_size, thickness, edge_width,
225  marker_radius, fill_type, fill, action, wiener, tile_size, plan);
226  if ( p ) {
227  if ( verbose )
228  cout << "Packing " << p->file_name() << " (" << mg->img_num << ")" << endl;
229  prec->fspace_pack_2D(p, mg->matrix, hi_res, 0, vscale, 1/pad_ratio, interp_type);
230 // prec->fspace_pack_2D(p, mg->matrix, hi_res, 0, vscale, 1, interp_type);
231  delete p;
232  }
233  }
234  }
235 
236  fft_destroy_plan(plan);
237 
238  timer_report(ti);
239 
240  if ( verbose )
241  cout << "Weighing reconstruction" << endl;
242 
243 #ifdef HAVE_GCD
244  dispatch_apply(ds, dispatch_get_global_queue(0, 0), ^(size_t i){
245 // weight[i] += 1;
246 // fom[i] += 1;
247  if ( weight[i] > SMALLFLOAT ) {
248  weight2[i] = weight[i] - weight2[i]/weight[i];
249  prec->set(i, prec->complex(i) / weight[i]);
250  if ( weight2[i] > 1e-3 ) fom[i] /= weight2[i];
251  else if ( weight[i] > 1e-3 ) fom[i] /= weight[i];
252 // fom[i] /= weight[i];
253  } else fom[i] = 0;
254  if ( fom[i] < 0 ) fom[i] = 0;
255  });
256 #else
257 #pragma omp parallel for
258  for ( long i=0; i<ds; i++ ) {
259  if ( weight[i] > SMALLFLOAT ) {
260  weight2[i] = weight[i] - weight2[i]/weight[i];
261  prec->set(i, prec->complex(i) / weight[i]);
262  if ( weight2[i] > 1e-3 ) fom[i] /= weight2[i];
263  else if ( weight[i] > 1e-3 ) fom[i] /= weight[i];
264 // fom[i] /= weight[i];
265  } else fom[i] = 0;
266  if ( fom[i] < 0 ) fom[i] = 0;
267  }
268 #endif
269 
270  long cov(0);
271  for ( long i=0; i<ds; i++ ) if ( weight[i] > SMALLFLOAT ) cov++;
272 
273 // prec->fspace_reconstruction_stats(hi_res, 4);
274 
275  prec->image->origin(prec->size()/2);
276  prec->phase_shift_to_origin();
277  prec->image->origin(prec->size()/2);
278 
279  if ( verbose & VERB_RESULT )
280  cout << "Coverage: " << cov << " (" << cov*100.0/ds << " %)" << endl << endl;
281 
282  delete prec->next;
283  prec->next = NULL;
284 
285  timer_report(ti);
286 
287  return prec;
288 }
289 
324  double scale, Vector3<long> size, int slab_start, int slab_end,
325  double marker_radius, int fill_type, double fill, int action, double wiener)
326 {
327  double ti;
328 // if ( verbose & VERB_TIME )
329  ti = timer_start();
330 
331  long i;
332 
333  Bfield* field = project->field;
334  Bmicrograph* mg = NULL;
335  Bimage* p = NULL;
336 
338  long nfld = count_list((char *) project->field);
339  long nrec(0);
340 
341  if ( nmg < 1 ) {
342  cerr << "Error: No micrographs selected!" << endl;
343  return NULL;
344  }
345 
346  for ( field = project->field; field; field = field->next ) {
347  for ( mg = field->mg; mg; mg = mg->next ) if ( mg->select ) {
348 // if ( mg->fft.length() ) break;
349  if ( mg->fmg.length() ) break;
350  }
351  if ( mg ) break;
352  }
353 
354  if ( !mg ) {
355  cerr << "No selected micrograph with a micrograph file found!" << endl << endl;
356  return NULL;
357  }
358 
359  if ( !mg->fmg.length() ) {
360  cerr << "No file name for micrograph " << mg->id << endl << endl;
361  return NULL;
362  }
363 
364  if ( mg->matrix.determinant() < 0.9 )
366 
367  if ( mg->fmg.length() )
368  p = read_img(mg->fmg, 0, 0);
369  else {
370  error_show("Error in project_fourier_reconstruction_slab ", __FILE__, __LINE__);
371  cerr << "No micrograph image or transform file name given for micrograph " << mg->id << endl << endl;
372  return NULL;
373  }
374 
375  if ( size.volume() < 1 )
376  size = {long(p->sizeX()*scale), long(p->sizeY()*scale), long(p->sizeX()*scale/10)};
377 
378 /* if ( size.volume() <= 0 ) {
379  size[0] = (int) (p->sizeX()*scale);
380  if ( size[0] < p->sizeY()*scale ) size[0] = (int) (p->sizeY()*scale);
381  if ( size[0] < p->sizeZ()*scale ) size[0] = (int) (p->sizeZ()*scale);
382  size[2] = size[1] = size[0];
383  }
384 */
385  if ( slab_start >= size[2] ) {
386  cerr << "Error: Slab " << slab_start << " - " << slab_end << " is outside the volume limits!" << endl;
387  return NULL;
388  }
389 
390  if ( slab_start < 0 ) slab_start = 0;
391  if ( slab_end < 0 || slab_end >= size[2] ) slab_end = size[2] - 1;
392  if ( slab_end < slab_start ) slab_end = slab_start;
393 
394  long slab_thickness = slab_end - slab_start + 1;
395 
396  int pad = 2;
397  long ft_size = findNextPowerOf((int)(pad*size[0]), 2);
398  if ( ft_size < p->sizeX() ) ft_size = p->sizeX();
399 
400  delete p;
401 
402  // Header structure for the final reconstruction
403  Bimage* prec = new Bimage(Float, TComplex, size[0], size[1], slab_thickness, 1);
404  prec->sampling(mg->pixel_size[0]/scale, mg->pixel_size[0]/scale, mg->pixel_size[0]/scale);
405  prec->origin(prec->sizeX()/2, prec->sizeY()/2, size[2]/2);
406  prec->check_resolution(hi_res);
407  prec->next = new Bimage(Float, TSimple, prec->size(), prec->images());
408  float* fom = (float *) prec->next->data_pointer();
409 
410  long ds = prec->sizeX()*prec->sizeY()*prec->sizeZ();
411  float* weight = new float[ds];
412  float* weight2 = new float[ds];
413 
414 // if ( verbose & VERB_PROCESS ) {
415  if ( verbose ) {
416  cout << "3D reciprocal space reconstruction on disk:" << endl;
417  cout << "Map size: " << prec->sizeX() << " " << prec->sizeY() << " " << size[2] << endl;
418  cout << "Scale: " << scale << endl;
419  cout << "Fourier transform size: " << ft_size << " x " << ft_size << endl;
420  cout << "Slab: " << slab_start << " - " << slab_end << endl << endl;
421  }
422 
423 
424  Vector3<long> tile_size(128,128,1);
425  fft_plan plan = fft_setup_plan(ft_size, ft_size, 1, FFTW_FORWARD, 0);
426 
427  for ( field = project->field; field; field = field->next ) {
428  for ( mg = field->mg; mg; mg = mg->next ) if ( mg->select ) {
429  p = mg_tomo_rec_prepare(mg, ft_size, size[2], 5.0,
430  marker_radius, fill_type, fill, action, wiener, tile_size, plan);
431  if ( !p ) {
432  error_show("project_fourier_reconstruction_slab", __FILE__, __LINE__);
433  return NULL;
434  }
435 
436  img_pack_2D_in_recip_space_slab(p, prec, size[2], slab_start, weight, weight2,
437  hi_res, mg->matrix, scale);
438 
439  delete p;
440 
441  nrec++;
442 
443  if ( verbose & ( VERB_TIME | VERB_PROCESS ) ) {
444  cout << "Complete: "
445  << setprecision(3) << nrec*100.0/(nfld*nmg) << " %\r";
446  cout.flush();
447  }
448  }
449  }
450 
451  fft_destroy_plan(plan);
452 
453  if ( nrec < 1 ) {
454  delete prec;
455  error_show("Error in project_fourier_reconstruction_slab: No micrographs used in this reconstruction!", __FILE__, __LINE__);
456  return NULL;
457  }
458 
459  if ( verbose & VERB_TIME )
460  timer_report(ti);
461 
462  if ( verbose )
463  cout << "Weighing reconstruction." << endl;
464 
465  long cov(0);
466 
467 #ifdef HAVE_GCD
468  dispatch_apply(ds, dispatch_get_global_queue(0, 0), ^(size_t i){
469  if ( weight[i] > SMALLFLOAT ) {
470  prec->set(i, prec->complex(i) / weight[i]);
471  weight2[i] = weight[i]*weight[i] - weight2[i]; // Unbiased weighting
472  if ( weight2[i] > 0.001 ) {
473  double rd2 = (prec->complex(i)).power();
474  fom[i] = (fom[i] - weight[i]*rd2)/weight2[i]; // Noise variance estimator with noise reduction
475  fom[i] = rd2/fom[i] - 1; // SNR
476  } else fom[i] = 0;
477  }
478  });
479 #else
480 #pragma omp parallel for
481  for ( i=0; i<ds; i++ ) {
482  if ( weight[i] > SMALLFLOAT ) {
483  prec->set(i, prec->complex(i) / weight[i]);
484  weight2[i] = weight[i]*weight[i] - weight2[i]; // Unbiased weighting
485  if ( weight2[i] > 0.001 ) {
486  double rd2 = (prec->complex(i)).power();
487  fom[i] = (fom[i] - weight[i]*rd2)/weight2[i]; // Noise variance estimator with noise reduction
488  fom[i] = rd2/fom[i] - 1; // SNR
489  } else fom[i] = 0;
490  }
491  }
492 #endif
493 
494  for ( i=0; i<ds; i++ ) if ( weight[i] > SMALLFLOAT ) cov++;
495 
496  delete[] weight;
497  delete[] weight2;
498 
499  img_phase_shift_slab_to_origin(prec, size[2], slab_start);
500 
501  long vol = ds;
502 
503  if ( verbose & VERB_RESULT ) {
504  cout << "Coverage:" << endl;
505  cout << "Total number of voxels: " << vol << endl;
506  cout << "Total coverage: " << cov << endl;
507  cout << "Percentage coverage: " << cov*100.0/vol << " %" << endl;
508  }
509 
510 // if ( verbose & VERB_TIME )
511  timer_report(ti);
512 
513  return prec;
514 }
515 
516 
517 
543  Vector3<long> recsize, double resolution, int interp_type,
544  long ft_size, Vector3<double> scale, fft_plan planp, fft_plan planr,
545  int ctf_action, double wiener, Bsymmetry* sym,
546  Bstring& partbase, Bstring& partpath, Bstring& partext)
547 {
548  Bstring filename("part");
549 
550  // Setting the base file name
551  if ( partbase.length() ) filename = partbase + Bstring(recpart->id, "_part%03d.");
552  else if ( recpart->fpart.length() ) filename = recpart->fpart.pre_rev('.') + "_r.";
553 // else filename = rec->frec.pre_rev('.') + Bstring(recpart->id, "_part%03d.");
554 
555  // Setting the extension
556  if ( partext.length() ) filename += partext;
557  else if ( recpart->fpart.length() ) filename += recpart->fpart.post_rev('.');
558 // else filename += rec->frec.post_rev('.');
559 
560  // Setting the path
561  if ( partpath.length() ) {
562  if ( filename.contains("/") ) filename = filename.post_rev('/');
563  filename = partpath + filename;
564  }
565 
566  if ( verbose & VERB_FULL )
567  cout << "Reconstructing particle " << recpart->id << endl;
568 
569  Bfield* field;
570  Bmicrograph* mg;
571  Bparticle* part;
572  Bparticle* partlist = NULL;
573 
574  for ( field = project->field; field; field = field->next ) if ( field->select ) {
575  for ( mg = field->mg; mg; mg = mg->next ) if ( mg->select ) {
576  for ( part = mg->part; part; part = part->next )
577  if ( recpart->id == part->id ) break;
578  if ( part ) {
579  particle_copy(&partlist, part);
580  }
581  }
582  }
583 
584  Bimage* prec = particle_reconstruct(partlist, sym, 0,
585  resolution, scale, recsize, ft_size, planp,
586  interp_type, ctf_action, wiener, 0, 0, 0);
587 
588  long nr = prec->image->FOM();
589 
591 
592  prec->phase_shift_to_center();
593  prec->origin(prec->size()/2);
594  prec->fft_back(planr);
595  prec->statistics();
596  prec->correct_background();
597 // prec->change_type(newdatatype);
598  write_img(filename, prec, 0);
599  recpart->fpart = filename;
600  recpart->view = View(0,0,1,0);
601  recpart->ori = prec->image->origin();
602 
603  delete prec;
604  particle_kill(partlist);
605 
606  return nr;
607 }
608 
629  double resolution, int interp_type, int pad_factor,
630  int ctf_action, double wiener, Bsymmetry* sym,
631  Bstring& partbase, Bstring& partpath, Bstring& partext)
632 {
633  if ( !partpath.empty() ) {
634  if ( partpath[-1] != '/' ) partpath += "/";
635  mkdir(partpath.c_str(), (mode_t)0755);
636  }
637 
638  if ( partext.length() < 1 ) partext = "pif";
639 
640 
641  Bfield* field;
642  Bmicrograph* mg = NULL;
643  Breconstruction* rec = NULL;
644  Bparticle* partlist = NULL;
645  Bparticle* part = NULL;
646 
647  for ( rec = project->rec; rec; rec = rec->next )
648  if ( rec->part ) {
649  partlist = rec->part;
650  break;
651  }
652 
653  if ( !partlist ) {
654  cerr << "No particle list found!" << endl << endl;
655  return -1;
656  }
657 
658  for ( field = project->field; field; field = field->next ) {
659  for ( mg = field->mg; mg; mg = mg->next )
660  if ( mg ) break;
661  if ( mg ) break;
662  }
663 
664  if ( !mg ) {
665  cerr << "No micrographs found!" << endl << endl;
666  return -1;
667  }
668 
669  if ( !mg->part ) {
670  cerr << "No micrograph particles found!" << endl << endl;
671  return -1;
672  }
673 
674 // Vector3<long> boxsize(mg->box_size);
675 
676  rec->box_size = mg->box_size.max(mg->box_size[0]);
677 // Vector3<long> recsize(boxsize);
678 // recsize[2] = recsize[1] = recsize[0];
679 
680  rec->voxel_size = mg->pixel_size;
681  rec->voxel_size[1] = rec->voxel_size[2] = rec->voxel_size[0];
682 
683  Vector3<double> scale(rec->box_size[0]*1.0/mg->box_size[0],
684  rec->box_size[1]*1.0/mg->box_size[1],
685  rec->box_size[0]*1.0/mg->box_size[0]);
686 
687  long ft_size = part_ft_size(mg->box_size[0], scale[0], pad_factor);
688 
689  fft_plan planp = fft_setup_plan(ft_size, ft_size, 1, FFTW_FORWARD, 1);
690  fft_plan planr = fft_setup_plan(rec->box_size, FFTW_BACKWARD, 1);
691 
692  if ( verbose ) {
693  cout << "Calculating individual particle reconstructions:" << endl;
694  cout << "Reconstruction: " << rec->id << endl;
695  cout << "Particle size: " << mg->box_size << endl;
696  cout << "Reconstruction size: " << rec->box_size << endl;
697  cout << "Fourier transform size: " << ft_size << " x " << ft_size << endl;
698  cout << "Padding factor: " << pad_factor << endl;
699  cout << "Reconstruction resolution: " << resolution << " A" << endl;
700  cout << "CTF application type: " << ctf_action << endl;
701  cout << endl;
702  }
703 
704  long i, npart = particle_count(partlist);
705 
706  Bparticle** partarr = new Bparticle*[npart];
707 
708  for ( i=0, part = partlist; part; part = part->next, i++ ) partarr[i] = part;
709 
710  if ( verbose & VERB_RESULT )
711  cout << "Part\t#" << endl;
712 
713 #ifdef HAVE_GCD
714  dispatch_queue_t myq = dispatch_queue_create(NULL, NULL);
715  dispatch_apply(npart, dispatch_get_global_queue(0, 0), ^(size_t i){
716  long nr = particle_tomo_reconstruct(project, partarr[i], rec->box_size,
717  resolution, interp_type, ft_size, scale, planp, planr,
718  ctf_action, wiener, sym, partbase, partpath, partext);
719  dispatch_sync(myq, ^{
720  if ( verbose & VERB_RESULT )
721  cout << partarr[i]->id << tab << fixed << nr << endl << flush;
722  });
723  });
724 #else
725 #pragma omp parallel for
726  for ( i=0; i<npart; i++ ) {
727  long nr = particle_tomo_reconstruct(project, partarr[i], rec->box_size,
728  resolution, interp_type, ft_size, scale, planp, planr,
729  ctf_action, wiener, sym, partbase, partpath, partext);
730  #pragma omp critical
731  {
732  if ( verbose & VERB_RESULT ) {
733  cout << partarr[i]->id << tab << fixed << nr << endl << flush;
734  }
735  }
736  }
737 #endif
738 
739  delete[] partarr;
740 
741  fft_destroy_plan(planp);
742  fft_destroy_plan(planr);
743 
744  return 0;
745 }
746 
747 
759 {
760  double ti;
761 // if ( verbose & VERB_TIME )
762  ti = timer_start();
763 
764  long i, j, z;
765  long slice_size = p->sizeX()*p->sizeY()*p->channels();
766  Vector3<double> extorigin;
767  Vector3<long> extsize(p->sizeX(), p->sizeY(), 1);
768  Bimage* ps = NULL;
769 
770  if ( verbose )
771  cout << "Backtransforming reconstruction slices." << endl << endl;
772 
773  for ( z=0; z<p->sizeZ(); z++ ) {
774  if ( verbose & VERB_FULL )
775  cout << "Shifting and backtransforming slice " << z << endl;
776  extorigin[2] = z;
777  ps = p->extract(0, extorigin, extsize);
778  ps->fft(FFTW_BACKWARD, 1);
779  for ( i=z*slice_size, j=0; j<slice_size; i++, j++ ) p->set(i, (*ps)[j]);
780  delete ps;
781  }
782 
783 // if ( verbose & VERB_TIME )
784  timer_report(ti);
785 
786  return 0;
787 }
788 
810  long zsize, long slab_start, float* weight, float* weight2,
811  double hi_res, Matrix3 mat, double scale)
812 {
813  if ( !p->data_pointer() ) return 0;
814 
815  prec->check_resolution(hi_res);
816 
817  long i, j, x, y;
818  long ix, iy, iz, izs, zs;
819  double w, d2;
820  Vector3<double> m, d, iv;
821 
822  int pad = 2;
823  long ft_size = p->sizeX();
824  Bimage* pt;
825 
826  // Do transformation if not already done
827  if ( p->compound_type() == TSimple ) {
828  ft_size = findNextPowerOf((int)(pad*prec->sizeX()), 2);
829  if ( ft_size < p->sizeX() ) ft_size = p->sizeX();
830  pt = p->pad_copy(ft_size, FILL_AVERAGE, p->average());
831  pt->fft();
832  pt->phase_shift_to_origin();
833  } else pt = p->copy();
834 
835  Vector3<double> vscale(prec->sizeX(), prec->sizeY(), zsize);
836  vscale /= scale*ft_size;
837  // mat = mat.transpose(); Handedness
838  mat = vscale * mat; // Matrix includes scaling going from image to map
839 
840  if ( verbose )
841  cout << "Packing " << p->file_name() << endl;
842 
843  if ( verbose & VERB_FULL ) {
844  cout << "Resolution limit: " << hi_res << " A" << endl;
845  cout << "Slab start: " << slab_start << endl;
846  cout << "Fourier transform size: " << ft_size << endl;
847  cout << "Transformation matrix:" << endl;
848  cout << mat << endl;
849  cout << endl;
850  }
851 
852  float* fom = (float *) prec->next->data_pointer();
853 
854  double max_rad = pt->sampling(0)[0]/hi_res;
855  if ( max_rad > 0.5 ) max_rad = 0.5;
856  max_rad *= pt->sizeX();
857  double max_rad_sq = max_rad*max_rad; // Maximum radius squared on ft scale
858  double xmin = floor(-max_rad);
859  double xmax = -xmin;
860  double ymin = floor(-max_rad);
861  double ymax = -ymin;
862 
863 // cout << "sam=" << pt->sampling(0)[0] << " hi_res=" << hi_res << " max_rad=" << max_rad << endl;
864 
865  for ( iv[1]=ymin; iv[1]<=ymax; iv[1]+=1 ) {
866  y = (long) iv[1];
867  if ( y < 0 ) y += (long)p->sizeY();
868  for ( iv[0]=xmin; iv[0]<=xmax; iv[0]+=1 ) {
869  x = (long) iv[0];
870  if ( x < 0 ) x += (long)p->sizeX();
871  d2 = iv.length2();
872  if ( d2 < max_rad_sq ) { // Nearest neighbour
873  m = mat * iv;
874  iz = izs = (long) floor(m[2] + 0.5); // Nearest neighbour
875  if ( izs < 0 ) izs += zsize;
876  zs = izs - slab_start;
877  if ( zs >= 0 && zs < prec->sizeZ() ) {
878  ix = (long) floor(m[0] + 0.5);
879  iy = (long) floor(m[1] + 0.5);
880  d[0] = ix - m[0];
881  d[1] = iy - m[1];
882  d[2] = iz - m[2];
883 // d /= vscale;
884  w = 1 - d.length();
885  if ( w > 1e-6 ) {
886  if ( ix < 0 ) ix += prec->sizeX();
887  if ( iy < 0 ) iy += prec->sizeY();
888  i = y*pt->sizeX() + x;
889  j = (zs*prec->sizeY() + iy)*prec->sizeX() + ix;
890 // prec->set(j, prec->complex(j) + pt->complex(i) * w);
891  prec->add(j, pt->complex(i) * w);
892  fom[j] += w*(pt->complex(i)).power();
893  weight[j] += w;
894  weight2[j] += w*w;
895  }
896  }
897  }
898  }
899  }
900 
901  delete pt;
902 
903  return 0;
904 }
905 
918 int img_phase_shift_slab_to_origin(Bimage* p, int zsize, int slab_start)
919 {
920  if ( p->compound_type() != TComplex ) return -1;
921 
922  long i, x, y, z, zs;
923  long h, k, l;
924  double skl, sl, phi;
925  Vector3<double> shift((p->sizeX()/2)*1.0/p->sizeX(), (p->sizeY()/2)*1.0/p->sizeY(), (zsize/2)*1.0/zsize);
926  Vector3<double> half((p->sizeX() - 1)/2, (p->sizeY() - 1)/2, (zsize - 1)/2);
927  Complex<double> temp;
928 
929  if ( verbose & VERB_PROCESS )
930  cout << "Translate slice within unit cell to phase origin by " << shift << endl << endl;
931 
932  for ( i=zs=0, z=slab_start; zs<p->sizeZ(); zs++, z++ ) {
933  l = z;
934  if ( l > half[2] ) l -= zsize;
935  sl = l*shift[2];
936  for ( y=0; y<p->sizeY(); y++ ) {
937  k = y;
938  if ( k > half[1] ) k -= p->sizeY();
939  skl = sl + k*shift[1];
940  for ( x=0; x<p->sizeX(); x++, i++ ) {
941  h = x;
942  if ( h > half[0] ) h -= p->sizeX();
943  phi = MIN2PI*(h*shift[0] + skl);
944  temp = Complex<double>(cos(phi), sin(phi));
945  p->set(i, (p->complex(i))*temp);
946  }
947  }
948  }
949 
950  return 0;
951 }
952 
964 int img_clear_extraneous_areas(Bimage* p, double tilt_axis, double tilt_angle, long thickness)
965 {
966  if ( p->sizeZ() > 1 ) return -1;
967  if ( p->images() > 1 ) return -1;
968 
969  long i, x, y, c;
970 
972 
973  if ( verbose & VERB_FULL )
974  cout << "Clearing extraneous areas from micrograph " << p->file_name() << endl;
975 
976  Vector3<double> v(-sin(tilt_axis), cos(tilt_axis), 0);
977  double a, b, d = 1e30;
978  if ( v[0] && d > fabs(p->sizeX()*0.5/v[0]) ) d = fabs(p->sizeX()*0.5/v[0]);
979  if ( v[1] && d > fabs(p->sizeY()*0.5/v[1]) ) d = fabs(p->sizeY()*0.5/v[1]);
980 
981  a = d*cos(tilt_angle) + fabs(0.5*thickness*sin(tilt_angle));
982 
983  for ( i=y=0; y<p->sizeY(); y++ ) {
984  for ( x=0; x<p->sizeX(); x++ ) {
985  b = fabs(v[0]*(x - p->image->origin()[0]) + v[1]*(y - p->image->origin()[1]));
986  for ( c=0; c<p->channels(); c++, i++ )
987  if ( b > a ) p->set(i, p->background(long(0)));
988  }
989  }
990 
991  return 0;
992 }
993 
1012 int mg_fft_write(Bproject* project, Vector3<int> size, double scale, int pad_factor,
1013  DataType datatype, double marker_radius, int fill_type, double fill)
1014 {
1015  if ( pad_factor < 0 ) pad_factor = 0; // 0 indicates using original size
1016  if ( pad_factor > 8 ) pad_factor = 8;
1017 
1018  if ( datatype < Short ) datatype = Float;
1019 
1020  int n, nsel(0);
1021 
1022  Bfield* field = project->field;
1023  Bmicrograph* mg = field->mg;
1024  Bmarker* mark = NULL;
1025  Bimage* p = read_img(mg->fmg, 0, 0);
1026 
1027  if ( !mg->fmg.length() ) {
1028  error_show("Error in mg_fft_write", __FILE__, __LINE__);
1029  cerr << "No micrograph image file name given for micrograph " << mg->id << endl << endl;
1030  return -1;
1031  }
1032 
1033  if ( size[2] < 1 ) size[2] = p->sizeX();
1034  if ( size.volume() < 1 ) size = Vector3<int>(p->size());
1035  if ( size[2] > p->sizeX() ) size[2] = p->sizeX();
1036  if ( size[2] > p->sizeY() ) size[2] = p->sizeY();
1037 
1038  long ft_size = findNextPowerOf((int)(pad_factor*size[0]*scale), 2);
1039  if ( ft_size < p->sizeX() ) ft_size = p->sizeX();
1040  if ( ft_size < p->sizeY() ) ft_size = p->sizeY();
1041 
1042  delete p;
1043 
1044  if ( verbose & VERB_PROCESS ) {
1045  cout << "Transforming micrographs:" << endl;
1046  cout << "Reconstruction size: " << size << endl;
1047  cout << "Reconstruction scale: " << scale << endl;
1048  cout << "Fourier transform size: " << ft_size << " x " << ft_size << endl << endl;
1049  }
1050 
1051  for ( n=0, mg=field->mg; mg; mg=mg->next, n++ ) if ( mg->select ) {
1052  if ( verbose )
1053  cout << "Reading image " << mg->img_num << " (micrograph " << mg->id << ")" << endl;
1054  if ( ( p = read_img(mg->fmg, 1, mg->img_num) ) == NULL ) {
1055  error_show("mg_fft_write", __FILE__, __LINE__);
1056  return -1;
1057  }
1058 
1059  p->image->origin(mg->origin);
1060 
1061  p->calculate_background();
1062  if ( size[2] < p->sizeX() && size[2] < p->sizeY() )
1063  img_clear_extraneous_areas(p, mg->tilt_axis, mg->tilt_angle, size[2]);
1064 
1065  if ( marker_radius > 0 ) {
1066  if ( marker_radius < mg->mark_radius ) marker_radius = 1.5*mg->mark_radius;
1067  p->statistics();
1068  for ( mark = mg->mark; mark; mark = mark->next )
1069  p->sphere(mark->loc, marker_radius, 2, fill_type, fill);
1070  }
1071 
1072  p->pad(ft_size, fill_type, fill);
1073 
1074  p->fft();
1075 
1076  p->phase_shift_to_origin();
1077 
1078  p->change_type(datatype);
1079 
1080  mg->fft = mg->fmg;
1081  if ( mg->fft.contains("/") ) mg->fft = mg->fft.post_rev('/');
1082  mg->fft = mg->fft.pre_rev('.') + "_ft" + Bstring(n, "%04d") + ".sup";
1083  write_img(mg->fft, p, 0);
1084 // mg->img_num = 0;
1085 
1086  delete p;
1087 
1088  nsel++;
1089  }
1090 
1091  return nsel;
1092 }
1093 
1107 Bimage* img_extract_ytile(Bstring* file_list, int ystart, int ysize)
1108 {
1109  Bimage* p = NULL;
1110  Bimage* pmap = NULL;
1111  int err(0);
1112  long i(0), j(0), y, z, yt, zt;
1113  Bstring* thisfile = NULL;
1114 
1115  if ( verbose )
1116 
1117  // First read the file headers to identify problems
1118  for ( thisfile = file_list; thisfile; thisfile = thisfile->next ) {
1119  p = read_img(*thisfile, 0, -1);
1120  if ( p != NULL ) {
1121  if ( verbose & VERB_DEBUG )
1122  cout << p->file_name() << ": " <<
1123  p->sizeX() << " " << p->sizeY() << " " << p->sizeZ() << " " << p->images() << " " << p->channels() << endl;
1124  if ( p->images() > 1 ) {
1125  cerr << p->file_name() << " must be a single image file!" << endl;
1126  err += -1;
1127  }
1128  if ( i == 0 ) {
1129  pmap = p;
1130  } else {
1131 // err += img_compatibility(pmap, p);
1132  if ( !p->compatible(pmap) ) err += -1;
1133  delete p;
1134  }
1135  i += p->sizeZ();
1136  } else err += -1;
1137  }
1138 
1139  if ( err < 0 || i == 0 ) {
1140  cerr << "Files not concatenated! Number of errors: " << -err << endl;
1141  return NULL;
1142  }
1143 
1144  pmap->sizeZ(i); // Only z-slabs!
1145 
1146 // Bstring filename = p->file_name().pre_rev('.') + ".tiles";
1147  Bstring filename("y.tiles");
1148  ofstream fd(filename.c_str());
1149  if ( fd.fail() ) {
1150  cerr << "Error: Tile parameter file %s not opened" << filename << endl;
1151  return NULL;
1152  }
1153  fd << pmap->sizeX() << " " << pmap->sizeY() << " " << pmap->sizeZ() << endl;
1154  fd << "0 0 0" << endl;
1155  y = ystart%ysize;
1156  if ( y ) fd << "0 0 0" << endl;;
1157  for ( ; y<pmap->sizeY(); y+=ysize ) fd << "0 " << y << " 0" << endl;
1158  fd.close();
1159 
1160  long elementsize = pmap->channels()*pmap->data_type_size();
1161  long linesize = elementsize*pmap->sizeX();
1162 
1163  if ( ystart + ysize > pmap->sizeY() ) ysize = pmap->sizeY() - ystart;
1164 
1165  pmap->sizeY(ysize);
1166  pmap->data_alloc();
1167  unsigned char* tile = pmap->data_pointer();
1168  unsigned char* data;
1169 
1170  if ( verbose ) {
1171  cout << "Extracting y tile from " << ystart << " to " << ystart+ysize-1 << endl;
1172  cout << "Tile size: " << pmap->size() << endl;
1173  cout << "Tile parameter file: " << filename << endl;
1174  }
1175 
1176  for ( zt=0, thisfile = file_list; thisfile; thisfile = thisfile->next ) {
1177  p = read_img(*thisfile, 1, 0);
1178  data = p->data_pointer();
1179  for ( z=0; z<p->sizeZ() && zt<pmap->sizeZ(); z++, zt++ ) {
1180  for ( y=ystart, yt=0; y<p->sizeY() && yt<pmap->sizeY(); y++, yt++ ) {
1181  i = (z*p->sizeY() + y)*linesize;
1182  j = (zt*pmap->sizeY() + yt)*linesize;
1183  memcpy(tile+j, data+i, linesize);
1184  }
1185  }
1186  delete p;
1187  }
1188 
1189  return pmap;
1190 }
1191 
1209  DataType datatype, double avg, double std, double cutmin, double cutmax)
1210 {
1211  Bimage* p = NULL;
1212  Bimage* pmap = NULL;
1213  int err(0);
1214  long i(0), j(0);
1215  Bstring* thisfile = NULL;
1216  Bstring tempfile("temp.raw");
1217 
1218  // First read the file headers to identify problems
1219  for ( thisfile = file_list; thisfile; thisfile = thisfile->next ) {
1220  p = read_img(*thisfile, 0, -1);
1221  if ( p != NULL ) {
1222  if ( verbose & VERB_DEBUG )
1223  cout << p->file_name() << ": " <<
1224  p->sizeX() << " " << p->sizeY() << " " << p->sizeZ() << " " << p->images() << " " << p->channels() << endl;
1225  if ( p->images() > 1 ) {
1226  cerr << p->file_name() << " must be a single image file!" << endl;
1227  err += -1;
1228  }
1229  if ( i == 0 ) {
1230  pmap = p;
1231  } else {
1232 // err += img_compatibility(pmap, p);
1233  if ( !p->compatible(pmap) ) err += -1;
1234  delete p;
1235  }
1236  i += p->sizeZ();
1237  j++;
1238  } else err += -1;
1239  }
1240 
1241  if ( err < 0 || j == 0 ) {
1242  cerr << "Files not concatenated! Number of errors: " << -err << endl;
1243  return NULL;
1244  }
1245 
1246  pmap->sizeZ(i);
1247 
1248  fstream* ftemp = new fstream(tempfile.c_str());
1249  if ( ftemp->fail() ) {
1250  cerr << "Error: " << tempfile << " not opened!" << endl << endl;
1251  return NULL;
1252  }
1253 
1254  long elementsize = pmap->channels()*pmap->data_type_size();
1255  long datasize(0);
1256 
1257  for ( thisfile = file_list; thisfile; thisfile = thisfile->next ) {
1258  p = read_img(*thisfile, 1, -1);
1259  if ( !p ) {
1260  cerr << "Error: Image " << *thisfile << " not read!" << endl;
1261  bexit(-1);
1262  } else {
1263  datasize = p->sizeX()*p->sizeY()*p->sizeZ()*p->images();
1264  ftemp->write((char *)p->data_pointer(), elementsize*datasize);
1265  }
1266  delete p;
1267  }
1268 
1269  img_backtransform_z_lines(ftemp, pmap);
1270 
1271  // Write the final real space map header
1272  pmap->file_name(recfile.str());
1273  if ( datatype == Unknown_Type ) pmap->data_type(Float);
1274  else pmap->data_type(datatype);
1275 
1276  img_write_data_block_with_type(ftemp, pmap, avg, std, cutmin, cutmax);
1277 
1278  ftemp->close();
1279  delete ftemp;
1280 
1281  remove(tempfile.c_str());
1282 
1283  return pmap;
1284 }
1285 
1287 {
1288  long i, x, z;
1289  long slice_size = p->sizeX()*p->sizeY();
1290  long zsize = p->sizeZ()*sizeof(Complex<float>);
1291  Complex<float>* zline = new Complex<float>[zsize];
1292 
1293  if ( verbose & VERB_PROCESS )
1294  if ( y%10 == 0 ) cout << "Backtransforming z lines for y=" << y << endl;
1295  for ( x=0; x<p->sizeX(); x++ ) {
1296  for ( z=0, i=y*p->sizeX() + x; z<p->sizeZ(); z++, i+=slice_size ) zline[z] = p->complex(i);
1297  fftw(plan, zline);
1298  for ( z=0, i=y*p->sizeX() + x; z<p->sizeZ(); z++, i+=slice_size ) p->set(i, zline[z]);
1299  }
1300 
1301  delete[] zline;
1302 
1303  return 0;
1304 }
1305 
1306 int img_backtransform_one_y_plane(fstream* ftemp, Bimage* p, fft_plan plan,
1307  int y, Complex<float>* zline, Complex<float>* cfdata)
1308 {
1309  long i, x, z, offset;
1310  long cfsize = sizeof(Complex<float>);
1311  long planesize = p->sizeX()*p->sizeZ();
1312  double v;
1313 
1314  if ( verbose & VERB_PROCESS )
1315  if ( y%10 == 0 ) cout << "Backtransforming z lines for y=" << y << endl;
1316  for ( z=0; z<p->sizeZ(); z++ ) {
1317  offset = (z*p->sizeY() + y)*p->sizeX()*cfsize;
1318 // fseek(ftemp, offset, SEEK_SET);
1319 // fread(&cfdata[z*p->sizeX()], cfsize, p->sizeX(), ftemp);
1320  ftemp->seekg(offset, ios::beg);
1321  ftemp->read((char *)&cfdata[z*p->sizeX()], cfsize*p->sizeX());
1322  }
1323  for ( x=0; x<p->sizeX(); x++ ) {
1324  for ( z=0; z<p->sizeZ(); z++ ) zline[z] = cfdata[z*p->sizeX() + x];
1325  fftw(plan, zline);
1326  for ( z=0; z<p->sizeZ(); z++ ) cfdata[z*p->sizeX() + x] = zline[z];
1327  }
1328  for ( z=0; z<p->sizeZ(); z++ ) {
1329  offset = (z*p->sizeY() + y)*p->sizeX()*cfsize;
1330 // fseek(ftemp, offset, SEEK_SET);
1331 // fwrite(&cfdata[z*p->sizeX()], cfsize, p->sizeX(), ftemp);
1332  ftemp->seekg(offset, ios::beg);
1333  ftemp->write((char *)&cfdata[z*p->sizeX()], cfsize*p->sizeX());
1334  }
1335 
1336  for ( i=0; i<planesize; i++ ) {
1337  v = cfdata[i].real();
1338  if ( p->minimum() > v ) p->minimum(v);
1339  if ( p->maximum() < v ) p->maximum(v);
1340  p->average(p->average() + v);
1341  p->standard_deviation(p->standard_deviation() + v*v);
1342  }
1343 
1344  return 0;
1345 }
1346 
1358 {
1359  fft_plan plan = fft_setup_plan(p->sizeZ(), 1, 1, FFTW_FORWARD, 1);
1360 
1361 #ifdef HAVE_GCD
1362  dispatch_apply(p->sizeY(), dispatch_get_global_queue(0, 0), ^(size_t y){
1363  img_backtransform_one_y_plane(p, plan, y);
1364  });
1365 #else
1366 #pragma omp parallel for
1367  for ( long y=0; y<p->sizeY(); y++ )
1368  img_backtransform_one_y_plane(p, plan, y);
1369 #endif
1370 
1371  fft_destroy_plan(plan);
1372 
1373  p->complex_to_real();
1374  p->statistics();
1375 
1376  if ( verbose ) {
1377  cout << "Reconstruction statistics:" << endl;
1378  cout << "Min and max: " << p->minimum() << " " << p->maximum() << endl;
1379  cout << "Avg and std: " << p->average() << " " << p->standard_deviation() << endl << endl;
1380  }
1381 
1382  return 0;
1383 }
1384 
1385 int img_backtransform_z_lines(fstream* ftemp, Bimage* p)
1386 {
1387  int y;
1388  long datasize = p->sizeX()*p->sizeY()*p->sizeZ()*p->images();
1389 
1390  fft_plan plan = fft_setup_plan(p->sizeZ(), 1, 1, FFTW_FORWARD, 1);
1391  Complex<float>* zline = new Complex<float>[p->sizeZ()];
1392  Complex<float>* cfdata = new Complex<float>[p->sizeX()*p->sizeZ()];
1393 
1394  p->minimum(1e37);
1395  p->maximum(-1e37);
1396  p->average(0);
1397  p->standard_deviation(0);
1398 
1399  for ( y=0; y<p->sizeY(); y++ )
1400  img_backtransform_one_y_plane(ftemp, p, plan, y, zline, cfdata);
1401 
1402  fft_destroy_plan(plan);
1403  delete[] zline;
1404  delete[] cfdata;
1405 
1406  p->average(p->average() / datasize);
1407  p->standard_deviation(p->standard_deviation()/datasize - p->average()*p->average());
1408  if ( p->standard_deviation() > 0 ) p->standard_deviation(sqrt(p->standard_deviation()));
1409  else p->standard_deviation(0);
1410 
1411  if ( verbose ) {
1412  cout << "Reconstruction statistics:" << endl;
1413  cout << "Min and max: " << p->minimum() << " " << p->maximum() << endl;
1414  cout << "Avg and std: " << p->average() << " " << p->standard_deviation() << endl << endl;
1415  }
1416 
1417  return 0;
1418 }
1419 
1420 
1431 {
1432  long size;
1433 
1434  switch ( type ) {
1435  case Bit:
1436  case UCharacter: case SCharacter: size = sizeof(char); break;
1437  case UShort: case Short: size = sizeof(short); break;
1438  case UInteger: case Integer: size = sizeof(int); break;
1439  case ULong: case Long: size = sizeof(long); break;
1440  case Float: size = sizeof(float); break;
1441  case Double: size = sizeof(double); break;
1442  default: size = 0;
1443  }
1444 
1445  return size;
1446 }
1447 
1448 
1460 int set_value_with_datatype(char* ptr, double value, DataType datatype)
1461 {
1462  int typesize = gettypesize(datatype);
1463  if ( typesize < 1 ) return -1;
1464 
1465  switch ( datatype ) {
1466  case UCharacter: *ptr = (unsigned char) value; break;
1467  case SCharacter: {
1468  signed char cval = (signed char) value;
1469  memcpy(ptr, &cval, typesize); break;
1470  }
1471  case UShort: {
1472  unsigned short uval = (unsigned short) value;
1473  memcpy(ptr, &uval, typesize); break;
1474  }
1475  case Short: {
1476  short sval = (short) value;
1477  memcpy(ptr, &sval, typesize); break;
1478  }
1479  case UInteger: {
1480  unsigned int ival = (unsigned int) value;
1481  memcpy(ptr, &ival, typesize); break;
1482  }
1483  case Integer: {
1484  int ival = (int) value;
1485  memcpy(ptr, &ival, typesize); break;
1486  }
1487  case ULong: {
1488  long ival = (long) value;
1489  memcpy(ptr, &ival, typesize); break;
1490  }
1491  case Long: {
1492  long ival = (long) value;
1493  memcpy(ptr, &ival, typesize); break;
1494  }
1495  case Float: {
1496  float fval = (float) value;
1497  memcpy(ptr, &fval, typesize); break;
1498  }
1499  case Double: {
1500  double dval = (double) value;
1501  memcpy(ptr, &dval, typesize); break;
1502  }
1503  default: ;
1504  }
1505 
1506  return 0;
1507 }
1508 
1509 /*
1510 @brief Rescales and writes a raw disk-based data block to a specific data type.
1511 @param fstream* ftemp file pointer to the complex raw data block.
1512 @param *p image header information.
1513 @return int 0.
1514 
1515  Each slice is read from the raw data block, rescaled, and written
1516  into the new file.
1517  Limitation: This function can only be used for an image file format
1518  with a contiguous data block (CCP4, MRC, PIF, etc.).
1519 
1520 **/
1521 int img_write_data_block_with_type(fstream* ftemp, Bimage* p,
1522  double avg, double std, double cutmin, double cutmax)
1523 {
1524 
1525  long i, z, offset;
1526  long elementsize = p->data_type_size();
1527  long cfsize = sizeof(Complex<float>);
1528  long slicesize = p->sizeX()*p->sizeY();
1529  double scale = 1;
1530  double shift(0);
1531 
1532  switch ( p->data_type() ) {
1533  case UCharacter: scale = 255/(p->maximum() - p->minimum());
1534  shift = -p->minimum()*scale; break;
1535  case SCharacter: scale = 255/(p->maximum() - p->minimum());
1536  shift = -128 - p->minimum()*scale; break;
1537  case UShort: scale = USHRT_MAX/(p->maximum() - p->minimum());
1538  shift = -p->minimum()*scale; break;
1539  case Short: scale = SHRT_MAX/(p->maximum() - p->minimum()); break;
1540  case Integer: scale = 2e9/(p->maximum() - p->minimum()); break;
1541  case Float:
1542  case Double: scale = 1; break;
1543  default: ;
1544  }
1545 
1546  if ( std ) {
1547  scale = std/p->standard_deviation();
1548  shift = avg - p->average()*scale;
1549  }
1550 
1551  p->minimum(scale*p->minimum() + shift);
1552  p->maximum(scale*p->maximum() + shift);
1553  p->average(scale*p->average() + shift);
1554  p->standard_deviation(p->standard_deviation() * scale);
1555 
1556  if ( cutmin >= cutmax ) {
1557  cutmin = p->minimum();
1558  cutmax = p->maximum();
1559  }
1560 
1561  p->minimum(p->data_type_min());
1562  p->maximum(p->data_type_max());
1563 
1564  if ( cutmin < p->minimum() ) cutmin = p->minimum();
1565  if ( cutmax > p->maximum() ) cutmax = p->maximum();
1566 
1567  p->minimum(cutmin);
1568  p->maximum(cutmax);
1569 
1571 
1572  write_img(p->file_name(), p, 0);
1573 
1574  double d;
1575  char* data = new char[slicesize*elementsize];
1576  Complex<float>* cfdata = new Complex<float>[slicesize];
1577 
1578  fstream fmap(p->file_name().c_str());
1579  if ( fmap.fail() ) return -1;
1580 
1581  for ( z=0; z<p->sizeZ(); z++ ) {
1582  offset = z*slicesize*cfsize;
1583  ftemp->seekg(offset, ios::beg);
1584  ftemp->read((char *)cfdata, cfsize*slicesize);
1585  for ( i=0; i<slicesize; i++ ) {
1586  d = cfdata[i].real()*scale + shift;
1587  if ( d < cutmin ) d = cutmin;
1588  if ( d > cutmax ) d = cutmax;
1589  set_value_with_datatype(data+i*elementsize, d, p->data_type());
1590  }
1591  offset = p->data_offset() + z*slicesize*elementsize;
1592  fmap.seekg(offset, ios::beg);
1593  fmap.write((char *)data, elementsize*slicesize);
1594  }
1595 
1596  fmap.close();
1597 
1598  delete[] data;
1599  delete[] cfdata;
1600 
1601  return 0;
1602 }
1603 
1618  Vector3<double> origin, double hi_res, double scale)
1619 {
1620  Bfield* field = project->field;
1621 
1622  if ( !field ) {
1623  cerr << "Error in project_missing_mask: Field-of-view not defined!" << endl;
1624  return NULL;
1625  }
1626 
1627  Bmicrograph* mg = field->mg;
1628 
1629  if ( !mg ) {
1630  cerr << "Error in project_missing_mask: Micrograph not defined!" << endl;
1631  return NULL;
1632  }
1633 
1634  Breconstruction* rec = NULL;
1635 
1636  if ( size.volume() < 10 ) {
1637  rec = project->rec;
1638  if ( !rec ) {
1639  cerr << "Error in project_missing_mask: Reconstruction and mask size not defined!" << endl;
1640  return NULL;
1641  }
1642  size = rec->box_size;
1643  }
1644 
1645  if ( size.volume() < 10 ) {
1646  cerr << "Error in project_missing_mask: Mask size not defined!" << endl;
1647  return NULL;
1648  }
1649 
1650  if ( mg->matrix.determinant() < 0.9 )
1652 
1653  // Image to pack
1654  Bimage* pimg = new Bimage(UCharacter, TSimple, size[0], size[1], 1, 1);
1655  pimg->fill(1);
1656 
1657  // Mask
1658  Bimage* pmask = new Bimage(UCharacter, TSimple, size, 1);
1659  pmask->sampling(mg->pixel_size[0]/scale, mg->pixel_size[0]/scale, mg->pixel_size[0]/scale);
1660  pmask->check_resolution(hi_res);
1661 
1662 // if ( verbose & VERB_PROCESS ) {
1663  if ( verbose ) {
1664  cout << "Generating a 3D reciprocal space mask from a tilt series:" << endl;
1665  cout << "Mask size: " << pmask->sizeX() << " " << pmask->sizeY() << " " << size[2] << endl;
1666  cout << "Origin: " << origin << endl;
1667  cout << "Resolution limit: " << hi_res << " A" << endl;
1668  cout << "Scale: " << scale << endl;
1669  }
1670 
1671  for ( mg=field->mg; mg; mg=mg->next ) if ( mg->select ) {
1672 
1673  pmask->mask_pack_plane(mg->matrix, hi_res, scale);
1674 
1675  }
1676 
1677  delete pimg;
1678 
1679  Vector3<long> shift((long) -(origin[0]+0.5), (long) -(origin[1]+0.5), (long) -(origin[2]+0.5));
1680  pmask->shift_wrap(shift);
1681 
1682  pmask->statistics();
1683 
1684  return pmask;
1685 }
1686 
img_backtransform_z_lines
int img_backtransform_z_lines(fstream *ftemp, Bimage *p)
Definition: mg_tomo_rec.cpp:1385
mg_reconstruct.h
Functions for reconstruction.
project_mg_tilt_to_matrix
int project_mg_tilt_to_matrix(Bproject *project)
Sets the matrix from the tilt and axis angles for each micrograph.
Definition: mg_tomography.cpp:2177
Bmicrograph::id
Bstring id
Definition: mg_processing.h:203
Bmicrograph
Definition: mg_processing.h:201
Breconstruction::part
Bparticle * part
Definition: mg_processing.h:307
Breconstruction::id
Bstring id
Definition: mg_processing.h:285
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:347
Bit
@ Bit
Definition: utilities.h:152
set_value_with_datatype
int set_value_with_datatype(char *ptr, double value, DataType datatype)
Set a pointer to a value with a given data type.
Definition: mg_tomo_rec.cpp:1460
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
ULong
@ ULong
Definition: utilities.h:159
Bimage::fspace_reconstruction_weigh
long fspace_reconstruction_weigh()
Weighs a reconstruction.
Definition: Bimage_reconstruct.cpp:412
UCharacter
@ UCharacter
Definition: utilities.h:153
Complex
Definition: Complex.h:23
Bparticle::view
View view
Definition: mg_processing.h:102
Complex::real
Type real()
Definition: Complex.h:101
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
Bstring
Definition: Bstring.h:29
Bimage::next
Bimage * next
Definition: Bimage.h:179
Breconstruction::voxel_size
Vector3< double > voxel_size
Definition: mg_processing.h:295
project_tomo_reconstruct_particles
long project_tomo_reconstruct_particles(Bproject *project, double resolution, int interp_type, int pad_factor, int ctf_action, double wiener, Bsymmetry *sym, Bstring &partbase, Bstring &partpath, Bstring &partext)
Reconstructs particles from a tilt series.
Definition: mg_tomo_rec.cpp:628
Bparticle
Definition: mg_processing.h:90
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:436
Bproject::field
Bfield * field
Definition: mg_processing.h:346
img_pack_2D_in_recip_space_slab
long img_pack_2D_in_recip_space_slab(Bimage *p, Bimage *prec, long zsize, long slab_start, float *weight, float *weight2, double hi_res, Matrix3 mat, double scale)
Packs a 2D Fourier transform into a 3D reciprocal space volume.
Definition: mg_tomo_rec.cpp:809
Bimage::copy
Bimage * copy()
Copies the header information and data of an image into a new image structure.
Definition: Bimage.cpp:2702
img_extract_ytile
Bimage * img_extract_ytile(Bstring *file_list, int ystart, int ysize)
Extracts a tile from the image with limits in y.
Definition: mg_tomo_rec.cpp:1107
Bmicrograph::tilt_angle
double tilt_angle
Definition: mg_processing.h:220
Vector3< long >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
Bmicrograph::img_num
int img_num
Definition: mg_processing.h:212
mg_select.h
Header file for micrograph processing.
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
Complex.h
Class for complex numbers.
TComplex
@ TComplex
Definition: Bimage.h:60
Bstring::empty
bool empty()
Definition: Bstring.h:71
Bimage::background
double background(long nn)
Definition: Bimage.h:549
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
part_ft_size
int part_ft_size(int xsize, double scale, int pad_factor)
Sets the Fourier transform size for 2D particle images for reconstruction.
Definition: mg_reconstruct.cpp:38
Breconstruction::box_size
Vector3< long > box_size
Definition: mg_processing.h:299
Bmicrograph::part
Bparticle * part
Definition: mg_processing.h:242
Bimage::data_type_size
long data_type_size() const
Returns the size of the datatype in bytes.
Definition: Bimage.cpp:1607
FILL_AVERAGE
#define FILL_AVERAGE
Definition: utilities.h:177
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
Bimage::fft_back
int fft_back()
Definition: Bimage.h:831
View
Definition: View.h:26
Bimage::shift_wrap
int shift_wrap(Vector3< double > vec)
Shifts an image.
Definition: Bimage_transform.cpp:641
Bimage::data_type_min
double data_type_min()
Get the minimum of a datatype.
Definition: Bimage.cpp:1633
Bfield
Definition: mg_processing.h:260
Bsub_image::FOM
double FOM()
Definition: Bimage.h:165
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bimage::correct_background
int correct_background(long nn, int flag)
Corrects the background for one sub-image.
Definition: Bimage_background.cpp:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bsymmetry
Definition: symmetry.h:38
Bimage::maximum
double maximum()
Definition: Bimage.h:542
count_list
long count_list(char *list)
Counts the number of items in a linked list.
Definition: linked_list.cpp:235
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:544
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
project_tomo_reconstruct
Bimage * project_tomo_reconstruct(Bproject *project, double hi_res, double scale, Vector3< long > size, int interp_type, int pad_factor, double edge_width, double marker_radius, int fill_type, double fill, int action, double wiener)
Reciprocal space reconstruction from the images in a multi-image file.
Definition: mg_tomo_rec.cpp:124
project_fourier_reconstruction_slab
Bimage * project_fourier_reconstruction_slab(Bproject *project, double hi_res, double scale, Vector3< long > size, int slab_start, int slab_end, double marker_radius, int fill_type, double fill, int action, double wiener)
Reciprocal space reconstruction from the images in a multi-image file.
Definition: mg_tomo_rec.cpp:323
gettypesize
long gettypesize(DataType type)
Get the size of a datatype.
Definition: mg_tomo_rec.cpp:1430
Standard
@ Standard
Definition: Bimage.h:42
Bmarker
Definition: marker.h:21
img_write_data_block_with_type
int img_write_data_block_with_type(fstream *ftemp, Bimage *p, double avg, double std, double cutmin, double cutmax)
Definition: mg_tomo_rec.cpp:1521
Bimage::images
long images()
Definition: Bimage.h:504
memory_check
int memory_check(long mem_required)
Checking if there is enough memory, abort if not.
Definition: utilities.cpp:255
project
Bproject * project
Definition: bshow.cpp:33
Matrix3
Definition: Matrix3.h:18
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
Short
@ Short
Definition: utilities.h:156
TSimple
@ TSimple
Definition: Bimage.h:59
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
Bmarker::next
Bmarker * next
Definition: marker.h:22
Bimage::add
void add(long j, double v)
Definition: Bimage.h:321
Bstring::str
string str()
Definition: Bstring.h:42
mg_fft_write
int mg_fft_write(Bproject *project, Vector3< int > size, double scale, int pad_factor, DataType datatype, double marker_radius, int fill_type, double fill)
Fourier transform micrographs and write to disk.
Definition: mg_tomo_rec.cpp:1012
Bparticle::fpart
Bstring fpart
Definition: mg_processing.h:92
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
fft_destroy_plan
int fft_destroy_plan(fft_plan plan)
Deallocates a plan for fast Fourier transforms.
Definition: fft.cpp:87
particle_reconstruct
Bimage * particle_reconstruct(Bparticle *partlist, Bsymmetry *sym, int sym_mode, double hi_res, Vector3< double > scale, Vector3< long > size, int ft_size, fft_plan plan, int interp_type=0, int ctf_action=0, double wiener=0.2, int twoD_flag=0, int bootstrap=0, int first=0)
Reciprocal space reconstruction from 2D particle images.
Definition: mg_reconstruct.cpp:100
SMALLFLOAT
#define SMALLFLOAT
Definition: utilities.h:46
Bimage::fft
int fft()
Definition: Bimage.h:830
project_count_mg_selected
long project_count_mg_selected(Bproject *project)
Counts all the selected micrographs in the project structure.
Definition: mg_select.cpp:367
Bimage::data_offset
long data_offset()
Definition: Bimage.h:288
Bmicrograph::origin
Vector3< double > origin
Definition: mg_processing.h:223
Bimage::fourier_type
FourierType fourier_type()
Definition: Bimage.h:380
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
VERB_RESULT
#define VERB_RESULT
Definition: utilities.h:109
img_phase_shift_slab_to_origin
int img_phase_shift_slab_to_origin(Bimage *p, int zsize, int slab_start)
Phase shifts a set of reflections to the image origin.
Definition: mg_tomo_rec.cpp:918
Bimage::data_type_max
double data_type_max()
Get the maximum of a datatype.
Definition: Bimage.cpp:1663
UShort
@ UShort
Definition: utilities.h:155
mg_ctf.h
Functions for CTF (contrast transfer function) processing.
Bmicrograph::tilt_axis
double tilt_axis
Definition: mg_processing.h:219
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:364
Bimage::average
double average(long cc, double xf, double yf, double zf, long nn, double iscale)
Averages in 3D when scaling is less than 1.
Definition: Bimage.cpp:1371
timer.h
Utilities for timing functions.
fftw
int fftw(fft_plan plan, Complex< float > *a)
Fast Fourier transforms a data array.
Definition: fft.cpp:106
Bproject
Definition: mg_processing.h:336
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:271
MIN2PI
#define MIN2PI
Definition: utilities.h:75
Bimage::mask_pack_plane
long mask_pack_plane(Matrix3 mat, double hi_res, double scale)
Packs a 2D mask into a 3D reciprocal space volume.
Definition: Bimage_mask.cpp:1244
img_backtransform_slices
int img_backtransform_slices(Bimage *p)
Backtransforms 2D slices in a 3D volume.
Definition: mg_tomo_rec.cpp:758
Bimage::complex
Complex< double > complex(long j)
Returns a complex value at the given index.
Definition: Bimage.cpp:1027
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
Bmicrograph::mark_radius
double mark_radius
Definition: mg_processing.h:238
Bmarker::loc
Vector3< float > loc
Definition: marker.h:25
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Bimage::check_resolution
void check_resolution(double &resolution)
Checks that the resolution falls within reasonable limits.
Definition: Bimage.cpp:635
Integer
@ Integer
Definition: utilities.h:158
Bimage::pad_copy
Bimage * pad_copy(long sz, int fill_type=0, double fill=0)
Pads an image to a new size with a given fill value.
Definition: Bimage_resize.cpp:388
img_erase_markers
long img_erase_markers(Bimage *p, Bmarker *mark, double marker_radius)
Erases or paints markers in an image.
Definition: mg_tomography.cpp:1236
Bmicrograph::box_size
Vector3< long > box_size
Definition: mg_processing.h:233
Long
@ Long
Definition: utilities.h:160
Breconstruction
Definition: mg_processing.h:283
fft_setup_plan
fft_plan fft_setup_plan(long x, long y, long z, fft_direction dir, int opt)
Sets up a plan for fast Fourier transforms.
Definition: fft.cpp:31
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bmicrograph::matrix
Matrix3 matrix
Definition: mg_processing.h:225
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
Bmicrograph::fmg
Bstring fmg
Definition: mg_processing.h:206
img_ttf_apply
int img_ttf_apply(Bimage *p, CTFparam ctf, int action, double wiener, Vector3< long > tile_size, double tilt, double axis, double res_lo, double res_hi)
Applies or corrects for the tilted contrast transfer function (TTF).
Definition: mg_ctf.cpp:388
Bfield::select
long select
Definition: mg_processing.h:265
img_clear_extraneous_areas
int img_clear_extraneous_areas(Bimage *p, double tilt_axis, double tilt_angle, long thickness)
Clears areas from a micrograph not used in a reconstruction.
Definition: mg_tomo_rec.cpp:964
micrograph_clear_extraneous_areas
int micrograph_clear_extraneous_areas(Bmicrograph *mg, Bimage *p, long thickness, double width)
Clears extraneous areas from a micrograph.
Definition: mg_tomography.cpp:2232
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Vector3::volume
double volume()
Definition: Vector3.h:237
mg_tomo_rec.h
Functions to do a tomographic reconstruction.
Bmicrograph::ctf
CTFparam * ctf
Definition: mg_processing.h:241
Bfield::next
Bfield * next
Definition: mg_processing.h:261
Bstring::next
Bstring * next
Definition: Bstring.h:31
Bmicrograph::mark
Bmarker * mark
Definition: mg_processing.h:245
Bimage::compatible
bool compatible(Bimage *p)
Check if this image has the same number of channels and data type as another.
Definition: Bimage.cpp:653
Bimage::fspace_pack_2D
int fspace_pack_2D(Bimage *p, Matrix3 mat, double hi_res, double lo_res, Vector3< double > scale, double part_weight=1, int interp_type=0)
Packs a 2D Fourier transform into a 3D reciprocal space volume.
Definition: Bimage_reconstruct.cpp:107
Bparticle::ori
Vector3< double > ori
Definition: mg_processing.h:101
Matrix3::determinant
double determinant()
Definition: Matrix3.h:260
Bimage::extract
Bimage * extract(long nn)
Extracts one sub-image into new image structure.
Definition: Bimage_extract.cpp:21
fft_plan
fftwf_plan fft_plan
Definition: fft.h:20
Bparticle::next
Bparticle * next
Definition: mg_processing.h:91
Bimage::fill
void fill(double v)
Definition: Bimage.h:706
linked_list.h
Header file for generalized linked list functions.
Bmicrograph::select
long select
Definition: mg_processing.h:204
mg_tomo_rec_prepare
Bimage * mg_tomo_rec_prepare(Bmicrograph *mg, int ft_size, long thickness, double edge_width, double marker_radius, int fill_type, double fill, int action, double wiener, Vector3< long > tile_size, fft_plan plan)
Definition: mg_tomo_rec.cpp:31
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
SCharacter
@ SCharacter
Definition: utilities.h:154
particle_copy
Bparticle * particle_copy(Bparticle **partlist, Bparticle *part)
Adds one particle onto a list of particle parameters.
Definition: mg_processing.cpp:614
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
verbose
int verbose
Bimage::pad
int pad(long sz, int fill_type=0, double fill=0)
Pads an image to a new size with a given fill value.
Definition: Bimage_resize.cpp:347
Bmicrograph::pixel_size
Vector3< double > pixel_size
Definition: mg_processing.h:215
img_backtransform_one_y_plane
int img_backtransform_one_y_plane(Bimage *p, fft_plan plan, int y)
Definition: mg_tomo_rec.cpp:1286
Bimage::minimum
double minimum()
Definition: Bimage.h:541
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bimage::phase_shift_to_origin
int phase_shift_to_origin()
Phase shifts a set of reflections to the image origin.
Definition: Bimage_complex.cpp:327
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
particle_kill
int particle_kill(Bparticle *part)
Frees a list of particles.
Definition: mg_processing.cpp:528
project_missing_mask
Bimage * project_missing_mask(Bproject *project, Vector3< long > size, Vector3< double > origin, double hi_res, double scale)
Creates a reciprocal space mask from the tilt series orientations.
Definition: mg_tomo_rec.cpp:1617
Double
@ Double
Definition: utilities.h:162
Vector3::length
double length()
Definition: Vector3.h:222
particle_tomo_reconstruct
long particle_tomo_reconstruct(Bproject *project, Bparticle *recpart, Vector3< long > recsize, double resolution, int interp_type, long ft_size, Vector3< double > scale, fft_plan planp, fft_plan planr, int ctf_action, double wiener, Bsymmetry *sym, Bstring &partbase, Bstring &partpath, Bstring &partext)
Reconstructs individual particles from a tilt series.
Definition: mg_tomo_rec.cpp:542
particle_count
long particle_count(Bparticle *part)
Counts the number of particles in a linked list.
Definition: mg_select.cpp:755
Float
@ Float
Definition: utilities.h:161
Bimage::channels
long channels()
Definition: Bimage.h:384
Bimage::phase_shift_to_center
int phase_shift_to_center()
Phase shifts a set of reflections to the nominal center of the image origin.
Definition: Bimage_complex.cpp:382
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:202
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:267
findNextPowerOf
int findNextPowerOf(int startNumber, int powerOf)
Finds the next greatest number that is a power of a given number.
Definition: utilities.cpp:397
UInteger
@ UInteger
Definition: utilities.h:157
Bparticle::id
int id
Definition: mg_processing.h:93
NoTransform
@ NoTransform
Definition: Bimage.h:41
Vector3::length2
double length2()
Definition: Vector3.h:221
Bimage::complex_to_real
void complex_to_real()
The real part of a complex image is written to a simple image.
Definition: Bimage_complex.cpp:50
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
Bimage::sphere
int sphere(Vector3< double > center, double radius, double width=0, int fill_type=0, double fill=0)
Fills a sphere within an image with a uniform value.
Definition: Bimage_edit.cpp:440
Bmicrograph::fft
Bstring fft
Definition: mg_processing.h:210
mg_tomography.h
Header file for single particle image processing in Bsoft.
img_backtransform_z_on_disk
Bimage * img_backtransform_z_on_disk(Bstring *file_list, Bstring &recfile, DataType datatype, double avg, double std, double cutmin, double cutmax)
The lines along the z-dimensions of a disk-based block is Fourier back-transformed.
Definition: mg_tomo_rec.cpp:1208
Breconstruction::next
Breconstruction * next
Definition: mg_processing.h:284
Bimage::rescale_to_avg_std
int rescale_to_avg_std(double nuavg, double nustd)
Rescales the image data to a given average and standard deviation.
Definition: Bimage_rescale.cpp:163
Vector3::max
Type max()
Definition: Vector3.h:175