Skip to content

Commit

Permalink
Desktop: Handle separation of absolute and MSL pressure (#140)
Browse files Browse the repository at this point in the history
It will now use the samples_v2.dat data source where available and store absolute and mean sea level pressure separately in the cache database.

SC_Pressure column is now a coalesce of MSL and ABS pressure. There are separate SC_AbsolutePressure and SC_MeanSeaLevelPressure options at the data layer but these are not currently surfaced in the UI as no currently supported hardware type supplies both.
  • Loading branch information
davidrg committed Feb 11, 2022
1 parent ca4760d commit 3b7ad31
Show file tree
Hide file tree
Showing 30 changed files with 553 additions and 145 deletions.
8 changes: 8 additions & 0 deletions desktop/charts/graphstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ GraphStyle::GraphStyle(StandardColumn column) {
colour = colours.pressure;
name = tr("Pressure");
break;
case SC_AbsolutePressure:
colour = colours.pressure;
name = tr("Absolute Pressure");
break;
case SC_MeanSeaLevelPressure:
colour = colours.pressure;
name = tr("Mean Sea Level Pressure");
break;
case SC_Rainfall:
colour = colours.rainfall;
name = tr("Rainfall");
Expand Down
2 changes: 2 additions & 0 deletions desktop/charts/plotwidget/abstractaxistag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ void AbstractAxisTag::setStyle(const GraphStyle &style) {
case SC_WindChill:
case SC_DewPoint:
case SC_Pressure:
case SC_AbsolutePressure:
case SC_MeanSeaLevelPressure:
case SC_Rainfall:
case SC_AverageWindSpeed:
case SC_GustWindSpeed:
Expand Down
13 changes: 13 additions & 0 deletions desktop/charts/weatherplotter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ AxisType WeatherPlotter::axisTypeForColumn(StandardColumn column) {
return AT_HUMIDITY;

case SC_Pressure:
case SC_MeanSeaLevelPressure:
case SC_AbsolutePressure:
return AT_PRESSURE;

case SC_Rainfall:
Expand Down Expand Up @@ -401,6 +403,8 @@ QVector<double> WeatherPlotter::samplesForColumn(StandardColumn column, SampleSe
case SC_WindChill:
case SC_DewPoint:
case SC_Pressure:
case SC_AbsolutePressure:
case SC_MeanSeaLevelPressure:
case SC_Rainfall:
case SC_HighRainRate:
case SC_Evapotranspiration:
Expand Down Expand Up @@ -676,6 +680,9 @@ void WeatherPlotter::addGraphs(QMap<dataset_id_t, SampleSet> sampleSets)
addGenericGraph(ds, SC_Pressure, samples);
//addPressureGraph(samples);

if (ds.columns.standard.testFlag(SC_AbsolutePressure))
addGenericGraph(ds, SC_AbsolutePressure, samples);

if (ds.columns.standard.testFlag(SC_Rainfall))
addRainfallGraph(ds, samples, SC_Rainfall); // keep

Expand Down Expand Up @@ -1232,6 +1239,12 @@ void WeatherPlotter::removeGraphs(dataset_id_t dataSetId, SampleColumns columns)
if (columns.standard.testFlag(SC_Pressure))
columnList << SC_Pressure;

if (columns.standard.testFlag(SC_AbsolutePressure))
columnList << SC_AbsolutePressure;

if (columns.standard.testFlag(SC_MeanSeaLevelPressure))
columnList << SC_MeanSeaLevelPressure;

if (columns.standard.testFlag(SC_Rainfall))
columnList << SC_Rainfall;

Expand Down
16 changes: 16 additions & 0 deletions desktop/datasetmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ QVariant DataSetModel::data(const QModelIndex &index, int role) const
case SC_Pressure:
value = sampleSet.pressure[row];
break;
case SC_AbsolutePressure:
value = sampleSet.absolutePressure[row];
break;
case SC_MeanSeaLevelPressure:
value = sampleSet.meanSeaLevelPressure[row];
break;
case SC_Rainfall:
value = sampleSet.rainfall[row];
break;
Expand Down Expand Up @@ -367,6 +373,10 @@ QVariant DataSetModel::headerData(int section, Qt::Orientation orientation, int
return tr("Indoor Humidity (%1)").arg(unit);
case SC_Pressure:
return tr("Pressure (%1)").arg(unit);
case SC_AbsolutePressure:
return tr("Absolute Pressure (%1)").arg(unit);
case SC_MeanSeaLevelPressure:
return tr("Mean Sea Level Pressure (%1)").arg(unit);
case SC_Rainfall:
return tr("Rainfall (%1)").arg(unit);
case SC_AverageWindSpeed:
Expand Down Expand Up @@ -508,6 +518,12 @@ QList<StandardColumn> DataSetModel::getColumns() {
if (columns.standard.testFlag(SC_Pressure) && !sampleSet.pressure.isEmpty())
columnList << SC_Pressure;

if (columns.standard.testFlag(SC_AbsolutePressure) && !sampleSet.absolutePressure.isEmpty())
columnList << SC_AbsolutePressure;

if (columns.standard.testFlag(SC_MeanSeaLevelPressure) && !sampleSet.meanSeaLevelPressure.isEmpty())
columnList << SC_MeanSeaLevelPressure;

if (columns.standard.testFlag(SC_Rainfall) && !sampleSet.rainfall.isEmpty())
columnList << SC_Rainfall;

Expand Down
31 changes: 29 additions & 2 deletions desktop/datasource/databasedatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,10 @@ QString buildColumnList(SampleColumns columns, QString format,
query += format.arg("indoor_relative_humidity");
if (columns.standard.testFlag(SC_Humidity))
query += format.arg("relative_humidity");
if (columns.standard.testFlag(SC_Pressure))
if (columns.standard.testFlag(SC_AbsolutePressure) || columns.standard.testFlag(SC_Pressure))
query += format.arg("absolute_pressure");
if (columns.standard.testFlag(SC_MeanSeaLevelPressure) || columns.standard.testFlag(SC_Pressure))
query += format.arg("mean_sea_level_pressure");
if (columns.standard.testFlag(SC_AverageWindSpeed))
query += format.arg("average_wind_speed");
if (columns.standard.testFlag(SC_GustWindSpeed))
Expand Down Expand Up @@ -356,6 +358,10 @@ QString buildSelectForColumns(SampleColumns columns)
QString query = "select time_stamp";
query += buildColumnList(columns, ", %1", true, ", %1 ");

if (columns.standard.testFlag(SC_Pressure)) {
query += ", coalesce(mean_sea_level_pressure, absolute_pressure) as pressure ";
}

return query;
}

Expand Down Expand Up @@ -406,10 +412,19 @@ QString buildGroupedSelect(SampleColumns columns, AggregateFunction function, Ag
query += buildColumnList(nonSummables, ", avg(iq.%1) as %1 ", false);
}

// Pressure is handled specially
if (nonSummables.standard.testFlag(SC_Pressure)) {
query += ", avg(coalesce(iq.mean_sea_level_pressure, iq.absolute_pressure)) as pressure ";
}

} else {
SampleColumns cols = columns;
cols.standard = columns.standard & ~SC_Timestamp;
query += buildColumnList(cols, QString(", %1(iq.%2) as %2 ").arg(fn).arg("%1"), false);

if (cols.standard.testFlag(SC_Pressure)) {
query += QString(", %1(coalesce(iq.mean_sea_level_pressure, iq.absolute_pressure)) as pressure ").arg(fn);
}
}

// Start of subquery 'iq'
Expand All @@ -431,7 +446,7 @@ QString buildGroupedSelect(SampleColumns columns, AggregateFunction function, Ag
// from the current sample where as special columns (such as those unique
// to davis hardware or columns whose value is computed) need to be included
// in their self-qualified form in this column list. Davis-specific columns
// will expect to come from relation 'ds'.
// will expect to come from relation 'ds'.
query += buildColumnList(columns, ", cur.%1 ", true, ", %1 ");

// Rest of subquery 'iq'
Expand Down Expand Up @@ -476,6 +491,10 @@ QString buildGroupedSelect(SampleColumns columns, AggregateFunction function, Ag
outer_query += buildColumnList(nonSummables, ", grouped.%1 as %1 ", false);
}

if (nonSummables.standard.testFlag(SC_Pressure)) {
outer_query += ", grouped.pressure as pressure ";
}

outer_query += " from (" + query + ") as grouped order by grouped.time_stamp asc";
query = outer_query;
qDebug() << query;
Expand Down Expand Up @@ -832,8 +851,16 @@ void DatabaseDataSource::fetchSamples(SampleColumns columns,

if (columns.standard.testFlag(SC_Pressure))
samples.pressure.append(nullableVariantDouble(
record.value("pressure")));

if (columns.standard.testFlag(SC_AbsolutePressure))
samples.absolutePressure.append(nullableVariantDouble(
record.value("absolute_pressure")));

if (columns.standard.testFlag(SC_MeanSeaLevelPressure))
samples.meanSeaLevelPressure.append(nullableVariantDouble(
record.value("mean_sea_level_pressure")));

if (columns.standard.testFlag(SC_Rainfall))
samples.rainfall.append(nullableVariantDouble(record.value("rainfall")));

Expand Down
14 changes: 9 additions & 5 deletions desktop/datasource/samplecolumns.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#include "aggregate.h"

// We could really really do with a 64bit qflags!

enum StandardColumn {
SC_NoColumns = 0x00000000,
SC_Timestamp = 0x00000001,
Expand All @@ -25,7 +27,7 @@ enum StandardColumn {
SC_DewPoint = 0x00000020,
SC_Humidity = 0x00000040,
SC_IndoorHumidity = 0x00000080,
SC_Pressure = 0x00000100,
SC_Pressure = 0x00000100, // MSL pressure if available, else absolute.
SC_Rainfall = 0x00000200,
SC_AverageWindSpeed = 0x00000400,
SC_GustWindSpeed = 0x00000800,
Expand All @@ -40,8 +42,10 @@ enum StandardColumn {
SC_Evapotranspiration = 0x00100000, // Vantage Pro2+
SC_HighSolarRadiation = 0x00200000, // Vantage Pro2+
SC_HighUVIndex = 0x00400000, // Vantage Pro2+
SC_ForecastRuleId = 0x00800000 // Davis
// Space for 8 more columns
SC_ForecastRuleId = 0x00800000, // Davis
SC_AbsolutePressure = 0x01000000, // Absolute pressure. May be null if the station doesn't support this.
SC_MeanSeaLevelPressure= 0x02000000 // Relative pressure. May be null if the station doesn't support this.
// Space for 6 more columns
};
Q_DECLARE_FLAGS(StandardColumns, StandardColumn)

Expand Down Expand Up @@ -84,7 +88,7 @@ typedef struct _sample_column {
SC_SolarRadiation | SC_UV_Index | SC_Reception | SC_HighTemperature | \
SC_LowTemperature | SC_HighRainRate | SC_GustWindDirection | \
SC_Evapotranspiration | SC_HighSolarRadiation | SC_HighUVIndex | \
SC_ForecastRuleId )
SC_ForecastRuleId | SC_MeanSeaLevelPressure | SC_AbsolutePressure )

#define ALL_EXTRA_COLUMNS (EC_LeafWetness1 | EC_LeafWetness2 | EC_LeafTemperature1 | \
EC_LeafTemperature2 | EC_SoilMoisture1 | EC_SoilMoisture2 | EC_SoilMoisture3 | \
Expand All @@ -110,7 +114,7 @@ typedef struct _sample_column {
| SC_HighSolarRadiation | SC_HighUVIndex)

#define OTHER_COLUMNS (SC_Pressure | SC_Rainfall | SC_HighRainRate | \
SC_Reception)
SC_Reception | SC_MeanSeaLevelPressure | SC_AbsolutePressure)

#define SOIL_COLUMNS (EC_SoilMoisture1 | EC_SoilMoisture2 | \
EC_SoilMoisture3 | EC_SoilMoisture4 | EC_SoilTemperature1 | \
Expand Down
9 changes: 9 additions & 0 deletions desktop/datasource/sampleset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ void ReserveSampleSetSpace(SampleSet& samples, int size, SampleColumns columns)
if (columns.standard.testFlag(SC_Pressure))
samples.pressure.reserve(size);

if (columns.standard.testFlag(SC_AbsolutePressure))
samples.absolutePressure.reserve(size);

if (columns.standard.testFlag(SC_MeanSeaLevelPressure))
samples.meanSeaLevelPressure.reserve(size);

if (columns.standard.testFlag(SC_Rainfall))
samples.rainfall.reserve(size);

Expand Down Expand Up @@ -158,6 +164,9 @@ void AppendNullSample(SampleSet &samples, SampleColumns columns, QDateTime time)
if (columns.standard.testFlag(SC_Pressure))
samples.pressure.append(qQNaN());

if (columns.standard.testFlag(SC_MeanSeaLevelPressure))
samples.pressure.append(qQNaN());

if (columns.standard.testFlag(SC_Rainfall))
samples.rainfall.append(qQNaN());

Expand Down
7 changes: 7 additions & 0 deletions desktop/datasource/sampleset.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ typedef struct _SampleSet {

// Pressure
QVector<double> pressure;
QVector<double> absolutePressure;
QVector<double> meanSeaLevelPressure;

QVector<double> rainfall;

// Wind speed
Expand Down Expand Up @@ -110,6 +113,8 @@ typedef struct _Sample {

// Pressure / rain
double pressure;
double meanSeaLevelPressure;

double rainfall;

// Wind
Expand Down Expand Up @@ -164,6 +169,8 @@ inline UnitConversions::unit_t SampleColumnUnits(StandardColumn column) {
case SC_IndoorHumidity:
return U_HUMIDITY;
case SC_Pressure:
case SC_AbsolutePressure:
case SC_MeanSeaLevelPressure:
return U_HECTOPASCALS;
case SC_Rainfall:
case SC_Evapotranspiration:
Expand Down
1 change: 1 addition & 0 deletions desktop/datasource/station_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ typedef struct _station_info_t {
bool hasSolarAndUV;
bool isValid;
hardware_type_t hardwareType;
unsigned int apiLevel; /*!< API level for remote stations */
} station_info_t;

typedef struct _sample_range_t {
Expand Down
Loading

0 comments on commit 3b7ad31

Please sign in to comment.