def _get_array_points_(self, x, y, fid, i):
        """Internal method called by _get_array_points().
        """
        # position i,j in raster (starts at 0)
        r = int(self.data['r'] -
                ((y - self.data['yllcorner']) // self.data['vpix']) - 1)
        c = int((x - self.data['xllcorner']) // self.data['spix'])

        # if point is not on the edge of raster or its
        # neighbours are not "NoDataValue", it will be saved
        # into array_points array
        nv = self.data['NoDataValue']
        if r != 0 and r != self.data['r'] \
           and c != 0 and c != self.data['c'] and \
           self.data['mat_dem'][r][c]   != nv and \
           self.data['mat_dem'][r-1][c] != nv and \
           self.data['mat_dem'][r+1][c] != nv and \
           self.data['mat_dem'][r][c-1] != nv and \
           self.data['mat_dem'][r][c+1] != nv:

            self.data['array_points'][i][0] = fid
            self.data['array_points'][i][1] = r
            self.data['array_points'][i][2] = c
            # x,y coordinates of current point stored in an array
            self.data['array_points'][i][3] = x
            self.data['array_points'][i][4] = y
        else:
            Logger.info(
                "Point FID = {} is at the edge of the raster. "
                "This point will not be included in results.".format(fid))

        return i
Exemple #2
0
    def __init__(self, indata_path):
        """Create argument parser."""
        super(OptimSensProvider, self).__init__()

        ## define CLI parser
        #parser = argparse.ArgumentParser(description='Run Smoderp2D.')

        ## type of computation
        #parser.add_argument(
        #    '--typecomp',
        #    help='type of computation',
        #    type=str,
        #    choices=['full',
        #             'dpre',
        #             'roff'],
        #    required=True
        #)

        ## data file (only required for runoff)
        #parser.add_argument(
        #    '--indata',
        #    help='file with prepared data',
        #    type=str
        #)
        #self.args = parser.parse_args()
        #self.args.typecomp = CompType()[self.args.typecomp]

        self.args = Namespace(indata=indata_path, typecomp='roff')

        # load configuration
        self._config = ConfigParser()
        if self.args.typecomp == CompType.roff:
            if not self.args.indata:
                parser.error('--indata required')
            if not os.path.exists(self.args.indata):
                raise ConfigError("{} does not exist".format(self.args.indata))
            self._config.read(self.args.indata)

        try:
            # set logging level
            Logger.setLevel(self._config.get('Other', 'logging'))
            # sys.stderr logging
            self._add_logging_handler(logging.StreamHandler(stream=sys.stderr))

            # must be defined for _cleanup() method
            Globals.outdir = self._config.get('Other', 'outdir')
        except NoSectionError as e:
            raise ConfigError('Config file {}: {}'.format(self.args.indata, e))

        # define storage writter
        self.storage = CmdWritter()
Exemple #3
0
    def set_options(self, options):
        """Set input paramaters.

        :param options: options dict to set
        """
        self._config.read(options['indata'])

        try:
            # set logging level
            Logger.setLevel(self._config.get('Other', 'logging'))
            # sys.stderr logging
            self._add_logging_handler(
                logging.StreamHandler(stream=sys.stderr)
            )

            # must be defined for _cleanup() method
            Globals.outdir = self._config.get('Other', 'outdir')
        except NoSectionError as e:
            raise ConfigError('Config file {}: {}'.format(
                options['indata'], e
            ))

        # define storage writter
        self.storage = CmdWritter()
    def prepare(self):
        self._setnull()  # not used for anything, just saves setnull

        Logger.info("Clip stream...")
        stream, stream_loc = self._clip_stream()

        Logger.info("Computing stream direction and elevation...")
        self._stream_direction(stream)

        mat_stream_seg = self._get_mat_stream_seg(stream)

        Logger.info("Computing stream hydraulics...")
        self._stream_hydraulics(stream)

        self._stream_slope(stream)

        self._get_streamlist(stream)

        return self.streamlist, mat_stream_seg, stream_loc
    def _set_output(self):
        """Creates empty output and temporary directories to which created
        files are saved.
        """
        if not self.data['outdir']:
            # no output directory defined, nothing to do
            return

        # delete output directory if exists and create new one
        Logger.info("Creating output directories <{}>".format(
            self.data['outdir']))
        if os.path.exists(self.data['outdir']):
            shutil.rmtree(self.data['outdir'])
        os.makedirs(self.data['outdir'])

        # create temporary dir
        self.data['temp'] = os.path.join(self.data['outdir'], "temp")
        Logger.debug("Creating temp directory <{}>".format(self.data['temp']))
        os.makedirs(self.data['temp'])

        # create control dir
        control = os.path.join(self.data['outdir'], "control")
        Logger.debug("Creating control directory <{}>".format(control))
        os.makedirs(control)
Exemple #6
0
 def __init__(self, msg):
     Logger.fatal("Invalid input for data preparation: {}".format(msg))
 def _diff_npoints(npoints1, npoints2):
     diffpts = npoints1 - npoints2
     if diffpts > 0:
         Logger.warning("{} points outside of computation domain "
                        "will be ignored".format(diffpts))
    def run(self):
        Logger.info('-' * 80)
        Logger.info("DATA PREPARATION")

        # check input data (overlaps)
        self._check_input_data()

        # set output data directory
        self._set_output_data()

        # create output folder, where temporary data are stored
        self._set_output()
        dem_copy, dem_mask = self._set_mask()

        # intersect
        Logger.info("Computing intersect of input data...")
        intersect, mask_shp, sfield = self._get_intersect(
            dem_copy, dem_mask, self._input_params['vegetation'],
            self._input_params['soil'], self._input_params['vegetation_type'],
            self._input_params['soil_type'],
            self._input_params['table_soil_vegetation'],
            self._input_params['table_soil_vegetation_code'])

        # clip
        Logger.info("Clip of the source data by intersect...")
        dem_clip = self._clip_data(dem_copy, intersect)

        # DTM computation
        Logger.info(
            "Computing fill, flow direction, flow accumulation, slope...")
        flow_direction_clip, flow_accumulation_clip, slope_clip = self._terrain_products(
            dem_clip)

        # raster to numpy array conversion
        Logger.info("Computing parameters of DTM...")
        self.data['mat_dem'] = self._rst2np(dem_clip)
        self.data['mat_slope'] = self._rst2np(slope_clip)

        # update data dict for spatial ref info
        self._get_raster_dim(dem_clip)
        if flow_direction_clip is not None:
            self.data['mat_fd'] = self._rst2np(flow_direction_clip)
            self.storage.write_raster(self.data['mat_fd'], 'fl_dir', 'temp')

        # build numpy array from selected attributes
        all_attrib = self._get_mat_par(sfield, intersect)

        # build points array
        Logger.info("Prepare points for hydrographs...")
        self._get_array_points()

        # build a/aa arrays
        self._get_a(all_attrib)

        Logger.info("Computing critical level...")
        self._get_crit_water(all_attrib)

        # load precipitation input file
        self.data['sr'], self.data['itera'] = \
            rainfall.load_precipitation(self._input_params['rainfall_file'])

        # compute aspect
        self._get_slope_dir(dem_clip)

        Logger.info("Computing stream preparation...")
        self._prepare_streams(mask_shp, dem_clip, intersect,
                              flow_accumulation_clip)

        # define mask (rc/rc variables)
        self._find_boundary_cells()

        self.data['mat_n'] = all_attrib[2]
        self.data['mat_pi'] = all_attrib[3]
        self.data['mat_ppl'] = all_attrib[4]
        self.data['mat_reten'] = all_attrib[5]
        self.data['mat_b'] = all_attrib[6]

        self.data['mfda'] = False
        self.data['mat_boundary'] = None
        self.data['spix'] = None
        self.data['vpix'] = None

        Logger.info("Data preparation has been finished")
        Logger.info('-' * 80)

        return self.data
 def _add_message(self, message):
     """
     Pops up a message into arcgis and saves it into log file.
     :param message: Message to be printed.
     """
     Logger.info(message)