ld_purify.c
Go to the documentation of this file.
1 #include <gsl/gsl_rng.h>
2 #include <gsl/gsl_randist.h>
3 
4 #include <math.h>
5 #include <libgen.h>
6 
7 #include <options/options.h>
8 #include "../csm/csm_all.h"
9 
10 void purify(LDP ld, double threshold_min, double threshold_max);
11 
12 
15  const char* file_input;
16  const char* file_output;
17 };
18 
19 
20 int main(int argc, const char * argv[]) {
21  sm_set_program_name(argv[0]);
22 
23 
24  options_banner("ld_purify: Makes sure that the file format is valid. \n * Sets valid=0 if reading is outside interval ");
25 
26  struct ld_purify_params p;
27 
28  struct option* ops = options_allocate(20);
29  options_double(ops, "threshold_min", &p.threshold_min, 0.01,
30  "Sets valid=0 if readings are less than this threshold.");
31  options_double(ops, "threshold_max", &p.threshold_max, 79.0,
32  "Sets valid=0 if readings are more than this threshold.");
33 
34  options_string(ops, "in", &p.file_input, "stdin", "Input file ");
35  options_string(ops, "out", &p.file_output, "stdout", "Output file ");
36 
37 
38  if(!options_parse_args(ops, argc, argv)) {
39  options_print_help(ops, stderr);
40  return -1;
41  }
42 
43  FILE * in = open_file_for_reading(p.file_input);
44  if(!in) return -3;
45 
46  FILE * out = open_file_for_writing(p.file_output);
47  if(!out) return -2;
48 
49 
50 
51  LDP ld; int count = -1;
52  while( (ld = ld_from_json_stream(in))) {
53 
55 
56  if(!ld_valid_fields(ld)) {
57  sm_error("Wait, we didn't purify enough (#%d in file)\n", count);
58  continue;
59  }
60 
61  ld_write_as_json(ld, out);
62  ld_free(ld);
63  }
64 
65  return 0;
66 }
67 
68 
69 
70 void purify(LDP ld, double threshold_min, double threshold_max) {
71  for(int i=0;i<ld->nrays;i++) {
72  if(!ld->valid[i]) continue;
73 
74  double rho = ld->readings[i];
75  if( is_nan(rho) | (rho < threshold_min) | (rho > threshold_max) ) {
76  ld->readings[i] = GSL_NAN;
77  ld->valid[i] = 0;
78  ld->alpha[i] = GSL_NAN;
79  ld->alpha_valid[i] = 0;
80  }
81 
82  }
83 
84 }
85 
86 
87 
int *restrict valid
Definition: laser_data.h:23
void options_banner(const char *message)
Definition: options.c:33
void sm_set_program_name(const char *name)
Definition: logging.c:21
void ld_write_as_json(LDP ld, FILE *stream)
void options_double(struct option *, const char *name, double *p, double def_value, const char *desc)
int ld_valid_fields(LDP ld)
Definition: laser_data.c:179
struct option * ops
Definition: rb_sm.c:31
FILE * open_file_for_writing(const char *filename)
Definition: utils.c:25
double threshold_min
Definition: ld_purify.c:14
double threshold_max
Definition: ld_purify.c:14
Definition: options.h:49
void ld_free(LDP ld)
Definition: laser_data.c:87
double *restrict readings
Definition: laser_data.h:24
struct option * options_allocate(int n)
LDP ld_from_json_stream(FILE *file)
int *restrict alpha_valid
Definition: laser_data.h:30
const char * file_output
Definition: ld_purify.c:16
void purify(LDP ld, double threshold_min, double threshold_max)
Definition: ld_purify.c:70
FILE * open_file_for_reading(const char *filename)
Definition: utils.c:19
int main(int argc, const char *argv[])
Definition: ld_purify.c:20
const char * file_input
Definition: ld_purify.c:15
void options_print_help(struct option *options, FILE *f)
Definition: options.c:398
int is_nan(double v)
Definition: math_utils.c:60
void options_string(struct option *, const char *name, const char **p, const char *def_balue, const char *desc)
double *restrict alpha
Definition: laser_data.h:28
void sm_error(const char *msg,...)
Definition: logging.c:49
int options_parse_args(struct option *ops, int argc, const char *argv[])
Definition: options.c:66


csm
Author(s): Andrea Censi
autogenerated on Tue May 11 2021 02:18:23