Example #1
0
    def test_ferret2sww_lat_longII(self):
        # Test that min lat long works

        #The test file has
        # LON = 150.66667, 150.83334, 151, 151.16667
        # LAT = -34.5, -34.33333, -34.16667, -34 ;

        #Read
        from anuga.coordinate_transforms.redfearn import redfearn
        fid = NetCDFFile(self.test_MOST_file + '_ha.nc')
        first_value = fid.variables['HA'][:][0, 0, 0]
        fourth_value = fid.variables['HA'][:][0, 0, 3]
        fid.close()

        #Call conversion (with zero origin)
        #ferret2sww('small', verbose=self.verbose,
        #           origin = (56, 0, 0))
        try:
            ferret2sww(self.test_MOST_file,
                       verbose=self.verbose,
                       origin=(56, 0, 0),
                       minlat=-34.5,
                       maxlat=-35)
        except AssertionError:
            pass
        else:
            self.assertTrue(0 == 1, 'Bad input did not throw exception error!')
Example #2
0
    def test_ferret2sww_2(self):
        """Test that georeferencing etc works when converting from
        ferret format (lat/lon) to sww format (UTM)
        """

        #The test file has
        # LON = 150.66667, 150.83334, 151, 151.16667
        # LAT = -34.5, -34.33333, -34.16667, -34 ;
        # TIME = 0, 0.1, 0.6, 1.1, 1.6, 2.1 ;
        #
        # First value (index=0) in small_ha.nc is 0.3400644 cm,
        # Fourth value (index==3) is -6.50198 cm

        from anuga.coordinate_transforms.redfearn import redfearn
        #fid = NetCDFFile('small_ha.nc')
        fid = NetCDFFile(self.test_MOST_file + '_ha.nc')

        #Pick a coordinate and a value

        time_index = 1
        lat_index = 0
        lon_index = 2

        test_value = fid.variables['HA'][:][time_index, lat_index, lon_index]
        test_time = fid.variables['TIME'][:][time_index]
        test_lat = fid.variables['LAT'][:][lat_index]
        test_lon = fid.variables['LON'][:][lon_index]

        linear_point_index = lat_index * 4 + lon_index
        fid.close()

        #Call conversion (with zero origin)
        ferret2sww(self.test_MOST_file,
                   verbose=self.verbose,
                   origin=(56, 0, 0))

        #Work out the UTM coordinates for test point
        zone, e, n = redfearn(test_lat, test_lon)

        #Read output file 'small.sww'
        fid = NetCDFFile(self.test_MOST_file + '.sww')

        x = fid.variables['x'][:]
        y = fid.variables['y'][:]

        #Check that test coordinate is correctly represented
        assert num.allclose(x[linear_point_index], e)
        assert num.allclose(y[linear_point_index], n)

        #Check test value
        stage = fid.variables['stage'][:]

        assert num.allclose(stage[time_index, linear_point_index],
                            old_div(test_value, 100))

        fid.close()

        #Cleanup
        import os
        os.remove(self.test_MOST_file + '.sww')
