Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More robust lon/lat initialization for imagestack #47

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
10 changes: 5 additions & 5 deletions pynetcf/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ def __init__(self, filename, grid=None, times=None,
self.time_units = "days since 1900-01-01"
self.time_chunksize = 1
self.lon_chunksize = 1
self.lat_chunksize = self.grid.lat2d.shape[1]
super(ImageStack, self).__init__(filename, name=name, mode=mode)

if self.mode == 'w':
Expand All @@ -173,10 +172,11 @@ def __init__(self, filename, grid=None, times=None,
elif self.mode in ['a', 'r']:
self._load_grid()
self._load_variables()
self.lat_chunksize = self.grid.ulon.__len__()

def _init_dimensions(self):
self.create_dim('lon', self.grid.lon2d.shape[0])
self.create_dim('lat', self.grid.lat2d.shape[1])
self.create_dim('lon', self.grid.ulon.__len__())
self.create_dim('lat', self.grid.ulat.__len__())
self.create_dim('time', len(self.times))

def _load_grid(self):
Expand Down Expand Up @@ -209,13 +209,13 @@ def _init_time(self):

def _init_location_variables(self):
# write station information, longitude, latitude and altitude
self.write_var('lon', data=self.grid.lon2d[:, 0], dim='lon',
self.write_var('lon', data=self.grid.ulon, dim='lon',
attr={'standard_name': 'longitude',
'long_name': 'location longitude',
'units': 'degrees_east',
'valid_range': (-180.0, 180.0)},
dtype=np.float)
self.write_var('lat', data=self.grid.lat2d[0, :], dim='lat',
self.write_var('lat', data=self.grid.ulat[::-1], dim='lat',
attr={'standard_name': 'latitude',
'long_name': 'location latitude',
'units': 'degrees_north',
Expand Down
17 changes: 11 additions & 6 deletions tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def setUp(self):
Define test file.
"""
self.testfilename = os.path.join(mkdtemp(), 'test.nc')
self.grid = grids.genreg_grid()
self.grid = grids.genreg_grid(0.25, 0.25).to_cell_grid(5)

def tearDown(self):
"""
Expand All @@ -63,16 +63,21 @@ def test_io(self):
with ncdata.ImageStack(self.testfilename, self.grid,
[datetime(2007, 1, 1),
datetime(2007, 1, 2)], mode="w") as nc:
nc[14] = {'variable': [141, 142]}
nc.write_ts([22, 23], {'variable': [[221, 222], [231, 232]]})
orig_gpis = [0, 1439, 719 * 1440]
orig_lon, orig_lat = self.grid.gpi2lonlat(orig_gpis)
nc[orig_gpis[0]] = {'variable': [141, 142]}
nc.write_ts(orig_gpis[1:2], {'variable': [[221, 222], [231, 232]]})

with ncdata.ImageStack(self.testfilename, self.grid) as nc:
data = nc[14]
with ncdata.ImageStack(self.testfilename) as nc:

test_gpis = nc.grid.find_nearest_gpi(orig_lon, orig_lat)[0]
data = nc[test_gpis[0]]
assert list(data['variable'].values) == [141, 142]
data = nc[22]
data = nc[test_gpis[1:2]]
assert list(data['variable'].values) == [221, 222]



class ArrayStackTests(unittest.TestCase):

def setUp(self):
Expand Down