<commit_before>""" Class definition for the VIC model interface

.. module:: vic
   :synopsis: Definition of the VIC model class

.. moduleauthor:: <NAME> <<EMAIL>>

"""

import output as vicoutput
from osgeo import ogr, gdal, osr
import decimal
import sys
import subprocess
import os
import string
import numpy
from datetime import date, datetime, timedelta
import multiprocessing as mp
import shutil
import numpy as np
from collections import OrderedDict
import drought
import pandas
import psycopg2 as pg
import rpath
import random
from raster import TileReader


class VIC:

    def __init__(self, path, dbname, resolution, startyear, startmonth, startday,
                 endyear, endmonth, endday, name="", savestate="", nlayer=3):
        self.model_path = path
        self.nodata = -9999.
        if bool(name):
            self.name = name
        else:
            self.name = None
        self.startyear = startyear
        self.startmonth = startmonth
        self.startday = startday
        self.endyear = endyear
        self.endmonth = endmonth
        self.endday = endday
        self.nlayers = nlayer
        self.dbname = dbname
        db = pg.connect(database=dbname)
        cur = db.cursor()
        cur.execute(
            "select resolution from vic.input order by abs(resolution - {0})".format(resolution))
        if not bool(cur.rowcount):
            print("No appropriate VIC input files found in the database. Exiting!")
            sys.exit()
        self.res = cur.fetchone()[0]
        cur.close()
        try:
            self.grid_decimal = - \
                (decimal.Decimal(self.res).as_tuple().exponent - 1)
        except:
            self.grid_decimal = - \
                (decimal.Decimal(str(self.res)).as_tuple().exponent - 1)
        self.lat = []
        self.lon = []
        self.gid = OrderedDict()
        self.lgid = OrderedDict()
        self.depths = OrderedDict()
        self.skipyear = 0
        self.elev = OrderedDict()
        self.statefile = ""

    def paramFromDB(self):
        """Retrieve file parameters from database."""
        db = pg.connect(database=self.dbname)
        cur = db.cursor()
        # cur = self.db.cursor()
        cur.execute(
            'select veglib,vegparam,snowbandfile from vic.input where resolution=%f;' % self.res)
        veglib, vegparam, snowbands = cur.fetchone()
        cur.close()
        db.close()
        return veglib, vegparam, snowbands

    def _getSnowbands(self, snowbands):
        """Find number of snow bands from file."""
        filename = "{0}/{1}".format(rpath.data, snowbands)
        with file(filename) as f:
            line = f.readline()
        return (len(line.split()) - 1) / 3

    def writeSoilFile(self, shapefile):
        """Write soil parameter file for current simulation based on basin shapefile."""
        ds = ogr.Open(shapefile)
        lyr = ds.GetLayer()
        db = pg.connect(database=self.dbname)
        cur = db.cursor()
        cur.execute(
            "select * from information_schema.tables where table_name='basin' and table_schema=%s", (self.name,))
        if not bool(cur.rowcount):
            temptable = ''.join(random.SystemRandom().choice(
                string.ascii_letters) for _ in range(8))
            cur.execute(
                "create table {0}(gid serial primary key, geom geometry)".format(temptable))
            for i in range(lyr.GetFeatureCount()):
                f = lyr.GetNextFeature()
                g = f.GetGeometryRef()
                cur.execute("insert into {0}(geom) values(st_geomfromtext('{1}',4326))".format(
                    temptable, g.ExportToWkt()))
            sql = "select updategeometrysrid('{0}','geom',4326)".format(
                temptable)
            db.commit()
            cur.execute(
                "create index {0}_s on {0} using gist(geom)".format(temptable))
            ds = None
            cur.execute(
                "select schema_name from information_schema.schemata where schema_name='{0}'".format(self.name))
            if not bool(cur.rowcount):
                cur.execute("create schema {0}".format(self.name))
            sql = "create table {0}.basin (gid integer, elev real, depths real[], geom geometry(Point, 4326), line text, constraint {0}_gidkey primary key(gid),  CONSTRAINT enforce_dims_geom CHECK (st_ndims(geom) = 2), CONSTRAINT enforce_geotype_geom CHECK (geometrytype(geom) = 'POINT'::text OR geom IS NULL))".format(self.name)
            cur.execute(sql)
            sql = "insert into {0}.basin (gid, elev, depths, geom, line) select v.id,v.elev,v.depths,v.geom,v.line from vic.soils as v,{1} as t where st_intersects(v.geom,t.geom) and resolution={2}".format(
                self.name, temptable, self.res)
            cur.execute(sql)
            cur.execute("drop table {0}".format(temptable))
            db.commit()
            cur.execute(
                "create index basin_s on {0}.basin using gist(geom)".format(self.name))
        sql = "select line,gid,st_y(geom),st_x(geom),elev,depths from {0}.basin order by gid".format(
            self.name)
        cur.execute(sql)
        lines = cur.fetchall()
        with open(self.model_path + '/soil.txt', 'w') as fout:
            for line in lines:
                gid, lat, lon, elev, depths = line[1:]
                fout.write("{0}\n".format(line[0]))
                self.lat.append(lat)
                self.lon.append(lon)
                self.gid[gid] = (lat, lon)
                self.lgid[(lat, lon)] = gid
                self.depths[gid] = depths
                self.elev[gid] = elev
        cur.execute("alter table {0}.basin drop column line".format(self.name))
        cur.close()
        db.close()

    def stateFile(self):
        """Retrieve state file path from database."""
        db = pg.connect(database=self.dbname)
        cur = db.cursor()
        sql = "select filename from {0}.state where fdate = date '{1}-{2}-{3}'".format(
            self.name, self.startyear, self.startmonth, self.startday)
        cur.execute(sql)
        result = cur.fetchone()
        if bool(result):<commit_msg>Added index creation in basin database table.<commit_after>""" Class definition for the VIC model interface

.. module:: vic
   :synopsis: Definition of the VIC model class

.. moduleauthor:: <NAME> <<EMAIL>>

"""

import output as vicoutput
from osgeo import ogr, gdal, osr
import decimal
import sys
import subprocess
import os
import string
import numpy
from datetime import date, datetime, timedelta
import multiprocessing as mp
import shutil
import numpy as np
from collections import OrderedDict
import drought
import pandas
import psycopg2 as pg
import rpath
import random
from raster import TileReader


class VIC:

    def __init__(self, path, dbname, resolution, startyear, startmonth, startday,
                 endyear, endmonth, endday, name="", savestate="", nlayer=3):
        self.model_path = path
        self.nodata = -9999.
        if bool(name):
            self.name = name
        else:
            self.name = None
        self.startyear = startyear
        self.startmonth = startmonth
        self.startday = startday
        self.endyear = endyear
        self.endmonth = endmonth
        self.endday = endday
        self.nlayers = nlayer
        self.dbname = dbname
        db = pg.connect(database=dbname)
        cur = db.cursor()
        cur.execute(
            "select resolution from vic.input order by abs(resolution - {0})".format(resolution))
        if not bool(cur.rowcount):
            print("No appropriate VIC input files found in the database. Exiting!")
            sys.exit()
        self.res = cur.fetchone()[0]
        cur.close()
        try:
            self.grid_decimal = - \
                (decimal.Decimal(self.res).as_tuple().exponent - 1)
        except:
            self.grid_decimal = - \
                (decimal.Decimal(str(self.res)).as_tuple().exponent - 1)
        self.lat = []
        self.lon = []
        self.gid = OrderedDict()
        self.lgid = OrderedDict()
        self.depths = OrderedDict()
        self.skipyear = 0
        self.elev = OrderedDict()
        self.statefile = ""

    def paramFromDB(self):
        """Retrieve file parameters from database."""
        db = pg.connect(database=self.dbname)
        cur = db.cursor()
        # cur = self.db.cursor()
        cur.execute(
            'select veglib,vegparam,snowbandfile from vic.input where resolution=%f;' % self.res)
        veglib, vegparam, snowbands = cur.fetchone()
        cur.close()
        db.close()
        return veglib, vegparam, snowbands

    def _getSnowbands(self, snowbands):
        """Find number of snow bands from file."""
        filename = "{0}/{1}".format(rpath.data, snowbands)
        with file(filename) as f:
            line = f.readline()
        return (len(line.split()) - 1) / 3

    def writeSoilFile(self, shapefile):
        """Write soil parameter file for current simulation based on basin shapefile."""
        ds = ogr.Open(shapefile)
        lyr = ds.GetLayer()
        db = pg.connect(database=self.dbname)
        cur = db.cursor()
        cur.execute(
            "select * from information_schema.tables where table_name='basin' and table_schema=%s", (self.name,))
        if not bool(cur.rowcount):
            temptable = ''.join(random.SystemRandom().choice(
                string.ascii_letters) for _ in range(8))
            cur.execute(
                "create table {0}(gid serial primary key, geom geometry)".format(temptable))
            for i in range(lyr.GetFeatureCount()):
                f = lyr.GetNextFeature()
                g = f.GetGeometryRef()
                cur.execute("insert into {0}(geom) values(st_geomfromtext('{1}',4326))".format(
                    temptable, g.ExportToWkt()))
            sql = "select updategeometrysrid('{0}','geom',4326)".format(
                temptable)
            db.commit()
            cur.execute(
                "create index {0}_s on {0} using gist(geom)".format(temptable))
            ds = None
            cur.execute(
                "select schema_name from information_schema.schemata where schema_name='{0}'".format(self.name))
            if not bool(cur.rowcount):
                cur.execute("create schema {0}".format(self.name))
            sql = "create table {0}.basin (gid integer, elev real, depths real[], geom geometry(Point, 4326), line text, constraint {0}_gidkey primary key(gid),  CONSTRAINT enforce_dims_geom CHECK (st_ndims(geom) = 2), CONSTRAINT enforce_geotype_geom CHECK (geometrytype(geom) = 'POINT'::text OR geom IS NULL))".format(self.name)
            cur.execute(sql)
            sql = "insert into {0}.basin (gid, elev, depths, geom, line) select v.id,v.elev,v.depths,v.geom,v.line from vic.soils as v,{1} as t where st_intersects(v.geom,t.geom) and resolution={2}".format(
                self.name, temptable, self.res)
            cur.execute(sql)
            cur.execute("drop table {0}".format(temptable))
            db.commit()
            cur.execute(
                "create index basin_s on {0}.basin using gist(geom)".format(self.name))
            db.commit()
        sql = "select line,gid,st_y(geom),st_x(geom),elev,depths from {0}.basin order by gid".format(
            self.name)
        cur.execute(sql)
        lines = cur.fetchall()
        with open(self.model_path + '/soil.txt', 'w') as fout:
            for line in lines:
                gid, lat, lon, elev, depths = line[1:]
                fout.write("{0}\n".format(line[0]))
                self.lat.append(lat)
                self.lon.append(lon)
                self.gid[gid] = (lat, lon)
                self.lgid[(lat, lon)] = gid
                self.depths[gid] = depths
                self.elev[gid] = elev
        cur.execute("alter table {0}.basin drop column line".format(self.name))
        cur.close()
        db.close()

    def stateFile(self):
        """Retrieve state file path from database."""
        db = pg.connect(database=self.dbname)
        cur = db.cursor()
        sql = "select filename from {0}.state where fdate = date '{1}-{2}-{3}'".format(
            self.name, self.startyear, self.startmonth, self.startday)
        cur.execute(sql)
        result = cur.fetchone()
        if bool(result):
{'fraction_non_alphanumeric': 0.12125141838223374, 'fraction_numerical': 0.00859134381585346, 'mean_word_length': 2.1222165991902835, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5278785', 'n_tokens_mistral': 3808, 'n_tokens_neox': 3646, 'n_words': 940}