Example #3
0
    def test_ferret2sww_nz_origin(self):
        from anuga.coordinate_transforms.redfearn import redfearn

        #Call conversion (with nonzero origin)
        ferret2sww(self.test_MOST_file,
                   verbose=self.verbose,
                   origin=(56, 100000, 200000))

        #Work out the UTM coordinates for first point
        zone, e, n = redfearn(-34.5, 150.66667)

        #Read output file 'small.sww'
        #fid = NetCDFFile('small.sww', netcdf_mode_r)
        fid = NetCDFFile(self.test_MOST_file + '.sww')

        x = fid.variables['x'][:]
        y = fid.variables['y'][:]

        #Check that first coordinate is correctly represented
        assert num.allclose(x[0], e - 100000)
        assert num.allclose(y[0], n - 200000)

        fid.close()

        #Cleanup
        os.remove(self.test_MOST_file + '.sww')
    def test_ferret2sww_lat_longII(self):
        # Test that min lat long works

        #The test file has
        # LON = 150.66667, 150.83334, 151, 151.16667
        # LAT = -34.5, -34.33333, -34.16667, -34 ;
        
        #Read
        from anuga.coordinate_transforms.redfearn import redfearn
        fid = NetCDFFile(self.test_MOST_file + '_ha.nc')
        first_value = fid.variables['HA'][:][0,0,0]
        fourth_value = fid.variables['HA'][:][0,0,3]
        fid.close()


        #Call conversion (with zero origin)
        #ferret2sww('small', verbose=self.verbose,
        #           origin = (56, 0, 0))
        try:
            ferret2sww(self.test_MOST_file, verbose=self.verbose,
                   origin = (56, 0, 0), minlat=-34.5, maxlat=-35)
        except AssertionError:
            pass
        else:
            self.assertTrue(0 ==1,  'Bad input did not throw exception error!')
    def test_ferret2sww_2(self):
        """Test that georeferencing etc works when converting from
        ferret format (lat/lon) to sww format (UTM)
        """

        #The test file has
        # LON = 150.66667, 150.83334, 151, 151.16667
        # LAT = -34.5, -34.33333, -34.16667, -34 ;
        # TIME = 0, 0.1, 0.6, 1.1, 1.6, 2.1 ;
        #
        # First value (index=0) in small_ha.nc is 0.3400644 cm,
        # Fourth value (index==3) is -6.50198 cm


        from anuga.coordinate_transforms.redfearn import redfearn
        #fid = NetCDFFile('small_ha.nc')
        fid = NetCDFFile(self.test_MOST_file + '_ha.nc')

        #Pick a coordinate and a value

        time_index = 1
        lat_index = 0
        lon_index = 2

        test_value = fid.variables['HA'][:][time_index, lat_index, lon_index]
        test_time = fid.variables['TIME'][:][time_index]
        test_lat = fid.variables['LAT'][:][lat_index]
        test_lon = fid.variables['LON'][:][lon_index]

        linear_point_index = lat_index*4 + lon_index
        fid.close()

        #Call conversion (with zero origin)
        ferret2sww(self.test_MOST_file, verbose=self.verbose,
                   origin = (56, 0, 0))


        #Work out the UTM coordinates for test point
        zone, e, n = redfearn(test_lat, test_lon)

        #Read output file 'small.sww'
        fid = NetCDFFile(self.test_MOST_file + '.sww')

        x = fid.variables['x'][:]
        y = fid.variables['y'][:]

        #Check that test coordinate is correctly represented
        assert num.allclose(x[linear_point_index], e)
        assert num.allclose(y[linear_point_index], n)

        #Check test value
        stage = fid.variables['stage'][:]

        assert num.allclose(stage[time_index, linear_point_index], test_value/100)

        fid.close()

        #Cleanup
        import os
        os.remove(self.test_MOST_file + '.sww')
    def test_ferret2sww_nz_origin(self):
        from anuga.coordinate_transforms.redfearn import redfearn

        #Call conversion (with nonzero origin)
        ferret2sww(self.test_MOST_file, verbose=self.verbose,
                   origin = (56, 100000, 200000))


        #Work out the UTM coordinates for first point
        zone, e, n = redfearn(-34.5, 150.66667)

        #Read output file 'small.sww'
        #fid = NetCDFFile('small.sww', netcdf_mode_r)
        fid = NetCDFFile(self.test_MOST_file + '.sww')

        x = fid.variables['x'][:]
        y = fid.variables['y'][:]

        #Check that first coordinate is correctly represented
        assert num.allclose(x[0], e-100000)
        assert num.allclose(y[0], n-200000)

        fid.close()

        #Cleanup
        os.remove(self.test_MOST_file + '.sww')
Example #7
0
    def test_ferret2sww_lat_longII(self):
        # Test that min lat long works

        #The test file has
        # LON = 150.66667, 150.83334, 151, 151.16667
        # LAT = -34.5, -34.33333, -34.16667, -34 ;

        #Read
        from anuga.coordinate_transforms.redfearn import redfearn
        fid = NetCDFFile(self.test_MOST_file + '_ha.nc')
        first_value = fid.variables['HA'][:][0, 0, 0]
        fourth_value = fid.variables['HA'][:][0, 0, 3]
        fid.close()

        #Call conversion (with zero origin)
        #ferret2sww('small', verbose=False,
        #           origin = (56, 0, 0))
        ferret2sww(self.test_MOST_file,
                   verbose=False,
                   origin=(56, 0, 0),
                   minlat=-34.4,
                   maxlat=-34.2)

        #Work out the UTM coordinates for first point
        zone, e, n = redfearn(-34.5, 150.66667)
        #print zone, e, n

        #Read output file 'small.sww'
        #fid = NetCDFFile('small.sww')
        fid = NetCDFFile(self.test_MOST_file + '.sww')

        x = fid.variables['x'][:]
        y = fid.variables['y'][:]
        #Check that first coordinate is correctly represented
        assert 12 == len(x)

        fid.close()

        #Cleanup
        import os
        os.remove(self.test_MOST_file + '.sww')
    def test_ferret2sww_lat_longII(self):
        # Test that min lat long works

        #The test file has
        # LON = 150.66667, 150.83334, 151, 151.16667
        # LAT = -34.5, -34.33333, -34.16667, -34 ;
        
        #Read
        from anuga.coordinate_transforms.redfearn import redfearn
        fid = NetCDFFile(self.test_MOST_file + '_ha.nc')
        first_value = fid.variables['HA'][:][0,0,0]
        fourth_value = fid.variables['HA'][:][0,0,3]
        fid.close()


        #Call conversion (with zero origin)
        #ferret2sww('small', verbose=False,
        #           origin = (56, 0, 0))
        ferret2sww(self.test_MOST_file, verbose=False,
                   origin = (56, 0, 0), minlat=-34.4, maxlat=-34.2)

        #Work out the UTM coordinates for first point
        zone, e, n = redfearn(-34.5, 150.66667)
        #print zone, e, n

        #Read output file 'small.sww'
        #fid = NetCDFFile('small.sww')
        fid = NetCDFFile(self.test_MOST_file + '.sww')

        x = fid.variables['x'][:]
        y = fid.variables['y'][:]
        #Check that first coordinate is correctly represented
        assert 12 == len(x)

        fid.close()

        #Cleanup
        import os
        os.remove(self.test_MOST_file + '.sww')
