-
Notifications
You must be signed in to change notification settings - Fork 1
/
exposure_summary.py
85 lines (50 loc) · 1.89 KB
/
exposure_summary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import sys
import os
import datetime
import psycopg2
import pandas
from subprocess import call, Popen
print "running exposure algorithim.."
conn_string = "dbname='hamlethurricane' user=postgres port='5432' host='127.0.0.1' password='password'"
try:
conn = psycopg2.connect(conn_string)
except Exception as e:
print str(e)
sys.exit()
hurricane_name = 'ARTHUR'
dataframe_cur = conn.cursor()
dataframe_sql = """Select * from hurricane_{}""".format(hurricane_name)
dataframe_cur.execute(dataframe_sql)
data = dataframe_cur.fetchall()
colnames = [desc[0] for desc in dataframe_cur.description]
dataframe = pandas.DataFrame(data)
dataframe.columns = colnames
conn.commit()
range_feat = range(len(dataframe)-1)
range_feat_strp = str(range_feat).strip('[]')
range_feat_strp_v2 = range_feat_strp.split(',')
drop_if_cur = conn.cursor()
drop_if_sql = """drop table if exists exposed_parcels, summary_statistics cascade"""
drop_if_cur.execute(drop_if_sql)
conn.commit()
exposed_cur = conn.cursor()
exposed_sql = """create table exposed_parcels as
select * from hurricane_{}_parcels where iso_time is not null""".format(hurricane_name, hurricane_name)
exposed_cur.execute(exposed_sql)
summary_cur = conn.cursor()
conn.commit()
summary_sql = """create table summary_statistics as
select sum(parval) as dollars_exposed,
count(gid), iso_time, cntyname from exposed_parcels
group by iso_time, cntyname; """
summary_cur.execute(summary_sql)
summary_stats_cur = conn.cursor()
summary_stats_sql = """Select * from summary_statistics"""
summary_stats_cur.execute(summary_stats_sql)
data = summary_stats_cur.fetchall()
colnames = [desc[0] for desc in summary_stats_cur.description]
dataframe = pandas.DataFrame(data)
dataframe.columns = colnames
conn.commit()
print 'The summary statistics are'
print dataframe