def _create_test_grid_header1(self): # A 190x130 grid with frac_width=19, frac_height=5 # # This means 10 horiz fractions and 26 vert fractions # with truncated fractions at the borders mod_pix_size = (231.65, -231.65) geot = (-1441428.76, mod_pix_size[0], 0, -480362.62, 0, mod_pix_size[1]) width = 200 height = 180 timestamps = [1, 2] meta = {'meta1': 'foobar'} header = jgrid3.Header(self.grid_root, width=width, height=height, shape=[height, width, len(timestamps)], frac_width=50, frac_height=60, dtype=np.float32, sr_wkt=WGS84_WKT, geot=geot, timestamps_ms=timestamps, meta=meta) assert header.num_x_fracs == 4 assert header.num_y_fracs == 3 assert header.num_fracs == 12 return header
def test_latlng2xy(self): # Earthexplorer is useful # http://earthexplorer.usgs.gov/ # geotransform for h19v08 geot = (1111950.519667, 231.65635826374995, 0.0, 1111950.519667, 0.0, -231.65635826395834) width, height, timestamps = 4800, 4800, [1, 2] header = jgrid3.Header(self.grid_root, width=width, height=height, shape=[height, width, len(timestamps)], frac_width=200, frac_height=200, sr_wkt=MODIS_SIN_WKT, dtype=np.float32, geot=geot, timestamps_ms=timestamps) min_lat, min_lng = header.xy2latlng((0, header.height)) max_lat, max_lng = header.xy2latlng((header.width, 0)) for i in xrange(10): lat = random.uniform(min_lat, max_lat) lng = random.uniform(min_lng, max_lng) xy = header.latlng2xy((lat, lng)) lat2, lng2 = header.xy2latlng(xy) # Since x, y are rounded to int, we can have an error of the # angle size of one pixel tol = (max_lng - min_lng) / 4800. assert np.allclose(lat, lat2, atol=tol) assert np.allclose(lng, lng2, atol=tol)
def test_load_slice(self): """Test reading a slice of a grid""" frac_width = 19 frac_height = 5 width = 190 height = 130 ndates = 11 data = np.random.uniform(size=(height, width, ndates)) geot = (0, 1, 0, 0, 0, 1) timestamps = list(np.arange(ndates)) header = jgrid3.Header(self.grid_root, width=width, height=height, shape=[height, width, len(timestamps)], frac_width=frac_width, frac_height=frac_height, frac_ndates=3, dtype=data.dtype, sr_wkt=WGS84_WKT, geot=geot, timestamps_ms=timestamps) header.write_all(data) def _do_xy(xy_from, xy_to): data2 = header.load_slice_xy(xy_from, xy_to) data_slice = data[xy_from[1]:xy_to[1], xy_from[0]:xy_to[0]] assert np.allclose(data_slice, data2, atol=1e-3) _do_xy((50, 60), (149, 119)) _do_xy((49, 60), (149, 119)) _do_xy((49, 60), (150, 120)) _do_xy((150, 120), (189, 129))
def test_write_load_frac_by_num(self): """Test read/write of single fraction""" frac_width = 6 frac_height = 8 frac_ndates = 2 timestamps = [1, 2, 3, 5, 6] data = np.random.uniform(size=(frac_height, frac_width, len(timestamps))) geot = (0, 1, 0, 0, 0, 1) width = 60 height = 48 header = jgrid3.Header(self.grid_root, width=width, height=height, shape=(height, width, len(timestamps)), frac_width=frac_width, frac_height=frac_height, frac_ndates=frac_ndates, dtype=data.dtype, sr_wkt=WGS84_WKT, geot=geot, timestamps_ms=timestamps) frac_num = 0 header.write_frac_by_num(frac_num, data) data2 = header.load_frac_by_num(frac_num) assert data.dtype == data2.dtype assert np.allclose(data, data2, atol=1e-3)
def test_load_slice_latlng(self): frac_width = 19 frac_height = 5 width = 190 height = 130 ndates = 2 data = np.random.uniform(size=(height, width, ndates)) geot = (0, 1, 0, 0, 0, -1) timestamps = [1, 2] header = jgrid3.Header(self.grid_root, width=width, height=height, shape=[height, width, len(timestamps)], frac_width=frac_width, frac_height=frac_height, dtype=data.dtype, sr_wkt=WGS84_WKT, geot=geot, timestamps_ms=timestamps) header.write_all(data) # In this case, max_lat > min_lat, because the lat axis growth in # the opposite (down to up) than the y axis (up to down) min_lat, min_lng = header.xy2latlng((0, 0)) max_lat, max_lng = header.xy2latlng((header.width, header.height)) pix_size_deg = ((max_lat - min_lat) / float(height), (max_lng - min_lng) / float(width)) data2, xy_from = header.load_slice_latlng( (min_lat + pix_size_deg[0] * 115, min_lng + pix_size_deg[1] * 1), (min_lat + pix_size_deg[0] * 125, min_lng + pix_size_deg[1] * 12)) assert xy_from == (1, 115) assert data2.shape[:2] == (10, 11) data_slice = data[115:125, 1:12] assert np.allclose(data_slice, data2, atol=1e-3)