Skip to content
Snippets Groups Projects
Commit 68d263e0 authored by Auke Klazema's avatar Auke Klazema
Browse files

SW-598: Add username, password, port options to load_rotation_matrices.py

parent 540217d3
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python #!/usr/bin/env python
#coding: iso-8859-15 # coding: iso-8859-15
import re,sys,pgdb,pg import sys
import numpy as np import pgdb
from math import * import pg
from database import * from optparse import OptionParser
import getpass
# get info from database.py
dbName=getDBname()
dbHost=getDBhost()
db1 = pgdb.connect(user="postgres", host=dbHost, database=dbName)
cursor = db1.cursor()
# calling stored procedures only works from the pg module for some reason.
db2 = pg.connect(user="postgres", host=dbHost, dbname=dbName)
# #
# getRotationLines # getRotationLines
# #
def getRotationLines(filename): def get_rotation_lines(filename):
""" """
Returns a list containing all lines with rotations Returns a list containing all lines with rotations
""" """
f = open(filename,'r') f = open(filename, 'r')
lines = f.readlines() lines = f.readlines()
f.close() f.close()
return [ line.strip().split(',') for line in lines[3:]] return [line.strip().split(',') for line in lines[3:]]
## ##
def getRotationMatrix(line): def get_rotation_matrix(line):
#print line # print line
station = str(line[0]).upper().strip() station = str(line[0]).upper().strip()
anttype = str(line[1]).upper().strip() anttype = str(line[1]).upper().strip()
# make db matrix [3][3] # make db matrix [3][3]
matrix = "ARRAY[[%f,%f,%f],[%f,%f,%f],[%f,%f,%f]]" %\ matrix = "ARRAY[[%f,%f,%f],[%f,%f,%f],[%f,%f,%f]]" %\
(float(line[2]),float(line[3]),float(line[4]), \ (float(line[2]), float(line[3]), float(line[4]),
float(line[5]),float(line[6]),float(line[7]), \ float(line[5]), float(line[6]), float(line[7]),
float(line[8]),float(line[9]),float(line[10])) float(line[8]), float(line[9]), float(line[10]))
return(station,anttype,matrix) return(station, anttype, matrix)
# #
# MAIN # MAIN
# #
if __name__ == '__main__': if __name__ == '__main__':
parser = OptionParser("Usage: %prog [options] datafile")
parser.add_option("-D", "--database",
dest="dbName",
type="string",
default="StationCoordinates",
help="Name of StationCoordinates database to use")
parser.add_option("-H", "--host",
dest="dbHost",
type="string",
default="sasdb.control.lofar",
help="Hostname of StationCoordinates database")
parser.add_option("-P", "--port",
dest="dbPort",
type="int",
default="5432",
help="Port of StationCoordinates database")
parser.add_option("-U", "--user",
dest="dbUser",
type="string",
default="postgres",
help="Username of StationCoordinates database")
# parse arguments
(options, args) = parser.parse_args()
dbName = options.dbName
dbHost = options.dbHost
dbPort = options.dbPort
dbUser = options.dbUser
dbPassword = getpass.getpass()
host = "{}:{}".format(dbHost, dbPort)
db1 = pgdb.connect(user=dbUser, host=host, database=dbName, password=dbPassword)
cursor = db1.cursor()
# calling stored procedures only works from the pg module for some reason.
db2 = pg.connect(user=dbUser, host=dbHost, dbname=dbName, port=dbPort, passwd=dbPassword)
# print sys.argv
if len(args) != 1:
parser.print_help()
sys.exit(1)
# check syntax of invocation # check syntax of invocation
# Expected syntax: load_measurement stationname objecttypes datafile # Expected syntax: load_measurement stationname objecttypes datafile
# #
if (len(sys.argv) != 2): filename = str(args[0])
print "Syntax: %s datafile" % sys.argv[0]
sys.exit(1) # filename = 'rotation-matrices/rotation_matrices.dat'
filename = str(sys.argv[1])
#filename = 'rotation-matrices/rotation_matrices.dat' lines = get_rotation_lines(filename)
lines = getRotationLines(filename)
for line in lines: for line in lines:
(stationname,anttype,matrix) = getRotationMatrix(line) (stationname, anttype, matrix) = get_rotation_matrix(line)
if stationname == 'CS001': print stationname,' ',anttype,' ',matrix[0] if stationname == 'CS001':
print stationname, ' ', anttype, ' ', matrix[0]
# check stationname # check stationname
cursor.execute("select name from station") cursor.execute("select name from station")
stations = cursor.fetchall() stations = cursor.fetchall()
station = [] station = []
station.append(stationname) station.append(stationname)
if station not in stations: if station not in stations:
print "station %s is not a legal stationame" % stationname print "station %s is not a legal stationame" % stationname
sys.exit(1) sys.exit(1)
try: try:
db2.query("select * from add_rotation_matrix('%s','%s',%s)" %(stationname, anttype, matrix)) db2.query("select * from add_rotation_matrix('%s','%s',%s)" %
(stationname, anttype, matrix))
print stationname,' ',anttype,' ',matrix
print stationname, ' ', anttype, ' ', matrix
except: except:
print 'ERR, station=%s has no types defined' %(stationname) print 'ERR, station=%s has no types defined' % (stationname)
print ' Done' print ' Done'
db1.close() db1.close()
db2.close() db2.close()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment