コード例 #1
0
ファイル: import_weather.py プロジェクト: waternk/SWATPlus-AW
	def add_weather_files(self, dir):
		if self.__abort: return
		hmd_start, hmd_end = self.add_weather_files_type(os.path.join(dir, HMD_CLI), "hmd", 0)
		if self.__abort: return
		pcp_start, pcp_end = self.add_weather_files_type(os.path.join(dir, PCP_CLI), "pcp", 5)
		if self.__abort: return
		slr_start, slr_end = self.add_weather_files_type(os.path.join(dir, SLR_CLI), "slr", 10)
		if self.__abort: return
		tmp_start, tmp_end = self.add_weather_files_type(os.path.join(dir, TMP_CLI), "tmp", 15)
		if self.__abort: return
		wnd_start, wnd_end = self.add_weather_files_type(os.path.join(dir, WND_CLI), "wnd", 20)

		starts = [hmd_start, pcp_start, slr_start, tmp_start, wnd_start]
		ends = [hmd_end, pcp_end, slr_end, tmp_end, wnd_end]
		starts = [v for v in starts if v is not None]
		ends = [v for v in ends if v is not None]
		if len(starts) > 0:
			ustarts = list(dict.fromkeys(starts))
			uends = list(dict.fromkeys(ends))
			if len(ustarts) > 1 or len(uends) > 1:
				raise ValueError("Dates in weather files do not match. Make sure all weather files have the same starting and ending dates.")
			
			st = ustarts[0].timetuple()
			start_day = st.tm_yday if st.tm_yday > 1 else 0
			start_year = st.tm_year

			et = uends[0].timetuple()
			end_day = 0 if et.tm_mon == 12 and et.tm_mday == 31 else et.tm_yday
			end_year = et.tm_year

			Time_sim.update(day_start=start_day, yrc_start=start_year, day_end=end_day, yrc_end=end_year).execute()

		if self.__abort: return
		"""warnings = []
コード例 #2
0
    def put(self, project_db):
        parser = reqparse.RequestParser()
        parser.add_argument('id', type=int, required=False, location='json')
        parser.add_argument('day_start',
                            type=int,
                            required=True,
                            location='json')
        parser.add_argument('yrc_start',
                            type=int,
                            required=True,
                            location='json')
        parser.add_argument('day_end',
                            type=int,
                            required=True,
                            location='json')
        parser.add_argument('yrc_end',
                            type=int,
                            required=True,
                            location='json')
        parser.add_argument('step', type=int, required=False, location='json')
        args = parser.parse_args(strict=True)

        SetupProjectDatabase.init(project_db)
        result = Time_sim.update_and_exec(args['day_start'], args['yrc_start'],
                                          args['day_end'], args['yrc_end'],
                                          args['step'])

        if result == 1:
            return 200

        abort(400, message='Unable to update time_sim table.')
コード例 #3
0
    def get(self, project_db):
        SetupProjectDatabase.init(project_db)
        try:
            c = Project_config.get()
            config = get_model_to_dict_dates(c, project_db)

            t = Time_sim.get_or_create_default()
            time = model_to_dict(t)

            m = Print_prt.get()
            prt = model_to_dict(m, recurse=False)

            o = Print_prt_object.select()
            objects = [model_to_dict(v, recurse=False) for v in o]

            prt = {'prt': prt, 'objects': objects}

            return {
                'config':
                config,
                'time':
                time,
                'print':
                prt,
                'imported_weather':
                climate.Weather_sta_cli.select().count() > 0
                and climate.Weather_wgn_cli.select().count() > 0,
                'has_observed_weather':
                climate.Weather_sta_cli.observed_count() > 0
            }
        except Project_config.DoesNotExist:
            abort(404,
                  message="Could not retrieve project configuration data.")
        except Print_prt.DoesNotExist:
            abort(404, message="Could not retrieve print_prt data.")
コード例 #4
0
	def put(self, project_db):
		SetupProjectDatabase.init(project_db)
		args = get_recallrec_args(True)

		try:
			param_dict = {}

			if args['rec_typ'] is not None:
				param_dict['rec_typ'] = args['rec_typ']

			con_table = Recall_con
			con_prop_field = Recall_con.rec_id
			prop_table = Recall_rec

			con_param_dict = {}
			if args['wst_name'] is not None:
				con_param_dict['wst_id'] = self.get_id_from_name(Weather_sta_cli, args['wst_name'])
			if args['elev'] is not None:
				con_param_dict['elev'] = args['elev']

			con_result = 1
			if (len(con_param_dict) > 0):
				con_query = con_table.update(con_param_dict).where(con_table.id.in_(args['selected_ids']))
				con_result = con_query.execute()

			if con_result > 0:
				result = 1
				if (len(param_dict) > 0):
					prop_ids = con_table.select(con_prop_field).where(con_table.id.in_(args['selected_ids']))
					query = prop_table.select().where(prop_table.id.in_(prop_ids))
					sim = Time_sim.get_or_create_default()
					for m in query:
						update_recall_rec(project_base.db, m, m.id, sim, args['rec_typ'])

					query2 = prop_table.update(param_dict).where(prop_table.id.in_(prop_ids))
					result = query2.execute()

				if result > 0:
					return 200

			abort(400, message='Unable to update routing unit properties.')
		except Exception as ex:
			abort(400, message="Unexpected error {ex}".format(ex=ex))
コード例 #5
0
	def put(self, project_db, id):
		#return self.base_put(project_db, id, Recall_rec, 'Recall')
		table = Recall_rec
		item_description = 'Recall'
		try:
			SetupProjectDatabase.init(project_db)
			args = self.get_args_reflect(table, project_db)

			sim = Time_sim.get_or_create_default()
			m = table.get(table.id==id)
			update_recall_rec(project_base.db, m, id, sim, args['rec_typ'])
			
			result = self.save_args(table, args, id=id)

			if result > 0:
				return 201

			abort(400, message='Unable to update {item} {id}.'.format(item=item_description.lower(), id=id))
		except IntegrityError as e:
			abort(400, message='{item} name must be unique. '.format(item=item_description) + str(e))
		except table.DoesNotExist:
			abort(404, message='{item} {id} does not exist'.format(item=item_description, id=id))
		except Exception as ex:
			abort(400, message="Unexpected error {ex}".format(ex=ex))
コード例 #6
0
    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()
コード例 #7
0
 def get(self, project_db):
     SetupProjectDatabase.init(project_db)
     m = Time_sim.get_or_create_default()
     return model_to_dict(m)
コード例 #8
0
    def put(self, project_db):
        parser = reqparse.RequestParser()
        parser.add_argument('input_files_dir',
                            type=str,
                            required=False,
                            location='json')
        parser.add_argument('time', type=dict, required=False, location='json')
        parser.add_argument('print',
                            type=dict,
                            required=False,
                            location='json')
        parser.add_argument('print_objects',
                            type=list,
                            required=False,
                            location='json')
        args = parser.parse_args(strict=False)

        SetupProjectDatabase.init(project_db)
        try:
            m = Project_config.get()
            m.input_files_dir = utils.rel_path(project_db,
                                               args['input_files_dir'])
            result = m.save()

            time = args['time']
            result = Time_sim.update_and_exec(time['day_start'],
                                              time['yrc_start'],
                                              time['day_end'], time['yrc_end'],
                                              time['step'])

            prt = args['print']
            q = Print_prt.update(nyskip=prt['nyskip'],
                                 day_start=prt['day_start'],
                                 day_end=prt['day_end'],
                                 yrc_start=prt['yrc_start'],
                                 yrc_end=prt['yrc_end'],
                                 interval=prt['interval'],
                                 csvout=prt['csvout'],
                                 dbout=prt['dbout'],
                                 cdfout=prt['cdfout'],
                                 soilout=prt['soilout'],
                                 mgtout=prt['mgtout'],
                                 hydcon=prt['hydcon'],
                                 fdcout=prt['fdcout'])
            result = q.execute()

            prtObj = args['print_objects']
            if prtObj is not None:
                for o in prtObj:
                    Print_prt_object.update(
                        daily=o['daily'],
                        monthly=o['monthly'],
                        yearly=o['yearly'],
                        avann=o['avann']).where(
                            Print_prt_object.id == o['id']).execute()

            if result > 0:
                return 200

            abort(400, message="Unable to update project configuration table.")
        except Project_config.DoesNotExist:
            abort(404,
                  message="Could not retrieve project configuration data.")
コード例 #9
0
    def put(self, project_db, id):
        #return self.base_put(project_db, id, Recall_rec, 'Recall')
        table = Recall_rec
        item_description = 'Recall'
        try:
            SetupProjectDatabase.init(project_db)
            args = self.get_args_reflect(table, project_db)

            m = table.get(table.id == id)
            if m.rec_typ != args['rec_typ']:
                Recall_dat.delete().where(
                    Recall_dat.recall_rec_id == id).execute()

                sim = Time_sim.get_or_create_default()
                years = 0
                months = 1
                days = 1
                start_day = 1
                start_month = 1
                insert_daily = False

                if args['rec_typ'] != 4:
                    years = sim.yrc_end - sim.yrc_start
                    if args['rec_typ'] != 3:
                        start_date = datetime.datetime(sim.yrc_start, 1,
                                                       1) + datetime.timedelta(
                                                           sim.day_start)
                        end_date = datetime.datetime(sim.yrc_end, 1,
                                                     1) + datetime.timedelta(
                                                         sim.day_end)
                        if sim.day_end == 0:
                            end_date = datetime.datetime(sim.yrc_end, 12, 31)

                        start_month = start_date.month
                        months = end_date.month

                        if args['rec_typ'] != 2:
                            insert_daily = True

                rec_data = []
                if not insert_daily:
                    for x in range(years + 1):
                        for y in range(start_month, months + 1):
                            t_step = x + 1 if months == 1 else y
                            data = {
                                'recall_rec_id':
                                id,
                                'yr':
                                x +
                                sim.yrc_start if args['rec_typ'] != 4 else 0,
                                't_step':
                                t_step if args['rec_typ'] != 4 else 0,
                                'flo':
                                0,
                                'sed':
                                0,
                                'ptl_n':
                                0,
                                'ptl_p':
                                0,
                                'no3_n':
                                0,
                                'sol_p':
                                0,
                                'chla':
                                0,
                                'nh3_n':
                                0,
                                'no2_n':
                                0,
                                'cbn_bod':
                                0,
                                'oxy':
                                0,
                                'sand':
                                0,
                                'silt':
                                0,
                                'clay':
                                0,
                                'sm_agg':
                                0,
                                'lg_agg':
                                0,
                                'gravel':
                                0,
                                'tmp':
                                0
                            }
                            rec_data.append(data)
                else:
                    current_date = start_date
                    while current_date <= end_date:
                        data = {
                            'recall_rec_id': id,
                            'yr': current_date.year,
                            't_step': current_date.timetuple().tm_yday,
                            'flo': 0,
                            'sed': 0,
                            'ptl_n': 0,
                            'ptl_p': 0,
                            'no3_n': 0,
                            'sol_p': 0,
                            'chla': 0,
                            'nh3_n': 0,
                            'no2_n': 0,
                            'cbn_bod': 0,
                            'oxy': 0,
                            'sand': 0,
                            'silt': 0,
                            'clay': 0,
                            'sm_agg': 0,
                            'lg_agg': 0,
                            'gravel': 0,
                            'tmp': 0
                        }
                        rec_data.append(data)
                        current_date = current_date + datetime.timedelta(1)

                db_lib.bulk_insert(project_base.db, Recall_dat, rec_data)

            result = self.save_args(table, args, id=id)

            if result > 0:
                return 201

            abort(400,
                  message='Unable to update {item} {id}.'.format(
                      item=item_description.lower(), id=id))
        except IntegrityError as e:
            abort(400,
                  message='{item} name must be unique. '.format(
                      item=item_description) + str(e))
        except table.DoesNotExist:
            abort(404,
                  message='{item} {id} does not exist'.format(
                      item=item_description, id=id))
        except Exception as ex:
            abort(400, message="Unexpected error {ex}".format(ex=ex))