def _generate_mask(self, interval, r_id): """ Actually generates a mask for a given interval and region (r_id) """ # Get GRASS interface instance g = grass.get_g() # Generate a random map name mapname = g.generate_map_name("mask") if r_id in self.bins.keys(): bins = self.bins[r_id] elif "__default" in self.bin.keys(): bins = self.bins["__default"] else: self.log.error( "Could not find any phenology bins for generating mask") # Find what bin the interval lies within for d_range, mean in bins.items(): if interval >= d_range[0] and interval <= d_range[1]: g.mapcalc(mapname, "if(%s>=%f,if(%s<=%f,1,0),0)" % (self.p_map_names[ r_id], d_range[0], self.p_map_names[r_id], d_range[1])) grassmap_mask = GrassMap(mapname) grassmap_mask.temporary = True return grassmap_mask self.log.debug( "No appropriate interval range found for interval %d" % interval)
def _generate_mask(self, interval, r_id): """ Actually generates a mask for a given interval and region (r_id) """ # Get GRASS interface instance g = grass.get_g() # Generate a random map name mapname = g.generate_map_name("mask") if r_id in self.bins.keys(): bins = self.bins[r_id] elif "__default" in self.bin.keys(): bins = self.bins["__default"] else: self.log.error("Could not find any phenology bins for generating mask") # Find what bin the interval lies within for d_range, mean in bins.items(): if interval >= d_range[0] and interval <= d_range[1]: g.mapcalc( mapname, "if(%s>=%f,if(%s<=%f,1,0),0)" % (self.p_map_names[r_id], d_range[0], self.p_map_names[r_id], d_range[1]), ) grassmap_mask = GrassMap(mapname) grassmap_mask.temporary = True return grassmap_mask self.log.debug("No appropriate interval range found for interval %d" % interval)
def _load_lifestage(self): """ Loads details from XML into class """ # Load name if "name" in self.xml_node.attrib.keys(): self.name = self.xml_node.attrib["name"] else: raise Exception("Can't find name of lifestage") # Load whether population based or not if "populationBased" in self.xml_node.attrib.keys(): if self.xml_node.attrib["populationBased"].upper().find("TRUE") != -1: self.populationBased = True # Load and construct event objects event_nodes = self.xml_node.xpath("event") for e_node in event_nodes: e = Event(e_node) self.events.append(e) # Load the initial distribution maps init_map_nodes = self.xml_node.xpath('initialDistribution') for i_node in init_map_nodes: r_id = i_node.attrib["region"] # stupid way of ignoring comments for i in i_node.iter(tag=lxml.etree.Element): if i == i_node: continue m_node = i break self.initial_maps[r_id] = GrassMap(m_node) # Init phenology bins self.init_phenology_bins()
def init_from_xml(self): if self.node.tag == "mfilter": self.area = Event(self.node) else: # If it's not an mfilter it must be a map self.area = GrassMap(self.node)
class TreatmentArea: def __init__(self, node, treatment, a_index): """ node is the xml node defining the TreatmentArea. treatment is the parent Treatment this area is for. a_index is the area index used to create the temp map name. """ self.treatment = treatment self.node = node self.area = None self.area_filter_output = None self.index = a_index # temporary map name self.area_temp = "x_t___strategy_" + self.treatment.strategy.get_name() + \ "_area_t_" + str(self.treatment.index) + "_" + str(self.index) self.init_from_xml() def __del__(self): if self.area_filter_output is not None: grass.get_g().remove_map(self.area_filter_output) def init_from_xml(self): if self.node.tag == "mfilter": self.area = Event(self.node) else: # If it's not an mfilter it must be a map self.area = GrassMap(self.node) def is_dynamic(self): """ Return whether this TreatmentArea changes each timestep or not. An area if dynamic if it uses an event/mfilter or a GrassMap with refresh==True. An example of the latter case is using r.mapcalc. """ if isinstance(self.area, Event): return True elif isinstance(self.area, GrassMap): return self.area.refresh else: raise Exception("Unknown area type") def get_map_resources(self): maps = [] if isinstance(self.area, Event): m = self.treatment.strategy.experiment maps.extend(self.area.get_map_resources(m)) elif isinstance(self.area, GrassMap): if self.area.xml_map_type == "name": g = grass.get_g() maps.append((self.area.filename, g.find_mapset(self.area.filename))) return maps def get_treatment_area(self, replicate, last_area=None): """ Get the map name representing the treatment area, generating it dynamically if necessary. """ if isinstance(self.area, Event): if self.area_filter_output is not None: grass.get_g().remove_map(self.area_filter_output) if last_area: dist_map = last_area else: dist_map = replicate.temp_map_names[self.treatment.area_ls][0] self.area.run(dist_map, self.area_temp, replicate, False) self.area_filter_output = self.area_temp return self.area_filter_output elif isinstance(self.area, GrassMap): replacements = { "POP_MAP": replicate.temp_map_names[self.treatment.area_ls][0], "START_MAP": replicate.initial_maps[self.treatment.area_ls].get_map_filename() } return self.area.get_map_filename(replacements)
class TreatmentArea: def __init__(self, node, treatment, a_index): """ node is the xml node defining the TreatmentArea. treatment is the parent Treatment this area is for. a_index is the area index used to create the temp map name. """ self.treatment = treatment self.node = node self.area = None self.area_filter_output = None self.index = a_index # temporary map name self.area_temp = "x_t___strategy_" + self.treatment.strategy.get_name() + \ "_area_t_" + str(self.treatment.index) + "_" + str(self.index) self.init_from_xml() def __del__(self): if self.area_filter_output is not None: grass.get_g().remove_map(self.area_filter_output) def init_from_xml(self): if self.node.tag == "mfilter": self.area = Event(self.node) else: # If it's not an mfilter it must be a map self.area = GrassMap(self.node) def is_dynamic(self): """ Return whether this TreatmentArea changes each timestep or not. An area if dynamic if it uses an event/mfilter or a GrassMap with refresh==True. An example of the latter case is using r.mapcalc. """ if isinstance(self.area, Event): return True elif isinstance(self.area, GrassMap): return self.area.refresh else: raise Exception("Unknown area type") def get_map_resources(self): maps = [] if isinstance(self.area, Event): m = self.treatment.strategy.experiment maps.extend(self.area.get_map_resources(m)) elif isinstance(self.area, GrassMap): if self.area.xml_map_type == "name": g = grass.get_g() maps.append( (self.area.filename, g.find_mapset(self.area.filename))) return maps def get_treatment_area(self, replicate, last_area=None): """ Get the map name representing the treatment area, generating it dynamically if necessary. """ if isinstance(self.area, Event): if self.area_filter_output is not None: grass.get_g().remove_map(self.area_filter_output) if last_area: dist_map = last_area else: dist_map = replicate.temp_map_names[self.treatment.area_ls][0] self.area.run(dist_map, self.area_temp, replicate, False) self.area_filter_output = self.area_temp return self.area_filter_output elif isinstance(self.area, GrassMap): replacements = { "POP_MAP": replicate.temp_map_names[self.treatment.area_ls][0], "START_MAP": replicate.initial_maps[ self.treatment.area_ls].get_map_filename() } return self.area.get_map_filename(replacements)