Skip to content

Commit

Permalink
Fixes #38. Fix bug when writing ensemble output.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kostas Andreadis committed Jun 24, 2016
1 parent 1fced73 commit a7a6210
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
13 changes: 6 additions & 7 deletions src/ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,15 @@ def _ESP(self, options):
date(self.startyear, self.startmonth, self.startday)).days
db = dbio.connect(self.models[0].dbname)
cur = db.cursor()
sql = "select distinct (date_part('year', fdate)) as year from precip.{0}".format(
options['vic']['precip'])
if self.startmonth < self.endmonth:
sql = "select distinct (date_part('year', fdate)) as year from precip.{0} where date_part('month', fdate) >= {1} and date_part('month', fdate) <= {2}".format(options['vic']['precip'], self.startmonth, self.endmonth)
else:
sql = "select distinct (date_part('year', fdate)) as year from precip.{0} where date_part('month', fdate) >= {1} or date_part('month', fdate) <= {2}".format(options['vic']['precip'], self.startmonth, self.endmonth)
cur.execute(sql)
years = map(lambda y: int(y[0]), cur.fetchall())
random.shuffle(years)
if self.startyear in years:
years.remove(self.startyear)
# will need to check whether enough days exist in sampled year before
# removing
years.remove(max(years))
while len(years) < self.nens:
years += years
for e in range(self.nens):
model = self.models[e]
model.startyear = years[e]
Expand Down
21 changes: 12 additions & 9 deletions src/vic/vic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""

from __future__ import division
import output as vicoutput
from osgeo import ogr, gdal, osr
import decimal
Expand Down Expand Up @@ -89,7 +90,7 @@ def _getSnowbands(self, snowbands):
filename = "{0}/{1}".format(rpath.data, snowbands)
with file(filename) as f:
line = f.readline()
return (len(line.split()) - 1) / 3
return int((len(line.split()) - 1) / 3)

def writeSoilFile(self, shapefile):
"""Write soil parameter file for current simulation based on basin shapefile."""
Expand Down Expand Up @@ -441,8 +442,8 @@ def readOutput(self, args):
outvars = self.getOutputStruct(self.model_path + "/global.txt")
outdata = {}
if len(self.lat) > 0 and len(self.lon) > 0:
nrows = np.round((max(self.lat) - min(self.lat)) / self.res) + 1
ncols = np.round((max(self.lon) - min(self.lon)) / self.res) + 1
nrows = int(np.round((max(self.lat) - min(self.lat)) / self.res) + 1)
ncols = int(np.round((max(self.lon) - min(self.lon)) / self.res) + 1)
nt = (date(self.endyear, self.endmonth, self.endday) -
date(self.startyear + self.skipyear, self.startmonth, self.startday)).days + 1
args = vicoutput.variableGroup(args)
Expand Down Expand Up @@ -516,8 +517,9 @@ def writeToDB(self, data, dates, tablename, initialize, ensemble=False, skipsave
cur.execute("drop table {0}.{1}".format(self.name, tablename))
db.commit()
if dbio.tableExists(self.dbname, self.name, tablename):
for dt in [self.startdate + timedelta(t) for t in range((self.enddate - self.startdate).days+1)]:
dbio.deleteRasters(self.dbname, "{0}.{1}".format(self.name, tablename), dt)
if initialize:
for dt in [self.startdate + timedelta(t) for t in range((self.enddate - self.startdate).days+1)]:
dbio.deleteRasters(self.dbname, "{0}.{1}".format(self.name, tablename), dt)
else:
sql = "create table {0}.{1} (id serial not null primary key, rid int not null, fdate date not null, rast raster)".format(
self.name, tablename)
Expand All @@ -541,10 +543,11 @@ def writeToDB(self, data, dates, tablename, initialize, ensemble=False, skipsave
self.model_path, tablename, dt.year, dt.month, dt.day, lyr + 1)
self._writeRaster(data[t, lyr, :, :], filename)
tiffiles.append(filename)
ps = subprocess.Popen(["{0}/raster2pgsql".format(rpath.bins), "-s", "4326", "-F", "-d", "-t", "auto"] + tiffiles + ["temp"], stdout=subprocess.PIPE)
subprocess.Popen(["{0}/psql".format(rpath.bins), "-d", self.dbname], stdin=ps.stdout)
ps.stdout.close()
ps.wait()
ps1 = subprocess.Popen(["{0}/raster2pgsql".format(rpath.bins), "-s", "4326", "-F", "-d", "-t", "auto"] + tiffiles + ["temp"], stdout=subprocess.PIPE)
ps2 = subprocess.Popen(["{0}/psql".format(rpath.bins), "-d", self.dbname], stdin=ps1.stdout, stdout=subprocess.PIPE)
ps1.stdout.close()
ps2.communicate()[0]
ps1.wait()
cur.execute("alter table temp add column fdate date")
cur.execute("update temp set fdate = date (concat_ws('-',substring(filename from {0} for 4),substring(filename from {1} for 2),substring(filename from {2} for 2)))".format(
len(tablename) + 2, len(tablename) + 6, len(tablename) + 8))
Expand Down

0 comments on commit a7a6210

Please sign in to comment.