Skip to content

Commit

Permalink
Merge pull request #11 from TUW-GEO/fix-netcdf-storage
Browse files Browse the repository at this point in the history
Fix netcdf storage
  • Loading branch information
cpaulik committed Apr 16, 2015
2 parents 6e103af + c1df68f commit 5e7780f
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 46 deletions.
64 changes: 39 additions & 25 deletions docs/Creating and working with grid objects.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {
"collapsed": false
},
Expand All @@ -30,7 +30,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 2,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -63,7 +63,7 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": 3,
"metadata": {
"collapsed": false
},
Expand All @@ -82,7 +82,7 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 4,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -119,7 +119,7 @@
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": 5,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -149,7 +149,7 @@
},
{
"cell_type": "code",
"execution_count": 46,
"execution_count": 6,
"metadata": {
"collapsed": false
},
Expand All @@ -160,7 +160,7 @@
"True"
]
},
"execution_count": 46,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -180,7 +180,7 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 7,
"metadata": {
"collapsed": false
},
Expand All @@ -207,7 +207,7 @@
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": 8,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -248,7 +248,7 @@
},
{
"cell_type": "code",
"execution_count": 57,
"execution_count": 9,
"metadata": {
"collapsed": false
},
Expand All @@ -257,10 +257,11 @@
"name": "stdout",
"output_type": "stream",
"text": [
"[ 17.29906911 -35.38535729 -71.93633461 -45.50119896 21.44038298\n",
" -51.8807349 -53.74357805 86.06241248 80.23097461 -70.56187193]\n",
"[ 117.908495 55.71301743 -107.521213 -167.90819856 70.20423657\n",
" -153.14746009 -12.72514138 -44.59386791 25.45141516 3.75179355]\n"
"[ -1.54836122e+01 -1.35887580e+00 -4.78383560e-02 4.78709682e+00\n",
" 1.11369002e+01 3.12077283e+01 -8.71094503e+01 2.20725149e+00\n",
" 6.64563916e+01 7.87705109e+01]\n",
"[ 121.05539154 111.61193334 -131.22315478 -46.73641204 15.1126928\n",
" -165.14751769 -115.24386825 -56.14158745 9.9695647 143.61467711]\n"
]
}
],
Expand All @@ -284,7 +285,7 @@
},
{
"cell_type": "code",
"execution_count": 59,
"execution_count": 10,
"metadata": {
"collapsed": false
},
Expand All @@ -293,7 +294,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"[281 455 583 469 241 506 520 13 20 594]\n"
"[390 353 328 301 271 181 618 300 90 68]\n"
]
}
],
Expand All @@ -311,7 +312,7 @@
},
{
"cell_type": "code",
"execution_count": 65,
"execution_count": 11,
"metadata": {
"collapsed": false
},
Expand All @@ -320,8 +321,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"[ 15. -35. -75. -45. 25. -55. -55. 85. 85. -75.]\n",
"[ 115. 55. -105. -165. 75. -155. -15. -45. 25. 5.]\n"
"[-15. -5. -5. 5. 15. 35. -85. 5. 65. 75.]\n",
"[ 125. 115. -135. -45. 15. -165. -115. -55. 5. 145.]\n"
]
}
],
Expand All @@ -341,7 +342,7 @@
},
{
"cell_type": "code",
"execution_count": 67,
"execution_count": 12,
"metadata": {
"collapsed": false
},
Expand All @@ -353,7 +354,7 @@
},
{
"cell_type": "code",
"execution_count": 75,
"execution_count": 13,
"metadata": {
"collapsed": true
},
Expand All @@ -364,12 +365,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"source": []
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"loadedgrid == randgrid"
]
}
],
"metadata": {
Expand Down
27 changes: 20 additions & 7 deletions docs/Creating and working with grid objects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,11 @@ calculate a lookup table once and reuse it later.
.. parsed-literal::
[ 17.29906911 -35.38535729 -71.93633461 -45.50119896 21.44038298
-51.8807349 -53.74357805 86.06241248 80.23097461 -70.56187193]
[ 117.908495 55.71301743 -107.521213 -167.90819856 70.20423657
-153.14746009 -12.72514138 -44.59386791 25.45141516 3.75179355]
[ -1.54836122e+01 -1.35887580e+00 -4.78383560e-02 4.78709682e+00
1.11369002e+01 3.12077283e+01 -8.71094503e+01 2.20725149e+00
6.64563916e+01 7.87705109e+01]
[ 121.05539154 111.61193334 -131.22315478 -46.73641204 15.1126928
-165.14751769 -115.24386825 -56.14158745 9.9695647 143.61467711]
Now lets calculate a lookup table to the regular 10x10° grid we created
Expand All @@ -170,7 +171,7 @@ earlier
.. parsed-literal::
[281 455 583 469 241 506 520 13 20 594]
[390 353 328 301 271 181 618 300 90 68]
The lookup table contains the grid point indices of the other grid,
Expand All @@ -185,8 +186,8 @@ autogrid in this case.
.. parsed-literal::
[ 15. -35. -75. -45. 25. -55. -55. 85. 85. -75.]
[ 115. 55. -105. -165. 75. -155. -15. -45. 25. 5.]
[-15. -5. -5. 5. 15. 35. -85. 5. 65. 75.]
[ 125. 115. -135. -45. 15. -165. -115. -55. 5. 145.]
Storing and loading grids
Expand All @@ -203,3 +204,15 @@ Grids can be stored to disk as CF compliant netCDF files
loadedgrid = nc.load_grid('example.nc')
.. code:: python
loadedgrid == randgrid
.. parsed-literal::
True
20 changes: 16 additions & 4 deletions pygeogrids/grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import pygeogrids.nearest_neighbor as NN

import numpy as np
import numpy.testing as nptest
try:
from itertools import izip as zip
except ImportError:
Expand Down Expand Up @@ -74,6 +75,8 @@ class BasicGrid(object):
if set (default) then the kdTree for nearest neighbour
search will be built on initialization
shape : tuple, optional
The regular shape of the array along dimensions (lon, lat).
e.g. for a 1x1 degree global regular grid the shape would be (360,180).
if given the grid can be reshaped into the given shape
this indicates that it is a regular grid and fills the
attributes self.londim and self.latdim which
Expand All @@ -86,9 +89,9 @@ class BasicGrid(object):
Attributes
----------
arrlon : numpy.array
array of all longitudes of the grid
1D array of all longitudes of the grid
arrlat : numpy.array
array of all latitudes of the grid
1D array of all latitudes of the grid
n_gpi : int
number of gpis in the grid
gpidirect : boolean
Expand Down Expand Up @@ -538,8 +541,17 @@ def __eq__(self, other):
-------
result : boolean
"""
lonsame = np.all(self.arrlon == other.arrlon)
latsame = np.all(self.arrlat == other.arrlat)
# only test to certain significance for float variables
try:
nptest.assert_allclose(self.arrlon, other.arrlon)
lonsame = True
except AssertionError:
lonsame = False
try:
nptest.assert_allclose(self.arrlat, other.arrlat)
latsame = True
except AssertionError:
latsame = False
gpisame = np.all(self.gpis == other.gpis)
if self.subset is not None and other.subset is not None:
subsetsame = np.all(self.subset == other.subset)
Expand Down
8 changes: 4 additions & 4 deletions pygeogrids/netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def save_lonlat(filename, arrlon, arrlat, arrcell=None,
type(global_attrs['shape']) is not int and
len(global_attrs['shape']) == 2):

latsize = global_attrs['shape'][0]
lonsize = global_attrs['shape'][1]
latsize = global_attrs['shape'][1]
lonsize = global_attrs['shape'][0]
ncfile.createDimension("lat", latsize)
ncfile.createDimension("lon", lonsize)
arrlat = np.unique(arrlat)[::-1] # sorts arrlat descending
Expand Down Expand Up @@ -118,7 +118,7 @@ def save_lonlat(filename, arrlon, arrlat, arrcell=None,
setattr(gpi, 'valid_range', [np.min(gpivalues), np.max(gpivalues)])
gpidirect = 0x0b

latitude = ncfile.createVariable('lat', np.dtype('float32').char,
latitude = ncfile.createVariable('lat', np.dtype('float64').char,
dim[0])
latitude[:] = arrlat
setattr(latitude, 'long_name', 'Latitude')
Expand All @@ -130,7 +130,7 @@ def save_lonlat(filename, arrlon, arrlat, arrcell=None,
londim = dim[1]
else:
londim = dim[0]
longitude = ncfile.createVariable('lon', np.dtype('float32').char,
longitude = ncfile.createVariable('lon', np.dtype('float64').char,
londim)
longitude[:] = arrlon
setattr(longitude, 'long_name', 'Longitude')
Expand Down
34 changes: 28 additions & 6 deletions tests/test_netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ def setUp(self):
self.subset = np.sort(np.random.choice(np.arange(self.lats.size),
size=500, replace=False))
self.basic = grids.BasicGrid(self.lons, self.lats, subset=self.subset,
shape=(180, 360))
self.basic_irregular = grids.BasicGrid(self.lons, self.lats,
shape=(360, 180))
self.basic_generated = grids.genreg_grid(1, 1)
self.basic_irregular = grids.BasicGrid(np.random.random(360 * 180) * 360 - 180,
np.random.random(
360 * 180) * 180 - 90,
subset=self.subset)
self.cellgrid = grids.CellGrid(self.lons, self.lats, self.cells,
subset=self.subset)
Expand Down Expand Up @@ -87,17 +90,36 @@ def test_save_basicgrid_nc(self):
nptest.assert_array_equal(self.subset,
np.where(nc_data.variables['subset_flag'][:].flatten() == 1)[0])
assert nc_data.test == 'test_attribute'
assert nc_data.shape[0] == 180
assert nc_data.shape[1] == 360
assert nc_data.shape[1] == 180
assert nc_data.shape[0] == 360

def test_save_basicgrid_generated(self):
grid_nc.save_grid(self.testfile,
self.basic,
global_attrs={'test': 'test_attribute'})

with Dataset(self.testfile) as nc_data:
nptest.assert_array_equal(np.unique(self.lats)[::-1],
nc_data.variables['lat'][:])
nptest.assert_array_equal(np.unique(self.lons),
nc_data.variables['lon'][:])

nptest.assert_array_equal(self.subset,
np.where(nc_data.variables['subset_flag'][:].flatten() == 1)[0])
assert nc_data.test == 'test_attribute'
assert nc_data.shape[1] == 180
assert nc_data.shape[0] == 360

def test_save_basicgrid_irregular_nc(self):
grid_nc.save_grid(self.testfile,
self.basic_irregular,
global_attrs={'test': 'test_attribute'})

with Dataset(self.testfile) as nc_data:
nptest.assert_array_equal(self.lats, nc_data.variables['lat'][:])
nptest.assert_array_equal(self.lons, nc_data.variables['lon'][:])
nptest.assert_array_equal(
self.basic_irregular.arrlat, nc_data.variables['lat'][:])
nptest.assert_array_equal(
self.basic_irregular.arrlon, nc_data.variables['lon'][:])
nptest.assert_array_equal(self.subset,
np.where(nc_data.variables['subset_flag'][:] == 1)[0])
assert nc_data.test == 'test_attribute'
Expand Down

0 comments on commit 5e7780f

Please sign in to comment.