Example #9
0
    def test_ferret2sww4(self):
        """Like previous but with augmented variable names as
        in files produced by ferret as opposed to MOST
        """

        #The test file has
        # LON = 150.66667, 150.83334, 151, 151.16667
        # LAT = -34.5, -34.33333, -34.16667, -34 ;
        # ELEVATION = [-1 -2 -3 -4
        #             -5 -6 -7 -8
        #              ...
        #              ...      -16]
        # where the top left corner is -1m,
        # and the ll corner is -13.0m
        #
        # First value (index=0) in small_ha.nc is 0.3400644 cm,
        # Fourth value (index==3) is -6.50198 cm

        from anuga.coordinate_transforms.redfearn import redfearn
        import os
        fid1 = NetCDFFile('test_ha.nc', netcdf_mode_w)
        fid2 = NetCDFFile('test_ua.nc', netcdf_mode_w)
        fid3 = NetCDFFile('test_va.nc', netcdf_mode_w)
        fid4 = NetCDFFile('test_e.nc', netcdf_mode_w)

        h1_list = [150.66667, 150.83334, 151.]
        h2_list = [-34.5, -34.33333]

        #        long_name = 'LON961_1261'
        #        lat_name = 'LAT481_841'
        #        time_name = 'TIME1'

        long_name = 'LON'
        lat_name = 'LAT'
        time_name = 'TIME'

        nx = 3
        ny = 2

        for fid in [fid1, fid2, fid3]:
            fid.createDimension(long_name, nx)
            fid.createVariable(long_name, netcdf_float, (long_name, ))
            fid.variables[long_name].point_spacing = 'uneven'
            fid.variables[long_name].units = 'degrees_east'
            fid.variables[long_name][:] = h1_list

            fid.createDimension(lat_name, ny)
            fid.createVariable(lat_name, netcdf_float, (lat_name, ))
            fid.variables[lat_name].point_spacing = 'uneven'
            fid.variables[lat_name].units = 'degrees_north'
            fid.variables[lat_name][:] = h2_list

            fid.createDimension(time_name, 2)
            fid.createVariable(time_name, netcdf_float, (time_name, ))
            fid.variables[time_name].point_spacing = 'uneven'
            fid.variables[time_name].units = 'seconds'
            fid.variables[time_name][:] = [0., 1.]
            #if fid == fid3: break

        for fid in [fid4]:
            fid.createDimension(long_name, nx)
            fid.createVariable(long_name, netcdf_float, (long_name, ))
            fid.variables[long_name].point_spacing = 'uneven'
            fid.variables[long_name].units = 'degrees_east'
            fid.variables[long_name][:] = h1_list

            fid.createDimension(lat_name, ny)
            fid.createVariable(lat_name, netcdf_float, (lat_name, ))
            fid.variables[lat_name].point_spacing = 'uneven'
            fid.variables[lat_name].units = 'degrees_north'
            fid.variables[lat_name][:] = h2_list

        name = {}
        name[fid1] = 'HA'
        name[fid2] = 'UA'
        name[fid3] = 'VA'
        name[fid4] = 'ELEVATION'

        units = {}
        units[fid1] = 'cm'
        units[fid2] = 'cm/s'
        units[fid3] = 'cm/s'
        units[fid4] = 'm'

        values = {}
        values[fid1] = [[[5., 10., 15.], [13., 18., 23.]],
                        [[50., 100., 150.], [130., 180., 230.]]]
        values[fid2] = [[[1., 2., 3.], [4., 5., 6.]],
                        [[7., 8., 9.], [10., 11., 12.]]]
        values[fid3] = [[[13., 12., 11.], [10., 9., 8.]],
                        [[7., 6., 5.], [4., 3., 2.]]]
        values[fid4] = [[-3000, -3100, -3200], [-4000, -5000, -6000]]

        for fid in [fid1, fid2, fid3]:
            fid.createVariable(name[fid], netcdf_float,
                               (time_name, lat_name, long_name))
            fid.variables[name[fid]].point_spacing = 'uneven'
            fid.variables[name[fid]].units = units[fid]
            fid.variables[name[fid]][:] = values[fid]
            fid.variables[name[fid]].missing_value = -99999999.
            #if fid == fid3: break

        for fid in [fid4]:
            fid.createVariable(name[fid], netcdf_float, (lat_name, long_name))
            fid.variables[name[fid]].point_spacing = 'uneven'
            fid.variables[name[fid]].units = units[fid]
            fid.variables[name[fid]][:] = values[fid]
            fid.variables[name[fid]].missing_value = -99999999.

        fid1.sync()
        fid1.close()
        fid2.sync()
        fid2.close()
        fid3.sync()
        fid3.close()
        fid4.sync()
        fid4.close()

        fid1 = NetCDFFile('test_ha.nc', netcdf_mode_r)
        fid2 = NetCDFFile('test_e.nc', netcdf_mode_r)
        fid3 = NetCDFFile('test_va.nc', netcdf_mode_r)

        first_amp = fid1.variables['HA'][:][0, 0, 0]
        third_amp = fid1.variables['HA'][:][0, 0, 2]
        first_elevation = fid2.variables['ELEVATION'][:][0, 0]
        third_elevation = fid2.variables['ELEVATION'][:][0, 2]
        first_speed = fid3.variables['VA'][:][0, 0, 0]
        third_speed = fid3.variables['VA'][:][0, 0, 2]

        fid1.close()
        fid2.close()
        fid3.close()

        #Call conversion (with zero origin)
        ferret2sww('test',
                   verbose=self.verbose,
                   origin=(56, 0, 0),
                   inverted_bathymetry=False)

        os.remove('test_va.nc')
        os.remove('test_ua.nc')
        os.remove('test_ha.nc')
        os.remove('test_e.nc')

        #Read output file 'test.sww'
        fid = NetCDFFile('test.sww')

        #Check first value
        elevation = fid.variables['elevation'][:]
        stage = fid.variables['stage'][:]
        xmomentum = fid.variables['xmomentum'][:]
        ymomentum = fid.variables['ymomentum'][:]

        #print ymomentum
        first_height = old_div(first_amp, 100) - first_elevation
        third_height = old_div(third_amp, 100) - third_elevation
        first_momentum = old_div(first_speed * first_height, 100)
        third_momentum = old_div(third_speed * third_height, 100)

        assert num.allclose(ymomentum[0][0], first_momentum)  #Meters
        assert num.allclose(ymomentum[0][2], third_momentum)  #Meters

        fid.close()

        #Cleanup
        os.remove('test.sww')
