Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add unit test

  • Loading branch information...
commit cb5112caeff414e11c7789d027f40a1e75f03881 1 parent 8d1630d
@ismailsunni ismailsunni authored
View
1  safe/common/testing.py
@@ -37,6 +37,7 @@ def _show_system_info(self):
TESTDATA = os.path.join(DATADIR, 'test') # Artificial datasets
HAZDATA = os.path.join(DATADIR, 'hazard') # Real hazard layers
EXPDATA = os.path.join(DATADIR, 'exposure') # Real exposure layers
+BOUNDDATA = os.path.join(DATADIR, 'boundaries') # Real exposure layers
UNITDATA = os.path.abspath(
os.path.join(os.path.dirname(__file__),
View
114 safe/storage/converter.py
@@ -0,0 +1,114 @@
+"""**Class Converter**
+
+Provide function for convert a file type to another Raster or Vector.
+"""
+
+import numpy
+import os
+from Scientific.IO.NetCDF import NetCDFFile
+
+from safe.common.utilities import unique_filename
+
+from raster import Raster
+from utilities import raster_geometry2geotransform
+
+
+def convert_netcdf2tif(filename, n, verbose=True):
+ """Convert netcdf to tif aggregating first n bands
+
+ Args
+ * filename: NetCDF multiband raster with extension .nc
+ * n: Positive integer determining how many bands to use
+ * verbose : if true, print all information
+
+ Returns
+ * Raster file in tif format. Each pixel will be the maximum
+ of that pixel in the first n bands in the input file.
+
+ Note : Adapted from scripts/netcdf2tif.py
+
+ """
+
+ if not isinstance(filename, basestring):
+ msg = 'Argument filename should be a string. I got %s' % filename
+ raise RuntimeError(msg)
+
+ basename, ext = os.path.splitext(filename)
+ msg = ('Expected NetCDF file with extension .nc - '
+ 'Instead I got %s' % filename)
+ if ext != '.nc':
+ raise RuntimeError(msg)
+
+ try:
+ n = int(n)
+ except:
+ msg = 'Argument N should be an integer. I got %s' % n
+ raise RuntimeError(msg)
+
+ # print filename, n
+
+ # Read NetCDF file
+ fid = NetCDFFile(filename)
+ if verbose:
+ dimensions = fid.dimensions.keys()
+ variables = fid.variables.keys()
+
+ title = getattr(fid, 'title')
+ institution = getattr(fid, 'institution')
+ source = getattr(fid, 'source')
+ history = getattr(fid, 'history')
+ references = getattr(fid, 'references')
+ conventions = getattr(fid, 'Conventions')
+ coordinate_system = getattr(fid, 'coordinate_system')
+
+ print 'Read from %s' % filename
+ print 'Title: %s' % title
+ print 'Institution: %s' % institution
+ print 'Source: %s' % source
+ print 'History: %s' % history
+ print 'References: %s' % references
+ print 'Conventions: %s' % conventions
+ print 'Coordinate system: %s' % coordinate_system
+
+ print 'Dimensions: %s' % dimensions
+ print 'Variables: %s' % variables
+
+ # Get data
+ x = fid.variables['x'][:]
+ y = fid.variables['y'][:]
+ _ = fid.variables['time'][:]
+ inundation_depth = fid.variables['Inundation_Depth'][:]
+
+ _ = inundation_depth.shape[0] # Number of time steps
+ M = inundation_depth.shape[1] # Steps in the y direction
+ N = inundation_depth.shape[2] # Steps in the x direction
+
+ # Compute the max of the first n timesteps
+ A = numpy.zeros((M, N), dtype='float')
+ for i in range(n):
+ B = inundation_depth[i, :, :]
+ A = numpy.maximum(A, B)
+
+ geotransform = raster_geometry2geotransform(x, y)
+ if verbose:
+ print 'Geotransform', geotransform
+
+ # Write result to tif file
+ # NOTE: This assumes a default projection (WGS 84, geographic)
+ date = os.path.split(basename)[-1].split('_')[0]
+ if verbose:
+ print 'date', date
+ R = Raster(data=A,
+ geotransform=geotransform,
+ keywords={'category': 'hazard',
+ 'subcategory': 'flood',
+ 'unit': 'm',
+ 'title': ('%d hour flood forecast grid '
+ 'in Jakarta at %s' % (n, date))})
+
+ tif_filename = unique_filename(suffix='.tif')
+ R.write_to_file(tif_filename)
+
+ if verbose:
+ print 'Success: %d hour forecast written to %s' % (n, R.filename)
+ return tif_filename
View
37 safe/storage/test_converter.py
@@ -0,0 +1,37 @@
+"""**Tests for safe common utilities**
+"""
+
+__author__ = 'Ismail Sunni <ismailsunni@yahoo.co.id>'
+__revision__ = '$Format:%H$'
+__date__ = '10/12/2012'
+__license__ = "GPL"
+__copyright__ = 'Copyright 2012, Australia Indonesia Facility for '
+__copyright__ += 'Disaster Reduction'
+
+
+import unittest
+import os
+
+
+from safe.common.testing import (TESTDATA)
+from converter import convert_netcdf2tif
+from core import read_layer
+
+
+class ConverterTest(unittest.TestCase):
+ def test_convert_netcdf2tif(self):
+ """Test convert netfdf file to tif."""
+ netcdf_file_name = "201212080500_Jakarta_200m_Sobek_Forecast_CCAM.nc"
+ netcdf_file_path = os.path.abspath(
+ os.path.join(TESTDATA, netcdf_file_name))
+ num_bands = 12
+ tif_file = convert_netcdf2tif(netcdf_file_path, num_bands, False)
+ print tif_file
+ assert (os.path.isfile(tif_file)), 'File not found'
+ tif_file = read_layer(tif_file)
+ assert tif_file.number_of_bands == 1, 'Number of band is not same'
+ assert tif_file.columns == 160, 'Number of columns is not same'
+ assert tif_file.rows == 162, 'Number of rows is not same'
+
+if __name__ == '__main__':
+ unittest.main()
View
1  safe/storage/test_utilities.py
@@ -120,6 +120,5 @@ def test_read_keywords_simple(self):
self.assertEquals(keywords, expected_keywords, msg)
LOGGER.debug(keywords)
- def test
if __name__ == '__main__':
unittest.main()
View
100 safe/storage/utilities.py
@@ -8,13 +8,12 @@
import math
from ast import literal_eval
from osgeo import ogr
-from Scientific.IO.NetCDF import NetCDFFile
from geometry import Polygon
+
from safe.common.numerics import ensure_numeric
from safe.common.utilities import verify
from safe.common.exceptions import BoundingBoxError, InaSAFEError
-from raster import *
# Default attribute to assign to vector layers
@@ -1090,100 +1089,3 @@ def get_polygondata(G):
# Return Polygon instance
return Polygon(outer_ring=outer_ring,
inner_rings=inner_rings)
-
-def convert_netcdf2tif(filename, n, verbose=True):
- """Convert netcdf to tif aggregating first n bands
-
- Args
- * filename: NetCDF multiband raster with extension .nc
- * n: Positive integer determining how many bands to use
- * verbose : if true print information related to the file
-
- Returns
- * Raster file in tif format. Each pixel will be the maximum
- of that pixel in the first n bands in the input file.
-
- """
- if not isinstance(filename, basestring):
- msg = 'Argument filename should be a string. I got %s' % filename
- raise RuntimeError(msg)
-
- basename, ext = os.path.splitext(filename)
- msg = ('Expected NetCDF file with extension .nc - '
- 'Instead I got %s' % filename)
- if ext != '.nc':
- raise RuntimeError(msg)
-
- try:
- n = int(n)
- except:
- msg = 'Argument N should be an integer. I got %s' % n
- raise RuntimeError(msg)
-
- print filename, n
-
- # Read NetCDF file
- fid = NetCDFFile(filename)
- dimensions = fid.dimensions.keys()
- variables = fid.variables.keys()
-
- title = getattr(fid, 'title')
- institution = getattr(fid, 'institution')
- source = getattr(fid, 'source')
- history = getattr(fid, 'history')
- references = getattr(fid, 'references')
- conventions = getattr(fid, 'Conventions')
- coordinate_system = getattr(fid, 'coordinate_system')
-
- if verbose:
- print 'Read from %s' % filename
- print 'Title: %s' % title
- print 'Institution: %s' % institution
- print 'Source: %s' % source
- print 'History: %s' % history
- print 'References: %s' % references
- print 'Conventions: %s' % conventions
- print 'Coordinate system: %s' % coordinate_system
-
- print 'Dimensions: %s' % dimensions
- print 'Variables: %s' % variables
-
- # Get data
- x = fid.variables['x'][:]
- y = fid.variables['y'][:]
- t = fid.variables['time'][:]
- inundation_depth = fid.variables['Inundation_Depth'][:]
-
- T = inundation_depth.shape[0] # Number of time steps
- M = inundation_depth.shape[1] # Steps in the y direction
- N = inundation_depth.shape[2] # Steps in the x direction
-
- # Compute the max of the first n timesteps
- A = numpy.zeros((M, N), dtype='float')
- for i in range(n):
- B = inundation_depth[i, :, :]
- A = numpy.maximum(A, B)
-
- geotransform = raster_geometry2geotransform(x, y)
- if verbose:
- print 'Geotransform', geotransform
-
- # Write result to tif file
- # NOTE: This assumes a default projection (WGS 84, geographic)
- date = os.path.split(basename)[-1].split('_')[0]
- if verbose:
- print 'date', date
- R = Raster(data=A,
- geotransform=geotransform,
- keywords={'category': 'hazard',
- 'subcategory': 'flood',
- 'unit': 'm',
- 'title': ('%d hour flood forecast grid '
- 'in Jakarta at %s' % (n, date))})
-
- tif_filename = '%s_%d_hours.tif' % (basename, n)
- R.write_to_file(tif_filename)
-
- if verbose:
- print 'Success: %d hour forecast written to %s' % (n, R.filename)
- return tif_filename
Please sign in to comment.
Something went wrong with that request. Please try again.