Ignore:
Timestamp:
Apr 28, 2009, 3:09:17 PM (15 years ago)
Author:
rwilson
Message:

Changes to cmpsww.py error reporting.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • anuga_validation/automated_validation_tests/patong_beach_validation/cmpsww.py

    r6844 r6918  
    2121#####
    2222
     23# allowable 'slop' when testing two float values
     24epsilon = 1.0e-9
     25
    2326# Global attributes that should exist and be same in both files
    2427# Don't have to have all of these, and we don't care about others.
    2528expect_global_attributes = ['smoothing', 'vertices_are_stored_uniquely',
    26                             'order', 'revision_number', 'starttime',
     29                            'order', 'starttime',
    2730                            'xllcorner', 'yllcorner',
    2831                            'zone', 'false_easting', 'false_northing',
     
    4548                      'volumes': ('number_of_volumes', 'number of vertices'),
    4649                      'time': ('number_of_timesteps',),
    47                       'stage': ('numbers_in_range',),
     50                      'stage': ('number_of_timesteps', 'numbers_of_points',),
    4851                      'stage_range': ('numbers_in_range',),
    4952                      'xmomentum': ('number_of_timesteps', 'number_of_points'),
     
    6972    # split out the filenames and check they exist
    7073    (file1, file2) = files
     74    filename1 = os.path.basename(file1)
     75    filename2 = os.path.basename(file2)
     76    width = max(len(filename1), len(filename2))
     77    filename1 = filename1.rjust(width)
     78    filename2 = filename2.rjust(width)
    7179
    7280    error = False
     
    7684        fid1 = NetCDFFile(file1, netcdf_mode_r)
    7785    except:
    78         error_msg += "File '%s' can't be opened?\n" % file1
     86        error_msg += "\nFile '%s' can't be opened?\n" % file1
    7987        error = True
    8088
     
    8290        fid2 = NetCDFFile(file2, netcdf_mode_r)
    8391    except:
    84         error_msg += "File '%s' can't be opened?\n" % file2
     92        error_msg += "\nFile '%s' can't be opened?\n" % file2
    8593        error = True
    8694        fid1.close()
     
    8896    if error:
    8997        raise RuntimeError, error_msg
     98
     99    if globals is None:
     100        globals = expect_global_attributes
    90101
    91102    #####
     
    96107    for key in expected_dimensions:
    97108        if key not in fid1.dimensions.keys():
    98             error_msg += ("File %s doesn't contain dimension '%s'\n"
    99                           % (file1, key))
     109            error_msg += ("\nFile %s doesn't contain dimension '%s'\n"
     110                          % (filename1, key))
    100111            error = True
    101112        if key not in fid2.dimensions.keys():
    102             error_msg += ("File %s doesn't contain dimension '%s'\n"
    103                           % (file2, key))
     113            error_msg += ("\nFile %s doesn't contain dimension '%s'\n"
     114                          % (filename2, key))
    104115            error = True
    105116
     
    109120        dim1_shape = fid1.dimensions.get(dim, None)
    110121        dim2_shape = fid2.dimensions.get(dim, None)
    111         if dim1_shape and dim2_shape and dim1_shape != dim2_shape:
    112             error_msg += ('File %s has %s dimension of size %s, '
    113                           'file %s has that dimension of size %s\n'
    114                           % (file1, dim, str(dim1_shape),
    115                              file2, str(dim2_shape)))
     122        if dim1_shape != dim2_shape:
     123            error_msg += ("\nFile %s has '%s' dimension of size %s,\n"
     124                          "file %s has that dimension of size %s\n"
     125                          % (filename1, dim, str(dim1_shape),
     126                             filename2, str(dim2_shape)))
    116127            error = True
    117128
     
    120131        for glob in globals:
    121132            if glob not in dir(fid1):
    122                 error_msg += ("Global attribute '%s' isn't in file %s\n"
    123                               % (glob, file1))
     133                error_msg += ("\nGlobal attribute '%s' isn't in file %s\n"
     134                              % (glob, filename1))
    124135                error = True
    125136            if glob not in dir(fid2):
    126                 error_msg += ("Global attribute '%s' isn't in file %s\n"
    127                               % (glob, file2))
     137                error_msg += ("\nGlobal attribute '%s' isn't in file %s\n"
     138                              % (glob, filename2))
    128139                error = True
    129140    else:
     
    139150        # now check attribute lists are same
    140151        if glob_vars1 != glob_vars2:
    141             error_msg = ('Files differ in global attributes:\n'
     152            error_msg = ('\nFiles differ in global attributes:\n'
    142153                         '%s: %s,\n'
    143                          '%s: %s\n' % (file1, str(glob_vars1),
    144                                      file2, str(glob_vars2)))
    145             error = True
    146         globals = glob_vars1
     154                         '%s: %s\n' % (filename1, str(glob_vars1),
     155                                     filename2, str(glob_vars2)))
     156            error = True
    147157
    148158    # get variables to test
     
    150160        for var in variables:
    151161            if var not in fid1.variables.keys():
    152                 error_msg += ("Variable '%s' isn't in file %s\n"
    153                               % (var, file1))
     162                error_msg += ("\nVariable '%s' isn't in file %s\n"
     163                              % (var, filename1))
    154164                error = True
    155165            if var not in fid2.variables.keys():
    156                 error_msg += ("Variable '%s' isn't in file %s\n"
    157                               % (var, file2))
     166                error_msg += ("\nVariable '%s' isn't in file %s\n"
     167                              % (var, filename2))
    158168                error = True
    159169    else:
     
    168178   
    169179        if var_names1 != var_names2:
    170             error_msg += ('Variables are not the same between files: '
    171                           '%s variables= %s, '
     180            error_msg += ('\nVariables are not the same between files:\n'
     181                          '%s variables= %s,\n'
    172182                          '%s variables = %s\n'
    173                           % (file1, str(var_names1), file2, str(var_names2)))
     183                          % (filename1, str(var_names1), filename2, str(var_names2)))
    174184            error = True
    175185        variables = var_names1
    176 
    177     if error:
    178         fid1.close()
    179         fid2.close()
    180         raise RuntimeError, error_msg
    181186
    182187    # get size of time dimension
     
    184189    num_timesteps2 = fid2.variables['time'].shape
    185190    if num_timesteps1 != num_timesteps2:
    186         error_msg += ('Files have different number of timesteps: %s=%d, %s=%d\n'
    187                       % (file1, num_timesteps1, file2, num_timesteps2))
     191        error_msg += ('Files have different number of timesteps:\n'
     192                      '%s=%d,\n'
     193                      '%s=%d\n'
     194                      % (filename1, num_timesteps1, filename2, num_timesteps2))
    188195        error = True
    189 
    190     if error:
    191         fid1.close()
    192         fid2.close()
    193         raise RuntimeError, error_msg
    194196
    195197    num_timesteps = num_timesteps1[0]
     
    205207                          '%s: %s,\n'
    206208                          '%s: %s\n'
    207                           % (var_name, file1, str(var1_shape),
    208                              file2, str(var2_shape)))
    209             error = True
    210             continue
     209                          % (var_name, filename1, str(var1_shape),
     210                             filename2, str(var2_shape)))
     211            error = True
    211212
    212213    if error:
     
    221222    # check values of global attributes
    222223    for glob_name in globals:
    223         g1 = getattr(fid1, glob_name)
    224         g2 = getattr(fid2, glob_name)
    225         if g1 != g2:
    226             error_msg += ("Files differ in global '%s': "
    227                           "%s: '%s', "
     224        if getattr(fid1, glob_name) != getattr(fid2, glob_name):
     225            error_msg += ("\nFiles differ in global '%s':\n"
     226                          "%s: '%s',\n"
    228227                          "%s: '%s'\n"
    229                           % (glob_name, file1, str(g1), file2, str(g2)))
     228                          % (glob_name, filename1, str(g1), filename2, str(g2)))
    230229            error = True
    231230
     
    235234        if (len(var_dims) > 1) and (var_dims[0] == 'number_of_timesteps'):
    236235            # time series, check by timestep block
    237             for i in xrange(num_timesteps):
    238                 var1 = num.array(fid1.variables[var_name][i,:])
    239                 var2 = num.array(fid2.variables[var_name][i,:])
    240                 if not num.allclose(var1, var2):
    241                     error_msg += ('Files differ in variable %s data:\n'
    242                                   '%s: %s\n'
    243                                   '%s: %s\n'
    244                                   % (glob_name, file1, str(var1),
    245                                                 file2, str(var1)))
     236            for t in xrange(num_timesteps):
     237                var1 = num.array(fid1.variables[var_name][t,:])
     238                var2 = num.array(fid2.variables[var_name][t,:])
     239                if var1 != var2:
     240                    for i in xrange(len(var1)):
     241                        if var1[i] != var2[i]:
     242                            error_msg += ('\nFiles differ in variable '
     243                                          '%s[%d,%d]:\n'
     244                                          '%s: %.14f\n'
     245                                          '%s: %.14f\n'
     246                                          % (var_name, t, i,
     247                                             filename1, var1[i],
     248                                             filename2, var2[i]))
     249                            break
    246250                    error = True
    247251        else:
     
    250254            var2 = num.array(fid2.variables[var_name][:])
    251255            if not num.allclose(var1, var2):
    252                 error_msg += ('Files differ in variable %s:\n'
    253                               '%s: %s,\n'
    254                               '%s: %s\n'
    255                               % (var_name, file1, str(var1),
    256                                            file2, str(var2)))
    257                 error = True
    258 
    259     if error:
    260         fid1.close()
    261         fid2.close()
    262         raise RuntimeError, error_msg
    263 
    264     #####
    265     # All OK, close files and signal EQUAL
     256                for j in xrange(len(var1)):
     257                    if abs(var1[j] - var2[j]) > epsilon:
     258                        error_msg += ('\nFiles differ in variable '
     259                                      '%s[%d]:\n'
     260                                      '%s: %.14f\n'
     261                                      '%s: %.14f\n'
     262                                       % (var_name, j, 
     263                                          filename1, var1[j],
     264                                          filename2, var2[j]))
     265                        break
     266                error = True
     267
     268    #####
     269    # close files and signal OK or ERROR
    266270    #####
    267271
    268272    fid1.close()
    269273    fid2.close()
     274
     275    if error:
     276        raise RuntimeError, error_msg
    270277
    271278    return
Note: See TracChangeset for help on using the changeset viewer.