Skip to content

Commit

Permalink
Added compatibility with linux (Ubuntu)
Browse files Browse the repository at this point in the history
  • Loading branch information
celray committed Apr 17, 2020
1 parent fcce4f8 commit 80b8624
Show file tree
Hide file tree
Showing 30 changed files with 213 additions and 134 deletions.
Binary file added editor_api/swat_exe/swatplusrev59-static.exe
Binary file not shown.
2 changes: 1 addition & 1 deletion editor_api/weather2012_to_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sys import argv, stdout, path
from shutil import copyfile

path.insert(0, "C:/SWAT/SWATPlus/Workflow/packages")
path.append(os.path.join(os.environ["swatplus_wf_dir"], "packages"))

from helper_functions import read_from, copy_file, write_to, show_progress, list_files

Expand Down
19 changes: 15 additions & 4 deletions main_stages/run_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import numpy
import time
import subprocess
import platform

from itertools import product

Expand All @@ -28,6 +29,7 @@
import pandas
import pystran as py
import namelist
from logger import log


# functions
Expand Down Expand Up @@ -114,9 +116,13 @@ def run_parameter_set(parameter_set_list, core_number, chg_typ_dict, header, cal
working_dir=working_dir, core=core_number))

if not os.path.isfile("rev59.3_64rel.exe"):
shutil.copyfile(swat_exe, "rev59.3_64rel.exe")
shutil.copyfile(executable_path, "rev59.3_64rel.exe")
print("\t> running SWAT+ in process {0}".format(core_number))
os.system("rev59.3_64rel.exe")
if platform.system() == "Linux":
os.system("chmod 777 ./rev59.3_64rel.exe")
os.system("./rev59.3_64rel.exe")
else:
os.system("rev59.3_64rel.exe")
# subprocess.Popen('rev59.3_64rel.exe', stdout=subprocess.PIPE)

# extract flow for specified unit at specified timestep
Expand Down Expand Up @@ -192,7 +198,12 @@ def run_parameter_set(parameter_set_list, core_number, chg_typ_dict, header, cal
start_time = time.time()

# set variables
swat_exe = "C:/SWAT/SWATPlus/Workflow/editor_api/swat_exe/rev59.3_64rel.exe"
variables = {
"swatplus_wf_dir": os.environ["swatplus_wf_dir"],
"swatplus_exe": "rev59.3_64rel.exe" if platform.system() == "Windows" else "swatplusrev59-static.exe"
}

executable_path = "{swatplus_wf_dir}editor_api/swat_exe/{swatplus_exe}".format(**variables)
base = "{0}/{1}/Scenarios/Default".format(
sys.argv[1], namelist.Project_Name)
working_dir = "{base_dir}/working".format(base_dir=base)
Expand Down Expand Up @@ -342,6 +353,6 @@ def run_parameter_set(parameter_set_list, core_number, chg_typ_dict, header, cal
write_to("{base}/TxtInOut/calibration.cal".format(base=base),
new_calibration_cal)
os.chdir("{base}/TxtInOut/".format(base=base))
os.system(swat_exe)
os.system("{0}rev59.3_64rel.exe".format("./" if platform.system() == "Linux" else ""))
log.info("finished running calibration\n", keep_log)
sys.exit(0)
49 changes: 32 additions & 17 deletions main_stages/run_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import sys
import shutil
import subprocess
import platform

# adding to path before importing custom modules
sys.path.insert(0, os.path.join(os.environ["swatplus_wf_dir"], "packages"))
Expand Down Expand Up @@ -66,11 +67,23 @@ def __init__(self, base_directory, model_name):
**self.variables)
self.wgn_db = "{api_dir}/swatplus_wgn.sqlite".format(
api_dir=self.api_dir)

if platform.system() == "Windows":
python_exe = "python"
else:
python_exe = "python3"


self.variables = {
"models_dir": self.model_dir,
"scripts_dir": self.scripts_dir,
"api": self.api,
"weather_format": "observed",
"txt_in_out_dir": self.txt_in_out_dir,
"swat_exe_release": "rev59.3_64rel.exe" if platform.system() == "Windows" else "./swatplusrev59-static.exe",
"swat_exe_debug": "rev59.3_64debug.exe" if platform.system() == "Windows" else "./swatplusrev59-static.exe",
"python_exe": python_exe,
"import_typ_wgn": "wgn",
"api_dir": self.api_dir,
"p_db": self.prj_database,
"r_db": self.ref_database,
Expand Down Expand Up @@ -153,9 +166,9 @@ def initialise_databases(self):
# import GIS
os.chdir(self.api_dir)
os.system(
'python {api} create_database --db_type=project --db_file="{p_db}" --db_file2="{r_db}"'.format(**self.variables))
'{python_exe} {api} create_database --db_type=project --db_file="{p_db}" --db_file2="{r_db}"'.format(**self.variables))
os.system(
'python {api} import_gis --delete_existing=y --project_db_file="{p_db}"'.format(**self.variables))
'{python_exe} {api} import_gis --delete_existing=y --project_db_file="{p_db}"'.format(**self.variables))

