Bsoft  2.1.0
Bernard's software package
mg_ctf.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "mg_ctf.h"
11 #include "mg_ctf_fit.h"
12 #include "ps_plot.h"
13 #include "ps_ctf_plot.h"
14 #include "mg_processing.h"
15 #include "utilities.h"
16 #include "timer.h"
17 
18 #include <sys/stat.h>
19 
20 // Declaration of global variables
21 extern int verbose; // Level of output to the screen
22 
45 Bimage* img_ctf_calculate(CTFparam cp, int action, double wiener, Vector3<long> size,
46  Vector3<double> sam, double lores, double hires)
47 {
48 // if ( cp.check_defocus() || cp.check_Cs() )
49 // cerr << "in img_ctf_calculate" << endl;
50 
51  if ( lores < 0 ) lores = 0;
52  if ( hires <= 0 ) hires = sam[0];
53  if ( lores > 0 && lores < hires ) swap(lores, hires);
54 
55  double shi(1/hires);
56  double slo = (lores > 0)? 1/lores: 0;
57 
58  Bimage* p = new Bimage(Float, TSimple, size, 1);
59  if ( sam.volume() > 0 ) p->sampling(sam);
60 
61  long i, x, y, z, sign;
62  double sx, sy, sz, s, s2, w, ctf_fac, base(0), env(0), ctf_env;
63  Vector3<double> freq_scale(1.0/p->real_size());
64  Vector3<double> h((p->size() - 1)/2);
65 
66  Bstring base_eq = cp.baseline_equation();
67  Bstring env_eq = cp.envelope_equation();
68 
69  if ( verbose & ( VERB_LABEL | VERB_PROCESS ) ) {
70  cout << "Calculating a CTF function:" << endl;
71  switch ( action ) {
72  case 1: cout << "Flipping phases" << endl; break;
73  case 2: cout << "Applying a CTF" << endl; break;
74  case 3: cout << "Correcting for the CTF" << endl; break;
75  case 4: cout << "Correcting for the CTF with an optimal Wiener filter" << endl; break;
76  case 5: cout << "Correcting for the CTF with baseline enhancement" << endl; break;
77  case 6: cout << "Correcting for the CTF with baseline2 enhancement" << endl; break;
78  case 7: cout << "Flipping phases with baseline enhancement" << endl; break;
79  case 8: cout << "Correcting for the CTF with baseline3 enhancement" << endl; break;
80 // case 9: cout << "Applying theoretical envelopes" << endl; break;
81 // case 10: cout << "Applying a CTF with theoretical envelopes" << endl; break;
82  default: break;
83  }
84  }
85  if ( verbose & VERB_PROCESS ) {
86  cp.show();
87  cout << "Resolution range: " << hires << " - ";
88  if ( lores > 0 ) cout << lores << " A" << endl;
89  else cout << "inf A" << endl;
90  if ( action == 3 || action == 5 || action == 6 )
91  cout << "Wiener factor: " << wiener << endl;
92  if ( action >= 4 ) {
93  cout << "Envelope: " << env_eq << endl;
94  cout << "Noise: " << base_eq << endl;
95  }
96  cout << endl;
97  }
98 
99  for ( i=z=0; z<p->sizeZ(); z++ ) {
100  sz = z;
101  if ( z > h[2] ) sz -= p->sizeZ();
102  sz *= freq_scale[2];
103  for ( y=0; y<p->sizeY(); y++ ) {
104  sy = y;
105  if ( y > h[1] ) sy -= p->sizeY();
106  sy *= freq_scale[1];
107  for ( x=0; x<p->sizeX(); x++, i++ ) {
108  sx = x;
109  if ( x > h[0] ) sx -= p->sizeX();
110  sx *= freq_scale[0];
111  s2 = sx*sx + sy*sy + sz*sz;
112  s = sqrt(s2);
113  if ( s >= slo && s <= shi ) {
114  ctf_fac = cp.calculate(s2, atan2(sy,sx));
115  w = 0;
116  sign = ( ctf_fac < 0 )? -1: 1;
117  if ( action > 3 ) base = cp.calc_baseline(s);
118  switch ( action ) {
119  case 1: w = sign; break; // Flip the phase
120  case 2: w = ctf_fac; break; // Apply the CTF
121  case 3:
122  w = ctf_fac/(ctf_fac*ctf_fac + wiener);
123  break; // Spider method
124  case 4:
125  if ( base > 0 ) {
126  env = cp.calc_envelope(s);
127  ctf_env = ctf_fac*env;
128  w = ctf_fac*sqrt(env)/(ctf_fac*ctf_env + base);
129  }
130  break; // Wiener filter method
131  case 5:
132  w = ctf_fac/(ctf_fac*ctf_fac*base + wiener);
133  break; // Baseline enhancement
134  case 6:
135  w = 1/(ctf_fac*base + sign*wiener);
136  break; // Baseline enhancement
137  case 7:
138  if ( base > 0 ) w = sign/sqrt(base);
139  break; // Flip phase with baseline enhancement
140  case 8:
141  env = cp.calc_envelope(s);
142  w = sign/sqrt(ctf_fac*ctf_fac*env + base);
143  break; // Baseline enhancement
144  default: break;
145  }
146  p->set(i, w);
147  }
148  }
149  }
150  }
151 
152  p->statistics();
153 
154  return p;
155 }
156 
175 {
176  Bimage* p = new Bimage(Float, TSimple, size, 1);
177  if ( sam.volume() > 0 ) p->sampling(sam);
178 
179  long i, n, x, y, z;
180  double sx, sy, sz, s2;
181  Vector3<double> freq_scale(1.0/p->real_size());
182  Vector3<double> h((p->sizeX() - 1)/2, (p->sizeY() - 1)/2, (p->sizeZ() - 1)/2);
183 
184  if ( verbose & VERB_PROCESS ) {
185  cout << "Calculating a wave aberration function:" << endl;
186  cout << "Defocus average & deviation: " << cp.defocus_average() << " A " << cp.defocus_deviation() << " A" << endl;
187  cout << "Astigmatism angle: " << cp.astigmatism_angle()*180/M_PI << " degrees" << endl;
188  cout << "Voltage: " << cp.volt() << " V" << endl;
189  cout << "Wavelength: " << cp.lambda() << " A" << endl;
190  cout << "Cs: " << cp.Cs() << " A" << endl;
191  cout << "Amplitude contrast: " << sin(cp.amp_shift()) << endl;
192  cout << endl;
193  }
194 
195  for ( i=n=0; n<p->images(); n++ ) {
196  for ( z=0; z<p->sizeZ(); z++ ) {
197  sz = z;
198  if ( z > h[2] ) sz -= p->sizeZ();
199  sz *= freq_scale[2];
200  for ( y=0; y<p->sizeY(); y++ ) {
201  sy = y;
202  if ( y > h[1] ) sy -= p->sizeY();
203  sy *= freq_scale[1];
204  for ( x=0; x<p->sizeX(); x++, i++ ) {
205  sx = x;
206  if ( x > h[0] ) sx -= p->sizeX();
207  sx *= freq_scale[0];
208  s2 = sx*sx + sy*sy + sz*sz;
209  p->set(i, cp.delta_phi(s2, atan2(sy,sx)));
210  }
211  }
212  }
213  }
214 
215  return p;
216 }
217 
237 int img_ctf_apply(Bimage* p, CTFparam em_ctf, int action, double wiener,
238  double lores, double hires)
239 {
240  if ( action < 1 || action > 10 ) return 0;
241 
242  if ( wiener < 0.01 ) wiener = 0.01;
243 
244  Bimage* pctf = img_ctf_calculate(em_ctf, action, wiener, p->size(),
245  p->sampling(0), lores, hires);
246  pctf->invert();
247 
248 // write_img("pctf.pif", pctf, 0);
249 // bexit(-1);
250 
251  int back_transform(0);
252  if ( p->fourier_type() == NoTransform ) {
253  p->fft();
254  back_transform = 1;
255  }
256 
257 // write_img("p.pif", p);
258 
259  long i, j, nn;
260 
261  for ( nn=j=0; nn<p->images(); nn++ )
262  for ( i=0; i<p->image_size(); i++, j++ )
263  p->set(j, p->complex(j) * (*pctf)[i]);
264 
265 // write_img("pf.pif", p);
266 
267  delete pctf;
268 
269  if ( back_transform ) p->fft(FFTW_BACKWARD);
270 
271 // p->statistics(); // fft includes statistics
272 
273  return 0;
274 }
275 
276 int img_ctf_apply(Bimage* p, CTFparam em_ctf, int action, double wiener,
277  double lores, double hires, fft_plan planf_2D, fft_plan planb_2D)
278 {
279  if ( action < 1 || action > 10 ) return 0;
280 
281  if ( wiener < 0.01 ) wiener = 0.01;
282 
283  Bimage* pctf = img_ctf_calculate(em_ctf, action, wiener, p->size(),
284  p->sampling(0), lores, hires);
285  pctf->invert();
286 
287  int back_transform(0);
288  if ( p->fourier_type() == NoTransform ) {
289  p->fft(planf_2D, 1);
290  back_transform = 1;
291  if ( verbose & VERB_DEBUG )
292  cout << "DEBUG img_ctf_apply: image transformed" << endl;
293  }
294 
295  long i, j, nn;
296 
297  for ( nn=j=0; nn<p->images(); nn++ )
298  for ( i=0; i<p->image_size(); i++, j++ )
299  p->set(j, p->complex(j) * (*pctf)[i]);
300 
301  delete pctf;
302 
303  if ( back_transform ) {
304  p->fft(planb_2D, 1);
305  p->complex_to_real();
306  if ( verbose & VERB_DEBUG )
307  cout << "DEBUG img_ctf_apply: image back-transformed" << endl;
308  }
309 
310  p->statistics();
311 
312  return 0;
313 }
314 
315 int img_ttf_apply_one(Bimage* p, long nn, CTFparam ctf, int action,
316  double wiener, double def, double res_lo, double res_hi,
318 {
319  verbose = 1;
320 
321  ctf.defocus_average(def);
322 
323  Bimage* pt = p->extract(nn);
324 
325  pt->calculate_background();
326 
327  Vector3<long> oldsize(pt->size()), translate;
328 
329  pt->pad(psize);
330 
331  img_ctf_apply(pt, ctf, action, wiener, res_lo, res_hi, planf, planb);
332 
333  pt->resize(oldsize, translate);
334 
335  p->replace(nn, pt);
336 
337  delete pt;
338 
339  return 0;
340 }
341 
354 int img_ctf_apply_to_proj(Bimage* proj, CTFparam em_ctf, double defocus, double res_lo, double res_hi, fft_plan planf_2D, fft_plan planb_2D)
355 {
356  if ( verbose & VERB_DEBUG )
357  cout << "DEBUG img_ctf_apply_to_proj: def_avg=" << em_ctf.defocus_average() << endl;
358 
359  if ( defocus > 0 ) em_ctf.defocus_average(defocus);
360 
361  img_ctf_apply(proj, em_ctf, 2, 0, res_lo, res_hi, planf_2D, planb_2D);
362 
363  return 0;
364 }
365 
388 int img_ttf_apply(Bimage* p, CTFparam ctf, int action, double wiener,
389  Vector3<long> tile_size, double tilt, double axis, double res_lo, double res_hi)
390 {
391  if ( res_lo > 0 && res_lo < res_hi ) swap(res_lo, res_hi);
392 
393  // Minimum and maximum tile sizes allowed
394  long min_size(64);
395  long max_size(2048);
396  Vector3<long> start;
397  Vector3<long> ext_size;
398 
399  tile_size = tile_size.min(p->size());
400 
401  tile_size = tile_size.max(min_size);
402  tile_size = tile_size.min(max_size);
403 
404  tile_size = tile_size.min(p->size());
405 
406  p->change_type(Float);
407 
408 // if ( verbose )
409 // cout << "Extracting tiles" << endl;
410  Bimage* pt = p->extract_tiles(0, tile_size, 0.5);
411 
412  long nn;
413  double ca(cos(axis)), sa(sin(axis)), tt(tan(tilt));
414  Vector3<double> coor;
415  double* d = new double[pt->images()];
416 
417 // if ( verbose )
418 // cout << "Tile\tx\ty\t∆f" << endl;
419  for ( nn=0; nn<pt->images(); nn++ ) {
420  coor = (pt->image[nn].origin() + pt->size()/2 - p->image->origin()) * pt->sampling(nn); // Center coordinates
421 // d[nn] = ctf.defocus_average() + (coor[0]*sa - coor[1]*ca)*tt;
422  d[nn] = ctf.defocus_average() + (coor[1]*ca - coor[0]*sa)*tt;
423 // if ( verbose )
424 // cout << nn << tab << pt->image[nn].origin() << tab << coor << tab << d[nn] << endl;
425  }
426 
427  Bstring base_eq = ctf.baseline_equation();
428  Bstring env_eq = ctf.envelope_equation();
429 
430  if ( verbose & ( VERB_LABEL | VERB_PROCESS ) ) {
431  cout << "Applying a tilted image transfer function:" << endl;
432  switch ( action ) {
433  case 1: cout << "Flipping phases" << endl; break;
434  case 2: cout << "Applying a CTF" << endl; break;
435  case 3: cout << "Correcting for the CTF" << endl; break;
436  case 4: cout << "Correcting for the CTF with an optimal Wiener filter" << endl; break;
437  case 5: cout << "Correcting for the CTF with baseline enhancement" << endl; break;
438  case 6: cout << "Correcting for the CTF with baseline2 enhancement" << endl; break;
439  case 7: cout << "Flipping phases with baseline enhancement" << endl; break;
440  case 8: cout << "Correcting for the CTF with baseline3 enhancement" << endl; break;
441  default: break;
442  }
443  }
444  if ( verbose & VERB_PROCESS ) {
445  cout << "Tile size: " << tile_size << endl;
446  cout << "Defocus range: " << d[0] << " - " << d[nn-1] << " (" << nn << ")" << endl;
447  cout << "Tilt angle and axis: " << tilt*180.0/M_PI << " " << axis*180.0/M_PI << endl;
448  ctf.show();
449  cout << "Resolution range: " << res_hi << " - ";
450  if ( res_lo > 0 ) cout << res_lo << " A" << endl;
451  else cout << "inf A" << endl;
452  if ( action == 3 || action == 5 || action == 6 )
453  cout << "Wiener factor: " << wiener << endl;
454  if ( action >= 4 ) {
455  cout << "Envelope: " << env_eq << endl;
456  cout << "Noise: " << base_eq << endl;
457  }
458  cout << endl;
459  }
460 
461  long pad_factor(1);
462  long ft_size = findNextPowerOf((int)(pad_factor*pt->sizeX()), 2);
463  Vector3<long> psize(ft_size, ft_size, 1);
464 
465  fft_plan planf = fft_setup_plan(psize, FFTW_FORWARD, 1);
466  fft_plan planb = fft_setup_plan(psize, FFTW_BACKWARD, 1);
467 
468 #ifdef HAVE_GCD
469  dispatch_apply(pt->images(), dispatch_get_global_queue(0, 0), ^(size_t i){
470  img_ttf_apply_one(pt, i, ctf, action, wiener, d[i], res_lo, res_hi, psize, planf, planb);
471  });
472 #else
473 #pragma omp parallel for
474  for ( long i=0; i<pt->images(); i++ )
475  img_ttf_apply_one(pt, i, ctf, action, wiener, d[i], res_lo, res_hi, psize, planf, planb);
476 #endif
477 
480 
481  p->assemble_tiles(pt, 1);
482 
483  delete pt;
484  delete[] d;
485 
486  return 0;
487 }
488 
489 
490 int mg_ps_name(Bmicrograph* mg, int img_num, Bstring& path, Bstring& newname, Bstring insert)
491 {
492  Bstring fn, ext;
493 
494  mg->fps = newname;
495 
496  if ( mg->fps.length() < 3 ) {
497  if ( img_num >= 0 ) insert = Bstring(img_num, "_%03d") + insert;
498  fn = mg->fmg;
499  if ( fn.length() < 1 ) fn = mg->fframe;
500  if ( fn.length() < 1 ) fn = mg->fpart;
501  if ( fn.length() ) {
502  ext = fn.extension();
503  if ( ( ext == "tif" ) || ( ext == "jpg" ) || ( ext == "jpeg" )
504  || ( ext == "png" ) || ext.contains("dm") )
505  ext = "mrc";
506  mg->fps = fn.pre_rev('.') + insert + ext;
507  } else {
508  mg->fps = "ps" + insert + ext;
509  }
510  }
511 
512  if ( path.length() > 1 )
513  mg->fps = path + mg->fps.post_rev('/');
514 
515  return 0;
516 }
517 
518 
519 Bimage* mg_ctf_prepare(Bmicrograph* mg, int action, double lores, double hires,
520  Vector3<long> tile_size, double def_start, double def_end, double def_inc, int flags)
521 {
522  int img_type(0), img_num(-1);
523  int ps_flags(7);
524  Vector3<double> pixel_size(mg->pixel_size);
525 
526  if ( !mg->ctf ) mg->ctf = new CTFparam;
527 
528  Bstring filename, insert, ext;
529  if ( action == 12 && mg->fps.length() ) {
530  filename = mg->fps;
531  img_type = 3;
532  img_num = mg->img_num;
533  if ( verbose & VERB_PROCESS )
534  cout << "Processing power spectrum " << mg->fps << " (" << img_num << ")" << endl;
535  } else if ( flags & 1 ) {
536  if ( mg->fframe.length() && ( flags & 16 ) ) {
537  filename = mg->fframe;
538  ps_flags |= 16;
539  } else {
540  filename = mg->fmg;
541  img_num = mg->img_num;
542  }
543  img_type = 1;
544  if ( verbose & VERB_PROCESS )
545  cout << "Processing micrograph " << filename << " (" << img_num << ")" << endl;
546  } else if ( mg->fpart.length() ) {
547  filename = mg->fpart;
548  img_type = 2;
549  img_num = -1;
550  ps_flags |= 16;
551  if ( mg->part->pixel_size[0] > 0 )
552  pixel_size = mg->part->pixel_size;
553  if ( verbose & VERB_PROCESS )
554  cout << "Processing particles " << filename << endl;
555  } else if ( mg->part && mg->part->fpart.length() ) {
556  img_type = 4;
557  ps_flags |= 16;
558  if ( verbose & VERB_PROCESS )
559  cout << "Processing particles from micrograph " << mg->id << endl;
560  }
561 
562  if ( img_type < 4 && !filename.length() ) {
563  if ( verbose & VERB_FULL )
564  cerr << "Warning: no file specified for micrograph " << mg->id << endl;
565  mg->select = 0;
566  return NULL;
567  }
568 
569  if ( verbose && filename.length() )
570  cout << "Processing file " << filename << " (" << img_num << ")" << endl;
571 
572  if ( action == 11 || action == 13 ) insert = "_ps.";
573  else insert = "_ctf.";
574 
575  Bimage* p = NULL;
576  if ( filename.length() ) {
577  p = read_img(filename, 1, img_num);
578  if ( !p ) {
579  cerr << "Warning: File " << filename << " not found or read!" << endl;
580  mg->select = 0;
581  return NULL;
582  }
583  p->sampling(pixel_size);
584  p->change_type(Float);
585  if ( ( flags & 16 ) && p->sizeZ() > 1 ) p->slices_to_images();
586  }
587 
588  if ( !p ) return NULL;
589 
590  if ( img_type > 1 ) tile_size = p->size();
591 
592  Bimage* ps = NULL;
593 
594  if ( flags & 2 )
595  p->filter_extremes();
596 
597 // cout << "volt=" << mg->ctf->volt() << " Cs=" << mg->ctf->Cs() << endl;
598 // cout << "p sampling = " << p->image->sampling() << endl;
599 
600  if ( action == 11 || action == 13 ) {
601  double lamda(electron_wavelength(mg->ctf->volt()));
602  double iCL2(1.0/(mg->ctf->Cs()*lamda*lamda));
603  ps = p->powerspectrum_tiled_and_tilted(tile_size, mg->tilt_axis,
604  mg->tilt_angle, 0, mg->ctf->defocus_average(), iCL2, ps_flags);
605 // ps = p->powerspectrum_tiled(0, tile_size, ps_flags);
606 // cout << "size = " << tile_size << endl;
607  delete p;
608  p = ps;
609  }
610 
611 // cout << "ps sampling = " << ps->image->sampling() << endl;
612 
613  if ( action == 12 || action == 13 )
614  mg->wri = img_ctf_fit(p, 0, *mg->ctf, lores, hires, def_start, def_end, def_inc, (flags & 8));
615 
616  return p;
617 }
618 
619 int rec_ctf_prepare(Breconstruction* rec, int action, double lores, double hires,
620  Vector3<long> tile_size, double def_start, double def_end, double def_inc,
621  Bstring& newname, int flags)
622 {
623  int img_type(0);
624 
625  if ( !rec->ctf ) rec->ctf = new CTFparam;
626 
627  if ( verbose & VERB_PROCESS )
628  cout << "Processing reconstruction " << rec->id << " (" << rec->frec << ")" << endl;
629 
630  Bstring filename, insert, ext;
631  if ( action == 12 && rec->fps.length() ) {
632  filename = rec->fps;
633  img_type = 3;
634  } else if ( ( flags & 1 ) && rec->frec.length() ) {
635  filename = rec->frec;
636  img_type = 1;
637  } else if ( rec->fpart.length() ) {
638  filename = rec->fpart;
639  img_type = 2;
640  } else if ( rec->part && rec->part->fpart.length() ) {
641  img_type = 4;
642  }
643 
644  if ( img_type < 4 && !filename.length() ) {
645  if ( verbose & VERB_FULL )
646  cerr << "Warning: no file specified for reconstruction " << rec->id << endl;
647  rec->select = 0;
648  return -1;
649  }
650 
651  if ( verbose && filename.length() )
652  cout << "Processing file " << filename << endl;
653 
654  if ( action == 11 || action == 13 ) insert = "_ps.";
655  else insert = "_ctf.";
656 
657  Bimage* p = NULL;
658  if ( filename.length() ) {
659  p = read_img(filename, 1, -1);
660  if ( !p ) {
661  cerr << "Warning: File " << filename << " not found or read!" << endl;
662  rec->select = 0;
663  return -1;
664  }
665  p->sampling(rec->voxel_size);
666  p->change_type(Float);
667  if ( verbose & VERB_LABEL )
668  cout << "Processing image " << p->file_name() << endl;
669  }
670 
671  Bimage* pex = NULL;
672 
673  if ( p && ( flags & 2 ) )
674  p->filter_extremes();
675 
676  if ( p && ( action == 11 || action == 13 ) ) {
677  pex = p->powerspectrum_tiled(0, tile_size, 7);
678  delete p;
679  p = pex;
680  if ( newname.length() )
681  rec->fps = newname;
682  else if ( rec->fps.length() < 3 ) {
683  ext = filename.extension();
684  if ( ( ext == "tif" ) || ( ext == "jpg" ) || ( ext == "jpeg" )
685  || ( ext == "png" ) || ext.contains("dm") )
686  ext = "pif";
687  rec->fps = filename.pre_rev('.') + insert + ext;
688  }
689  write_img(rec->fps, p, 0);
690  }
691 
692  if ( action == 12 || action == 13 )
693  rec->fom = img_ctf_fit(p, 0, *rec->ctf, lores, hires, def_start, def_end, def_inc, (flags & 8));
694 
695  delete p;
696 
697  return 0;
698 }
699 
719 int project_ctf_prepare(Bproject* project, int action, double lores,
720  double hires, Vector3<long> tile_size,
721  double def_start, double def_end, double def_inc,
722  Bstring& path, Bstring& newname, int flags)
723 {
724  if ( action < 1 ) return 0;
725 
726  if ( lores < 0 ) lores = 0;
727  if ( lores > 0 && lores < hires ) swap(lores, hires);
728 
729  if ( path.length() > 1 ) {
730  mkdir(path.c_str(), (mode_t)0755);
731  if ( path[-1] != '/' ) path += "/";
732  }
733 
734  Bfield* field;
735  Bmicrograph* mg;
736  Breconstruction* rec;
737  CTFparam* em_ctf = project->field->mg->ctf;
738 
739  if ( verbose ) {
740  if ( project->select < 1 )
741  cout << "CTF operations on all micrographs:" << endl;
742  else
743  cout << "CTF operations on all reconstructions:" << endl;
744  switch ( action ) {
745  case 11: cout << "Calculating power spectra" << endl; break;
746  case 12: cout << "Fitting power spectra" << endl; break;
747  case 13: cout << "Calculating and fitting power spectra" << endl; break;
748  default: break;
749  }
750  if ( action == 12 || action == 13 ) {
751  em_ctf->show();
752  cout << "Baseline type: " << em_ctf->baseline_type() << endl;
753  cout << "Envelope type: " << em_ctf->envelope_type() << endl;
754  cout << "Defocus min, max, inc: " << def_start << " - " << def_end << " ∆ " << def_inc << endl << endl;
755  }
756  cout << "Resolution range: " << hires << " - ";
757  if ( lores > 0 ) cout << lores << " A" << endl;
758  else cout << "inf A" << endl;
759  cout << "Flags: " << flags << endl << endl;
760  }
761 
762  long nmg, onefile(0);
763  Bimage* ps = NULL;;
764  Bimage* ps1;
765 
766  Bstring filename, insert, ext;
767  if ( action == 11 || action == 13 ) insert = "_ps.";
768  else insert = "_ctf.";
769 
770  if ( project->select < 1 ) {
771  for ( field = project->field; field; field = field->next ) {
772  for ( nmg = 1, mg = field->mg; mg->next; mg = mg->next ) nmg++;
773  if ( ( action == 11 || action == 13 ) && ( field->mg->fmg == mg->fmg ) ) {
774 // ps = new Bimage(Float, TSimple, tile_size, nmg);
775 // ps->sampling(field->mg->pixel_size);
776  onefile = 1;
777  } else onefile = 0;
778  for ( mg = field->mg; mg; mg = mg->next ) {
779  ps1 = mg_ctf_prepare(mg, action, lores, hires, tile_size, def_start, def_end, def_inc, flags);
780  if ( ps1 ) {
781  if ( action == 11 || action == 13 ) {
782  if ( onefile == 1 ) {
783  ps = new Bimage(Float, TSimple, ps1->size(), nmg);
784  onefile = 2;
785  }
786  if ( onefile == 2 ) {
787  ps->replace(mg->img_num, ps1);
788  ps->sampling(ps1->sampling(0));
789  mg_ps_name(mg, -1, path, newname, insert);
790  } else {
791 // mg_ps_name(mg, mg->img_num, path, newname, insert);
792  mg_ps_name(mg, -1, path, newname, insert);
793  write_img(mg->fps, ps1, 0);
794  }
795  }
796  delete ps1;
797  } else {
798  cerr << "Warning: No power spectrum calculated for micrograph "
799  << mg->id << endl;
800  }
801  }
802  if ( ps ) {
803  write_img(field->mg->fps, ps, 0);
804  delete ps;
805  }
806  }
807  } else {
808  for ( rec = project->rec; rec; rec = rec->next ) {
809  rec_ctf_prepare(rec, action, lores, hires, tile_size,
810  def_start, def_end, def_inc, newname, flags);
811  }
812  }
813 
814 
815  return 0;
816 }
817 
818 
819 int part_ctf(Bparticle* partlist, int action, double lores, double hires,
820  double wiener, DataType datatype, Bstring& partpath,
821  Bstring& newname, int flags)
822 {
823  if ( verbose & VERB_DEBUG )
824  cout << "DEBUG part_ctf: partlist=" << partlist << endl;
825 
826  Bparticle* part = partlist;
827  if ( !part ) return -1;
828 
829  Bmicrograph* mg = part->mg;
830  Breconstruction* rec = part->rec;
831 
832  if ( verbose & VERB_DEBUG )
833  cout << "DEBUG part_ctf: mg=" << mg << " rec=" << rec << endl;
834 
835  int fn_flag(0); // particle file flag: 0=mg, 1=part
836  long npart(0);
837  Vector3<double> pixel_size;
838  Bstring filename, insert("_ctf."), ext;
839  Bimage* p = NULL;
840  Bimage* pnu = NULL;
841 
842  CTFparam* ctf = NULL;
843  if ( mg ) {
844  filename = mg->fpart;
845  pixel_size = mg->pixel_size;
846  if ( mg->ctf ) ctf = mg->ctf;
847  else ctf = new CTFparam;
848  } else if ( rec ) {
849  filename = rec->fpart;
850  pixel_size = rec->voxel_size;
851  if ( rec->ctf ) ctf = rec->ctf;
852  else ctf = new CTFparam;
853  }
854 
855  for ( part = partlist; part; part = part->next ) npart++;
856  part = partlist;
857 
858  if ( part->fpart.length() ) {
859  filename = part->fpart;
860  fn_flag = 1;
861  }
862 
863  if ( !filename.length() ) {
864  if ( verbose & VERB_FULL )
865  cerr << "Warning: no particle file specified!" << endl;
866  return -1;
867  }
868 
869  p = read_img(filename, 0, -1);
870  if ( !p ) {
871  if ( verbose & VERB_FULL )
872  cerr << "Warning: particle file " << filename << " not read!" << endl;
873  return -1;
874  }
875 
876  if ( verbose )
877  cout << p->file_name() << " (" << p->images() << ")" << endl;
878 
879  if ( pixel_size[0] < 0.01 ) pixel_size = p->sampling(0);
880 
881  DataType nudatatype = datatype;
882  if ( nudatatype == Unknown_Type ) nudatatype = p->data_type();
883 
884  if ( verbose & VERB_DEBUG )
885  cout << "DEBUG part_ctf: p->images()=" << p->images() << " npart=" << npart << endl;
886 
887  if ( fn_flag < 1 || p->images() > 1 ) {
888  if ( p->images() != npart ) {
889  cerr << "Error: Micrograph " << mg->id << " has " << npart << " particles," << endl;
890  cerr << " but particle image file " << p->file_name() << " has " << p->images() << " images!" << endl;
891  delete p;
892 // return -2;
893  bexit(-1);
894  }
895  pnu = p->copy_header();
896  pnu->data_type(nudatatype);
897  pnu->data_alloc();
898  pnu->sampling(pixel_size);
899 // pnu->change_type(nudatatype);
900  }
901 
902  delete p;
903 
905  part_ctf.update(ctf);
906 
907  double cosax(1), sinax(0), sintilt(0);
908 
909  if ( mg ) {
910  cosax = cos(mg->tilt_axis);
911  sinax = sin(mg->tilt_axis);
912  sintilt = sin(mg->tilt_angle);
913  }
914 
915  for ( part = partlist; part; part = part->next ) {
916  if ( part->def > 0 ) part_ctf.defocus_average(part->def);
917  else if ( mg )
918  part_ctf.defocus_average(mg->ctf->defocus_average() +
919  fabs(cosax*part->loc[0] - sinax*part->loc[1])*sintilt);
920  if ( part->dev ) part_ctf.defocus_deviation(part->dev);
921  if ( part->ast ) part_ctf.astigmatism_angle(part->ast);
922  part->pixel_size = pixel_size;
923 
924  if ( part->fpart.length() ) filename = part->fpart;
925  else if ( mg ) filename = mg->fpart;
926  else if ( rec ) filename = rec->fpart;
927  if ( part->fpart.length() ) p = read_img(filename, 1, 0);
928  else p = read_img(filename, 1, part->id - 1);
929  p->sampling(pixel_size);
930  p->change_type(Float);
931 
932  if ( verbose & VERB_LABEL )
933  cout << "Processing image " << p->file_name() << " (" << part->id << ")" << endl;
934 
935  img_ctf_apply(p, part_ctf, action, wiener, lores, hires);
936 
937  if ( flags & 4 ) p->correct_background();
938  p->change_type(nudatatype);
939 
940 // if ( nimg == 1 ) insert = Bstring(part->id, "_%04d.");
941  if ( newname.length() ) filename = newname;
942  else filename = filename.pre_rev('.') + insert + filename.post_rev('.');
943 
944  if ( partpath.length() > 1 ) {
945  if ( filename.contains("/") ) filename = filename.post_rev('/');
946  filename = partpath + filename;
947  }
948 
949  if ( part->fpart.length() ) {
950  write_img(filename, p, 0);
951  part->fpart = filename;
952  } else {
953  pnu->replace(part->id - 1, p);
954  }
955 
956  delete p;
957 
958  if ( fabs(sintilt) > 1e-6 ) part->def = part_ctf.defocus_average();
959  }
960 
961  if ( pnu ) {
962  write_img(filename, pnu, 0);
963  if ( mg ) mg->fpart = filename;
964  else if ( rec ) rec->fpart = filename;
965  }
966 
967  delete pnu;
968 
969  return 0;
970 }
971 
972 int mg_ctf(Bmicrograph* mg, Bimage* pmg, int action, double lores, double hires, double wiener,
973  Vector3<long> tile_size, DataType datatype, Bstring& newname, int flags)
974 {
975  if ( !mg->ctf ) mg->ctf = new CTFparam;
976 
977  Bstring filename(mg->fmg), insert("_ctf."), ext("pif");
978 
979  if ( !filename.length() ) {
980  if ( verbose & VERB_FULL )
981  cerr << "Warning: no file specified for micrograph " << mg->id << endl;
982  mg->select = 0;
983  return -1;
984  }
985 
986  Bimage* p = NULL;
987  if ( !pmg ) p = read_img(filename, 1, mg->img_num);
988  else p = pmg->extract(mg->img_num);
989 
990  if ( !p ) {
991  cerr << "Warning: File " << filename << " not found or read!" << endl;
992  mg->select = 0;
993  return -1;
994  }
995 
996  p->origin(mg->origin);
997  if ( p->image->origin().length() < 1 ) p->origin(p->size()/2);
998  p->sampling(mg->pixel_size);
999  p->change_type(Float);
1000 
1001  DataType nudatatype = datatype;
1002  if ( pmg ) nudatatype = pmg->data_type();
1003  else if ( nudatatype == Unknown_Type ) nudatatype = p->data_type();
1004 
1005  if ( p && ( flags & 2 ) )
1006  p->filter_extremes();
1007 
1008  if ( mg->tilt_angle ) {
1009  if ( verbose )
1010  cout << p->file_name() << " (" << mg->img_num << ") " << mg->tilt_angle*180.0/M_PI << endl;
1011  img_ttf_apply(p, *(mg->ctf), action, wiener,
1012  tile_size, mg->tilt_angle, mg->tilt_axis, lores, hires);
1013  } else {
1014  if ( verbose )
1015  cout << p->file_name() << " (" << mg->img_num << ")" << endl;
1016  img_ctf_apply(p, *(mg->ctf), action, wiener, lores, hires);
1017  }
1018 
1019 
1020  if ( flags & 4 ) p->correct_background();
1021  p->change_type(nudatatype);
1022 
1023  if ( pmg ) {
1024  pmg->replace(mg->img_num, p);
1025  } else {
1026  if ( newname.length() ) filename = newname;
1027  else {
1028  insert = Bstring(mg->img_num, "_%03d_ctf.");
1029  if ( filename.contains(".dm") )
1030  filename = filename.pre_rev('.') + insert + ext;
1031  else
1032  filename = filename.pre_rev('.') + insert + filename.post_rev('.');
1033  }
1034  mg->fmg = filename;
1035  mg->img_num = 0;
1036  if ( verbose & VERB_DEBUG )
1037  cout << "DEBUG mg_ctf: Writing file " << filename << endl;
1038  write_img(filename, p, 0);
1039  }
1040 
1041  delete p;
1042 
1043  return 0;
1044 }
1045 
1046 int rec_ctf(Breconstruction* rec, int action, double lores, double hires, double wiener,
1047  DataType datatype, Bstring& newname, int flags)
1048 {
1049  if ( !rec->ctf ) rec->ctf = new CTFparam;
1050 
1051  Bstring filename(rec->frec), insert("_ctf."), ext;
1052 
1053  if ( !filename.length() ) {
1054  if ( verbose & VERB_FULL )
1055  cerr << "Warning: no file specified for reconstruction " << rec->id << endl;
1056  rec->select = 0;
1057  return -1;
1058  }
1059 
1060  Bimage* p = read_img(filename, 1, 0);
1061  if ( !p ) {
1062  cerr << "Warning: File " << filename << " not found or read!" << endl;
1063  rec->select = 0;
1064  return -1;
1065  }
1066 
1067  p->origin(rec->origin);
1068  p->sampling(rec->voxel_size);
1069  p->change_type(Float);
1070 
1071  if ( verbose )
1072  cout << p->file_name() << endl;
1073 
1074  DataType nudatatype = datatype;
1075  if ( nudatatype == Unknown_Type ) nudatatype = p->data_type();
1076 
1077  if ( flags & 2 )
1078  p->filter_extremes();
1079 
1080  img_ctf_apply(p, *(rec->ctf), action, wiener, lores, hires);
1081 
1082  if ( newname.length() ) filename = newname;
1083  else filename = filename.pre_rev('.') + insert + filename.post_rev('.');
1084 
1085  rec->frec = filename;
1086 
1087  if ( p ) {
1088  if ( flags & 4 ) p->correct_background();
1089  if ( verbose & VERB_DEBUG )
1090  cout << "DEBUG rec_ctf: Writing file " << filename << endl;
1091  p->change_type(nudatatype);
1092  write_img(filename, p, 0);
1093  }
1094 
1095  delete p;
1096 
1097  return 0;
1098 }
1099 
1119 int project_ctf(Bproject* project, int action, double lores,
1120  double hires, Vector3<long> tile_size, double wiener,
1121  DataType datatype, Bstring& partpath, Bstring& newname, int flags)
1122 {
1123  if ( action < 1 ) return 0;
1124 
1125  if ( lores < 0 ) lores = 0;
1126  if ( lores > 0 && lores < hires ) swap(lores, hires);
1127 
1128  Bfield* field;
1129  Bmicrograph* mg;
1130  Breconstruction* rec;
1131  Bimage* p = NULL;
1132  Bstring filename, insert("_ctf."), ext("pif");
1133 
1134  if ( verbose ) {
1135  if ( project->select < 1 )
1136  cout << "CTF operations on all micrographs:" << endl;
1137  else
1138  cout << "CTF operations on all reconstructions:" << endl;
1139  switch ( action ) {
1140  case 1: cout << "Flipping phases" << endl; break;
1141  case 2: cout << "Applying a CTF" << endl; break;
1142  case 3: cout << "Correcting for the CTF" << endl; break;
1143  case 4: cout << "Correcting for the CTF with an optimal Wiener filter" << endl; break;
1144  case 5: cout << "Correcting for the CTF with baseline enhancement" << endl; break;
1145  case 6: cout << "Correcting for the CTF with baseline2 enhancement" << endl; break;
1146  case 7: cout << "Flipping phases with baseline enhancement" << endl; break;
1147  case 8: cout << "Correcting for the CTF with baseline3 enhancement" << endl; break;
1148  default: break;
1149  }
1150  cout << "Resolution range: " << hires << " - ";
1151  if ( lores > 0 ) cout << lores << " A" << endl;
1152  else cout << "inf A" << endl;
1153  cout << "Flags: " << flags << endl << endl;
1154  }
1155 
1156  if ( project->select < 1 ) {
1157  for ( field = project->field; field; field = field->next ) {
1158  if ( flags & 1 ) {
1159  for ( mg = field->mg; mg->next; mg = mg->next ) ;
1160  if ( field->mg->fmg == mg->fmg ) {
1161  if ( verbose )
1162  cout << "Reading: " << mg->fmg << " (" << mg->img_num + 1 << ")" << endl;
1163  p = read_img(mg->fmg, 1, -1);
1164  }
1165  }
1166  if ( verbose )
1167  cout << "Image\tNumber\tTilt" << endl;
1168  for ( mg = field->mg; mg; mg = mg->next ) {
1169  if ( flags & 1 )
1170  mg_ctf(mg, p, action, lores, hires, wiener, tile_size,
1171  datatype, newname, flags);
1172  else
1173  part_ctf(mg->part, action, lores, hires, wiener,
1174  datatype, partpath, newname, flags);
1175  }
1176  if ( p ) {
1177  filename = p->file_name();
1178  if ( newname.length() ) filename = newname;
1179  else {
1180  if ( filename.contains(".dm") )
1181  filename = filename.pre_rev('.') + insert + ext;
1182  else
1183  filename = filename.pre_rev('.') + insert + filename.post_rev('.');
1184  }
1185  for ( mg = field->mg; mg; mg = mg->next ) mg->fmg = filename;
1186  if ( verbose )
1187  cout << "Writing: " << filename << endl;
1188  write_img(filename, p, 0);
1189  delete p;
1190  }
1191  }
1192  } else {
1193  for ( rec = project->rec; rec; rec = rec->next ) {
1194  if ( flags & 1 )
1195  rec_ctf(rec, action, lores, hires, wiener,
1196  datatype, newname, flags);
1197  else
1198  part_ctf(rec->part, action, lores, hires, wiener,
1199  datatype, partpath, newname, flags);
1200  }
1201  }
1202 
1203  return 0;
1204 }
1205 
1212 {
1213  double def_min(1e37), def_max(0), pdef(0);
1214  Bfield* field;
1215  Bmicrograph* mg = NULL;
1216  Bparticle* part = NULL;
1217 
1218  for ( field = project->field; field; field = field->next ) if ( field->select ) {
1219  for ( mg = field->mg; mg; mg = mg->next ) if ( mg->select ) {
1220  for ( part = mg->part; part; part = part->next ) if ( part->sel ) {
1221  if ( part->def ) {
1222  if ( def_min > part->def )
1223  def_min = part->def;
1224  if ( def_max < part->def )
1225  def_max = part->def;
1226  pdef = 1;
1227  }
1228  }
1229  if ( !pdef && mg->ctf ) {
1230  if ( def_min > mg->ctf->defocus_average() )
1231  def_min = mg->ctf->defocus_average();
1232  if ( def_max < mg->ctf->defocus_average() )
1233  def_max = mg->ctf->defocus_average();
1234  }
1235  }
1236  }
1237 
1238  JSvalue js(JSobject);
1239  js["defocus_minimum"] = def_min;
1240  js["defocus_maximum"] = def_max;
1241 
1242  if ( verbose & VERB_PROCESS )
1243  cout << js << endl;
1244 
1245  return js;
1246 }
1247 
1260 {
1261  Bfield* field;
1262  Bmicrograph* mg = NULL;
1263 
1264  for ( field = project->field; field; field = field->next )
1265  for ( mg = field->mg; mg; mg = mg->next )
1266  if ( mg->box_size[0] ) break;
1267 
1268  if ( !mg ) {
1269  cerr << "Error: No micrograph with a box size specified!" << endl;
1270  return -1;
1271  }
1272 
1273  int i, j, n(0), maxrad = mg->box_size[0]/2;
1274  double recip_interval = 1.0/(2*maxrad*mg->pixel_size[0]);
1275  vector<double> ctf;
1276 
1277  int ncol = 2;
1278  Bstring title("Compound CTF curves"), txt;
1279  Bplot* plot = new Bplot(1, maxrad, ncol);
1280  plot->title(title);
1281 
1282  plot->page(0).title(title);
1283  plot->page(0).columns(ncol);
1284  for ( i=0; i<ncol; i++ ) plot->page(0).column(i).number(i);
1285  plot->page(0).column(0).label("s");
1286  plot->page(0).column(1).label("CTFavg");
1287  plot->page(0).column(0).axis(1);
1288  plot->page(0).column(1).axis(3);
1289  plot->page(0).column(1).type(2);
1290  plot->page(0).axis(1).flags(1);
1291  plot->page(0).axis(1).label("Resolution (A)");
1292 // plot->page(0).axis(1).min(0);
1293 // plot->page(0).axis(1).max(x_max);
1294  plot->page(0).axis(3).label("Average CTF");
1295  plot->page(0).axis(3).min(0);
1296  plot->page(0).axis(3).max(1);
1297 
1298  for ( field = project->field; field; field = field->next ) {
1299  for ( mg = field->mg; mg; mg = mg->next ) {
1300  if ( mg->ctf ) {
1301  ctf = mg->ctf->calculate(maxrad, 1, recip_interval);
1302  for ( i=0, j=maxrad; i<maxrad; i++, j++ )
1303  (*plot)[j] += ctf[i]*ctf[i];
1304 // delete ctf;
1305  n++;
1306  }
1307  }
1308  }
1309 
1310  for ( i=0, j=maxrad; i<maxrad; i++, j++ ) {
1311  (*plot)[i] = i*recip_interval;
1312  (*plot)[j] /= n;
1313  }
1314 
1315  txt = "Number of micrographs: " + Bstring(n, "%d");
1316  plot->page(0).add_text(txt);
1317 
1318  ps_plot(psname, plot);
1319 
1320  delete plot;
1321 
1322  return 0;
1323 }
1324 
1333 {
1334  long n(0);
1335  Bimage* ps = NULL;
1336  Bimage* pt = NULL;
1337  Bimage* psavg = NULL;
1338  Bfield* field;
1339  Bmicrograph* mg = NULL;
1340  Vector3<double> origin, scale(1,1,1), translate;
1341  Matrix3 mat(1);
1342 
1343  if ( verbose ) {
1344  cout << "Calculating an average power spectrum with target defocus: " << deftarget << endl;
1345  cout << "Micrograph\tDefocus\tScale" << endl;
1346  }
1347 
1348  for ( field = project->field; field; field = field->next ) {
1349  for ( mg = field->mg; mg; mg = mg->next ) {
1350  if ( mg->fps.length() )
1351  ps = read_img(mg->fps, 1, mg->img_num);
1352  if ( ps ) {
1353  if ( !psavg ) {
1354  psavg = ps->copy(1);
1355  psavg->origin(ps->size()/2);
1356  psavg->fill(0);
1357  psavg->sampling(mg->pixel_size);
1358  }
1359  ps->calculate_background();
1360  scale[0] = scale[1] = sqrt(mg->ctf->defocus_average()/deftarget);
1361  if ( verbose )
1362  cout << mg->id << tab << mg->ctf->defocus_average() << tab << scale << endl;
1363  pt = ps->transform(0, ps->size(), scale, psavg->image->origin(), translate, mat, FILL_BACKGROUND);
1364  psavg->add(pt);
1365  delete pt;
1366  delete ps;
1367  n++;
1368  }
1369  }
1370  }
1371 
1372  if ( n ) psavg->multiply(1.0/n);
1373 
1374  return psavg;
1375 }
1376 
1384 {
1385  if ( !project || ! ctfproject ) return 0;
1386 
1387  Bfield *field, *ctffield;
1388  Bmicrograph *mg, *ctfmg;
1389  Breconstruction *rec, *ctfrec;
1390 
1391  if ( verbose & VERB_PROCESS )
1392  cout << "Merging CTF parameters into the main project" << endl << endl;
1393 
1394  for ( field=project->field, ctffield=ctfproject->field; field; field=field->next, ctffield=ctffield->next ) {
1395  for ( mg=field->mg, ctfmg=ctffield->mg; mg && ctfmg; mg=mg->next, ctfmg=ctfmg->next ) {
1396  if ( ctfmg->fps.length() > 0 ) mg->fps = ctfmg->fps;
1397  if ( ctfmg->magnification ) mg->magnification = ctfmg->magnification;
1398  if ( ctfmg->sampling ) mg->sampling = ctfmg->sampling;
1399  if ( ctfmg->ctf ) {
1400  if ( !mg->ctf ) mg->ctf = new CTFparam;
1401  mg->ctf->update(ctfmg->ctf);
1402  }
1403  }
1404  }
1405 
1406  for ( rec=project->rec, ctfrec=ctfproject->rec; rec && ctfrec; rec=rec->next, ctfrec=ctfrec->next ) {
1407  if ( ctfrec->fps.length() > 0 ) rec->fps = ctfrec->fps;
1408  if ( ctfrec->ctf ) {
1409  if ( !rec->ctf ) rec->ctf = new CTFparam;
1410  rec->ctf->update(ctfrec->ctf);
1411  }
1412  }
1413 
1414  return 0;
1415 }
1416 
1423 {
1424  if ( !project ) return 0;
1425 
1426  Bfield* field;
1427  Bmicrograph* mg;
1428  Breconstruction* rec;
1429  Bparticle* part;
1430 
1431  if ( verbose & VERB_PROCESS )
1432  cout << "Transferring CTF parameters to particles" << endl << endl;
1433 
1434  for ( field = project->field; field; field = field->next ) {
1435  for ( mg = field->mg; mg; mg = mg->next ) if ( mg->ctf ) {
1436  for ( part = mg->part; part; part = part->next ) {
1437  part->def = mg->ctf->defocus_average();
1438  part->dev = mg->ctf->defocus_deviation();
1439  part->ast = mg->ctf->astigmatism_angle();
1440  }
1441  }
1442  }
1443 
1444  for ( rec = project->rec; rec; rec = rec->next ) if ( rec->ctf ) {
1445  for ( part = rec->part; part; part = part->next ) {
1446  part->def = rec->ctf->defocus_average();
1447  part->dev = rec->ctf->defocus_deviation();
1448  part->ast = rec->ctf->astigmatism_angle();
1449  }
1450  }
1451 
1452  return 0;
1453 }
1454 
1463 int project_set_defocus(Bproject* project, double def_avg,
1464  double def_dev, double ast_angle)
1465 {
1466  if ( !project ) return 0;
1467 
1468  long n(0);
1469  Bfield* field;
1470  Bmicrograph* mg;
1471  Breconstruction* rec;
1472 
1473  if ( verbose & VERB_DEBUG )
1474  cout << "DEBUG project_set_defocus: avg=" << def_avg << " dev=" << def_dev << " ang=" << ast_angle*180.0/M_PI << endl;
1475 
1476  for ( n=0, field = project->field; field; field = field->next ) {
1477  for ( mg = field->mg; mg; mg = mg->next ) {
1478  if ( !mg->ctf ) mg->ctf = new CTFparam;
1479  mg->ctf->defocus_average(def_avg);
1480  if ( def_dev ) {
1481  mg->ctf->defocus_deviation(def_dev);
1482  mg->ctf->astigmatism_angle(ast_angle);
1483  }
1484  n++;
1485  }
1486  }
1487 
1488  if ( verbose & VERB_DEBUG )
1489  cout << "DEBUG project_set_defocus: micrographs set = " << n << endl;
1490 
1491  for ( n=0, rec = project->rec; rec; rec = rec->next ) {
1492  if ( !rec->ctf ) rec->ctf = new CTFparam;
1493  rec->ctf->defocus_average(def_avg);
1494  if ( def_dev ) {
1495  rec->ctf->defocus_deviation(def_dev);
1496  rec->ctf->astigmatism_angle(ast_angle);
1497  }
1498  n++;
1499  }
1500 
1501  if ( verbose & VERB_DEBUG )
1502  cout << "DEBUG project_set_defocus: reconstructions set = " << n << endl;
1503 
1504  return 0;
1505 }
1506 
1514 int project_set_astigmatism(Bproject* project, double def_dev, double ast_angle)
1515 {
1516  if ( !project ) return 0;
1517 
1518  Bfield* field;
1519  Bmicrograph* mg;
1520  Breconstruction* rec;
1521 
1522  if ( verbose & VERB_DEBUG )
1523  cout << "DEBUG project_set_astigmatism: dev=" << def_dev << " ang=" << ast_angle*180.0/M_PI << endl;
1524 
1525  for ( field = project->field; field; field = field->next ) {
1526  for ( mg = field->mg; mg; mg = mg->next ) {
1527  if ( !mg->ctf ) mg->ctf = new CTFparam;
1528  mg->ctf->defocus_deviation(def_dev);
1529  mg->ctf->astigmatism_angle(ast_angle);
1530  }
1531  }
1532 
1533  for ( rec = project->rec; rec; rec = rec->next ) {
1534  if ( !rec->ctf ) rec->ctf = new CTFparam;
1535  rec->ctf->defocus_deviation(def_dev);
1536  rec->ctf->astigmatism_angle(ast_angle);
1537  }
1538 
1539  return 0;
1540 }
1541 
1549 {
1550  if ( !project ) return 0;
1551 
1552  Bfield* field;
1553  Bmicrograph* mg;
1554  Breconstruction* rec;
1555 
1556  if ( verbose & VERB_FULL )
1557  cout << "CTF parameters:" << endl << jsctf << endl;
1558 
1559  for ( field = project->field; field; field = field->next )
1560  for ( mg = field->mg; mg; mg = mg->next ) {
1561  if ( !mg->ctf ) mg->ctf = new CTFparam;
1562  mg->ctf[0] = ctf_from_json(jsctf);
1563  }
1564 
1565  for ( rec = project->rec; rec; rec = rec->next ) {
1566  if ( !rec->ctf ) rec->ctf = new CTFparam;
1567  rec->ctf[0] = ctf_from_json(jsctf);
1568  }
1569 
1570  return 0;
1571 }
1572 
1580 {
1581  if ( !project ) return 0;
1582 
1583  if ( volts < 1e4 ) volts *= 1000; // Asume kV
1584 
1585  Bfield* field;
1586  Bmicrograph* mg;
1587  Breconstruction* rec;
1588 
1589  for ( field = project->field; field; field = field->next )
1590  for ( mg = field->mg; mg; mg = mg->next ) {
1591  if ( !mg->ctf ) mg->ctf = new CTFparam;
1592  mg->ctf->volt(volts);
1593  }
1594 
1595  for ( rec = project->rec; rec; rec = rec->next ) {
1596  if ( !rec->ctf ) rec->ctf = new CTFparam;
1597  rec->ctf->volt(volts);
1598  }
1599 
1600  return 0;
1601 }
1602 
1610 {
1611  if ( !project ) return 0;
1612  if ( Cs < 10 ) Cs *= 1e7; // Assume mm
1613 
1614  Bfield* field;
1615  Bmicrograph* mg;
1616  Breconstruction* rec;
1617 
1618  for ( field = project->field; field; field = field->next )
1619  for ( mg = field->mg; mg; mg = mg->next ) {
1620  if ( !mg->ctf ) mg->ctf = new CTFparam;
1621  mg->ctf->Cs(Cs);
1622  }
1623 
1624  for ( rec = project->rec; rec; rec = rec->next ) {
1625  if ( !rec->ctf ) rec->ctf = new CTFparam;
1626  rec->ctf->Cs(Cs);
1627  }
1628 
1629  return 0;
1630 }
1631 
1638 int project_set_amp_shift(Bproject* project, double amp_shift)
1639 {
1640  if ( !project ) return 0;
1641 
1642  Bfield* field;
1643  Bmicrograph* mg;
1644  Breconstruction* rec;
1645 
1646  for ( field = project->field; field; field = field->next )
1647  for ( mg = field->mg; mg; mg = mg->next ) {
1648  if ( !mg->ctf ) mg->ctf = new CTFparam;
1649  mg->ctf->amp_shift(amp_shift);
1650  }
1651 
1652  for ( rec = project->rec; rec; rec = rec->next ) {
1653  if ( !rec->ctf ) rec->ctf = new CTFparam;
1654  rec->ctf->amp_shift(amp_shift);
1655  }
1656 
1657  return 0;
1658 }
1659 
1666 int project_set_focal_length(Bproject* project, double focal_length)
1667 {
1668  if ( !project ) return 0;
1669  if ( focal_length < 10 ) focal_length *= 1e7; // Assume mm
1670 
1671  Bfield* field;
1672  Bmicrograph* mg;
1673  Breconstruction* rec;
1674 
1675  for ( field = project->field; field; field = field->next )
1676  for ( mg = field->mg; mg; mg = mg->next ) {
1677  if ( !mg->ctf ) mg->ctf = new CTFparam;
1678  mg->ctf->focal_length(focal_length);
1679  }
1680 
1681  for ( rec = project->rec; rec; rec = rec->next ) {
1682  if ( !rec->ctf ) rec->ctf = new CTFparam;
1683  rec->ctf->focal_length(focal_length);
1684  }
1685 
1686  return 0;
1687 }
1688 
1695 int project_set_aperture(Bproject* project, double aperture)
1696 {
1697  if ( !project ) return 0;
1698  if ( aperture < 1000 ) aperture *= 1e4; // Assume µm
1699 
1700  Bfield* field;
1701  Bmicrograph* mg;
1702  Breconstruction* rec;
1703 
1704  for ( field = project->field; field; field = field->next )
1705  for ( mg = field->mg; mg; mg = mg->next ) {
1706  if ( !mg->ctf ) mg->ctf = new CTFparam;
1707  mg->ctf->objective_aperture(aperture);
1708  }
1709 
1710  for ( rec = project->rec; rec; rec = rec->next ) {
1711  if ( !rec->ctf ) rec->ctf = new CTFparam;
1712  rec->ctf->objective_aperture(aperture);
1713  }
1714 
1715  return 0;
1716 }
1717 
1725 {
1726  if ( !project ) return 0;
1727 
1728  Bfield* field;
1729  Bmicrograph* mg;
1730  Breconstruction* rec;
1731 
1732  for ( field = project->field; field; field = field->next )
1733  for ( mg = field->mg; mg; mg = mg->next ) {
1734  if ( !mg->ctf ) mg->ctf = new CTFparam;
1735  mg->ctf->slit_width(slit);
1736  }
1737 
1738  for ( rec = project->rec; rec; rec = rec->next ) {
1739  if ( !rec->ctf ) rec->ctf = new CTFparam;
1740  rec->ctf->slit_width(slit);
1741  }
1742 
1743  return 0;
1744 }
1745 
1753 {
1754  if ( !project ) return 0;
1755 
1756  Bfield* field;
1757  Bmicrograph* mg;
1758  Breconstruction* rec;
1759 
1760  for ( field = project->field; field; field = field->next )
1761  for ( mg = field->mg; mg; mg = mg->next ) {
1762  if ( !mg->ctf ) mg->ctf = new CTFparam;
1763  mg->ctf->alpha(alpha);
1764  }
1765 
1766  for ( rec = project->rec; rec; rec = rec->next ) {
1767  if ( !rec->ctf ) rec->ctf = new CTFparam;
1768  rec->ctf->alpha(alpha);
1769  }
1770 
1771  return 0;
1772 }
1773 
1781 {
1782  if ( !project ) return 0;
1783 
1784  Bfield* field;
1785  Bmicrograph* mg;
1786  Breconstruction* rec;
1787 
1788  for ( field = project->field; field; field = field->next )
1789  for ( mg = field->mg; mg; mg = mg->next ) {
1790  if ( !mg->ctf ) mg->ctf = new CTFparam;
1791  mg->ctf->envelope_type(type);
1792  }
1793 
1794  for ( rec = project->rec; rec; rec = rec->next ) {
1795  if ( !rec->ctf ) rec->ctf = new CTFparam;
1796  rec->ctf->envelope_type(type);
1797  }
1798 
1799  return 0;
1800 }
1801 
1809 int project_set_envelope(Bproject* project, int type, double* coeff)
1810 {
1811  if ( !project ) return 0;
1812 
1813  Bfield* field;
1814  Bmicrograph* mg;
1815  Breconstruction* rec;
1816 
1817  for ( field = project->field; field; field = field->next )
1818  for ( mg = field->mg; mg; mg = mg->next ) {
1819  if ( !mg->ctf ) mg->ctf = new CTFparam;
1820  mg->ctf->envelope_type(type);
1821  mg->ctf->envelope(coeff);
1822  }
1823 
1824  for ( rec = project->rec; rec; rec = rec->next ) {
1825  if ( !rec->ctf ) rec->ctf = new CTFparam;
1826  rec->ctf->envelope_type(type);
1827  rec->ctf->envelope(coeff);
1828  }
1829 
1830  return 0;
1831 }
1832 
1845 {
1846  if ( !project ) return 0;
1847 
1848  Bfield* field;
1849  Bmicrograph* mg;
1850  Breconstruction* rec;
1851 
1852  for ( field = project->field; field; field = field->next )
1853  for ( mg = field->mg; mg; mg = mg->next ) {
1854  if ( !mg->ctf ) mg->ctf = new CTFparam;
1855  mg->ctf->envelope(1, -M_PI*M_PI*mg->ctf->alpha()*mg->ctf->alpha()*mg->ctf->defocus_average()*mg->ctf->defocus_average());
1856  }
1857 
1858  for ( rec = project->rec; rec; rec = rec->next ) {
1859  if ( !rec->ctf ) rec->ctf = new CTFparam;
1860  rec->ctf->envelope(1, -M_PI*M_PI*rec->ctf->alpha()*rec->ctf->alpha()*rec->ctf->defocus_average()*rec->ctf->defocus_average());
1861  }
1862 
1863  return 0;
1864 }
1865 
1873 {
1874  if ( !project ) return 0;
1875 
1876  Bfield* field;
1877  Bmicrograph* mg;
1878  Breconstruction* rec;
1879 
1880  for ( field = project->field; field; field = field->next ) {
1881  for ( mg = field->mg; mg; mg = mg->next ) {
1882  if ( !mg->ctf ) mg->ctf = new CTFparam;
1883  mg->ctf->baseline_type(type);
1884  }
1885  }
1886 
1887  for ( rec = project->rec; rec; rec = rec->next ) {
1888  if ( !rec->ctf ) rec->ctf = new CTFparam;
1889  rec->ctf->baseline_type(type);
1890  }
1891 
1892  return 0;
1893 }
1894 
1902 int project_set_baseline(Bproject* project, int type, double* coeff)
1903 {
1904  if ( !project ) return 0;
1905 
1906  Bfield* field;
1907  Bmicrograph* mg;
1908  Breconstruction* rec;
1909 
1910  for ( field = project->field; field; field = field->next ) {
1911  for ( mg = field->mg; mg; mg = mg->next ) {
1912  if ( !mg->ctf ) mg->ctf = new CTFparam;
1913  mg->ctf->baseline_type(type);
1914  mg->ctf->baseline(coeff);
1915  }
1916  }
1917 
1918  for ( rec = project->rec; rec; rec = rec->next ) {
1919  if ( !rec->ctf ) rec->ctf = new CTFparam;
1920  rec->ctf->baseline_type(type);
1921  rec->ctf->baseline(coeff);
1922  }
1923 
1924  return 0;
1925 }
1926 
1933 {
1934  if ( !project ) return 0;
1935 
1936  Bfield* field;
1937  Bmicrograph* mg;
1938  Breconstruction* rec;
1939 
1940  if ( verbose & VERB_DEBUG )
1941  cout << "DEBUG project_update_first_zero: start" << endl;
1942 
1943  for ( field = project->field; field; field = field->next ) {
1944  for ( mg = field->mg; mg; mg = mg->next ) if ( mg->ctf ) {
1945  if ( mg->ctf && mg->ctf->defocus_average() > 0 && mg->ctf->volt() > 0 && mg->ctf->Cs() > 0 )
1946  mg->ctf->zero(1);
1947  }
1948  }
1949 
1950  for ( rec = project->rec; rec; rec = rec->next ) {
1951  if ( rec->ctf && rec->ctf->defocus_average() > 0 && rec->ctf->volt() > 0 && rec->ctf->Cs() > 0 )
1952  rec->ctf->zero(1);
1953  }
1954 
1955  if ( verbose & VERB_DEBUG )
1956  cout << "DEBUG project_update_first_zero: done" << endl;
1957 
1958  return 0;
1959 }
1960 
1968 {
1969  if ( !project ) return 0;
1970 
1971  Bfield* field;
1972  Bmicrograph* mg;
1973  Bimage* p;
1974  Bimage* prad;
1975  Bstring basename = filename.pre_rev('.');
1976  Bstring psname;
1977 
1978  for ( field = project->field; field; field = field->next ) {
1979  for ( mg = field->mg; mg; mg = mg->next ) if ( mg->ctf ) {
1980  p = read_img(mg->fps, 1, 0);
1981  if ( p ) {
1982  psname = basename + "_" + mg->fps.pre_rev('.') + ".ps";
1983  prad = img_ctf_radial_average(p, 0, *mg->ctf);
1984  ps_ctf_plot(prad->sizeX(), (double *)prad->data_pointer(),
1985  1.0/p->real_size()[0], mg->ctf, psname);
1986  delete prad;
1987  }
1988  }
1989  }
1990 
1991  return 0;
1992 }
1993 
rec_ctf
int rec_ctf(Breconstruction *rec, int action, double lores, double hires, double wiener, DataType datatype, Bstring &newname, int flags)
Definition: mg_ctf.cpp:1046
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
project_ctf_average
int project_ctf_average(Bproject *project, Bstring &psname)
Calculates or fits CTF curves to multiple power spectra.
Definition: mg_ctf.cpp:1259
Bimage::real_size
Vector3< double > real_size()
Definition: Bimage.h:500
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
Breconstruction::fpart
Bstring fpart
Definition: mg_processing.h:291
ps_plot
int ps_plot(Bstring filename, Bplot *plot)
Generates postscript graphs.
Definition: ps_plot.cpp:129
project_set_volts
int project_set_volts(Bproject *project, double volts)
Sets the acceleration voltage of all the micrographs.
Definition: mg_ctf.cpp:1579
CTFparam::zero
double zero(int i)
Definition: ctf.h:228
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:347
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Baxis::flags
long flags()
Definition: Bplot.h:38
CTFparam::calc_envelope
double calc_envelope(double s)
Definition: ctf.h:199
Bproject::select
int select
Definition: mg_processing.h:342
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
CTFparam::update
int update(CTFparam *ctf)
Definition: ctf.h:81
Breconstruction::voxel_size
Vector3< double > voxel_size
Definition: mg_processing.h:295
Bstring::extension
Bstring extension()
Definition: Bstring.cpp:523
mg_ps_name
int mg_ps_name(Bmicrograph *mg, int img_num, Bstring &path, Bstring &newname, Bstring insert)
Definition: mg_ctf.cpp:490
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
base
string base(string &filename)
Definition: string_util.cpp:28
project_update_first_zero
int project_update_first_zero(Bproject *project)
Updates the first zero from the defocus average for all the micrographs.
Definition: mg_ctf.cpp:1932
mg_ctf_prepare
Bimage * mg_ctf_prepare(Bmicrograph *mg, int action, double lores, double hires, Vector3< long > tile_size, double def_start, double def_end, double def_inc, int flags)
Definition: mg_ctf.cpp:519
img_ctf_fit
double img_ctf_fit(Bimage *p, long n, CTFparam &em_ctf, double lores, double hires, double def_start=1e3, double def_end=2e5, double def_inc=1e3, int flag=0)
Determines the CTF parameters from a power spectrum.
Definition: mg_ctf_fit.cpp:210
CTFparam::objective_aperture
double objective_aperture()
Definition: ctf.h:102
Bimage::copy
Bimage * copy()
Copies the header information and data of an image into a new image structure.
Definition: Bimage.cpp:2702
Breconstruction::ctf
CTFparam * ctf
Definition: mg_processing.h:306
Breconstruction::fom
double fom
Definition: mg_processing.h:305
CTFparam::astigmatism_angle
double astigmatism_angle()
Definition: ctf.h:110
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
Bparticle::ast
double ast
Definition: mg_processing.h:98
project_set_envelope
int project_set_envelope(Bproject *project, int type, double *coeff)
Sets the envelope equations of all the micrographs.
Definition: mg_ctf.cpp:1809
Bmicrograph::img_num
int img_num
Definition: mg_processing.h:212
project_set_Cs
int project_set_Cs(Bproject *project, double Cs)
Sets the spherical aberation constant of all the micrographs.
Definition: mg_ctf.cpp:1609
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Bparticle::mg
Bmicrograph * mg
Definition: mg_processing.h:105
Bmicrograph::sampling
double sampling
Definition: mg_processing.h:214
Bplot
Definition: Bplot.h:146
Bmicrograph::part
Bparticle * part
Definition: mg_processing.h:242
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
Bimage::transform
int transform(Vector3< double > scale, Vector3< double > origin, Vector3< double > translate, Matrix3 mat, int fill_type=0, double fill=0)
Transforms an image by translation, rotation, scaling and skewing, in place.
Definition: Bimage_transform.cpp:37
img_ttf_apply_one
int img_ttf_apply_one(Bimage *p, long nn, CTFparam ctf, int action, double wiener, double def, double res_lo, double res_hi, Vector3< long > psize, fft_plan planf, fft_plan planb)
Definition: mg_ctf.cpp:315
verbose
int verbose
Bmicrograph::fpart
Bstring fpart
Definition: mg_processing.h:208
Bcolumn::type
long type()
Definition: Bplot.h:66
Bfield
Definition: mg_processing.h:260
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
Bcolumn::axis
long axis()
Definition: Bplot.h:68
ps_plot.h
Header file for postscript output functions.
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bmicrograph::fframe
Bstring fframe
Definition: mg_processing.h:207
project_set_coherence_envelope
int project_set_coherence_envelope(Bproject *project)
Replaces envelope equations with those based on partial coherence in all micrographs.
Definition: mg_ctf.cpp:1844
CTFparam::calc_baseline
double calc_baseline(double s)
Definition: ctf.h:171
project_set_slit_width
int project_set_slit_width(Bproject *project, double slit)
Sets the energy filter slit width of all the micrographs.
Definition: mg_ctf.cpp:1724
planf
fft_plan planf
Definition: bico.cpp:24
Bpage::axis
Baxis & axis(long id)
Definition: Bplot.h:117
Breconstruction::origin
Vector3< double > origin
Definition: mg_processing.h:296
Bpage::title
Bstring & title()
Definition: Bplot.h:100
CTFparam::calculate
double calculate(double s2, double angle)
Definition: ctf.h:159
project_set_focal_length
int project_set_focal_length(Bproject *project, double focal_length)
Sets the focal length of all the micrographs.
Definition: mg_ctf.cpp:1666
project_CTF_to_part
int project_CTF_to_part(Bproject *project)
Transfers CTF parameters from micrographs to particles.
Definition: mg_ctf.cpp:1422
Bimage::images
long images()
Definition: Bimage.h:504
project
Bproject * project
Definition: bshow.cpp:33
project_set_baseline_type
int project_set_baseline_type(Bproject *project, int type)
Sets the baseline type of all the micrographs.
Definition: mg_ctf.cpp:1872
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
ps_ctf_plot
int ps_ctf_plot(Bstring &filename, CTFparam &em_ctf, size_t n, double freq_step)
Generates a postscript plot from CTF parameters for defocus and the envelope.
Definition: ps_ctf_plot.cpp:71
Bimage::multiply
void multiply(long j, Complex< double > cv)
Definition: Bimage.h:325
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
ps_ctf_plot.h
Header file for postscript output functions.
Bimage::add
void add(long j, double v)
Definition: Bimage.h:321
project_set_defocus
int project_set_defocus(Bproject *project, double def_avg, double def_dev, double ast_angle)
Sets the defocus values of all the micrographs.
Definition: mg_ctf.cpp:1463
Bparticle::fpart
Bstring fpart
Definition: mg_processing.h:92
insert
string insert(string &filename, string ins)
Definition: string_util.cpp:48
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
Bimage::slices_to_images
int slices_to_images()
Changes the slices in a 3D image into a set of 2D images.
Definition: Bimage.cpp:2074
Bimage::fft
int fft()
Definition: Bimage.h:830
Bimage::image_size
long image_size()
Definition: Bimage.h:290
project_set_aperture
int project_set_aperture(Bproject *project, double aperture)
Sets the objective aperture of all the micrographs.
Definition: mg_ctf.cpp:1695
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
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
mg_ctf.h
Functions for CTF (contrast transfer function) processing.
CTFparam::show
void show()
Definition: ctf.h:242
Bmicrograph::magnification
double magnification
Definition: mg_processing.h:213
Bmicrograph::tilt_axis
double tilt_axis
Definition: mg_processing.h:219
Bpage::add_text
void add_text(Bstring &s)
Definition: Bplot.h:122
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Bpage::column
Bcolumn & column(long i)
Definition: Bplot.h:110
CTFparam::envelope_equation
Bstring envelope_equation()
Generates an envelope equation string from 4 coefficients.
Definition: ctf.cpp:211
Bimage::extract_tiles
Bimage * extract_tiles(long nn, vector< Vector3< long >> &coords, Vector3< long > tile_size)
Extracts a set of tiles at specified positions from an image into a new image.
Definition: Bimage_extract.cpp:331
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
Bparticle::sel
long sel
Definition: mg_processing.h:104
JSvalue
Definition: json.h:53
Bparticle::pixel_size
Vector3< double > pixel_size
Definition: mg_processing.h:99
Bimage::replace
int replace(Bimage *img)
Replaces the data with that from the given image.
Definition: Bimage.cpp:2830
Bplot::title
Bstring & title()
Definition: Bplot.h:194
CTFparam::volt
double volt()
Definition: ctf.h:87
project_powerspectrum_average
Bimage * project_powerspectrum_average(Bproject *project, double deftarget)
Averages multiple power spectra based on defocus estimates.
Definition: mg_ctf.cpp:1332
project_set_amp_shift
int project_set_amp_shift(Bproject *project, double amp_shift)
Sets the amplitude contribution of all the micrographs.
Definition: mg_ctf.cpp:1638
Bimage::complex
Complex< double > complex(long j)
Returns a complex value at the given index.
Definition: Bimage.cpp:1027
CTFparam::defocus_deviation
double defocus_deviation()
Definition: ctf.h:108
Baxis::min
double min()
Definition: Bplot.h:30
Bimage::assemble_tiles
int assemble_tiles(Bimage *pt, int flag=0)
Assembles overlapping tiles into this image.
Definition: Bimage_combine.cpp:433
CTFparam::lambda
double lambda()
Definition: ctf.h:145
Bparticle::loc
Vector3< double > loc
Definition: mg_processing.h:100
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
Baxis::label
Bstring & label()
Definition: Bplot.h:28
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.
CTFparam::slit_width
double slit_width()
Definition: ctf.h:104
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
CTFparam::delta_phi
double delta_phi(double s2, double angle)
Definition: ctf.h:155
Bmicrograph::box_size
Vector3< long > box_size
Definition: mg_processing.h:233
project_set_baseline
int project_set_baseline(Bproject *project, int type, double *coeff)
Sets the baseline equations of all the micrographs.
Definition: mg_ctf.cpp:1902
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
Breconstruction::select
long select
Definition: mg_processing.h:286
project_ctf
int project_ctf(Bproject *project, int action, double lores, double hires, Vector3< long > tile_size, double wiener, DataType datatype, Bstring &partpath, Bstring &newname, int flags)
Calculates or fits CTF curves to multiple power spectra.
Definition: mg_ctf.cpp:1119
Bparticle::def
double def
Definition: mg_processing.h:96
mg_ctf
int mg_ctf(Bmicrograph *mg, Bimage *pmg, int action, double lores, double hires, double wiener, Vector3< long > tile_size, DataType datatype, Bstring &newname, int flags)
Definition: mg_ctf.cpp:972
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
Bmicrograph::fmg
Bstring fmg
Definition: mg_processing.h:206
Bfield::select
long select
Definition: mg_processing.h:265
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
CTFparam::baseline
vector< double > & baseline()
Definition: ctf.h:114
CTFparam
Definition: ctf.h:28
Vector3::volume
double volume()
Definition: Vector3.h:237
rwimg.h
Header file for 2D and 3D image I/O.
Bmicrograph::ctf
CTFparam * ctf
Definition: mg_processing.h:241
Bparticle::dev
double dev
Definition: mg_processing.h:97
Bfield::next
Bfield * next
Definition: mg_processing.h:261
img_wave_aberration
Bimage * img_wave_aberration(CTFparam cp, Vector3< long > size, Vector3< double > sam)
Calculates a wave aberration function.
Definition: mg_ctf.cpp:174
Bimage::copy_header
Bimage * copy_header()
Definition: Bimage.h:658
project_defocus_range
JSvalue project_defocus_range(Bproject *project)
Determines the minimum and maximum defocus values.
Definition: mg_ctf.cpp:1211
CTFparam::envelope_type
long envelope_type()
Definition: ctf.h:119
JSobject
@ JSobject
Definition: json.h:26
mg_processing.h
Header file for micrograph processing.
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
CTFparam::defocus_average
double defocus_average()
Definition: ctf.h:106
Bparticle::next
Bparticle * next
Definition: mg_processing.h:91
Bimage::fill
void fill(double v)
Definition: Bimage.h:706
Bimage::powerspectrum_tiled_and_tilted
Bimage * powerspectrum_tiled_and_tilted(Vector3< long > tile_size, double tilt_axis, double tilt_angle, double tilt_offset, double defocus, double iCL2, int flags=0)
Prepares a tiled powerspectrum from a tilted image for determining CTF parameters.
Definition: Bimage_powerspectrum.cpp:356
Bmicrograph::select
long select
Definition: mg_processing.h:204
electron_wavelength
double electron_wavelength(double volt)
Calculates the wavelength in angstrom from the acceleration voltage.
Definition: ctf.cpp:456
Bmicrograph::fps
Bstring fps
Definition: mg_processing.h:211
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bimage::invert
void invert()
Inverts the data in the image.
Definition: Bimage.cpp:3111
Breconstruction::frec
Bstring frec
Definition: mg_processing.h:290
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:178
Vector3::min
Type min()
Definition: Vector3.h:169
Bmicrograph::wri
double wri
Definition: mg_processing.h:218
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
CTFparam::alpha
double alpha()
Definition: ctf.h:95
project_plot_ctf
int project_plot_ctf(Bproject *project, Bstring &filename)
Plots the CTF curves.
Definition: mg_ctf.cpp:1967
CTFparam::focal_length
double focal_length()
Definition: ctf.h:89
planb
fft_plan planb
Definition: bico.cpp:24
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
CTFparam::amp_shift
double amp_shift()
Definition: ctf.h:99
Bimage::powerspectrum_tiled
Bimage * powerspectrum_tiled(long img_num, Vector3< long > tile_size, int flags=0)
Prepares a tiled power spectrum from an image for determining CTF parameters.
Definition: Bimage_powerspectrum.cpp:97
img_ctf_calculate
Bimage * img_ctf_calculate(CTFparam cp, int action, double wiener, Vector3< long > size, Vector3< double > sam, double lores, double hires)
Calculates a CTF complex image.
Definition: mg_ctf.cpp:45
mg_ctf_fit.h
Header file for CTF (contrast transfer function) functions.
Bcolumn::label
Bstring & label()
Definition: Bplot.h:63
rec_ctf_prepare
int rec_ctf_prepare(Breconstruction *rec, int action, double lores, double hires, Vector3< long > tile_size, double def_start, double def_end, double def_inc, Bstring &newname, int flags)
Definition: mg_ctf.cpp:619
Bplot::page
Bpage & page(long i)
Definition: Bplot.h:216
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
img_ctf_apply
int img_ctf_apply(Bimage *p, CTFparam em_ctf, int action, double wiener, double lores, double hires)
Applies or corrects for the contrast transfer function (CTF).
Definition: mg_ctf.cpp:237
project_set_astigmatism
int project_set_astigmatism(Bproject *project, double def_dev, double ast_angle)
Sets the defocus deviation and astigmatism angle of all the micrographs.
Definition: mg_ctf.cpp:1514
Breconstruction::fps
Bstring fps
Definition: mg_processing.h:294
ctf_from_json
CTFparam ctf_from_json(JSvalue &js)
Converts a JSON object list to microscope parameters.
Definition: ctf.cpp:423
CTFparam::baseline_equation
Bstring baseline_equation()
Generates a baseline equation string from 4 to 10 coefficients.
Definition: ctf.cpp:167
Bcolumn::number
long number()
Definition: Bplot.h:61
img_ctf_radial_average
Bimage * img_ctf_radial_average(Bimage *p, long n, CTFparam &em_ctf)
Calculates the power spectrum radial average corrected for astigmatism.
Definition: mg_ctf_fit.cpp:46
Float
@ Float
Definition: utilities.h:161
Baxis::max
double max()
Definition: Bplot.h:32
project_merge_CTF_parameters
int project_merge_CTF_parameters(Bproject *project, Bproject *ctfproject)
Puts CTF parameters from one project into another.
Definition: mg_ctf.cpp:1383
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
CTFparam::envelope
vector< double > & envelope()
Definition: ctf.h:121
part_ctf
int part_ctf(Bparticle *partlist, int action, double lores, double hires, double wiener, DataType datatype, Bstring &partpath, Bstring &newname, int flags)
Definition: mg_ctf.cpp:819
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:202
Bparticle::rec
Breconstruction * rec
Definition: mg_processing.h:106
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
Bparticle::id
int id
Definition: mg_processing.h:93
NoTransform
@ NoTransform
Definition: Bimage.h:41
img_ctf_apply_to_proj
int img_ctf_apply_to_proj(Bimage *proj, CTFparam em_ctf, double defocus, double res_lo, double res_hi, fft_plan planf_2D, fft_plan planb_2D)
Applies a CTF to a projection image.
Definition: mg_ctf.cpp:354
project_update_ctf
int project_update_ctf(Bproject *project, JSvalue &jsctf)
Sets the acceleration voltage of all the micrographs.
Definition: mg_ctf.cpp:1548
Bpage::columns
void columns(long ncol)
Definition: Bplot.h:105
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
project_set_envelope_type
int project_set_envelope_type(Bproject *project, int type)
Sets the envelope type of all the micrographs.
Definition: mg_ctf.cpp:1780
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
project_ctf_prepare
int project_ctf_prepare(Bproject *project, int action, double lores, double hires, Vector3< long > tile_size, double def_start, double def_end, double def_inc, Bstring &path, Bstring &newname, int flags)
Calculates power spectra and optionally fits CTF curves.
Definition: mg_ctf.cpp:719
Bimage::filter_extremes
int filter_extremes()
Filters the extremes out of a micrograph image.
Definition: Bimage_segment.cpp:620
Bimage::resize
int resize(Vector3< long > nusize, Vector3< long > translate, int fill_type=0, double fill=0)
Resizes without interpolation or rescaling.
Definition: Bimage_resize.cpp:28
CTFparam::Cs
double Cs()
Definition: ctf.h:91
project_set_alpha
int project_set_alpha(Bproject *project, double alpha)
Sets the beam source size/divergence angle of all the micrographs.
Definition: mg_ctf.cpp:1752
CTFparam::baseline_type
long baseline_type()
Definition: ctf.h:112
Breconstruction::next
Breconstruction * next
Definition: mg_processing.h:284
Vector3::max
Type max()
Definition: Vector3.h:175