Bsoft  2.1.0
Bernard's software package
bcyl.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "utilities.h"
11 #include "options.h"
12 #include "timer.h"
13 
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16 
18 
19 // Usage assistance
20 const char* use[] = {
21 " ",
22 "Usage: bcyl [options] input.img output.img",
23 "------------------------------------------",
24 "Unwraps an image to its cylindrical projection.",
25 " ",
26 "Actions:",
27 "-symmetrize Symmetrizes a map cylindrically around the origin.",
28 "-unwrap 1.5 Unwraps an image to its cylindrical projection with the given angle.",
29 "-autocorrelate Autocorrelates the unwrapped map and outputs the autocorrelation map.",
30 "-rescale -0.1,5.2 Rescale data to average and standard deviation (after -truncate).",
31 "-shells Calculate cylindrical shells projecting along the y-axis.",
32 "-reslice yzx Reslice (switch axes) after unwrapping.",
33 " ",
34 "Parameters:",
35 "-verbose 7 Verbosity of output.",
36 "-datatype u Force writing of a new data type.",
37 "-origin 0,-10.5,30 Set the origin.",
38 "-resolution 4.5,130 Resolution range for correlation (default 0 - 1e6 angstrom).",
39 " ",
40 NULL
41 };
42 
43 int main(int argc, char **argv)
44 {
45  // Initialize variables
46  int symmetrize(0); // Flag to symmetrize cylindrically
47  double unwrap_angle(0); // Angle for cylindrical unwrapping
48  int autocorrelate(0); // Flag to autocorrelate the unwrapped map
49  int shells(0); // Flag to calculate cylindrical shells
50  int setrescale(0);
51  double nuavg(0), nustd(0); // Rescaling to average and stdev
52  int setreslice(0);
53  Bstring order("xyz");
54  DataType nudatatype = Unknown_Type; // Conversion to new type
55  Vector3<double> origin {0,0,0}; // Origin
56  int set_origin(0); // Flag to set origin
57  double hires(0), lores(1e6); // Limiting resolution range (hires must be > 0 to be set)
58 
59  int optind;
60  Boption* option = get_option_list(use, argc, argv, optind);
61  Boption* curropt;
62  for ( curropt = option; curropt; curropt = curropt->next ) {
63  if ( curropt->tag == "symmetrize" ) symmetrize = 1;
64  if ( curropt->tag == "unwrap" ) {
65  if ( ( unwrap_angle = curropt->value.real() ) < 0.1 )
66  cerr << "-unwrap: An angle must be specified!" << endl;
67  else
68  unwrap_angle *= M_PI/180.0;
69  }
70  if ( curropt->tag == "autocorrelate" ) autocorrelate = 1;
71  if ( curropt->tag == "shells" ) shells = 1;
72  if ( curropt->tag == "rescale" ) {
73  if ( curropt->values(nuavg, nustd) < 2 )
74  cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
75  else
76  setrescale = 1;
77  if ( nustd <= 0 ) setrescale = 0;
78  }
79  if ( curropt->tag == "reslice" ) {
80  if ( curropt->value.length() < 3 )
81  cerr << "-reslice: The order of x, y and z must be specified!" << endl;
82  else {
83  order = curropt->value;
84  setreslice = 1;
85  }
86  }
87  if ( curropt->tag == "datatype" )
88  nudatatype = curropt->datatype();
89  if ( curropt->tag == "origin" ) {
90  if ( curropt->value[0] == 'c' ) {
91  set_origin = 2;
92  } else {
93  origin = curropt->origin();
94  set_origin = 1;
95  }
96  }
97  if ( curropt->tag == "resolution" )
98  if ( curropt->values(hires, lores) < 1 )
99  cerr << "-resolution: A high resolution limit must be specified" << endl;
100  }
101  option_kill(option);
102 
103  double ti = timer_start();
104 
105  // Read image file
106  Bimage* p = read_img(argv[optind++], 1, -1);
107  if ( p == NULL ) bexit(-1);
108 
109  if ( set_origin ) {
110  if ( set_origin == 2 ) p->origin(p->default_origin());
111  else p->origin(origin);
112  }
113 
114  if ( symmetrize ) p->symmetrize_cylinder();
115 
116  Bimage* pcyl = NULL;
117  if ( unwrap_angle > 0 ) {
118  pcyl = p->cartesian_to_cylindrical(p->sizeX()/2, TWOPI/unwrap_angle);
119  delete p;
120  p = pcyl;
121  if ( autocorrelate ) {
122  p->auto_correlate(hires, lores);
124  }
125  }
126 
127  if ( shells ) p->cylindrical_shells();
128 
129  if ( optind < argc ) {
130  if ( setrescale ) p->rescale_to_avg_std(nuavg, nustd);
131  if ( setreslice ) p->reslice(order);
132 
133  p->change_type(nudatatype);
134 
135  // Copy the command line to the label field of the image structure
136 
137  write_img(argv[optind], p, 0);
138  }
139 
140  delete p;
141  order = 0;
142 
143  if ( verbose & VERB_TIME )
144  timer_report(ti);
145 
146  bexit(0);
147 }
148 
150 {
151  long i, x, y(0), z, z1, z2, zm, n, dz(10);
152  double a, v, cc_max;
153 
154  for ( n=0; n<p->images(); n++ ) {
155  z1 = 0; z2 = dz;
156  cout << "Image " << n+1 << ":" << endl << "Angle\tz\tCC" << endl;
157  for ( x=0; x<p->sizeX(); x++ ) {
158  a = x*TWOPI/p->sizeX();
159  for ( z=z1, zm=0, cc_max = p->minimum(); z<z2; z++ ) {
160  i = p->index(0, x, y, z, n);
161  v = (*p)[i];
162  if ( cc_max < v ) {
163  cc_max = v;
164  zm = z;
165  }
166  }
167  z1 = (zm > dz)? zm - dz: 0;
168  z2 = zm + dz;
169  cout << a*180.0/M_PI << tab << zm*p->sampling(0)[2] << tab << cc_max << endl;
170  }
171  cout << endl;
172  }
173 
174  return 0;
175 }
176 
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::symmetrize_cylinder
Bimage * symmetrize_cylinder(int flag)
Calculates a cylindrically symmetrized map.
Definition: Bimage_helix.cpp:524
Bimage::reslice
void reslice(const char *order)
Definition: Bimage.h:717
Bstring
Definition: Bstring.h:29
Bimage::cylindrical_shells
int cylindrical_shells()
Calculates an image with slices representing cylindrical shell projections.
Definition: Bimage_polar.cpp:928
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Vector3< double >
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
Bimage::default_origin
Vector3< double > default_origin()
Definition: Bimage.h:592
get_option_list
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
Definition: options.cpp:40
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
Boption::origin
Vector3< double > origin()
Definition: options.h:180
main
int main(int argc, char **argv)
Definition: bcyl.cpp:43
Bimage::cartesian_to_cylindrical
Bimage * cartesian_to_cylindrical(long nannuli, long nphi, int flag=0)
Calculates an image with cylindrical coordinates.
Definition: Bimage_polar.cpp:592
Bimage::images
long images()
Definition: Bimage.h:504
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
img_plot_max_vs_angle
int img_plot_max_vs_angle(Bimage *p)
Definition: bcyl.cpp:149
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
verbose
int verbose
Boption::value
Bstring value
Definition: options.h:30
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
use
const char * use[]
Definition: bcyl.cpp:20
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Bstring::length
long length() const
Definition: Bstring.cpp:263
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
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Bimage::index
long index(long nx, long ny) const
Definition: Bimage.h:396
rwimg.h
Header file for 2D and 3D image I/O.
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
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::auto_correlate
int auto_correlate(double hires, double lores)
Calculates an autocorrelation map by Fast Fourier transformation.
Definition: Bimage_correlate.cpp:265
TWOPI
#define TWOPI
Definition: utilities.h:72
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