-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetcdf.py
More file actions
48 lines (39 loc) · 1.23 KB
/
netcdf.py
File metadata and controls
48 lines (39 loc) · 1.23 KB
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
import sys
from ucar.nc2.dataset import NetcdfDataset
from com.ziclix.python.sql import zxJDBC
###
PATH = "C:/Works/"
FILENAME = "cedh20140801_0000+00300"
DATABASE = "forecast.db"
VARIABLE = "conprec"
JDBC_URL = "jdbc:sqlite:%s%s" % (PATH, DATABASE)
JDBC_DRIVER = "org.sqlite.JDBC"
QRY_PRCID = "SELECT * FROM PrecipCells WHERE PrecipID=?"
DEL_DSS = "DELETE FROM cdfadat WHERE filename=?"
INS_DSS = "INSERT INTO cdfadat VALUES(?,?,?)"
###
def getConnection(jdbc_url, driverName):
try:
dbConn = zxJDBC.connect(jdbc_url, None, None, driverName)
except zxJDBC.DatabaseError, msg:
print msg
sys.exit(-1)
return dbConn
###
nc = NetcdfDataset.openDataset("%s%s" % (PATH, FILENAME))
precval = nc.findVariable("%s" % VARIABLE)
cells = precval.read()
nc.close()
shapes = cells.getShape()
dbConn = getConnection(JDBC_URL, JDBC_DRIVER)
cursor = dbConn.cursor()
cursor.execute(DEL_DSS, [FILENAME])
for x in range(shapes[1]):
for y in range(shapes[2]):
valID = 1000*y + x
cursor.execute(QRY_PRCID, [valID])
if cursor.rowcount > 0:
valPrec = cells.get(0, x, y)
cursor.execute(INS_DSS,[valID, FILENAME, valPrec])
dbConn.commit()
dbConn.close()