Example #10
0
    def test_ferret2sww_zscale(self):
        """Test that zscale workse
        """
        import os, sys

        #The test file has
        # LON = 150.66667, 150.83334, 151, 151.16667
        # LAT = -34.5, -34.33333, -34.16667, -34 ;
        # TIME = 0, 0.1, 0.6, 1.1, 1.6, 2.1 ;
        #
        # First value (index=0) in small_ha.nc is 0.3400644 cm,
        # Fourth value (index==3) is -6.50198 cm

        #Read
        from anuga.coordinate_transforms.redfearn import redfearn
        fid = NetCDFFile(self.test_MOST_file + '_ha.nc')
        first_value = fid.variables['HA'][:][0, 0, 0]
        fourth_value = fid.variables['HA'][:][0, 0, 3]
        fid.close()

        #Call conversion (with no scaling)
        ferret2sww(self.test_MOST_file,
                   verbose=self.verbose,
                   origin=(56, 0, 0))

        #Work out the UTM coordinates for first point
        fid = NetCDFFile(self.test_MOST_file + '.sww')

        #Check values
        stage_1 = fid.variables['stage'][:]
        xmomentum_1 = fid.variables['xmomentum'][:]
        ymomentum_1 = fid.variables['ymomentum'][:]

        assert num.allclose(stage_1[0, 0], old_div(first_value, 100))  #Meters
        assert num.allclose(stage_1[0, 3], old_div(fourth_value, 100))  #Meters

        fid.close()

        #Call conversion (with scaling)
        ferret2sww(self.test_MOST_file,
                   zscale=5,
                   verbose=self.verbose,
                   origin=(56, 0, 0))

        #Work out the UTM coordinates for first point
        fid = NetCDFFile(self.test_MOST_file + '.sww')

        #Check values
        stage_5 = fid.variables['stage'][:]
        xmomentum_5 = fid.variables['xmomentum'][:]
        ymomentum_5 = fid.variables['ymomentum'][:]
        elevation = fid.variables['elevation'][:]

        assert num.allclose(stage_5[0, 0], old_div(5 * first_value,
                                                   100))  #Meters
        assert num.allclose(stage_5[0, 3], old_div(5 * fourth_value,
                                                   100))  #Meters

        assert num.allclose(5 * stage_1, stage_5)

        # Momentum will also be changed due to new depth

        depth_1 = stage_1 - elevation
        depth_5 = stage_5 - elevation

        for i in range(stage_1.shape[0]):
            for j in range(stage_1.shape[1]):
                if depth_1[i, j] > epsilon:

                    scale = old_div(depth_5[i, j], depth_1[i, j])
                    ref_xmomentum = xmomentum_1[i, j] * scale
                    ref_ymomentum = ymomentum_1[i, j] * scale

                    #print i, scale, xmomentum_1[i,j], xmomentum_5[i,j]

                    assert num.allclose(xmomentum_5[i, j], ref_xmomentum)
                    assert num.allclose(ymomentum_5[i, j], ref_ymomentum)

        fid.close()

        #Cleanup
        import os
        os.remove(self.test_MOST_file + '.sww')
