diff --git a/src/richardutils/richardutils.py b/src/richardutils/richardutils.py index 609dc77..8c83758 100644 --- a/src/richardutils/richardutils.py +++ b/src/richardutils/richardutils.py @@ -1133,7 +1133,7 @@ def create_vrt_for_geotiffs(directory): def tif_to_ers(strpath, masked=True, chunks=None, dsmatch=None): """ - Walks a directory of geotiffs and returns each as an ers grid and writes to fiel + Walks a directory of geotiffs and returns each as an ers grid and writes to file Args: strpath: directory name chunks: tuple of integers @@ -1170,6 +1170,47 @@ def tif_to_ers(strpath, masked=True, chunks=None, dsmatch=None): return check_dict + +def ers_to_tif(strpath, masked=True, chunks=None, dsmatch=None): + """ + Walks a directory of ers and returns each as an ers geotiff + Args: + strpath: directory name + chunks: tuple of integers + dsmatch: data array to match the directory of rasters to + masked: whether to mask by nodata, default is yes, pass masked=False if not desired + Returns: + a dictionary of rioxarrays + + Examples: + tif_dict(r'D:\BananaSplits') + tif_dict(r'D:\BananaSplits', chunk=s(1,1024,1024)) + + """ + + check_dict = {} + for root, dirs, files in os.walk(strpath): + for file in files: + if '.ers' in file and '.xml' not in file: + if dsmatch is None: + if chunks is None: + check_dict[file] = rioxarray.open_rasterio(os.path.join(root,file),masked=masked) + else: + check_dict[file] = rioxarray.open_rasterio(os.path.join(root,file), masked=masked, chunks=chunks) + else: + if chunks is None: + check_dict[file] = rioxarray.open_rasterio(os.path.join(root,file), masked=masked).rio.reproject_match(dsmatch) + else: + check_dict[file] = rioxarray.open_rasterio(os.path.join(root,file), masked=masked, chunks=chunks).rio.reproject_match(dsmatch) + + for file in check_dict: + ersfile = file.replace('.ers','.tif') + print(file.replace('.ers','.etif')) + check_dict[file].rio.to_raster(ersfile, driver='GTiff') + + + return check_dict + def tif_to_img(strpath, masked=True, chunks=None, dsmatch=None):