7 #include <radarelab/algo/vpr.h>
18 using namespace radarelab;
20 namespace elaboradar {
22 Assets::Assets(
const Config& cfg)
23 : logging_category(log4c_category_get(
"radar.assets")), cfg(cfg), outfile_devel_data(0)
32 if (outfile_devel_data)
33 delete outfile_devel_data;
44 conf_acq_time = acq_time;
45 struct tm* tempo = gmtime(&acq_time);
46 conf_year = tempo->tm_year + 1900;
47 conf_month = tempo->tm_mon + 1;
48 conf_day = tempo->tm_mday;
49 conf_hour = tempo->tm_hour;
50 conf_minute = tempo->tm_min;
55 if (acq_time == 0) acq_time = conf_acq_time;
58 const char* last_file = getenv(
"LAST_FILE");
59 if (last_file == NULL)
61 LOG_INFO(
"$LAST_FILE not set");
68 FILE* fp = fopen(last_file,
"r");
73 LOG_INFO(
"$LAST_FILE=%s does not exist", last_file);
79 if (fread(&last_time, 4, 1, fp) != 1)
81 LOG_INFO(
"$LAST_FILE=%s cannot be read", last_file);
88 int diff = acq_time - last_time;
89 LOG_INFO(
"%s: new acq_time is old %c %d", last_file, diff < 0 ?
'-' :
'+', abs(diff));
92 if (acq_time <= last_time)
97 if ((fp = fopen(last_file,
"w")) == NULL)
99 LOG_WARN(
"cannot write to %s: %s", last_file, strerror(errno));
100 throw std::runtime_error(
"cannot (re)create $LAST_FILE");
104 last_time = acq_time;
105 if (fwrite(&last_time, 4, 1, fp) != 1)
107 LOG_WARN(
"cannot write to %s: %s", last_file, strerror(errno));
108 throw std::runtime_error(
"cannot write to $LAST_FILE");
122 const char* fname = getenv(
"FIRST_LEVEL_FILE");
125 load_raw(fname,
"mappa statica", matrix);
140 double parse_hray(
File& fd, std::function<
void (
unsigned el,
unsigned bin,
double value)> on_sample)
148 dtrs = strtod(line, NULL);
155 double val = strtod(s, &next);
156 if (next == s)
break;
157 on_sample(el, line_no - 1, val);
165 throw std::runtime_error(
"hray/hray_inf file is empty");
173 File fd(logging_category);
175 throw std::runtime_error(
"cannot open hray file");
176 return parse_hray(fd, on_sample);
181 File fd(logging_category);
183 throw std::runtime_error(
"cannot open hray inf file");
184 return parse_hray(fd, on_sample);
189 const char* dir = getenv(
"DIR_OUT_PP_BLOC");
190 if (!dir)
throw runtime_error(
"DIR_OUT_PP_BLOC is not set");
193 sprintf(fname,
"%s/%04d%02d%02d%02d%02d%s", dir,
194 conf_year, conf_month, conf_day, conf_hour, conf_minute, suffix);
201 LOG_CATEGORY(
"radar.vpr");
202 File in(logging_category);
211 if(fscanf(in,
"%f %f %f \n",&lon,&lat,&t) == EOF)
break;
212 if (fabs(conf_site->
radarSite.lat_r-lat)<=maxdlat && fabs(conf_site->
radarSite.lon_r-lon)<=maxdlon) {
214 media_t += t - 273.15;
220 LOG_ERROR(
"Temperature data not found in $FILE_T=%s", in.name());
224 media_t /= (float)icount;
225 LOG_INFO(
"ho %i stazioni dati affidabili e la t media è %f\n", icount, media_t);
231 LOG_CATEGORY(
"radar.vpr");
232 File in(logging_category);
240 fread(&last_time, 4, 1, in);
242 long int gap1 = abs(conf_acq_time - last_time)/900;
243 LOG_INFO(
"old_data_header.norm.maq.acq_date last_time gap %ld %u %ld", conf_acq_time, last_time, gap1);
250 LOG_CATEGORY(
"radar.vpr");
251 File in(logging_category);
256 if (fscanf(in,
"%i ", &heating) != 1)
258 LOG_ERROR(
"Cannot read $VPR_HEATING=%s: %s", in.name(), strerror(errno));
267 LOG_CATEGORY(
"radar.vpr");
268 File out(logging_category);
272 if (fprintf(out,
" %i \n", value) < 0)
273 LOG_ERROR(
"Cannot write $VPR_HEATING=%s: %s", out.name(), strerror(errno));
278 LOG_CATEGORY(
"radar.class");
279 File in(logging_category);
283 if (fscanf(in,
"%f", &zeroterm) != 1)
285 LOG_ERROR(
"$FILE_ZERO_TERMICO=%s cannot be read: %s", in.name(), strerror(errno));
295 const char* fname = getenv(
"LAST_VPR");
296 if (!fname)
throw runtime_error(
"$LAST_VPR is not set");
298 uint32_t val = conf_acq_time;
299 fwrite(&val, 4, 1, out);
305 File in(logging_category);
310 if (fscanf(in,
"%i", &value) != 1)
312 LOG_ERROR(
"$VPR_HMAX=%s cannot be read: %s", in.name(), strerror(errno));
316 LOG_INFO(
"fatta lettura hmax vpr = %i", value);
322 const char* fname = getenv(
"VPR_HMAX");
323 if (!fname)
throw runtime_error(
"$VPR_HMAX is not set");
325 fprintf(out,
"%d", hvprmax);
331 File in(logging_category);
334 for (
unsigned i = 0; i < vpr0.size(); ++i)
336 if (fscanf(in,
"%f %li\n", &vpr0.val[i], &vpr0.area[i]) != 2)
338 LOG_ERROR(
"$VPR0_FILE=%s cannot be read: %s", in.name(), strerror(errno));
339 throw std::runtime_error(
"cannot read $VPR0_FILE");
347 const char* dir = getenv(
"DIR_STORE_VPR");
348 if (!dir)
return false;
354 snprintf(fname, 64,
"%04d%02d%02d%02d%02d_vpr_%s",
355 t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
356 t.tm_hour, t.tm_min, conf_site->
name.c_str());
358 string pathname = dir;
362 File in(logging_category);
363 if (!in.
open(pathname,
"r",
"archived VPR file"))
369 fscanf(in,
" %s %s %s %s" ,stringa ,stringa,stringa,stringa);
370 for (
unsigned ilay=0; ilay < vpr.size(); ++ilay){
374 fscanf(in,
" %i %f %li", &il, &vpr_dbz, &ar);
379 vpr.val[ilay] = dbz.DBZtoR(vpr_dbz);
383 vpr.val[ilay] = NODATAVPR;
400 LOG_WARN(
"non esiste file vpr vecchio: %s",getenv(
"VPR0_FILE"));
420 time_t Time = conf_acq_time;
425 for (
unsigned i=0;i<MEMORY;i++)
437 const char* fname = getenv(
"VPR0_FILE");
438 if (!fname)
throw runtime_error(
"$VPR0_FILE (ultimo vpr) is not set");
440 for (
unsigned i = 0; i < vpr.size(); ++i)
441 if (fprintf(out,
" %10.3f %li\n", vpr.val[i], vpr.area[i]) < 0)
443 LOG_ERROR(
"$VPR0_FILE=%s cannot be written: %s", fname, strerror(errno));
445 throw std::runtime_error(
"cannot write to $VPR0_FILE");
452 const char* dirname = getenv(
"OUTPUT_Z_LOWRIS_DIR");
453 if (!dirname)
throw runtime_error(
"OUTPUT_Z_LOWRIS_DIR is not set");
454 string fname(dirname);
455 fname +=
"/MP_coeff";
456 File out(logging_category);
457 out.
open(fname,
"wb",
"MP coefficients");
459 unsigned char MP_coeff[2];
460 MP_coeff[0]=(
unsigned char)(dbz.aMP/10);
461 MP_coeff[1]=(
unsigned char)(dbz.bMP*10);
463 fwrite(MP_coeff,
sizeof(MP_coeff), 1, out);
468 if (!outfile_devel_data)
470 const char* qdir = getenv(
"DIR_QUALITY");
471 if (!qdir)
throw runtime_error(
"$DIR_QUALITY is not set");
473 fname +=
"/devel-data.h5";
474 outfile_devel_data =
new H5::H5File(fname, H5F_ACC_TRUNC);
476 return *outfile_devel_data;
482 LOG_INFO(
"Opening %s %s", desc, fname.c_str());
486 fseek(in, 0,SEEK_END);
487 long fsize = ftell(in);
491 if ((
unsigned)fsize != matrix.size() *
sizeof(T))
493 LOG_ERROR(
"Il file %s è %ld byte ma dovrebbe invece essere %ld byte\n",
494 fname.c_str(), fsize, matrix.size() *
sizeof(T));
495 throw std::runtime_error(
"La dimensione della mappa statica non è quello che mi aspetto");
497 LOG_INFO (
"DIMENSIONE MAPPA STATICA %ld %ld", matrix.rows(), matrix.cols());
499 for (
unsigned i = 0; i < matrix.rows(); ++i)
500 if (fread(matrix.data() + i * matrix.cols(), matrix.cols(), 1, in) != 1)
502 std::string errmsg(
"Error reading ");
505 errmsg += strerror(errno);
507 throw std::runtime_error(errmsg);
515 LOG_INFO(
"Opening %s %s", desc, fname.c_str());
518 for (
unsigned x = 0; x < matrix.cols(); ++x)
519 for (
unsigned y = 0; y < matrix.rows(); ++y)
522 fscanf(in,
"%f ", &val);
531 const unsigned buf_size = 16;
534 struct tm *tempo = gmtime(&conf_acq_time);
536 snprintf(buf, buf_size,
"%04d%02d%02d%02d%02d",
537 tempo->tm_year+1900, tempo->tm_mon+1, tempo->tm_mday,
538 tempo->tm_hour, tempo->tm_min);
545 const char* dir = getenv(dir_env_var);
548 LOG_INFO(
"$%s not set", dir_env_var);
549 throw runtime_error(
"required env var is not set");
555 LOG_INFO(
"aperto file %s dimensione matrice %zd\n", fname.c_str(), image.size());
559 for (
unsigned y = 0; y < image.cols(); ++y)
560 for (
unsigned x = 0; x < image.rows(); ++x)
561 transformed(x, image.cols()-1-y) = image(y, x);
562 if (fwrite(transformed.data(), transformed.size(), 1, out) != 1)
564 LOG_WARN(
"cannot write to %s: %s", fname.c_str(), strerror(errno));
566 throw std::runtime_error(
"cannot write to image file");
574 const char* dir = getenv(dir_env_var);
577 LOG_INFO(
"$%s not set", dir_env_var);
578 throw runtime_error(
"required env var is not set");
581 string fname = string(dir) +
"/" +
fname_from_acq_time() +
"_" + std::to_string(image_side) + ext;
584 LOG_INFO(
"aperto file %s dimensione matrice %zd\n", fname.c_str(), image.size());
587 unsigned xofs = (image.cols() - image_side) / 2;
588 unsigned yofs = (image.rows() - image_side) / 2;
591 for (
unsigned y = 0; y < image_side; ++y)
592 for (
unsigned x = 0; x < image_side; ++x)
593 transformed(x, image_side-1-y) = image(y + yofs, x + xofs);
595 if (fwrite(transformed.data(), transformed.size(), 1, out) != 1)
597 LOG_WARN(
"cannot write to %s: %s", fname.c_str(), strerror(errno));
599 throw std::runtime_error(
"cannot write to image file");
607 const char* dir = getenv(dir_env_var);
610 LOG_INFO(
"$%s not set", dir_env_var);
611 throw runtime_error(
"required env var is not set");
613 string fname = string(dir) +
"/" +
fname_from_acq_time() +
"_" + std::to_string(image_side) +
"_"+algos+ext;
616 LOG_INFO(
"aperto file %s dimensione matrice %zd\n", fname.c_str(), image.size());
619 unsigned xofs = (image.cols() - image_side) / 2;
620 unsigned yofs = (image.rows() - image_side) / 2;
621 LOG_INFO(
" Image_size %4d , Image.cols %4d Image.Rows %4d -- xofs %d yofs %d", image_side, image.cols(), image.rows(), xofs, yofs);
623 for (
unsigned y = 0; y < image_side; ++y)
624 for (
unsigned x = 0; x < image_side; ++x)
625 transformed(x, image_side-1-y) = image(y + yofs, x + xofs);
627 if (fwrite(transformed.data(), transformed.size(), 1, out) != 1)
629 LOG_WARN(
"cannot write to %s: %s", fname.c_str(), strerror(errno));
631 throw std::runtime_error(
"cannot write to image file");
641 const char* dir = getenv(dir_env_var);
644 LOG_INFO(
"$%s not set", dir_env_var);
645 throw runtime_error(
"required env var is not set");
648 string fname = string(dir) +
"/" +
fname_from_acq_time() +
"-" + name +
"." + gdal_extension_for_format(format);
650 radarelab::write_image(image, fname, format);
652 throw std::runtime_error(
"GDAL support was not enabled at compile time");
663 time_t Assets::getAcqTime() {
return conf_acq_time;} ;
664 RadarSite Assets::getRadarSite() {
return conf_site->
radarSite; };
virtual const char * get_dem_file_name() const =0
Return dem file name.
definisce struttura Site Contiene le informazioni di base che caratterizzano il sito radar ...
void configure(const Site &site, time_t acq_time)
Configure asset lookup with the given details.
void write_vpr_hmax(int hvprmax)
write in $VPR_HMAX the vpr peak's height.
float read_t_ground() const
fornisce temperatura al suolo, da lettura file esterno
void write_vpr0(const radarelab::algo::VPR &vpr)
Write in $VPR0_FILE the vpr calculated.
bool find_vpr0(const radarelab::algo::DBZ &dbz, radarelab::algo::VPR &vpr0, long int &gap)
Read the gap and the vpr0, and if vpr0 is not found, look it up among the archived VPRs...
void read_lines(std::function< void(char *, size_t)> line_cb)
Read the file line by line, calling line_cb on each line read.
Class to manage reflectivity functions (simply attenuation correction, conversion between Z...
void write_subimage(const radarelab::Matrix2D< unsigned char > &image, unsigned image_side, const char *dir_env_var, const char *ext, const char *desc)
Write an image in a raw file in ${dir_env_var}, with the acquisition date as file name and the given ...
FILE * fopen_checked(const char *fname, const char *mode, const char *description)
A wrapper of fopen that throws an exception if it cannot open the file.
bool open_from_env(const char *varname, const char *mode, const char *desc=nullptr)
Opens a file taking its name from the environment variable envname.
void write_vpr_heating(int value) const
Write a new value to $VPR_HEATING (counter of consecutive vpr calculated, see scientific documentatio...
H5::H5File get_devel_data_output() const
Return an open HDF5 File ( $DIR_QUALITY/devel-data.h5) to which we can write datasets used to debug r...
RadarSite radarSite
Description of radar site.
std::string fname_out_pp_bloc(const char *suffix) const
Compute the file name of a date/time based file in $DIR_OUT_PP_BLOC.
std::string fname_from_acq_time() const
Build a basename (without extension) for a file given the current acquisition time.
bool read_vpr0(radarelab::algo::VPR &vpr0)
Read in $VPR0_FILE the last vpr available.
static const Site & get(const char *name)
Get a Site object according to a site name.
void load_first_level(radarelab::Matrix2D< unsigned char > &matrix)
Open the first level file.
void load_dem(radarelab::Matrix2D< float > &matrix)
Open the dem file.
void load_first_level_bb_bloc(radarelab::Matrix2D< unsigned char > &matrix)
Open the first level elevation BB bloc file.
bool read_0term(float &zeroterm)
Read $FILE_ZERO_TERMICO.
void load_raw(const std::string &fname, const char *desc, radarelab::Matrix2D< T > &matrix)
Load a Matrix2D, from packed row-major binary data.
int read_vpr_heating() const
Read the value of $VPR_HEATING (counter of consecutive vpr calculated, see scientific documentation) ...
bool save_acq_time(time_t acq_time=0)
Save acq_time in $LAST_FILE, comparing it with the previous value.
void write_last_vpr()
Write the acquisition time in $LAST_VPR file.
virtual const char * get_first_level_file_name(unsigned month) const =0
Return first_elev file name.
int read_vpr_hmax()
Read in $VPR_HMAX the vpr peak's height.
void write_image(const radarelab::Matrix2D< unsigned char > &image, const char *dir_env_var, const char *ext, const char *desc)
Write an image in a raw file in ${dir_env_var}, with the acquisition date as file name and the given ...
void load_first_level_bb_el(radarelab::Matrix2D< unsigned char > &matrix)
Open the first level elevation BB el file.
void gdal_init_once()
Initialize the GDAL library when called for the first time; does nothing all other times...
double read_file_hray_inf(std::function< void(unsigned el, unsigned bin, double value)> on_sample)
Read the hray file, calling a callback on each parsed value.
Open a file taking its name from a given env variable.
std::string name
Nome sito radar.
Gestisce risorse usate dal programma.
void write_gdal_image(const radarelab::Matrix2D< T > &image, const char *dir_env_var, const char *name, const char *format)
Write a graphic image with gdal.
long int read_profile_gap() const
Read the gap between the time in $LAST_VPR and the current acquisition time.
bool open(const std::string &fname, const char *mode, const char *desc=nullptr)
Opens a file by its pathname.
bool read_archived_vpr(const radarelab::algo::DBZ &dbz, time_t time, radarelab::algo::VPR &vpr)
Try to read the archived VPR at time time.
double read_file_hray(std::function< void(unsigned el, unsigned bin, double value)> on_sample)
Read the hray file, calling a callback on each parsed value.
void load_ascii(const std::string &fname, const char *desc, radarelab::Matrix2D< float > &matrix)
Load a Matrix2D, from space-separated column-major ascii floats.
void write_dbz_coefficients(const radarelab::algo::DBZ &dbz)
Write in $OUTPUT_Z_LOWRIS_DIR/MP_coeff the MP coefficients.