Example #11
0
    def test_ferret2sww1(self):
        """Test that georeferencing etc works when converting from
        ferret format (lat/lon) to sww format (UTM)
        """
        import os, sys

        #The test file has
        # LON = 150.66667, 150.83334, 151, 151.16667
        # LAT = -34.5, -34.33333, -34.16667, -34 ;
        # TIME = 0, 0.1, 0.6, 1.1, 1.6, 2.1 ;
        #
        # First value (index=0) in small_ha.nc is 0.3400644 cm,
        # Fourth value (index==3) is -6.50198 cm

        #Read
        from anuga.coordinate_transforms.redfearn import redfearn
        #fid = NetCDFFile(self.test_MOST_file)
        fid = NetCDFFile(self.test_MOST_file + '_ha.nc')
        first_value = fid.variables['HA'][:][0, 0, 0]
        fourth_value = fid.variables['HA'][:][0, 0, 3]
        fid.close()

        #Call conversion (with zero origin)
        #ferret2sww('small', verbose=False,
        #           origin = (56, 0, 0))
        ferret2sww(self.test_MOST_file,
                   verbose=self.verbose,
                   origin=(56, 0, 0))

        #Work out the UTM coordinates for first point
        zone, e, n = redfearn(-34.5, 150.66667)
        #print zone, e, n

        #Read output file 'small.sww'
        #fid = NetCDFFile('small.sww')
        fid = NetCDFFile(self.test_MOST_file + '.sww')

        x = fid.variables['x'][:]
        y = fid.variables['y'][:]

        #Check that first coordinate is correctly represented
        assert num.allclose(x[0], e)
        assert num.allclose(y[0], n)

        #Check first value
        stage = fid.variables['stage'][:]
        xmomentum = fid.variables['xmomentum'][:]
        ymomentum = fid.variables['ymomentum'][:]

        #print ymomentum

        assert num.allclose(stage[0, 0], old_div(first_value, 100))  #Meters

        #Check fourth value
        assert num.allclose(stage[0, 3], old_div(fourth_value, 100))  #Meters

        fid.close()

        #Cleanup
        import os
        os.remove(self.test_MOST_file + '.sww')