def setup_project(self):
# specify project config info
Expand Down Expand Up @@ -232,12 +245,9 @@ def set_printing_weather(self, Start_Year, End_Year):

# add weather
os.chdir(self.api_dir)
os.system('python {api_d}/weather2012_to_weather.py "{ws}" "{wdd}" {wf}'.format(
ws=self.weather_source, wdd=self.weather_data_dir, wf="observed", api_d=self.api_dir))
os.system('python {api} import_weather --delete_existing=y --create_stations=n --import_type={imp_typ} --import_method=database --project_db_file="{p_db}"'.format(
api=self.api, p_db=self.prj_database, imp_typ="wgn"))
os.system('python {api} import_weather --delete_existing=y --create_stations=y --import_type={imp_typ} --project_db_file="{p_db}"'.format(
api=self.api, p_db=self.prj_database, imp_typ="observed"))
os.system('{python_exe} {api_dir}/weather2012_to_weather.py "{w_src}" "{w_dat}" {weather_format}'.format(**self.variables))
os.system('{python_exe} {api} import_weather --delete_existing=y --create_stations=n --import_type={import_typ_wgn} --import_method=database --project_db_file="{p_db}"'.format(**self.variables))
os.system('{python_exe} {api} import_weather --delete_existing=y --create_stations=y --import_type={weather_format} --project_db_file="{p_db}"'.format(**self.variables))

# set simulation times
time_sim_data = ["1", "0", str(Start_Year), "0", str(End_Year), "0"]
Expand All @@ -251,8 +261,7 @@ def write_files(self):

# write files
os.chdir(self.api_dir)
os.system('python {0} write_files --output_files_dir="{2}" --project_db_file="{1}"'.format(
self.api, self.prj_database, self.txt_in_out_dir))
os.system('{python_exe} {api} write_files --output_files_dir="{txt_in_out_dir}" --project_db_file="{p_db}"'.format(**self.variables))

# setting weather dir in cio
# cio_content = read_from(
Expand Down Expand Up @@ -282,15 +291,18 @@ def write_files(self):
def run(self, exe_type):
os.chdir(self.txt_in_out_dir)

copy_file("{base}/editor_api/swat_exe/rev59.3_64debug.exe".format(base=self.scripts_dir),
"{txt_in_out_dir}rev59.3_64debug.exe".format(txt_in_out_dir=self.txt_in_out_dir))
copy_file("{base}/editor_api/swat_exe/rev59.3_64rel.exe".format(base=self.scripts_dir),
"{txt_in_out_dir}/rev59.3_64rel.exe".format(txt_in_out_dir=self.txt_in_out_dir))
copy_file("{scripts_dir}/editor_api/swat_exe/{swat_exe_debug}".format(**self.variables),
"{txt_in_out_dir}/{swat_exe_debug}".format(**self.variables))
copy_file("{scripts_dir}/editor_api/swat_exe/{swat_exe_release}".format(**self.variables),
"{txt_in_out_dir}/{swat_exe_release}".format(**self.variables))

