@golemio/microclimate 1.1.6 → 1.1.7-dev.887727768
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var dbm;
|
|
4
|
+
var type;
|
|
5
|
+
var seed;
|
|
6
|
+
var fs = require('fs');
|
|
7
|
+
var path = require('path');
|
|
8
|
+
var Promise;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* We receive the dbmigrate dependency from dbmigrate initially.
|
|
12
|
+
* This enables us to not have to rely on NODE_PATH.
|
|
13
|
+
*/
|
|
14
|
+
exports.setup = function(options, seedLink) {
|
|
15
|
+
dbm = options.dbmigrate;
|
|
16
|
+
type = dbm.dataType;
|
|
17
|
+
seed = seedLink;
|
|
18
|
+
Promise = options.Promise;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
exports.up = function(db) {
|
|
22
|
+
var filePath = path.join(__dirname, 'sqls', '20230602112229-new-sensors-up.sql');
|
|
23
|
+
return new Promise( function( resolve, reject ) {
|
|
24
|
+
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
|
|
25
|
+
if (err) return reject(err);
|
|
26
|
+
console.log('received data: ' + data);
|
|
27
|
+
|
|
28
|
+
resolve(data);
|
|
29
|
+
});
|
|
30
|
+
})
|
|
31
|
+
.then(function(data) {
|
|
32
|
+
return db.runSql(data);
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
exports.down = function(db) {
|
|
37
|
+
var filePath = path.join(__dirname, 'sqls', '20230602112229-new-sensors-down.sql');
|
|
38
|
+
return new Promise( function( resolve, reject ) {
|
|
39
|
+
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
|
|
40
|
+
if (err) return reject(err);
|
|
41
|
+
console.log('received data: ' + data);
|
|
42
|
+
|
|
43
|
+
resolve(data);
|
|
44
|
+
});
|
|
45
|
+
})
|
|
46
|
+
.then(function(data) {
|
|
47
|
+
return db.runSql(data);
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
exports._meta = {
|
|
52
|
+
"version": 1
|
|
53
|
+
};
|
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
DROP VIEW microclimate.v_sensor_points;
|
|
2
|
+
|
|
3
|
+
DROP VIEW microclimate.v_sensor_measurements;
|
|
4
|
+
|
|
5
|
+
DROP TABLE microclimate.sensor_devices_import;
|
|
6
|
+
|
|
7
|
+
CREATE TABLE microclimate.sensor_devices_import (
|
|
8
|
+
location_id int4 NULL,
|
|
9
|
+
point_id int4 NULL,
|
|
10
|
+
"location" varchar(50) NULL,
|
|
11
|
+
loc_description varchar(50) NULL,
|
|
12
|
+
loc_orientation varchar(50) NULL,
|
|
13
|
+
loc_surface varchar(50) NULL,
|
|
14
|
+
point_name varchar(50) NULL,
|
|
15
|
+
point_whole_name varchar(50) NULL,
|
|
16
|
+
address varchar(50) NULL,
|
|
17
|
+
sensor_position varchar(50) NULL,
|
|
18
|
+
sensor_position_detail varchar(50) NULL,
|
|
19
|
+
sensor_id varchar(50) NOT NULL,
|
|
20
|
+
data_relevance varchar(50) NULL,
|
|
21
|
+
lat float4 NULL,
|
|
22
|
+
lng float4 NULL,
|
|
23
|
+
air_temp int4 NULL,
|
|
24
|
+
air_hum int4 NULL,
|
|
25
|
+
pressure int4 NULL,
|
|
26
|
+
wind_dir int4 NULL,
|
|
27
|
+
wind_impact int4 NULL,
|
|
28
|
+
wind_speed int4 NULL,
|
|
29
|
+
precip int4 NULL,
|
|
30
|
+
sun_irr int4 NULL,
|
|
31
|
+
soil_temp int4 NULL,
|
|
32
|
+
water_pot int4 NULL,
|
|
33
|
+
dendro_circ int4 NULL,
|
|
34
|
+
dendro_gain int4 NULL,
|
|
35
|
+
CONSTRAINT sensor_devices_import_pkey PRIMARY KEY (sensor_id)
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
CREATE OR REPLACE VIEW microclimate.v_sensor_points
|
|
39
|
+
AS SELECT DISTINCT s.point_id,
|
|
40
|
+
s.point_name,
|
|
41
|
+
s.location_id,
|
|
42
|
+
s.location,
|
|
43
|
+
s.loc_description,
|
|
44
|
+
s.loc_orientation,
|
|
45
|
+
s.loc_surface,
|
|
46
|
+
s.lat,
|
|
47
|
+
s.lng,
|
|
48
|
+
NULL::text AS x_jtsk,
|
|
49
|
+
NULL::text AS y_jtsk,
|
|
50
|
+
NULL::text AS elevation_m,
|
|
51
|
+
s.sensor_position,
|
|
52
|
+
s.sensor_position_detail,
|
|
53
|
+
concat(unpivot.measure, unpivot.height_cm) AS measure,
|
|
54
|
+
CASE
|
|
55
|
+
WHEN unpivot.measure = 'air_temp'::text THEN 'Teplota vzduchu'::text
|
|
56
|
+
WHEN unpivot.measure = 'air_hum'::text THEN 'Vlhkost vzduchu'::text
|
|
57
|
+
WHEN unpivot.measure = 'pressure'::text THEN 'Atmosférický tlak'::text
|
|
58
|
+
WHEN unpivot.measure = 'wind_dir'::text THEN 'Směr větru'::text
|
|
59
|
+
WHEN unpivot.measure = 'wind_impact'::text THEN 'Náraz větru'::text
|
|
60
|
+
WHEN unpivot.measure = 'wind_speed'::text THEN 'Rychlost větru'::text
|
|
61
|
+
WHEN unpivot.measure = 'precip'::text THEN 'Úhrn srážek'::text
|
|
62
|
+
WHEN unpivot.measure = 'sun_irr'::text THEN 'Sluneční záření'::text
|
|
63
|
+
WHEN unpivot.measure = 'soil_temp'::text THEN 'Teplota půdy'::text
|
|
64
|
+
WHEN unpivot.measure = 'water_pot'::text THEN 'Vodní potenciál půdy'::text
|
|
65
|
+
WHEN unpivot.measure = 'dendro_circ'::text THEN 'Obvod stromu'::text
|
|
66
|
+
WHEN unpivot.measure = 'dendro_gain'::text THEN 'Přírůstek obvodu stromu'::text
|
|
67
|
+
ELSE NULL::text
|
|
68
|
+
END AS measure_cz,
|
|
69
|
+
CASE
|
|
70
|
+
WHEN unpivot.measure = 'air_temp'::text THEN '°C'::text
|
|
71
|
+
WHEN unpivot.measure = 'air_hum'::text THEN '%'::text
|
|
72
|
+
WHEN unpivot.measure = 'pressure'::text THEN 'Pa'::text
|
|
73
|
+
WHEN unpivot.measure = 'wind_dir'::text THEN '°'::text
|
|
74
|
+
WHEN unpivot.measure = 'wind_impact'::text THEN 'km/h'::text
|
|
75
|
+
WHEN unpivot.measure = 'wind_speed'::text THEN 'km/h'::text
|
|
76
|
+
WHEN unpivot.measure = 'precip'::text THEN 'mm'::text
|
|
77
|
+
WHEN unpivot.measure = 'sun_irr'::text THEN 'lux'::text
|
|
78
|
+
WHEN unpivot.measure = 'soil_temp'::text THEN '°C'::text
|
|
79
|
+
WHEN unpivot.measure = 'water_pot'::text THEN 'kPa'::text
|
|
80
|
+
WHEN unpivot.measure = 'dendro_circ'::text THEN 'mm'::text
|
|
81
|
+
WHEN unpivot.measure = 'dendro_gain'::text THEN 'µm'::text
|
|
82
|
+
ELSE NULL::text
|
|
83
|
+
END AS unit
|
|
84
|
+
FROM microclimate.sensor_devices_import s,
|
|
85
|
+
LATERAL ( VALUES ('air_temp'::text,s.air_temp), ('air_hum'::text,s.air_hum), ('pressure'::text,s.pressure), ('wind_dir'::text,s.wind_dir), ('wind_impact'::text,s.wind_impact), ('wind_speed'::text,s.wind_speed), ('precip'::text,s.precip), ('sun_irr'::text,s.sun_irr), ('soil_temp'::text,s.soil_temp), ('water_pot'::text,s.water_pot), ('dendro_circ'::text,s.dendro_circ), ('dendro_gain'::text,s.dendro_gain)) unpivot(measure, height_cm)
|
|
86
|
+
WHERE unpivot.height_cm IS NOT NULL
|
|
87
|
+
ORDER BY s.point_id;
|
|
88
|
+
|
|
89
|
+
CREATE OR REPLACE VIEW microclimate.v_sensor_measurements
|
|
90
|
+
AS WITH points_heights AS (
|
|
91
|
+
SELECT s.sensor_id,
|
|
92
|
+
s.point_id,
|
|
93
|
+
s.location_id,
|
|
94
|
+
unpivot.measure,
|
|
95
|
+
unpivot.height_cm,
|
|
96
|
+
CASE
|
|
97
|
+
WHEN unpivot.measure = 'air_temp'::text THEN '°C'::text
|
|
98
|
+
WHEN unpivot.measure = 'air_hum'::text THEN '%'::text
|
|
99
|
+
WHEN unpivot.measure = 'pressure'::text THEN 'Pa'::text
|
|
100
|
+
WHEN unpivot.measure = 'wind_dir'::text THEN '°'::text
|
|
101
|
+
WHEN unpivot.measure = 'wind_impact'::text THEN 'km/h'::text
|
|
102
|
+
WHEN unpivot.measure = 'wind_speed'::text THEN 'km/h'::text
|
|
103
|
+
WHEN unpivot.measure = 'precip'::text THEN 'mm'::text
|
|
104
|
+
WHEN unpivot.measure = 'sun_irr'::text THEN 'lux'::text
|
|
105
|
+
WHEN unpivot.measure = 'soil_temp'::text THEN '°C'::text
|
|
106
|
+
WHEN unpivot.measure = 'water_pot'::text THEN 'kPa'::text
|
|
107
|
+
WHEN unpivot.measure = 'dendro_circ'::text THEN 'mm'::text
|
|
108
|
+
WHEN unpivot.measure = 'dendro_gain'::text THEN 'µm'::text
|
|
109
|
+
ELSE NULL::text
|
|
110
|
+
END AS unit
|
|
111
|
+
FROM microclimate.sensor_devices_import s,
|
|
112
|
+
LATERAL ( VALUES ('air_temp'::text,s.air_temp), ('air_hum'::text,s.air_hum), ('pressure'::text,s.pressure), ('wind_dir'::text,s.wind_dir), ('wind_impact'::text,s.wind_impact), ('wind_speed'::text,s.wind_speed), ('precip'::text,s.precip), ('sun_irr'::text,s.sun_irr), ('soil_temp'::text,s.soil_temp), ('water_pot'::text,s.water_pot), ('dendro_circ'::text,s.dendro_circ), ('dendro_gain'::text,s.dendro_gain)) unpivot(measure, height_cm)
|
|
113
|
+
WHERE unpivot.height_cm IS NOT NULL
|
|
114
|
+
), measurements AS (
|
|
115
|
+
SELECT m.sensor_id,
|
|
116
|
+
m.measured_at,
|
|
117
|
+
unpivot.measure,
|
|
118
|
+
unpivot.value
|
|
119
|
+
FROM microclimate.measurements m,
|
|
120
|
+
LATERAL ( VALUES ('air_temp'::text,m.air_temp), ('air_hum'::text,m.air_hum), ('pressure'::text,m.pressure), ('wind_dir'::text,
|
|
121
|
+
CASE
|
|
122
|
+
WHEN m.wind_dir::text = 'N'::text THEN 0
|
|
123
|
+
WHEN m.wind_dir::text = 'NE'::text THEN 45
|
|
124
|
+
WHEN m.wind_dir::text = 'E'::text THEN 90
|
|
125
|
+
WHEN m.wind_dir::text = 'SE'::text THEN 135
|
|
126
|
+
WHEN m.wind_dir::text = 'S'::text THEN 180
|
|
127
|
+
WHEN m.wind_dir::text = 'SW'::text THEN 225
|
|
128
|
+
WHEN m.wind_dir::text = 'W'::text THEN 270
|
|
129
|
+
WHEN m.wind_dir::text = 'NW'::text THEN 315
|
|
130
|
+
ELSE NULL::integer
|
|
131
|
+
END), ('wind_impact'::text,m.wind_impact), ('wind_speed'::text,m.wind_speed), ('precip'::text,m.precip), ('sun_irr'::text,m.sun_irr), ('soil_temp'::text,m.soil_temp), ('water_pot'::text,m.water_pot), ('dendro_circ'::text,m.dendro_circ), ('dendro_gain'::text,m.dendro_gain)) unpivot(measure, value)
|
|
132
|
+
WHERE unpivot.value IS NOT NULL
|
|
133
|
+
)
|
|
134
|
+
SELECT q.sensor_id,
|
|
135
|
+
q.measured_at,
|
|
136
|
+
q.point_id,
|
|
137
|
+
q.location_id,
|
|
138
|
+
q.measure,
|
|
139
|
+
q.value,
|
|
140
|
+
q.unit,
|
|
141
|
+
row_number() OVER (PARTITION BY q.point_id, q.measure ORDER BY q.point_id, q.measure, q.measured_at DESC) AS rn
|
|
142
|
+
FROM ( SELECT measurements.sensor_id,
|
|
143
|
+
measurements.measured_at,
|
|
144
|
+
points_heights.point_id,
|
|
145
|
+
points_heights.location_id,
|
|
146
|
+
concat(measurements.measure, points_heights.height_cm) AS measure,
|
|
147
|
+
measurements.value,
|
|
148
|
+
points_heights.unit
|
|
149
|
+
FROM measurements
|
|
150
|
+
JOIN points_heights ON points_heights.sensor_id::text = measurements.sensor_id::text AND points_heights.measure = measurements.measure) q
|
|
151
|
+
ORDER BY q.point_id, q.measure, q.measured_at DESC;
|
|
152
|
+
|
|
153
|
+
CREATE OR REPLACE VIEW analytic.v_microclimate_measurement_hights
|
|
154
|
+
AS SELECT DISTINCT s.sensor_id,
|
|
155
|
+
unpivot.measure,
|
|
156
|
+
unpivot.hight_cm
|
|
157
|
+
FROM microclimate.sensor_devices_import s,
|
|
158
|
+
LATERAL ( VALUES
|
|
159
|
+
('avg_temperature'::text,s.air_temp),
|
|
160
|
+
('max_temperature'::text,s.air_temp),
|
|
161
|
+
('min_temperature'::text,s.air_temp),
|
|
162
|
+
('avg_air_hum'::text,s.air_hum),
|
|
163
|
+
('avg_pressure'::text,s.pressure),
|
|
164
|
+
('max_wind_speed'::text,s.wind_speed),
|
|
165
|
+
('avg_wind_speed'::text,s.wind_speed),
|
|
166
|
+
('sum_precip'::text,s.precip),
|
|
167
|
+
('avg_soil_temp'::text,s.soil_temp),
|
|
168
|
+
('avg_water_pot'::text,s.water_pot),
|
|
169
|
+
('wind_dir_at_max_speed'::text,s.wind_dir)) unpivot(measure, hight_cm);
|
|
170
|
+
|
|
171
|
+
CREATE OR REPLACE VIEW analytic.v_microclimate_measurements_daily
|
|
172
|
+
AS WITH calendar AS (
|
|
173
|
+
SELECT calendar_1.date,
|
|
174
|
+
sensors_1.sensor_id
|
|
175
|
+
FROM ( SELECT generate_series('2022-08-02 00:00:00+02'::timestamp with time zone, CURRENT_DATE::timestamp with time zone, '1 day'::interval)::date AS date) calendar_1
|
|
176
|
+
CROSS JOIN ( SELECT DISTINCT sensor_devices_import.sensor_id
|
|
177
|
+
FROM microclimate.sensor_devices_import) sensors_1
|
|
178
|
+
), first_measurement_per_hour AS (
|
|
179
|
+
SELECT DISTINCT ON (m.sensor_id, (timezone('CET'::text, m.measured_at)::date), (date_part('hour'::text, timezone('CET'::text, m.measured_at)))) m.sensor_id,
|
|
180
|
+
timezone('CET'::text, m.measured_at) AS measured_at,
|
|
181
|
+
timezone('CET'::text, m.measured_at)::date AS date,
|
|
182
|
+
date_part('hour'::text, timezone('CET'::text, m.measured_at)) AS hour,
|
|
183
|
+
m.air_temp,
|
|
184
|
+
m.air_hum,
|
|
185
|
+
m.pressure,
|
|
186
|
+
m.wind_speed,
|
|
187
|
+
m.water_pot,
|
|
188
|
+
m.soil_temp
|
|
189
|
+
FROM microclimate.measurements m
|
|
190
|
+
WHERE date_part('hour'::text, timezone('CET'::text, m.measured_at)) = ANY (ARRAY[7::double precision, 14::double precision, 21::double precision])
|
|
191
|
+
ORDER BY m.sensor_id, (timezone('CET'::text, m.measured_at)::date), (date_part('hour'::text, timezone('CET'::text, m.measured_at))), m.measured_at
|
|
192
|
+
), seven AS (
|
|
193
|
+
SELECT first_measurement_per_hour.sensor_id,
|
|
194
|
+
first_measurement_per_hour.measured_at,
|
|
195
|
+
first_measurement_per_hour.date,
|
|
196
|
+
first_measurement_per_hour.hour,
|
|
197
|
+
first_measurement_per_hour.air_temp,
|
|
198
|
+
first_measurement_per_hour.air_hum,
|
|
199
|
+
first_measurement_per_hour.pressure,
|
|
200
|
+
first_measurement_per_hour.wind_speed,
|
|
201
|
+
first_measurement_per_hour.water_pot,
|
|
202
|
+
first_measurement_per_hour.soil_temp
|
|
203
|
+
FROM first_measurement_per_hour
|
|
204
|
+
WHERE first_measurement_per_hour.hour = 7::double precision
|
|
205
|
+
), two AS (
|
|
206
|
+
SELECT first_measurement_per_hour.sensor_id,
|
|
207
|
+
first_measurement_per_hour.measured_at,
|
|
208
|
+
first_measurement_per_hour.date,
|
|
209
|
+
first_measurement_per_hour.hour,
|
|
210
|
+
first_measurement_per_hour.air_temp,
|
|
211
|
+
first_measurement_per_hour.air_hum,
|
|
212
|
+
first_measurement_per_hour.pressure,
|
|
213
|
+
first_measurement_per_hour.wind_speed,
|
|
214
|
+
first_measurement_per_hour.water_pot,
|
|
215
|
+
first_measurement_per_hour.soil_temp
|
|
216
|
+
FROM first_measurement_per_hour
|
|
217
|
+
WHERE first_measurement_per_hour.hour = 14::double precision
|
|
218
|
+
), nine AS (
|
|
219
|
+
SELECT first_measurement_per_hour.sensor_id,
|
|
220
|
+
first_measurement_per_hour.measured_at,
|
|
221
|
+
first_measurement_per_hour.date,
|
|
222
|
+
first_measurement_per_hour.hour,
|
|
223
|
+
first_measurement_per_hour.air_temp,
|
|
224
|
+
first_measurement_per_hour.air_hum,
|
|
225
|
+
first_measurement_per_hour.pressure,
|
|
226
|
+
first_measurement_per_hour.wind_speed,
|
|
227
|
+
first_measurement_per_hour.water_pot,
|
|
228
|
+
first_measurement_per_hour.soil_temp
|
|
229
|
+
FROM first_measurement_per_hour
|
|
230
|
+
WHERE first_measurement_per_hour.hour = 21::double precision
|
|
231
|
+
), wind AS (
|
|
232
|
+
SELECT DISTINCT ON (m.sensor_id, (timezone('CET'::text, m.measured_at)::date)) m.sensor_id,
|
|
233
|
+
timezone('CET'::text, m.measured_at)::date AS date,
|
|
234
|
+
m.wind_speed AS max_wind_speed,
|
|
235
|
+
CASE
|
|
236
|
+
WHEN m.wind_dir::text = 'N'::text THEN 0
|
|
237
|
+
WHEN m.wind_dir::text = 'NE'::text THEN 45
|
|
238
|
+
WHEN m.wind_dir::text = 'E'::text THEN 90
|
|
239
|
+
WHEN m.wind_dir::text = 'SE'::text THEN 135
|
|
240
|
+
WHEN m.wind_dir::text = 'S'::text THEN 180
|
|
241
|
+
WHEN m.wind_dir::text = 'SW'::text THEN 225
|
|
242
|
+
WHEN m.wind_dir::text = 'W'::text THEN 270
|
|
243
|
+
WHEN m.wind_dir::text = 'NW'::text THEN 315
|
|
244
|
+
ELSE NULL::integer
|
|
245
|
+
END AS wind_dir_at_max_speed
|
|
246
|
+
FROM microclimate.measurements m
|
|
247
|
+
ORDER BY m.sensor_id, (timezone('CET'::text, m.measured_at)::date), m.wind_speed DESC
|
|
248
|
+
), min_max_daily_temp AS (
|
|
249
|
+
SELECT m.sensor_id,
|
|
250
|
+
timezone('-04'::text, m.measured_at)::date AS date,
|
|
251
|
+
max(m.air_temp) AS max_temperature,
|
|
252
|
+
min(m.air_temp) AS min_temperature
|
|
253
|
+
FROM microclimate.measurements m
|
|
254
|
+
GROUP BY m.sensor_id, (timezone('-04'::text, m.measured_at)::date)
|
|
255
|
+
ORDER BY m.sensor_id, (timezone('-04'::text, m.measured_at)::date)
|
|
256
|
+
), precip AS (
|
|
257
|
+
SELECT m.sensor_id,
|
|
258
|
+
timezone('+06'::text, m.measured_at)::date AS date,
|
|
259
|
+
sum(m.precip) AS sum_precip
|
|
260
|
+
FROM microclimate.measurements m
|
|
261
|
+
GROUP BY m.sensor_id, (timezone('+06'::text, m.measured_at)::date)
|
|
262
|
+
ORDER BY m.sensor_id, (timezone('+06'::text, m.measured_at)::date)
|
|
263
|
+
), calc AS (
|
|
264
|
+
SELECT sensors.address,
|
|
265
|
+
sensors.sensor_position_detail,
|
|
266
|
+
sensors.point_id,
|
|
267
|
+
sensors.air_temp,
|
|
268
|
+
sensors.air_hum,
|
|
269
|
+
sensors.pressure,
|
|
270
|
+
sensors.precip,
|
|
271
|
+
sensors.wind_dir,
|
|
272
|
+
sensors.wind_impact,
|
|
273
|
+
sensors.wind_speed,
|
|
274
|
+
sensors.sun_irr,
|
|
275
|
+
sensors.soil_temp,
|
|
276
|
+
sensors.water_pot,
|
|
277
|
+
sensors.dendro_circ,
|
|
278
|
+
sensors.dendro_gain,
|
|
279
|
+
calendar.date,
|
|
280
|
+
calendar.sensor_id,
|
|
281
|
+
(seven.air_temp + two.air_temp + 2::double precision * nine.air_temp) / 4::double precision AS avg_temperature,
|
|
282
|
+
(seven.air_hum + two.air_hum + nine.air_hum) / 3::double precision AS avg_air_hum,
|
|
283
|
+
(seven.pressure + two.pressure + nine.pressure) / 3 / 100 AS avg_pressure,
|
|
284
|
+
(seven.wind_speed + two.wind_speed + nine.wind_speed) / 3::double precision AS avg_wind_speed,
|
|
285
|
+
(seven.water_pot + two.water_pot + nine.water_pot) / 3::double precision AS avg_water_pot,
|
|
286
|
+
(seven.soil_temp + two.soil_temp + nine.soil_temp) / 3::double precision AS avg_soil_temp,
|
|
287
|
+
wind.max_wind_speed,
|
|
288
|
+
wind.wind_dir_at_max_speed,
|
|
289
|
+
min_max_daily_temp.max_temperature,
|
|
290
|
+
min_max_daily_temp.min_temperature,
|
|
291
|
+
precip.sum_precip
|
|
292
|
+
FROM calendar
|
|
293
|
+
LEFT JOIN seven ON calendar.sensor_id::text = seven.sensor_id::text AND calendar.date = seven.date
|
|
294
|
+
LEFT JOIN two ON calendar.sensor_id::text = two.sensor_id::text AND calendar.date = two.date
|
|
295
|
+
LEFT JOIN nine ON calendar.sensor_id::text = nine.sensor_id::text AND calendar.date = nine.date
|
|
296
|
+
LEFT JOIN wind ON calendar.sensor_id::text = wind.sensor_id::text AND calendar.date = wind.date
|
|
297
|
+
LEFT JOIN min_max_daily_temp ON calendar.sensor_id::text = min_max_daily_temp.sensor_id::text AND calendar.date = min_max_daily_temp.date
|
|
298
|
+
LEFT JOIN precip ON calendar.sensor_id::text = precip.sensor_id::text AND calendar.date = precip.date
|
|
299
|
+
LEFT JOIN microclimate.sensor_devices_import sensors ON calendar.sensor_id::text = sensors.sensor_id::text
|
|
300
|
+
)
|
|
301
|
+
SELECT calc.address,
|
|
302
|
+
calc.sensor_position_detail,
|
|
303
|
+
calc.date,
|
|
304
|
+
calc.sensor_id,
|
|
305
|
+
calc.point_id,
|
|
306
|
+
unpivot.metrika,
|
|
307
|
+
unpivot.value
|
|
308
|
+
FROM calc,
|
|
309
|
+
LATERAL ( VALUES ('avg_temperature'::text,calc.avg_temperature), ('avg_air_hum'::text,calc.avg_air_hum), ('avg_pressure'::text,calc.avg_pressure), ('avg_wind_speed'::text,calc.avg_wind_speed), ('avg_water_pot'::text,calc.avg_water_pot), ('avg_soil_temp'::text,calc.avg_soil_temp), ('max_wind_speed'::text,calc.max_wind_speed), ('max_temperature'::text,calc.max_temperature), ('min_temperature'::text,calc.min_temperature), ('sum_precip'::text,calc.sum_precip), ('wind_dir_at_max_speed'::text,calc.wind_dir_at_max_speed)) unpivot(metrika, value)
|
|
310
|
+
WHERE unpivot.value IS NOT NULL;
|
|
311
|
+
|
|
312
|
+
CREATE OR REPLACE VIEW analytic.v_microclimate_raw_data
|
|
313
|
+
AS WITH tmp_hights AS (
|
|
314
|
+
SELECT DISTINCT s.sensor_id,
|
|
315
|
+
s.address,
|
|
316
|
+
s.sensor_position_detail,
|
|
317
|
+
unpivot.measure,
|
|
318
|
+
unpivot.hight_cm
|
|
319
|
+
FROM microclimate.sensor_devices_import s,
|
|
320
|
+
LATERAL ( VALUES ('Teplota vzduchu'::text,s.air_temp), ('Vlhkost vzduchu'::text,s.air_hum), ('Tlak vzduchu'::text,s.pressure), ('Náraz a směr větru'::text,s.wind_impact), ('Rychlost a směr větru'::text,s.wind_speed), ('Úhrn srážek'::text,s.precip), ('Sluneční svit'::text,s.sun_irr), ('Vodní potenciál půdy'::text,s.water_pot), ('Teplota půdy'::text,s.soil_temp), ('Obvod stromu'::text,s.dendro_circ), ('Přírůstek obvodu stromu'::text,s.dendro_gain)) unpivot(measure, hight_cm)
|
|
321
|
+
WHERE unpivot.hight_cm IS NOT NULL
|
|
322
|
+
), tmp_measures AS (
|
|
323
|
+
SELECT m_1.sensor_id,
|
|
324
|
+
m_1.measured_at,
|
|
325
|
+
m_1.measured_at::date AS measured_date,
|
|
326
|
+
unpivot.measure_name,
|
|
327
|
+
unpivot.measure,
|
|
328
|
+
CASE
|
|
329
|
+
WHEN m_1.wind_dir::text = 'N'::text THEN 0
|
|
330
|
+
WHEN m_1.wind_dir::text = 'NE'::text THEN 45
|
|
331
|
+
WHEN m_1.wind_dir::text = 'E'::text THEN 90
|
|
332
|
+
WHEN m_1.wind_dir::text = 'SE'::text THEN 135
|
|
333
|
+
WHEN m_1.wind_dir::text = 'S'::text THEN 180
|
|
334
|
+
WHEN m_1.wind_dir::text = 'SW'::text THEN 225
|
|
335
|
+
WHEN m_1.wind_dir::text = 'W'::text THEN 270
|
|
336
|
+
WHEN m_1.wind_dir::text = 'NW'::text THEN 315
|
|
337
|
+
ELSE NULL::integer
|
|
338
|
+
END AS wind_dir
|
|
339
|
+
FROM microclimate.measurements m_1,
|
|
340
|
+
LATERAL ( VALUES ('Teplota vzduchu'::text,m_1.air_temp), ('Vlhkost vzduchu'::text,m_1.air_hum), ('Tlak vzduchu'::text,m_1.pressure), ('Náraz a směr větru'::text,m_1.wind_impact), ('Rychlost a směr větru'::text,m_1.wind_speed), ('Úhrn srážek'::text,m_1.precip), ('Sluneční svit'::text,m_1.sun_irr), ('Vodní potenciál půdy'::text,m_1.water_pot), ('Teplota půdy'::text,m_1.soil_temp), ('Obvod stromu'::text,m_1.dendro_circ), ('Přírůstek obvodu stromu'::text,m_1.dendro_gain)) unpivot(measure_name, measure)
|
|
341
|
+
WHERE m_1.measured_at::date >= (CURRENT_DATE - 7)
|
|
342
|
+
)
|
|
343
|
+
SELECT m.sensor_id,
|
|
344
|
+
t.address,
|
|
345
|
+
t.sensor_position_detail,
|
|
346
|
+
t.hight_cm,
|
|
347
|
+
m.measured_at,
|
|
348
|
+
m.measured_at::date AS measured_date,
|
|
349
|
+
m.measure_name,
|
|
350
|
+
CASE
|
|
351
|
+
WHEN m.measure_name = 'Teplota vzduchu'::text THEN '°C'::text
|
|
352
|
+
WHEN m.measure_name = 'Vlhkost vzduchu'::text THEN '%'::text
|
|
353
|
+
WHEN m.measure_name = 'Tlak vzduchu'::text THEN 'Pa'::text
|
|
354
|
+
WHEN m.measure_name = 'Náraz a směr větru'::text THEN 'km/h'::text
|
|
355
|
+
WHEN m.measure_name = 'Rychlost a směr větru'::text THEN 'km/h'::text
|
|
356
|
+
WHEN m.measure_name = 'Úhrn srážek'::text THEN 'mm'::text
|
|
357
|
+
WHEN m.measure_name = 'Sluneční svit'::text THEN 'lux'::text
|
|
358
|
+
WHEN m.measure_name = 'Vodní potenciál půdy'::text THEN 'kPa'::text
|
|
359
|
+
WHEN m.measure_name = 'Teplota půdy'::text THEN '°C'::text
|
|
360
|
+
WHEN m.measure_name = 'Obvod stromu'::text THEN 'mm'::text
|
|
361
|
+
WHEN m.measure_name = 'Přírůstek obvodu stromu'::text THEN 'µm'::text
|
|
362
|
+
ELSE NULL::text
|
|
363
|
+
END AS unit,
|
|
364
|
+
m.measure,
|
|
365
|
+
CASE
|
|
366
|
+
WHEN m.measure_name = 'Náraz a směr větru'::text THEN m.wind_dir
|
|
367
|
+
WHEN m.measure_name = 'Rychlost a směr větru'::text THEN m.wind_dir
|
|
368
|
+
ELSE NULL::integer
|
|
369
|
+
END AS measure_wind_dir
|
|
370
|
+
FROM tmp_hights t
|
|
371
|
+
JOIN tmp_measures m ON t.sensor_id::text = m.sensor_id::text AND t.measure = m.measure_name;
|
|
372
|
+
|
|
373
|
+
CREATE OR REPLACE VIEW analytic.v_microclimate_sensor_devices
|
|
374
|
+
AS SELECT sdi.sensor_id,
|
|
375
|
+
sdi.location_id,
|
|
376
|
+
sdi.point_id,
|
|
377
|
+
sdi.location,
|
|
378
|
+
sdi.loc_description,
|
|
379
|
+
sdi.loc_orientation,
|
|
380
|
+
sdi.loc_surface,
|
|
381
|
+
sdi.point_name,
|
|
382
|
+
sdi.address,
|
|
383
|
+
sdi.sensor_position,
|
|
384
|
+
sdi.sensor_position_detail,
|
|
385
|
+
sdi.lat,
|
|
386
|
+
sdi.lng,
|
|
387
|
+
('https://storage.golemio.cz/oict-mikroklima/'::text || sdi.point_id::text) || '.jpg'::text AS url_foto
|
|
388
|
+
FROM microclimate.sensor_devices_import sdi;
|
|
389
|
+
|
|
390
|
+
CREATE OR REPLACE VIEW analytic.v_microclimate_measurements_daily_w_hights
|
|
391
|
+
AS SELECT m.date,
|
|
392
|
+
m.sensor_id,
|
|
393
|
+
m.address,
|
|
394
|
+
m.sensor_position_detail,
|
|
395
|
+
h.measure,
|
|
396
|
+
m.value,
|
|
397
|
+
h.hight_cm,
|
|
398
|
+
CASE
|
|
399
|
+
WHEN h.measure = 'wind_dir_at_max_speed'::text THEN 'Směr maximální rychlosti větru'::text
|
|
400
|
+
WHEN h.measure = 'sum_precip'::text THEN 'Denní úhrn srážek'::text
|
|
401
|
+
WHEN h.measure = 'avg_temperature'::text THEN 'Průměrná denní teplota vzduchu'::text
|
|
402
|
+
WHEN h.measure = 'max_wind_speed'::text THEN 'Maximální rychlost větru'::text
|
|
403
|
+
WHEN h.measure = 'avg_water_pot'::text THEN 'Průměrná denní vlhkost půdy'::text
|
|
404
|
+
WHEN h.measure = 'avg_wind_speed'::text THEN 'Průměrná denní rychlost větru'::text
|
|
405
|
+
WHEN h.measure = 'avg_air_hum'::text THEN 'Průměrná denní relativní vlhkost vzduchu'::text
|
|
406
|
+
WHEN h.measure = 'avg_pressure'::text THEN 'Průměrný denní tlak vzduchu'::text
|
|
407
|
+
WHEN h.measure = 'max_temperature'::text THEN 'Maximální denní teplota vzduchu'::text
|
|
408
|
+
WHEN h.measure = 'avg_soil_temp'::text THEN 'Průměrná denní teplota půdy'::text
|
|
409
|
+
WHEN h.measure = 'min_temperature'::text THEN 'Minimální denní teplota vzduchu'::text
|
|
410
|
+
ELSE NULL::text
|
|
411
|
+
END AS measure_cz,
|
|
412
|
+
CASE
|
|
413
|
+
WHEN h.measure = 'wind_dir_at_max_speed'::text THEN 'm/s'::text
|
|
414
|
+
WHEN h.measure = 'sum_precip'::text THEN 'mm'::text
|
|
415
|
+
WHEN h.measure = 'avg_temperature'::text THEN '°C'::text
|
|
416
|
+
WHEN h.measure = 'max_wind_speed'::text THEN 'm/s'::text
|
|
417
|
+
WHEN h.measure = 'avg_water_pot'::text THEN 'kPa'::text
|
|
418
|
+
WHEN h.measure = 'avg_wind_speed'::text THEN 'm/s'::text
|
|
419
|
+
WHEN h.measure = 'avg_air_hum'::text THEN '%'::text
|
|
420
|
+
WHEN h.measure = 'avg_pressure'::text THEN 'hPa'::text
|
|
421
|
+
WHEN h.measure = 'max_temperature'::text THEN '°C'::text
|
|
422
|
+
WHEN h.measure = 'avg_soil_temp'::text THEN '°C'::text
|
|
423
|
+
WHEN h.measure = 'min_temperature'::text THEN '°C'::text
|
|
424
|
+
ELSE NULL::text
|
|
425
|
+
END AS unit
|
|
426
|
+
FROM analytic.v_microclimate_measurements_daily m
|
|
427
|
+
LEFT JOIN analytic.v_microclimate_measurement_hights h ON m.sensor_id::text = h.sensor_id::text AND m.metrika = h.measure
|
|
428
|
+
ORDER BY m.sensor_id, m.date;
|
|
429
|
+
|
|
430
|
+
CREATE OR REPLACE VIEW analytic.v_microclimate_wind_dir_at_max_speed
|
|
431
|
+
AS WITH max_wind_speed AS (
|
|
432
|
+
SELECT DISTINCT ON (v_microclimate_measurements_daily_w_hights.sensor_id, (date_trunc('month'::text, v_microclimate_measurements_daily_w_hights.date::timestamp with time zone))) v_microclimate_measurements_daily_w_hights.date,
|
|
433
|
+
v_microclimate_measurements_daily_w_hights.sensor_id,
|
|
434
|
+
v_microclimate_measurements_daily_w_hights.value AS wind_speed
|
|
435
|
+
FROM analytic.v_microclimate_measurements_daily_w_hights
|
|
436
|
+
WHERE v_microclimate_measurements_daily_w_hights.measure = 'max_wind_speed'::text
|
|
437
|
+
ORDER BY v_microclimate_measurements_daily_w_hights.sensor_id, (date_trunc('month'::text, v_microclimate_measurements_daily_w_hights.date::timestamp with time zone)), v_microclimate_measurements_daily_w_hights.value DESC
|
|
438
|
+
), dir_wind_speed AS (
|
|
439
|
+
SELECT vmma.date,
|
|
440
|
+
vmma.sensor_id,
|
|
441
|
+
vmma.value AS wind_dir
|
|
442
|
+
FROM analytic.v_microclimate_measurements_daily_w_hights vmma
|
|
443
|
+
WHERE vmma.measure = 'wind_dir_at_max_speed'::text AND vmma.value IS NOT NULL
|
|
444
|
+
)
|
|
445
|
+
SELECT max_wind_speed.date,
|
|
446
|
+
max_wind_speed.wind_speed,
|
|
447
|
+
max_wind_speed.sensor_id,
|
|
448
|
+
dir_wind_speed.wind_dir AS dir
|
|
449
|
+
FROM max_wind_speed
|
|
450
|
+
JOIN dir_wind_speed ON max_wind_speed.sensor_id::text = dir_wind_speed.sensor_id::text AND max_wind_speed.date = dir_wind_speed.date
|
|
451
|
+
ORDER BY max_wind_speed.date;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
DROP VIEW analytic.v_microclimate_wind_dir_at_max_speed;
|
|
2
|
+
|
|
3
|
+
DROP VIEW analytic.v_microclimate_measurements_daily_w_hights;
|
|
4
|
+
|
|
5
|
+
DROP VIEW analytic.v_microclimate_measurement_hights;
|
|
6
|
+
|
|
7
|
+
DROP VIEW analytic.v_microclimate_measurements_daily;
|
|
8
|
+
|
|
9
|
+
DROP VIEW analytic.v_microclimate_raw_data;
|
|
10
|
+
|
|
11
|
+
DROP VIEW analytic.v_microclimate_sensor_devices;
|
|
12
|
+
|
|
13
|
+
DROP VIEW microclimate.v_sensor_points;
|
|
14
|
+
|
|
15
|
+
DROP VIEW microclimate.v_sensor_measurements;
|
|
16
|
+
|
|
17
|
+
DROP TABLE microclimate.sensor_devices_import;
|
|
18
|
+
|
|
19
|
+
CREATE TABLE microclimate.sensor_devices_import (
|
|
20
|
+
location_id int4 NULL,
|
|
21
|
+
point_id int4 NULL,
|
|
22
|
+
"location" varchar(50) NULL,
|
|
23
|
+
loc_description varchar(50) NULL,
|
|
24
|
+
loc_orientation varchar(50) NULL,
|
|
25
|
+
loc_surface varchar(50) NULL,
|
|
26
|
+
point_name varchar(50) NULL,
|
|
27
|
+
point_whole_name varchar(50) NULL,
|
|
28
|
+
address varchar(50) NULL,
|
|
29
|
+
sensor_position varchar(50) NULL,
|
|
30
|
+
sensor_position_detail varchar(50) NULL,
|
|
31
|
+
sensor_id varchar(50) NOT NULL,
|
|
32
|
+
data_relevance varchar(50) NULL,
|
|
33
|
+
lat float4 NULL,
|
|
34
|
+
lng float4 NULL,
|
|
35
|
+
air_temp int4 NULL,
|
|
36
|
+
air_hum int4 NULL,
|
|
37
|
+
pressure int4 NULL,
|
|
38
|
+
wind_dir int4 NULL,
|
|
39
|
+
wind_impact int4 NULL,
|
|
40
|
+
wind_speed int4 NULL,
|
|
41
|
+
precip int4 NULL,
|
|
42
|
+
sun_irr int4 NULL,
|
|
43
|
+
soil_temp int4 NULL,
|
|
44
|
+
water_pot int4 NULL,
|
|
45
|
+
dendro_circ int4 NULL,
|
|
46
|
+
dendro_gain int4 NULL,
|
|
47
|
+
data_until varchar(50) NULL,
|
|
48
|
+
CONSTRAINT sensor_devices_import_pkey PRIMARY KEY (sensor_id)
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
CREATE OR REPLACE VIEW microclimate.v_sensor_points
|
|
52
|
+
AS SELECT DISTINCT s.point_id,
|
|
53
|
+
s.point_name,
|
|
54
|
+
s.location_id,
|
|
55
|
+
s.location,
|
|
56
|
+
s.loc_description,
|
|
57
|
+
s.loc_orientation,
|
|
58
|
+
s.loc_surface,
|
|
59
|
+
s.lat,
|
|
60
|
+
s.lng,
|
|
61
|
+
NULL::text AS x_jtsk,
|
|
62
|
+
NULL::text AS y_jtsk,
|
|
63
|
+
NULL::text AS elevation_m,
|
|
64
|
+
s.sensor_position,
|
|
65
|
+
s.sensor_position_detail,
|
|
66
|
+
concat(unpivot.measure, unpivot.height_cm) AS measure,
|
|
67
|
+
CASE
|
|
68
|
+
WHEN unpivot.measure = 'air_temp'::text THEN 'Teplota vzduchu'::text
|
|
69
|
+
WHEN unpivot.measure = 'air_hum'::text THEN 'Vlhkost vzduchu'::text
|
|
70
|
+
WHEN unpivot.measure = 'pressure'::text THEN 'Atmosférický tlak'::text
|
|
71
|
+
WHEN unpivot.measure = 'wind_dir'::text THEN 'Směr větru'::text
|
|
72
|
+
WHEN unpivot.measure = 'wind_impact'::text THEN 'Náraz větru'::text
|
|
73
|
+
WHEN unpivot.measure = 'wind_speed'::text THEN 'Rychlost větru'::text
|
|
74
|
+
WHEN unpivot.measure = 'precip'::text THEN 'Úhrn srážek'::text
|
|
75
|
+
WHEN unpivot.measure = 'sun_irr'::text THEN 'Sluneční záření'::text
|
|
76
|
+
WHEN unpivot.measure = 'soil_temp'::text THEN 'Teplota půdy'::text
|
|
77
|
+
WHEN unpivot.measure = 'water_pot'::text THEN 'Vodní potenciál půdy'::text
|
|
78
|
+
WHEN unpivot.measure = 'dendro_circ'::text THEN 'Obvod stromu'::text
|
|
79
|
+
WHEN unpivot.measure = 'dendro_gain'::text THEN 'Přírůstek obvodu stromu'::text
|
|
80
|
+
ELSE NULL::text
|
|
81
|
+
END AS measure_cz,
|
|
82
|
+
CASE
|
|
83
|
+
WHEN unpivot.measure = 'air_temp'::text THEN '°C'::text
|
|
84
|
+
WHEN unpivot.measure = 'air_hum'::text THEN '%'::text
|
|
85
|
+
WHEN unpivot.measure = 'pressure'::text THEN 'Pa'::text
|
|
86
|
+
WHEN unpivot.measure = 'wind_dir'::text THEN '°'::text
|
|
87
|
+
WHEN unpivot.measure = 'wind_impact'::text THEN 'km/h'::text
|
|
88
|
+
WHEN unpivot.measure = 'wind_speed'::text THEN 'km/h'::text
|
|
89
|
+
WHEN unpivot.measure = 'precip'::text THEN 'mm'::text
|
|
90
|
+
WHEN unpivot.measure = 'sun_irr'::text THEN 'lux'::text
|
|
91
|
+
WHEN unpivot.measure = 'soil_temp'::text THEN '°C'::text
|
|
92
|
+
WHEN unpivot.measure = 'water_pot'::text THEN 'kPa'::text
|
|
93
|
+
WHEN unpivot.measure = 'dendro_circ'::text THEN 'mm'::text
|
|
94
|
+
WHEN unpivot.measure = 'dendro_gain'::text THEN 'µm'::text
|
|
95
|
+
ELSE NULL::text
|
|
96
|
+
END AS unit
|
|
97
|
+
FROM microclimate.sensor_devices_import s,
|
|
98
|
+
LATERAL ( VALUES ('air_temp'::text,s.air_temp), ('air_hum'::text,s.air_hum), ('pressure'::text,s.pressure), ('wind_dir'::text,s.wind_dir), ('wind_impact'::text,s.wind_impact), ('wind_speed'::text,s.wind_speed), ('precip'::text,s.precip), ('sun_irr'::text,s.sun_irr), ('soil_temp'::text,s.soil_temp), ('water_pot'::text,s.water_pot), ('dendro_circ'::text,s.dendro_circ), ('dendro_gain'::text,s.dendro_gain)) unpivot(measure, height_cm)
|
|
99
|
+
WHERE unpivot.height_cm IS NOT NULL
|
|
100
|
+
ORDER BY s.point_id;
|
|
101
|
+
|
|
102
|
+
CREATE OR REPLACE VIEW microclimate.v_sensor_measurements
|
|
103
|
+
AS WITH points_heights AS (
|
|
104
|
+
SELECT s.sensor_id,
|
|
105
|
+
s.point_id,
|
|
106
|
+
s.location_id,
|
|
107
|
+
unpivot.measure,
|
|
108
|
+
unpivot.height_cm,
|
|
109
|
+
CASE
|
|
110
|
+
WHEN unpivot.measure = 'air_temp'::text THEN '°C'::text
|
|
111
|
+
WHEN unpivot.measure = 'air_hum'::text THEN '%'::text
|
|
112
|
+
WHEN unpivot.measure = 'pressure'::text THEN 'Pa'::text
|
|
113
|
+
WHEN unpivot.measure = 'wind_dir'::text THEN '°'::text
|
|
114
|
+
WHEN unpivot.measure = 'wind_impact'::text THEN 'km/h'::text
|
|
115
|
+
WHEN unpivot.measure = 'wind_speed'::text THEN 'km/h'::text
|
|
116
|
+
WHEN unpivot.measure = 'precip'::text THEN 'mm'::text
|
|
117
|
+
WHEN unpivot.measure = 'sun_irr'::text THEN 'lux'::text
|
|
118
|
+
WHEN unpivot.measure = 'soil_temp'::text THEN '°C'::text
|
|
119
|
+
WHEN unpivot.measure = 'water_pot'::text THEN 'kPa'::text
|
|
120
|
+
WHEN unpivot.measure = 'dendro_circ'::text THEN 'mm'::text
|
|
121
|
+
WHEN unpivot.measure = 'dendro_gain'::text THEN 'µm'::text
|
|
122
|
+
ELSE NULL::text
|
|
123
|
+
END AS unit
|
|
124
|
+
FROM microclimate.sensor_devices_import s,
|
|
125
|
+
LATERAL ( VALUES ('air_temp'::text,s.air_temp), ('air_hum'::text,s.air_hum), ('pressure'::text,s.pressure), ('wind_dir'::text,s.wind_dir), ('wind_impact'::text,s.wind_impact), ('wind_speed'::text,s.wind_speed), ('precip'::text,s.precip), ('sun_irr'::text,s.sun_irr), ('soil_temp'::text,s.soil_temp), ('water_pot'::text,s.water_pot), ('dendro_circ'::text,s.dendro_circ), ('dendro_gain'::text,s.dendro_gain)) unpivot(measure, height_cm)
|
|
126
|
+
WHERE unpivot.height_cm IS NOT NULL
|
|
127
|
+
), measurements AS (
|
|
128
|
+
SELECT m.sensor_id,
|
|
129
|
+
m.measured_at,
|
|
130
|
+
unpivot.measure,
|
|
131
|
+
unpivot.value
|
|
132
|
+
FROM microclimate.measurements m,
|
|
133
|
+
LATERAL ( VALUES ('air_temp'::text,m.air_temp), ('air_hum'::text,m.air_hum), ('pressure'::text,m.pressure), ('wind_dir'::text,
|
|
134
|
+
CASE
|
|
135
|
+
WHEN m.wind_dir::text = 'N'::text THEN 0
|
|
136
|
+
WHEN m.wind_dir::text = 'NE'::text THEN 45
|
|
137
|
+
WHEN m.wind_dir::text = 'E'::text THEN 90
|
|
138
|
+
WHEN m.wind_dir::text = 'SE'::text THEN 135
|
|
139
|
+
WHEN m.wind_dir::text = 'S'::text THEN 180
|
|
140
|
+
WHEN m.wind_dir::text = 'SW'::text THEN 225
|
|
141
|
+
WHEN m.wind_dir::text = 'W'::text THEN 270
|
|
142
|
+
WHEN m.wind_dir::text = 'NW'::text THEN 315
|
|
143
|
+
ELSE NULL::integer
|
|
144
|
+
END), ('wind_impact'::text,m.wind_impact), ('wind_speed'::text,m.wind_speed), ('precip'::text,m.precip), ('sun_irr'::text,m.sun_irr), ('soil_temp'::text,m.soil_temp), ('water_pot'::text,m.water_pot), ('dendro_circ'::text,m.dendro_circ), ('dendro_gain'::text,m.dendro_gain)) unpivot(measure, value)
|
|
145
|
+
WHERE unpivot.value IS NOT NULL
|
|
146
|
+
)
|
|
147
|
+
SELECT q.sensor_id,
|
|
148
|
+
q.measured_at,
|
|
149
|
+
q.point_id,
|
|
150
|
+
q.location_id,
|
|
151
|
+
q.measure,
|
|
152
|
+
q.value,
|
|
153
|
+
q.unit,
|
|
154
|
+
row_number() OVER (PARTITION BY q.point_id, q.measure ORDER BY q.point_id, q.measure, q.measured_at DESC) AS rn
|
|
155
|
+
FROM ( SELECT measurements.sensor_id,
|
|
156
|
+
measurements.measured_at,
|
|
157
|
+
points_heights.point_id,
|
|
158
|
+
points_heights.location_id,
|
|
159
|
+
concat(measurements.measure, points_heights.height_cm) AS measure,
|
|
160
|
+
measurements.value,
|
|
161
|
+
points_heights.unit
|
|
162
|
+
FROM measurements
|
|
163
|
+
JOIN points_heights ON points_heights.sensor_id::text = measurements.sensor_id::text AND points_heights.measure = measurements.measure) q
|
|
164
|
+
ORDER BY q.point_id, q.measure, q.measured_at DESC;
|