def __init__(self, shape, bbox, crs, name, n_subchunks, outputdir, band_tags=None, independent=False, **kwargs): """ pass in additional geotif write options in kwargs """ # affine self.A, _, _ = image.bbox2affine(bbox[1, 0], bbox[0, 0], bbox[0, 1], bbox[1, 1], shape[0], shape[1]) self.shape = shape self.outbands = len(band_tags) self.bbox = bbox self.name = name self.outputdir = outputdir self.n_subchunks = n_subchunks self.independent = independent # mpi control self.sub_starts = [k[0] for k in np.array_split( np.arange(self.shape[1]), mpiops.chunks * self.n_subchunks)] # file tags don't have spaces if band_tags: file_tags = ["_".join(k.lower().split()) for k in band_tags] else: file_tags = [str(k) for k in range(self.outbands)] band_tags = file_tags files = [] file_names = [] if mpiops.chunk_index == 0: for band in range(self.outbands): output_filename = os.path.join(outputdir, name + "_" + file_tags[band] + ".tif") f = rasterio.open(output_filename, 'w', driver='GTiff', width=self.shape[0], height=self.shape[1], dtype=np.float32, count=1, crs=crs, transform=self.A, nodata=self.nodata_value, **kwargs ) f.update_tags(1, image_type=band_tags[band]) files.append(f) file_names.append(output_filename) if independent: self.files = files else: if mpiops.chunk_index == 0: # create a file for each band self.files = files self.file_names = file_names else: self.file_names = [] self.file_names = mpiops.comm.bcast(self.file_names, root=0)
def __init__(self, shape, bbox, crs, name, n_subchunks, outputdir, band_tags=None): # affine self.A, _, _ = image.bbox2affine(bbox[1, 0], bbox[0, 0], bbox[0, 1], bbox[1, 1], shape[0], shape[1]) self.shape = shape self.outbands = len(band_tags) self.bbox = bbox self.name = name self.outputdir = outputdir self.n_subchunks = n_subchunks self.sub_starts = [ k[0] for k in np.array_split(np.arange(self.shape[1]), mpiops.chunks * self.n_subchunks) ] # file tags don't have spaces if band_tags: file_tags = ["_".join(k.lower().split()) for k in band_tags] else: file_tags = [str(k) for k in range(self.outbands)] band_tags = file_tags if mpiops.chunk_index == 0: # create a file for each band self.files = [] for band in range(self.outbands): output_filename = os.path.join( outputdir, name + "_" + file_tags[band] + ".tif") f = rasterio.open(output_filename, 'w', driver='GTiff', width=self.shape[0], height=self.shape[1], dtype=np.float32, count=1, crs=crs, transform=self.A, nodata=self.nodata_value) f.update_tags(1, image_type=band_tags[band]) self.files.append(f)
def __init__(self, shape, bbox, crs, n_subchunks, outpath, outbands, band_tags=None, independent=False, **kwargs): """ pass in additional geotif write options in kwargs """ # affine self.A, _, _ = image.bbox2affine(bbox[1, 0], bbox[0, 0], bbox[0, 1], bbox[1, 1], shape[0], shape[1]) self.shape = shape self.outbands = outbands self.bbox = bbox self.outpath = outpath self.n_subchunks = n_subchunks self.independent = independent # mpi control self.sub_starts = [ k[0] for k in np.array_split(np.arange(self.shape[1]), mpiops.chunks * self.n_subchunks) ] # file tags don't have spaces if band_tags: if self.outbands > len(band_tags): _logger.warning( f"Specified more outbands ({self.outbands}) than there are " f"prediction tags available ({len(band_tags)}). " f"Limiting outbands to number of prediction tags.") self.outbands = len(band_tags) file_tags = ["_".join(k.lower().split()) for k in band_tags] else: file_tags = [str(k) for k in range(self.outbands)] band_tags = file_tags files = [] file_names = [] if mpiops.chunk_index == 0: for band in range(self.outbands): output_filename = self.outpath.format(file_tags[band]) f = rasterio.open(output_filename, 'w', driver='GTiff', width=self.shape[0], height=self.shape[1], dtype=np.float32, count=1, crs=crs, transform=self.A, nodata=self.nodata_value, **kwargs) f.update_tags(1, image_type=band_tags[band]) files.append(f) file_names.append(output_filename) if independent: self.files = files else: if mpiops.chunk_index == 0: # create a file for each band self.files = files self.file_names = file_names else: self.file_names = [] self.file_names = mpiops.comm.bcast(self.file_names, root=0)