-
Notifications
You must be signed in to change notification settings - Fork 3
/
DSRA_runCreateTableShakemap.py
94 lines (82 loc) · 3.37 KB
/
DSRA_runCreateTableShakemap.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
86
87
88
89
90
91
92
93
94
#Import statements
import sys
import os
import argparse
import configparser
import psycopg2
import logging
import csv
'''
Script to run Create_table_shakemap.sql
python3 DSRA_runCreateTableShakemap.py --shakemapFile=s_shakemap_IDM6p8_Sidney_221.csv
'''
#Main Function
def main ():
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[logging.FileHandler('/tmp/{}.log'.format(os.path.splitext(sys.argv[0])[0])),
logging.StreamHandler()])
os.chdir(sys.path[0])
auth = get_config_params('config.ini')
args = parse_args()
eqScenario='{}_{}'.format(args.shakemapFile.split('_')[2],args.shakemapFile.split('_')[3])
with open(args.shakemapFile, "r") as f:
reader = csv.reader(f)
headerFields = next(reader)
headerFields = ','.join('"{0}"'.format(w) for w in headerFields)
headerFields = headerFields.replace('"site_id"','site_id')
headerFields = headerFields.replace('"gmv_PGA"','gmv_PGA')
headerFields = headerFields.replace('"lon"','lon')
headerFields = headerFields.replace('"lat"','lat')
sqlquerystring = open('Create_table_shakemap.sql', 'r').read().format(**{
'shakemapFile':args.shakemapFile,
'eqScenario':eqScenario,
'headerFields':headerFields})
try:
connection = psycopg2.connect(user = auth.get('rds', 'postgres_un'),
password = auth.get('rds', 'postgres_pw'),
host = auth.get('rds', 'postgres_host'),
port = auth.get('rds', 'postgres_port'),
database = auth.get('rds', 'postgres_db'))
cursor = connection.cursor()
cursor.execute(sqlquerystring)
#cursor.commit()
connection.commit()
except (Exception, psycopg2.Error) as error :
logging.error(error)
finally:
if(connection):
cursor.close()
connection.close()
systemCall="""psql -h ${{POSTGRES_HOST}}
-U ${{POSTGRES_USER}}
-d ${{DB_NAME}}
-a
-c '\copy gmf.shakemap_{eqScenario} ({headerFields})
FROM /usr/src/app/{shakemapFile}
WITH
CSV HEADER ;'""".format(**{'shakemapFile':args.shakemapFile,
'eqScenario':eqScenario,
'headerFields':headerFields})
systemCall = ' '.join(systemCall.split())
os.system(systemCall)
return
#Support Functions
def get_config_params(args):
"""
Parse Input/Output columns from supplied *.ini file
"""
configParseObj = configparser.ConfigParser()
configParseObj.read(args)
return configParseObj
def parse_args():
parser = argparse.ArgumentParser(description='''Script to create DSRA shakemap
table in PostGIS database from raw CSV file
Can be run from the command line with mandatory arguments
Run this script with a command like:
python3 DSRA_runCreateTableShakemap.py --shakemapFile=s_shakemap_IDM6p8_Sidney_221.csv ''')
parser.add_argument("--shakemapFile", type=str, help="Shakemap CSV File")
args = parser.parse_args()
return args
if __name__ == '__main__':
main()