Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/dataset/topo/srtmgl3.py: 93%

69 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2024-03-07 11:54 +0000

1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

4# ---|P------/S----------~Lg---------- 

5 

6''' 

7NASA Shuttle Radar Topography Mission Global 30 arc second (`SRTMGL3 <https://lpdaac.usgs.gov/dataset_discovery/measures/measures_products_table/srtmgl3_v003>`_) topography data. 

8''' # noqa 

9 

10import zipfile 

11import os.path as op 

12import os 

13import re 

14# from pyrocko.util import urlopen 

15 

16import numpy as num 

17 

18from pyrocko import util, config 

19from . import tile, dataset 

20 

21citation = ''' 

22Farr, T. G., and M. Kobrick, 2000, Shuttle Radar Topography Mission produces a 

23wealth of data. Eos Trans. AGU, 81:583-585. 

24 

25Farr, T. G. et al., 2007, The Shuttle Radar Topography Mission, Rev. Geophys., 

2645, RG2004, doi:10.1029/2005RG000183. (Also available online at 

27http://www2.jpl.nasa.gov/srtm/SRTM_paper.pdf) 

28 

29Kobrick, M., 2006, On the toes of giants-How SRTM was born, Photogramm. Eng. 

30Remote Sens., 72:206-210. 

31 

32Rosen, P. A. et al., 2000, Synthetic aperture radar interferometry, Proc. IEEE, 

3388:333-382. 

34''' 

35 

36__doc__ += ''' 

37.. rubric:: Citation 

38 

39''' + citation 

40 

41 

42class AuthenticationRequired(Exception): 

43 pass 

44 

45 

46class SRTMGL3(dataset.TiledGlobalDataset): 

47 ''' 

48 

49 ''' 

50 

51 def __init__( 

52 self, 

53 name='SRTMGL3', 

54 data_dir=op.join(op.dirname(__file__), 'data', 'SRTMGL3'), 

55 raw_data_url='https://mirror.pyrocko.org/e4ftl01.cr.usgs.gov/' 

56 'MEASURES/SRTMGL3.003/2000.02.11'): 

57 

58 dataset.TiledGlobalDataset.__init__( 

59 self, 

60 name, 

61 432001, 216001, 1201, 1201, 

62 num.dtype('>i2'), 

63 data_dir=data_dir, 

64 citation=citation, 

65 region=(-180., 180., -60., 60.)) 

66 

67 self.raw_data_url = raw_data_url 

68 self._available_tilenames = None 

69 self.config = config.config() 

70 

71 def tilename(self, itx, ity): 

72 itx -= 180 

73 ity -= 90 

74 if ity >= 0: 

75 s = 'N%02i' % ity 

76 else: 

77 s = 'S%02i' % -ity 

78 

79 if itx >= 0: 

80 s += 'E%03i' % itx 

81 else: 

82 s += 'W%03i' % -itx 

83 

84 return s 

85 

86 def available_tilenames(self): 

87 if self._available_tilenames is None: 

88 fpath = op.join(self.data_dir, 'available.list') 

89 if not op.exists(fpath) or os.stat(fpath).st_size == 0: 

90 util.ensuredirs(fpath) 

91 # remote structure changed, we would have to clawl through 

92 # many pages. Now keeping tile index here: 

93 self.download_file( 

94 'https://mirror.pyrocko.org/e4ftl01.cr.usgs.gov/' 

95 'MEASURES/SRTMGL3.003/2000.02.11/available.list', fpath) 

96 

97 # url = self.raw_data_url + '/' 

98 # f = urlopen(url) 

99 # data = f.read().decode() 

100 # available = re.findall( 

101 # r'([NS]\d\d[EW]\d\d\d)\.SRTMGL3\.hgt', data) 

102 # 

103 # f.close() 

104 # 

105 # with open(fpath, 'w') as f: 

106 # f.writelines('%s\n' % s for s in available) 

107 

108 with open(fpath, 'r') as f: 

109 available = [ 

110 s.strip() for s in f.readlines() 

111 if re.match(r'^[NS]\d\d[EW]\d\d\d$', s.strip())] 

112 

113 self._available_tilenames = set(available) 

114 

115 return self._available_tilenames 

116 

117 def tilefilename(self, tilename): 

118 return tilename + '.SRTMGL3.hgt.zip' 

119 

120 def tilepath(self, tilename): 

121 fn = self.tilefilename(tilename) 

122 return op.join(self.data_dir, fn) 

123 

124 def download_tile(self, tilename): 

125 fpath = self.tilepath(tilename) 

126 fn = self.tilefilename(tilename) 

127 url = self.raw_data_url + '/' + fn 

128 try: 

129 # we have to follow the oauth redirect here... 

130 self.download_file(url, fpath) 

131 except Exception as e: 

132 raise e 

133 

134 def download(self): 

135 for tn in self.available_tilenames(): 

136 fpath = self.tilepath(tn) 

137 if not op.exists(fpath): 

138 self.download_tile(tn) 

139 

140 def get_tile(self, itx, ity): 

141 tn = self.tilename(itx, ity) 

142 if tn not in self.available_tilenames(): 

143 return None 

144 else: 

145 fpath = self.tilepath(tn) 

146 if not op.exists(fpath): 

147 self.download_tile(tn) 

148 

149 zipf = zipfile.ZipFile(fpath, 'r') 

150 rawdata = zipf.read(tn + '.hgt') 

151 zipf.close() 

152 data = num.frombuffer(rawdata, dtype=self.dtype) 

153 assert data.size == self.ntx * self.nty 

154 data = data.reshape(self.nty, self.ntx)[::-1, ::] 

155 return tile.Tile( 

156 self.xmin + itx*self.stx, 

157 self.ymin + ity*self.sty, 

158 self.dx, self.dx, data) 

159 

160 

161if __name__ == '__main__': 

162 import sys 

163 

164 util.setup_logging('pyrocko.topo.srtmgl3', 'info') 

165 

166 if len(sys.argv) != 2: 

167 sys.exit('usage: python -m pyrocko.topo.srtmgl3 download') 

168 

169 if sys.argv[1] == 'download': 

170 srtmgl3 = SRTMGL3() 

171 srtmgl3.download()