WSL/SLF GitLab Repository

Commit 256866c6 authored by Mathias Bavay's avatar Mathias Bavay
Browse files

Code cleanup in libsmet, better min/max for ILWR in PNGIO, support for color,...

Code cleanup in libsmet, better min/max for ILWR in PNGIO, support for color, description, units, min and max in smet files in SMETIO and smet2agr. The SMET specifications will still have to be updated to reflect these recent additions, though...
parent 3a7d17ed
......@@ -576,7 +576,7 @@ void PNGIO::write2DGrid(const Grid2DObject& grid_in, const MeteoGrids::Parameter
gradient.set(Gradient::heat, min, max, autoscale);
} else if (parameter==MeteoGrids::ILWR) {
if (!autoscale) {
min = 200.; max = 500.;
min = 150.; max = 400.;
}
gradient.set(Gradient::heat, min, max, autoscale);
} else if (parameter==MeteoGrids::SWE) {
......@@ -741,7 +741,7 @@ std::string PNGIO::decimal_to_dms(const double& decimal) {
const double s = 3600.*(decimal - (double)d) - 60.*m;
std::ostringstream dms;
dms << d << "/1 " << static_cast<int>(m*100) << "/100 " << fixed << setprecision(6) << s << "/1";
dms << d << "/1 " << static_cast<int>(m*100.) << "/100 " << fixed << setprecision(6) << s << "/1";
return dms.str();
}
......
......@@ -473,7 +473,7 @@ void SMETIO::generateHeaderInfo(const StationData& sd, const bool& i_outputIsAsc
* - timezone
* - meta data (lat/lon/alt or east/north/alt/epsg if not part of data section)
*/
ostringstream ss;
std::ostringstream ss;
mywriter.set_header_value("station_id", sd.stationID);
if (!sd.stationName.empty())
......@@ -481,6 +481,7 @@ void SMETIO::generateHeaderInfo(const StationData& sd, const bool& i_outputIsAsc
mywriter.set_header_value("nodata", IOUtils::nodata);
std::vector<int> myprecision, mywidth; //set meaningful precision/width for each column
std::ostringstream plot_units, plot_description, plot_color, plot_min, plot_max;
if (i_outputIsAscii) {
ss << "timestamp";
......@@ -489,6 +490,11 @@ void SMETIO::generateHeaderInfo(const StationData& sd, const bool& i_outputIsAsc
myprecision.push_back(8);
mywidth.push_back(16);
}
plot_units << "time ";
plot_description << "time ";
plot_color << "#000000 ";
plot_min << IOUtils::nodata << " ";
plot_max << IOUtils::nodata << " ";
if (isConsistent) {
mywriter.set_header_value("latitude", sd.position.getLat());
......@@ -514,21 +520,82 @@ void SMETIO::generateHeaderInfo(const StationData& sd, const bool& i_outputIsAsc
int tmpwidth, tmpprecision;
for (size_t ll=0; ll<nr_of_parameters; ll++) {
if (vecParamInUse[ll]) {
string column = vecColumnName.at(ll);
std::string column( vecColumnName.at(ll) );
if (column == "RSWR") column = "OSWR";
ss << " " << column;
getFormatting(ll, tmpprecision, tmpwidth);
myprecision.push_back(tmpprecision);
mywidth.push_back(tmpwidth);
getPlotProperties(ll, plot_units, plot_description, plot_color, plot_min, plot_max);
}
}
mywriter.set_header_value("fields", ss.str());
mywriter.set_header_value("plot_units", plot_units.str());
mywriter.set_header_value("plot_description", plot_description.str());
mywriter.set_header_value("plot_color", plot_color.str());
mywriter.set_header_value("plot_min", plot_min.str());
mywriter.set_header_value("plot_max", plot_max.str());
mywriter.set_width(mywidth);
mywriter.set_precision(myprecision);
}
void SMETIO::getPlotProperties(const size_t& param, std::ostringstream &plot_units, std::ostringstream &plot_description, std::ostringstream &plot_color, std::ostringstream &plot_min, std::ostringstream &plot_max)
{
if (param==MeteoData::P) {
plot_units << "Pa "; plot_description << "local_air_pressure ";
plot_color << "#AEAEAE "; plot_min << "87000 "; plot_max << "115650 ";
} else if (param==MeteoData::TA) {
plot_units << "K "; plot_description << "air_temperature ";
plot_color << "#8324A4 "; plot_min << "253.15 "; plot_max << "283.15 ";
} else if (param==MeteoData::RH) {
plot_units << "na "; plot_description << "relative_humidity ";
plot_color << "#50CBDB "; plot_min << "0 "; plot_max << "1 ";
} else if (param==MeteoData::TSG) {
plot_units << "K "; plot_description << "ground_surface_temperature ";
plot_color << "#DE22E2 "; plot_min << "253.15 "; plot_max << "283.15 ";
} else if (param==MeteoData::TSS) {
plot_units << "K "; plot_description << "snow_surface_temperature ";
plot_color << "#FA72B7 "; plot_min << "253.15 "; plot_max << "283.15 ";
} else if (param==MeteoData::HS) {
plot_units << "m "; plot_description << "height_of_snow ";
plot_color << "#000000 "; plot_min << "0 "; plot_max << "3 ";
} else if (param==MeteoData::VW) {
plot_units << "m/s "; plot_description << "wind_velocity ";
plot_color << "#297E24 "; plot_min << "0 "; plot_max << "30 ";
} else if (param==MeteoData::DW) {
plot_units << "° "; plot_description << "wind_direction ";
plot_color << "#64DD78 "; plot_min << "0 "; plot_max << "360 ";
} else if (param==MeteoData::VW_MAX) {
plot_units << "m/s "; plot_description << "max_wind_velocity ";
plot_color << "#244A22 "; plot_min << "0 "; plot_max << "30 ";
} else if (param==MeteoData::RSWR) {
plot_units << "W/m2 "; plot_description << "outgoing_short_wave_radiation ";
plot_color << "#7D643A "; plot_min << "0 "; plot_max << "1400 ";
} else if (param==MeteoData::ISWR) {
plot_units << "W/m2 "; plot_description << "incoming_short_wave_radiation ";
plot_color << "#F9CA25 "; plot_min << "0 "; plot_max << "1400 ";
} else if (param==MeteoData::ILWR) {
plot_units << "W/m2 "; plot_description << "incoming_long_wave_radiation ";
plot_color << "#D99521 "; plot_min << "150 "; plot_max << "400 ";
} else if (param==MeteoData::TAU_CLD) {
plot_units << "na "; plot_description << "cloud_transmissivity ";
plot_color << "#D9A48F "; plot_min << "0 "; plot_max << "1 ";
} else if (param==MeteoData::PSUM) {
plot_units << "kg/m2 "; plot_description << "water_equivalent_precipitation_sum ";
plot_color << "#2431A4 "; plot_min << "0 "; plot_max << "20 ";
} else if (param==MeteoData::PSUM_PH) {
plot_units << "na "; plot_description << "precipitation_phase ";
plot_color << "#7E8EDF "; plot_min << "0 "; plot_max << "1 ";
} else {
plot_units << "na "; plot_description << "na ";
plot_color << "#A0A0A0 "; plot_min << IOUtils::nodata << " "; plot_max << IOUtils::nodata << " ";
}
}
void SMETIO::getFormatting(const size_t& param, int& prec, int& width)
{
/**
......
......@@ -66,7 +66,8 @@ class SMETIO : public IOInterface {
size_t getNrOfParameters(const std::string& stationname, const std::vector<MeteoData>& vecMeteo);
void checkForUsedParameters(const std::vector<MeteoData>& vecMeteo, const size_t& nr_parameters, double& tz,
std::vector<bool>& vecParamInUse, std::vector<std::string>& vecColumnName);
void getFormatting(const size_t& param, int& prec, int& width);
static void getPlotProperties(const size_t& param, std::ostringstream &plot_units, std::ostringstream &plot_description, std::ostringstream &plot_color, std::ostringstream &plot_min, std::ostringstream &plot_max);
static void getFormatting(const size_t& param, int& prec, int& width);
double olwr_to_tss(const double& olwr);
void generateHeaderInfo(const StationData& sd, const bool& i_outputIsAscii, const bool& isConsistent,
const double& timezone, const size_t& nr_of_parameters,
......
......@@ -651,6 +651,13 @@ void SMETWriter::write(const std::vector<double>& data)
fout.close();
}
void SMETWriter::print_if_exists(const std::string& header_field, std::ofstream& fout) const
{
const std::map<string,string>::const_iterator it = header.find(header_field);
if (it != header.end())
fout << std::left << std::setw(16) << header_field << " = " << it->second << "\n";
}
void SMETWriter::write_header(std::ofstream& fout)
{
if (!valid_header()) {
......@@ -664,63 +671,42 @@ void SMETWriter::write_header(std::ofstream& fout)
else fout << "BINARY" << "\n";
fout << "[HEADER]" << "\n";
fout << "station_id = " << header["station_id"] << "\n";
map<string,string>::const_iterator it = header.find("station_name");
if (it != header.end())
fout << "station_name = " << it->second << "\n";
print_if_exists("station_id", fout);
print_if_exists("station_name", fout);
if (location_in_header){
if (location_wgs84 == 7){
fout << "latitude = " << header["latitude"] << "\n";
fout << "longitude = " << header["longitude"] << "\n";
fout << "altitude = " << header["altitude"] << "\n";
print_if_exists("latitude", fout);
print_if_exists("longitude", fout);
print_if_exists("altitude", fout);
}
if (location_epsg == 15){
fout << "easting = " << header["easting"] << "\n";
fout << "northing = " << header["northing"] << "\n";
print_if_exists("easting", fout);
print_if_exists("northing", fout);
if (location_wgs84 != 7)
fout << "altitude = " << header["altitude"] << "\n";
fout << "epsg = " << header["epsg"] << "\n";
print_if_exists("altitude", fout);
print_if_exists("epsg", fout);
}
} else {
if (location_in_data_epsg)
fout << "epsg = " << header["epsg"] << "\n";
print_if_exists("epsg", fout);
}
fout << "nodata = " << header["nodata"] << "\n";
//Optional header keys:
it = header.find("tz");
if (it != header.end())
fout << "tz = " << it->second << "\n";
print_if_exists("nodata", fout);
it = header.find("creation");
if (it != header.end())
fout << "creation = " << it->second << "\n";
it = header.find("source");
if (it != header.end())
fout << "source = " << it->second << "\n";
it = header.find("units_offset");
if (it != header.end())
fout << "units_offset = " << it->second << "\n";
it = header.find("units_multiplier");
if (it != header.end())
fout << "units_multiplier = " << it->second << "\n";
it = header.find("comment");
if (it != header.end())
fout << "comment = " << it->second << "\n";
print_if_exists("tz", fout);
print_if_exists("creation", fout);
print_if_exists("source", fout);
print_if_exists("units_offset", fout);
print_if_exists("units_multiplier", fout);
print_if_exists("comment", fout);
for (size_t ii=0; ii<other_header_keys.size(); ii++){
fout << other_header_keys[ii] << " = " << header[other_header_keys[ii]] << "\n";
fout << std::left << std::setw(16) << other_header_keys[ii] << " = " << header[other_header_keys[ii]] << "\n";
}
fout << "fields = " << header["fields"] << "\n";
print_if_exists("fields", fout);
fout << "[DATA]" << endl;
}
......
......@@ -157,6 +157,7 @@ class SMETWriter {
void set_width(const std::vector<int>& vec_width);
private:
void print_if_exists(const std::string& header_field, std::ofstream& fout) const;
void write_header(std::ofstream& fout); //only writes when all necessary header values are set
void write_data_line_ascii(const std::string& timestamp, const std::vector<double>& data, std::ofstream& fout);
void write_data_line_binary(const std::vector<double>& data, std::ofstream& fout);
......
......@@ -12,12 +12,46 @@ printf "@page size 792, 612\n"
datum=$(date --rfc-3339=seconds)
printf "@timestamp def \"${datum}\"\n"
#print the color table and generate the table of indices
plot_colors=`head -100 ${INPUT} | grep "plot_color" | tr -s '\t' ' ' | cut -d'=' -f2 | tr ' ' '\n'`
sorted_plot_colors=`echo "${plot_colors}" | sort -u | tr '\n' ' ' `
color_table=`echo "${plot_colors}" | awk '
BEGIN {
split("'"${sorted_plot_colors}"'", sorted, " ")
}
/#/ {
for (color_idx in sorted) {
if ($1==sorted[color_idx]) {
printf("%d ", color_idx)
next
}
}
} '
`
echo "${sorted_plot_colors}" | tr ' ' '\n' | awk --non-decimal-data '
BEGIN {
printf("@map color 0 to (255, 255, 255), \"white\"\n")
}
/#/ {
col_count++
r=int( sprintf("%f", "0x" substr($1,2,2)) )
g=int( sprintf("%f", "0x" substr($1,4,2)) )
b=int( sprintf("%f", "0x" substr($1,6,2)) )
color_name=$1
if (r==0 && g==0 && b==0)
color_name="black"
printf("@map color %d to (%d, %d, %d), \"%s\"\n", col_count, r,g,b, color_name)
}
'
#create g0 graph
stat_id=$(head -20 ${INPUT} | grep "station_id" | tr -s '\t' ' ' | cut -d' ' -f 3-)
stat_name=$(head -20 ${INPUT} | grep "station_name" | tr -s '\t' ' ' | cut -d' ' -f 3-)
lat=$(head -20 ${INPUT} | grep "latitude" | tr -s '\t' ' ' | cut -d' ' -f 3-)
lon=$(head -20 ${INPUT} | grep "longitude" | tr -s '\t' ' ' | cut -d' ' -f 3-)
alt=$(head -20 ${INPUT} | grep "altitude" | tr -s '\t' ' ' | cut -d' ' -f 3-)
stat_id=`head -100 ${INPUT} | grep "station_id" | tr -s '\t' ' ' | cut -d' ' -f 3-`
stat_name=`head -100 ${INPUT} | grep "station_name" | tr -s '\t' ' ' | cut -d' ' -f 3-`
lat=`head -100 ${INPUT} | grep "latitude" | tr -s '\t' ' ' | cut -d' ' -f 3-`
lon=`head -100 ${INPUT} | grep "longitude" | tr -s '\t' ' ' | cut -d' ' -f 3-`
alt=`head -100 ${INPUT} | grep "altitude" | tr -s '\t' ' ' | cut -d' ' -f 3-`
printf "@g0 on\n"
printf "@g0 hidden false\n"
printf "@g0 type XY\n"
......@@ -42,8 +76,11 @@ printf "@ xaxis ticklabel on\n"
printf "@ xaxis ticklabel format yymmdd\n"
#create data sets metadata
columns=$(head -20 ${INPUT} | grep "fields")
columns=$(head -100 ${INPUT} | grep "fields")
echo "${columns}" | awk '
BEGIN {
split("'"${color_table}"'", color_table, " ")
}
/fields/ {
for(i=4; i<=NF; i++) {
f=i-4
......@@ -51,13 +88,13 @@ echo "${columns}" | awk '
printf("@ s%d type xy\n", f)
printf("@ s%d comment \"%s\"\n", f, $(i))
printf("@ s%d legend \"%s\"\n", f, $(i))
printf("@ s%d line color %d\n", f, color_table[f+2])
}
}
'
#create data sets data
nb_sets=$(echo "${columns}" | wc -w)
#for i in {4..${nb_sets}}; do
for (( i=4 ; i<=${nb_sets} ; i++ )); do
f=$(( i-4 ))
printf "@target G0.S${f}\n@type xy\n"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment