def patch(self): """Patch the final results.""" bboxes = split_region_tiles(width=self.width, height=self.height) loc = Location() mset = loc[self.mset.name] mset.visible.extend(loc.mapsets()) noutputs = 0 for otmap in self.module.outputs: otm = self.module.outputs[otmap] if otm.typedesc == "raster" and otm.value: rpatch_map( otm.value, self.mset.name, self.msetstr, bboxes, self.module.flags.overwrite, self.start_row, self.start_col, self.out_prefix, ) noutputs += 1 if noutputs < 1: msg = "No raster output option defined for <{}>".format(self.module.name) if self.module.name == "r.mapcalc": msg += ". Use <{}.simple> instead".format(self.module.name) raise RuntimeError(msg)
def patch(self): """Patch the final results.""" bboxes = split_region_tiles(width=self.width, height=self.height) loc = Location() mset = loc[self.mset.name] mset.visible.extend(loc.mapsets()) for otmap in self.module.outputs: otm = self.module.outputs[otmap] if otm.typedesc == 'raster' and otm.value: rpatch_map(otm.value, self.mset.name, self.msetstr, bboxes, self.module.flags.overwrite, self.start_row, self.start_col, self.out_prefix)
def patch(self): """Patch the final results.""" # make all mapset in the location visible loc = Location() mset = loc[self.mset.name] mset.current() mset.visible.extend(loc.mapsets()) # patch all the outputs bboxes = split_region_tiles(width=self.width, height=self.height) inputs = self.module.inputs print("Start patching the segments") start = time.time() rpatch_map( inputs.outputs_prefix % inputs.thresholds[-1], self.mset.name, self.msetstr, bboxes, self.module.flags.overwrite, self.start_row, self.start_col, self.out_prefix, ) print("%s, required: %.2fs" % (OPTS["output"], time.time() - start)) # segment print("Start running segment for the last time in the whole region") start = time.time() iseg = Module("i.segment") threshold = self.module.inputs.thresholds[-1] iseg( group=self.module.inputs.group, output=self.module.outputs.output, threshold=threshold, method=self.module.inputs.method, similarity=self.module.inputs.similarity, minsize=self.module.inputs.minsizes[-1], memory=self.memory, iterations=3, seeds=self.module.inputs.outputs_prefix % threshold, ) print("%s, required: %.2fs" % (OPTS["output"], time.time() - start)) self.mset.current() if self.move: copy_rasters( [ self.module.outputs.output, ], self.gisrc_dst, self.gisrc_src, )
def register(mset_pat, rast_pat, datefmt="%Y%m%d_%H", mapset_fmt="r%Y_%m", raster_fmt="{base}_{date:%Y%m}", sep='|', overwrite=False, **kwargs): reg_dir = gscript.tempdir() env = os.environ.copy() loc = Location() temporals = {} for mset_name in loc.mapsets(pattern=mset_pat): menv = gs.grass_init(gs.GISBASE, loc.gisdbase, loc.name, mset_name, env=env.copy()) for rname in loc[mset_name].glist("raster", pattern=rast_pat): base, date = extract_date(rname, datefmt=datefmt) trast = raster_fmt.format(base=base, date=date) rasts = temporals.get(trast, []) rasts.append((rname, "{date:%Y-%m-%d %H:%M:%S}".format(date=date))) temporals[trast] = rasts # create for tname, trasts in temporals.items(): gscript.run_command( "t.create", type="strds", temporaltype="absolute", output=tname, title=tname, description="COSMO REA6: {tname}".format(tname=tname), semantictype="mean", overwrite=overwrite, env=menv) csvfile = os.path.join(reg_dir, "{}.csv".format(tname)) with open(csvfile, mode="w") as csv: for row in trasts: csv.write(sep.join(row) + '\n') gscript.run_command("t.register", overwrite=overwrite, type="raster", input=tname, file=csvfile, separator=SEP.get(sep, sep), env=menv)
def clean_location(self, location=None): """Remove all created mapsets. :param location: a Location instance where we are running the analysis :type location: Location object """ if location is None: if self.n_mset: self.n_mset.current() location = Location() mapsets = location.mapsets(self.msetstr.split('_')[0] + '_*') for mset in mapsets: Mapset(mset).delete() if self.n_mset and self.n_mset.is_current(): self.mset.current()
def reproject(igisdbase, ilocation, olocation, mset_pat, rast_pat, datefmt="%Y%m%d_%H", mapset_fmt="r%Y_%m", raster_fmt="T{elev:03d}m_%Y%m%d_%H", nprocs=4, ogisdbase=None, **kwargs): env = os.environ.copy() ogisdbase = igisdbase if ogisdbase is None else ogisdbase mset_envs = {} mset_rasters = {} queue = ParallelModuleQueue(nprocs=nprocs) iloc = Location(location=ilocation, gisdbase=igisdbase) # oloc = Location(location=olocation, gisdbase=ogisdbase) #import ipdb; ipdb.set_trace() for imset_name in iloc.mapsets(pattern=mset_pat): for rname in iloc[imset_name].glist("raster", pattern=rast_pat): base, date, elev = extract_date(rname, datefmt=datefmt) rast_name = date.strftime(raster_fmt.format(elev=elev)) mset_name = date.strftime(mapset_fmt) mset_path = os.path.join(ogisdbase, olocation, mset_name) if not os.path.exists(mset_path): gs.grass_create(gs.GRASSBIN, mset_path, create_opts="") try: os.makedirs(os.path.join(mset_path, '.tmp')) os.makedirs( os.path.join(mset_path, '.tmp', socket.gethostname())) except: # ignore error in creating the pass try: menv = mset_envs[mset_name] rasters = mset_rasters[mset_name] except KeyError: menv = gs.grass_init(gs.GISBASE, ogisdbase, olocation, mset_name, env=env.copy()) mset_envs[mset_name] = menv mset = Mapset(mset_name, location=olocation, gisdbase=ogisdbase) rasters = set(mset.glist("raster")) mset_rasters[mset_name] = rasters # set region for the mapset sregion = read_command("r.proj", location=ilocation, dbase=igisdbase, mapset=imset_name, input=rname, output=rast_name, flags="g", env=menv) #import ipdb; ipdb.set_trace() kregion = dict([tuple(s.split('=')) for s in sregion.split()]) run_command("g.region", save=mset_name, env=menv, overwrite=True, **kregion) menv["WIND_OVERRIDE"] = mset_name if rast_name not in rasters: mod = Module("r.proj", location=ilocation, dbase=igisdbase, mapset=imset_name, input=rname, output=rast_name, run_=False, **kwargs) mod.env_ = menv print(rast_name) #time.sleep(0.2) # sllep otherwise there is a problem in creating queue.put(mod) queue.wait()