Example #1
0
def set_ldpath(gisbase):
    """Add GRASS libraries to the dynamic library path
    And then re-execute the process to take the changes into account
    """
    # choose right path for each platform
    if (sys.platform.startswith('linux') or 'bsd' in sys.platform
            or 'solaris' in sys.platform):
        ldvar = 'LD_LIBRARY_PATH'
    elif sys.platform.startswith('win'):
        ldvar = 'PATH'
    elif sys.platform == 'darwin':
        ldvar = 'DYLD_LIBRARY_PATH'
    else:
        msgr.fatal("Platform not configured: {}".format(sys.platform))

    ld_base = os.path.join(gisbase, u"lib")
    if not os.environ.get(ldvar):
        # if the path variable is not set
        msgr.debug("{} not set. Setting and restart".format(ldvar))
        os.environ[ldvar] = ld_base
        reexec()
    elif ld_base not in os.environ[ldvar]:
        msgr.debug("{} not in {}. Setting and restart".format(ld_base, ldvar))
        # if the variable exists but does not have the path
        os.environ[ldvar] += os.pathsep + ld_base
        reexec()
Example #2
0
    def initialize(self, conf_file):
        """Parse the configuration file, set GRASS,
        and initialize the simulation.
        """
        # parsing configuration file
        self.conf = ConfigReader(conf_file)

        # display parameters (if verbose)
        msgr.message(f"Starting simulation of {os.path.basename(conf_file)}...")
        self.conf.display_sim_param()

        # If run outside of grass, set it
        if self.need_grass_session:
            self.set_grass_session()
        # GRASS libs can now be safely imported
        import itzi.gis as gis
        from itzi.simulation import SimulationManager
        msgr.debug('GRASS session set')
        # return error if output files exist
        gis.check_output_files(self.conf.output_map_names.values())
        msgr.debug('Output files OK')
        # Instantiate Simulation object and initialize it
        self.sim = SimulationManager(sim_times=self.conf.sim_times,
                                     stats_file=self.conf.stats_file,
                                     dtype=np.float32,
                                     input_maps=self.conf.input_map_names,
                                     output_maps=self.conf.output_map_names,
                                     sim_param=self.conf.sim_param,
                                     drainage_params=self.conf.drainage_params,
                                     grass_params=self.conf.grass_params)
        self.sim.initialize()
        return self
Example #3
0
 def run(self):
     """prepare the simulation, run it and clean up
     """
     if self.prof:
         self.prof.start()
     # If run outside of grass, set it
     if self.grass_use_file:
         self.set_grass_session()
     import itzi.gis as gis
     msgr.debug('GRASS session set')
     # return error if output files exist
     # (should be done once GRASS set up)
     gis.check_output_files(self.conf.output_map_names.itervalues())
     msgr.debug('Output files OK')
     # stop program if location is latlong
     if gis.is_latlon():
         msgr.fatal(u"latlong location is not supported. "
                    u"Please use a projected location")
     # set region
     if self.conf.grass_params['region']:
         gis.set_temp_region(self.conf.grass_params['region'])
     # set mask
     if self.conf.grass_params['mask']:
         gis.set_temp_mask(self.conf.grass_params['mask'])
     # Run simulation (SimulationManager needs GRASS, so imported now)
     from itzi.simulation import SimulationManager
     sim = SimulationManager(sim_times=self.conf.sim_times,
                             stats_file=self.conf.stats_file,
                             dtype=np.float32,
                             input_maps=self.conf.input_map_names,
                             output_maps=self.conf.output_map_names,
                             sim_param=self.conf.sim_param,
                             drainage_params=self.conf.drainage_params)
     sim.run()
     # return to previous region and mask
     if self.conf.grass_params['region']:
         gis.del_temp_region()
     if self.conf.grass_params['mask']:
         gis.del_temp_mask()
     # Delete the rcfile
     if self.grass_use_file:
         os.remove(self.rcfile)
     # end profiling and print results
     if self.prof:
         self.prof.stop()
         print(self.prof.output_text(unicode=True, color=True))
     return self