Example #12
0
def urs2sww(basename_in='o',
            basename_out=None,
            verbose=False,
            remove_nc_files=True,
            minlat=None,
            maxlat=None,
            minlon=None,
            maxlon=None,
            mint=None,
            maxt=None,
            mean_stage=0,
            origin=None,
            zscale=1,
            fail_on_NaN=True,
            NaN_filler=0):
    """Convert a URS file to an SWW file.
    Convert URS C binary format for wave propagation to
    sww format native to abstract_2d_finite_volumes.

    Specify only basename_in and read files of the form
    basefilename-z-mux2, basefilename-e-mux2 and
    basefilename-n-mux2 containing relative height,
    x-velocity and y-velocity, respectively.

    Also convert latitude and longitude to UTM. All coordinates are
    assumed to be given in the GDA94 datum. The latitude and longitude
    information is for  a grid.

    min's and max's: If omitted - full extend is used.
    To include a value min may equal it, while max must exceed it.
    Lat and lon are assumed to be in decimal degrees.
    NOTE: minlon is the most east boundary.

    origin is a 3-tuple with geo referenced
    UTM coordinates (zone, easting, northing)
    It will be the origin of the sww file. This shouldn't be used,
    since all of anuga should be able to handle an arbitary origin.

    URS C binary format has data orgainised as TIME, LONGITUDE, LATITUDE
    which means that latitude is the fastest
    varying dimension (row major order, so to speak)

    In URS C binary the latitudes and longitudes are in assending order.
    """

    if basename_out is None:
        basename_out = basename_in

    files_out = urs2nc(basename_in, basename_out)

    ferret2sww(basename_out,
               minlat=minlat,
               maxlat=maxlat,
               minlon=minlon,
               maxlon=maxlon,
               mint=mint,
               maxt=maxt,
               mean_stage=mean_stage,
               origin=origin,
               zscale=zscale,
               fail_on_NaN=fail_on_NaN,
               NaN_filler=NaN_filler,
               inverted_bathymetry=True,
               verbose=verbose)

    if remove_nc_files:
        for file_out in files_out:
            os.remove(file_out)
    def test_ferret2sww4(self):
        """Like previous but with augmented variable names as
        in files produced by ferret as opposed to MOST
        """

        #The test file has
        # LON = 150.66667, 150.83334, 151, 151.16667
        # LAT = -34.5, -34.33333, -34.16667, -34 ;
        # ELEVATION = [-1 -2 -3 -4
        #             -5 -6 -7 -8
        #              ...
        #              ...      -16]
        # where the top left corner is -1m,
        # and the ll corner is -13.0m
        #
        # First value (index=0) in small_ha.nc is 0.3400644 cm,
        # Fourth value (index==3) is -6.50198 cm

        from anuga.coordinate_transforms.redfearn import redfearn
        import os
        fid1 = NetCDFFile('test_ha.nc',netcdf_mode_w)
        fid2 = NetCDFFile('test_ua.nc',netcdf_mode_w)
        fid3 = NetCDFFile('test_va.nc',netcdf_mode_w)
        fid4 = NetCDFFile('test_e.nc',netcdf_mode_w)

        h1_list = [150.66667,150.83334,151.]
        h2_list = [-34.5,-34.33333]

