You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@climate.apache.org by bo...@apache.org on 2013/08/09 17:59:34 UTC

svn commit: r1512362 - /incubator/climate/branches/RefactorInput/ocw/tests/test_local.py

Author: boustani
Date: Fri Aug  9 15:59:34 2013
New Revision: 1512362

URL: http://svn.apache.org/r1512362
Log:
first unittest for some of local.py functions

Added:
    incubator/climate/branches/RefactorInput/ocw/tests/test_local.py

Added: incubator/climate/branches/RefactorInput/ocw/tests/test_local.py
URL: http://svn.apache.org/viewvc/incubator/climate/branches/RefactorInput/ocw/tests/test_local.py?rev=1512362&view=auto
==============================================================================
--- incubator/climate/branches/RefactorInput/ocw/tests/test_local.py (added)
+++ incubator/climate/branches/RefactorInput/ocw/tests/test_local.py Fri Aug  9 15:59:34 2013
@@ -0,0 +1,120 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import unittest
+import numpy
+import numpy.ma as ma
+import os
+import netCDF4
+import datetime
+from data_source.local import *
+
+
+class test_load_ile(unittest.TestCase):
+
+
+    def setUp(self):
+        #To create the temporary netCDF file
+        self.file_path = os.getcwd() + '/temporaryNetcdf.nc'
+        netCDF_file = netCDF4.Dataset(self.file_path, 'w',  format='NETCDF4')
+        #To create dimensions
+        netCDF_file.createDimension('lat_dim', 5)
+        netCDF_file.createDimension('lon_dim', 5)
+        netCDF_file.createDimension('time_dim', 3)
+        netCDF_file.createDimension('level_dim', 2)
+        #To create variables
+        latitudes = netCDF_file.createVariable('latitude', 'd', ('lat_dim',))
+        longitudes = netCDF_file.createVariable('longitude', 'd', ('lon_dim',))
+        times = netCDF_file.createVariable('time', 'd', ('time_dim',))
+        levels = netCDF_file.createVariable('level', 'd', ('level_dim',))
+        values = netCDF_file.createVariable('value', 'd', ('level_dim', 'time_dim', 'lat_dim', 'lon_dim'))
+        #To latitudes and longitudes for five values
+        self.latitudes = [i for i in range(0,5)]
+        self.longitudes = [i for i in range(200,205)]
+        #Three months of data
+        self.times = [month for month in range(3)]
+        #Two levels
+        self.levels = [100, 200]
+        #Create 150 values
+        self.values = numpy.array([i for i in range(150)])
+        #Reshape values to 4D array (level, time, lats, lons)
+        self.values = self.values.reshape(len(self.levels), len(self.times),len(self.latitudes),len(self.longitudes))
+        #Ingest values to netCDF file
+        latitudes[:] = self.latitudes
+        longitudes[:] = self.longitudes
+        times[:] = self.times
+        levels[:] = self.levels
+        values[:] = self.values
+        #Assigne time info to time variable
+        netCDF_file.variables['time'].units = 'months since 2001-01-01 00:00:00' 
+        netCDF_file.close()
+        #Read netCDF file
+        self.netCDF_file = netCDF4.Dataset(self.file_path, 'r')
+        self.user_value_variable_name = 'value'
+        self.variable_name_list = ['latitude', 'longitude', 'time', 'level', 'value']
+        self.possible_value_name = ['latitude', 'longitude', 'time', 'level']
+
+
+    def tearDown(self):
+        '''To remove the temporary netCDF file'''
+        os.remove(self.file_path)
+
+
+    def test_function_get_lat_name(self):
+        '''To test get_lat_name function'''
+        self.assertItemsEqual(get_lat_name(self.variable_name_list), self.variable_name_list[0])
+
+
+    def test_function_get_lon_name(self):
+        '''To test get_lon_name function'''
+        self.assertItemsEqual(get_lon_name(self.variable_name_list), self.variable_name_list[1])
+
+
+    def test_function_get_time_name(self):
+        '''To test get_time_name function'''
+        self.assertItemsEqual(get_time_name(self.variable_name_list), self.variable_name_list[2])
+
+
+    def test_function_get_level_name(self):
+        '''To test get_level_name function'''
+        self.assertItemsEqual(get_level_name(self.variable_name_list), self.variable_name_list[3])
+
+
+    def test_function_get_time_step(self):
+        '''To test get_time_step function'''
+        self.assertItemsEqual(get_time_step(self.netCDF_file, self.variable_name_list[2]), ('months', 'months since 2001-01-01 00:00:00', 12))
+
+
+    def test_function_get_time_base(self):
+        '''To test get_time_base function'''
+        self.assertEqual(get_time_base('months since 2001-01-01 00:00:00', 12), datetime(2001,01,01))
+
+
+    def test_function_calculate_time(self):
+        '''To test calculate_time function'''
+        newTimes = datetime(2001,01,01), datetime(2001,02,01), datetime(2001,03,01)
+        self.assertItemsEqual(calculate_time(self.netCDF_file, self.times, self.variable_name_list[2]), newTimes)
+
+'''
+    def test_function_load_file(self):
+        print self.file_path
+        self.assertEqual(load_file('/Users/Mboustani/Documents/python/program/ocw2/temporaryNetcdf.nc', None), Dataset())
+'''
+
+
+if __name__ == '__main__':
+    unittest.main()
\ No newline at end of file