Bsoft  2.1.0
Bernard's software package
blocfilt.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 
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17 
18 // Usage assistence
19 const char* use[] = {
20 " ",
21 "Usage: blocfilt [options] input.img output.img",
22 "----------------------------------------------",
23 " ",
24 "Actions:",
25 "-box 20 Kernel size for filtering according to local resolution (voxels).",
26 " ",
27 "Parameters:",
28 "-verbose 7 Verbosity of output.",
29 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
30 "-origin 0.8,-10,15.7 Set the origin (default from input image).",
31 "-symmetry D5 Point group symmetry.",
32 "-edge 10 Pixels at the edge of the whole map to exclude from analysis (default: half the size of the resolution box).",
33 " ",
34 "Input:",
35 "-Resolution locres.tif Resolution map to use for local filtering.",
36 "-Mask mask.tif,2 Mask file to use for limiting the analysis to a defined region",
37 " and level value to use (optional, default: all but zero).",
38 " ",
39 NULL
40 };
41 
42 int main(int argc, char **argv)
43 {
44  // Initialize variables
45  DataType nudatatype(Unknown_Type); // Conversion to new type
46  Vector3<double> origin; // New image origin
47  int set_origin(0); // Flag to set origin
48  Vector3<double> sam; // Units for the three axes (A/pixel)
49  int boxsize(0); // Linear size of box designated by user
50  Vector3<long> vedge(-1,-1,-1); // Edge size to exclude
51  Bstring maskfile; // Mask file name
52  Bstring locresfile; // Local resolution file name
53  int mask_level(-1); // Mask level (-1 => all but zero)
54  int setimg(-1); // Select all images
55  Bstring symmetry_string; // Point group
56 
57  int optind;
58  Boption* option = get_option_list(use, argc, argv, optind);
59  Boption* curropt;
60  for ( curropt = option; curropt; curropt = curropt->next ) {
61  if ( curropt->tag == "box" )
62  if ( ( boxsize = curropt->value.integer() ) < 1 )
63  cerr << "-box: A resolution box size must be specified!" << endl;
64  if ( curropt->tag == "edge" )
65  vedge = curropt->size();
66  if ( curropt->tag == "sampling" )
67  sam = curropt->scale();
68  if ( curropt->tag == "origin" ) {
69  if ( curropt->value[0] == 'c' ) {
70  set_origin = 2;
71  } else {
72  origin = curropt->origin();
73  set_origin = 1;
74  }
75  }
76  if ( curropt->tag == "symmetry" )
77  symmetry_string = curropt->symmetry_string();
78  if ( curropt->tag == "Mask" ) {
79  maskfile = curropt->filename();
80  if ( maskfile.contains(",") ) {
81  mask_level = (maskfile.post(',')).integer();
82  maskfile = maskfile.pre(',');
83  }
84  }
85  if ( curropt->tag == "Resolution" )
86  locresfile = curropt->filename();
87  }
88  option_kill(option);
89 
90  double ti = timer_start();
91 
92 
93  Bimage* p1 = read_img(argv[optind++], 1, setimg);
94  if ( p1 == NULL ) bexit(-1);
95 
96 
97  if ( sam[0] > 0 && sam[1] > 0 && sam[2] > 0 ) {
98  p1->sampling(sam);
99  }
100 
101  if ( set_origin ) {
102  if ( set_origin == 2 ) p1->origin(p1->default_origin());
103  else p1->origin(origin);
104  }
105 
106  if ( ! locresfile.length()) {
107  cerr << "ERROR: Local resolution file needed! (option -Resolution)" << endl;
108  bexit(1);
109  }
110 
111  if ( ! boxsize ) {
112  cerr << "ERROR: Box size value needed! (option -box)" << endl;
113  bexit(1);
114  }
115 
116  Bimage* p = NULL;
117  Bimage* psymmask = NULL;
118  Bimage* pmask = NULL;
119  Bimage* resmap = NULL;
120  Bsymmetry* sym = NULL;
121 
122  if ( maskfile.length() ) pmask = read_img(maskfile, 1, -1);
123  if ( locresfile.length() ) resmap = read_img(locresfile, 1, -1);
124 
125  if ( symmetry_string.length() ) {
126  sym = init_point_group_symmetry(symmetry_string);
127  if ( sym->point > 101 ) {
128  psymmask = p1->levelmask_asymmetric_units(sym, 1);
129  maskfile = p1->file_name();
130  maskfile = maskfile.pre_rev('.') + "_levelmask." + maskfile.post_rev('.');
131  psymmask->file_name(maskfile.str());
132  psymmask->mask_dilate(2);
133  if ( pmask ) {
134  pmask->multiply(psymmask);
135  delete psymmask;
136  } else {
137  pmask = psymmask;
138  }
139  psymmask = NULL;
140  } else {
141  kill_symmetry(sym);
142  sym = NULL;
143  }
144  }
145 
146 // write_img("locres_mask.map", pmask);
147 
148  p = p1->local_filter(pmask, mask_level, resmap, boxsize, vedge);
149  if ( !p ) {
150  cerr << "Error: No local-filtered map produced!" << endl;
151  bexit(-1);
152  }
153 
154  if ( sym ) {
156  kill_symmetry(sym);
157  }
158 
159  // Write an output file if a file name is given
160  if ( optind < argc ) {
161  p->change_type(nudatatype);
162  write_img(argv[optind], p, 0);
163  }
164 
165  if (pmask) delete pmask;
166  if (resmap) delete resmap;
167  delete p1;
168  delete p;
169 
170  if ( verbose & VERB_TIME )
171  timer_report(ti);
172 
173  bexit(0);
174 }
175 
176 
Boption
A class for a command line input option.
Definition: options.h:26
verbose
int verbose
Boption::scale
Vector3< double > scale()
Definition: options.h:190
Bimage::mask_dilate
long mask_dilate(long times=1)
Dilates a binary mask.
Definition: Bimage_mask.cpp:423
Bsymmetry::point
int point
Definition: symmetry.h:39
Bstring
Definition: Bstring.h:29
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:436
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::next
Boption * next
Definition: options.h:28
Vector3< double >
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
options.h
Header file for option handlers.
Bstring::post
Bstring post(char c)
Definition: Bstring.cpp:402
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Bsymmetry
Definition: symmetry.h:38
Bimage::local_filter
Bimage * local_filter(Bimage *pmask, int mask_level, Bimage *resmap, int size, Vector3< long > vedge)
Applies a local resolution filter to a map.
Definition: Bimage_resolution.cpp:782
Boption::tag
Bstring tag
Definition: options.h:29
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
Bimage::multiply
void multiply(long j, Complex< double > cv)
Definition: Bimage.h:325
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
use
const char * use[]
Definition: blocfilt.cpp:19
Bstring::str
string str()
Definition: Bstring.h:42
Bimage::replicate_asymmetric_unit
int replicate_asymmetric_unit(Bsymmetry *sym)
Calculates a full map from one asymmetric unit.
Definition: Bimage_symmetry.cpp:971
Bstring::pre
Bstring pre(char c)
Definition: Bstring.cpp:389
Boption::value
Bstring value
Definition: options.h:30
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
Bimage::levelmask_asymmetric_units
Bimage * levelmask_asymmetric_units(Bsymmetry *sym, int index)
Calculates a multi-level mask to indicate asymmetric units.
Definition: Bimage_symmetry.cpp:898
Boption::symmetry_string
Bstring symmetry_string()
Definition: options.h:233
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
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
utilities.h
Header file for general utilities.
Boption::size
Vector3< long > size()
Definition: options.h:163
Bstring::length
long length() const
Definition: Bstring.cpp:263
init_point_group_symmetry
Bsymmetry * init_point_group_symmetry(Bstring symmetry_string)
Sets up a symmetry structure from a string identifier.
Definition: symmetry.cpp:39
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
rwimg.h
Header file for 2D and 3D image I/O.
kill_symmetry
int kill_symmetry(Bsymmetry *sym)
Deallocates a symmetry structure.
Definition: symmetry.cpp:207
Boption::filename
Bstring filename()
Definition: options.h:85
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
main
int main(int argc, char **argv)
Definition: blocfilt.cpp:42
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592