-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdownload_REMA_tile.py
More file actions
executable file
·119 lines (100 loc) · 4.82 KB
/
download_REMA_tile.py
File metadata and controls
executable file
·119 lines (100 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import geopandas as gpd
import numpy as np
from os.path import exists
import requests
from shapely.geometry import MultiPoint
import subprocess
import warnings
warnings.filterwarnings("ignore")
def download_REMA_tile(xs,ys,final_path='/mnt/data01/Data/Antarctic_SurfaceElevation/REMA_Tiles/',all_files=0):
"""
% (C) Nick Holschuh - Amherst College - 2022 ([email protected])
% This function downloads REMA tiles associated with the x and y
% input coordinates provided to the function.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The inputs are as follows:
%
% xs - x_coordinates (polarstereo) where you want REMA coverage
% ys - y_coordinates (polarstereo) where you want REMA coverage
% final_path - the path to place your REMA tiles
% all_files - flag, whether to keep all the rema files [1] or just the elevation tif [0]
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The function returns the following objects:
%
% file_list - a list of all the files you need to cover your area (and their full path)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
"""
#### This section reads the tile shape file
tile_file_exists = exists(final_path+'REMA_Tile_Index_Rel1_1.shp')
if tile_file_exists == False:
print('Downloading and Reading the tile shape file')
############# This handles the case where the tile shape file doesn't already exist
url = 'https://data.pgc.umn.edu/elev/dem/setsm/REMA/indexes/REMA_Tile_Index_Rel1.1.zip'
tile_file = final_path+'temp.zip'
r = requests.get(url, verify=False)
with requests.get(url, stream=True, verify=False) as r:
r.raise_for_status()
with open(tile_file, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
# If you have chunk encoded response uncomment if
# and set chunk_size parameter to None.
#if chunk:
f.write(chunk)
bash_command = 'unzip '+tile_file+' -d '+final_path
process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
bash_command = 'rm '+tile_file
process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
else:
print('Reading the tile shape file')
data = gpd.read_file(final_path+'REMA_Tile_Index_Rel1_1.shp')
target = MultiPoint(np.array([xs,ys]))
######### Here is the debug plot information
#data.plot()
#for i in target:
# plt.plot(i.x,i.y,'o',color='red')
################# Here we find the indecies in the tile file to download
ki = []
for i in target:
vals = data.buffer(10**-1).contains(i)
vals = [vals]
trash, ki_temp = np.where(vals)
ki = np.concatenate([ki,ki_temp])
ki = np.unique(ki).astype('int')
################# Here we loop through the files we need to download
file_list = []
for i in ki:
url = data.iloc[i]['fileurl']
fname = url.split('/')[-1].split('.')[0]+'_dem.tif'
file_exists = exists(final_path+fname)
if file_exists:
print('Already Have File -- '+fname)
else:
########## Here we set the filename
local_filename = final_path+'temp.tar'
########## This does the downloading
r = requests.get(url, verify=False)
with requests.get(url, stream=True, verify=False) as r:
r.raise_for_status()
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
# If you have chunk encoded response uncomment if
# and set chunk_size parameter to None.
#if chunk:
f.write(chunk)
if all_files == 0:
bash_command = 'tar -xvf '+local_filename+' --directory '+final_path+' '+fname
else:
bash_command = 'tar -xvf '+local_filename+' --directory '+final_path
process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
bash_command = 'rm '+local_filename
process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
print('Successfully Downloaded -- '+fname)
file_list.append(final_path+fname)
return file_list