def import_data(self): try: config = Project_config.get() weather_data_dir = utils.full_path(self.project_db_file, config.weather_data_dir) if not os.path.exists(weather_data_dir): sys.exit('Weather data directory {dir} does not exist.'.format(dir=weather_data_dir)) self.add_weather_files(weather_data_dir) if self.create_stations: self.create_weather_stations(20, 45) self.match_stations(65) else: self.match_files_to_stations(20, 45) except Project_config.DoesNotExist: sys.exit('Could not retrieve project configuration from database')
def __init__(self, project_db_file, delete_existing, create_stations, source_dir): self.__abort = False SetupProjectDatabase.init(project_db_file) config = Project_config.get() weather_data_dir = utils.full_path(project_db_file, config.weather_data_dir) if not os.path.exists(weather_data_dir): sys.exit('Weather data directory {dir} does not exist.'.format( dir=weather_data_dir)) self.output_dir = weather_data_dir self.project_db_file = project_db_file self.project_db = project_base.db self.source_dir = source_dir self.delete_existing = delete_existing self.create_stations = create_stations
def get(self, project_db): SetupProjectDatabase.init(project_db) try: m = Project_config.get() return { "has_weather": climate.Weather_sta_cli.select().count() > 0 and climate.Weather_wgn_cli.select().count() > 0, "input_files_dir": utils.full_path(project_db, m.input_files_dir), "input_files_last_written": utils.json_encode_datetime(m.input_files_last_written), "swat_last_run": utils.json_encode_datetime(m.swat_last_run) } except Project_config.DoesNotExist: abort(404, message="Could not retrieve project configuration data.")
def __init__(self, project_db_file, delete_existing, create_stations, import_method='database', file1=None, file2=None): self.__abort = False SetupProjectDatabase.init(project_db_file) self.project_db_file = project_db_file self.project_db = project_base.db self.create_stations = create_stations self.import_method = import_method self.file1 = file1 self.file2 = file2 try: config = Project_config.get() if self.import_method == 'database' and self.project_db_file != '' and self.project_db_file is not None: wgn_db = utils.full_path(self.project_db_file, config.wgn_db) if not os.path.exists(wgn_db): sys.exit( 'WGN path {dir} does not exist.'.format(dir=wgn_db)) if config.wgn_table_name is None: sys.exit( 'Weather generator table name not set in config table.' ) self.wgn_database = wgn_db self.wgn_table = config.wgn_table_name except Project_config.DoesNotExist: sys.exit('Could not retrieve project configuration from database') if delete_existing: self.delete_existing()
def __init__(self, project_db, editor_version, project_name=None, datasets_db=None, constant_ps=True, is_lte=False, project_description=None): self.__abort = False base_path = os.path.dirname(project_db) rel_project_db = os.path.relpath(project_db, base_path) if datasets_db is None: conn = lib.open_db(project_db) if not lib.exists_table(conn, 'project_config'): sys.exit( 'No datasets database provided and the project_config table in your project database does not exist. Please provide either a datasets database file or an existing project database.' ) SetupProjectDatabase.init(project_db) try: config = Project_config.get() datasets_db = utils.full_path(project_db, config.reference_db) if project_name is None: project_name = config.project_name except Project_config.DoesNotExist: sys.exit('Could not retrieve project configuration data.') rel_datasets_db = os.path.relpath(datasets_db, base_path) ver_check = SetupDatasetsDatabase.check_version( datasets_db, editor_version) if ver_check is not None: sys.exit(ver_check) # Backup original db before beginning do_gis = False if os.path.exists(project_db): do_gis = True try: self.emit_progress(2, 'Backing up GIS database...') filename, file_extension = os.path.splitext(rel_project_db) bak_filename = filename + '_bak_' + time.strftime( '%Y%m%d-%H%M%S') + file_extension bak_dir = os.path.join(base_path, 'DatabaseBackups') if not os.path.exists(bak_dir): os.makedirs(bak_dir) backup_db_file = os.path.join(bak_dir, bak_filename) copyfile(project_db, backup_db_file) except IOError as err: sys.exit(err) try: SetupProjectDatabase.init(project_db, datasets_db) self.emit_progress(10, 'Creating database tables...') SetupProjectDatabase.create_tables() self.emit_progress(50, 'Copying data from SWAT+ datasets database...') description = project_description if project_description is not None else project_name SetupProjectDatabase.initialize_data( description, is_lte, overwrite_plants=OVERWRITE_PLANTS) config = Project_config.get_or_create_default( editor_version=editor_version, project_name=project_name, project_db=rel_project_db, reference_db=rel_datasets_db, project_directory='', is_lte=is_lte) conn = lib.open_db(project_db) plant_cols = lib.get_column_names(conn, 'plants_plt') plant_col_names = [v['name'] for v in plant_cols] if 'days_mat' not in plant_col_names: migrator = SqliteMigrator(SqliteDatabase(project_db)) migrate( migrator.rename_column('plants_plt', 'plnt_hu', 'days_mat')) for p in project_plants: dp = dataset_plants.get_or_none( dataset_plants.name == p.name) if dp is not None: p.days_mat = dp.days_mat else: p.days_mat = 0 p.save() except Exception as ex: if backup_db_file is not None: self.emit_progress(50, "Error occurred. Rolling back database...") SetupProjectDatabase.rollback(project_db, backup_db_file) self.emit_progress(100, "Error occurred.") sys.exit(str(ex)) if do_gis: api = GisImport(project_db, True, constant_ps, backup_db_file) api.insert_default()
def __init__(self, project_db, editor_version, settings_file=None, project_name=None, datasets_db=None, constant_ps=True, is_lte=False): self.__abort = False base_path = os.path.dirname(project_db) rel_project_db = os.path.relpath(project_db, base_path) if datasets_db is None: conn = lib.open_db(project_db) if not lib.exists_table(conn, 'project_config'): sys.exit( 'No datasets database provided and the project_config table in your project database does not exist. Please provide either a datasets database file or an existing project database.' ) SetupProjectDatabase.init(project_db) try: config = Project_config.get() datasets_db = utils.full_path(project_db, config.reference_db) project_name = config.project_name except Project_config.DoesNotExist: sys.exit('Could not retrieve project configuration data.') rel_datasets_db = os.path.relpath(datasets_db, base_path) # Run updates if needed SetupProjectDatabase.init(project_db, datasets_db) config = Project_config.get() if config.editor_version in update_project.available_to_update: update_project.UpdateProject(project_db, editor_version, update_project_values=True) # Backup original db before beginning try: self.emit_progress(2, 'Backing up project database...') filename, file_extension = os.path.splitext(rel_project_db) bak_filename = filename + '_bak_' + time.strftime( '%Y%m%d-%H%M%S') + file_extension bak_dir = os.path.join(base_path, 'DatabaseBackups') if not os.path.exists(bak_dir): os.makedirs(bak_dir) backup_db_file = os.path.join(bak_dir, bak_filename) copyfile(project_db, backup_db_file) except IOError as err: sys.exit(err) self.emit_progress(5, 'Updating project settings...') config = Project_config.get( ) # Get again due to modification when updating config.imported_gis = False config.is_lte = is_lte config.save() api = GisImport(project_db, True, constant_ps, backup_db_file) api.insert_default() if settings_file is not None: settings_data = { 'swatplus-project': { 'version': editor_version, 'name': project_name, 'databases': { 'project': rel_project_db, 'datasets': rel_datasets_db }, 'model': 'SWAT+' if not is_lte else 'SWAT+ lte' } } with open(settings_file, 'w') as file: json.dump(settings_data, file, indent='\t')
def __init__(self, project_db, editor_version, swat_exe, weather_dir, weather_save_dir='', weather_import_format='plus', wgn_import_method='database', wgn_db='C:/SWAT/SWATPlus/Databases/swatplus_wgn.sqlite', wgn_table='wgn_cfsr_world', wgn_csv_sta_file=None, wgn_csv_mon_file=None, year_start=None, day_start=None, year_end=None, day_end=None, input_files_dir=None, swat_version=None): # Setup project databases and import GIS data SetupProject(project_db, editor_version, project_db.replace('.sqlite', '.json', 1)) rel_input_files = 'Scenarios/Default/TxtInOut' if input_files_dir is None else utils.rel_path( project_db, input_files_dir) input_files_path = utils.full_path(project_db, rel_input_files) if weather_import_format != 'plus' and weather_save_dir == '': weather_save_dir = rel_input_files # Set project config table arguments used by APIs m = Project_config.get() m.wgn_db = wgn_db m.wgn_table_name = wgn_table m.weather_data_dir = utils.rel_path( project_db, weather_dir ) if weather_import_format == 'plus' else utils.rel_path( project_db, weather_save_dir) m.input_files_dir = rel_input_files result = m.save() # Import WGN wgn_api = WgnImport(project_db, True, False, wgn_import_method, wgn_csv_sta_file, wgn_csv_mon_file) wgn_api.import_data() # Import weather files if weather_import_format == 'plus': weather_api = WeatherImport(project_db, True, True) weather_api.import_data() else: weather_api = Swat2012WeatherImport(project_db, True, True, weather_dir) weather_api.import_data() # Set time_sim if parameters given if year_start is not None: Time_sim.update_and_exec(day_start, year_start, day_end, year_end, 0) # Write input files write_api = WriteFiles(project_db, swat_version) write_api.write() # Run the model cwd = os.getcwd() os.chdir(input_files_path) run_result = os.system(swat_exe) print(run_result) # Import output files to db if successful run if run_result == 0: os.chdir(cwd) output_db_file = os.path.join(input_files_path, '../', 'Results', 'swatplus_output.sqlite') output_api = ReadOutput(input_files_path, output_db_file) output_api.read() m = Project_config.get() m.swat_last_run = datetime.now() m.output_last_imported = datetime.now() result = m.save()