NetCDF to PostGIS Script
Script
# Import modules
import psycopg2, time, datetime
from scipy.io import netcdf
import psycopg2, time, datetime
from scipy.io import netcdf
# Establish connection
db1 = psycopg2.connect("host=xxxx dbname=priogrid user=xxxx password=xxxx")
cur = db1.cursor()
db1 = psycopg2.connect("host=xxxx dbname=priogrid user=xxxx password=xxxx")
cur = db1.cursor()
# Create Table in postgis
print str(time.ctime())+ " Creating precipud19002008 table."
cur.execute("DROP TABLE IF EXISTS precipud19002008;")
cur.execute("CREATE TABLE precipud19002008 (gid serial PRIMARY KEY not null, year int, month int, lon decimal, lat decimal, prec decimal);")
print str(time.ctime())+ " Creating precipud19002008 table."
cur.execute("DROP TABLE IF EXISTS precipud19002008;")
cur.execute("CREATE TABLE precipud19002008 (gid serial PRIMARY KEY not null, year int, month int, lon decimal, lat decimal, prec decimal);")
# Read netcdf file using the netcdf_file class. 'r' for read.
f = netcdf.netcdf_file('/mnt/pc258/prio_grid/source/ClimateData/precipud19002008.nc', 'r')
f = netcdf.netcdf_file('/mnt/pc258/prio_grid/source/ClimateData/precipud19002008.nc', 'r')
# Create lathash. Read the f.variables['lat'].data.tolist() creates a list of lat coodinates.
print str(time.ctime())+ " Looping through lat coords."
temp = f.variables['lat'].data.tolist()
# Create list of lat coords
lathash = {}
for entry in temp:
lathash[temp.index(entry)] = entry
print str(time.ctime())+ " Looping through lat coords."
temp = f.variables['lat'].data.tolist()
# Create list of lat coords
lathash = {}
for entry in temp:
lathash[temp.index(entry)] = entry
# Create list of lon coords
print str(time.ctime())+ " Looping through long coords."
temp = f.variables['lon'].data.tolist()
lonhash = {}
for entry in temp:
lonhash[temp.index(entry)] = entry
print str(time.ctime())+ " Looping through long coords."
temp = f.variables['lon'].data.tolist()
lonhash = {}
for entry in temp:
lonhash[temp.index(entry)] = entry
# Loop through every observation. Set timedimension and lat and long observations.
for _month in xrange(1308):
print str(time.ctime())+ " Extracting NetCDF information for month number "+str(_month+1)+" of 1308"
for _lon in xrange(720):
for _lat in xrange(360):
thisyear = int((_month)/12+1900)
thismonth = ((_month) % 12)+1
thisdate = datetime.date(thisyear,thismonth, 1)
data = [int(thisyear), int(thismonth), lonhash[_lon]-180.00, lathash[_lat], f.variables[('data')].data[_month, _lat, _lon]+32767]
for _month in xrange(1308):
print str(time.ctime())+ " Extracting NetCDF information for month number "+str(_month+1)+" of 1308"
for _lon in xrange(720):
for _lat in xrange(360):
thisyear = int((_month)/12+1900)
thismonth = ((_month) % 12)+1
thisdate = datetime.date(thisyear,thismonth, 1)
data = [int(thisyear), int(thismonth), lonhash[_lon]-180.00, lathash[_lat], f.variables[('data')].data[_month, _lat, _lon]+32767]
cur.execute("INSERT INTO precipud19002008 (year, month, lon, lat, prec) VALUES "+str(tuple(data))+";")
print str(time.ctime())+ " Done!"
db1.commit()
cur.close()
Comments
Post a Comment