meteo-vm2  2.0.11
oracle-source.py

This example is a Python script that generates a source table from an Oracle database.

1 import cx_Oracle
2 import sys
3 
4 oracle = cx_Oracle.Connection(sys.argv[1]).cursor()
5 
6 print("return {")
7 print(" stations={")
8 for row in oracle.execute("""
9  SELECT
10  s.id,
11  p.lat * 100000,
12  p.lon * 100000,
13  LOWER(r.name),
14  i.NOME
15  FROM
16  SX_STATIONS s,
17  SX_POINTS p,
18  SX_REPORTS r,
19  SX_STATS_INFO i
20  WHERE
21  p.id = s.point_id AND
22  r.id = s.report_id AND
23  s.id = i.station_id AND
24  LOWER(r.name) NOT IN ('synop', 'temp', 'metar')
25  ORDER BY
26  s.id
27  """):
28  s = " [%d]={lat=%d,lon=%d,rep='%s'," % (row[0],row[1],row[2],row[3])
29 
30  if row[4]: s += "B01019='%s'," % (row[4].replace("'", "\\'"))
31  s += "},"
32  print(s)
33 print(" },")
34 
35 print(" variables={")
36 for row in oracle.execute("""
37  SELECT
38  v.id,
39  u.abbreviazione,
40  v.bcode,
41  v.pind,
42  v.p1,
43  v.p2,
44  v.ltype1,
45  v.l1,
46  v.ltype2,
47  v.l2
48  FROM
49  SX_VARIABLES v,
50  SX_UNITS u
51  WHERE
52  u.id = v.unit_id
53  ORDER BY
54  v.id
55  """):
56  s = " [%d]={unit='%s',bcode='%s',tr=%d,p1=%d,p2=%d," % (row[0],row[1],row[2],int(row[3]),int(row[4]),int(row[5]))
57  import math
58  if (not math.isnan(row[6])): s += "lt1=%d," % (int(row[6]),)
59  if (not math.isnan(row[7])): s += "l1=%d," % (int(row[7]),)
60  if (not math.isnan(row[8])): s += "lt2=%d," % (int(row[8]),)
61  if (not math.isnan(row[9])): s += "l2=%d," % (int(row[9]),)
62  s += "},"
63  print(s)
64 print(" }")
65 
66 
67 print("}")