def writeToDB(self, data, dates, tablename, initialize, ensemble=False, skipsave=0):
        db = dbio.connect(self.dbname)
        cur = db.cursor()
        cur.execute(
            "select * from information_schema.tables where table_name='{0}' and table_schema='{1}'".format(tablename, self.name))
        # FIXME: Check earliest date to be saved: if it overlaps with existing
        # table, create new table; else append to table
        if initialize and bool(cur.rowcount):
            cur.execute("drop table {0}.{1}".format(self.name, tablename))
        if initialize or not bool(cur.rowcount):
            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)
            cur.execute(sql)
            if data.shape[1] > 1:
                cur.execute("alter table {0}.{1} add column layer int".format(
                    self.name, tablename))
            if ensemble:
                cur.execute("alter table {0}.{1} add column ensemble int".format(
                    self.name, tablename))
        startyear, startmonth, startday = self.startyear, self.startmonth, self.startday
        if skipsave > 0:
            ts = date(self.startyear, self.startmonth,
                      self.startday) + timedelta(skipsave)
            data = data[skipsave:]
            startyear, startmonth, startday = ts.year, ts.month, ts.day
        tiffiles = []
        for t in range(data.shape[0]):
            dt = date(startyear, startmonth, startday) + timedelta(t)
            for lyr in range(data.shape[1]):
                filename = "{0}/{1}_{2}{3:02d}{4:02d}_{5:02d}.tif".format(
                    self.model_path, tablename, dt.year, dt.month, dt.day, lyr + 1)
                # if np.all(data[t, lyr, :, :]):
                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()
        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))
        if data.shape[1] > 1:
            cur.execute("alter table temp add column layer int")
            cur.execute("update temp set layer=(substring(filename from {0} for 2))::int".format(
                len(tablename) + 11))
        cur.execute("select count(*) from temp")
        n = int(cur.fetchone()[0])
        ntiles = n / data.shape[0]
        if data.shape[1] > 1:
            cur.execute(
                "insert into {0}.{1} (rid,fdate,layer,rast) select ((rid+{2}) % {2})+1,fdate,layer,rast from temp".format(self.name, tablename, ntiles))
        else:
            cur.execute(
                "insert into {0}.{1} (rid,fdate,rast) select ((rid+{2}) % {2})+1,fdate,rast from temp".format(self.name, tablename, ntiles))
        if bool(ensemble):
            sql = "update {0}.{1} set ensemble = {2} where ensemble is null".format(
                self.name, tablename, int(ensemble))
            cur.execute(sql)
        cur.execute("drop index if exists {0}.{1}_dtidx".format(
            self.name, tablename))
        cur.execute("create index {1}_dtidx on {0}.{1}(fdate)".format(
            self.name, tablename))
        cur.execute("drop index if exists {0}.{1}_spidx".format(
            self.name, tablename))
        cur.execute("create index {1}_spidx on {0}.{1} using gist(st_convexhull(rast))".format(
            self.name, tablename))
        db.commit()
        cur.close()
        db.close()
Writes output data into database.
dataset/ETHPy150Open nasa/RHEAS/src/vic/vic.py/VIC.writeToDB