climate-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From huiky...@apache.org
Subject [1/4] climate git commit: CLIMATE-821 - write_netcdf() assumes lat and lon are 1D arrays
Date Fri, 15 Jul 2016 15:47:37 GMT
Repository: climate
Updated Branches:
  refs/heads/master d58034c99 -> ffd21590d


CLIMATE-821 - write_netcdf() assumes lat and lon are 1D arrays


Project: http://git-wip-us.apache.org/repos/asf/climate/repo
Commit: http://git-wip-us.apache.org/repos/asf/climate/commit/427450d5
Tree: http://git-wip-us.apache.org/repos/asf/climate/tree/427450d5
Diff: http://git-wip-us.apache.org/repos/asf/climate/diff/427450d5

Branch: refs/heads/master
Commit: 427450d50d9339bc27bde98a0972528438549a6d
Parents: 83ed37f
Author: Alex Goodman <agoodm@users.noreply.github.com>
Authored: Tue Jun 28 18:33:30 2016 -0700
Committer: Alex Goodman <agoodm@users.noreply.github.com>
Committed: Tue Jun 28 18:33:30 2016 -0700

----------------------------------------------------------------------
 ocw/dataset_processor.py            | 20 +++++++++++++++-----
 ocw/tests/test_dataset_processor.py | 22 +++++++++++++++++++---
 2 files changed, 34 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/climate/blob/427450d5/ocw/dataset_processor.py
----------------------------------------------------------------------
diff --git a/ocw/dataset_processor.py b/ocw/dataset_processor.py
index 9ab4e50..a112ab8 100755
--- a/ocw/dataset_processor.py
+++ b/ocw/dataset_processor.py
@@ -569,9 +569,19 @@ def write_netcdf(dataset, path, compress=True):
     '''
     out_file = netCDF4.Dataset(path, 'w', format='NETCDF4')
 
-    # Set attribute lenghts
-    lat_len = len(dataset.lats)
-    lon_len = len(dataset.lons)
+    # Set attribute lengths
+    if dataset.lats.ndim == 2:
+        lat_len = dataset.lats.shape[0]
+        lon_len = dataset.lons.shape[1]
+        lat_dim_info = ('lat', 'lon')
+        lon_dim_info = ('lat', 'lon')
+
+    else:
+        lat_len = len(dataset.lats)
+        lon_len = len(dataset.lons)
+        lat_dim_info = ('lat',)
+        lon_dim_info = ('lon',)
+
     time_len = len(dataset.times)
 
     # Create attribute dimensions
@@ -580,8 +590,8 @@ def write_netcdf(dataset, path, compress=True):
     out_file.createDimension('time', time_len)
 
     # Create variables
-    lats = out_file.createVariable('lat', 'f8', ('lat',), zlib=compress)
-    lons = out_file.createVariable('lon', 'f8', ('lon',), zlib=compress)
+    lats = out_file.createVariable('lat', 'f8', lat_dim_info, zlib=compress)
+    lons = out_file.createVariable('lon', 'f8', lon_dim_info, zlib=compress)
     times = out_file.createVariable('time', 'f8', ('time',), zlib=compress)
 
     var_name = dataset.variable if dataset.variable else 'var'

http://git-wip-us.apache.org/repos/asf/climate/blob/427450d5/ocw/tests/test_dataset_processor.py
----------------------------------------------------------------------
diff --git a/ocw/tests/test_dataset_processor.py b/ocw/tests/test_dataset_processor.py
index eb39ba6..9060070 100644
--- a/ocw/tests/test_dataset_processor.py
+++ b/ocw/tests/test_dataset_processor.py
@@ -625,6 +625,7 @@ class TestFailingSubset(unittest.TestCase):
 class TestNetCDFWrite(unittest.TestCase):
     def setUp(self):
         self.ds = ten_year_monthly_dataset()
+        self.ds_2d = ten_year_monthly_dataset(latlon2d=True)
         self.file_name = 'test.nc'
 
     def tearDown(self):
@@ -635,6 +636,14 @@ class TestNetCDFWrite(unittest.TestCase):
         dp.write_netcdf(self.ds, self.file_name)
         self.assertTrue(os.path.isfile(self.file_name))
 
+    def test_file_write_2d(self):
+        dp.write_netcdf(self.ds_2d, self.file_name)
+        self.assertTrue(os.path.isfile(self.file_name))
+
+    def test_file_write(self):
+        dp.write_netcdf(self.ds, self.file_name)
+        self.assertTrue(os.path.isfile(self.file_name))
+
     def test_that_file_contents_are_valid(self):
         dp.write_netcdf(self.ds, self.file_name)
         new_ds = local.load_file(self.file_name, self.ds.variable)
@@ -646,15 +655,22 @@ class TestNetCDFWrite(unittest.TestCase):
         np.testing.assert_array_equal(self.ds.values, new_ds.values)
 
 
-def ten_year_monthly_dataset():
+def ten_year_monthly_dataset(latlon2d=False):
     lats = np.array(range(-89, 90, 2))
     lons = np.array(range(-179, 180, 2))
+    # Need separate variable for input lats / lons because dataset only
+    # makes shallow copies of them.
+    ilats, ilons = lats, lons
+    # For testing 2D lat lon grids
+    if latlon2d:
+        lons2, lats2 = np.meshgrid(lons, lats)
+        ilats, ilons = lats2, lons2
     # Ten Years of monthly data
     times = np.array([datetime.datetime(year, month, 1)
                       for year in range(2000, 2010) for month in range(1, 13)])
     values = np.ones([len(times), len(lats), len(lons)])
-    input_dataset = ds.Dataset(lats,
-                               lons,
+    input_dataset = ds.Dataset(ilats,
+                               ilons,
                                times,
                                values,
                                variable="test variable name",


Mime
View raw message