7#include <radarelab/algo/vpr.h>
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");
117 load_ascii(conf_site->get_dem_file_name(),
"file dem", matrix);
122 const char* fname = getenv(
"FIRST_LEVEL_FILE");
124 LOG_WARN(
"leggo datipath da conf_site..");
125 fname = conf_site->get_first_level_file_name(conf_month);
127 load_raw(fname,
"mappa statica", matrix);
142double parse_hray(
File& fd, std::function<
void (
unsigned el,
unsigned bin,
double value)> on_sample)
150 dtrs = strtod(line, NULL);
157 double val = strtod(s, &next);
158 if (next == s)
break;
159 on_sample(el, line_no - 1, val);
167 throw std::runtime_error(
"hray/hray_inf file is empty");
175 File fd(logging_category);
177 throw std::runtime_error(
"cannot open hray file");
178 return parse_hray(fd, on_sample);
183 File fd(logging_category);
185 throw std::runtime_error(
"cannot open hray inf file");
186 return parse_hray(fd, on_sample);
191 const char* dir = getenv(
"DIR_OUT_PP_BLOC");
192 if (!dir)
throw runtime_error(
"DIR_OUT_PP_BLOC is not set");
195 sprintf(fname,
"%s/%04d%02d%02d%02d%02d%s", dir,
196 conf_year, conf_month, conf_day, conf_hour, conf_minute, suffix);
203 LOG_CATEGORY(
"radar.vpr");
204 File in(logging_category);
213 if(fscanf(in,
"%f %f %f \n",&lon,&lat,&t) == EOF)
break;
214 if (fabs(conf_site->radarSite.lat_r-lat)<=maxdlat && fabs(conf_site->radarSite.lon_r-lon)<=maxdlon) {
216 media_t += t - 273.15;
222 LOG_ERROR(
"Temperature data not found in $FILE_T=%s", in.name());
226 media_t /= (float)icount;
227 LOG_INFO(
"ho %i stazioni dati affidabili e la t media è %f\n", icount, media_t);
233 LOG_CATEGORY(
"radar.vpr");
234 File in(logging_category);
242 fread(&last_time, 4, 1, in);
244 long int gap1 = abs(conf_acq_time - last_time)/900;
245 LOG_INFO(
"old_data_header.norm.maq.acq_date last_time gap %ld %u %ld", conf_acq_time, last_time, gap1);
252 LOG_CATEGORY(
"radar.vpr");
253 File in(logging_category);
258 if (fscanf(in,
"%i ", &heating) != 1)
260 LOG_ERROR(
"Cannot read $VPR_HEATING=%s: %s", in.name(), strerror(errno));
269 LOG_CATEGORY(
"radar.vpr");
270 File out(logging_category);
274 if (fprintf(out,
" %i \n", value) < 0)
275 LOG_ERROR(
"Cannot write $VPR_HEATING=%s: %s", out.name(), strerror(errno));
280 LOG_CATEGORY(
"radar.class");
281 File in(logging_category);
285 if (fscanf(in,
"%f", &zeroterm) != 1)
287 LOG_ERROR(
"$FILE_ZERO_TERMICO=%s cannot be read: %s", in.name(), strerror(errno));
297 const char* fname = getenv(
"LAST_VPR");
298 if (!fname)
throw runtime_error(
"$LAST_VPR is not set");
300 uint32_t val = conf_acq_time;
301 fwrite(&val, 4, 1, out);
307 File in(logging_category);
312 if (fscanf(in,
"%i", &value) != 1)
314 LOG_ERROR(
"$VPR_HMAX=%s cannot be read: %s", in.name(), strerror(errno));
318 LOG_INFO(
"fatta lettura hmax vpr = %i", value);
324 const char* fname = getenv(
"VPR_HMAX");
325 if (!fname)
throw runtime_error(
"$VPR_HMAX is not set");
327 fprintf(out,
"%d", hvprmax);
333 File in(logging_category);
336 for (
unsigned i = 0; i < vpr0.size(); ++i)
338 if (fscanf(in,
"%f %li\n", &vpr0.val[i], &vpr0.area[i]) != 2)
340 LOG_ERROR(
"$VPR0_FILE=%s cannot be read: %s", in.name(), strerror(errno));
341 throw std::runtime_error(
"cannot read $VPR0_FILE");
349 const char* dir = getenv(
"DIR_STORE_VPR");
350 if (!dir)
return false;
356 snprintf(fname, 64,
"%04d%02d%02d%02d%02d_vpr_%s",
357 t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
358 t.tm_hour, t.tm_min, conf_site->name.c_str());
360 string pathname = dir;
364 File in(logging_category);
365 if (!in.
open(pathname,
"r",
"archived VPR file"))
371 fscanf(in,
" %s %s %s %s" ,stringa ,stringa,stringa,stringa);
372 for (
unsigned ilay=0; ilay < vpr.size(); ++ilay){
376 fscanf(in,
" %i %f %li", &il, &vpr_dbz, &ar);
381 vpr.val[ilay] = dbz.DBZtoR(vpr_dbz);
385 vpr.val[ilay] = NODATAVPR;
402 LOG_WARN(
"non esiste file vpr vecchio: %s",getenv(
"VPR0_FILE"));
422 time_t Time = conf_acq_time;
427 for (
unsigned i=0;i<MEMORY;i++)
439 const char* fname = getenv(
"VPR0_FILE");
440 if (!fname)
throw runtime_error(
"$VPR0_FILE (ultimo vpr) is not set");
442 for (
unsigned i = 0; i < vpr.size(); ++i)
443 if (fprintf(out,
" %10.3f %li\n", vpr.val[i], vpr.area[i]) < 0)
445 LOG_ERROR(
"$VPR0_FILE=%s cannot be written: %s", fname, strerror(errno));
447 throw std::runtime_error(
"cannot write to $VPR0_FILE");
454 const char* dirname = getenv(
"OUTPUT_Z_LOWRIS_DIR");
455 if (!dirname)
throw runtime_error(
"OUTPUT_Z_LOWRIS_DIR is not set");
456 string fname(dirname);
457 fname +=
"/MP_coeff";
458 File out(logging_category);
459 out.
open(fname,
"wb",
"MP coefficients");
461 unsigned char MP_coeff[2];
462 MP_coeff[0]=(
unsigned char)(dbz.aMP/10);
463 MP_coeff[1]=(
unsigned char)(dbz.bMP*10);
465 fwrite(MP_coeff,
sizeof(MP_coeff), 1, out);
470 if (!outfile_devel_data)
472 const char* qdir = getenv(
"DIR_QUALITY");
473 if (!qdir)
throw runtime_error(
"$DIR_QUALITY is not set");
475 fname +=
"/devel-data.h5";
476 outfile_devel_data =
new H5::H5File(fname, H5F_ACC_TRUNC);
478 return *outfile_devel_data;
484 LOG_INFO(
"Opening %s %s", desc, fname.c_str());
488 fseek(in, 0,SEEK_END);
489 long fsize = ftell(in);
493 if ((
unsigned)fsize != matrix.size() *
sizeof(T))
495 LOG_ERROR(
"Il file %s è %ld byte ma dovrebbe invece essere %ld byte\n",
496 fname.c_str(), fsize, matrix.size() *
sizeof(T));
497 throw std::runtime_error(
"La dimensione della mappa statica non è quello che mi aspetto");
499 LOG_INFO (
"DIMENSIONE MAPPA STATICA %ld %ld", matrix.rows(), matrix.cols());
501 for (
unsigned i = 0; i < matrix.rows(); ++i)
502 if (fread(matrix.data() + i * matrix.cols(), matrix.cols(), 1, in) != 1)
504 std::string errmsg(
"Error reading ");
507 errmsg += strerror(errno);
509 throw std::runtime_error(errmsg);
517 LOG_INFO(
"Opening %s %s", desc, fname.c_str());
520 for (
unsigned x = 0; x < matrix.cols(); ++x)
521 for (
unsigned y = 0; y < matrix.rows(); ++y)
524 fscanf(in,
"%f ", &val);
533 const unsigned buf_size = 16;
535 struct tm *tempo = gmtime(&conf_acq_time);
536 strftime(buf, buf_size,
"%Y%m%d%H%M", tempo);
542 const char* dir = getenv(dir_env_var);
545 LOG_INFO(
"$%s not set", dir_env_var);
546 throw runtime_error(
"required env var is not set");
552 LOG_INFO(
"aperto file %s dimensione matrice %zd\n", fname.c_str(), image.size());
556 for (
unsigned y = 0; y < image.cols(); ++y)
557 for (
unsigned x = 0; x < image.rows(); ++x)
558 transformed(x, image.cols()-1-y) = image(y, x);
559 if (fwrite(transformed.data(), transformed.size(), 1, out) != 1)
561 LOG_WARN(
"cannot write to %s: %s", fname.c_str(), strerror(errno));
563 throw std::runtime_error(
"cannot write to image file");
571 const char* dir = getenv(dir_env_var);
574 LOG_INFO(
"$%s not set", dir_env_var);
575 throw runtime_error(
"required env var is not set");
578 string fname = string(dir) +
"/" +
fname_from_acq_time() +
"_" + std::to_string(image_side) + ext;
581 LOG_INFO(
"aperto file %s dimensione matrice %zd\n", fname.c_str(), image.size());
584 unsigned xofs = (image.cols() - image_side) / 2;
585 unsigned yofs = (image.rows() - image_side) / 2;
588 for (
unsigned y = 0; y < image_side; ++y)
589 for (
unsigned x = 0; x < image_side; ++x)
590 transformed(x, image_side-1-y) = image(y + yofs, x + xofs);
592 if (fwrite(transformed.data(), transformed.size(), 1, out) != 1)
594 LOG_WARN(
"cannot write to %s: %s", fname.c_str(), strerror(errno));
596 throw std::runtime_error(
"cannot write to image file");
604 const char* dir = getenv(dir_env_var);
607 LOG_INFO(
"$%s not set", dir_env_var);
608 throw runtime_error(
"required env var is not set");
610 string fname = string(dir) +
"/" +
fname_from_acq_time() +
"_" + std::to_string(image_side) +
"_"+algos+ext;
613 LOG_INFO(
"aperto file %s dimensione matrice %zd\n", fname.c_str(), image.size());
616 unsigned xofs = (image.cols() - image_side) / 2;
617 unsigned yofs = (image.rows() - image_side) / 2;
618 LOG_INFO(
" Image_size %4d , Image.cols %4d Image.Rows %4d -- xofs %d yofs %d", image_side, (
int)image.cols(), (
int)image.rows(), xofs, yofs);
620 for (
unsigned y = 0; y < image_side; ++y)
621 for (
unsigned x = 0; x < image_side; ++x)
622 transformed(x, image_side-1-y) = image(y + yofs, x + xofs);
624 if (fwrite(transformed.data(), transformed.size(), 1, out) != 1)
626 LOG_WARN(
"cannot write to %s: %s", fname.c_str(), strerror(errno));
628 throw std::runtime_error(
"cannot write to image file");
638 const char* dir = getenv(dir_env_var);
641 LOG_INFO(
"$%s not set", dir_env_var);
642 throw runtime_error(
"required env var is not set");
645 string fname = string(dir) +
"/" +
fname_from_acq_time() +
"-" + name +
"." + gdal_extension_for_format(format);
647 radarelab::write_image(image, fname, format);
649 throw std::runtime_error(
"GDAL support was not enabled at compile time");
660 time_t Assets::getAcqTime() {
return conf_acq_time;} ;
661 RadarSite Assets::getRadarSite() {
return conf_site->
radarSite; };
Gestisce risorse usate dal programma.
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.
Assets(const Config &cfg)
Constructor.
void load_first_level_bb_bloc(radarelab::Matrix2D< unsigned char > &matrix)
Open the first level elevation BB bloc file.
std::string fname_from_acq_time() const
Build a basename (without extension) for a file given the current acquisition time.
void write_vpr0(const radarelab::algo::VPR &vpr)
Write in $VPR0_FILE the vpr calculated.
bool read_0term(float &zeroterm)
Read $FILE_ZERO_TERMICO.
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 ...
bool save_acq_time(time_t acq_time=0)
Save acq_time in $LAST_FILE, comparing it with the previous value.
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 write_vpr_hmax(int hvprmax)
write in $VPR_HMAX the vpr peak's height.
void load_dem(radarelab::Matrix2D< float > &matrix)
Open the dem file.
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...
void load_raw(const std::string &fname, const char *desc, radarelab::Matrix2D< T > &matrix)
Load a Matrix2D, from packed row-major binary data.
void load_ascii(const std::string &fname, const char *desc, radarelab::Matrix2D< float > &matrix)
Load a Matrix2D, from space-separated column-major ascii floats.
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.
bool read_vpr0(radarelab::algo::VPR &vpr0)
Read in $VPR0_FILE the last vpr available.
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 configure(const Site &site, time_t acq_time)
Configure asset lookup with the given details.
float read_t_ground() const
fornisce temperatura al suolo, da lettura file esterno
void write_vpr_heating(int value) const
Write a new value to $VPR_HEATING (counter of consecutive vpr calculated, see scientific documentatio...
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.
void write_dbz_coefficients(const radarelab::algo::DBZ &dbz)
Write in $OUTPUT_Z_LOWRIS_DIR/MP_coeff the MP coefficients.
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 load_first_level(radarelab::Matrix2D< unsigned char > &matrix)
Open the first level file.
void load_first_level_bb_el(radarelab::Matrix2D< unsigned char > &matrix)
Open the first level elevation BB el file.
int read_vpr_heating() const
Read the value of $VPR_HEATING (counter of consecutive vpr calculated, see scientific documentation)
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.
int read_vpr_hmax()
Read in $VPR_HMAX the vpr peak's height.
void write_last_vpr()
Write the acquisition time in $LAST_VPR 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 read_lines(std::function< void(char *, size_t)> line_cb)
Read the file line by line, calling line_cb on each line read.
bool open(const std::string &fname, const char *mode, const char *desc=nullptr)
Opens a file by its pathname.
Open a file taking its name from a given env variable.
Class to manage reflectivity functions (simply attenuation correction, conversion between Z,...
name space generale del programma
void gdal_init_once()
Initialize the GDAL library when called for the first time; does nothing all other times.
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.
definisce struttura Site Contiene le informazioni di base che caratterizzano il sito radar
static const Site & get(const char *name)
Get a Site object according to a site name.
RadarSite radarSite
Description of radar site.
Base for all matrices we use, since we rely on row-major data.