Skip to content

Commit

Permalink
Handle different 3DEP 1m filenames from USGS.
Browse files Browse the repository at this point in the history
  • Loading branch information
akirmse committed Oct 24, 2024
1 parent 64cbe43 commit 5c37e8e
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions scripts/run_3dep1m_prominence.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,37 @@ def run_command(command_string):
@handle_ctrl_c
def process_tile(args):
(project, x, y, zone, tile_dir) = args

filename_base = f"USGS_1M_{zone}_x{x:02}y{y:03}"

flt_filename = os.path.join(tile_dir, filename_base + ".flt")
# The USGS isn't consistent about naming their files; try all these
potential_filenames = [
f"USGS_1M_{zone}_x{x:02}y{y:03}",
f"USGS_one_meter_x{x:02}y{y:03}",
f"USGS_1m_x{x:02}y{y:03}",
]

# Use the first format for local filenames
local_filename_base = potential_filenames[0]

flt_filename = os.path.join(tile_dir, local_filename_base + ".flt")
tif_filename = os.path.join(tile_dir, local_filename_base + ".tif")

# Tile already exists locally?
if not os.path.isfile(flt_filename):
tif_filename = os.path.join(tile_dir, filename_base + ".tif")

# Download TIF file
tif_url = f"{DOWNLOAD_URL_BASE}/{project}/TIFF/{filename_base}_{project}.tif"
print("Downloading tile for x =", x, ", y =", y)
r = requests.get(tif_url, allow_redirects=True)
if r.status_code == 200:
with open(tif_filename, "wb") as f:
f.write(r.content)

# Convert to .flt file format
gdal_command = f"gdal_translate -of EHdr {tif_filename} {flt_filename}"
run_command(gdal_command)
for potential_filename in potential_filenames:
# Download TIF file
tif_url = f"{DOWNLOAD_URL_BASE}/{project}/TIFF/{potential_filename}_{project}.tif"
r = requests.get(tif_url, allow_redirects=True)
if r.status_code == 200:
with open(tif_filename, "wb") as f:
f.write(r.content)

# Convert to .flt file format
gdal_command = f"gdal_translate -of EHdr {tif_filename} {flt_filename}"
run_command(gdal_command)

return


def main():
parser = argparse.ArgumentParser(description='Download GLO tiles and run prominence on them')
Expand Down

0 comments on commit 5c37e8e

Please sign in to comment.