if not os.path.isfile(variables.pcp_file_txt):
    pass
else:
    subbasins = cj.extract_table_from_mdb(variables.ProjMDB, 'Watershed',
                                          variables.path + "\\watershed.tmp~")
    pcp_stations = cj.read_from(variables.pcp_file_txt)

    #pcp_fields = ["ID", "NAME", "LAT", "LONG", "ELEVATION"]
    pcp_values = {}

    # initialising the dictionary
    #for field in pcp_fields:
    #    pcp_values[field] = 0

    pcp = mdt.mdb_with_ops(variables.ProjMDB)
    pcp.connect()
    pcp.clear_table("pcp")

    for i in range(
            1, len(pcp_stations)
    ):  # the square brackets are used to escape reserved column names
        pcp_values["[ID]"] = int(
            pcp_stations[i].strip("\n").split(",")[0].strip("'"))
        pcp_values["[NAME]"] = str(
            pcp_stations[i].strip("\n").split(",")[1].strip("'"))
        pcp_values["[LAT]"] = float(
            pcp_stations[i].strip("\n").split(",")[2].strip("'"))
        pcp_values["[LONG]"] = float(
            pcp_stations[i].strip("\n").split(",")[3].strip("'"))
        pcp_values["[ELEVATION]"] = float(
    if record.split(",")[0].strip(" ") != "":
        if (record.split(",")[5][0:4] == "AUTO") or (record.split(",")[5][0:4]
                                                     == "INTE"):
            if record.split(",")[3] == "na":
                mgt1_defaults[record.split(",")[0]] = int(1)
            else:
                mgt1_defaults[record.split(",")[0]] = int(record.split(",")[3])
        elif (record.split(",")[5][0:4] == "FLOA"):
            mgt1_defaults[record.split(",")[0]] = float(record.split(",")[3])
        elif (record.split(",")[5][0:4] == "TEXT"):
            mgt1_defaults[record.split(",")[0]] = record.split(",")[3]
"""
# here we commit to table the parameters for the hru to a row in the table mgt1
"""

mgt1 = mdt.mdb_with_ops(variables.ProjMDB)
mgt1.RunSQL("""ALTER TABLE "mgt1" ADD UNIQUE (OID))""", True)
mgt1.clear_table("mgt1")

for hru in hrus:  # getting field values from from hrus table
    mgt1_defaults["SUBBASIN"] = int(hru.split(",")[1].split(".")[0])
    mgt1_defaults["HRU"] = int(hru.split(",")[12][6:10])
    mgt1_defaults["OID"] = int(hru.split(",")[0])
    mgt1_defaults["LANDUSE"] = hru.split(",")[3]
    mgt1_defaults["SOIL"] = hru.split(",")[5]
    mgt1_defaults["SLOPE_CD"] = hru.split(",")[7]

    HYDR_GROUP = None

    for record in usersoil:
        if mgt1_defaults["SOIL"] == record.split(",")[3].strip(" "):
Ejemplo n.º 3
0
if not os.path.isfile(
        "{txt_dir}TxtInOut/model.in".format(txt_dir=DefaultSimDir)):
    calibration_file = ""
else:
    calibration_file = "model.in"
    cj_function_lib.copy_file(
        "{txt_dir}TxtInOut/model.in".format(txt_dir=DefaultSimDir),
        "{rt}/Data/parameters/model.in".format(rt=root))

# replace values in project string

if is_area == "0":
    is_area = 2

# get database data
pj_db = mdt.mdb_with_ops("{rt}model/{nm}/{nm}.mdb".format(rt=root,
                                                          nm=ProjName))
rf_db = mdt.mdb_with_ops("{rt}model/{nm}/QSWATRef2012.mdb".format(rt=root,
                                                                  nm=ProjName))

pj_db.connect()
rf_db.connect()

pj_db.get_values(land_use_lookup)
land_lu_string = "LANDUSE_ID,SWAT_CODE\n"
for row in pj_db.columns:
    land_lu_string += "{code},{lu_c}\n".format(code=row[0], lu_c=row[1])
cj_function_lib.write_to(
    "{rt}Data/tables/{fn}.csv".format(rt=root, fn=land_use_lookup),
    land_lu_string)

pj_db.get_values(soil_lookup)
if not os.path.isfile(variables.tmp_file_txt):
    pass
else:
    subbasins = cj.extract_table_from_mdb(variables.ProjMDB, 'Watershed',
                                          variables.path + "\\watershed.tmp~")
    tmp_stations = cj.read_from(variables.tmp_file_txt)

    #tmp_fields = ["ID", "NAME", "LAT", "LONG", "ELEVATION"]
    tmp_values = {}

    # initialising the dictionary
    #for field in tmp_fields:
    #    tmp_values[field] = 0

    tmp = mdt.mdb_with_ops(variables.ProjMDB)
    tmp.connect()
    tmp.create_table("tmp", "[ID]", "INTEGER")
    tmp.add_field("tmp", "[NAME]", "TEXT")
    tmp.add_field("tmp", "[LAT]", "FLOAT")
    tmp.add_field("tmp", "[LONG]", "FLOAT")
    tmp.add_field("tmp", "[ELEVATION]", "FLOAT")

    tmp.clear_table(
        "tmp"
    )  # in case the table was there and there are records already, we recalculate.

    for i in range(
            1, len(tmp_stations)
    ):  # the square brackets are used to escape reserved column names
        tmp_values["[ID]"] = int(
if not os.path.isfile(variables.wnd_file_txt):
    pass
else:
    subbasins = cj.extract_table_from_mdb(variables.ProjMDB, 'Watershed',
                                          variables.path + "\\watershed.tmp~")
    wnd_stations = cj.read_from(variables.wnd_file_txt)

    #wnd_fields = ["ID", "NAME", "LAT", "LONG", "ELEVATION"]
    wnd_values = {}

    # initialising the dictionary
    #for field in wnd_fields:
    #    wnd_values[field] = 0

    wnd = mdt.mdb_with_ops(variables.ProjMDB)
    wnd.connect()
    wnd.create_table("wnd", "[ID]", "INTEGER")
    wnd.add_field("wnd", "[NAME]", "TEXT")
    wnd.add_field("wnd", "[LAT]", "FLOAT")
    wnd.add_field("wnd", "[LONG]", "FLOAT")
    wnd.add_field("wnd", "[ELEVATION]", "FLOAT")

    wnd.clear_table(
        "wnd"
    )  # in case the table was there and there are records already, we recalculate.

    for i in range(
            1, len(wnd_stations)
    ):  # the square brackets are used to escape reserved column names
        wnd_values["[ID]"] = int(
Ejemplo n.º 6
0
if not os.path.isfile(variables.hmd_file_txt):
    pass
else:
    subbasins = cj.extract_table_from_mdb(variables.ProjMDB, 'Watershed',
                                          variables.path + "\\watershed.tmp~")
    hmd_stations = cj.read_from(variables.hmd_file_txt)

    #hmd_fields = ["ID", "NAME", "LAT", "LONG", "ELEVATION"]
    hmd_values = {}

    # initialising the dictionary
    #for field in hmd_fields:
    #    hmd_values[field] = 0

    hmd = mdt.mdb_with_ops(variables.ProjMDB)
    hmd.connect()
    hmd.create_table("hmd", "[ID]", "INTEGER")
    hmd.add_field("hmd", "[NAME]", "TEXT")
    hmd.add_field("hmd", "[LAT]", "FLOAT")
    hmd.add_field("hmd", "[LONG]", "FLOAT")
    hmd.add_field("hmd", "[ELEVATION]", "FLOAT")

    hmd.clear_table(
        "hmd"
    )  # in case the table was there and there are records already, we recalculate.

    for i in range(
            1, len(hmd_stations)
    ):  # the square brackets are used to escape reserved column names
        hmd_values["[ID]"] = int(
print("\t> Getting database tables \t\t: {0}".format(
    os.path.join(sys.argv[1], "Data", "tables")))

# Here we set lookup tables, usersoil and WGEN_user in the Ref and Project Databases
soil_lu = cj.read_from(
    os.path.join(sys.argv[1], "Data", "tables", namelist.soil_lookup))
landuse_lu = cj.read_from(
    os.path.join(sys.argv[1], "Data", "tables", namelist.landuse_lookup))
WGEN_data = cj.read_from(
    os.path.join(sys.argv[1], "Data", "tables", namelist.WGEN_user))
usersoil_data = cj.read_from(
    os.path.join(sys.argv[1], "Data", "tables", namelist.Usersoil))

print("\t> Configuring databasses \t\t: {0}.mdb & QSWATRef2012.mdb".format(
    project_name))
prj_dbase = mdt.mdb_with_ops(
    os.path.join(current_root, project_name, project_name + ".mdb"))
ref_dbase = mdt.mdb_with_ops(
    os.path.join(current_root, project_name, "QSWATRef2012.mdb"))

# how to remove existing table if any?
try:
    # prj_dbase.delete_table("landuse_lookup")
    prj_dbase.delete_table("hru")
except:
    pass
try:
    prj_dbase.delete_table("soil_lookup")
except:
    pass

prj_dbase.clear_table("landuse_lookup")
import sys

import cj_function_lib as cj
import init_file as variables
import mdbtools as mdt

master = mdt.mdb_with_ops(variables.ProjMDB)

master_dictionary = {}

master_dictionary["WorkDir"] = variables.ProjDir
master_dictionary["OutputGDB"] = variables.ProjMDB
master_dictionary["RasterGDB"] = ""
master_dictionary["SwatGDB"] = "{0}\QSWATRef2012.mdb".format(variables.ProjDir)
master_dictionary["WshdGrid"] = ""
master_dictionary["ClipDemGrid"] = ""
master_dictionary["SoilOption"] = "name"
master_dictionary["NumLuClasses"] = 0
master_dictionary["DoneWSDDel"] = 1
master_dictionary["DoneSoilLand"] = 1
master_dictionary["DoneWeather"] = 1
master_dictionary["DoneModelSetup"] = 1
master_dictionary["OID"] = 1
master_dictionary["MGT1_Checked"] = 1
master_dictionary["ArcSWAT_V_Create"] = ""
master_dictionary["ArcSWAT_V_Curr"] = "2012.10.19"
master_dictionary["AccessExePath"] = ""
master_dictionary["DoneModelRun"] = 1

master.clear_table("MasterProgress")
if not os.path.isfile(variables.slr_file_txt):
    pass
else:
    subbasins = cj.extract_table_from_mdb(variables.ProjMDB, 'Watershed',
                                          variables.path + "\\watershed.tmp~")
    slr_stations = cj.read_from(variables.slr_file_txt)

    #slr_fields = ["ID", "NAME", "LAT", "LONG", "ELEVATION"]
    slr_values = {}

    # initialising the dictionary
    #for field in slr_fields:
    #    slr_values[field] = 0

    slr = mdt.mdb_with_ops(variables.ProjMDB)
    slr.connect()
    try:
        slr.create_table("SubSlr", "ObjectID", "INTEGER")
    except:
        pass
    try:
        slr.add_field("SubSlr", "Subbasin", "INTEGER")
        slr.add_field("SubSlr", "MinDist", "FLOAT")
        slr.add_field("SubSlr", "MinRec", "INTEGER")
        slr.add_field("SubSlr", "Station", "TEXT")
        slr.add_field("SubSlr", "OrderID", "INTEGER")
        slr.add_field("SubSlr", "TimeStep", "INTEGER")
    except:
        pass