-
-
Save carderne/d44d73eea12b70f2ba0822ca0fa97480 to your computer and use it in GitHub Desktop.
Convert all Geosoft *.grd files to geotiff in a directory (recursively)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pathlib | |
from itertools import product | |
from tqdm import tqdm | |
import rasterio | |
import geosoft | |
import geosoft.gxpy.gx as gx | |
import geosoft.gxpy.coordinate_system as gxcs | |
import geosoft.gxpy.grid as gxgrid | |
# Load up Geosoft session (handles authentication etc) | |
print('Geosoft version:', geosoft.__version__) | |
gxp = gx.GXpy() | |
print(f'Module loaded ok, signed in as {gxp.gid}') | |
# Define some CRSes | |
cs_mga53 = gxcs.Coordinate_system('GDA94 / MGA zone 53') | |
cs_utm = gxcs.Coordinate_system('WGS 84') | |
cs_transform = gxcs.Coordinate_translate(cs_mga53, cs_utm) | |
# Sidecar files to clean up at the end | |
EXTENSIONS_TO_DELETE = [ | |
ext1 + ext2 for ext1, ext2 in product(('.grd', '.gxf'), ('.gi', '.xml')) | |
] | |
def process_grids_to_tiff(root_folder, clean=True, ignore_errors=True): | |
""" | |
Convert grid files to geotiff using Geosoft GXDeveloper tools and Rasterio | |
Does a two-stage conversion: | |
1. Converts the *.grd files to grid interchange format (GXF) | |
2. Converts the *.gxf files to a floating tiff (.geo.tiff) | |
We don't do any reprojection to avoid introducing new noise, so the CRS of | |
the final grid will be the same as the original GRD file. | |
Final file names are the same as the original file stem, plus the relevant | |
suffix (.gxf or .geo.tif). | |
Parameters: | |
grids - a list of pathlib.Path objects pointing to grid files to convert | |
clean - whether to clean the sidecar files (.gi and .xml) from reading | |
the grids. Optional, defaults to True | |
ignore_errors - whether to raise or ignore errors | |
""" | |
# Find all .grd files under the root directory | |
grids = list(pathlib.Path(root_folder).rglob("*.grd")) | |
# Loop and convert | |
for grid in tqdm(grids, total=len(grids), desc='Processing grids'): | |
# Convert grd to gxf format | |
new_gxf = str(grid.parent / pathlib.Path(grid.stem + '.gxf')) | |
try: | |
with gxgrid.Grid.open(f'{grid}(GRD)', mode=gxgrid.FILE_READWRITE) as src: | |
if src.coordinate_system == '*unknown': | |
src.coordinate_system = cs_mga53 | |
gxgrid.Grid.copy(src, new_gxf + '(GXF)', overwrite=True) | |
except Exception as err: | |
if ignore_errors: | |
continue | |
else: | |
raise err | |
# Convert gxf to geotiff-float with rasterio | |
new_tiff = str(grid.parent / pathlib.Path(grid.stem + '.geo.tif')) | |
with rasterio.open(new_gxf, 'r') as src: | |
meta = src.meta | |
meta['driver'] = 'GTiff' | |
meta['dtype'] = 'float32' | |
with rasterio.open(new_tiff, 'w', **meta) as sink: | |
sink.write(src.read(1), 1) | |
# Clean up useless .gi and .xml files since we don't need those for export! | |
for filename in grid.parent.iterdir(): | |
if any(filename.name.endswith(e) for e in EXTENSIONS_TO_DELETE): | |
filename.unlink() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment