Mapping for monthly_climate_summary

Mapping for stations_view
=> join of stations with high_quality_stations
Stations Attribute
Station No
Station Location
Station Name
Station State
Original Column
stations.stn_num
stations.geom_loc
stations.pr_name
stations.state
Column Name for the view
stations
geom_loc
station_name
station_state
-- View: stations_view
-- DROP VIEW stations_view;
CREATE OR REPLACE VIEW stations_view AS
SELECT stations.stn_num AS station, stations.geom_loc, stations.pr_name AS
station_name, stations.state AS station_state
FROM stations
JOIN high_quality_stations ON stations.stn_num = high_quality_stations.stn_num;
ALTER TABLE stations_view OWNER TO bomuser;
GRANT ALL ON TABLE stations_view TO bomuser;
GRANT SELECT ON TABLE stations_view TO public;
COMMENT ON VIEW stations_view IS 'Stations View';
DB Script for sfc_days_hqs_view (smaller set of daily climate data
filter by high quality stations)
-- View: sfc_days_hqs_view
-- DROP VIEW sfc_days_hqs_view;
CREATE OR REPLACE VIEW sfc_days_hqs_view AS
SELECT sfc_days.*
FROM sfc_days
JOIN high_quality_stations ON sfc_days.stn_num = high_quality_stations.stn_num;
ALTER TABLE sfc_days_hqs_view OWNER TO bomuser;
GRANT ALL ON TABLE sfc_days_hqs_view TO bomuser;
GRANT SELECT ON TABLE sfc_days_hqs_view TO public;
DB Script for sfc_mos_hqs_view (smaller set of monthly climate data
filter by high quality stations)
-- View: sfc_mos_hqs_view
-- DROP VIEW sfc_mos_hqs_view;
CREATE OR REPLACE VIEW sfc_mos_hqs_view AS
SELECT sfc_mos.*
FROM sfc_mos
JOIN high_quality_stations ON sfc_mos.stn_num = high_quality_stations.stn_num;
ALTER TABLE sfc_mos_hqs_view OWNER TO bomuser;
GRANT ALL ON TABLE sfc_mos_hqs_view TO bomuser;
GRANT SELECT ON TABLE sfc_mos_hqs_view TO public;
DB Script for onedrain_hqs_view (smaller set of daily rain fall data
filter by high quality stations)
-- View: onedrain_hqs_view
-- DROP VIEW onedrain_hqs_view;
CREATE OR REPLACE VIEW onedrain_hqs_view AS
SELECT onedrain.*
FROM onedrain
JOIN high_quality_stations ON onedrain.stn_num = high_quality_stations.stn_num;
ALTER TABLE onedrain_hqs_view OWNER TO bomuser;
COMMENT ON VIEW onedrain_hqs_view IS 'daily rain fall data with high quality station';
Create onedrain_hqs materialized view
SELECT
CREATE
CREATE
CREATE
create_matview('onedrain_hqs', 'onedrain_hqs_view');
INDEX onedrain_station_idx ON onedrain_hqs(stn_num);
INDEX onedrain_prcp_idx ON onedrain_hqs(prcp);
INDEX onedrain_lsd_idx ON onedrain_hqs(lsd);
DB Script for mo_rains_hqs_view (smaller set of monthly rain fall
data filter by high quality stations)
-- View: mo_rains_hqs_view
-- DROP VIEW mo_rains_hqs_view;
CREATE OR REPLACE VIEW mo_rains_hqs_view AS
SELECT mo_rains.*
FROM mo_rains
JOIN high_quality_stations ON mo_rains.stn_num = high_quality_stations.stn_num;
ALTER TABLE mo_rains_hqs_view OWNER TO bomuser;
Mapping for daily_climate_summary
sfc_days = sfc_days_hqs_view view(smaller set of daily climate
summary data base on high quality stations)
Temperature Attribute
Highest temperature
Lowest temperature
Maximum Wind Speed
Maximum Wind Direction
Station
Date
Location
Station Name
Station State
Precipitation
Original Column
sfc_days.max_air_temp
sfc_days.min_air_temp
sfc_days.max_wnd_gust_spd
sfc_days.max_wnd_gust_dir
sfc_days.stn_num
sfc_days.lsd
stations_view.location
stations_view.station_name
stations_view.station_state
Onedrain.prcp
Column Name for the view
max_temp
min_temp
max_wind_speed
max_wind_direction
Station
Date
geom_loc
station_name
station_state
precipitation
DBScript:
-- View: daily_climate_summary_view
-- DROP VIEW daily_climate_summary_view;
CREATE OR REPLACE VIEW daily_climate_summary_view AS
SELECT sfc_days.max_air_temp AS max_temp, sfc_days.min_air_temp AS min_temp,
sfc_days.max_wnd_gust_spd AS max_wind_speed, sfc_days.max_wnd_gust_dir AS max_wind_direction,
onedrain_hqs.prcp AS precipitation, sfc_days.stn_num AS station, to_char(sfc_days.lsd::timestamp
with time zone, 'yyyy-mm-dd'::text) AS date, stations.geom_loc, stations.pr_name AS station_name,
stations.state AS station_state
FROM sfc_days_hqs_view sfc_days
LEFT JOIN stations ON sfc_days.stn_num = stations.stn_num
LEFT JOIN onedrain_hqs ON sfc_days.stn_num = onedrain_hqs.stn_num AND sfc_days.lsd =
onedrain_hqs.lsd;
ALTER TABLE daily_climate_summary_view OWNER TO bomuser;
GRANT ALL ON TABLE daily_climate_summary_view TO bomuser;
GRANT SELECT ON TABLE daily_climate_summary_view TO public;
COMMENT ON VIEW daily_climate_summary_view IS 'Daily Climate Summary View';
To view the geometry column :
Select ST_Extent(location) from daily_climate_summary where station=’1006’
Create
SELECT
CREATE
CREATE
CREATE
CREATE
CREATE
daily_climate_summary materialized view
create_matview('daily_climate_summary', 'daily_climate_summary_view');
INDEX daily_station_idx ON daily_climate_summary(station);
INDEX daily_date_idx ON daily_climate_summary(date);
INDEX daily_max_temp_idx ON daily_climate_summary(max_temp);
INDEX daily_min_temp_idx ON daily_climate_summary(min_temp);
INDEX daily_wind_speed_idx ON daily_climate_summary(max_wind_speed);
daily_climate_summary materialized view
-- Table: daily_climate_summary
-- DROP TABLE daily_climate_summary;
CREATE TABLE daily_climate_summary
(
max_temp numeric(7,1),
min_temp numeric(7,1),
max_wind_speed numeric(5,1),
max_wind_direction numeric(5,1),
precipitation numeric(6,1),
station numeric(6),
date text,
geom_loc geometry,
station_name character varying(40),
station_state character varying(3)
)
WITH (OIDS=FALSE);
ALTER TABLE daily_climate_summary OWNER TO bomuser;
GRANT ALL ON TABLE daily_climate_summary TO bomuser;
GRANT SELECT ON TABLE daily_climate_summary TO public;
-- Index: daily_date_idx
-- DROP INDEX daily_date_idx;
CREATE INDEX daily_date_idx
ON daily_climate_summary
USING btree
(date);
-- Index: daily_max_temp_idx
-- DROP INDEX daily_max_temp_idx;
CREATE INDEX daily_max_temp_idx
ON daily_climate_summary
USING btree
(max_temp);
-- Index: daily_min_temp_idx
-- DROP INDEX daily_min_temp_idx;
CREATE INDEX daily_min_temp_idx
ON daily_climate_summary
USING btree
(min_temp);
-- Index: daily_station_idx
-- DROP INDEX daily_station_idx;
CREATE INDEX daily_station_idx
ON daily_climate_summary
USING btree
(station);
-- Index: daily_wind_speed_idx
-- DROP INDEX daily_wind_speed_idx;
CREATE INDEX daily_wind_speed_idx
ON daily_climate_summary
USING btree
(max_wind_speed);
Mapping for monthly_climate_summary
sfc_mos = sfc_mos_hqs_view view (smaller set of monthly climate
summary data base on high quality stations)
Temperature Attribute
Mean maximum
temperature
Mean minimum
temperature
Highest temperature
Lowest maximum
temperature
Highest minimum
temperature
Lowest temperature
Maximum Wind Speed
Maximum Wind Direction
Station
Maximum Air pressure
Date
Location
Station Name
Station State
Precipitation
Original Column
sfc_mos.mn_max_air_temp
Column Name for the view
mn_max_air_temp
sfc_mos.mn_min_air_temp
mn_min_air_temp
sfc_mos.max_max_air_temp
sfc_mos.min_max_air_temp
max_temp
min_max_air_temp
sfc_mos.max_min_air_temp
max_min_air_temp
sfc_mos.min_min_air_temp
sfc_mos.max_wnd_gust_dines
sfc_mos.max_wnd_gust_dines_dir
sfc_mos.stn_num
max value of
{
max(sfc_mos.mn_15_stn_pres)
GROUP BY station
min_temp
max_wind_speed
max_wind_direction
station
max_air_pressure
max(sfc_mos.mn_09_stn_pres)
GROUP BY station
}
sfc_mos.lsd
stations_view.location
stations_view.station_name
stations_view.station_state
mo_rains.ttl_mo_prcp
date
geom_loc
station_name
Station_state
precipitation
DBScript (Monthly Summary Normal View):
-- View: monthly_climate_summary_view
-- DROP VIEW monthly_climate_summary_view;
CREATE OR REPLACE VIEW monthly_climate_summary_view AS
SELECT sfc_mos.mn_max_air_temp, sfc_mos.mn_min_air_temp, sfc_mos.max_max_air_temp AS max_temp,
sfc_mos.min_max_air_temp, sfc_mos.max_min_air_temp, sfc_mos.min_min_air_temp AS min_temp,
sfc_mos.max_wnd_gust_dines AS max_wind_speed,
sfc_mos.max_wnd_gust_dines_dir AS max_wind_direction,
max_air_pressure_view.max_air_pressure,
mo_rains.ttl_mo_prcp AS precipitation,
sfc_mos.stn_num AS station, to_char(sfc_mos.lsd, 'yyyy-mm-dd'::text) AS date,
stations.geom_loc, stations.pr_name AS station_name, stations.state AS station_state
FROM sfc_mos_hqs_view sfc_mos
LEFT JOIN stations ON sfc_mos.stn_num = stations.stn_num
LEFT JOIN mo_rains_hqs_view mo_rains ON sfc_mos.stn_num = mo_rains.stn_num AND sfc_mos.lsd = mo_rains.lsd
LEFT JOIN max_air_pressure_view ON sfc_mos.stn_num = max_air_pressure_view.stn_num;
ALTER TABLE monthly_climate_summary_view OWNER TO bomuser;
GRANT ALL ON TABLE monthly_climate_summary_view TO bomuser;
GRANT SELECT ON TABLE monthly_climate_summary_view TO public;
COMMENT ON VIEW monthly_climate_summary_view IS 'Monthly Climate Summary data from sfc_mos table';
Views needed for monthly_summary_normal_view :
To get the max_air_pressure, another view has been created :
-- View: max_air_pressure_view
-- DROP VIEW max_air_pressure_view;
CREATE OR REPLACE VIEW max_air_pressure_view AS
SELECT sfc_mos.stn_num, GREATEST(max(sfc_mos.mn_15_stn_pres),
max(sfc_mos.mn_09_stn_pres)) AS max_air_pressure
FROM sfc_mos
GROUP BY sfc_mos.stn_num
ORDER BY sfc_mos.stn_num;
ALTER TABLE max_air_pressure_view OWNER TO bomuser;
Create monthly_climate_summary materialized view
SELECT
CREATE
CREATE
CREATE
CREATE
CREATE
CREATE
create_matview('monthly_climate_summary', 'monthly_climate_summary_view');
INDEX station_idx ON monthly_climate_summary(station);
INDEX date_idx ON monthly_climate_summary(date);
INDEX max_temp_idx ON monthly_climate_summary(max_temp, mn_max_air_temp, max_min_air_temp);
INDEX min_temp_idx ON monthly_climate_summary(min_temp, mn_min_air_temp, min_max_air_temp);
INDEX wind_speed_idx ON monthly_climate_summary(max_wind_speed);
INDEX max_air_pressure_idx ON monthly_climate_summary(max_air_pressure);
monthly_climate_summary materialized view
-- Table: monthly_climate_summary
-- DROP TABLE monthly_climate_summary;
CREATE TABLE monthly_climate_summary
(
mn_max_air_temp numeric(7,1),
mn_min_air_temp numeric(7,1),
max_temp numeric(7,1),
min_max_air_temp numeric(7,1),
max_min_air_temp numeric(7,1),
min_temp numeric(7,1),
max_wind_speed numeric(5,1),
max_wind_direction numeric(5,1),
max_air_pressure numeric,
precipitation numeric(8,1),
station numeric(6),
date text,
geom_loc geometry,
station_name character varying(40),
station_state character varying(3)
)
WITH (OIDS=FALSE);
ALTER TABLE monthly_climate_summary OWNER TO bomuser;
GRANT ALL ON TABLE monthly_climate_summary TO bomuser;
GRANT SELECT ON TABLE monthly_climate_summary TO public;
-- Index: date_idx
-- DROP INDEX date_idx;
CREATE INDEX date_idx
ON monthly_climate_summary
USING btree
(date);
-- Index: max_air_pressure_idx
-- DROP INDEX max_air_pressure_idx;
CREATE INDEX max_air_pressure_idx
ON monthly_climate_summary
USING btree
(max_air_pressure);
-- Index: max_temp_idx
-- DROP INDEX max_temp_idx;
CREATE INDEX max_temp_idx
ON monthly_climate_summary
USING btree
(max_temp, mn_max_air_temp, max_min_air_temp);
-- Index: min_temp_idx
-- DROP INDEX min_temp_idx;
CREATE INDEX min_temp_idx
ON monthly_climate_summary
USING btree
(min_temp, mn_min_air_temp, min_max_air_temp);
-- Index: station_idx
-- DROP INDEX station_idx;
CREATE INDEX station_idx
ON monthly_climate_summary
USING btree
(station);
-- Index: wind_speed_idx
-- DROP INDEX wind_speed_idx;
CREATE INDEX wind_speed_idx
ON monthly_climate_summary
USING btree
(max_wind_speed);
Mapping for extremes
sfc_mos = sfc_mos_hqs_view view (smaller set of monthly data base on
high quality stations)
Attribute
Highest temperature
Date for Highest
temperature
Maximum Wind Speed
Date for Maximum Wind
Speed
Column of the sfc_mos table
Max (max_max_air_temp) GROUP
BY station
max_max_air_temp_lst
This should be the date
corresponding to Max
(max_max_air_temp) record
Max (max_wnd_gust_dines)
GROUP BY station
max_wnd_gust_dines_lst
Column Name for the view
max_temp
max_temp_lst
max_wind_speed
max_wind_speed_lst
This should be the date
corresponding to Max
(max_wnd_gust_dines) record
Maximum Wind Direction
Maximum Air pressure
Lowest temperature
Date for Lowest
temperature
max_wnd_gust_dines_dir for
the record of Maximum Wind
Speed
Max Value of
{
Max(mn_15_stn_pres) GROUP BY
station
Max (mn_09_stn_pres) GROUP BY
station
}
Min (min_min_air_temp)
GROUP BY station
min_min_air_temp _lst
max_wind_direction
max_air_pressure
min_temp
min_temp_lst
This should be the date
corresponding to Min
(min_min_air_temp) record
Minimum Wind Speed
Date for Minimum Wind
Speed
Min (max_wnd_gust_dines)
GROUP BY station
max_wnd_gust_dines_lst
min_wind_speed
min_wind_speed_lst
This should be the date
corresponding to Min
(max_wnd_gust_dines)
Minimum Wind
Direction
Minimum
Air pressure
Station location
Station
Station Name
Station State
Highest Monthly
Precipitation
Highest Daily
Precipitation
Date for Highest Daily
Precipitation
max_wnd_gust_dines_dir for
the record of Minimum Wind
Speed
Min Value of
{
Min(mn_15_stn_pres) GROUP BY
station
min_wind_direction
min_air_pressure
Min (mn_09_stn_pres) GROUP BY
station
}
geom_loc
stn_num
pr_name ( station table)
state (station table)
Max(ttl_mo_prcp) GROUP BY
station (mo_rains table)
Max (prcp) GROUP BY station
(onedrain table)
geom_loc
station
station_name
station_state
ttl_mo_prcp
lsd (onedrain table)
ttl_day_prcp_lst
This should be the date
corresponding Max (prcp)
ttl_day_prcp
DBScript:
1) extreme_preliminary_view
-- View: extreme_preliminary_view
-- DROP VIEW extreme_preliminary_view;
CREATE OR REPLACE VIEW extreme_preliminary_view AS
SELECT sfc_mos.stn_num AS station,
max(sfc_mos.max_max_air_temp) AS max_temp,
max_temp_date(sfc_mos.stn_num, max(sfc_mos.max_max_air_temp)) AS max_temp_lst,
max(sfc_mos.max_wnd_gust_dines) AS max_wind_speed,
max_windspeed_date(sfc_mos.stn_num, max(sfc_mos.max_wnd_gust_dines)) AS max_wind_speed_lst,
max_wind_direction(sfc_mos.stn_num, max(sfc_mos.max_wnd_gust_dines)) AS max_wind_direction,
max_air_pressure(max(sfc_mos.mn_15_stn_pres), max(sfc_mos.mn_09_stn_pres)) AS max_air_pressure,
min(sfc_mos.min_min_air_temp) AS min_temp,
min_temp_date(sfc_mos.stn_num, min(sfc_mos.min_min_air_temp)) AS min_temp_lst,
min(sfc_mos.max_wnd_gust_dines) AS min_wind_speed,
min_windspeed_date(sfc_mos.stn_num, min(sfc_mos.max_wnd_gust_dines)) AS min_wind_speed_lst,
min_wind_direction(sfc_mos.stn_num, min(sfc_mos.max_wnd_gust_dines)) AS min_wind_direction,
min_air_pressure(min(sfc_mos.mn_15_stn_pres), min(sfc_mos.mn_09_stn_pres)) AS min_air_pressure,
max(mo_rains_hqs.ttl_mo_prcp) AS ttl_mo_prcp
FROM sfc_mos_hqs_view sfc_mos
LEFT JOIN mo_rains_hqs_view mo_rains_hqs ON sfc_mos.stn_num = mo_rains_hqs.stn_num
GROUP BY sfc_mos.stn_num;
ALTER TABLE extreme_preliminary_view OWNER TO bomuser;
GRANT ALL ON TABLE extreme_preliminary_view TO bomuser;
GRANT SELECT ON TABLE extreme_preliminary_view TO public;
Functions needed for extreme_preliminary_view :
i) max_temp_date(numeric, numeric)
=> returning the date corresponding to max(max_max_air_temp) record
-- Function: max_temp_date(numeric, numeric)
-- DROP FUNCTION max_temp_date(numeric, numeric);
CREATE OR REPLACE FUNCTION max_temp_date(numeric, numeric)
RETURNS text AS
$BODY$
DECLARE
station ALIAS FOR $1;
max_temp ALIAS FOR $2;
max_temp_date date;
BEGIN
SELECT INTO max_temp_date max_max_air_temp_lst from sfc_mos_hqs_view where stn_num = station
and max_max_air_temp = max_temp;
RETURN max_temp_date;
END
$BODY$
LANGUAGE 'plpgsql' VOLATILE SECURITY DEFINER
COST 100;
ALTER FUNCTION max_temp_date(numeric, numeric) OWNER TO bomuser;
ii) max_windspeed_date(numeric, numeric)
=> returning the date corresponding to max(max_wnd_gust_dines) record
-- Function: max_windspeed_date(numeric, numeric)
-- DROP FUNCTION max_windspeed_date(numeric, numeric);
CREATE OR REPLACE FUNCTION max_windspeed_date(numeric, numeric)
RETURNS text AS
$BODY$
DECLARE
station ALIAS FOR $1;
max_windspeed ALIAS FOR $2;
max_windspeed_date date;
BEGIN
SELECT INTO max_windspeed_date max_wnd_gust_dines_lst from sfc_mos_hqs_view where stn_num = station and
max_wnd_gust_dines = max_windspeed;
RETURN max_windspeed_date;
END
$BODY$
LANGUAGE 'plpgsql' VOLATILE SECURITY DEFINER
COST 100;
ALTER FUNCTION max_windspeed_date(numeric, numeric) OWNER TO bomuser;
iii) max_wind_direction(numeric, numeric)
Max wind direction for the record of maximum wind speed
-- Function: max_wind_direction(numeric, numeric)
-- DROP FUNCTION max_wind_direction(numeric, numeric);
CREATE OR REPLACE FUNCTION max_wind_direction(numeric, numeric)
RETURNS numeric AS
$BODY$
DECLARE
station ALIAS FOR $1;
maxwndgustdines ALIAS FOR $2;
max_wind_dir numeric;
BEGIN
SELECT INTO max_wind_dir COALESCE(max_wnd_gust_dines_dir,0) from sfc_mos_hqs_view where
stn_num = station and max_wnd_gust_dines = maxwndgustdines;
RETURN max_wind_dir;
END
$BODY$
LANGUAGE 'plpgsql' VOLATILE SECURITY DEFINER
COST 100;
ALTER FUNCTION max_wind_direction(numeric, numeric) OWNER TO bomuser;
iv) max_air_pressure(numeric, numeric)
-- Function: max_air_pressure(numeric, numeric)
-- DROP FUNCTION max_air_pressure(numeric, numeric);
CREATE OR REPLACE FUNCTION max_air_pressure(numeric, numeric)
RETURNS numeric AS
$BODY$
DECLARE
mn_15 ALIAS FOR $1;
mn_09 ALIAS FOR $2;
BEGIN
IF mn_15 > mn_09 THEN
RETURN mn_15;
ELSE
RETURN mn_09;
END IF;
END
$BODY$
LANGUAGE 'plpgsql' VOLATILE SECURITY DEFINER
COST 100;
ALTER FUNCTION max_air_pressure(numeric, numeric) OWNER TO bomuser;
v) min_temp_date(numeric, numeric)
-- Function: min_temp_date(numeric, numeric)
-- DROP FUNCTION min_temp_date(numeric, numeric);
CREATE OR REPLACE FUNCTION min_temp_date(numeric, numeric)
RETURNS text AS
$BODY$
DECLARE
station ALIAS FOR $1;
min_temp ALIAS FOR $2;
min_temp_date date;
BEGIN
SELECT INTO min_temp_date min_min_air_temp_lst from sfc_mos_hqs_view where stn_num = station
and min_min_air_temp = min_temp;
RETURN min_temp_date;
END
$BODY$
LANGUAGE 'plpgsql' VOLATILE SECURITY DEFINER
COST 100;
ALTER FUNCTION min_temp_date(numeric, numeric) OWNER TO bomuser;
vi) min_windspeed_date(numeric, numeric)
=> date corresponding to min(min_min_air_temp) record
-- Function: min_windspeed_date(numeric, numeric)
-- DROP FUNCTION min_windspeed_date(numeric, numeric);
CREATE OR REPLACE FUNCTION min_windspeed_date(numeric, numeric)
RETURNS text AS
$BODY$
DECLARE
station ALIAS FOR $1;
min_windspeed ALIAS FOR $2;
min_windspeed_date date;
BEGIN
SELECT INTO min_windspeed_date max_wnd_gust_dines_lst from sfc_mos_hqs_view where
stn_num = station and max_wnd_gust_dines = min_windspeed;
RETURN min_windspeed_date;
END
$BODY$
LANGUAGE 'plpgsql' VOLATILE SECURITY DEFINER
COST 100;
ALTER FUNCTION min_windspeed_date(numeric, numeric) OWNER TO bomuser;
vii) min_wind_direction(numeric, numeric)
=> min wind direction base on record of min wind speed
-- Function: min_wind_direction(numeric, numeric)
-- DROP FUNCTION min_wind_direction(numeric, numeric);
CREATE OR REPLACE FUNCTION min_wind_direction(numeric, numeric)
RETURNS numeric AS
$BODY$
DECLARE
station ALIAS FOR $1;
minwndgustdines ALIAS FOR $2;
min_wind_dir numeric;
BEGIN
SELECT INTO min_wind_dir COALESCE(max_wnd_gust_dines_dir,0) from sfc_mos_hqs_view where
stn_num = station and max_wnd_gust_dines = minwndgustdines;
RETURN min_wind_dir;
END
$BODY$
LANGUAGE 'plpgsql' VOLATILE SECURITY DEFINER
COST 100;
ALTER FUNCTION min_wind_direction(numeric, numeric) OWNER TO bomuser;
viii) min_air_pressure(numeric, numeric)
-- Function: min_air_pressure(numeric, numeric)
-- DROP FUNCTION min_air_pressure(numeric, numeric);
CREATE OR REPLACE FUNCTION min_air_pressure(numeric, numeric)
RETURNS numeric AS
$BODY$
DECLARE
mn_15 ALIAS FOR $1;
mn_09 ALIAS FOR $2;
BEGIN
IF mn_15 < mn_09 THEN
RETURN mn_15;
ELSE
RETURN mn_09;
END IF;
END
$BODY$
LANGUAGE 'plpgsql' VOLATILE SECURITY DEFINER
COST 100;
ALTER FUNCTION min_air_pressure(numeric, numeric) OWNER TO bomuser;
ix) max_daily_precipitation_date(numeric, numeric)
-- Function: max_daily_precipitation_date(numeric, numeric)
-- DROP FUNCTION max_daily_precipitation_date(numeric, numeric);
CREATE OR REPLACE FUNCTION max_daily_precipitation_date(numeric, numeric)
RETURNS text AS
$BODY$
DECLARE
station ALIAS FOR $1;
max_precipitation ALIAS FOR $2;
max_precipitation_date date;
BEGIN
SELECT INTO max_precipitation_date lsd from onedrain_hqs where
stn_num = station and prcp = max_precipitation;
RETURN max_precipitation_date;
END
$BODY$
LANGUAGE 'plpgsql' VOLATILE SECURITY DEFINER
COST 100;
ALTER FUNCTION max_daily_precipitation_date(numeric, numeric) OWNER TO bomuser;
2) daily_precipitation_view
-- View: daily_precipitation_view
-- DROP VIEW daily_precipitation_view;
CREATE OR REPLACE VIEW daily_precipitation_view AS
SELECT onedrain_hqs.stn_num AS station, max(onedrain_hqs.prcp) AS ttl_day_prcp,
max_daily_precipitation_date(onedrain_hqs.stn_num, max(onedrain_hqs.prcp)) AS ttl_day_prcp_lst
FROM onedrain_hqs
GROUP BY onedrain_hqs.stn_num;
ALTER TABLE daily_precipitation_view OWNER TO bomuser;
COMMENT ON VIEW daily_precipitation_view IS 'Daily precipitation view';
Functions needed for extreme_preliminary_view :
i) max_daily_precipitation_date(numeric, numeric)
-- Function: max_daily_precipitation_date(numeric, numeric)
-- DROP FUNCTION max_daily_precipitation_date(numeric, numeric);
CREATE OR REPLACE FUNCTION max_daily_precipitation_date(numeric, numeric)
RETURNS text AS
$BODY$
DECLARE
station ALIAS FOR $1;
max_precipitation ALIAS FOR $2;
max_precipitation_date date;
BEGIN
SELECT INTO max_precipitation_date lsd from onedrain_hqs where
stn_num = station and prcp = max_precipitation;
RETURN max_precipitation_date;
END
$BODY$
LANGUAGE 'plpgsql' VOLATILE SECURITY DEFINER
COST 100;
ALTER FUNCTION max_daily_precipitation_date(numeric, numeric) OWNER TO bomuser;
3) extreme_final_view
(Joining extreme_preliminary_view with daily_precipitation_view for daily rainfall
data and joining stations_view for station’s location, name and state details)
-- View: extreme_final_view
-- DROP VIEW extreme_final_view;
CREATE OR REPLACE VIEW extreme_final_view AS
SELECT extreme_preliminary_view.*,
daily_precipitation_view.ttl_day_prcp, daily_precipitation_view.ttl_day_prcp_lst,
stations_view.geom_loc, stations_view.station_name, stations_view.station_state
FROM extreme_preliminary_view
LEFT JOIN daily_precipitation_view ON extreme_preliminary_view.station =
daily_precipitation_view.station
LEFT JOIN stations_view ON extreme_preliminary_view.station = stations_view.station;
ALTER TABLE extreme_final_view OWNER TO bomuser;
GRANT ALL ON TABLE extreme_final_view TO bomuser;
GRANT SELECT ON TABLE extreme_final_view TO public;
Create extreme materialized view
SELECT create_matview('extreme', 'extreme_final_view');
CREATE INDEX extreme_station_idx ON extreme(station);
extreme materialized view
Syntax:
SELECT create_matview('extreme', 'extreme_final_view');
CREATE INDEX extreme_station_idx ON extreme(station);
Created Table :
-- Table: extreme
-- DROP TABLE extreme;
CREATE TABLE extreme
(
station numeric(6),
max_temp numeric,
max_wind_speed numeric,
max_wind_direction numeric,
max_air_pressure numeric,
min_temp numeric,
min_wind_speed numeric,
min_wind_direction numeric,
min_air_pressure numeric,
geom_loc geometry,
station_name character varying(40),
station_state character varying(3)
)
WITH (OIDS=FALSE);
ALTER TABLE extreme OWNER TO bomuser;
GRANT ALL ON TABLE extreme TO bomuser;
GRANT SELECT ON TABLE extreme TO public;
-- Index: extreme_station_idx
-- DROP INDEX extreme_station_idx;
CREATE INDEX extreme_station_idx
ON extreme
USING btree
(station);
DBScript:
1) high_quality_data_network_view
-- View: high_quality_data_network_view
-- DROP VIEW high_quality_data_network_view;
CREATE OR REPLACE VIEW high_quality_data_network_view AS
SELECT high_quality_data_network.stn_num AS station, high_quality_data_network.ann,
high_quality_data_network.sum, high_quality_data_network.aut,
high_quality_data_network.win, high_quality_data_network.spr,
high_quality_data_network.mon, high_quality_data_network.day,
high_quality_data_network.measurement_type, stations.geom_loc, stations.pr_name AS
station_name, stations.state AS station_state
FROM high_quality_data_network
JOIN stations ON high_quality_data_network.stn_num::numeric = stations.stn_num;
ALTER TABLE high_quality_data_network_view OWNER TO bomuser;
COMMENT ON VIEW high_quality_data_network_view IS 'high_quality_data_network with
stations location from stations table';
2) Index For high_quality_climate_data :
1) anomaly_type_idx
-- Index: anomaly_type_idx
-- DROP INDEX anomaly_type_idx;
CREATE INDEX anomaly_type_idx
ON high_quality_climate_data
USING btree
(anomaly_type);
2) measurement_type_idx
-- Index: measurement_type_idx
-- DROP INDEX measurement_type_idx;
CREATE INDEX measurement_type_idx
ON high_quality_climate_data
USING btree
(measurement_type);
3) period_type_idx
-- Index: period_type_idx
-- DROP INDEX period_type_idx;
CREATE INDEX period_type_idx
ON high_quality_climate_data
USING btree
(period_type);
4) stn_num_idx
-- Index: stn_num_idx
-- DROP INDEX stn_num_idx;
CREATE INDEX stn_num_idx
ON high_quality_climate_data
USING btree
(stn_num);
To reindex :
REINDEX TABLE high_quality_climate_data
(takes about 2453759 ms = about 41 minutes)
(tried in spatial db, removed the 4 index and recreate them took only 664826ms = abt
11 minutes)
high_quality_climate_data_view
-- View: high_quality_climate_data_view
-- DROP VIEW high_quality_climate_data_view;
CREATE OR REPLACE VIEW high_quality_climate_data_view AS
SELECT high_quality_climate_data.stn_num, high_quality_climate_data.start_date,
high_quality_climate_data.end_date, high_quality_climate_data.value,
high_quality_climate_data.measurement_type, high_quality_climate_data.period_type,
high_quality_climate_data.anomaly_type, stations.geom_loc, stations.pr_name AS
station_name, stations.state AS station_state
FROM high_quality_climate_data
JOIN stations ON high_quality_climate_data.stn_num = stations.stn_num;
ALTER TABLE high_quality_climate_data_view OWNER TO bomuser;
COMMENT ON VIEW high_quality_climate_data_view IS 'High Quality Climate Data with
Geometry Column of location';
Note :
i) Need to perform correction to value column in high_quality_climate_data table by
running this script : update high_quality_climate_data set value = null where value =
99999.9
ii) This view is used only in ClimateReports web application (ClimateReports.war)
iii) After deployed the latest ClimateReports.war, need to replace the
HighQualityData.rptdesign with the latest copy from Anu dated 4/Oct/2010.
Views which do not need anymore :
To get the min_air_pressure, another view has been created :
-- View: min_air_pressure_view
-- DROP VIEW min_air_pressure_view;
CREATE OR REPLACE VIEW min_air_pressure_view AS
SELECT sfc_mos.stn_num, LEAST(min(sfc_mos.mn_15_stn_pres),
min(sfc_mos.mn_09_stn_pres)) AS min_air_pressure
FROM sfc_mos
GROUP BY sfc_mos.stn_num
ORDER BY sfc_mos.stn_num;
ALTER TABLE min_air_pressure_view OWNER TO bomuser;
1) max_max_wnd_gust_dines_dr()
-- Function: max_max_wnd_gust_dines_dir()
-- DROP FUNCTION max_max_wnd_gust_dines_dir();
CREATE OR REPLACE FUNCTION max_max_wnd_gust_dines_dir()
RETURNS numeric AS
'select max(max_wnd_gust_dines_dir) from sfc_mos'
LANGUAGE 'sql' VOLATILE
COST 100;
ALTER FUNCTION max_max_wnd_gust_dines_dir() OWNER TO bomuser;
2) min_max_wnd_gust_dines_dir()
-- Function: min_max_wnd_gust_dines_dir()
-- DROP FUNCTION min_max_wnd_gust_dines_dir();
CREATE OR REPLACE FUNCTION min_max_wnd_gust_dines_dir()
RETURNS numeric AS
'select min(max_wnd_gust_dines_dir) from sfc_mos'
LANGUAGE 'sql' VOLATILE
COST 100;
ALTER FUNCTION min_max_wnd_gust_dines_dir() OWNER TO bomuser;