#        long_name = 'LON961_1261'
#        lat_name = 'LAT481_841'
#        time_name = 'TIME1'

        long_name = 'LON'
        lat_name = 'LAT'
        time_name = 'TIME'

        nx = 3
        ny = 2

        for fid in [fid1,fid2,fid3]:
            fid.createDimension(long_name,nx)
            fid.createVariable(long_name,netcdf_float,(long_name,))
            fid.variables[long_name].point_spacing='uneven'
            fid.variables[long_name].units='degrees_east'
            fid.variables[long_name][:] = h1_list

            fid.createDimension(lat_name,ny)
            fid.createVariable(lat_name,netcdf_float,(lat_name,))
            fid.variables[lat_name].point_spacing='uneven'
            fid.variables[lat_name].units='degrees_north'
            fid.variables[lat_name][:] = h2_list

            fid.createDimension(time_name,2)
            fid.createVariable(time_name,netcdf_float,(time_name,))
            fid.variables[time_name].point_spacing='uneven'
            fid.variables[time_name].units='seconds'
            fid.variables[time_name][:] = [0.,1.]
            #if fid == fid3: break


        for fid in [fid4]:
            fid.createDimension(long_name,nx)
            fid.createVariable(long_name,netcdf_float,(long_name,))
            fid.variables[long_name].point_spacing='uneven'
            fid.variables[long_name].units='degrees_east'
            fid.variables[long_name][:] = h1_list

            fid.createDimension(lat_name,ny)
            fid.createVariable(lat_name,netcdf_float,(lat_name,))
            fid.variables[lat_name].point_spacing='uneven'
            fid.variables[lat_name].units='degrees_north'
            fid.variables[lat_name][:] = h2_list

        name = {}
        name[fid1]='HA'
        name[fid2]='UA'
        name[fid3]='VA'
        name[fid4]='ELEVATION'

        units = {}
        units[fid1]='cm'
        units[fid2]='cm/s'
        units[fid3]='cm/s'
        units[fid4]='m'

        values = {}
        values[fid1]=[[[5., 10.,15.], [13.,18.,23.]],[[50.,100.,150.],[130.,180.,230.]]]
        values[fid2]=[[[1., 2.,3.], [4.,5.,6.]],[[7.,8.,9.],[10.,11.,12.]]]
        values[fid3]=[[[13., 12.,11.], [10.,9.,8.]],[[7.,6.,5.],[4.,3.,2.]]]
        values[fid4]=[[-3000,-3100,-3200],[-4000,-5000,-6000]]

        for fid in [fid1,fid2,fid3]:
          fid.createVariable(name[fid],netcdf_float,(time_name,lat_name,long_name))
          fid.variables[name[fid]].point_spacing='uneven'
          fid.variables[name[fid]].units=units[fid]
          fid.variables[name[fid]][:] = values[fid]
          fid.variables[name[fid]].missing_value = -99999999.
          #if fid == fid3: break

        for fid in [fid4]:
            fid.createVariable(name[fid],netcdf_float,(lat_name,long_name))
            fid.variables[name[fid]].point_spacing='uneven'
            fid.variables[name[fid]].units=units[fid]
            fid.variables[name[fid]][:] = values[fid]
            fid.variables[name[fid]].missing_value = -99999999.


        fid1.sync(); fid1.close()
        fid2.sync(); fid2.close()
        fid3.sync(); fid3.close()
        fid4.sync(); fid4.close()

        fid1 = NetCDFFile('test_ha.nc',netcdf_mode_r)
        fid2 = NetCDFFile('test_e.nc',netcdf_mode_r)
        fid3 = NetCDFFile('test_va.nc',netcdf_mode_r)


        first_amp = fid1.variables['HA'][:][0,0,0]
        third_amp = fid1.variables['HA'][:][0,0,2]
        first_elevation = fid2.variables['ELEVATION'][0,0]
        third_elevation= fid2.variables['ELEVATION'][:][0,2]
        first_speed = fid3.variables['VA'][0,0,0]
        third_speed = fid3.variables['VA'][:][0,0,2]

        fid1.close()
        fid2.close()
        fid3.close()

        #Call conversion (with zero origin)
        ferret2sww('test', verbose=self.verbose, origin = (56, 0, 0)
                   , inverted_bathymetry=False)

        os.remove('test_va.nc')
        os.remove('test_ua.nc')
        os.remove('test_ha.nc')
        os.remove('test_e.nc')

        #Read output file 'test.sww'
        fid = NetCDFFile('test.sww')


        #Check first value
        elevation = fid.variables['elevation'][:]
        stage = fid.variables['stage'][:]
        xmomentum = fid.variables['xmomentum'][:]
        ymomentum = fid.variables['ymomentum'][:]

        #print ymomentum
        first_height = first_amp/100 - first_elevation
        third_height = third_amp/100 - third_elevation
        first_momentum=first_speed*first_height/100
        third_momentum=third_speed*third_height/100

        assert num.allclose(ymomentum[0][0],first_momentum)  #Meters
        assert num.allclose(ymomentum[0][2],third_momentum)  #Meters

        fid.close()

        #Cleanup
        os.remove('test.sww')
    def test_ferret2sww_zscale(self):
        """Test that zscale workse
        """
        import os, sys

        #The test file has
        # LON = 150.66667, 150.83334, 151, 151.16667
        # LAT = -34.5, -34.33333, -34.16667, -34 ;
        # TIME = 0, 0.1, 0.6, 1.1, 1.6, 2.1 ;
        #
        # First value (index=0) in small_ha.nc is 0.3400644 cm,
        # Fourth value (index==3) is -6.50198 cm


        #Read
        from anuga.coordinate_transforms.redfearn import redfearn
        fid = NetCDFFile(self.test_MOST_file + '_ha.nc')
        first_value = fid.variables['HA'][:][0,0,0]
        fourth_value = fid.variables['HA'][:][0,0,3]
        fid.close()

        #Call conversion (with no scaling)
        ferret2sww(self.test_MOST_file, verbose=self.verbose,
                   origin = (56, 0, 0))

        #Work out the UTM coordinates for first point
        fid = NetCDFFile(self.test_MOST_file + '.sww')

        #Check values
        stage_1 = fid.variables['stage'][:]
        xmomentum_1 = fid.variables['xmomentum'][:]
        ymomentum_1 = fid.variables['ymomentum'][:]

        assert num.allclose(stage_1[0,0], first_value/100)  #Meters
        assert num.allclose(stage_1[0,3], fourth_value/100)  #Meters

        fid.close()

        #Call conversion (with scaling)
        ferret2sww(self.test_MOST_file,
                   zscale = 5,
                   verbose=self.verbose,
                   origin = (56, 0, 0))

        #Work out the UTM coordinates for first point
        fid = NetCDFFile(self.test_MOST_file + '.sww')

        #Check values
        stage_5 = fid.variables['stage'][:]
        xmomentum_5 = fid.variables['xmomentum'][:]
        ymomentum_5 = fid.variables['ymomentum'][:]
        elevation = fid.variables['elevation'][:]

        assert num.allclose(stage_5[0,0], 5*first_value/100)  #Meters
        assert num.allclose(stage_5[0,3], 5*fourth_value/100)  #Meters

        assert num.allclose(5*stage_1, stage_5)

        # Momentum will also be changed due to new depth

        depth_1 = stage_1-elevation
        depth_5 = stage_5-elevation


        for i in range(stage_1.shape[0]):
            for j in range(stage_1.shape[1]):            
                if depth_1[i,j] > epsilon:

                    scale = depth_5[i,j]/depth_1[i,j]
                    ref_xmomentum = xmomentum_1[i,j] * scale
                    ref_ymomentum = ymomentum_1[i,j] * scale
                    
                    #print i, scale, xmomentum_1[i,j], xmomentum_5[i,j]
                    
                    assert num.allclose(xmomentum_5[i,j], ref_xmomentum)
                    assert num.allclose(ymomentum_5[i,j], ref_ymomentum)
                    
        

        fid.close()


        #Cleanup
        import os
        os.remove(self.test_MOST_file + '.sww')
    def test_ferret2sww1(self):
        """Test that georeferencing etc works when converting from
        ferret format (lat/lon) to sww format (UTM)
        """
        import os, sys

        #The test file has
        # LON = 150.66667, 150.83334, 151, 151.16667
        # LAT = -34.5, -34.33333, -34.16667, -34 ;
        # TIME = 0, 0.1, 0.6, 1.1, 1.6, 2.1 ;
        #
        # First value (index=0) in small_ha.nc is 0.3400644 cm,
        # Fourth value (index==3) is -6.50198 cm



        #Read
        from anuga.coordinate_transforms.redfearn import redfearn
        #fid = NetCDFFile(self.test_MOST_file)
        fid = NetCDFFile(self.test_MOST_file + '_ha.nc')
        first_value = fid.variables['HA'][:][0,0,0]
        fourth_value = fid.variables['HA'][:][0,0,3]
        fid.close()


        #Call conversion (with zero origin)
        #ferret2sww('small', verbose=False,
        #           origin = (56, 0, 0))
        ferret2sww(self.test_MOST_file, verbose=self.verbose,
                   origin = (56, 0, 0))

        #Work out the UTM coordinates for first point
        zone, e, n = redfearn(-34.5, 150.66667)
        #print zone, e, n

        #Read output file 'small.sww'
        #fid = NetCDFFile('small.sww')
        fid = NetCDFFile(self.test_MOST_file + '.sww')

        x = fid.variables['x'][:]
        y = fid.variables['y'][:]

        #Check that first coordinate is correctly represented
        assert num.allclose(x[0], e)
        assert num.allclose(y[0], n)

        #Check first value
        stage = fid.variables['stage'][:]
        xmomentum = fid.variables['xmomentum'][:]
        ymomentum = fid.variables['ymomentum'][:]

        #print ymomentum

        assert num.allclose(stage[0,0], first_value/100)  #Meters

        #Check fourth value
        assert num.allclose(stage[0,3], fourth_value/100)  #Meters

        fid.close()

        #Cleanup
        import os
        os.remove(self.test_MOST_file + '.sww')