if exe_type == 1:
print("\n >> running SWAT+")
# os.system("rev59.3_64rel.exe")
sub_process = subprocess.Popen("rev59.3_64rel.exe", close_fds=True, shell=True,
if platform.system() == "Linux":
os.system("chmod 777 {swat_exe_release}".format(**self.variables))

# os.system("{swat_exe_release}")
sub_process = subprocess.Popen("{swat_exe_release}".format(**self.variables), close_fds=True, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
line = ""
while sub_process.poll() is None:
Expand Down Expand Up @@ -321,7 +333,10 @@ def run(self, exe_type):
line = ""

elif exe_type == 2:
os.system("rev59.3_64debug.exe")
if platform.system() == "Linux":
os.system("chmod 777 {swat_exe_release}".format(**self.variables))

os.system("{swat_exe_debug}".format(**self.variables))

def model_options(self):
self.db.connect()
Expand Down
2 changes: 2 additions & 0 deletions main_stages/run_make_figures.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def __init__(self, wb_parts):

if namelist.Model_2_namelist:
sys.exit(0)
if not namelist.Make_Figures:
sys.exit(0)

base = sys.argv[1].replace("\\\\", "/")
base = base.replace("\\", "/")
Expand Down
45 changes: 29 additions & 16 deletions main_stages/run_qswat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
import os.path
import shutil
import sys

import platform
import warnings

if platform.system() == "Linux":
import pyximport # importing cython needs this on linux
pyximport.install()

# skip deprecation warnings when importing PyQt5
with warnings.catch_warnings():
warnings.simplefilter("ignore")
Expand All @@ -25,15 +29,23 @@
qgs = QgsApplication([], True)
qgs.initQgis()

# Prepare processing framework
sys.path.append('{QGIS_Dir}/apps/qgis-ltr/python/plugins'.format(
QGIS_Dir = os.environ['QGIS_Dir'])) # Folder where Processing is located
from processing.core.Processing import Processing
Processing.initialize()
# Prepare processing framework
if platform.system() == "Windows":
sys.path.append('{QGIS_Dir}/apps/qgis-ltr/python/plugins'.format(
QGIS_Dir = os.environ['QGIS_Dir'])) # Folder where Processing is located
else:
sys.path.append('/usr/share/qgis/python/plugins') # Folder where Processing is located

# skip syntax warnings on linux
with warnings.catch_warnings():
warnings.simplefilter("ignore")
from processing.core.Processing import Processing
Processing.initialize()

import processing
import processing

sys.path.append(os.path.join(os.environ["swatplus_wf_dir"], "packages"))
sys.path.append(os.path.join(os.environ["swatplus_wf_dir"], "qswatplus"))
sys.path.append(os.path.join(os.environ["swatplus_wf_dir"]))
sys.path.insert(0, sys.argv[1])

Expand All @@ -44,12 +56,13 @@
import namelist
from logger import log

os.chdir(os.path.join(os.environ["swatplus_wf_dir"], "qswatplus"))

from qswatplus.QSWATPlus import QSWATPlus
from qswatplus.delineation import Delineation
from qswatplus.hrus import HRUs
from qswatplus.QSWATUtils import QSWATUtils
from qswatplus.parameters import Parameters
from QSWATPlus import QSWATPlus
from delineation import Delineation
from hrus import HRUs
from QSWATUtils import QSWATUtils
from parameters import Parameters
from glob import glob

atexit.register(QgsApplication.exitQgis)
Expand Down Expand Up @@ -102,7 +115,7 @@ def layers(self):
# clean up before new files
log.info("cleaning up files from 'Watershed\Shapes'", keep_log)
Watershed_shapes = list_files(QSWATUtils.join(
projDir, r'Watershed\Shapes'), "shp")
projDir, r'Watershed/Shapes'), "shp")
delete_shapes = []

for Watershed_shape in Watershed_shapes:
Expand Down Expand Up @@ -132,7 +145,7 @@ def layers(self):
print("\n >> setting up model hrus")

log.info("cleaning up files from 'Watershed\\Text'", keep_log)
shutil.rmtree(QSWATUtils.join(projDir, r'Watershed\Text'), ignore_errors=True)
shutil.rmtree(QSWATUtils.join(projDir, r'Watershed/Text'), ignore_errors=True)

projName = os.path.split(projDir)[1]
projFile = "{dir}/{nm}.qgs".format(dir=projDir, nm=projName)
Expand Down Expand Up @@ -202,13 +215,13 @@ def layers(self):
sys.exit(1)


if not os.path.exists(QSWATUtils.join(projDir, r'Watershed\Shapes\rivs1.shp')):
if not os.path.exists(QSWATUtils.join(projDir, r'Watershed/Shapes/rivs1.shp')):
log.error("error creating HRUs", keep_log)
QSWATUtils.error('\t ! Streams shapefile not created', True)
sys.exit(1)


if not os.path.exists(QSWATUtils.join(projDir, r'Watershed\Shapes\subs1.shp')):
if not os.path.exists(QSWATUtils.join(projDir, r'Watershed/Shapes/subs1.shp')):
log.error("error creating HRUs", keep_log)
QSWATUtils.error('\t ! Subbasins shapefile not created', True)
sys.exit(1)
Expand Down
6 changes: 3 additions & 3 deletions qswatplus/DBUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
import traceback

