start_year = current_year for i in range(1, len(stations_dict["1"].data_list)): line = "" current_day += 1 if current_day <= end_day: datecode = str(current_year) + cj.trailing_zeros(3, current_day, 0) #print datecode else: current_day = 1 current_year += 1 end_day = cj.get_days_in_year(current_year) datecode = str(current_year) + cj.trailing_zeros(3, current_day, 0) #print end_day for sta_nr in station_nr: rec = cj.trailing_zeros(5, stations_dict[str(sta_nr)].data_list[i], 1) line += rec line = datecode + line + "\n" pcp_string += line end_year = current_year IDAL = end_day cj.write_to(variables.path +"\\cio_config.tmp~", str(start_year) + "\n" + str(end_year) + "\n" + str((end_year - start_year) + 1) + \ "\n" + str(IDAF) + "\n" + str(IDAL) + "\n\n" + str(len(stations_list))) cj.write_to(variables.DefaultSimDir + "TxtInOut\\pcp1.pcp", pcp_string)
# Building String rte_file = " .rte file Subbasin: " + SubBasin + " " + DateAndTime + " " + SWAT_Vers + \ "\n" + cj.trailing_spaces(14, CHW2, 3) + " | CHW2 : Main channel width [m]" + \ "\n" + cj.trailing_spaces(14, CHD, 3) + " | CHD : Main channel depth [m]" + \ "\n" + cj.trailing_spaces(14, CH_S2, 5) + " | CH_S2 : Main channel slope [m/m]" + \ "\n" + cj.trailing_spaces(14, CH_L2, 3) + " | CH_L2 : Main channel length [km]" + \ "\n" + cj.trailing_spaces(14, CH_N2, 3) + " | CH_N2 : Manning's nvalue for main channel" + \ "\n" + cj.trailing_spaces(14, CH_K2, 3) + " | CH_K2 : Effective hydraulic conductivity [mm/hr]" + \ "\n" + cj.trailing_spaces(14, CH_COV1, 3) + " | CH_COV1: Channel erodibility factor" + \ "\n" + cj.trailing_spaces(14, CH_COV2, 3) + " | CH_COV2 : Channel cover factor" + \ "\n" + cj.trailing_spaces(14, CH_WDR, 3) + " | CH_WDR : Channel width:depth ratio [m/m]" + \ "\n" + cj.trailing_spaces(14, ALPHA_BNK, 3) + " | ALPHA_BNK : Baseflow alpha factor for bank storage [days]" + \ "\n" + cj.trailing_spaces(14, ICANAL, 2) + " | ICANAL : Code for irrigation canal" + \ "\n" + cj.trailing_spaces(14, CH_ONCO, 2) + " | CH_ONCO : Organic nitrogen concentration in the channel [ppm]" + \ "\n" + cj.trailing_spaces(14, CH_OPCO, 2) + " | CH_OPCO : Organic phosphorus concentration in the channel [ppm]" + \ "\n" + cj.trailing_spaces(14, CH_SIDE, 2) + " | CH_SIDE : Change in horizontal distance per unit vertical distance" + \ "\n" + cj.trailing_spaces(14, CH_BNK_BD, 2) + " | CH_BNK_BD : Bulk density of channel bank sediment (g/cc)" + \ "\n" + cj.trailing_spaces(14, CH_BED_BD, 2) + " | CH_BED_BD : Bulk density of channel bed sediment (g/cc)" + \ "\n" + cj.trailing_spaces(14, CH_BNK_KD, 2) + " | CH_BNK_KD : Erodibility of channel bank sediment by jet test (cm3/N-s)" + \ "\n" + cj.trailing_spaces(14, CH_BED_KD, 2) + " | CH_BED_KD : Erodibility of channel bed sediment by jet test (cm3/N-s)" + \ "\n" + cj.trailing_spaces(14, CH_BNK_D50, 2) + " | CH_BNK_D50 : D50 Median particle size diameter of channel bank sediment (micro-m)" + \ "\n" + cj.trailing_spaces(14, CH_BED_D50, 2) + " | CH_BED_D50 : D50 Median particle size diameter of channel bed sediment (micro-m)" + \ "\n" + cj.trailing_spaces(14, CH_BNK_TC, 2) + " | CH_BNK_TC : Critical shear stress of channel bank (N/m2)" + \ "\n" + cj.trailing_spaces(14, CH_BED_TC, 2) + " | CH_BED_TC : Critical shear stress of channel bed (N/m2)" + \ "\n" + cj.trailing_spaces(6, CH_ERODMO1, 2) + cj.trailing_spaces(6, CH_ERODMO2, 2) + cj.trailing_spaces(6, CH_ERODMO3, 2) + cj.trailing_spaces(6, CH_ERODMO4, 2) + cj.trailing_spaces(6, CH_ERODMO5, 2) + cj.trailing_spaces(6, CH_ERODMO6, 2) + cj.trailing_spaces(6, CH_ERODMO7, 2) + cj.trailing_spaces(6, CH_ERODMO8, 2) + cj.trailing_spaces(6, CH_ERODMO9, 2) + cj.trailing_spaces(6, CH_ERODMO10, 2) + cj.trailing_spaces(6, CH_ERODMO11, 2) + cj.trailing_spaces(6, CH_ERODMO12, 2) + \ "\n" + cj.trailing_spaces(14, CH_EQN, 0) + " | CH_EQN : Sediment routing methods\n" fileName = cj.get_filename(int(SubBasin), int(0), "rte") cj.write_to(variables.DefaultSimDir + "TxtInOut\\" + fileName, rte_file) #print fileName
# 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) soil_string = "SOIL_ID,SNAM\n" for row in pj_db.columns: soil_string += "{code},{so_c}\n".format(code=row[0], so_c=row[1]) cj_function_lib.write_to( "{rt}Data/tables/{fn}.csv".format(rt=root, fn=soil_lookup), soil_string) rf_db.get_values("WGEN_user") wgen_string = ','.join(rf_db.col_names) + "\n" for row_ in rf_db.columns: str_row = [str(x) for x in row_] wgen_string += ','.join(str_row) + "\n" cj_function_lib.write_to(
second_confluence_string = "add 5" + cj.trailing_spaces(6, str(reach_dictionary[str(later_reach)].order_number - 2), 0) + cj.trailing_spaces(6, later_reach, 0) + cj.trailing_spaces(6, reach_dictionary[str(reach_dictionary[str(later_reach)].receives_from[0])].order_number, 0) + \ "\nadd 5" + cj.trailing_spaces(6, str(reach_dictionary[str(later_reach)].order_number - 1), 0) + cj.trailing_spaces(6, reach_dictionary[str(later_reach)].order_number - 2, 0) + cj.trailing_spaces(6, reach_dictionary[str(reach_dictionary[str(later_reach)].receives_from[1])].order_number, 0) + \ "\nroute 2" + cj.trailing_spaces(6, reach_dictionary[str(later_reach)].order_number, 0) + cj.trailing_spaces(6, later_reach, 0) + cj.trailing_spaces(6, str(reach_dictionary[str(later_reach)].order_number - 1), 0) + \ "\n " + cj.trailing_zeros(5, later_reach, 0) + "0000.rte" + cj.trailing_zeros(5, later_reach, 0) + "0000.swq" + "\n" + second_confluence_string # for the last confluence last_confluence_string = "add 5" + cj.trailing_spaces(6, reach_dictionary[str(end_reach)].order_number - 2, 0) + cj.trailing_spaces(6, end_reach, 0) + cj.trailing_spaces(6, reach_dictionary[str(reach_dictionary[str(end_reach)].receives_from[0])].order_number, 0) + \ "\nadd 5" + cj.trailing_spaces(6, reach_dictionary[str(end_reach)].order_number - 1, 0) + cj.trailing_spaces(6, reach_dictionary[str(end_reach)].order_number - 2, 0) + cj.trailing_spaces(6, reach_dictionary[str(reach_dictionary[str(end_reach)].receives_from[1])].order_number, 0) + \ "\nroute 2" + cj.trailing_spaces(6, reach_dictionary[str(end_reach)].order_number, 0) + cj.trailing_spaces(6, end_reach, 0) + cj.trailing_spaces(6, reach_dictionary[str(end_reach)].order_number - 1, 0) + \ "\n " + cj.trailing_zeros(5, end_reach, 0) + "0000.rte" + cj.trailing_zeros(5, end_reach, 0) + "0000.swq" # here we get the whole string and save to file fig_string = fig_string + first_confluence_string + second_confluence_string + last_confluence_string + fig_bottom fileName = "fig.fig" cj.write_to(variables.DefaultSimDir + "TxtInOut\\" + fileName, fig_string) # Here we also generate the ATM file, I do not know which fields in the BSN table contain these. now = datetime.now() DateAndTime = str(now.month) + "/" + str(now.day) + "/" + \ str(now.year) + " " + str(now.time()).split(".")[0] SWAT_Vers = "QSWAT Workflow v1.5.2" ATM = "Watershed atmospheric deposition file .atm file " + DateAndTime + " " + SWAT_Vers + \ """
# Get projection info for the project file proj4, is_proj = cj.get_proj4_from(current_root + "/" + project_name + "/Source/crop/" + namelist.Land_Use) if not is_proj: print("\t\t! Warning: makesure your land use map is projected.") xmin, ymax, xmax, ymin = cj.get_extents(current_root + "/" + project_name + "/Source/dem.tif") epsg_code, srs_id, prj_name = cj.get_auth(current_root + "/" + project_name + "/Source/dem.tif") soil_epsg_code, soil_srs_id, soil_prj_name = cj.get_auth( os.path.join(current_root, project_name, "Source", "crop", namelist.Land_Use)) cj.write_to("epsg_code.tmp~", "{0}".format(epsg_code)) projlist = proj4.split(" ") proj4_dic = {} for item in projlist: if (item == '+no_defs') or (item == ''): continue else: proj4_dic[item.split("=")[0]] = item.split("=")[1] ## Here we replace the text inside the project file project_file_list = cj.read_from(root + "/workflow_lib/template.qgs") project_file_string = "" for line in project_file_list: project_file_string += line
for i in range(1, len(stations_dict["1"].data_list)): line = "" current_day += 1 if current_day <= end_day: datecode = str(current_tear) + cj.trailing_zeros(3, current_day, 0) #print datecode else: current_day = 1 current_tear += 1 end_day = cj.get_days_in_year(current_tear) datecode = str(current_tear) + cj.trailing_zeros(3, current_day, 0) #print end_day for sta_nr in station_nr: rec = ( "000.0" if cj.trailing_zeros( 5, stations_dict[str(sta_nr)].data_list[i].split(",")[0], 1) == "-00.0" else cj.trailing_zeros( 5, stations_dict[str(sta_nr)].data_list[i].split(",")[0], 1) ) + ("000.0" if cj.trailing_zeros( 5, stations_dict[str(sta_nr)].data_list[i].split(",")[1], 1) == "-00.0" else cj.trailing_zeros( 5, stations_dict[str(sta_nr)].data_list[i].split(",")[1], 1)) line += rec line = datecode + line + "\n" tmp_string += line cj.write_to(variables.DefaultSimDir + "TxtInOut\\tmp1.tmp", tmp_string)
0.227 7.500 14.000 0.000 3.290 0.000 0.000 0.000 0.000 0.000 0.000 0.0 19 CWT5 Municipal wastewater w/lagoon and constructed wetland 2 0.227 1.700 9.500 0.000 1.980 0.000 0.000 0.000 0.000 0.000 0.000 0.0 20 BFL1 Waterloo biofilter (plastic media) 2 0.227 28.500 18.500 58.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.0 21 BFL2 Waterloo biofilter (plastic media) 2 0.227 16.000 5.000 0.000 10.200 5.700 0.000 0.000 0.000 0.000 0.000 190000.0 22 BFL3 Peat biofilter 2 0.227 4.500 6.500 2.500 1.000 20.000 0.000 0.000 0.000 0.000 0.450 945.0 23 TXF1 Recirulating textile filter 2 0.227 27.500 16.000 26.500 0.000 0.000 0.000 0.000 0.000 0.000 0.000 50790.0 24 TXF2 Foam or textile filter effluent 2 0.227 10.000 7.500 45.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 505.0 25 GFL1 Septic, recirculating gravel filter, UV disinfection 2 0.227 25.000 4.900 0.400 0.000 12.200 0.000 0.000 0.000 0.000 0.000 7.3 26 FSPT Texas A&M reference 3 0.227 250.000 700.000 40.000 25.000 0.000 0.000 25.000 12.000 10.000 2.000 10000000.0 """ fileName = "septwq.dat" cj.write_to(variables.DefaultSimDir + "TxtInOut\\" + fileName, septwq) #print fileName
wnd_string = "Input File wnd.wnd " + DateAndTime + " " + SWAT_Vers + "\n" current_tear = int(stations_dict["1"].data_list[0][0:4]) end_day = cj.get_days_in_year(current_tear) current_day = 0 for i in range(1, len(stations_dict["1"].data_list)): line = "" current_day += 1 if current_day <= end_day: datecode = str(current_tear) + cj.trailing_zeros(3, current_day, 0) #print datecode else: current_day = 1 current_tear += 1 end_day = cj.get_days_in_year(current_tear) datecode = str(current_tear) + cj.trailing_zeros(3, current_day, 0) #print end_day for sta_nr in station_nr: rec = cj.trailing_zeros(8, stations_dict[str(sta_nr)].data_list[i], 3) line += rec line = datecode + line + "\n" wnd_string += line cj.write_to(variables.DefaultSimDir + "TxtInOut\\wnd.wnd", wnd_string)
hmd_string = "Input File hmd.hmd " + DateAndTime + " " + SWAT_Vers + "\n" current_tear = int(stations_dict["1"].data_list[0][0:4]) end_day = cj.get_days_in_year(current_tear) current_day = 0 for i in range(1, len(stations_dict["1"].data_list)): line = "" current_day += 1 if current_day <= end_day: datecode = str(current_tear) + cj.trailing_zeros(3, current_day, 0) #print datecode else: current_day = 1 current_tear += 1 end_day = cj.get_days_in_year(current_tear) datecode = str(current_tear) + cj.trailing_zeros(3, current_day, 0) #print end_day for sta_nr in station_nr: rec = cj.trailing_zeros(8, stations_dict[str(sta_nr)].data_list[i], 3) line += rec line = datecode + line + "\n" hmd_string += line cj.write_to(variables.DefaultSimDir + "TxtInOut\\hmd.hmd", hmd_string)
SWAT_Vers = "QSWAT Workflow v1.5.2" slr_string = "Input File slr.slr " + DateAndTime + " " + SWAT_Vers + "\n" current_tear = int(stations_dict["1"].data_list[0][0:4]) end_day = cj.get_days_in_year(current_tear) current_day = 0 for i in range(1, len(stations_dict["1"].data_list)): line = "" current_day += 1 if current_day <= end_day: datecode = str(current_tear) + cj.trailing_zeros(3, current_day, 0) #print datecode else: current_day = 1 current_tear += 1 end_day = cj.get_days_in_year(current_tear) datecode = str(current_tear) + cj.trailing_zeros(3, current_day, 0) #print end_day for sta_nr in station_nr: rec = cj.trailing_zeros(8, stations_dict[str(sta_nr)].data_list[i], 3) line += rec line = datecode + line + "\n" slr_string += line cj.write_to(variables.DefaultSimDir + "TxtInOut\\slr.slr", slr_string)
import os, osr import sys # import init_file as variables from shutil import copyfile import cj_function_lib as cj import namelist # cwd = variables.path + "\\" cwd = sys.argv[1] + "/model/" path = os.path.dirname(cj.__file__) cj.write_to(path + "/root_path.py", "pth = '{0}'\n".format(cwd.replace("\\", "/"))) root = path.replace("workflow_lib", "") sys.path.append(root) import namelist ProjList = cj.list_files_from(cwd, "qgs") if len(ProjList)==0: print("Error: No project File Prepared") sys.exit() ProjName = namelist.Project_Name #Directories ProjDir = cwd + ProjName + "\\" DefaultSimDir = ProjDir + "Scenarios\\default\\" #Databases ProjMDB = ProjDir + ProjName + ".mdb"