ld_purify.c
Go to the documentation of this file.
00001 #include <gsl/gsl_rng.h>
00002 #include <gsl/gsl_randist.h>
00003 
00004 #include <math.h>
00005 #include <libgen.h>
00006 
00007 #include <options/options.h>
00008 #include "../csm/csm_all.h"
00009 
00010 void purify(LDP ld, double threshold_min, double threshold_max);
00011 
00012 
00013 struct ld_purify_params {
00014         double threshold_min, threshold_max;
00015         const char* file_input;
00016         const char* file_output;
00017 };
00018 
00019 
00020 int main(int argc, const char * argv[]) {
00021         sm_set_program_name(argv[0]);
00022         
00023         
00024         options_banner("ld_purify: Makes sure that the file format is valid. \n * Sets valid=0 if reading is outside interval ");
00025         
00026         struct ld_purify_params p;
00027         
00028         struct option* ops = options_allocate(20);
00029         options_double(ops, "threshold_min", &p.threshold_min, 0.01, 
00030                 "Sets valid=0 if readings are less than this threshold.");
00031         options_double(ops, "threshold_max", &p.threshold_max, 79.0, 
00032                 "Sets valid=0 if readings are more than this threshold.");
00033         
00034         options_string(ops, "in", &p.file_input, "stdin", "Input file ");
00035         options_string(ops, "out", &p.file_output, "stdout", "Output file ");
00036                 
00037                 
00038         if(!options_parse_args(ops, argc, argv)) {
00039                 options_print_help(ops, stderr);
00040                 return -1;
00041         }
00042 
00043         FILE * in = open_file_for_reading(p.file_input);
00044         if(!in) return -3;
00045 
00046         FILE * out = open_file_for_writing(p.file_output);
00047         if(!out) return -2;
00048 
00049 
00050 
00051         LDP ld; int count = -1;
00052         while( (ld = ld_from_json_stream(in))) {
00053                 
00054                 purify(ld, p.threshold_min, p.threshold_max);
00055                 
00056                 if(!ld_valid_fields(ld))  {
00057                         sm_error("Wait, we didn't purify enough  (#%d in file)\n", count);
00058                         continue;
00059                 }
00060                 
00061                 ld_write_as_json(ld, out);
00062                 ld_free(ld);
00063         }
00064         
00065         return 0;
00066 }
00067 
00068 
00069 
00070 void purify(LDP ld, double threshold_min, double threshold_max) {
00071         for(int i=0;i<ld->nrays;i++) {
00072                 if(!ld->valid[i]) continue;
00073                 
00074                 double rho = ld->readings[i];
00075                 if( is_nan(rho) | (rho < threshold_min) | (rho > threshold_max) ) {
00076                         ld->readings[i] = GSL_NAN;
00077                         ld->valid[i] = 0;
00078                         ld->alpha[i] = GSL_NAN;
00079                         ld->alpha_valid[i] = 0;
00080                 }
00081                 
00082         }
00083         
00084 }
00085 
00086 
00087 


csm
Author(s): Andrea Censi
autogenerated on Mon Jan 16 2017 03:48:29