Example #16
0
def urs2sww(basename_in='o', basename_out=None, verbose=False,
            remove_nc_files=True,
            minlat=None, maxlat=None,
            minlon=None, maxlon=None,
            mint=None, maxt=None,
            mean_stage=0,
            origin=None,
            zscale=1,
            fail_on_NaN=True,
            NaN_filler=0):
    """Convert a URS file to an SWW file.
    Convert URS C binary format for wave propagation to
    sww format native to abstract_2d_finite_volumes.

    Specify only basename_in and read files of the form
    basefilename-z-mux2, basefilename-e-mux2 and
    basefilename-n-mux2 containing relative height,
    x-velocity and y-velocity, respectively.

    Also convert latitude and longitude to UTM. All coordinates are
    assumed to be given in the GDA94 datum. The latitude and longitude
    information is for  a grid.

    min's and max's: If omitted - full extend is used.
    To include a value min may equal it, while max must exceed it.
    Lat and lon are assumed to be in decimal degrees.
    NOTE: minlon is the most east boundary.

    origin is a 3-tuple with geo referenced
    UTM coordinates (zone, easting, northing)
    It will be the origin of the sww file. This shouldn't be used,
    since all of anuga should be able to handle an arbitary origin.

    URS C binary format has data orgainised as TIME, LONGITUDE, LATITUDE
    which means that latitude is the fastest
    varying dimension (row major order, so to speak)

    In URS C binary the latitudes and longitudes are in assending order.
    """

    if basename_out is None:
        basename_out = basename_in

    files_out = urs2nc(basename_in, basename_out)

    ferret2sww(basename_out,
               minlat=minlat,
               maxlat=maxlat,
               minlon=minlon,
               maxlon=maxlon,
               mint=mint,
               maxt=maxt,
               mean_stage=mean_stage,
               origin=origin,
               zscale=zscale,
               fail_on_NaN=fail_on_NaN,
               NaN_filler=NaN_filler,
               inverted_bathymetry=True,
               verbose=verbose)
    
    if remove_nc_files:
        for file_out in files_out:
            os.remove(file_out)