Elaboradar  0.1
elaboradar.cpp
Vai alla documentazione di questo file.
1 
8 /*----------------------------------------------------------------------------*/
9 /* INCLUDE file */
10 /*----------------------------------------------------------------------------*/
11 
12 #include "cum_bac.h"
13 #include <radarelab/logging.h>
14 #include "config.h"
15 #include "site.h"
16 #include "cartproducts.h"
17 #include "cum_bac_clparser.h"
18 
19 #include <memory>
20 #include <cstdlib>
21 #include <cmath>
22 
23 #include <setwork.h>
24 
25 using namespace std;
26 using namespace radarelab;
27 using namespace elaboradar;
28 
29 /*----------------------------------------------------------------------------*/
30 /* FINE SEZIONE INCLUDE */
31 /*----------------------------------------------------------------------------*/
32 // DEFINIZIONI PREPROCESSORE NB: VPR E CLASS IMPLICANO QUALITY ,
33 
34 /*----------------------------------------------------------------------------*/
35 /* DICHIARATIVE GLOBALI */
36 /*----------------------------------------------------------------------------*/
37 
38  /*non cancellare*/
39  /* int trovo_top() */
40  /* { */
41  /* int ier,i,k,l; */
42  /* FILE *file0; */
43 
44  /* for(i=0; i<NUM_AZ_X_PPI; i++){ */
45  /* for (k=0; k<volume.vol_pol[0][i].ray.size(); k++){ */
46  /* for (l=first_level_static[i][k]; l<NEL; l++) */
47  /* { */
48  /* if (BYTEtoDB(volume.vol_pol[l][i].ray[k]) > 10. ) */
49  /* top[i][k]=(unsigned char)(quota_f((float)(volume.vol_pol[l][i].teta_true)*CONV_RAD+0.45*DTOR,k)/100.);{ //top in ettometri */
50  /* if (l >= NEL -1 ) top[i][k]=0; */
51  /* } */
52  /* } */
53  /* } */
54  /* } */
55  /* return ier; */
56  /* } */
57 
61 static void startup_banner(CUM_BAC_CLOPT *opt)
62 {
63  LOG_CATEGORY("radar.banner");
64 #ifdef WRITE_DBP
65 // " WRITE_DBP" eventualmente da re-inserire
66 #endif
67 #ifdef WRITE_DBP_REORDER
68 // " WRITE_DBP_REORDER" eventualemnte da re-inserire
69 #endif
70 
71 #ifdef Z_AVERAGE
72 // " Z_AVERAGE" obsoleta da togliere
73 #endif
74 #ifdef R_AVERAGE
75 // " R_AVERAGE" obsoleta da togliere
76 #endif
77 
79 
80  LOG_INFO("Lancio Programma");
81  LOG_INFO("-----------------------------------------------------------------");
82  std::string FlagRunTime ="Flag di RunTime: ";
83 
84  FlagRunTime=FlagRunTime+" "+opt->sito;
85  if(opt->do_declut)FlagRunTime=FlagRunTime+" DECLUTTER";
86  if(!opt->do_medium)FlagRunTime=FlagRunTime+" SHORT";
87  if(opt->do_medium)FlagRunTime=FlagRunTime+" MEDIUM";
88  if(opt->do_beamblocking)FlagRunTime=FlagRunTime+" BEAMBLOCKING";
89  if(opt->do_quality)FlagRunTime=FlagRunTime+" QUALITY";
90  if(opt->do_readStaticMap) FlagRunTime=FlagRunTime + " STATIC";
91 
92  LOG_INFO("%s", FlagRunTime.c_str());
93 
94  LOG_INFO("-----------------------------------------------------------------");
95  LOG_INFO("Variabili d'Ambiente:");
96  LOG_INFO("LISTA_FILE = %s", getenv("LISTA_FILE"));
97  LOG_INFO("LAST_FILE = %s", getenv("LAST_FILE"));
98  LOG_INFO("ANAP_STAT_FILE = %s", getenv("ANAP_STAT_FILE"));
99  LOG_INFO("BLOC_STAT_FILE = %s", getenv("BLOC_STAT_FILE"));
100  LOG_INFO("ELEV_STAT_FILE = %s", getenv("ELEV_STAT_FILE"));
101  LOG_INFO("DIR_OUT_PP_BLOC = %s", getenv("DIR_OUT_PP_BLOC"));
102  LOG_INFO("FILE_DEM_SPC = %s", getenv("FILE_DEM_SPC"));
103  LOG_INFO("FILE_DEM_GAT = %s", getenv("FILE_DEM_GAT"));
104  LOG_INFO("DIR_QUALITY = %s", getenv("DIR_QUALITY"));
105  LOG_INFO("FIRST_LEVEL_FILE = %s", getenv("FIRST_LEVEL_FILE"));
106  LOG_INFO("OUTPUT_Z_DIR = %s", getenv("OUTPUT_Z_DIR"));
107  LOG_INFO("OUTPUT_RAIN_DIR = %s", getenv("OUTPUT_RAIN_DIR"));
108  LOG_INFO("OUTPUT_Z_LOWRIS_DIR = %s", getenv("OUTPUT_Z_LOWRIS_DIR"));
109  LOG_INFO("-----------------------------------------------------------------");
110 }
111 
113 void commandline_error(const char* progname, const char* msg)
114 {
115  fprintf(stderr, "%s\n\n", msg);
116  fprintf(stderr, "Usage: %s volume_file file_type site_name\n", progname);
117  exit(1);
118 }
119 
120 void check_volume(const Volume<double>& volume, int file_type)
121 {
122 //Definizioni per test_file
123 #define NUM_MIN_BEAM 200
124 #define SHORT_DEC 0
125 #define SHORT_FULL_VOLUME 1
126 #define SHORT_HAIL 2
127 #define MEDIUM_PULSE 3
128 #define SHORT_212 4
129  LOG_CATEGORY("radar.io");
130 
131  unsigned n_elev = 0;
132  int expected_size_cell = 0;// != volume.resolution?
133 
134  //--- switch tra tipo di file per definire nelev = elevazioni da testare e la risoluzione
135  switch (file_type)
136  {
137  case SHORT_DEC:
138  LOG_INFO("CASO SHORT_DEC");
139 // if (!volume.load_info->declutter_rsp)
140 // throw runtime_error("File Senza Declutter Dinamico--cos' è???");
141  expected_size_cell = 250;
142  n_elev=4;
143  break;
144  //------------se tipo =1 esco
145  case SHORT_FULL_VOLUME://-----??? DUBBIO
146  LOG_INFO("CASO SHORT_FULL_VOLUME");
147  if (volume.load_info->declutter_rsp)
148  throw runtime_error("File con Declutter Dinamico");
149  expected_size_cell = 250;
150  n_elev=4;
151  break;
152  case SHORT_HAIL://-----??? DA BUTTARE NON ESISTE PIÙ
153  LOG_INFO("CASO SHORT_HAIL");
154  expected_size_cell = 250;
155  n_elev=3;
156  break;
157  case MEDIUM_PULSE:
158  LOG_INFO("CASO MEDIO OLD");
159  expected_size_cell = 1000;
160  n_elev=4;
161  break;
162  case SHORT_212://----- CORRISPONDE A VOL_NEW - da questo si ottengono il corto e il medio
163  LOG_INFO("CASO SHORT_212");
164 // if (!volume.load_info->declutter_rsp)
165 // throw runtime_error("File senza Declutter Dinamico");
166  expected_size_cell = 250;
167  n_elev=4;
168  break;
169  }
170 
171  //----------verifico che la risoluzione di ogni elevazione sia la stessa
172  if ( ! volume.is_unique_cell_size())
173  {
174  LOG_ERROR("File Risoluzione/size_cell non costante nel volume");
175  throw runtime_error("File Risoluzione/size_cell non costante");
176  }
177  //----------se la risoluzione del file è diversa da quella prevista dal tipo_file dà errore ed esce (perchè poi probabilmente le matrici sballano ?)
178  if (volume[0].cell_size != expected_size_cell)
179  {
180  LOG_ERROR("File Risoluzione/size_cell Sbagliata %f", volume[0].cell_size);
181  throw runtime_error("File Risoluzione/size_cell Sbagliata");
182  }
183  //------eseguo test su n0 beam sulle prime 4 elevazioni, se fallisce esco ------------
184 
185  if (volume.size() < n_elev)
186  {
187  LOG_ERROR("Volume has %zd elevations, but we are expecting at least %d", volume.size(), n_elev);
188  throw runtime_error("Insufficient elevation count");
189  }
190 
191 unsigned count_good = 0, bin_in_vol=0;
192  for (unsigned k = 0; k < volume.size(); k++) /* testo solo le prime 4 elevazioni */
193  {
194  LOG_INFO("Numero beam presenti: %4u -- elevazione %d", volume[k].beam_count, k);
195 
196  if (volume[k].beam_count < NUM_MIN_BEAM)
197  // se numero beam < numero minimo---Scrivolog ed esco !!!!!!!!!!!!!!!!!!!
198  {
199  LOG_ERROR("Trovati Pochi Beam Elevazione %2d - num.: %3d", k, volume[k].beam_count);
200  throw runtime_error("Insufficient beam count");
201  }
202 
203  count_good += (volume[k].array() > -19.0).count();
204  bin_in_vol += volume.beam_count*volume[k].beam_size;
205 
206  }
207 
208 // Check if volume data are bad (too many data probably fault due to AFC control
209  if ((float) (count_good)/bin_in_vol > 0.9 ){
210  LOG_ERROR("Trovati troppi dati > -19 dBZ %5.2f\%, volume con possibile problema di AFC", (float) (count_good)/bin_in_vol*100.);
211  throw runtime_error ("Possible bad volume (AFC)");
212  }
213 }
214 
215 /*
216 void CUM_BAC::StampoFlag(){
217  std::cout<<" Flag do_medium :"<< (this->do_medium?" true":" false")<<std::endl;
218  std::cout<<" Flag do_clean :"<< (this->do_clean?" true":" false")<<std::endl;
219  std::cout<<" Flag do_quality :"<< (this->do_quality?" true":" false")<<std::endl;
220  std::cout<<" Flag do_beamblocking :"<< (this->do_beamblocking?" true":" false")<<std::endl;
221  std::cout<<" Flag do_declutter :"<< (this->do_declutter?" true":" false")<<std::endl;
222  std::cout<<" Flag do_bloccorr :"<< (this->do_bloccorr?" true":" false")<<std::endl;
223  std::cout<<" Flag do_vpr :"<< (this->do_vpr?" true":" false")<<std::endl;
224  std::cout<<" Flag do_class :"<< (this->do_class?" true":" false")<<std::endl;
225  std::cout<<" Flag do_zlr_media :"<< (this->do_zlr_media?" true":" false")<<std::endl;
226  std::cout<<" Flag do_devel :"<< (this->do_devel?" true":" false")<<std::endl;
227  std::cout<<" Flag do_readStaticMap:"<< (this->do_readStaticMap?" true":" false")<<std::endl;
228 }
229 */
230 
231 /* ================================ */
232 int main (int argc, char **argv)
233  /* ================================ */
234 {
235  char *nome_file;
236  int ier_main=0;//uscite errore generico (lettura volume e succ.anap) , di test_file, del main
237  char *sito;//GAT O SPC
238  char *fuzzypath;
239  int file_type; // -- x definire n_elev e reolution e se è =1 esco
240 
241  CUM_BAC_CLOPT CL_opt;
242  parseOptions(argc,argv,&CL_opt);
243  PrintOptions(&CL_opt);
244 
245 
246  // Initialize logging
247  Logging logging;
248 
249  LOG_CATEGORY("radar.main");
250 
251  int MyMAX_BIN = 512;
252  if(CL_opt.do_medium) MyMAX_BIN=1024;
253  //------- verifica n0 argomenti ------
254 
255  if (argc < 4)
256  commandline_error(argv[0], "some argument is missing.");
257 
258  nome_file = (char *)CL_opt.filename.c_str();
259  file_type = CL_opt.filetype;
260  sito = (char *)CL_opt.sito.c_str();
261  fuzzypath = (char *)CL_opt.fuzzy_path.c_str();
262 
263  if(CL_opt.do_medium && CL_opt.filetype == 3) MyMAX_BIN = 512; // questo dovrebbe essere il caso del medio vecchio
264 
265  elaboradar::Config cfg;
266 
267  setwork(sito); //-------setto ambiente lavoro (se var amb lavoro non settate le setta in automatico) ------
268 
269  startup_banner(&CL_opt);
270 
271  const Site& site(Site::get(sito));
272  site.datipath = fuzzypath;
273  Volume<double> volume(NUM_AZ_X_PPI);
274 
275  cout<<"volume size = "<<volume.size()<<endl;
276  try {
277  if (CL_opt.data_in_odim)
278  // Legge e controlla il volume dal file ODIM
279  CUM_BAC::read_odim_volume(volume, site, nome_file, fuzzypath, CL_opt.do_clean, CL_opt.do_medium, CL_opt.set_undetect);
280  else
281  // Legge e controlla il volume dal file SP20
282  CUM_BAC::read_sp20_volume(volume, site, nome_file, CL_opt.do_clean, CL_opt.do_medium);
283  } catch (std::exception& e) {
284  LOG_ERROR("Errore nel caricamento del volume: %s", e.what());
285  return 2;
286  }
287  check_volume(volume, file_type);
288 
289  unique_ptr<elaboradar::CUM_BAC> cb(new elaboradar::CUM_BAC(volume, cfg, site, CL_opt.do_medium,MyMAX_BIN));
290  // Set feature flags
291  cb->do_quality = CL_opt.do_quality;
292  cb->do_beamblocking = CL_opt.do_beamblocking;
293  cb->do_declutter = CL_opt.do_declut;
294  cb->do_bloccorr = CL_opt.do_bloccor;
295  cb->do_class = CL_opt.do_class;
296  cb->do_devel = CL_opt.do_devel;
297  cb->do_readStaticMap= CL_opt.do_readStaticMap;
298  cb->do_zlr_media = true;
299  cb->do_anaprop = CL_opt.do_anaprop;
300 
301  printwork();
302  std::string algos;
303  if (CL_opt.do_clean) algos = algos+"CL";
304  if (CL_opt.do_anaprop) algos = algos+"_AP";
305  if (CL_opt.do_beamblocking) algos = algos+"_BB";
306  try {
307  //--------------se def anaprop : rimozione propagazione anomala e correzione beam blocking-----------------//
308  LOG_INFO("inizio rimozione anaprop e beam blocking");
309  for(unsigned k=0; k<volume.size(); k++) LOG_INFO(" SCAN # %2d - BeamSIZE %4d",k,volume[k].beam_size);
310  cb->declutter_anaprop();
311  cb->caratterizzo_volume();
312 
313  unsigned CART_DIM_ZLR = CL_opt.do_medium ? 512 : 256;
314  unsigned ZLR_N_ELEMENTARY_PIXEL = volume.at(0).cell_size == 1000. ? 1 : 4;
315  if (CL_opt.do_SaveBothRanges) {
316  if (volume.max_beam_size() * volume.at(0).cell_size * 0.001 > 128 ){
317  CART_DIM_ZLR = 512;
318  } else {
319  CART_DIM_ZLR = 256;
320  CL_opt.do_SaveBothRanges = false;
321  }
322  }
323 
324  if (CL_opt.do_intermediateProd){
325  CartProducts products(volume, CART_DIM_ZLR, ZLR_N_ELEMENTARY_PIXEL);
326  cb->generate_maps(products);
327  products.write_out(cb->assets,CART_DIM_ZLR, algos);
328  OdimProdDefs odimProd(products.z_out, products.qual_Z_1x1, products.ScaledRes);
329  products.write_odim(cb->assets, CART_DIM_ZLR, algos, odimProd);
330  if (CL_opt.do_SaveFullRes){
331  OdimProdDefs odimProdFullRes(products.z_fr, products.FullsizeRes);
332  products.write_odim(cb->assets, 256. * ZLR_N_ELEMENTARY_PIXEL, algos, odimProdFullRes);
333  }
334  if (CL_opt.do_SaveBothRanges){
335  LOG_INFO("Salvo sub-image intermedie");
336  products.write_out(cb->assets, 256, algos);
337  products.write_odim(cb->assets, 256, algos,odimProd);
338  }
339  }
340 
341  // cb->StampoFlag();
342  if (CL_opt.do_vpr) {
343  cb->want_vpr();
344  if (cb->do_quality) cb->vpr_class();
345  if (CL_opt.do_vpr) algos = algos+"_VPR";
346  if (CL_opt.do_class) algos = algos+"_CLASS";
347  }
348  CartProducts products(volume, CART_DIM_ZLR, ZLR_N_ELEMENTARY_PIXEL);
349  cb->generate_maps(products);
350  OdimProdDefs odimProd(products.z_out, products.qual_Z_1x1, products.ScaledRes);
351  if (CL_opt.do_SaveBothRanges){
352  products.write_out(cb->assets,CART_DIM_ZLR,algos);
353  products.write_odim(cb->assets, CART_DIM_ZLR, algos, odimProd);
354  LOG_INFO("Salvo sub-image");
355  products.write_out(cb->assets, 256, algos);
356  products.write_odim(cb->assets, 256, algos, odimProd);
357  }else{
358  products.write_out(cb->assets,CART_DIM_ZLR,algos);
359  products.write_odim(cb->assets, CART_DIM_ZLR, algos, odimProd);
360  // products.write_out(cb->assets);
361  }
362  if (CL_opt.do_SaveFullRes){
363  OdimProdDefs odimProdFullRes(products.z_fr, products.FullsizeRes);
364  products.write_odim(cb->assets, 256. * ZLR_N_ELEMENTARY_PIXEL, algos, odimProdFullRes);
365  }
366  } catch (std::exception& e) {
367  LOG_ERROR("Errore nella processazione: %s", e.what());
368  ier_main = 1;
369  }
370 
371  LOG_INFO("End of processing, result: %d", ier_main);
372  return ier_main;
373 }
374 
Classe principale del programma.
Definition: cum_bac.h:62
const unsigned max_beam_size() const
Return the maximum beam size in all PolarScans.
Definition: volume.h:455
const unsigned beam_count
Number of beam_count used ast each elevations.
Definition: volume.h:432
bool is_unique_cell_size() const
Test if same cell_size in all PolarScans.
Definition: volume.h:464
std::shared_ptr< LoadInfo > load_info
Polar volume information.
Definition: volume.h:270
codice principale di elaborazione dei volumi di riflettivita' radar usato per impulso corto
void commandline_error(const char *progname, const char *msg)
Report a command line error and quit.
Definition: elaboradar.cpp:113
name space generale del programma
Definition: assets.h:28
String functions.
Definition: cart.cpp:4
void printwork()
funzione che stampa l'ambiente lavoro con chiamata a LOG_INFO
Definition: setwork.cpp:107
int setwork(char *sito)
funzione che setta ambiente lavoro nel caso non sia settato dall'esterno
Definition: setwork.cpp:9
settaggio ambiente lavoro nel caso non sia settato dall'esterno
definisce struttura Site Contiene le informazioni di base che caratterizzano il sito radar
Radar site information.
Definition: site.h:24