1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
5from __future__ import absolute_import
7import zipfile
8import os.path as op
10import numpy as num
12from . import tile, dataset
14try:
15 range = xrange
16except NameError:
17 pass
19citation = '''
20Amante, C. and B.W. Eakins, 2009. ETOPO1 1 Arc-Minute Global Relief Model:
21Procedures, Data Sources and Analysis. NOAA Technical Memorandum NESDIS
22NGDC-24. National Geophysical Data Center, NOAA. doi:10.7289/V5C8276M
23[access date].
24'''
27class ETOPO1(dataset.TiledGlobalDataset):
29 def __init__(
30 self,
31 name='ETOPO1',
32 data_dir=op.join(op.dirname(__file__), 'data', 'ETOPO1'),
33 base_fn='etopo1_ice_g_i2',
34 raw_data_url=('https://mirror.pyrocko.org/www.ngdc.noaa.gov/mgg'
35 '/global/relief/ETOPO1/data/ice_surface/'
36 'grid_registered/binary/%s.zip')):
38 dataset.TiledGlobalDataset.__init__(
39 self,
40 name,
41 21601, 10801, 1351, 1351,
42 num.dtype('<i2'),
43 data_dir=data_dir,
44 citation=citation)
46 self.base_fn = base_fn
47 self.raw_data_url = raw_data_url
49 def download(self):
50 fpath = op.join(self.data_dir, '%s.zip' % self.base_fn)
51 if not op.exists(fpath):
52 self.download_file(
53 self.raw_data_url % self.base_fn, fpath)
55 self.make_tiles()
57 def make_tiles(self):
58 fpath = op.join(self.data_dir, '%s.zip' % self.base_fn)
60 zipf = zipfile.ZipFile(fpath, 'r')
61 rawdata = zipf.read('%s.bin' % self.base_fn)
62 zipf.close()
63 data = num.fromstring(rawdata, dtype=self.dtype)
64 assert data.size == self.nx * self.ny
65 data = data.reshape((self.ny, self.nx))[::-1]
67 for ity in range(self.ntilesy):
68 for itx in range(self.ntilesx):
69 tiledata = data[ity*(self.nty-1):(ity+1)*(self.nty-1)+1,
70 itx*(self.ntx-1):(itx+1)*(self.ntx-1)+1]
72 fn = '%s.%02i.%02i.bin' % (self.base_fn, ity, itx)
73 fpath = op.join(self.data_dir, fn)
74 with open(fpath, 'w') as f:
75 tiledata.tofile(f)
77 def get_tile(self, itx, ity):
78 assert 0 <= itx < self.ntilesx
79 assert 0 <= ity < self.ntilesy
81 fn = '%s.%02i.%02i.bin' % (self.base_fn, ity, itx)
82 fpath = op.join(self.data_dir, fn)
83 if not op.exists(fpath):
84 self.download()
86 with open(fpath, 'r') as f:
87 data = num.fromfile(f, dtype=self.dtype)
89 assert data.size == self.ntx*self.nty
90 data = data.reshape((self.ntx, self.nty))
92 return tile.Tile(
93 self.xmin + itx*self.stx,
94 self.ymin + ity*self.sty,
95 self.dx, self.dx, data)