try:
from .QSWATUtils import QSWATUtils, FileTypes
from .dataInC import BasinData, CellData, LSUData, WaterBody # @UnresolvedImport
from .parameters import Parameters
from QSWATUtils import QSWATUtils, FileTypes
from dataInC import BasinData, CellData, LSUData, WaterBody # @UnresolvedImport
from parameters import Parameters
except:
# used by convertFromArc
from QSWATUtils import QSWATUtils, FileTypes # @UnresolvedImport
Expand Down
10 changes: 5 additions & 5 deletions qswatplus/QSWATBatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
import shutil

import atexit
from .QSWATPlus import QSWATPlus
from .delineation import Delineation
from .hrus import HRUs
from .QSWATUtils import QSWATUtils
from .parameters import Parameters
from QSWATPlus import QSWATPlus
from delineation import Delineation
from hrus import HRUs
from QSWATUtils import QSWATUtils
from parameters import Parameters

osGeo4wRoot = os.getenv('OSGEO4W_ROOT')
QgsApplication.setPrefixPath(osGeo4wRoot + r'\apps\qgis', True)
Expand Down
24 changes: 12 additions & 12 deletions qswatplus/QSWATPlus.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,36 @@

# Initialize Qt resources from file resources_rc.py
try:
from .resources_rc import * # @UnusedWildImport
from resources_rc import * # @UnusedWildImport
except:
from resources_rc import * # for convertFromArc @UnresolvedImport
# Import the code for the dialog
# allow this to fail so no exception when loaded in wrong architecture (32 or 64 bit)
# QSWATUtils should have no further dependencies, especially in Cython modules
try:
from .QSWATUtils import QSWATUtils, FileTypes # @UnresolvedImport
from QSWATUtils import QSWATUtils, FileTypes # @UnresolvedImport
except:
# for convertFromArc
from QSWATUtils import QSWATUtils, FileTypes # @UnresolvedImport
try:
txt = 'QSwatDialog'
from .qswatdialog import QSwatDialog
from qswatdialog import QSwatDialog
txt = 'HRUs'
from .hrus import HRUs
from hrus import HRUs
txt = 'QSWATTopology'
from .QSWATTopology import QSWATTopology
from QSWATTopology import QSWATTopology
txt = 'GlobalVars'
from .globals import GlobalVars
from globals import GlobalVars
txt = 'Delineation'
from .delineation import Delineation
from delineation import Delineation
txt = 'Parameters'
from .parameters import Parameters
from parameters import Parameters
txt = 'Visualise'
from .visualise import Visualise
from visualise import Visualise
txt = 'AboutQSWAT'
from .about import AboutQSWAT
from about import AboutQSWAT
txt = 'ExportTable'
from .exporttable import ExportTable
from exporttable import ExportTable
except Exception:
QSWATUtils.loginfo('QSWAT+ failed to import {0}: {1}'.format(txt, traceback.format_exc()))

Expand All @@ -81,7 +81,7 @@ def __init__(self, iface):
## flag to show if init ran successfully
self.loadFailed = False
try:
from . import dataInC # @UnusedImport
import dataInC # @UnusedImport
except Exception:
QSWATUtils.loginfo('Failed to load Cython module: wrong architecture?: {0}'.format(traceback.format_exc()))
self.loadFailed = True
Expand Down
10 changes: 5 additions & 5 deletions qswatplus/QSWATTopology.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
import traceback

try:
from .QSWATUtils import QSWATUtils, FileTypes, ListFuns
from .DBUtils import DBUtils
from .parameters import Parameters
from .raster import Raster
from .dataInC import ReachData, MergedChannelData, LakeData # @UnresolvedImport
from QSWATUtils import QSWATUtils, FileTypes, ListFuns
from DBUtils import DBUtils
from parameters import Parameters
from raster import Raster
from dataInC import ReachData, MergedChannelData, LakeData # @UnresolvedImport
except:
# used by convertFromArc
from QSWATUtils import QSWATUtils, FileTypes, ListFuns
Expand Down
4 changes: 2 additions & 2 deletions qswatplus/TauDEMUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import subprocess
import webbrowser

from .QSWATUtils import QSWATUtils
from .parameters import Parameters
from QSWATUtils import QSWATUtils
from parameters import Parameters

class TauDEMUtils:

Expand Down
2 changes: 1 addition & 1 deletion qswatplus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@

def classFactory(iface):
# load QSWATPlus class from file QSWATPlus
from .QSWATPlus import QSWATPlus
from QSWATPlus import QSWATPlus
return QSWATPlus(iface)
Loading

0 comments on commit 80b8624

Please sign in to comment.