Example #4
0
    def update_input_arrays(self, sim_time):
        """Get new array using TimedArray
        First update the DEM and mask if needed
        Replace the NULL values (mask)
        """
        # make sure DEM is treated first
        if not self.tarr['z'].is_valid(sim_time):
            self.arr['z'][:] = self.tarr['z'].get(sim_time)
            self.isnew['z'] = True
            # note: must run update_flow_dir() in SuperficialSimulation
            self.update_mask(self.arr['z'])
            self.mask_array(self.arr['z'], np.finfo(self.dtype).max)

        # loop through the arrays
        for k, ta in self.tarr.items():
            if not ta.is_valid(sim_time):
                # z is done before
                if k == 'z':
                    continue
                # calculate statistics before updating array
                if k in ['in_q', 'rain']:
                    self.populate_stat_array(k, sim_time)
                # update array
                msgr.debug(u"{}: update input array <{}>".format(sim_time, k))
                self.arr[k][:] = ta.get(sim_time)
                self.isnew[k] = True
                if k == 'n':
                    fill_value = 1
                else:
                    fill_value = 0
                # mask arrays
                self.mask_array(self.arr[k], fill_value)
            else:
                self.isnew[k] = False
        # calculate water volume at the beginning of the simulation
        if self.isnew['h']:
            self.start_volume = self.asum('h')
        return self
Example #5
0
    def populate_stat_array(self, k, sim_time):
        """given an input array key,
        populate the corresponding statistic array.
        If it's the first update, only check in the time.
        Should be called before updating the array
        """
        sk = self.stats_corresp[k]
        update_time = self.stats_update_time[sk]
        # make sure everything is in m/s
        if k in ['rain', 'inf', 'capped_losses']:
            conv_factor = 1 / self.mmh_to_ms
        else:
            conv_factor = 1.

        if self.stats_update_time[sk] is None:
            self.stats_update_time[sk] = sim_time
        else:
            msgr.debug(u"{}: Populating array <{}>".format(sim_time, sk))
            time_diff = (sim_time - update_time).total_seconds()
            flow.populate_stat_array(self.arr[k], self.arr[sk], conv_factor,
                                     time_diff)
            self.stats_update_time[sk] = sim_time
        return None
Example #6
0
 def cleanup(self):
     """Remove temporary region and mask.
     """
     msgr.debug("Reset mask and region")
     if self.raster_mask_id:
         msgr.debug("Remove temp MASK...")
         self.del_temp_mask()
     if self.region_id:
         msgr.debug("Remove temp region...")
         gscript.del_temp_region()
     self.raster_writer_queue.put(None)
     self.raster_writer_thread.join()
Example #7
0
    def __set_models(self):
        """Instantiate models objects
        """
        # RasterDomain
        msgr.debug(u"Setting up raster domain...")
        try:
            self.rast_domain = RasterDomain(self.dtype, self.gis,
                                            self.in_map_names,
                                            self.out_map_names)
        except MemoryError:
            msgr.fatal(u"Out of memory.")
        # Infiltration
        msgr.debug(u"Setting up raster infiltration...")
        inf_class = {
            'constant': infiltration.InfConstantRate,
            'green-ampt': infiltration.InfGreenAmpt,
            'null': infiltration.InfNull
        }
        try:
            self.infiltration = inf_class[self.inf_model](self.rast_domain,
                                                          self.dtinf)
        except KeyError:
            assert False, u"Unknow infiltration model: {}".format(
                self.inf_model)
        # Hydrology
        msgr.debug(u"Setting up hydrologic model...")
        self.hydrology = hydrology.Hydrology(self.rast_domain, self.dtinf,
                                             self.infiltration)
        # Surface flows simulation
        msgr.debug(u"Setting up surface model...")
        self.surf_sim = SurfaceFlowSimulation(self.rast_domain, self.sim_param)
        # Instantiate Massbal object
        if self.stats_file:
            msgr.debug(u"Setting up mass balance object...")
            self.massbal = MassBal(self.stats_file, self.rast_domain,
                                   self.start_time, self.temporal_type)
        else:
            self.massbal = None

        # Drainage
        if self.drainage_params['swmm_inp']:
            msgr.debug(u"Setting up drainage model...")
            self.drainage = DrainageSimulation(self.rast_domain,
                                               self.drainage_params, self.gis,
                                               self.sim_param['g'])
        else:
            self.drainage = None

        # reporting object
        msgr.debug(u"Setting up reporting object...")
        self.report = Report(self.gis, self.temporal_type,
                             self.sim_param['hmin'], self.massbal,
                             self.rast_domain, self.drainage,
                             self.drainage_params['output'])
        return self
Example #8
0
 def finalize(self):
     """Make sure that all maps are written.
     """
     msgr.debug("Writing last maps...")
     self.raster_writer_queue.join()