Changeset 7769


Ignore:
Timestamp:
Jun 2, 2010, 3:31:36 PM (14 years ago)
Author:
hudson
Message:

Moved some file modules out of data_manager.

Location:
trunk/anuga_core/source/anuga/shallow_water
Files:
1 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/anuga_core/source/anuga/shallow_water/data_manager.py

    r7765 r7769  
    455455
    456456
    457 
    458 ##
    459 # @brief Get the extents of a NetCDF data file.
    460 # @param file_name The path to the SWW file.
    461 # @return A list of x, y, z and stage limits (min, max).
    462 def extent_sww(file_name):
    463     """Read in an sww file.
    464 
    465     Input:
    466     file_name - the sww file
    467 
    468     Output:
    469     A list: [min(x),max(x),min(y),max(y),min(stage.flat),max(stage.flat)]
    470     """
    471 
    472     from Scientific.IO.NetCDF import NetCDFFile
    473 
    474     #Get NetCDF
    475     fid = NetCDFFile(file_name, netcdf_mode_r)
    476 
    477     # Get the variables
    478     x = fid.variables['x'][:]
    479     y = fid.variables['y'][:]
    480     stage = fid.variables['stage'][:]
    481 
    482     fid.close()
    483 
    484     return [min(x), max(x), min(y), max(y), num.min(stage), num.max(stage)]
    485 
    486 
    487 ##
    488 # @brief
    489 # @param filename
    490 # @param boundary
    491 # @param t
    492 # @param fail_if_NaN
    493 # @param NaN_filler
    494 # @param verbose
    495 # @param very_verbose
    496 # @return
    497 def load_sww_as_domain(filename, boundary=None, t=None,
    498                fail_if_NaN=True, NaN_filler=0,
    499                verbose=False, very_verbose=False):
    500     """
    501     Usage: domain = sww2domain('file.sww',t=time (default = last time in file))
    502 
    503     Boundary is not recommended if domain.smooth is not selected, as it
    504     uses unique coordinates, but not unique boundaries. This means that
    505     the boundary file will not be compatable with the coordinates, and will
    506     give a different final boundary, or crash.
    507     """
    508    
    509     from Scientific.IO.NetCDF import NetCDFFile
    510     from shallow_water import Domain
    511 
    512     # initialise NaN.
    513     NaN = 9.969209968386869e+036
    514 
    515     if verbose: log.critical('Reading from %s' % filename)
    516 
    517     fid = NetCDFFile(filename, netcdf_mode_r)    # Open existing file for read
    518     time = fid.variables['time']       # Timesteps
    519     if t is None:
    520         t = time[-1]
    521     time_interp = get_time_interp(time,t)
    522 
    523     # Get the variables as numeric arrays
    524     x = fid.variables['x'][:]                   # x-coordinates of vertices
    525     y = fid.variables['y'][:]                   # y-coordinates of vertices
    526     elevation = fid.variables['elevation']      # Elevation
    527     stage = fid.variables['stage']              # Water level
    528     xmomentum = fid.variables['xmomentum']      # Momentum in the x-direction
    529     ymomentum = fid.variables['ymomentum']      # Momentum in the y-direction
    530 
    531     starttime = fid.starttime[0]
    532     volumes = fid.variables['volumes'][:]       # Connectivity
    533     coordinates = num.transpose(num.asarray([x.tolist(), y.tolist()]))
    534     # FIXME (Ole): Something like this might be better:
    535     #                 concatenate((x, y), axis=1)
    536     # or              concatenate((x[:,num.newaxis], x[:,num.newaxis]), axis=1)
    537 
    538     conserved_quantities = []
    539     interpolated_quantities = {}
    540     other_quantities = []
    541 
    542     # get geo_reference
    543     try:                             # sww files don't have to have a geo_ref
    544         geo_reference = Geo_reference(NetCDFObject=fid)
    545     except: # AttributeError, e:
    546         geo_reference = None
    547 
    548     if verbose: log.critical('    getting quantities')
    549 
    550     for quantity in fid.variables.keys():
    551         dimensions = fid.variables[quantity].dimensions
    552         if 'number_of_timesteps' in dimensions:
    553             conserved_quantities.append(quantity)
    554             interpolated_quantities[quantity] = \
    555                   interpolated_quantity(fid.variables[quantity][:], time_interp)
    556         else:
    557             other_quantities.append(quantity)
    558 
    559     other_quantities.remove('x')
    560     other_quantities.remove('y')
    561     #other_quantities.remove('z')
    562     other_quantities.remove('volumes')
    563     try:
    564         other_quantities.remove('stage_range')
    565         other_quantities.remove('xmomentum_range')
    566         other_quantities.remove('ymomentum_range')
    567         other_quantities.remove('elevation_range')
    568     except:
    569         pass
    570 
    571     conserved_quantities.remove('time')
    572 
    573     if verbose: log.critical('    building domain')
    574 
    575     #    From domain.Domain:
    576     #    domain = Domain(coordinates, volumes,\
    577     #                    conserved_quantities = conserved_quantities,\
    578     #                    other_quantities = other_quantities,zone=zone,\
    579     #                    xllcorner=xllcorner, yllcorner=yllcorner)
    580 
    581     # From shallow_water.Domain:
    582     coordinates = coordinates.tolist()
    583     volumes = volumes.tolist()
    584     # FIXME:should this be in mesh? (peter row)
    585     if fid.smoothing == 'Yes':
    586         unique = False
    587     else:
    588         unique = True
    589     if unique:
    590         coordinates, volumes, boundary = weed(coordinates, volumes,boundary)
    591 
    592      
    593    
    594     try:
    595         domain = Domain(coordinates, volumes, boundary)
    596     except AssertionError, e:
    597         fid.close()
    598         msg = 'Domain could not be created: %s. ' \
    599               'Perhaps use "fail_if_NaN=False and NaN_filler = ..."' % e
    600         raise DataDomainError, msg
    601 
    602     if not boundary is None:
    603         domain.boundary = boundary
    604 
    605     domain.geo_reference = geo_reference
    606 
    607     domain.starttime = float(starttime) + float(t)
    608     domain.time = 0.0
    609 
    610     for quantity in other_quantities:
    611         try:
    612             NaN = fid.variables[quantity].missing_value
    613         except:
    614             pass                       # quantity has no missing_value number
    615         X = fid.variables[quantity][:]
    616         if very_verbose:
    617             log.critical('       %s' % str(quantity))
    618             log.critical('        NaN = %s' % str(NaN))
    619             log.critical('        max(X)')
    620             log.critical('       %s' % str(max(X)))
    621             log.critical('        max(X)==NaN')
    622             log.critical('       %s' % str(max(X)==NaN))
    623             log.critical('')
    624         if max(X) == NaN or min(X) == NaN:
    625             if fail_if_NaN:
    626                 msg = 'quantity "%s" contains no_data entry' % quantity
    627                 raise DataMissingValuesError, msg
    628             else:
    629                 data = (X != NaN)
    630                 X = (X*data) + (data==0)*NaN_filler
    631         if unique:
    632             X = num.resize(X, (len(X)/3, 3))
    633         domain.set_quantity(quantity, X)
    634     #
    635     for quantity in conserved_quantities:
    636         try:
    637             NaN = fid.variables[quantity].missing_value
    638         except:
    639             pass                       # quantity has no missing_value number
    640         X = interpolated_quantities[quantity]
    641         if very_verbose:
    642             log.critical('       %s' % str(quantity))
    643             log.critical('        NaN = %s' % str(NaN))
    644             log.critical('        max(X)')
    645             log.critical('       %s' % str(max(X)))
    646             log.critical('        max(X)==NaN')
    647             log.critical('       %s' % str(max(X)==NaN))
    648             log.critical('')
    649         if max(X) == NaN or min(X) == NaN:
    650             if fail_if_NaN:
    651                 msg = 'quantity "%s" contains no_data entry' % quantity
    652                 raise DataMissingValuesError, msg
    653             else:
    654                 data = (X != NaN)
    655                 X = (X*data) + (data==0)*NaN_filler
    656         if unique:
    657             X = num.resize(X, (X.shape[0]/3, 3))
    658         domain.set_quantity(quantity, X)
    659 
    660     fid.close()
    661 
    662     return domain
    663 
    664 
    665 ##
    666 # @brief Interpolate a quantity wrt time.
    667 # @param saved_quantity The quantity to interpolate.
    668 # @param time_interp (index, ratio)
    669 # @return A vector of interpolated values.
    670 def interpolated_quantity(saved_quantity, time_interp):
    671     '''Given an index and ratio, interpolate quantity with respect to time.'''
    672 
    673     index, ratio = time_interp
    674 
    675     Q = saved_quantity
    676 
    677     if ratio > 0:
    678         q = (1-ratio)*Q[index] + ratio*Q[index+1]
    679     else:
    680         q = Q[index]
    681 
    682     #Return vector of interpolated values
    683     return q
    684 
    685 
    686 ##
    687 # @brief
    688 # @parm time
    689 # @param t
    690 # @return An (index, ration) tuple.
    691 def get_time_interp(time, t=None):
    692     #Finds the ratio and index for time interpolation.
    693     #It is borrowed from previous abstract_2d_finite_volumes code.
    694     if t is None:
    695         t=time[-1]
    696         index = -1
    697         ratio = 0.
    698     else:
    699         T = time
    700         tau = t
    701         index=0
    702         msg = 'Time interval derived from file %s [%s:%s]' \
    703               % ('FIXMEfilename', T[0], T[-1])
    704         msg += ' does not match model time: %s' % tau
    705         if tau < time[0]: raise DataTimeError, msg
    706         if tau > time[-1]: raise DataTimeError, msg
    707         while tau > time[index]: index += 1
    708         while tau < time[index]: index -= 1
    709         if tau == time[index]:
    710             #Protect against case where tau == time[-1] (last time)
    711             # - also works in general when tau == time[i]
    712             ratio = 0
    713         else:
    714             #t is now between index and index+1
    715             ratio = (tau - time[index])/(time[index+1] - time[index])
    716 
    717     return (index, ratio)
    718 
    719 
    720 ##
    721 # @brief
    722 # @param coordinates
    723 # @param volumes
    724 # @param boundary
    725 def weed(coordinates, volumes, boundary=None):
    726     if isinstance(coordinates, num.ndarray):
    727         coordinates = coordinates.tolist()
    728     if isinstance(volumes, num.ndarray):
    729         volumes = volumes.tolist()
    730 
    731     unique = False
    732     point_dict = {}
    733     same_point = {}
    734     for i in range(len(coordinates)):
    735         point = tuple(coordinates[i])
    736         if point_dict.has_key(point):
    737             unique = True
    738             same_point[i] = point
    739             #to change all point i references to point j
    740         else:
    741             point_dict[point] = i
    742             same_point[i] = point
    743 
    744     coordinates = []
    745     i = 0
    746     for point in point_dict.keys():
    747         point = tuple(point)
    748         coordinates.append(list(point))
    749         point_dict[point] = i
    750         i += 1
    751 
    752     for volume in volumes:
    753         for i in range(len(volume)):
    754             index = volume[i]
    755             if index > -1:
    756                 volume[i] = point_dict[same_point[index]]
    757 
    758     new_boundary = {}
    759     if not boundary is None:
    760         for segment in boundary.keys():
    761             point0 = point_dict[same_point[segment[0]]]
    762             point1 = point_dict[same_point[segment[1]]]
    763             label = boundary[segment]
    764             #FIXME should the bounday attributes be concaterated
    765             #('exterior, pond') or replaced ('pond')(peter row)
    766 
    767             if new_boundary.has_key((point0, point1)):
    768                 new_boundary[(point0,point1)] = new_boundary[(point0, point1)]
    769 
    770             elif new_boundary.has_key((point1, point0)):
    771                 new_boundary[(point1,point0)] = new_boundary[(point1, point0)]
    772             else: new_boundary[(point0, point1)] = label
    773 
    774         boundary = new_boundary
    775 
    776     return coordinates, volumes, boundary
    777 
    778 
    779457##
    780458# @brief Read DEM file, decimate data, write new DEM file.
     
    1164842# END URS 2 SWW
    1165843################################################################################
    1166 
    1167 ################################################################################
    1168 # URS UNGRIDDED 2 SWW
    1169 ################################################################################
    1170 
    1171 ### PRODUCING THE POINTS NEEDED FILE ###
    1172 
    1173 # Ones used for FESA 2007 results
    1174 #LL_LAT = -50.0
    1175 #LL_LONG = 80.0
    1176 #GRID_SPACING = 1.0/60.0
    1177 #LAT_AMOUNT = 4800
    1178 #LONG_AMOUNT = 3600
    1179 
    1180 
    1181 ##
    1182 # @brief
    1183 # @param file_name
    1184 # @param boundary_polygon
    1185 # @param zone
    1186 # @param ll_lat
    1187 # @param ll_long
    1188 # @param grid_spacing
    1189 # @param lat_amount
    1190 # @param long_amount
    1191 # @param isSouthernHemisphere
    1192 # @param export_csv
    1193 # @param use_cache
    1194 # @param verbose True if this function is to be verbose.
    1195 # @return
    1196 def URS_points_needed_to_file(file_name, boundary_polygon, zone,
    1197                               ll_lat, ll_long,
    1198                               grid_spacing,
    1199                               lat_amount, long_amount,
    1200                               isSouthernHemisphere=True,
    1201                               export_csv=False, use_cache=False,
    1202                               verbose=False):
    1203     """
    1204     Given the info to replicate the URS grid and a polygon output
    1205     a file that specifies the cloud of boundary points for URS.
    1206 
    1207     This creates a .urs file.  This is in the format used by URS;
    1208     1st line is the number of points,
    1209     each line after represents a point,in lats and longs.
    1210 
    1211     Note: The polygon cannot cross zones or hemispheres.
    1212 
    1213     A work-a-round for different zones or hemispheres is to run this twice,
    1214     once for each zone, and then combine the output.
    1215 
    1216     file_name - name of the urs file produced for David.
    1217     boundary_polygon - a list of points that describes a polygon.
    1218                       The last point is assumed ot join the first point.
    1219                       This is in UTM (lat long would be better though)
    1220 
    1221      This is info about the URS model that needs to be inputted.
    1222 
    1223     ll_lat - lower left latitude, in decimal degrees
    1224     ll-long - lower left longitude, in decimal degrees
    1225     grid_spacing - in deciamal degrees
    1226     lat_amount - number of latitudes
    1227     long_amount- number of longs
    1228 
    1229     Don't add the file extension.  It will be added.
    1230     """
    1231 
    1232     geo = URS_points_needed(boundary_polygon, zone, ll_lat, ll_long,
    1233                             grid_spacing,
    1234                             lat_amount, long_amount, isSouthernHemisphere,
    1235                             use_cache, verbose)
    1236 
    1237     if not file_name[-4:] == ".urs":
    1238         file_name += ".urs"
    1239 
    1240     geo.export_points_file(file_name, isSouthHemisphere=isSouthernHemisphere)
    1241 
    1242     if export_csv:
    1243         if file_name[-4:] == ".urs":
    1244             file_name = file_name[:-4] + ".csv"
    1245         geo.export_points_file(file_name)
    1246 
    1247     return geo
    1248 
    1249 
    1250 ##
    1251 # @brief
    1252 # @param boundary_polygon
    1253 # @param zone
    1254 # @param ll_lat
    1255 # @param ll_long
    1256 # @param grid_spacing
    1257 # @param lat_amount
    1258 # @param long_amount
    1259 # @param isSouthHemisphere
    1260 # @param use_cache
    1261 # @param verbose
    1262 def URS_points_needed(boundary_polygon, zone, ll_lat,
    1263                       ll_long, grid_spacing,
    1264                       lat_amount, long_amount, isSouthHemisphere=True,
    1265                       use_cache=False, verbose=False):
    1266     args = (boundary_polygon,
    1267             zone, ll_lat,
    1268             ll_long, grid_spacing,
    1269             lat_amount, long_amount, isSouthHemisphere)
    1270     kwargs = {}
    1271 
    1272     if use_cache is True:
    1273         try:
    1274             from anuga.caching import cache
    1275         except:
    1276             msg = 'Caching was requested, but caching module' \
    1277                   'could not be imported'
    1278             raise msg
    1279 
    1280         geo = cache(_URS_points_needed,
    1281                     args, kwargs,
    1282                     verbose=verbose,
    1283                     compression=False)
    1284     else:
    1285         geo = apply(_URS_points_needed, args, kwargs)
    1286 
    1287     return geo
    1288 
    1289 
    1290 ##
    1291 # @brief
    1292 # @param boundary_polygon An iterable of points that describe a polygon.
    1293 # @param zone
    1294 # @param ll_lat Lower left latitude, in decimal degrees
    1295 # @param ll_long Lower left longitude, in decimal degrees
    1296 # @param grid_spacing Grid spacing in decimal degrees.
    1297 # @param lat_amount
    1298 # @param long_amount
    1299 # @param isSouthHemisphere
    1300 def _URS_points_needed(boundary_polygon,
    1301                        zone, ll_lat,
    1302                        ll_long, grid_spacing,
    1303                        lat_amount, long_amount,
    1304                        isSouthHemisphere):
    1305     """
    1306     boundary_polygon - a list of points that describes a polygon.
    1307                       The last point is assumed ot join the first point.
    1308                       This is in UTM (lat long would b better though)
    1309 
    1310     ll_lat - lower left latitude, in decimal degrees
    1311     ll-long - lower left longitude, in decimal degrees
    1312     grid_spacing - in decimal degrees
    1313 
    1314     """
    1315 
    1316     msg = "grid_spacing can not be zero"
    1317     assert not grid_spacing == 0, msg
    1318 
    1319     a = boundary_polygon
    1320 
    1321     # List of segments.  Each segment is two points.
    1322     segs = [i and [a[i-1], a[i]] or [a[len(a)-1], a[0]] for i in range(len(a))]
    1323 
    1324     # convert the segs to Lat's and longs.
    1325     # Don't assume the zone of the segments is the same as the lower left
    1326     # corner of the lat long data!!  They can easily be in different zones
    1327     lat_long_set = frozenset()
    1328     for seg in segs:
    1329         points_lat_long = points_needed(seg, ll_lat, ll_long, grid_spacing,
    1330                                         lat_amount, long_amount, zone,
    1331                                         isSouthHemisphere)
    1332         lat_long_set |= frozenset(points_lat_long)
    1333 
    1334     if lat_long_set == frozenset([]):
    1335         msg = "URS region specified and polygon does not overlap."
    1336         raise ValueError, msg
    1337 
    1338     # Warning there is no info in geospatial saying the hemisphere of
    1339     # these points.  There should be.
    1340     geo = Geospatial_data(data_points=list(lat_long_set),
    1341                           points_are_lats_longs=True)
    1342 
    1343     return geo
    1344 
    1345 
    1346 ##
    1347 # @brief Get the points that are needed to interpolate any point a a segment.
    1348 # @param seg Two points in the UTM.
    1349 # @param ll_lat Lower left latitude, in decimal degrees
    1350 # @param ll_long Lower left longitude, in decimal degrees
    1351 # @param grid_spacing
    1352 # @param lat_amount
    1353 # @param long_amount
    1354 # @param zone
    1355 # @param isSouthHemisphere
    1356 # @return A list of points.
    1357 def points_needed(seg, ll_lat, ll_long, grid_spacing,
    1358                   lat_amount, long_amount, zone,
    1359                   isSouthHemisphere):
    1360     """
    1361     seg is two points, in UTM
    1362     return a list of the points, in lats and longs that are needed to
    1363     interpolate any point on the segment.
    1364     """
    1365 
    1366     from math import sqrt
    1367 
    1368     geo_reference = Geo_reference(zone=zone)
    1369     geo = Geospatial_data(seg, geo_reference=geo_reference)
    1370     seg_lat_long = geo.get_data_points(as_lat_long=True,
    1371                                        isSouthHemisphere=isSouthHemisphere)
    1372 
    1373     # 1.415 = 2^0.5, rounded up....
    1374     sqrt_2_rounded_up = 1.415
    1375     buffer = sqrt_2_rounded_up * grid_spacing
    1376 
    1377     max_lat = max(seg_lat_long[0][0], seg_lat_long[1][0]) + buffer
    1378     max_long = max(seg_lat_long[0][1], seg_lat_long[1][1]) + buffer
    1379     min_lat = min(seg_lat_long[0][0], seg_lat_long[1][0]) - buffer
    1380     min_long = min(seg_lat_long[0][1], seg_lat_long[1][1]) - buffer
    1381 
    1382     first_row = (min_long - ll_long) / grid_spacing
    1383 
    1384     # To round up
    1385     first_row_long = int(round(first_row + 0.5))
    1386 
    1387     last_row = (max_long - ll_long) / grid_spacing # round down
    1388     last_row_long = int(round(last_row))
    1389 
    1390     first_row = (min_lat - ll_lat) / grid_spacing
    1391     # To round up
    1392     first_row_lat = int(round(first_row + 0.5))
    1393 
    1394     last_row = (max_lat - ll_lat) / grid_spacing # round down
    1395     last_row_lat = int(round(last_row))
    1396 
    1397     max_distance = 157147.4112 * grid_spacing
    1398     points_lat_long = []
    1399 
    1400     # Create a list of the lat long points to include.
    1401     for index_lat in range(first_row_lat, last_row_lat + 1):
    1402         for index_long in range(first_row_long, last_row_long + 1):
    1403             lat = ll_lat + index_lat*grid_spacing
    1404             long = ll_long + index_long*grid_spacing
    1405 
    1406             #filter here to keep good points
    1407             if keep_point(lat, long, seg, max_distance):
    1408                 points_lat_long.append((lat, long)) #must be hashable
    1409 
    1410     # Now that we have these points, lets throw ones out that are too far away
    1411     return points_lat_long
    1412 
    1413 
    1414 ##
    1415 # @brief
    1416 # @param lat
    1417 # @param long
    1418 # @param seg Two points in UTM.
    1419 # @param max_distance
    1420 def keep_point(lat, long, seg, max_distance):
    1421     """
    1422     seg is two points, UTM
    1423     """
    1424 
    1425     from math import sqrt
    1426 
    1427     _ , x0, y0 = redfearn(lat, long)
    1428     x1 = seg[0][0]
    1429     y1 = seg[0][1]
    1430     x2 = seg[1][0]
    1431     y2 = seg[1][1]
    1432     x2_1 = x2-x1
    1433     y2_1 = y2-y1
    1434     try:
    1435         d = abs((x2_1)*(y1-y0)-(x1-x0)*(y2_1))/sqrt( \
    1436             (x2_1)*(x2_1)+(y2_1)*(y2_1))
    1437     except ZeroDivisionError:
    1438         if sqrt((x2_1)*(x2_1)+(y2_1)*(y2_1)) == 0 \
    1439            and abs((x2_1)*(y1-y0)-(x1-x0)*(y2_1)) == 0:
    1440             return True
    1441         else:
    1442             return False
    1443 
    1444     return d <= max_distance
    1445 
    1446844
    1447845
     
    1555953################################################################################
    1556954
    1557 ##
    1558 # @brief Get mesh and quantity data from an SWW file.
    1559 # @param filename Path to data file to read.
    1560 # @param quantities UNUSED!
    1561 # @param verbose True if this function is to be verbose.
    1562 # @return (mesh, quantities, time) where mesh is the mesh data, quantities is
    1563 #         a dictionary of {name: value}, and time is the time vector.
    1564 # @note Quantities extracted: 'elevation', 'stage', 'xmomentum' and 'ymomentum'
    1565 def get_mesh_and_quantities_from_file(filename,
    1566                                       quantities=None,
    1567                                       verbose=False):
    1568     """Get and rebuild mesh structure and associated quantities from sww file
    1569 
    1570     Input:
    1571         filename - Name os sww file
    1572         quantities - Names of quantities to load
    1573 
    1574     Output:
    1575         mesh - instance of class Interpolate
    1576                (including mesh and interpolation functionality)
    1577         quantities - arrays with quantity values at each mesh node
    1578         time - vector of stored timesteps
    1579 
    1580     This function is used by e.g.:
    1581         get_interpolated_quantities_at_polyline_midpoints
    1582     """
    1583 
    1584     # FIXME (Ole): Maybe refactor filefunction using this more fundamental code.
    1585 
    1586     import types
    1587     from Scientific.IO.NetCDF import NetCDFFile
    1588     from shallow_water import Domain
    1589     from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh
    1590 
    1591     if verbose: log.critical('Reading from %s' % filename)
    1592 
    1593     fid = NetCDFFile(filename, netcdf_mode_r)    # Open existing file for read
    1594     time = fid.variables['time'][:]    # Time vector
    1595     time += fid.starttime[0]
    1596 
    1597     # Get the variables as numeric arrays
    1598     x = fid.variables['x'][:]                   # x-coordinates of nodes
    1599     y = fid.variables['y'][:]                   # y-coordinates of nodes
    1600     elevation = fid.variables['elevation'][:]   # Elevation
    1601     stage = fid.variables['stage'][:]           # Water level
    1602     xmomentum = fid.variables['xmomentum'][:]   # Momentum in the x-direction
    1603     ymomentum = fid.variables['ymomentum'][:]   # Momentum in the y-direction
    1604 
    1605     # Mesh (nodes (Mx2), triangles (Nx3))
    1606     nodes = num.concatenate((x[:,num.newaxis], y[:,num.newaxis]), axis=1)
    1607     triangles = fid.variables['volumes'][:]
    1608 
    1609     # Get geo_reference
    1610     try:
    1611         geo_reference = Geo_reference(NetCDFObject=fid)
    1612     except: #AttributeError, e:
    1613         # Sww files don't have to have a geo_ref
    1614         geo_reference = None
    1615 
    1616     if verbose: log.critical('    building mesh from sww file %s' % filename)
    1617 
    1618     boundary = None
    1619 
    1620     #FIXME (Peter Row): Should this be in mesh?
    1621     if fid.smoothing != 'Yes':
    1622         nodes = nodes.tolist()
    1623         triangles = triangles.tolist()
    1624         nodes, triangles, boundary = weed(nodes, triangles, boundary)
    1625 
    1626     try:
    1627         mesh = Mesh(nodes, triangles, boundary, geo_reference=geo_reference)
    1628     except AssertionError, e:
    1629         fid.close()
    1630         msg = 'Domain could not be created: %s. "' % e
    1631         raise DataDomainError, msg
    1632 
    1633     quantities = {}
    1634     quantities['elevation'] = elevation
    1635     quantities['stage'] = stage
    1636     quantities['xmomentum'] = xmomentum
    1637     quantities['ymomentum'] = ymomentum
    1638 
    1639     fid.close()
    1640 
    1641     return mesh, quantities, time
    1642955
    1643956
  • trunk/anuga_core/source/anuga/shallow_water/eqf_v2.py

    r7765 r7769  
    224224                    U[i]=U[i]+SIGN*DU[i]
    225225           
    226         U1 =U[0]                                                         
    227         U2 =U[1]                                                         
    228         U3 =U[2]                                                         
    229         U11=U[3]                                                         
    230         U12=U[4]                                                         
    231         U21=U[5]                                                         
    232         U22=U[6]                                                         
    233         U31=U[7]                                                         
    234         U32=U[8]
     226        U1  = U[0]                                                         
     227        U2  = U[1]                                                         
     228        U3  = U[2]                                                         
     229        U11 = U[3]                                                         
     230        U12 = U[4]                                                         
     231        U21 = U[5]                                                         
     232        U22 = U[6]                                                         
     233        U31 = U[7]                                                         
     234        U32 = U[8]
    235235
    236236        self.U3 = U3
     
    298298
    299299        if CD == 0:
    300             #C==============================                                        
    301             #C=====   INCLINED FAULT   =====                                        
     300            #C==============================     
     301            #C=====   INCLINED FAULT   =====     
    302302            #C==============================
    303303            RD2=RD*RD
     
    311311            C3= ALP*SD/RD*(XI2*RRD - F1) 
    312312        else:
    313             #C==============================                                        
    314             #C=====   VERTICAL FAULT   =====                                        
     313            #C==============================   
     314            #C=====   VERTICAL FAULT   =====     
    315315            #C==============================
    316316            TD=SD/CD                                                         
     
    345345       
    346346        if DISL1 != F0:
    347             #C======================================                                
    348             #C=====  STRIKE-SLIP CONTRIBUTION  =====                                 
     347            #C======================================   
     348            #C=====  STRIKE-SLIP CONTRIBUTION  ===== 
    349349            #C======================================
    350350            UN=DISL1/PI2                                                     
     
    362362
    363363        if DISL2 != F0:
    364             #C===================================                                   
    365             #C=====  DIP-SLIP CONTRIBUTION  =====                                   
     364            #C=================================== 
     365            #C=====  DIP-SLIP CONTRIBUTION  =====       
    366366            #C===================================
    367367            UN=DISL2/PI2                                                     
     
    379379        if DISL3 != F0:
    380380            #C========================================
    381             #C=====  TENSILE-FAULT CONTRIBUTION  =====                              
     381            #C=====  TENSILE-FAULT CONTRIBUTION  =====   
    382382            #C========================================
    383383            UN=DISL3/PI2                                                     
     
    443443        PI2 = 6.283185307179586
    444444
    445         D =DEP                                                           
    446         P =Y*CD + D*SD                                                   
    447         Q =Y*SD - D*CD                                                   
    448         S =P*SD + Q*CD                                                   
    449         X2=X*X                                                           
    450         Y2=Y*Y                                                           
    451         XY=X*Y                                                           
    452         D2=D*D                                                           
    453         R2=X2 + Y2 + D2                                                   
    454         R =sqrt(R2)                                                       
    455         R3=R *R2
    456         R5=R3*R2
    457         QR=F3*Q/R5
    458         XR =F5*X2/R2
    459         YR =F5*Y2/R2
    460         XYR=F5*XY/R2
    461         DR =F5*D /R2
    462         RD =R + D
    463         R12=F1/(R*RD*RD)
    464         R32=R12*(F2*R + D)/ R2
    465         R33=R12*(F3*R + D)/(R2*RD)
    466         R53=R12*(F8*R2 + F9*R*D + F3*D2)/(R2*R2*RD)
    467         R54=R12*(F5*R2 + F4*R*D +    D2)/R3*R12                           
    468 
    469         A1= ALP*Y*(R12-X2*R33)                                           
    470         A2= ALP*X*(R12-Y2*R33)                                           
    471         A3= ALP*X/R3 - A2                                                 
    472         A4=-ALP*XY*R32                                                   
    473         A5= ALP*( F1/(R*RD) - X2*R32 )                                   
    474         B1= ALP*(-F3*XY*R33      + F3*X2*XY*R54)                         
    475         B2= ALP*( F1/R3 - F3*R12 + F3*X2*Y2*R54)                         
    476         B3= ALP*( F1/R3 - F3*X2/R5) - B2                                 
    477         B4=-ALP*F3*XY/R5 - B1                                             
    478         C1=-ALP*Y*(R32 - X2*R53)                                         
    479         C2=-ALP*X*(R32 - Y2*R53)                                         
    480         C3=-ALP*F3*X*D/R5 - C2                                           
    481 
    482         U1 =F0                                                           
    483         U2 =F0                                                           
    484         U3 =F0                                                           
    485         U11=F0                                                           
    486         U12=F0                                                           
    487         U21=F0                                                           
    488         U22=F0                                                           
    489         U31=F0                                                           
    490         U32=F0
     445        D = DEP                                                           
     446        P = Y*CD + D*SD                                                   
     447        Q = Y*SD - D*CD                                                   
     448        S = P*SD + Q*CD                                                   
     449        X2 = X*X                                                           
     450        Y2 = Y*Y                                                           
     451        XY = X*Y                                                           
     452        D2 = D*D                                                           
     453        R2= X2 + Y2 + D2                                                   
     454        R = sqrt(R2)                                                       
     455        R3 = R *R2
     456        R5 = R3*R2
     457        QR = F3*Q/R5
     458        XR = F5*X2/R2
     459        YR = F5*Y2/R2
     460        XYR = F5*XY/R2
     461        DR  = F5*D /R2
     462        RD = R + D
     463        R12 = F1/(R*RD*RD)
     464        R32 = R12*(F2*R + D)/ R2
     465        R33 = R12*(F3*R + D)/(R2*RD)
     466        R53 = R12*(F8*R2 + F9*R*D + F3*D2)/(R2*R2*RD)
     467        R54 = R12*(F5*R2 + F4*R*D +    D2)/R3*R12                           
     468
     469        A1 = ALP*Y*(R12-X2*R33)                                           
     470        A2 = ALP*X*(R12-Y2*R33)                                           
     471        A3 = ALP*X/R3 - A2                                                 
     472        A4 = -ALP*XY*R32                                                   
     473        A5 = ALP*( F1/(R*RD) - X2*R32 )                                   
     474        B1 = ALP*(-F3*XY*R33      + F3*X2*XY*R54)                         
     475        B2 = ALP*( F1/R3 - F3*R12 + F3*X2*Y2*R54)                         
     476        B3 = ALP*( F1/R3 - F3*X2/R5) - B2                                 
     477        B4 = -ALP*F3*XY/R5 - B1                                             
     478        C1 = -ALP*Y*(R32 - X2*R53)                                         
     479        C2 = -ALP*X*(R32 - Y2*R53)                                         
     480        C3 = -ALP*F3*X*D/R5 - C2                                           
     481
     482        U1 = F0                                                           
     483        U2 = F0                                                           
     484        U3 = F0                                                           
     485        U11= F0                                                           
     486        U12= F0                                                           
     487        U21= F0                                                           
     488        U22= F0                                                           
     489        U31= F0                                                           
     490        U32= F0
    491491       
    492492        #======================================                                 
     
    495495
    496496        if POT1 != F0:
    497             UN=POT1/PI2                                                       
    498             QRX=QR*X                                                         
    499             FX=F3*X/R5*SD                                                     
    500             U1 =U1 - UN*( QRX*X + A1*SD )                                     
    501             U2 =U2 - UN*( QRX*Y + A2*SD )                                     
    502             U3 =U3 - UN*( QRX*D + A4*SD )
     497            UN = POT1/PI2                                                       
     498            QRX = QR*X                                                         
     499            FX = F3*X/R5*SD                                                     
     500            U1 = U1 - UN*( QRX*X + A1*SD )                                     
     501            U2 = U2 - UN*( QRX*Y + A2*SD )                                     
     502            U3 = U3 - UN*( QRX*D + A4*SD )
    503503             
    504             U11=U11- UN*( QRX* (F2-XR)        + B1*SD )                      
    505             U12=U12- UN*(-QRX*XYR      + FX*X + B2*SD )                       
    506             U21=U21- UN*( QR*Y*(F1-XR)        + B2*SD )                       
    507             U22=U22- UN*( QRX *(F1-YR) + FX*Y + B4*SD )                      
    508             U31=U31- UN*( QR*D*(F1-XR)        + C1*SD )                      
    509             U32=U32- UN*(-QRX*DR*Y     + FX*D + C2*SD )                     
    510 
    511         #======================================                                
    512         #=====    DIP-SLIP CONTRIBUTION   =====                                 
    513         #======================================                                 
     504            U11 = U11 - UN * ( QRX* (F2-XR)        + B1*SD ) 
     505            U12 = U12 - UN * (-QRX*XYR      + FX*X + B2*SD )   
     506            U21 = U21 - UN * ( QR*Y*(F1-XR)        + B2*SD )   
     507            U22 = U22 - UN * ( QRX *(F1-YR) + FX*Y + B4*SD )   
     508            U31 = U31 - UN * ( QR*D*(F1-XR)        + C1*SD ) 
     509            U32 = U32 - UN * (-QRX*DR*Y     + FX*D + C2*SD )
     510
     511        #======================================           
     512        #=====    DIP-SLIP CONTRIBUTION   =====         
     513        #======================================         
    514514
    515515        if POT2 != F0:
    516             UN=POT2/PI2                                                       
    517             SDCD=SD*CD                                                       
    518             QRP=QR*P                                                         
    519             FS=F3*S/R5                                                       
    520             U1 =U1 - UN*( QRP*X - A3*SDCD )                                   
    521             U2 =U2 - UN*( QRP*Y - A1*SDCD )                                   
    522             U3 =U3 - UN*( QRP*D - A5*SDCD )                                   
    523             U11=U11- UN*( QRP*(F1-XR)        - B3*SDCD )                     
    524             U12=U12- UN*(-QRP*XYR     + FS*X - B1*SDCD )                     
    525             U21=U21- UN*(-QRP*XYR            - B1*SDCD )                     
    526             U22=U22- UN*( QRP*(F1-YR) + FS*Y - B2*SDCD )                     
    527             U31=U31- UN*(-QRP*DR*X           - C3*SDCD )                     
    528             U32=U32- UN*(-QRP*DR*Y    + FS*D - C1*SDCD )
    529            
    530         #========================================                               
    531         #=====  TENSILE-FAULT CONTRIBUTION  =====                               
    532         #========================================                                                                    
     516            UN = POT2/PI2                                                       
     517            SDCD = SD*CD                                                       
     518            QRP = QR*P                                                         
     519            FS = F3*S/R5                                                       
     520            U1  = U1 - UN*( QRP*X - A3*SDCD )                                   
     521            U2  = U2 - UN*( QRP*Y - A1*SDCD )                                   
     522            U3  = U3 - UN*( QRP*D - A5*SDCD )                                   
     523            U11 = U11- UN*( QRP*(F1-XR)        - B3*SDCD )                     
     524            U12 = U12- UN*(-QRP*XYR     + FS*X - B1*SDCD )                     
     525            U21 = U21- UN*(-QRP*XYR            - B1*SDCD )                     
     526            U22 = U22- UN*( QRP*(F1-YR) + FS*Y - B2*SDCD )                     
     527            U31 = U31- UN*(-QRP*DR*X           - C3*SDCD )                     
     528            U32 = U32- UN*(-QRP*DR*Y    + FS*D - C1*SDCD )
     529           
     530        #========================================   
     531        #=====  TENSILE-FAULT CONTRIBUTION  =====   
     532        #========================================                             
    533533
    534534        if POT3 != F0:
    535             UN=POT3/PI2                                                       
    536             SDSD=SD*SD                                                       
    537             QRQ=QR*Q                                                         
    538             FQ=F2*QR*SD                                                       
    539             U1 =U1 + UN*( QRQ*X - A3*SDSD )                                   
    540             U2 =U2 + UN*( QRQ*Y - A1*SDSD )                                   
    541             U3 =U3 + UN*( QRQ*D - A5*SDSD )                                   
    542             U11=U11+ UN*( QRQ*(F1-XR)        - B3*SDSD )                     
    543             U12=U12+ UN*(-QRQ*XYR     + FQ*X - B1*SDSD )                     
    544             U21=U21+ UN*(-QRQ*XYR            - B1*SDSD )                     
    545             U22=U22+ UN*( QRQ*(F1-YR) + FQ*Y - B2*SDSD )                     
    546             U31=U31+ UN*(-QRQ*DR*X           - C3*SDSD )                     
    547             U32=U32+ UN*(-QRQ*DR*Y    + FQ*D - C1*SDSD )
    548 
     535            UN = POT3/PI2                                                       
     536            SDSD = SD*SD                                                       
     537            QRQ = QR*Q                                                         
     538            FQ = F2*QR*SD                                                       
     539            U1  = U1 + UN*( QRQ*X - A3*SDSD )                                   
     540            U2  = U2 + UN*( QRQ*Y - A1*SDSD )                                   
     541            U3  = U3 + UN*( QRQ*D - A5*SDSD )                                   
     542            U11 = U11+ UN*( QRQ*(F1-XR)        - B3*SDSD )                     
     543            U12 = U12+ UN*(-QRQ*XYR     + FQ*X - B1*SDSD )                     
     544            U21 = U21+ UN*(-QRQ*XYR            - B1*SDSD )                     
     545            U22 = U22+ UN*( QRQ*(F1-YR) + FQ*Y - B2*SDSD )                     
     546            U31 = U31+ UN*(-QRQ*DR*X           - C3*SDSD )                     
     547            U32 = U32+ UN*(-QRQ*DR*Y    + FQ*D - C1*SDSD )
     548
  • trunk/anuga_core/source/anuga/shallow_water/test_data_manager.py

    r7765 r7769  
    2929from anuga.config import netcdf_float, epsilon, g
    3030
     31
    3132# import all the boundaries - some are generic, some are shallow water
    3233from boundaries import Reflective_boundary, \
     
    4445from anuga.geospatial_data.geospatial_data import Geospatial_data
    4546
    46 
    47 class Test_Data_Manager(unittest.TestCase):
     47# use helper methods from other unit test
     48from anuga.file.test_mux import Test_Mux
     49
     50
     51class Test_Data_Manager(Test_Mux):
    4852    # Class variable
    4953    verbose = False
     
    12321236
    12331237
    1234     def test_sww2domain1(self):
    1235         ################################################
    1236         #Create a test domain, and evolve and save it.
    1237         ################################################
    1238         from mesh_factory import rectangular
    1239 
    1240         #Create basic mesh
    1241 
    1242         yiel=0.01
    1243         points, vertices, boundary = rectangular(10,10)
    1244 
    1245         #Create shallow water domain
    1246         domain = Domain(points, vertices, boundary)
    1247         domain.geo_reference = Geo_reference(56,11,11)
    1248         domain.smooth = False
    1249         domain.store = True
    1250         domain.set_name('bedslope')
    1251         domain.default_order=2
    1252         #Bed-slope and friction
    1253         domain.set_quantity('elevation', lambda x,y: -x/3)
    1254         domain.set_quantity('friction', 0.1)
    1255         # Boundary conditions
    1256         from math import sin, pi
    1257         Br = Reflective_boundary(domain)
    1258         Bt = Transmissive_boundary(domain)
    1259         Bd = Dirichlet_boundary([0.2,0.,0.])
    1260         Bw = Time_boundary(domain=domain,f=lambda t: [(0.1*sin(t*2*pi)), 0.0, 0.0])
    1261 
    1262         #domain.set_boundary({'left': Bd, 'right': Br, 'top': Br, 'bottom': Br})
    1263         domain.set_boundary({'left': Bd, 'right': Bd, 'top': Bd, 'bottom': Bd})
    1264 
    1265         domain.quantities_to_be_stored['xmomentum'] = 2
    1266         domain.quantities_to_be_stored['ymomentum'] = 2
    1267         #Initial condition
    1268         h = 0.05
    1269         elevation = domain.quantities['elevation'].vertex_values
    1270         domain.set_quantity('stage', elevation + h)
    1271 
    1272         domain.check_integrity()
    1273         #Evolution
    1274         #domain.tight_slope_limiters = 1
    1275         for t in domain.evolve(yieldstep = yiel, finaltime = 0.05):
    1276             #domain.write_time()
    1277             pass
    1278 
    1279 
    1280         ##########################################
    1281         #Import the example's file as a new domain
    1282         ##########################################
    1283         from data_manager import sww2domain
    1284         import os
    1285 
    1286         filename = domain.datadir + os.sep + domain.get_name() + '.sww'
    1287         domain2 = sww2domain(filename,None,fail_if_NaN=False,verbose=self.verbose)
    1288         #points, vertices, boundary = rectangular(15,15)
    1289         #domain2.boundary = boundary
    1290         ###################
    1291         ##NOW TEST IT!!!
    1292         ###################
    1293 
    1294         os.remove(filename)
    1295 
    1296         bits = ['vertex_coordinates']
    1297         for quantity in domain.quantities_to_be_stored:
    1298             bits.append('get_quantity("%s").get_integral()' % quantity)
    1299             bits.append('get_quantity("%s").get_values()' % quantity)
    1300 
    1301         for bit in bits:
    1302             #print 'testing that domain.'+bit+' has been restored'
    1303             #print bit
    1304             #print 'done'
    1305             assert num.allclose(eval('domain.'+bit),eval('domain2.'+bit))
    1306 
    1307         ######################################
    1308         #Now evolve them both, just to be sure
    1309         ######################################x
    1310         domain.time = 0.
    1311         from time import sleep
    1312 
    1313         final = .1
    1314         domain.set_quantity('friction', 0.1)
    1315         domain.store = False
    1316         domain.set_boundary({'left': Bd, 'right': Bd, 'top': Bd, 'bottom': Bd})
    1317 
    1318 
    1319         for t in domain.evolve(yieldstep = yiel, finaltime = final):
    1320             #domain.write_time()
    1321             pass
    1322 
    1323         final = final - (domain2.starttime-domain.starttime)
    1324         #BUT since domain1 gets time hacked back to 0:
    1325         final = final + (domain2.starttime-domain.starttime)
    1326 
    1327         domain2.smooth = False
    1328         domain2.store = False
    1329         domain2.default_order=2
    1330         domain2.set_quantity('friction', 0.1)
    1331         #Bed-slope and friction
    1332         # Boundary conditions
    1333         Bd2=Dirichlet_boundary([0.2,0.,0.])
    1334         domain2.boundary = domain.boundary
    1335         #print 'domain2.boundary'
    1336         #print domain2.boundary
    1337         domain2.set_boundary({'left': Bd, 'right': Bd, 'top': Bd, 'bottom': Bd})
    1338         #domain2.set_boundary({'exterior': Bd})
    1339 
    1340         domain2.check_integrity()
    1341 
    1342         for t in domain2.evolve(yieldstep = yiel, finaltime = final):
    1343             #domain2.write_time()
    1344             pass
    1345 
    1346         ###################
    1347         ##NOW TEST IT!!!
    1348         ##################
    1349 
    1350         bits = ['vertex_coordinates']
    1351 
    1352         for quantity in ['elevation','stage', 'ymomentum','xmomentum']:
    1353             bits.append('get_quantity("%s").get_integral()' %quantity)
    1354             bits.append('get_quantity("%s").get_values()' %quantity)
    1355 
    1356         #print bits
    1357         for bit in bits:
    1358             #print bit
    1359             #print eval('domain.'+bit)
    1360             #print eval('domain2.'+bit)
    1361            
    1362             #print eval('domain.'+bit+'-domain2.'+bit)
    1363             msg = 'Values in the two domains are different for ' + bit
    1364             assert num.allclose(eval('domain.'+bit),eval('domain2.'+bit),
    1365                                 rtol=1.e-5, atol=3.e-8), msg
    1366 
    1367 
    13681238    def DISABLEDtest_sww2domain2(self):
    13691239        ##################################################################
     
    14131283        #Import the file as a new domain
    14141284        ##################################
    1415         from data_manager import sww2domain
     1285        from data_manager import load_sww_as_domain
    14161286        import os
    14171287
     
    14241294        #                     verbose=self.verbose)       
    14251295        try:
    1426             domain2 = sww2domain(filename,
     1296            domain2 = load_sww_as_domain(filename,
    14271297                                 boundary,
    14281298                                 fail_if_NaN=True,
     
    14311301            # Now import it, filling NaNs to be -9999
    14321302            filler = -9999
    1433             domain2 = sww2domain(filename,
     1303            domain2 = load_sww_as_domain(filename,
    14341304                                 None,
    14351305                                 fail_if_NaN=False,
     
    14421312        # Now import it, filling NaNs to be 0
    14431313        filler = -9999
    1444         domain2 = sww2domain(filename,
     1314        domain2 = load_sww_as_domain(filename,
    14451315                             None,
    14461316                             fail_if_NaN=False,
     
    15511421
    15521422
    1553         ##########################################
    1554         #Import the example's file as a new domain
    1555         ##########################################
    1556         from data_manager import sww2domain
    1557         import os
    1558 
    15591423        filename = domain.datadir + os.sep + domain.get_name() + '.sww'
    1560         domain2 = sww2domain(filename,None,fail_if_NaN=False,verbose=self.verbose)
     1424        domain2 = load_sww_as_domain(filename,None,fail_if_NaN=False,verbose=self.verbose)
    15611425        #points, vertices, boundary = rectangular(15,15)
    15621426        #domain2.boundary = boundary
     
    18121676        os.remove(root + '.dem')
    18131677        os.remove(root + '_100.dem')     
    1814        
    1815     def test_urs2sts0(self):
    1816         """
    1817         Test single source
    1818         """
    1819         tide=0
    1820         time_step_count = 3
    1821         time_step = 2
    1822         lat_long_points =[(-21.5,114.5),(-21,114.5),(-21.5,115), (-21.,115.)]
    1823         n=len(lat_long_points)
    1824         first_tstep=num.ones(n,num.int)
    1825         first_tstep[0]+=1
    1826         first_tstep[2]+=1
    1827         last_tstep=(time_step_count)*num.ones(n,num.int)
    1828         last_tstep[0]-=1
    1829 
    1830         gauge_depth=20*num.ones(n,num.float)
    1831         ha=2*num.ones((n,time_step_count),num.float)
    1832         ha[0]=num.arange(0,time_step_count)
    1833         ha[1]=num.arange(time_step_count,2*time_step_count)
    1834         ha[2]=num.arange(2*time_step_count,3*time_step_count)
    1835         ha[3]=num.arange(3*time_step_count,4*time_step_count)
    1836         ua=5*num.ones((n,time_step_count),num.float)
    1837         va=-10*num.ones((n,time_step_count),num.float)
    1838 
    1839         base_name, files = self.write_mux2(lat_long_points,
    1840                                       time_step_count, time_step,
    1841                                       first_tstep, last_tstep,
    1842                                       depth=gauge_depth,
    1843                                       ha=ha,
    1844                                       ua=ua,
    1845                                       va=va)
    1846 
    1847         urs2sts(base_name,
    1848                 basename_out=base_name,
    1849                 mean_stage=tide,verbose=False)
    1850 
    1851         # now I want to check the sts file ...
    1852         sts_file = base_name + '.sts'
    1853 
    1854         #Let's interigate the sww file
    1855         # Note, the sww info is not gridded.  It is point data.
    1856         fid = NetCDFFile(sts_file)
    1857 
    1858         # Make x and y absolute
    1859         x = fid.variables['x'][:]
    1860         y = fid.variables['y'][:]
    1861 
    1862         geo_reference = Geo_reference(NetCDFObject=fid)
    1863         points = geo_reference.get_absolute(map(None, x, y))
    1864         points = ensure_numeric(points)
    1865 
    1866         x = points[:,0]
    1867         y = points[:,1]
    1868 
    1869         #Check that first coordinate is correctly represented       
    1870         #Work out the UTM coordinates for first point
    1871         for i in range(4):
    1872             zone, e, n = redfearn(lat_long_points[i][0], lat_long_points[i][1])
    1873             assert num.allclose([x[i],y[i]], [e,n])
    1874 
    1875         #Check the time vector
    1876         times = fid.variables['time'][:]
    1877 
    1878         times_actual = []
    1879         for i in range(time_step_count):
    1880             times_actual.append(time_step * i)
    1881 
    1882         assert num.allclose(ensure_numeric(times),
    1883                             ensure_numeric(times_actual))
    1884 
    1885         #Check first value
    1886         stage = fid.variables['stage'][:]
    1887         xmomentum = fid.variables['xmomentum'][:]
    1888         ymomentum = fid.variables['ymomentum'][:]
    1889         elevation = fid.variables['elevation'][:]
    1890 
    1891         # Set original data used to write mux file to be zero when gauges are
    1892         #not recdoring
    1893         ha[0][0]=0.0
    1894         ha[0][time_step_count-1]=0.0;
    1895         ha[2][0]=0.0;
    1896         ua[0][0]=0.0
    1897         ua[0][time_step_count-1]=0.0;
    1898         ua[2][0]=0.0;
    1899         va[0][0]=0.0
    1900         va[0][time_step_count-1]=0.0;
    1901         va[2][0]=0.0;
    1902 
    1903         assert num.allclose(num.transpose(ha),stage)  #Meters
    1904 
    1905         #Check the momentums - ua
    1906         #momentum = velocity*(stage-elevation)
    1907         # elevation = - depth
    1908         #momentum = velocity_ua *(stage+depth)
    1909 
    1910         depth=num.zeros((len(lat_long_points),time_step_count),num.float)
    1911         for i in range(len(lat_long_points)):
    1912             depth[i]=gauge_depth[i]+tide+ha[i]
    1913         assert num.allclose(num.transpose(ua*depth),xmomentum)
    1914 
    1915         #Check the momentums - va
    1916         #momentum = velocity*(stage-elevation)
    1917         # elevation = - depth
    1918         #momentum = velocity_va *(stage+depth)
    1919 
    1920         assert num.allclose(num.transpose(va*depth),ymomentum)
    1921 
    1922         # check the elevation values.
    1923         # -ve since urs measures depth, sww meshers height,
    1924         assert num.allclose(-elevation, gauge_depth)  #Meters
    1925 
    1926         fid.close()
    1927         self.delete_mux(files)
    1928         os.remove(sts_file)
    1929 
    1930     def test_urs2sts_nonstandard_meridian(self):
    1931         """
    1932         Test single source using the meridian from zone 50 as a nonstandard meridian
    1933         """
    1934         tide=0
    1935         time_step_count = 3
    1936         time_step = 2
    1937         lat_long_points =[(-21.,114.5),(-21.,113.5),(-21.,114.), (-21.,115.)]
    1938         n=len(lat_long_points)
    1939         first_tstep=num.ones(n,num.int)
    1940         first_tstep[0]+=1
    1941         first_tstep[2]+=1
    1942         last_tstep=(time_step_count)*num.ones(n,num.int)
    1943         last_tstep[0]-=1
    1944 
    1945         gauge_depth=20*num.ones(n,num.float)
    1946         ha=2*num.ones((n,time_step_count),num.float)
    1947         ha[0]=num.arange(0,time_step_count)
    1948         ha[1]=num.arange(time_step_count,2*time_step_count)
    1949         ha[2]=num.arange(2*time_step_count,3*time_step_count)
    1950         ha[3]=num.arange(3*time_step_count,4*time_step_count)
    1951         ua=5*num.ones((n,time_step_count),num.float)
    1952         va=-10*num.ones((n,time_step_count),num.float)
    1953 
    1954         base_name, files = self.write_mux2(lat_long_points,
    1955                                            time_step_count, time_step,
    1956                                            first_tstep, last_tstep,
    1957                                            depth=gauge_depth,
    1958                                            ha=ha,
    1959                                            ua=ua,
    1960                                            va=va)
    1961 
    1962         urs2sts(base_name,
    1963                 basename_out=base_name,
    1964                 central_meridian=123,
    1965                 mean_stage=tide,
    1966                 verbose=False)
    1967 
    1968         # now I want to check the sts file ...
    1969         sts_file = base_name + '.sts'
    1970 
    1971         #Let's interigate the sww file
    1972         # Note, the sww info is not gridded.  It is point data.
    1973         fid = NetCDFFile(sts_file)
    1974 
    1975         # Make x and y absolute
    1976         x = fid.variables['x'][:]
    1977         y = fid.variables['y'][:]
    1978 
    1979         geo_reference = Geo_reference(NetCDFObject=fid)
    1980         points = geo_reference.get_absolute(map(None, x, y))
    1981         points = ensure_numeric(points)
    1982 
    1983         x = points[:,0]
    1984         y = points[:,1]
    1985 
    1986         # Check that all coordinate are correctly represented       
    1987         # Using the non standard projection (50)
    1988         for i in range(4):
    1989             zone, e, n = redfearn(lat_long_points[i][0],
    1990                                   lat_long_points[i][1],
    1991                                   central_meridian=123)
    1992             assert num.allclose([x[i],y[i]], [e,n])
    1993             assert zone==-1
    1994         self.delete_mux(files)
    1995        
    1996 
    1997 
    1998     def test_urs2sts_individual_sources(self):   
    1999         """Test that individual sources compare to actual urs output
    2000            Test that the first recording time is the smallest
    2001            over waveheight, easting and northing velocity
    2002         """
    2003        
    2004         # Get path where this test is run
    2005         path = get_pathname_from_package('anuga.shallow_water')       
    2006        
    2007         testdir = os.path.join(path, 'urs_test_data')
    2008         ordering_filename=os.path.join(testdir, 'thinned_bound_order_test.txt')
    2009        
    2010         sources = ['1-z.grd','2-z.grd','3-z.grd']
    2011        
    2012         # Start times by source and station taken manually from urs header files
    2013         time_start_z = num.array([[10.0,11.5,13,14.5,17.7],
    2014                                   [9.8,11.2,12.7,14.2,17.4],
    2015                                   [9.5,10.9,12.4,13.9,17.1]])
    2016 
    2017         time_start_e = time_start_n = time_start_z
    2018 
    2019         # time step in urs output
    2020         delta_t = 0.1
    2021        
    2022         # Make sts file for each source
    2023         for k, source_filename in enumerate(sources):
    2024             source_number = k + 1 # Source numbering starts at 1
    2025            
    2026             urs_filenames = os.path.join(testdir, source_filename)
    2027             weights = [1.]
    2028             sts_name_out = 'test'
    2029            
    2030             urs2sts(urs_filenames,
    2031                     basename_out=sts_name_out,
    2032                     ordering_filename=ordering_filename,
    2033                     weights=weights,
    2034                     mean_stage=0.0,
    2035                     verbose=False)
    2036 
    2037             # Read in sts file for this source file
    2038             fid = NetCDFFile(sts_name_out+'.sts', netcdf_mode_r) # Open existing file for read
    2039             x = fid.variables['x'][:]+fid.xllcorner    # x-coordinates of vertices
    2040             y = fid.variables['y'][:]+fid.yllcorner    # y-coordinates of vertices
    2041             elevation = fid.variables['elevation'][:]
    2042             time=fid.variables['time'][:]+fid.starttime
    2043 
    2044             # Get quantity data from sts file
    2045             quantity_names=['stage','xmomentum','ymomentum']
    2046             quantities = {}
    2047             for i, name in enumerate(quantity_names):
    2048                 quantities[name] = fid.variables[name][:]
    2049            
    2050             # Make sure start time from sts file is the minimum starttime
    2051             # across all stations (for this source)
    2052             #print k, time_start_z[k,:]
    2053             starttime = min(time_start_z[k, :])
    2054             sts_starttime = fid.starttime[0]
    2055             msg = 'sts starttime for source %d was %f. Should have been %f'\
    2056                 %(source_number, sts_starttime, starttime)
    2057             assert num.allclose(sts_starttime, starttime), msg             
    2058 
    2059             # For each station, compare urs2sts output to known urs output
    2060             for j in range(len(x)):
    2061                 index_start_urs = 0
    2062                 index_end_urs = 0
    2063                 index_start = 0
    2064                 index_end = 0
    2065                 count = 0
    2066 
    2067                 # read in urs test data for stage, e and n velocity
    2068                 urs_file_name_z = 'z_'+str(source_number)+'_'+str(j)+'.csv'
    2069                 dict = load_csv_as_array(os.path.join(testdir, urs_file_name_z))
    2070                 urs_stage = dict['urs_stage']
    2071                 urs_file_name_e = 'e_'+str(source_number)+'_'+str(j)+'.csv'
    2072                 dict = load_csv_as_array(os.path.join(testdir, urs_file_name_e))
    2073                 urs_e = dict['urs_e']
    2074                 urs_file_name_n = 'n_'+str(source_number)+'_'+str(j)+'.csv'
    2075                 dict = load_csv_as_array(os.path.join(testdir, urs_file_name_n))
    2076                 urs_n = dict['urs_n']
    2077 
    2078                 # find start and end time for stage             
    2079                 for i in range(len(urs_stage)):
    2080                     if urs_stage[i] == 0.0:
    2081                         index_start_urs_z = i+1
    2082                     if int(urs_stage[i]) == 99 and count <> 1:
    2083                         count +=1
    2084                         index_end_urs_z = i
    2085 
    2086                 if count == 0: index_end_urs_z = len(urs_stage)
    2087 
    2088                 start_times_z = time_start_z[source_number-1]
    2089 
    2090                 # start times for easting velocities should be the same as stage
    2091                 start_times_e = time_start_e[source_number-1]
    2092                 index_start_urs_e = index_start_urs_z
    2093                 index_end_urs_e = index_end_urs_z
    2094 
    2095                 # start times for northing velocities should be the same as stage
    2096                 start_times_n = time_start_n[source_number-1]
    2097                 index_start_urs_n = index_start_urs_z
    2098                 index_end_urs_n = index_end_urs_z
    2099                
    2100                 # Check that actual start time matches header information for stage
    2101                 msg = 'stage start time from urs file is not the same as the '
    2102                 msg += 'header file for source %i and station %i' %(source_number,j)
    2103                 assert num.allclose(index_start_urs_z,start_times_z[j]/delta_t), msg
    2104 
    2105                 msg = 'e velocity start time from urs file is not the same as the '
    2106                 msg += 'header file for source %i and station %i' %(source_number,j)
    2107                 assert num.allclose(index_start_urs_e,start_times_e[j]/delta_t), msg
    2108 
    2109                 msg = 'n velocity start time from urs file is not the same as the '
    2110                 msg += 'header file for source %i and station %i' %(source_number,j)
    2111                 assert num.allclose(index_start_urs_n,start_times_n[j]/delta_t), msg
    2112                
    2113                 # get index for start and end time for sts quantities
    2114                 index_start_stage = 0
    2115                 index_end_stage = 0
    2116                 count = 0
    2117                 sts_stage = quantities['stage'][:,j]
    2118                 for i in range(len(sts_stage)):
    2119                     if sts_stage[i] <> 0.0 and count <> 1:
    2120                         count += 1
    2121                         index_start_stage = i
    2122                     if int(sts_stage[i]) == 99 and count <> 1:
    2123                         count += 1
    2124                         index_end_stage = i
    2125 
    2126                 index_end_stage = index_start_stage + len(urs_stage[index_start_urs_z:index_end_urs_z])
    2127 
    2128                 sts_xmom = quantities['xmomentum'][:,j]
    2129                 index_start_x = index_start_stage
    2130                 index_end_x = index_start_x + len(urs_e[index_start_urs_e:index_end_urs_e])
    2131 
    2132                 sts_ymom = quantities['ymomentum'][:,j]
    2133                 index_start_y = index_start_stage
    2134                 index_end_y = index_start_y + len(urs_n[index_start_urs_n:index_end_urs_n])
    2135 
    2136                 # check that urs stage and sts stage are the same
    2137                 msg = 'urs stage is not equal to sts stage for for source %i and station %i' %(source_number,j)
    2138                 assert num.allclose(urs_stage[index_start_urs_z:index_end_urs_z],
    2139                                     sts_stage[index_start_stage:index_end_stage],
    2140                                     rtol=1.0e-6, atol=1.0e-5 ), msg                               
    2141                                
    2142                 # check that urs e velocity and sts xmomentum are the same
    2143                 msg = 'urs e velocity is not equivalent to sts x momentum for for source %i and station %i' %(source_number,j)
    2144                 assert num.allclose(urs_e[index_start_urs_e:index_end_urs_e]*(urs_stage[index_start_urs_e:index_end_urs_e]-elevation[j]),
    2145                                 sts_xmom[index_start_x:index_end_x],
    2146                                 rtol=1.0e-5, atol=1.0e-4 ), msg
    2147                
    2148                 # check that urs n velocity and sts ymomentum are the same
    2149                 #print 'urs n velocity', urs_n[index_start_urs_n:index_end_urs_n]*(urs_stage[index_start_urs_n:index_end_urs_n]-elevation[j])
    2150                 #print 'sts momentum', sts_ymom[index_start_y:index_end_y]                                                             
    2151                 msg = 'urs n velocity is not equivalent to sts y momentum for source %i and station %i' %(source_number,j)
    2152                 assert num.allclose(urs_n[index_start_urs_n:index_end_urs_n]*(urs_stage[index_start_urs_n:index_end_urs_n]-elevation[j]),
    2153                                 -sts_ymom[index_start_y:index_end_y],
    2154                                 rtol=1.0e-5, atol=1.0e-4 ), msg
    2155                                                
    2156                                
    2157             fid.close()
    2158            
    2159         os.remove(sts_name_out+'.sts')
    2160 
    2161     def test_urs2sts_combined_sources(self):   
    2162         """Test that combined sources compare to actual urs output
    2163            Test that the first recording time is the smallest
    2164            over waveheight, easting and northing velocity
    2165         """
    2166 
    2167         # combined
    2168         time_start_z = num.array([9.5,10.9,12.4,13.9,17.1])
    2169         time_start_e = time_start_n = time_start_z
    2170          
    2171         # make sts file for combined sources
    2172         weights = [1., 2., 3.]
    2173        
    2174         path = get_pathname_from_package('anuga.shallow_water')       
    2175                
    2176         testdir = os.path.join(path, 'urs_test_data')       
    2177         ordering_filename=os.path.join(testdir, 'thinned_bound_order_test.txt')
    2178 
    2179         urs_filenames = [os.path.join(testdir,'1-z.grd'),
    2180                          os.path.join(testdir,'2-z.grd'),
    2181                          os.path.join(testdir,'3-z.grd')]
    2182         sts_name_out = 'test'
    2183        
    2184         urs2sts(urs_filenames,
    2185                 basename_out=sts_name_out,
    2186                 ordering_filename=ordering_filename,
    2187                 weights=weights,
    2188                 mean_stage=0.0,
    2189                 verbose=False)
    2190        
    2191         # read in sts file for combined source
    2192         fid = NetCDFFile(sts_name_out+'.sts', netcdf_mode_r)    # Open existing file for read
    2193         x = fid.variables['x'][:]+fid.xllcorner   # x-coordinates of vertices
    2194         y = fid.variables['y'][:]+fid.yllcorner   # y-coordinates of vertices
    2195         elevation = fid.variables['elevation'][:]
    2196         time=fid.variables['time'][:]+fid.starttime
    2197        
    2198        
    2199         # Check that stored permutation is as per default
    2200         permutation = range(len(x))
    2201         stored_permutation = fid.variables['permutation'][:]
    2202         msg = 'Permutation was not stored correctly. I got '
    2203         msg += str(stored_permutation)
    2204         assert num.allclose(stored_permutation, permutation), msg       
    2205 
    2206         # Get quantity data from sts file
    2207         quantity_names=['stage','xmomentum','ymomentum']
    2208         quantities = {}
    2209         for i, name in enumerate(quantity_names):
    2210             quantities[name] = fid.variables[name][:]
    2211 
    2212         # For each station, compare urs2sts output to known urs output   
    2213         delta_t = 0.1
    2214        
    2215         # Make sure start time from sts file is the minimum starttime
    2216         # across all stations (for this source)
    2217         starttime = min(time_start_z[:])
    2218         sts_starttime = fid.starttime[0]
    2219         msg = 'sts starttime was %f. Should have been %f'\
    2220             %(sts_starttime, starttime)
    2221         assert num.allclose(sts_starttime, starttime), msg
    2222    
    2223         #stations = [1,2,3]
    2224         #for j in stations:
    2225         for j in range(len(x)):
    2226             index_start_urs_z = 0
    2227             index_end_urs_z = 0
    2228             index_start_urs_e = 0
    2229             index_end_urs_e = 0
    2230             index_start_urs_n = 0
    2231             index_end_urs_n = 0
    2232             count = 0
    2233 
    2234             # read in urs test data for stage, e and n velocity
    2235             urs_file_name_z = 'z_combined_'+str(j)+'.csv'
    2236             dict = load_csv_as_array(os.path.join(testdir, urs_file_name_z))
    2237             urs_stage = dict['urs_stage']
    2238             urs_file_name_e = 'e_combined_'+str(j)+'.csv'
    2239             dict = load_csv_as_array(os.path.join(testdir, urs_file_name_e))
    2240             urs_e = dict['urs_e']
    2241             urs_file_name_n = 'n_combined_'+str(j)+'.csv'
    2242             dict = load_csv_as_array(os.path.join(testdir, urs_file_name_n))
    2243             urs_n = dict['urs_n']
    2244 
    2245             # find start and end time for stage         
    2246             for i in range(len(urs_stage)):
    2247                 if urs_stage[i] == 0.0:
    2248                     index_start_urs_z = i+1
    2249                 if int(urs_stage[i]) == 99 and count <> 1:
    2250                     count +=1
    2251                     index_end_urs_z = i
    2252 
    2253             if count == 0: index_end_urs_z = len(urs_stage)
    2254 
    2255             start_times_z = time_start_z[j]
    2256 
    2257             start_times_e = time_start_e[j]
    2258             index_start_urs_e = index_start_urs_z
    2259 
    2260             start_times_n = time_start_n[j]
    2261             index_start_urs_n = index_start_urs_z
    2262                
    2263             # Check that actual start time matches header information for stage
    2264             msg = 'stage start time from urs file is not the same as the '
    2265             msg += 'header file at station %i' %(j)
    2266             assert num.allclose(index_start_urs_z,start_times_z/delta_t), msg
    2267 
    2268             msg = 'e velocity start time from urs file is not the same as the '
    2269             msg += 'header file at station %i' %(j)
    2270             assert num.allclose(index_start_urs_e,start_times_e/delta_t), msg
    2271 
    2272             msg = 'n velocity start time from urs file is not the same as the '
    2273             msg += 'header file at station %i' %(j)
    2274             assert num.allclose(index_start_urs_n,start_times_n/delta_t), msg
    2275                
    2276             # get index for start and end time for sts quantities
    2277             index_start_stage = 0
    2278             index_end_stage = 0
    2279             index_start_x = 0
    2280             index_end_x = 0
    2281             index_start_y = 0
    2282             index_end_y = 0
    2283             count = 0
    2284             count1 = 0
    2285             sts_stage = quantities['stage'][:,j]
    2286             for i in range(len(sts_stage)):
    2287                 if sts_stage[i] <> 0.0 and count <> 1:
    2288                     count += 1
    2289                     index_start_stage = i
    2290                 if int(urs_stage[i]) == 99 and count <> 1:
    2291                     count +=1
    2292                     index_end_stage = i
    2293                
    2294             index_end_stage = index_start_stage + len(urs_stage[index_start_urs_z:index_end_urs_z])
    2295 
    2296             index_start_x = index_start_stage
    2297             index_end_x = index_start_x + len(urs_stage[index_start_urs_e:index_end_urs_e])
    2298             sts_xmom = quantities['ymomentum'][:,j]
    2299 
    2300             index_start_y = index_start_stage
    2301             index_end_y = index_start_y + len(urs_stage[index_start_urs_n:index_end_urs_n])
    2302             sts_ymom = quantities['ymomentum'][:,j]
    2303 
    2304             # check that urs stage and sts stage are the same
    2305             msg = 'urs stage is not equal to sts stage for station %i' %j
    2306             #print 'urs stage', urs_stage[index_start_urs_z:index_end_urs_z]
    2307             #print 'sts stage', sts_stage[index_start_stage:index_end_stage]
    2308             #print 'diff', max(urs_stage[index_start_urs_z:index_end_urs_z]-sts_stage[index_start_stage:index_end_stage])
    2309             #print 'index', index_start_stage, index_end_stage, len(sts_stage)
    2310             assert num.allclose(urs_stage[index_start_urs_z:index_end_urs_z],
    2311                             sts_stage[index_start_stage:index_end_stage],
    2312                                 rtol=1.0e-5, atol=1.0e-4 ), msg                               
    2313                                
    2314             # check that urs e velocity and sts xmomentum are the same         
    2315             msg = 'urs e velocity is not equivalent to sts xmomentum for station %i' %j
    2316             assert num.allclose(urs_e[index_start_urs_e:index_end_urs_e]*(urs_stage[index_start_urs_e:index_end_urs_e]-elevation[j]),
    2317                             sts_xmom[index_start_x:index_end_x],
    2318                             rtol=1.0e-5, atol=1.0e-4 ), msg
    2319                
    2320             # check that urs n velocity and sts ymomentum are the same                           
    2321             msg = 'urs n velocity is not equivalent to sts ymomentum for station %i' %j
    2322             assert num.allclose(urs_n[index_start_urs_n:index_end_urs_n]*(urs_stage[index_start_urs_n:index_end_urs_n]-elevation[j]),
    2323                             sts_ymom[index_start_y:index_end_y],
    2324                             rtol=1.0e-5, atol=1.0e-4 ), msg
    2325 
    2326         fid.close()
    2327        
    2328         os.remove(sts_name_out+'.sts')
    2329        
    2330        
    2331        
    2332     def test_urs2sts_ordering(self):
    2333         """Test multiple sources with ordering file
    2334         """
    2335        
    2336         tide = 0.35
    2337         time_step_count = 6 # I made this a little longer (Ole)
    2338         time_step = 2
    2339         lat_long_points =[(-21.5,114.5),(-21,114.5),(-21.5,115), (-21.,115.)]
    2340         n=len(lat_long_points)
    2341         first_tstep=num.ones(n,num.int)
    2342         first_tstep[0]+=1
    2343         first_tstep[2]+=1
    2344         last_tstep=(time_step_count)*num.ones(n,num.int)
    2345         last_tstep[0]-=1
    2346 
    2347         gauge_depth=20*num.ones(n,num.float)
    2348         ha=2*num.ones((n,time_step_count),num.float)
    2349         ha[0]=num.arange(0,time_step_count)
    2350         ha[1]=num.arange(time_step_count,2*time_step_count)
    2351         ha[2]=num.arange(2*time_step_count,3*time_step_count)
    2352         ha[3]=num.arange(3*time_step_count,4*time_step_count)
    2353         ua=5*num.ones((n,time_step_count),num.float)
    2354         va=-10*num.ones((n,time_step_count),num.float)
    2355 
    2356         # Create two identical mux files to be combined by urs2sts
    2357         base_nameI, filesI = self.write_mux2(lat_long_points,
    2358                                              time_step_count, time_step,
    2359                                              first_tstep, last_tstep,
    2360                                              depth=gauge_depth,
    2361                                              ha=ha,
    2362                                              ua=ua,
    2363                                              va=va)
    2364 
    2365         base_nameII, filesII = self.write_mux2(lat_long_points,
    2366                                                time_step_count, time_step,
    2367                                                first_tstep, last_tstep,
    2368                                                depth=gauge_depth,
    2369                                                ha=ha,
    2370                                                ua=ua,
    2371                                                va=va)
    2372 
    2373                                                
    2374         # Create ordering file
    2375         permutation = [3,0,2]
    2376 
    2377         _, ordering_filename = tempfile.mkstemp('')
    2378         order_fid = open(ordering_filename, 'w') 
    2379         order_fid.write('index, longitude, latitude\n')
    2380         for index in permutation:
    2381             order_fid.write('%d, %f, %f\n' %(index,
    2382                                              lat_long_points[index][1],
    2383                                              lat_long_points[index][0]))
    2384         order_fid.close()
    2385        
    2386            
    2387 
    2388                                                
    2389         # Call urs2sts with multiple mux files
    2390         urs2sts([base_nameI, base_nameII],
    2391                 basename_out=base_nameI,
    2392                 ordering_filename=ordering_filename,
    2393                 weights=[1.0, 1.0],
    2394                 mean_stage=tide,
    2395                 verbose=False)
    2396 
    2397         # now I want to check the sts file ...
    2398         sts_file = base_nameI + '.sts'
    2399 
    2400         #Let's interrogate the sts file
    2401         # Note, the sts info is not gridded.  It is point data.
    2402         fid = NetCDFFile(sts_file)
    2403        
    2404         # Check that original indices have been stored
    2405         stored_permutation = fid.variables['permutation'][:]
    2406         msg = 'Permutation was not stored correctly. I got '
    2407         msg += str(stored_permutation)
    2408         assert num.allclose(stored_permutation, permutation), msg
    2409        
    2410 
    2411         # Make x and y absolute
    2412         x = fid.variables['x'][:]
    2413         y = fid.variables['y'][:]
    2414 
    2415         geo_reference = Geo_reference(NetCDFObject=fid)
    2416         points = geo_reference.get_absolute(map(None, x, y))
    2417         points = ensure_numeric(points)
    2418 
    2419         x = points[:,0]
    2420         y = points[:,1]
    2421 
    2422         #print
    2423         #print x
    2424         #print y
    2425         for i, index in enumerate(permutation):
    2426             # Check that STS points are stored in the correct order
    2427            
    2428             # Work out the UTM coordinates sts point i
    2429             zone, e, n = redfearn(lat_long_points[index][0],
    2430                                   lat_long_points[index][1])             
    2431 
    2432             #print i, [x[i],y[i]], [e,n]
    2433             assert num.allclose([x[i],y[i]], [e,n])
    2434            
    2435                        
    2436         # Check the time vector
    2437         times = fid.variables['time'][:]
    2438 
    2439         times_actual = []
    2440         for i in range(time_step_count):
    2441             times_actual.append(time_step * i)
    2442 
    2443         assert num.allclose(ensure_numeric(times),
    2444                             ensure_numeric(times_actual))
    2445                        
    2446 
    2447         # Check sts values
    2448         stage = fid.variables['stage'][:]
    2449         xmomentum = fid.variables['xmomentum'][:]
    2450         ymomentum = fid.variables['ymomentum'][:]
    2451         elevation = fid.variables['elevation'][:]
    2452 
    2453         # Set original data used to write mux file to be zero when gauges are
    2454         # not recdoring
    2455        
    2456         ha[0][0]=0.0
    2457         ha[0][time_step_count-1]=0.0
    2458         ha[2][0]=0.0
    2459         ua[0][0]=0.0
    2460         ua[0][time_step_count-1]=0.0
    2461         ua[2][0]=0.0
    2462         va[0][0]=0.0
    2463         va[0][time_step_count-1]=0.0
    2464         va[2][0]=0.0;
    2465 
    2466        
    2467         # The stage stored in the .sts file should be the sum of the stage
    2468         # in the two mux2 files because both have weights = 1. In this case
    2469         # the mux2 files are the same so stage == 2.0 * ha
    2470         #print 2.0*num.transpose(ha) - stage
    2471        
    2472         ha_permutation = num.take(ha, permutation, axis=0)
    2473         ua_permutation = num.take(ua, permutation, axis=0)
    2474         va_permutation = num.take(va, permutation, axis=0)
    2475         gauge_depth_permutation = num.take(gauge_depth, permutation, axis=0)
    2476 
    2477        
    2478         assert num.allclose(2.0*num.transpose(ha_permutation)+tide, stage)  # Meters
    2479 
    2480         #Check the momentums - ua
    2481         #momentum = velocity*(stage-elevation)
    2482         # elevation = - depth
    2483         #momentum = velocity_ua *(stage+depth)
    2484 
    2485         depth=num.zeros((len(lat_long_points),time_step_count),num.float)
    2486         for i in range(len(lat_long_points)):
    2487             depth[i]=gauge_depth[i]+tide+2.0*ha[i]
    2488             #2.0*ha necessary because using two files with weights=1 are used
    2489            
    2490         depth_permutation = num.take(depth, permutation, axis=0)                     
    2491        
    2492 
    2493         # The xmomentum stored in the .sts file should be the sum of the ua
    2494         # in the two mux2 files multiplied by the depth.
    2495         assert num.allclose(2.0*num.transpose(ua_permutation*depth_permutation), xmomentum)
    2496 
    2497         #Check the momentums - va
    2498         #momentum = velocity*(stage-elevation)
    2499         # elevation = - depth
    2500         #momentum = velocity_va *(stage+depth)
    2501 
    2502         # The ymomentum stored in the .sts file should be the sum of the va
    2503         # in the two mux2 files multiplied by the depth.
    2504         assert num.allclose(2.0*num.transpose(va_permutation*depth_permutation), ymomentum)
    2505 
    2506         # check the elevation values.
    2507         # -ve since urs measures depth, sww meshers height,
    2508         assert num.allclose(-gauge_depth_permutation, elevation)  #Meters
    2509 
    2510         fid.close()
    2511         self.delete_mux(filesI)
    2512         self.delete_mux(filesII)
    2513         os.remove(sts_file)
    2514        
    2515 
    2516        
    2517        
    2518        
    2519     def Xtest_urs2sts_ordering_exception(self):
    2520         """Test that inconsistent lats and lons in ordering file are caught.
    2521         """
    2522        
    2523         tide=0
    2524         time_step_count = 3
    2525         time_step = 2
    2526         lat_long_points =[(-21.5,114.5),(-21,114.5),(-21.5,115), (-21.,115.)]
    2527         n=len(lat_long_points)
    2528         first_tstep=num.ones(n,num.int)
    2529         first_tstep[0]+=1
    2530         first_tstep[2]+=1
    2531         last_tstep=(time_step_count)*num.ones(n,num.int)
    2532         last_tstep[0]-=1
    2533 
    2534         gauge_depth=20*num.ones(n,num.float)
    2535         ha=2*num.ones((n,time_step_count),num.float)
    2536         ha[0]=num.arange(0,time_step_count)
    2537         ha[1]=num.arange(time_step_count,2*time_step_count)
    2538         ha[2]=num.arange(2*time_step_count,3*time_step_count)
    2539         ha[3]=num.arange(3*time_step_count,4*time_step_count)
    2540         ua=5*num.ones((n,time_step_count),num.float)
    2541         va=-10*num.ones((n,time_step_count),num.float)
    2542 
    2543         # Create two identical mux files to be combined by urs2sts
    2544         base_nameI, filesI = self.write_mux2(lat_long_points,
    2545                                              time_step_count, time_step,
    2546                                              first_tstep, last_tstep,
    2547                                              depth=gauge_depth,
    2548                                              ha=ha,
    2549                                              ua=ua,
    2550                                              va=va)
    2551 
    2552         base_nameII, filesII = self.write_mux2(lat_long_points,
    2553                                                time_step_count, time_step,
    2554                                                first_tstep, last_tstep,
    2555                                                depth=gauge_depth,
    2556                                                ha=ha,
    2557                                                ua=ua,
    2558                                                va=va)
    2559 
    2560                                                
    2561         # Create ordering file
    2562         permutation = [3,0,2]
    2563 
    2564         # Do it wrongly and check that exception is being raised
    2565         _, ordering_filename = tempfile.mkstemp('')
    2566         order_fid = open(ordering_filename, 'w') 
    2567         order_fid.write('index, longitude, latitude\n')
    2568         for index in permutation:
    2569             order_fid.write('%d, %f, %f\n' %(index,
    2570                                              lat_long_points[index][0],
    2571                                              lat_long_points[index][1]))
    2572         order_fid.close()
    2573        
    2574         try:
    2575             urs2sts([base_nameI, base_nameII],
    2576                     basename_out=base_nameI,
    2577                     ordering_filename=ordering_filename,
    2578                     weights=[1.0, 1.0],
    2579                     mean_stage=tide,
    2580                     verbose=False) 
    2581             os.remove(ordering_filename)           
    2582         except:
    2583             pass
    2584         else:
    2585             msg = 'Should have caught wrong lat longs'
    2586             raise Exception, msg
    2587 
    2588        
    2589         self.delete_mux(filesI)
    2590         self.delete_mux(filesII)
    2591 
    2592        
    2593 
    2594        
    2595     def test_urs2sts_ordering_different_sources(self):
    2596         """Test multiple sources with ordering file, different source files and weights.
    2597            This test also has more variable values than the previous ones
    2598         """
    2599        
    2600         tide = 1.5
    2601         time_step_count = 10
    2602         time_step = 0.2
    2603        
    2604         times_ref = num.arange(0, time_step_count*time_step, time_step)
    2605         #print 'time vector', times_ref
    2606        
    2607         lat_long_points = [(-21.5,114.5), (-21,114.5), (-21.5,115), (-21.,115.), (-22., 117.)]
    2608         n = len(lat_long_points)
    2609        
    2610         # Create non-trivial weights
    2611         #weights = [0.8, 1.5] # OK
    2612         #weights = [0.8, 10.5] # Fail (up to allclose tolerance)
    2613         #weights = [10.5, 10.5] # OK
    2614         #weights = [0.0, 10.5] # OK
    2615         #weights = [0.8, 0.] # OK               
    2616         #weights = [8, 0.1] # OK                       
    2617         #weights = [0.8, 10.0] # OK                               
    2618         #weights = [0.8, 10.6] # OK           
    2619         weights = [3.8, 7.6] # OK                   
    2620         #weights = [0.5, 0.5] # OK                           
    2621         #weights = [2., 2.] # OK                           
    2622         #weights = [0.0, 0.5] # OK                                         
    2623         #weights = [1.0, 1.0] # OK                                                 
    2624        
    2625        
    2626         # Create different timeseries starting and ending at different times
    2627         first_tstep=num.ones(n,num.int)
    2628         first_tstep[0]+=2   # Point 0 starts at 2
    2629         first_tstep[1]+=4   # Point 1 starts at 4       
    2630         first_tstep[2]+=3   # Point 2 starts at 3
    2631        
    2632         last_tstep=(time_step_count)*num.ones(n,num.int)
    2633         last_tstep[0]-=1    # Point 0 ends 1 step early
    2634         last_tstep[1]-=2    # Point 1 ends 2 steps early               
    2635         last_tstep[4]-=3    # Point 4 ends 3 steps early       
    2636        
    2637         #print
    2638         #print 'time_step_count', time_step_count
    2639         #print 'time_step', time_step
    2640         #print 'first_tstep', first_tstep
    2641         #print 'last_tstep', last_tstep               
    2642        
    2643        
    2644         # Create varying elevation data (positive values for seafloor)
    2645         gauge_depth=20*num.ones(n,num.float)
    2646         for i in range(n):
    2647             gauge_depth[i] += i**2
    2648            
    2649         #print 'gauge_depth', gauge_depth
    2650        
    2651         # Create data to be written to first mux file       
    2652         ha0=2*num.ones((n,time_step_count),num.float)
    2653         ha0[0]=num.arange(0,time_step_count)
    2654         ha0[1]=num.arange(time_step_count,2*time_step_count)
    2655         ha0[2]=num.arange(2*time_step_count,3*time_step_count)
    2656         ha0[3]=num.arange(3*time_step_count,4*time_step_count)
    2657         ua0=5*num.ones((n,time_step_count),num.float)
    2658         va0=-10*num.ones((n,time_step_count),num.float)
    2659 
    2660         # Ensure data used to write mux file to be zero when gauges are
    2661         # not recording
    2662         for i in range(n):
    2663              # For each point
    2664              
    2665              for j in range(0, first_tstep[i]-1) + range(last_tstep[i], time_step_count):
    2666                  # For timesteps before and after recording range
    2667                  ha0[i][j] = ua0[i][j] = va0[i][j] = 0.0                                 
    2668 
    2669 
    2670                  
    2671         #print
    2672         #print 'using varying start and end time'
    2673         #print 'ha0', ha0[4]
    2674         #print 'ua0', ua0
    2675         #print 'va0', va0       
    2676        
    2677         # Write first mux file to be combined by urs2sts
    2678         base_nameI, filesI = self.write_mux2(lat_long_points,
    2679                                              time_step_count, time_step,
    2680                                              first_tstep, last_tstep,
    2681                                              depth=gauge_depth,
    2682                                              ha=ha0,
    2683                                              ua=ua0,
    2684                                              va=va0)
    2685 
    2686                                              
    2687                                              
    2688         # Create data to be written to second mux file       
    2689         ha1=num.ones((n,time_step_count),num.float)
    2690         ha1[0]=num.sin(times_ref)
    2691         ha1[1]=2*num.sin(times_ref - 3)
    2692         ha1[2]=5*num.sin(4*times_ref)
    2693         ha1[3]=num.sin(times_ref)
    2694         ha1[4]=num.sin(2*times_ref-0.7)
    2695                
    2696         ua1=num.zeros((n,time_step_count),num.float)
    2697         ua1[0]=3*num.cos(times_ref)       
    2698         ua1[1]=2*num.sin(times_ref-0.7)   
    2699         ua1[2]=num.arange(3*time_step_count,4*time_step_count)
    2700         ua1[4]=2*num.ones(time_step_count)
    2701        
    2702         va1=num.zeros((n,time_step_count),num.float)
    2703         va1[0]=2*num.cos(times_ref-0.87)       
    2704         va1[1]=3*num.ones(time_step_count, num.int)       #array default#
    2705         va1[3]=2*num.sin(times_ref-0.71)       
    2706        
    2707        
    2708         # Ensure data used to write mux file to be zero when gauges are
    2709         # not recording
    2710         for i in range(n):
    2711              # For each point
    2712              
    2713              for j in range(0, first_tstep[i]-1) + range(last_tstep[i], time_step_count):
    2714                  # For timesteps before and after recording range
    2715                  ha1[i][j] = ua1[i][j] = va1[i][j] = 0.0                                 
    2716 
    2717 
    2718         #print
    2719         #print 'using varying start and end time'
    2720         #print 'ha1', ha1[4]
    2721         #print 'ua1', ua1
    2722         #print 'va1', va1       
    2723                                              
    2724                                              
    2725         # Write second mux file to be combined by urs2sts                                             
    2726         base_nameII, filesII = self.write_mux2(lat_long_points,
    2727                                                time_step_count, time_step,
    2728                                                first_tstep, last_tstep,
    2729                                                depth=gauge_depth,
    2730                                                ha=ha1,
    2731                                                ua=ua1,
    2732                                                va=va1)
    2733 
    2734                                                
    2735         # Create ordering file
    2736         permutation = [4,0,2]
    2737 
    2738         _, ordering_filename = tempfile.mkstemp('')
    2739         order_fid = open(ordering_filename, 'w') 
    2740         order_fid.write('index, longitude, latitude\n')
    2741         for index in permutation:
    2742             order_fid.write('%d, %f, %f\n' %(index,
    2743                                              lat_long_points[index][1],
    2744                                              lat_long_points[index][0]))
    2745         order_fid.close()
    2746        
    2747            
    2748 
    2749         #------------------------------------------------------------
    2750         # Now read the mux files one by one without weights and test
    2751        
    2752         # Call urs2sts with mux file #0
    2753         urs2sts([base_nameI],
    2754                 basename_out=base_nameI,
    2755                 ordering_filename=ordering_filename,
    2756                 mean_stage=tide,
    2757                 verbose=False)
    2758 
    2759         # Now read the sts file and check that values have been stored correctly.
    2760         sts_file = base_nameI + '.sts'
    2761         fid = NetCDFFile(sts_file)
    2762        
    2763 
    2764         # Check that original indices have been stored
    2765         stored_permutation = fid.variables['permutation'][:]
    2766         msg = 'Permutation was not stored correctly. I got '
    2767         msg += str(stored_permutation)
    2768         assert num.allclose(stored_permutation, permutation), msg
    2769        
    2770 
    2771        
    2772        
    2773         # Make x and y absolute
    2774         x = fid.variables['x'][:]
    2775         y = fid.variables['y'][:]
    2776 
    2777         geo_reference = Geo_reference(NetCDFObject=fid)
    2778         points = geo_reference.get_absolute(map(None, x, y))
    2779         points = ensure_numeric(points)
    2780 
    2781         x = points[:,0]
    2782         y = points[:,1]
    2783 
    2784         for i, index in enumerate(permutation):
    2785             # Check that STS points are stored in the correct order
    2786            
    2787             # Work out the UTM coordinates sts point i
    2788             zone, e, n = redfearn(lat_long_points[index][0],
    2789                                   lat_long_points[index][1])             
    2790 
    2791             #print i, [x[i],y[i]], [e,n]
    2792             assert num.allclose([x[i],y[i]], [e,n])
    2793            
    2794                        
    2795         # Check the time vector
    2796         times = fid.variables['time'][:]
    2797         assert num.allclose(ensure_numeric(times),
    2798                             ensure_numeric(times_ref))
    2799                        
    2800 
    2801         # Check sts values for mux #0
    2802         stage0 = fid.variables['stage'][:].copy()
    2803         xmomentum0 = fid.variables['xmomentum'][:].copy()
    2804         ymomentum0 = fid.variables['ymomentum'][:].copy()
    2805         elevation0 = fid.variables['elevation'][:].copy()
    2806 
    2807        
    2808         #print 'stage', stage0
    2809         #print 'xmomentum', xmomentum0
    2810         #print 'ymomentum', ymomentum0       
    2811         #print 'elevation', elevation0
    2812        
    2813         # The quantities stored in the .sts file should be the weighted sum of the
    2814         # quantities written to the mux2 files subject to the permutation vector.
    2815        
    2816         ha_ref = num.take(ha0, permutation, axis=0)
    2817         ua_ref = num.take(ua0, permutation, axis=0)       
    2818         va_ref = num.take(va0, permutation, axis=0)               
    2819 
    2820         gauge_depth_ref = num.take(gauge_depth, permutation, axis=0)                     
    2821        
    2822         assert num.allclose(num.transpose(ha_ref)+tide, stage0)  # Meters
    2823        
    2824        
    2825        
    2826         #Check the momentums - ua
    2827         #momentum = velocity*(stage-elevation)
    2828         # elevation = - depth
    2829         #momentum = velocity_ua *(stage+depth)
    2830 
    2831         depth_ref = num.zeros((len(permutation), time_step_count), num.float)
    2832         for i in range(len(permutation)):
    2833             depth_ref[i]=gauge_depth_ref[i]+tide+ha_ref[i]
    2834 
    2835 
    2836         # The xmomentum stored in the .sts file should be the sum of the ua
    2837         # in the two mux2 files multiplied by the depth.
    2838         assert num.allclose(num.transpose(ua_ref*depth_ref), xmomentum0)
    2839 
    2840         #Check the momentums - va
    2841         #momentum = velocity*(stage-elevation)
    2842         # elevation = - depth
    2843         #momentum = velocity_va *(stage+depth)
    2844 
    2845         # The ymomentum stored in the .sts file should be the sum of the va
    2846         # in the two mux2 files multiplied by the depth.
    2847        
    2848        
    2849         #print transpose(va_ref*depth_ref)
    2850         #print ymomentum
    2851         assert num.allclose(num.transpose(va_ref*depth_ref), ymomentum0)       
    2852 
    2853         # check the elevation values.
    2854         # -ve since urs measures depth, sww meshers height,
    2855         assert num.allclose(-gauge_depth_ref, elevation0)
    2856 
    2857         fid.close()
    2858         os.remove(sts_file)
    2859        
    2860        
    2861 
    2862        
    2863         # Call urs2sts with mux file #1
    2864         urs2sts([base_nameII],
    2865                 basename_out=base_nameI,
    2866                 ordering_filename=ordering_filename,
    2867                 mean_stage=tide,
    2868                 verbose=False)
    2869 
    2870         # Now read the sts file and check that values have been stored correctly.
    2871         sts_file = base_nameI + '.sts'
    2872         fid = NetCDFFile(sts_file)
    2873        
    2874        
    2875         # Check that original indices have been stored
    2876         stored_permutation = fid.variables['permutation'][:]
    2877         msg = 'Permutation was not stored correctly. I got '
    2878         msg += str(stored_permutation)
    2879         assert num.allclose(stored_permutation, permutation), msg
    2880        
    2881         # Make x and y absolute
    2882         x = fid.variables['x'][:]
    2883         y = fid.variables['y'][:]
    2884 
    2885         geo_reference = Geo_reference(NetCDFObject=fid)
    2886         points = geo_reference.get_absolute(map(None, x, y))
    2887         points = ensure_numeric(points)
    2888 
    2889         x = points[:,0]
    2890         y = points[:,1]
    2891 
    2892         for i, index in enumerate(permutation):
    2893             # Check that STS points are stored in the correct order
    2894            
    2895             # Work out the UTM coordinates sts point i
    2896             zone, e, n = redfearn(lat_long_points[index][0],
    2897                                   lat_long_points[index][1])             
    2898 
    2899             #print i, [x[i],y[i]], [e,n]
    2900             assert num.allclose([x[i],y[i]], [e,n])
    2901            
    2902                        
    2903         # Check the time vector
    2904         times = fid.variables['time'][:]
    2905         assert num.allclose(ensure_numeric(times),
    2906                             ensure_numeric(times_ref))
    2907                        
    2908 
    2909         # Check sts values for mux #1
    2910         stage1 = fid.variables['stage'][:].copy()
    2911         xmomentum1 = fid.variables['xmomentum'][:].copy()
    2912         ymomentum1 = fid.variables['ymomentum'][:].copy()
    2913         elevation1 = fid.variables['elevation'][:].copy()
    2914 
    2915        
    2916         #print 'stage', stage1
    2917         #print 'xmomentum', xmomentum1
    2918         #print 'ymomentum', ymomentum1       
    2919         #print 'elevation', elevation1
    2920        
    2921         # The quantities stored in the .sts file should be the weighted sum of the
    2922         # quantities written to the mux2 files subject to the permutation vector.
    2923        
    2924         ha_ref = num.take(ha1, permutation, axis=0)
    2925         ua_ref = num.take(ua1, permutation, axis=0)       
    2926         va_ref = num.take(va1, permutation, axis=0)               
    2927 
    2928         gauge_depth_ref = num.take(gauge_depth, permutation, axis=0)                         
    2929 
    2930 
    2931         #print
    2932         #print stage1
    2933         #print transpose(ha_ref)+tide - stage1
    2934        
    2935 
    2936         assert num.allclose(num.transpose(ha_ref)+tide, stage1)  # Meters
    2937         #import sys; sys.exit()
    2938 
    2939         #Check the momentums - ua
    2940         #momentum = velocity*(stage-elevation)
    2941         # elevation = - depth
    2942         #momentum = velocity_ua *(stage+depth)
    2943 
    2944         depth_ref = num.zeros((len(permutation), time_step_count), num.float)
    2945         for i in range(len(permutation)):
    2946             depth_ref[i]=gauge_depth_ref[i]+tide+ha_ref[i]
    2947 
    2948 
    2949         # The xmomentum stored in the .sts file should be the sum of the ua
    2950         # in the two mux2 files multiplied by the depth.
    2951         assert num.allclose(num.transpose(ua_ref*depth_ref), xmomentum1)
    2952 
    2953         #Check the momentums - va
    2954         #momentum = velocity*(stage-elevation)
    2955         # elevation = - depth
    2956         #momentum = velocity_va *(stage+depth)
    2957 
    2958         # The ymomentum stored in the .sts file should be the sum of the va
    2959         # in the two mux2 files multiplied by the depth.
    2960        
    2961        
    2962         #print transpose(va_ref*depth_ref)
    2963         #print ymomentum
    2964         assert num.allclose(num.transpose(va_ref*depth_ref), ymomentum1)       
    2965 
    2966         # check the elevation values.
    2967         # -ve since urs measures depth, sww meshers height,
    2968         assert num.allclose(-gauge_depth_ref, elevation1)
    2969 
    2970         fid.close()
    2971         os.remove(sts_file)
    2972        
    2973         #----------------------
    2974         # Then read the mux files together and test
    2975        
    2976                                                
    2977         # Call urs2sts with multiple mux files
    2978         urs2sts([base_nameI, base_nameII],
    2979                 basename_out=base_nameI,
    2980                 ordering_filename=ordering_filename,
    2981                 weights=weights,
    2982                 mean_stage=tide,
    2983                 verbose=False)
    2984 
    2985         # Now read the sts file and check that values have been stored correctly.
    2986         sts_file = base_nameI + '.sts'
    2987         fid = NetCDFFile(sts_file)
    2988 
    2989         # Make x and y absolute
    2990         x = fid.variables['x'][:]
    2991         y = fid.variables['y'][:]
    2992 
    2993         geo_reference = Geo_reference(NetCDFObject=fid)
    2994         points = geo_reference.get_absolute(map(None, x, y))
    2995         points = ensure_numeric(points)
    2996 
    2997         x = points[:,0]
    2998         y = points[:,1]
    2999 
    3000         for i, index in enumerate(permutation):
    3001             # Check that STS points are stored in the correct order
    3002            
    3003             # Work out the UTM coordinates sts point i
    3004             zone, e, n = redfearn(lat_long_points[index][0],
    3005                                   lat_long_points[index][1])             
    3006 
    3007             #print i, [x[i],y[i]], [e,n]
    3008             assert num.allclose([x[i],y[i]], [e,n])
    3009            
    3010                        
    3011         # Check the time vector
    3012         times = fid.variables['time'][:]
    3013         assert num.allclose(ensure_numeric(times),
    3014                             ensure_numeric(times_ref))
    3015                        
    3016 
    3017         # Check sts values
    3018         stage = fid.variables['stage'][:].copy()
    3019         xmomentum = fid.variables['xmomentum'][:].copy()
    3020         ymomentum = fid.variables['ymomentum'][:].copy()
    3021         elevation = fid.variables['elevation'][:].copy()
    3022 
    3023        
    3024         #print 'stage', stage
    3025         #print 'elevation', elevation
    3026        
    3027         # The quantities stored in the .sts file should be the weighted sum of the
    3028         # quantities written to the mux2 files subject to the permutation vector.
    3029        
    3030         ha_ref = (weights[0]*num.take(ha0, permutation, axis=0)
    3031                   + weights[1]*num.take(ha1, permutation, axis=0))
    3032         ua_ref = (weights[0]*num.take(ua0, permutation, axis=0)
    3033                   + weights[1]*num.take(ua1, permutation, axis=0))
    3034         va_ref = (weights[0]*num.take(va0, permutation, axis=0)
    3035                   + weights[1]*num.take(va1, permutation, axis=0))
    3036 
    3037         gauge_depth_ref = num.take(gauge_depth, permutation, axis=0)                         
    3038 
    3039 
    3040         #print
    3041         #print stage
    3042         #print transpose(ha_ref)+tide - stage
    3043 
    3044         assert num.allclose(num.transpose(ha_ref)+tide, stage)  # Meters
    3045 
    3046         #Check the momentums - ua
    3047         #momentum = velocity*(stage-elevation)
    3048         # elevation = - depth
    3049         #momentum = velocity_ua *(stage+depth)
    3050 
    3051         depth_ref = num.zeros((len(permutation), time_step_count), num.float)
    3052         for i in range(len(permutation)):
    3053             depth_ref[i]=gauge_depth_ref[i]+tide+ha_ref[i]
    3054 
    3055            
    3056        
    3057 
    3058         # The xmomentum stored in the .sts file should be the sum of the ua
    3059         # in the two mux2 files multiplied by the depth.
    3060         assert num.allclose(num.transpose(ua_ref*depth_ref), xmomentum)
    3061 
    3062         #Check the momentums - va
    3063         #momentum = velocity*(stage-elevation)
    3064         # elevation = - depth
    3065         #momentum = velocity_va *(stage+depth)
    3066 
    3067         # The ymomentum stored in the .sts file should be the sum of the va
    3068         # in the two mux2 files multiplied by the depth.
    3069        
    3070        
    3071         #print transpose(va_ref*depth_ref)
    3072         #print ymomentum
    3073 
    3074         assert num.allclose(num.transpose(va_ref*depth_ref), ymomentum)
    3075 
    3076         # check the elevation values.
    3077         # -ve since urs measures depth, sww meshers height,
    3078         assert num.allclose(-gauge_depth_ref, elevation)  #Meters
    3079 
    3080         fid.close()
    3081         self.delete_mux(filesI)
    3082         self.delete_mux(filesII)
    3083         os.remove(sts_file)
    3084        
    3085         #---------------
    3086         # "Manually" add the timeseries up with weights and test
    3087         # Tide is discounted from individual results and added back in       
    3088         #
    3089 
    3090         stage_man = weights[0]*(stage0-tide) + weights[1]*(stage1-tide) + tide
    3091         assert num.allclose(stage_man, stage)
    3092                
    3093        
    3094     def test_file_boundary_stsI(self):
    3095         """test_file_boundary_stsI(self):
    3096         """
    3097        
    3098         # FIXME (Ole): These tests should really move to
    3099         # test_generic_boundaries.py
    3100        
    3101         from anuga.shallow_water import Domain
    3102         from anuga.shallow_water import Reflective_boundary
    3103         from anuga.shallow_water import Dirichlet_boundary
    3104         from anuga.shallow_water import File_boundary
    3105         from anuga.pmesh.mesh_interface import create_mesh_from_regions
    3106 
    3107         bounding_polygon=[[6.0,97.0],[6.01,97.0],[6.02,97.0],[6.02,97.02],[6.00,97.02]]
    3108         tide = 0.37
    3109         time_step_count = 5
    3110         time_step = 2
    3111         lat_long_points =bounding_polygon[0:3]
    3112         n=len(lat_long_points)
    3113         first_tstep=num.ones(n,num.int)
    3114         last_tstep=(time_step_count)*num.ones(n,num.int)
    3115 
    3116         h = 20       
    3117         w = 2
    3118         u = 10
    3119         v = -10
    3120         gauge_depth=h*num.ones(n,num.float)
    3121         ha=w*num.ones((n,time_step_count),num.float)
    3122         ua=u*num.ones((n,time_step_count),num.float)
    3123         va=v*num.ones((n,time_step_count),num.float)
    3124         base_name, files = self.write_mux2(lat_long_points,
    3125                                            time_step_count, time_step,
    3126                                            first_tstep, last_tstep,
    3127                                            depth=gauge_depth,
    3128                                            ha=ha,
    3129                                            ua=ua,
    3130                                            va=va)
    3131 
    3132         sts_file=base_name
    3133         urs2sts(base_name,
    3134                 sts_file,
    3135                 mean_stage=tide,
    3136                 verbose=False)
    3137         self.delete_mux(files)
    3138 
    3139         #print 'start create mesh from regions'
    3140         for i in range(len(bounding_polygon)):
    3141             zone,bounding_polygon[i][0],bounding_polygon[i][1]=redfearn(bounding_polygon[i][0],bounding_polygon[i][1])
    3142         extent_res=1000000
    3143         meshname = 'urs_test_mesh' + '.tsh'
    3144         interior_regions=None
    3145         boundary_tags={'ocean': [0,1], 'otherocean': [2,3,4]}
    3146         create_mesh_from_regions(bounding_polygon,
    3147                                  boundary_tags=boundary_tags,
    3148                                  maximum_triangle_area=extent_res,
    3149                                  filename=meshname,
    3150                                  interior_regions=interior_regions,
    3151                                  verbose=False)
    3152        
    3153         domain_fbound = Domain(meshname)
    3154         domain_fbound.set_quantity('stage', tide)
    3155         Bf = File_boundary(sts_file+'.sts',
    3156                            domain_fbound,
    3157                            boundary_polygon=bounding_polygon)
    3158         Br = Reflective_boundary(domain_fbound)
    3159         Bd = Dirichlet_boundary([w+tide, u*(w+h+tide), v*(w+h+tide)])       
    3160 
    3161         domain_fbound.set_boundary({'ocean': Bf,'otherocean': Br})
    3162        
    3163         # Check boundary object evaluates as it should
    3164         for i, ((vol_id, edge_id), B) in enumerate(domain_fbound.boundary_objects):
    3165             if B is Bf:
    3166            
    3167                 qf = B.evaluate(vol_id, edge_id)  # File boundary
    3168                 qd = Bd.evaluate(vol_id, edge_id) # Dirichlet boundary
    3169 
    3170                 assert num.allclose(qf, qd)
    3171                
    3172        
    3173         # Evolve
    3174         finaltime=time_step*(time_step_count-1)
    3175         yieldstep=time_step
    3176         temp_fbound=num.zeros(int(finaltime/yieldstep)+1,num.float)
    3177 
    3178         for i, t in enumerate(domain_fbound.evolve(yieldstep=yieldstep,
    3179                                                    finaltime=finaltime,
    3180                                                    skip_initial_step=False)):
    3181                                                    
    3182             D = domain_fbound
    3183             temp_fbound[i]=D.quantities['stage'].centroid_values[2]
    3184 
    3185             # Check that file boundary object has populated
    3186             # boundary array correctly 
    3187             # FIXME (Ole): Do this for the other tests too!
    3188             for j, val in enumerate(D.get_quantity('stage').boundary_values):
    3189            
    3190                 (vol_id, edge_id), B = D.boundary_objects[j]
    3191                 if isinstance(B, File_boundary):
    3192                     #print j, val
    3193                     assert num.allclose(val, w + tide)
    3194 
    3195 
    3196        
    3197         domain_drchlt = Domain(meshname)
    3198         domain_drchlt.set_quantity('stage', tide)
    3199         Br = Reflective_boundary(domain_drchlt)
    3200 
    3201         domain_drchlt.set_boundary({'ocean': Bd,'otherocean': Br})
    3202         temp_drchlt=num.zeros(int(finaltime/yieldstep)+1,num.float)
    3203 
    3204         for i, t in enumerate(domain_drchlt.evolve(yieldstep=yieldstep,
    3205                                                    finaltime=finaltime,
    3206                                                    skip_initial_step=False)):
    3207             temp_drchlt[i]=domain_drchlt.quantities['stage'].centroid_values[2]
    3208 
    3209         #print domain_fbound.quantities['stage'].vertex_values
    3210         #print domain_drchlt.quantities['stage'].vertex_values
    3211                    
    3212         assert num.allclose(temp_fbound,temp_drchlt)
    3213        
    3214         assert num.allclose(domain_fbound.quantities['stage'].vertex_values,
    3215                             domain_drchlt.quantities['stage'].vertex_values)
    3216                        
    3217         assert num.allclose(domain_fbound.quantities['xmomentum'].vertex_values,
    3218                             domain_drchlt.quantities['xmomentum'].vertex_values)
    3219                        
    3220         assert num.allclose(domain_fbound.quantities['ymomentum'].vertex_values,
    3221                             domain_drchlt.quantities['ymomentum'].vertex_values)
    3222        
    3223        
    3224         os.remove(sts_file+'.sts')
    3225         os.remove(meshname)
    3226                
    3227        
    3228     def test_file_boundary_stsI_beyond_model_time(self):
    3229         """test_file_boundary_stsI(self):
    3230        
    3231         Test that file_boundary can work when model time
    3232         exceeds available data.
    3233         This is optional and requires the user to specify a default
    3234         boundary object.
    3235         """
    3236        
    3237         # Don't do warnings in unit test
    3238         import warnings
    3239         warnings.simplefilter('ignore')
    3240        
    3241         from anuga.shallow_water import Domain
    3242         from anuga.shallow_water import Reflective_boundary
    3243         from anuga.shallow_water import Dirichlet_boundary
    3244         from anuga.shallow_water import File_boundary
    3245         from anuga.pmesh.mesh_interface import create_mesh_from_regions
    3246 
    3247         bounding_polygon=[[6.0,97.0],[6.01,97.0],[6.02,97.0],
    3248                           [6.02,97.02],[6.00,97.02]]
    3249         tide = 0.37
    3250         time_step_count = 5
    3251         time_step = 2
    3252         lat_long_points = bounding_polygon[0:3]
    3253         n=len(lat_long_points)
    3254         first_tstep=num.ones(n,num.int)
    3255         last_tstep=(time_step_count)*num.ones(n,num.int)
    3256 
    3257         h = 20       
    3258         w = 2
    3259         u = 10
    3260         v = -10
    3261         gauge_depth=h*num.ones(n,num.float)
    3262         ha=w*num.ones((n,time_step_count),num.float)
    3263         ua=u*num.ones((n,time_step_count),num.float)
    3264         va=v*num.ones((n,time_step_count),num.float)
    3265         base_name, files = self.write_mux2(lat_long_points,
    3266                                            time_step_count, time_step,
    3267                                            first_tstep, last_tstep,
    3268                                            depth=gauge_depth,
    3269                                            ha=ha,
    3270                                            ua=ua,
    3271                                            va=va)
    3272 
    3273         sts_file=base_name
    3274         urs2sts(base_name,
    3275                 sts_file,
    3276                 mean_stage=tide,
    3277                 verbose=False)
    3278         self.delete_mux(files)
    3279 
    3280         #print 'start create mesh from regions'
    3281         for i in range(len(bounding_polygon)):
    3282             zone,\
    3283             bounding_polygon[i][0],\
    3284             bounding_polygon[i][1]=redfearn(bounding_polygon[i][0],
    3285                                             bounding_polygon[i][1])
    3286                                            
    3287         extent_res=1000000
    3288         meshname = 'urs_test_mesh' + '.tsh'
    3289         interior_regions=None
    3290         boundary_tags={'ocean': [0,1], 'otherocean': [2,3,4]}
    3291         create_mesh_from_regions(bounding_polygon,
    3292                                  boundary_tags=boundary_tags,
    3293                                  maximum_triangle_area=extent_res,
    3294                                  filename=meshname,
    3295                                  interior_regions=interior_regions,
    3296                                  verbose=False)
    3297        
    3298         domain_fbound = Domain(meshname)
    3299         domain_fbound.set_quantity('stage', tide)
    3300        
    3301         Br = Reflective_boundary(domain_fbound)
    3302         Bd = Dirichlet_boundary([w+tide, u*(w+h+tide), v*(w+h+tide)])       
    3303         Bf = File_boundary(sts_file+'.sts',
    3304                            domain_fbound,
    3305                            boundary_polygon=bounding_polygon,
    3306                            default_boundary=Bd) # Condition to be used
    3307                                                 # if model time exceeds
    3308                                                 # available data
    3309 
    3310         domain_fbound.set_boundary({'ocean': Bf,'otherocean': Br})
    3311        
    3312         # Check boundary object evaluates as it should
    3313         for i, ((vol_id, edge_id), B) in enumerate(domain_fbound.boundary_objects):
    3314             if B is Bf:
    3315            
    3316                 qf = B.evaluate(vol_id, edge_id)  # File boundary
    3317                 qd = Bd.evaluate(vol_id, edge_id) # Dirichlet boundary
    3318 
    3319                 assert num.allclose(qf, qd)
    3320                
    3321        
    3322         # Evolve
    3323         data_finaltime = time_step*(time_step_count-1)
    3324         finaltime = data_finaltime + 10 # Let model time exceed available data
    3325         yieldstep = time_step
    3326         temp_fbound=num.zeros(int(finaltime/yieldstep)+1, num.float)
    3327 
    3328         for i, t in enumerate(domain_fbound.evolve(yieldstep=yieldstep,
    3329                                                    finaltime=finaltime,
    3330                                                    skip_initial_step=False)):
    3331                                                    
    3332             D = domain_fbound
    3333             temp_fbound[i]=D.quantities['stage'].centroid_values[2]
    3334 
    3335             # Check that file boundary object has populated
    3336             # boundary array correctly 
    3337             # FIXME (Ole): Do this for the other tests too!
    3338             for j, val in enumerate(D.get_quantity('stage').boundary_values):
    3339            
    3340                 (vol_id, edge_id), B = D.boundary_objects[j]
    3341                 if isinstance(B, File_boundary):
    3342                     #print j, val
    3343                     assert num.allclose(val, w + tide)
    3344 
    3345 
    3346         domain_drchlt = Domain(meshname)
    3347         domain_drchlt.set_quantity('stage', tide)
    3348         Br = Reflective_boundary(domain_drchlt)
    3349 
    3350         domain_drchlt.set_boundary({'ocean': Bd,'otherocean': Br})
    3351         temp_drchlt=num.zeros(int(finaltime/yieldstep)+1,num.float)
    3352 
    3353         for i, t in enumerate(domain_drchlt.evolve(yieldstep=yieldstep,
    3354                                                    finaltime=finaltime,
    3355                                                    skip_initial_step=False)):
    3356             temp_drchlt[i]=domain_drchlt.quantities['stage'].centroid_values[2]
    3357 
    3358         #print domain_fbound.quantities['stage'].vertex_values
    3359         #print domain_drchlt.quantities['stage'].vertex_values
    3360                    
    3361         assert num.allclose(temp_fbound,temp_drchlt)
    3362        
    3363         assert num.allclose(domain_fbound.quantities['stage'].vertex_values,
    3364                             domain_drchlt.quantities['stage'].vertex_values)
    3365                        
    3366         assert num.allclose(domain_fbound.quantities['xmomentum'].vertex_values,
    3367                             domain_drchlt.quantities['xmomentum'].vertex_values)
    3368                        
    3369         assert num.allclose(domain_fbound.quantities['ymomentum'].vertex_values,
    3370                             domain_drchlt.quantities['ymomentum'].vertex_values)
    3371        
    3372         os.remove(sts_file+'.sts')
    3373         os.remove(meshname)
    3374                
    3375        
    3376     def test_field_boundary_stsI_beyond_model_time(self):
    3377         """test_field_boundary(self):
    3378        
    3379         Test that field_boundary can work when model time
    3380         exceeds available data whilst adjusting mean_stage.
    3381        
    3382         """
    3383        
    3384         # Don't do warnings in unit test
    3385         import warnings
    3386         warnings.simplefilter('ignore')
    3387        
    3388         from anuga.shallow_water import Domain
    3389         from anuga.shallow_water import Reflective_boundary
    3390         from anuga.shallow_water import Dirichlet_boundary
    3391         from anuga.shallow_water import File_boundary
    3392         from anuga.pmesh.mesh_interface import create_mesh_from_regions
    3393 
    3394         bounding_polygon=[[6.0,97.0],[6.01,97.0],[6.02,97.0],
    3395                           [6.02,97.02],[6.00,97.02]]
    3396         tide = 0.37
    3397         time_step_count = 5
    3398         time_step = 2
    3399         lat_long_points = bounding_polygon[0:3]
    3400         n=len(lat_long_points)
    3401         first_tstep=num.ones(n,num.int)
    3402         last_tstep=(time_step_count)*num.ones(n,num.int)
    3403 
    3404         h = 20       
    3405         w = 2
    3406         u = 10
    3407         v = -10
    3408         gauge_depth=h*num.ones(n,num.float)
    3409         ha=w*num.ones((n,time_step_count),num.float)
    3410         ua=u*num.ones((n,time_step_count),num.float)
    3411         va=v*num.ones((n,time_step_count),num.float)
    3412         base_name, files = self.write_mux2(lat_long_points,
    3413                                            time_step_count, time_step,
    3414                                            first_tstep, last_tstep,
    3415                                            depth=gauge_depth,
    3416                                            ha=ha,
    3417                                            ua=ua,
    3418                                            va=va)
    3419 
    3420         sts_file=base_name
    3421         urs2sts(base_name,
    3422                 sts_file,
    3423                 mean_stage=0.0, # Deliberately let Field_boundary do the adjustment
    3424                 verbose=False)
    3425         self.delete_mux(files)
    3426 
    3427         #print 'start create mesh from regions'
    3428         for i in range(len(bounding_polygon)):
    3429             zone,\
    3430             bounding_polygon[i][0],\
    3431             bounding_polygon[i][1]=redfearn(bounding_polygon[i][0],
    3432                                             bounding_polygon[i][1])
    3433                                            
    3434         extent_res=1000000
    3435         meshname = 'urs_test_mesh' + '.tsh'
    3436         interior_regions=None
    3437         boundary_tags={'ocean': [0,1], 'otherocean': [2,3,4]}
    3438         create_mesh_from_regions(bounding_polygon,
    3439                                  boundary_tags=boundary_tags,
    3440                                  maximum_triangle_area=extent_res,
    3441                                  filename=meshname,
    3442                                  interior_regions=interior_regions,
    3443                                  verbose=False)
    3444        
    3445         domain_fbound = Domain(meshname)
    3446         domain_fbound.set_quantity('stage', tide)
    3447        
    3448         Br = Reflective_boundary(domain_fbound)
    3449         Bd = Dirichlet_boundary([w+tide, u*(w+h), v*(w+h)])
    3450         Bdefault = Dirichlet_boundary([w, u*(w+h), v*(w+h)])       
    3451                
    3452         Bf = Field_boundary(sts_file+'.sts',
    3453                            domain_fbound,
    3454                            mean_stage=tide, # Field boundary to adjust for tide
    3455                            boundary_polygon=bounding_polygon,
    3456                            default_boundary=Bdefault) # Condition to be used
    3457                                                       # if model time exceeds
    3458                                                       # available data
    3459 
    3460         domain_fbound.set_boundary({'ocean': Bf,'otherocean': Br})
    3461        
    3462         # Check boundary object evaluates as it should
    3463         for i, ((vol_id, edge_id), B) in enumerate(domain_fbound.boundary_objects):
    3464             if B is Bf:
    3465            
    3466                 qf = B.evaluate(vol_id, edge_id)  # Field boundary
    3467                 qd = Bd.evaluate(vol_id, edge_id) # Dirichlet boundary
    3468                
    3469                 msg = 'Got %s, should have been %s' %(qf, qd)
    3470                 assert num.allclose(qf, qd), msg
    3471                
    3472         # Evolve
    3473         data_finaltime = time_step*(time_step_count-1)
    3474         finaltime = data_finaltime + 10 # Let model time exceed available data
    3475         yieldstep = time_step
    3476         temp_fbound=num.zeros(int(finaltime/yieldstep)+1, num.float)
    3477 
    3478         for i, t in enumerate(domain_fbound.evolve(yieldstep=yieldstep,
    3479                                                    finaltime=finaltime,
    3480                                                    skip_initial_step=False)):
    3481                                                    
    3482             D = domain_fbound
    3483             temp_fbound[i]=D.quantities['stage'].centroid_values[2]
    3484 
    3485             # Check that file boundary object has populated
    3486             # boundary array correctly 
    3487             # FIXME (Ole): Do this for the other tests too!
    3488             for j, val in enumerate(D.get_quantity('stage').boundary_values):
    3489            
    3490                 (vol_id, edge_id), B = D.boundary_objects[j]
    3491                 if isinstance(B, Field_boundary):
    3492                     msg = 'Got %f should have been %f' %(val, w+tide)
    3493                     assert num.allclose(val, w + tide), msg
    3494 
    3495 
    3496     def test_file_boundary_stsII(self):
    3497         """test_file_boundary_stsII(self):
    3498        
    3499          mux2 file has points not included in boundary
    3500          mux2 gauges are not stored with the same order as they are
    3501          found in bounding_polygon. This does not matter as long as bounding
    3502          polygon passed to file_function contains the mux2 points needed (in
    3503          the correct order).
    3504          """
    3505          
    3506         from anuga.shallow_water import Domain
    3507         from anuga.shallow_water import Reflective_boundary
    3508         from anuga.shallow_water import Dirichlet_boundary
    3509         from anuga.shallow_water import File_boundary
    3510         from anuga.pmesh.mesh_interface import create_mesh_from_regions
    3511 
    3512         bounding_polygon=[[6.01,97.0],[6.02,97.0],[6.02,97.02],[6.00,97.02],[6.0,97.0]]
    3513         tide = -2.20
    3514         time_step_count = 20
    3515         time_step = 2
    3516         lat_long_points=bounding_polygon[0:2]
    3517         lat_long_points.insert(0,bounding_polygon[len(bounding_polygon)-1])
    3518         lat_long_points.insert(0,[6.0,97.01])
    3519         n=len(lat_long_points)
    3520         first_tstep=num.ones(n,num.int)
    3521         last_tstep=(time_step_count)*num.ones(n,num.int)
    3522         gauge_depth=20*num.ones(n,num.float)
    3523         ha=2*num.ones((n,time_step_count),num.float)
    3524         ua=10*num.ones((n,time_step_count),num.float)
    3525         va=-10*num.ones((n,time_step_count),num.float)
    3526         base_name, files = self.write_mux2(lat_long_points,
    3527                                            time_step_count,
    3528                                            time_step,
    3529                                            first_tstep,
    3530                                            last_tstep,
    3531                                            depth=gauge_depth,
    3532                                            ha=ha,
    3533                                            ua=ua,
    3534                                            va=va)
    3535 
    3536         sts_file=base_name
    3537         urs2sts(base_name,sts_file,mean_stage=tide,verbose=False)
    3538         self.delete_mux(files)
    3539 
    3540         #print 'start create mesh from regions'
    3541         for i in range(len(bounding_polygon)):
    3542             zone,\
    3543             bounding_polygon[i][0],\
    3544             bounding_polygon[i][1]=redfearn(bounding_polygon[i][0],
    3545                                             bounding_polygon[i][1])
    3546            
    3547         extent_res=1000000
    3548         meshname = 'urs_test_mesh' + '.tsh'
    3549         interior_regions=None
    3550         boundary_tags={'ocean': [0,1], 'otherocean': [2,3,4]}
    3551         # have to change boundary tags from last example because now bounding
    3552         # polygon starts in different place.
    3553         create_mesh_from_regions(bounding_polygon,boundary_tags=boundary_tags,
    3554                          maximum_triangle_area=extent_res,filename=meshname,
    3555                          interior_regions=interior_regions,verbose=False)
    3556        
    3557         domain_fbound = Domain(meshname)
    3558         domain_fbound.set_quantity('stage', tide)
    3559         Bf = File_boundary(sts_file+'.sts',
    3560                            domain_fbound,
    3561                            boundary_polygon=bounding_polygon)
    3562         Br = Reflective_boundary(domain_fbound)
    3563 
    3564         domain_fbound.set_boundary({'ocean': Bf,'otherocean': Br})
    3565         finaltime=time_step*(time_step_count-1)
    3566         yieldstep=time_step
    3567         temp_fbound=num.zeros(int(finaltime/yieldstep)+1,num.float)
    3568        
    3569         for i, t in enumerate(domain_fbound.evolve(yieldstep=yieldstep,
    3570                                                    finaltime=finaltime,
    3571                                                    skip_initial_step = False)):
    3572             temp_fbound[i]=domain_fbound.quantities['stage'].centroid_values[2]
    3573        
    3574         domain_drchlt = Domain(meshname)
    3575         domain_drchlt.set_quantity('stage', tide)
    3576         Br = Reflective_boundary(domain_drchlt)
    3577         Bd = Dirichlet_boundary([2.0+tide,220+10*tide,-220-10*tide])
    3578         domain_drchlt.set_boundary({'ocean': Bd,'otherocean': Br})
    3579         temp_drchlt=num.zeros(int(finaltime/yieldstep)+1,num.float)
    3580 
    3581         for i, t in enumerate(domain_drchlt.evolve(yieldstep=yieldstep,
    3582                                                    finaltime=finaltime,
    3583                                                    skip_initial_step = False)):
    3584             temp_drchlt[i]=domain_drchlt.quantities['stage'].centroid_values[2]
    3585 
    3586 
    3587         assert num.allclose(temp_fbound,temp_drchlt)           
    3588            
    3589         #print domain_fbound.quantities['stage'].vertex_values
    3590         #print domain_drchlt.quantities['stage'].vertex_values
    3591                    
    3592            
    3593         assert num.allclose(domain_fbound.quantities['stage'].vertex_values,
    3594                             domain_drchlt.quantities['stage'].vertex_values)
    3595                        
    3596         assert num.allclose(domain_fbound.quantities['xmomentum'].vertex_values,
    3597                             domain_drchlt.quantities['xmomentum'].vertex_values)
    3598                        
    3599         assert num.allclose(domain_fbound.quantities['ymomentum'].vertex_values,
    3600                             domain_drchlt.quantities['ymomentum'].vertex_values)
    3601            
    3602            
    3603 
    3604         os.remove(sts_file+'.sts')
    3605         os.remove(meshname)
    3606 
    3607        
    3608        
    3609     def test_file_boundary_stsIII_ordering(self):
    3610         """test_file_boundary_stsIII_ordering(self):
    3611         Read correct points from ordering file and apply sts to boundary
    3612         """
    3613         from anuga.shallow_water import Domain
    3614         from anuga.shallow_water import Reflective_boundary
    3615         from anuga.shallow_water import Dirichlet_boundary
    3616         from anuga.shallow_water import File_boundary
    3617         from anuga.pmesh.mesh_interface import create_mesh_from_regions
    3618 
    3619         lat_long_points=[[6.01,97.0],[6.02,97.0],[6.05,96.9],[6.0,97.0]]
    3620         bounding_polygon=[[6.0,97.0],[6.01,97.0],[6.02,97.0],
    3621                           [6.02,97.02],[6.00,97.02]]
    3622         tide = 3.0
    3623         time_step_count = 50
    3624         time_step = 2
    3625         n=len(lat_long_points)
    3626         first_tstep=num.ones(n,num.int)
    3627         last_tstep=(time_step_count)*num.ones(n,num.int)
    3628         gauge_depth=20*num.ones(n,num.float)
    3629         ha=2*num.ones((n,time_step_count),num.float)
    3630         ua=10*num.ones((n,time_step_count),num.float)
    3631         va=-10*num.ones((n,time_step_count),num.float)
    3632         base_name, files = self.write_mux2(lat_long_points,
    3633                                            time_step_count,
    3634                                            time_step,
    3635                                            first_tstep,
    3636                                            last_tstep,
    3637                                            depth=gauge_depth,
    3638                                            ha=ha,
    3639                                            ua=ua,
    3640                                            va=va)
    3641 
    3642         # Write order file
    3643         file_handle, order_base_name = tempfile.mkstemp("")
    3644         os.close(file_handle)
    3645         os.remove(order_base_name)
    3646         d=","
    3647         order_file=order_base_name+'order.txt'
    3648         fid=open(order_file,'w')
    3649        
    3650         # Write Header
    3651         header='index, longitude, latitude\n'
    3652         fid.write(header)
    3653         indices=[3,0,1]
    3654         for i in indices:
    3655             line=str(i)+d+str(lat_long_points[i][1])+d+\
    3656                 str(lat_long_points[i][0])+"\n"
    3657             fid.write(line)
    3658         fid.close()
    3659 
    3660         sts_file=base_name
    3661         urs2sts(base_name,
    3662                 basename_out=sts_file,
    3663                 ordering_filename=order_file,
    3664                 mean_stage=tide,
    3665                 verbose=False)
    3666         self.delete_mux(files)
    3667 
    3668         boundary_polygon = create_sts_boundary(base_name)
    3669 
    3670         os.remove(order_file)
    3671 
    3672         # Append the remaining part of the boundary polygon to be defined by
    3673         # the user
    3674         bounding_polygon_utm=[]
    3675         for point in bounding_polygon:
    3676             zone,easting,northing=redfearn(point[0],point[1])
    3677             bounding_polygon_utm.append([easting,northing])
    3678 
    3679         boundary_polygon.append(bounding_polygon_utm[3])
    3680         boundary_polygon.append(bounding_polygon_utm[4])
    3681 
    3682         plot=False
    3683         if plot:
    3684             from pylab import plot,show,axis
    3685             boundary_polygon=ensure_numeric(boundary_polygon)
    3686             bounding_polygon_utm=ensure_numeric(bounding_polygon_utm)
    3687             #lat_long_points=ensure_numeric(lat_long_points)
    3688             #plot(lat_long_points[:,0],lat_long_points[:,1],'o')
    3689             plot(boundary_polygon[:,0], boundary_polygon[:,1],'d')
    3690             plot(bounding_polygon_utm[:,0],bounding_polygon_utm[:,1],'o')
    3691             show()
    3692 
    3693         assert num.allclose(bounding_polygon_utm,boundary_polygon)
    3694 
    3695 
    3696         extent_res=1000000
    3697         meshname = 'urs_test_mesh' + '.tsh'
    3698         interior_regions=None
    3699         boundary_tags={'ocean': [0,1], 'otherocean': [2,3,4]}
    3700        
    3701         # have to change boundary tags from last example because now bounding
    3702         # polygon starts in different place.
    3703         create_mesh_from_regions(boundary_polygon,boundary_tags=boundary_tags,
    3704                          maximum_triangle_area=extent_res,filename=meshname,
    3705                          interior_regions=interior_regions,verbose=False)
    3706        
    3707         domain_fbound = Domain(meshname)
    3708         domain_fbound.set_quantity('stage', tide)
    3709         Bf = File_boundary(sts_file+'.sts',
    3710                            domain_fbound,
    3711                            boundary_polygon=boundary_polygon)
    3712         Br = Reflective_boundary(domain_fbound)
    3713 
    3714         domain_fbound.set_boundary({'ocean': Bf,'otherocean': Br})
    3715         finaltime=time_step*(time_step_count-1)
    3716         yieldstep=time_step
    3717         temp_fbound=num.zeros(int(finaltime/yieldstep)+1,num.float)
    3718    
    3719         for i, t in enumerate(domain_fbound.evolve(yieldstep=yieldstep,
    3720                                                    finaltime=finaltime,
    3721                                                    skip_initial_step = False)):
    3722             temp_fbound[i]=domain_fbound.quantities['stage'].centroid_values[2]
    3723    
    3724        
    3725         domain_drchlt = Domain(meshname)
    3726         domain_drchlt.set_quantity('stage', tide)
    3727         Br = Reflective_boundary(domain_drchlt)
    3728         Bd = Dirichlet_boundary([2.0+tide,220+10*tide,-220-10*tide])
    3729         domain_drchlt.set_boundary({'ocean': Bd,'otherocean': Br})
    3730         temp_drchlt=num.zeros(int(finaltime/yieldstep)+1,num.float)
    3731        
    3732         for i, t in enumerate(domain_drchlt.evolve(yieldstep=yieldstep,
    3733                                                    finaltime=finaltime,
    3734                                                    skip_initial_step = False)):
    3735             temp_drchlt[i]=domain_drchlt.quantities['stage'].centroid_values[2]
    3736 
    3737        
    3738         #print domain_fbound.quantities['stage'].vertex_values
    3739         #print domain_drchlt.quantities['stage'].vertex_values
    3740                    
    3741         assert num.allclose(temp_fbound,temp_drchlt)
    3742 
    3743        
    3744         assert num.allclose(domain_fbound.quantities['stage'].vertex_values,
    3745                             domain_drchlt.quantities['stage'].vertex_values)
    3746                        
    3747         assert num.allclose(domain_fbound.quantities['xmomentum'].vertex_values,
    3748                             domain_drchlt.quantities['xmomentum'].vertex_values)                       
    3749                        
    3750         assert num.allclose(domain_fbound.quantities['ymomentum'].vertex_values,
    3751                             domain_drchlt.quantities['ymomentum'].vertex_values)
    3752        
    3753         # Use known Dirichlet condition (if sufficient timesteps have been taken)
    3754 
    3755         #FIXME: What do these assertions test? Also do they assume tide =0
    3756         #print domain_fbound.quantities['stage'].vertex_values
    3757         #assert allclose(domain_drchlt.quantities['stage'].vertex_values[6], 2)       
    3758         #assert allclose(domain_fbound.quantities['stage'].vertex_values[6], 2)
    3759        
    3760        
    3761 
    3762         try:
    3763             os.remove(sts_file+'.sts')
    3764         except:
    3765             # Windoze can't remove this file for some reason
    3766             pass
    3767        
    3768         os.remove(meshname)
    3769        
    37701678
    37711679       
     
    41812089    #### END TESTS FOR URS 2 SWW  ###
    41822090
    4183     #### TESTS URS UNGRIDDED 2 SWW ###
    4184     def test_URS_points_needed(self):
    4185        
    4186         ll_lat = -21.5
    4187         ll_long = 114.5
    4188         grid_spacing = 1./60.
    4189         lat_amount = 30
    4190         long_amount = 30
    4191         zone = 50
    4192 
    4193         boundary_polygon = [[250000,7660000],[280000,7660000],
    4194                              [280000,7630000],[250000,7630000]]
    4195         geo=URS_points_needed(boundary_polygon, zone,
    4196                               ll_lat, ll_long, grid_spacing,
    4197                               lat_amount, long_amount,
    4198                               verbose=self.verbose)
    4199         # to test this geo, can info from it be transfered onto the boundary
    4200         # poly?
    4201         #Maybe see if I can fit the data to the polygon - have to represent
    4202         # the poly as points though.
    4203         #geo.export_points_file("results.txt", as_lat_long=True)
    4204         results = frozenset(geo.get_data_points(as_lat_long=True))
    4205         #print 'results',results
    4206 
    4207         # These are a set of points that have to be in results
    4208         points = []
    4209         for i in range(18):
    4210             lat = -21.0 - 8./60 - grid_spacing * i
    4211             points.append((lat,degminsec2decimal_degrees(114,35,0)))
    4212             points.append((lat,degminsec2decimal_degrees(114,36,0)))
    4213             points.append((lat,degminsec2decimal_degrees(114,52,0)))
    4214             points.append((lat,degminsec2decimal_degrees(114,53,0)))
    4215         geo_answer = Geospatial_data(data_points=points,
    4216                                      points_are_lats_longs=True)
    4217         #geo_answer.export_points_file("answer.txt", as_lat_long=True) 
    4218         answer = frozenset(points)
    4219        
    4220         outs = answer.difference(results)
    4221         #print "outs", outs
    4222         # This doesn't work.  Though visualising the results shows that
    4223         # it is correct.
    4224         #assert answer.issubset(results)
    4225         # this is why;
    4226         #point (-21.133333333333333, 114.58333333333333)
    4227         #result (-21.133333332232368, 114.58333333300342)
    4228        
    4229         for point in points:
    4230             found = False
    4231             for result in results:
    4232                 if num.allclose(point, result):
    4233                     found = True
    4234                     break
    4235             if not found:
    4236                 assert False
    4237        
    4238    
    4239     def dave_test_URS_points_needed(self):
    4240         ll_lat = -21.51667
    4241         ll_long = 114.51667
    4242         grid_spacing = 2./60.
    4243         lat_amount = 15
    4244         long_amount = 15
    4245 
    4246        
    4247         boundary_polygon = [[250000,7660000],[280000,7660000],
    4248                              [280000,7630000],[250000,7630000]]
    4249         URS_points_needed_to_file('a_test_example',boundary_polygon,
    4250                                   ll_lat, ll_long, grid_spacing,
    4251                                   lat_amount, long_amount,
    4252                                   verbose=self.verbose)
    4253        
    4254     def X_test_URS_points_neededII(self):
    4255         ll_lat = -21.5
    4256         ll_long = 114.5
    4257         grid_spacing = 1./60.
    4258         lat_amount = 30
    4259         long_amount = 30
    4260 
    4261         # change this so lats and longs are inputed, then converted
    4262        
    4263         #boundary_polygon = [[7660000,250000],[7660000,280000],
    4264         #                     [7630000,280000],[7630000,250000]]
    4265         URS_points_needed(boundary_polygon, ll_lat, ll_long, grid_spacing,
    4266                           lat_amount, long_amount,
    4267                           verbose=self.verbose)
    4268        
    4269     def test_URS_points_northern_hemisphere(self):
    4270                
    4271         LL_LAT = 8.0
    4272         LL_LONG = 97.0
    4273         GRID_SPACING = 2.0/60.0
    4274         LAT_AMOUNT = 2
    4275         LONG_AMOUNT = 2
    4276         ZONE = 47
    4277 
    4278         #
    4279         points = []
    4280         for i in range(2):
    4281             for j in range(2):
    4282                 points.append((degminsec2decimal_degrees(8,1+i*2,0),
    4283                                degminsec2decimal_degrees(97,1+i*2,0)))
    4284         #print "points", points
    4285         geo_poly = Geospatial_data(data_points=points,
    4286                                      points_are_lats_longs=True)
    4287         poly_lat_long = geo_poly.get_data_points(as_lat_long=False,
    4288                                        isSouthHemisphere=False)
    4289         #print "seg_lat_long",  poly_lat_long
    4290        
    4291       #   geo=URS_points_needed_to_file('test_example_poly3', poly_lat_long,
    4292 #                                   ZONE,
    4293 #                                   LL_LAT, LL_LONG,
    4294 #                                   GRID_SPACING,
    4295 #                                   LAT_AMOUNT, LONG_AMOUNT,
    4296 #                                   isSouthernHemisphere=False,
    4297 #                                   export_csv=True,
    4298 #                                   verbose=self.verbose)
    4299        
    4300         geo=URS_points_needed(poly_lat_long,
    4301                                   ZONE,
    4302                                   LL_LAT, LL_LONG,
    4303                                   GRID_SPACING,
    4304                                   LAT_AMOUNT, LONG_AMOUNT,
    4305                                   isSouthHemisphere=False,
    4306                                   verbose=self.verbose)
    4307        
    4308         results = frozenset(geo.get_data_points(as_lat_long=True,
    4309                                                 isSouthHemisphere=False))
    4310         #print 'results',results
    4311 
    4312         # These are a set of points that have to be in results
    4313         points = []
    4314         for i in range(2):
    4315             for j in range(2):
    4316                 points.append((degminsec2decimal_degrees(8,i*2,0),
    4317                                degminsec2decimal_degrees(97,i*2,0)))
    4318         #print "answer points", points
    4319         answer = frozenset(points)
    4320        
    4321         for point in points:
    4322             found = False
    4323             for result in results:
    4324                 if num.allclose(point, result):
    4325                     found = True
    4326                     break
    4327             if not found:
    4328                 assert False
    4329        
    4330 
    4331     def covered_in_other_tests_test_URS_points_needed_poly1(self):
    4332         # Values used for FESA 2007 results
    4333         # domain in southern hemisphere zone 51       
    4334         LL_LAT = -50.0
    4335         LL_LONG = 80.0
    4336         GRID_SPACING = 2.0/60.0
    4337         LAT_AMOUNT = 4800
    4338         LONG_AMOUNT = 3600
    4339         ZONE = 51
    4340        
    4341         poly1 = [[296361.89, 8091928.62],
    4342                  [429495.07,8028278.82],
    4343                  [447230.56,8000674.05],
    4344                  [429661.2,7982177.6],
    4345                  [236945.9,7897453.16],
    4346                  [183493.44,7942782.27],
    4347                  [226583.04,8008058.96]]
    4348 
    4349         URS_points_needed_to_file('test_example_poly2', poly1,
    4350                                   ZONE,
    4351                                   LL_LAT, LL_LONG,
    4352                                   GRID_SPACING,
    4353                                   LAT_AMOUNT, LONG_AMOUNT,
    4354                                   verbose=self.verbose)
    4355        
    4356 
    4357 
    4358     def covered_in_other_tests_test_URS_points_needed_poly2(self):
    4359         # Values used for 2004 validation work
    4360         # domain in northern hemisphere zone 47       
    4361         LL_LAT = 0.0
    4362         LL_LONG = 90.0
    4363         GRID_SPACING = 2.0/60.0
    4364         LAT_AMOUNT = (15-LL_LAT)/GRID_SPACING
    4365         LONG_AMOUNT = (100-LL_LONG)/GRID_SPACING
    4366         ZONE = 47
    4367        
    4368         poly2 = [[419336.424,810100.845],
    4369                  [342405.0281,711455.8026],
    4370                  [274649.9152,723352.9603],
    4371                  [272089.092,972393.0131],
    4372                  [347633.3754,968551.7784],
    4373                  [427979.2022,885965.2313],
    4374                  [427659.0993,875721.9386],
    4375                  [429259.6138,861317.3083],
    4376                  [436301.8775,840830.723]]
    4377        
    4378         URS_points_needed_to_file('test_example_poly2', poly2,
    4379                                   ZONE,
    4380                                   LL_LAT, LL_LONG,
    4381                                   GRID_SPACING,
    4382                                   LAT_AMOUNT, LONG_AMOUNT,
    4383                                   isSouthernHemisphere=False,
    4384                                   verbose=self.verbose)
    4385        
    4386     #### END TESTS URS UNGRIDDED 2 SWW ###
    4387     def test_Urs_points(self):
    4388         time_step_count = 3
    4389         time_step = 2
    4390         lat_long_points =[(-21.5,114.5),(-21.5,115),(-21.,115)]
    4391         base_name, files = self.write_mux(lat_long_points,
    4392                                           time_step_count, time_step)
    4393         for file in files:
    4394             urs = Urs_points(file)
    4395             assert time_step_count == urs.time_step_count
    4396             assert time_step == urs.time_step
    4397 
    4398             for lat_lon, dep in map(None, lat_long_points, urs.lonlatdep):
    4399                     _ , e, n = redfearn(lat_lon[0], lat_lon[1])
    4400                     assert num.allclose(n, dep[2])
    4401                        
    4402             count = 0
    4403             for slice in urs:
    4404                 count += 1
    4405                 #print slice
    4406                 for lat_lon, quantity in map(None, lat_long_points, slice):
    4407                     _ , e, n = redfearn(lat_lon[0], lat_lon[1])
    4408                     #print "quantity", quantity
    4409                     #print "e", e
    4410                     #print "n", n
    4411                     if file[-5:] == WAVEHEIGHT_MUX_LABEL[-5:] or \
    4412                            file[-5:] == NORTH_VELOCITY_LABEL[-5:] :
    4413                         assert num.allclose(e, quantity)
    4414                     if file[-5:] == EAST_VELOCITY_LABEL[-5:]:
    4415                         assert num.allclose(n, quantity)
    4416             assert count == time_step_count
    4417                      
    4418         self.delete_mux(files)
    4419 
    4420     def test_urs_ungridded2sww (self):
    4421        
    4422         #Zone:   50   
    4423         #Easting:  240992.578  Northing: 7620442.472
    4424         #Latitude:   -21  30 ' 0.00000 ''  Longitude: 114  30 ' 0.00000 ''
    4425         lat_long = [[-21.5,114.5],[-21,114.5],[-21,115]]
    4426         time_step_count = 2
    4427         time_step = 400
    4428         tide = 9000000
    4429         base_name, files = self.write_mux(lat_long,
    4430                                           time_step_count, time_step)
    4431         urs_ungridded2sww(base_name, mean_stage=tide,
    4432                           verbose=self.verbose)
    4433        
    4434         # now I want to check the sww file ...
    4435         sww_file = base_name + '.sww'
    4436        
    4437         #Let's interigate the sww file
    4438         # Note, the sww info is not gridded.  It is point data.
    4439         fid = NetCDFFile(sww_file)
    4440        
    4441         # Make x and y absolute
    4442         x = fid.variables['x'][:]
    4443         y = fid.variables['y'][:]
    4444         geo_reference = Geo_reference(NetCDFObject=fid)
    4445         points = geo_reference.get_absolute(map(None, x, y))
    4446         points = ensure_numeric(points)
    4447         x = points[:,0]
    4448         y = points[:,1]
    4449        
    4450         #Check that first coordinate is correctly represented       
    4451         #Work out the UTM coordinates for first point
    4452         zone, e, n = redfearn(lat_long[0][0], lat_long[0][1])
    4453         assert num.allclose([x[0],y[0]], [e,n])
    4454 
    4455         #Check the time vector
    4456         times = fid.variables['time'][:]
    4457        
    4458         times_actual = []
    4459         for i in range(time_step_count):
    4460             times_actual.append(time_step * i)
    4461        
    4462         assert num.allclose(ensure_numeric(times),
    4463                             ensure_numeric(times_actual))
    4464        
    4465         #Check first value
    4466         stage = fid.variables['stage'][:]
    4467         xmomentum = fid.variables['xmomentum'][:]
    4468         ymomentum = fid.variables['ymomentum'][:]
    4469         elevation = fid.variables['elevation'][:]
    4470         assert num.allclose(stage[0,0], e +tide)  #Meters
    4471 
    4472 
    4473         #Check the momentums - ua
    4474         #momentum = velocity*(stage-elevation)
    4475         # elevation = - depth
    4476         #momentum = velocity_ua *(stage+depth)
    4477         # = n*(e+tide+n) based on how I'm writing these files
    4478         #
    4479         answer_x = n*(e+tide+n)
    4480         actual_x = xmomentum[0,0]
    4481         #print "answer_x",answer_x
    4482         #print "actual_x",actual_x
    4483         assert num.allclose(answer_x, actual_x)  #Meters
    4484        
    4485         #Check the momentums - va
    4486         #momentum = velocity*(stage-elevation)
    4487         # elevation = - depth
    4488         #momentum = velocity_va *(stage+depth)
    4489         # = e*(e+tide+n) based on how I'm writing these files
    4490         #
    4491         answer_y = -1*e*(e+tide+n)
    4492         actual_y = ymomentum[0,0]
    4493         #print "answer_y",answer_y
    4494         #print "actual_y",actual_y
    4495         assert num.allclose(answer_y, actual_y)  #Meters
    4496 
    4497         # check the stage values, first time step.
    4498         # These arrays are equal since the Easting values were used as
    4499         # the stage
    4500         assert num.allclose(stage[0], x +tide)  #Meters
    4501         # check the elevation values.
    4502         # -ve since urs measures depth, sww meshers height,
    4503         # these arrays are equal since the northing values were used as
    4504         # the elevation
    4505         assert num.allclose(-elevation, y)  #Meters
    4506        
    4507         fid.close()
    4508         self.delete_mux(files)
    4509         os.remove(sww_file)
    4510  
    4511     def test_urs_ungridded2swwII (self):
    4512        
    4513         #Zone:   50   
    4514         #Easting:  240992.578  Northing: 7620442.472
    4515         #Latitude:   -21  30 ' 0.00000 ''  Longitude: 114  30 ' 0.00000 ''
    4516         lat_long = [[-21.5,114.5],[-21,114.5],[-21,115]]
    4517         time_step_count = 2
    4518         time_step = 400
    4519         tide = 9000000
    4520         geo_reference = Geo_reference(50, 3434543,34534543)
    4521         base_name, files = self.write_mux(lat_long,
    4522                                           time_step_count, time_step)
    4523         urs_ungridded2sww(base_name, mean_stage=tide, origin = geo_reference,
    4524                           verbose=self.verbose)
    4525        
    4526         # now I want to check the sww file ...
    4527         sww_file = base_name + '.sww'
    4528        
    4529         #Let's interigate the sww file
    4530         # Note, the sww info is not gridded.  It is point data.
    4531         fid = NetCDFFile(sww_file)
    4532        
    4533         # Make x and y absolute
    4534         x = fid.variables['x'][:]
    4535         y = fid.variables['y'][:]
    4536         geo_reference = Geo_reference(NetCDFObject=fid)
    4537         points = geo_reference.get_absolute(map(None, x, y))
    4538         points = ensure_numeric(points)
    4539         x = points[:,0]
    4540         y = points[:,1]
    4541        
    4542         #Check that first coordinate is correctly represented       
    4543         #Work out the UTM coordinates for first point
    4544         zone, e, n = redfearn(lat_long[0][0], lat_long[0][1])
    4545         assert num.allclose([x[0],y[0]], [e,n])
    4546 
    4547         #Check the time vector
    4548         times = fid.variables['time'][:]
    4549        
    4550         times_actual = []
    4551         for i in range(time_step_count):
    4552             times_actual.append(time_step * i)
    4553        
    4554         assert num.allclose(ensure_numeric(times),
    4555                             ensure_numeric(times_actual))
    4556        
    4557         #Check first value
    4558         stage = fid.variables['stage'][:]
    4559         xmomentum = fid.variables['xmomentum'][:]
    4560         ymomentum = fid.variables['ymomentum'][:]
    4561         elevation = fid.variables['elevation'][:]
    4562         assert num.allclose(stage[0,0], e +tide)  #Meters
    4563 
    4564         #Check the momentums - ua
    4565         #momentum = velocity*(stage-elevation)
    4566         # elevation = - depth
    4567         #momentum = velocity_ua *(stage+depth)
    4568         # = n*(e+tide+n) based on how I'm writing these files
    4569         #
    4570         answer_x = n*(e+tide+n)
    4571         actual_x = xmomentum[0,0]
    4572         #print "answer_x",answer_x
    4573         #print "actual_x",actual_x
    4574         assert num.allclose(answer_x, actual_x)  #Meters
    4575        
    4576         #Check the momentums - va
    4577         #momentum = velocity*(stage-elevation)
    4578         # elevation = - depth
    4579         #momentum = velocity_va *(stage+depth)
    4580         # = e*(e+tide+n) based on how I'm writing these files
    4581         #
    4582         answer_y = -1*e*(e+tide+n)
    4583         actual_y = ymomentum[0,0]
    4584         #print "answer_y",answer_y
    4585         #print "actual_y",actual_y
    4586         assert num.allclose(answer_y, actual_y)  #Meters
    4587 
    4588         # check the stage values, first time step.
    4589         # These arrays are equal since the Easting values were used as
    4590         # the stage
    4591         assert num.allclose(stage[0], x +tide)  #Meters
    4592         # check the elevation values.
    4593         # -ve since urs measures depth, sww meshers height,
    4594         # these arrays are equal since the northing values were used as
    4595         # the elevation
    4596         assert num.allclose(-elevation, y)  #Meters
    4597        
    4598         fid.close()
    4599         self.delete_mux(files)
    4600         os.remove(sww_file)
    4601  
    4602     def test_urs_ungridded2swwIII (self):
    4603        
    4604         #Zone:   50   
    4605         #Easting:  240992.578  Northing: 7620442.472
    4606         #Latitude:   -21  30 ' 0.00000 ''  Longitude: 114  30 ' 0.00000 ''
    4607         lat_long = [[-21.5,114.5],[-21,114.5],[-21,115]]
    4608         time_step_count = 2
    4609         time_step = 400
    4610         tide = 9000000
    4611         base_name, files = self.write_mux(lat_long,
    4612                                           time_step_count, time_step)
    4613         urs_ungridded2sww(base_name, mean_stage=tide, origin =(50,23432,4343),
    4614                           verbose=self.verbose)
    4615        
    4616         # now I want to check the sww file ...
    4617         sww_file = base_name + '.sww'
    4618        
    4619         #Let's interigate the sww file
    4620         # Note, the sww info is not gridded.  It is point data.
    4621         fid = NetCDFFile(sww_file)
    4622        
    4623         # Make x and y absolute
    4624         x = fid.variables['x'][:]
    4625         y = fid.variables['y'][:]
    4626         geo_reference = Geo_reference(NetCDFObject=fid)
    4627         points = geo_reference.get_absolute(map(None, x, y))
    4628         points = ensure_numeric(points)
    4629         x = points[:,0]
    4630         y = points[:,1]
    4631        
    4632         #Check that first coordinate is correctly represented       
    4633         #Work out the UTM coordinates for first point
    4634         zone, e, n = redfearn(lat_long[0][0], lat_long[0][1])
    4635         assert num.allclose([x[0],y[0]], [e,n])
    4636 
    4637         #Check the time vector
    4638         times = fid.variables['time'][:]
    4639        
    4640         times_actual = []
    4641         for i in range(time_step_count):
    4642             times_actual.append(time_step * i)
    4643        
    4644         assert num.allclose(ensure_numeric(times),
    4645                             ensure_numeric(times_actual))
    4646        
    4647         #Check first value
    4648         stage = fid.variables['stage'][:]
    4649         xmomentum = fid.variables['xmomentum'][:]
    4650         ymomentum = fid.variables['ymomentum'][:]
    4651         elevation = fid.variables['elevation'][:]
    4652         assert num.allclose(stage[0,0], e +tide)  #Meters
    4653 
    4654         #Check the momentums - ua
    4655         #momentum = velocity*(stage-elevation)
    4656         # elevation = - depth
    4657         #momentum = velocity_ua *(stage+depth)
    4658         # = n*(e+tide+n) based on how I'm writing these files
    4659         #
    4660         answer_x = n*(e+tide+n)
    4661         actual_x = xmomentum[0,0]
    4662         #print "answer_x",answer_x
    4663         #print "actual_x",actual_x
    4664         assert num.allclose(answer_x, actual_x)  #Meters
    4665        
    4666         #Check the momentums - va
    4667         #momentum = velocity*(stage-elevation)
    4668         # elevation = - depth
    4669         #momentum = velocity_va *(stage+depth)
    4670         # = e*(e+tide+n) based on how I'm writing these files
    4671         #
    4672         answer_y = -1*e*(e+tide+n)
    4673         actual_y = ymomentum[0,0]
    4674         #print "answer_y",answer_y
    4675         #print "actual_y",actual_y
    4676         assert num.allclose(answer_y, actual_y)  #Meters
    4677 
    4678         # check the stage values, first time step.
    4679         # These arrays are equal since the Easting values were used as
    4680         # the stage
    4681         assert num.allclose(stage[0], x +tide)  #Meters
    4682         # check the elevation values.
    4683         # -ve since urs measures depth, sww meshers height,
    4684         # these arrays are equal since the northing values were used as
    4685         # the elevation
    4686         assert num.allclose(-elevation, y)  #Meters
    4687        
    4688         fid.close()
    4689         self.delete_mux(files)
    4690         os.remove(sww_file)
    4691 
    4692        
    4693     def test_urs_ungridded_hole (self):
    4694        
    4695         #Zone:   50   
    4696         #Easting:  240992.578  Northing: 7620442.472
    4697         #Latitude:   -21  30 ' 0.00000 ''  Longitude: 114  30 ' 0.00000 ''
    4698         lat_long = [[-20.5, 114.5],
    4699                     [-20.6, 114.6],
    4700                     [-20.5, 115.],
    4701                     [-20.6, 115.],
    4702                     [-20.5, 115.5],
    4703                     [-20.6, 115.4],
    4704                    
    4705                     [-21., 114.5],
    4706                     [-21., 114.6],
    4707                     [-21., 115.5],
    4708                     [-21., 115.4],
    4709                    
    4710                     [-21.5, 114.5],
    4711                     [-21.4, 114.6],
    4712                     [-21.5, 115.],
    4713                     [-21.4, 115.],
    4714                     [-21.5, 115.5],
    4715                     [-21.4, 115.4]
    4716                     ]
    4717         time_step_count = 6
    4718         time_step = 100
    4719         tide = 9000000
    4720         base_name, files = self.write_mux(lat_long,
    4721                                           time_step_count, time_step)
    4722         #Easting:  292110.784  Northing: 7676551.710
    4723         #Latitude:   -21  0 ' 0.00000 ''  Longitude: 115  0 ' 0.00000 ''
    4724 
    4725         urs_ungridded2sww(base_name, mean_stage=-240992.0,
    4726                           hole_points_UTM=[ 292110.784, 7676551.710 ],
    4727                           verbose=self.verbose)
    4728        
    4729         # now I want to check the sww file ...
    4730         sww_file = base_name + '.sww'
    4731        
    4732         #Let's interigate the sww file
    4733         # Note, the sww info is not gridded.  It is point data.
    4734         fid = NetCDFFile(sww_file)
    4735        
    4736         number_of_volumes = fid.variables['volumes']
    4737         #print "number_of_volumes",len(number_of_volumes)
    4738         assert num.allclose(16, len(number_of_volumes))
    4739        
    4740         fid.close()
    4741         self.delete_mux(files)
    4742         #print "sww_file", sww_file
    4743         os.remove(sww_file)
    4744        
    4745     def test_urs_ungridded_holeII(self):
    4746 
    4747         # Check that if using a hole that returns no triangles,
    4748         # urs_ungridded2sww removes the hole label.
    4749        
    4750         lat_long = [[-20.5, 114.5],
    4751                     [-20.6, 114.6],
    4752                     [-20.5, 115.5],
    4753                     [-20.6, 115.4],
    4754                    
    4755                    
    4756                     [-21.5, 114.5],
    4757                     [-21.4, 114.6],
    4758                     [-21.5, 115.5],
    4759                     [-21.4, 115.4]
    4760                     ]
    4761         time_step_count = 6
    4762         time_step = 100
    4763         tide = 9000000
    4764         base_name, files = self.write_mux(lat_long,
    4765                                           time_step_count, time_step)
    4766         #Easting:  292110.784  Northing: 7676551.710
    4767         #Latitude:   -21  0 ' 0.00000 ''  Longitude: 115  0 ' 0.00000 ''
    4768 
    4769         urs_ungridded2sww(base_name, mean_stage=-240992.0,
    4770                           hole_points_UTM=[ 292110.784, 7676551.710 ],
    4771                           verbose=self.verbose)
    4772        
    4773         # now I want to check the sww file ...
    4774         sww_file = base_name + '.sww'
    4775         fid = NetCDFFile(sww_file)
    4776        
    4777         volumes = fid.variables['volumes']
    4778         #print "number_of_volumes",len(volumes)
    4779 
    4780         fid.close()
    4781         os.remove(sww_file)
    4782        
    4783         urs_ungridded2sww(base_name, mean_stage=-240992.0)
    4784        
    4785         # now I want to check the sww file ...
    4786         sww_file = base_name + '.sww'
    4787         fid = NetCDFFile(sww_file)
    4788        
    4789         volumes_again = fid.variables['volumes']
    4790         #print "number_of_volumes",len(volumes_again)
    4791         assert num.allclose(len(volumes_again),
    4792                             len(volumes))
    4793         fid.close()
    4794         os.remove(sww_file)
    4795         self.delete_mux(files)
    4796        
    4797     def test_urs_ungridded2sww_mint_maxt (self):
    4798        
    4799         #Zone:   50   
    4800         #Easting:  240992.578  Northing: 7620442.472
    4801         #Latitude:   -21  30 ' 0.00000 ''  Longitude: 114  30 ' 0.00000 ''
    4802         lat_long = [[-21.5,114.5],[-21,114.5],[-21,115]]
    4803         time_step_count = 6
    4804         time_step = 100
    4805         tide = 9000000
    4806         base_name, files = self.write_mux(lat_long,
    4807                                           time_step_count, time_step)
    4808         urs_ungridded2sww(base_name, mean_stage=tide, origin =(50,23432,4343),
    4809                           mint=101, maxt=500,
    4810                           verbose=self.verbose)
    4811        
    4812         # now I want to check the sww file ...
    4813         sww_file = base_name + '.sww'
    4814        
    4815         #Let's interigate the sww file
    4816         # Note, the sww info is not gridded.  It is point data.
    4817         fid = NetCDFFile(sww_file)
    4818        
    4819         # Make x and y absolute
    4820         x = fid.variables['x'][:]
    4821         y = fid.variables['y'][:]
    4822         geo_reference = Geo_reference(NetCDFObject=fid)
    4823         points = geo_reference.get_absolute(map(None, x, y))
    4824         points = ensure_numeric(points)
    4825         x = points[:,0]
    4826         y = points[:,1]
    4827        
    4828         #Check that first coordinate is correctly represented       
    4829         #Work out the UTM coordinates for first point
    4830         zone, e, n = redfearn(lat_long[0][0], lat_long[0][1])
    4831         assert num.allclose([x[0],y[0]], [e,n])
    4832 
    4833         #Check the time vector
    4834         times = fid.variables['time'][:]
    4835        
    4836         times_actual = [0,100,200,300]
    4837        
    4838         assert num.allclose(ensure_numeric(times),
    4839                             ensure_numeric(times_actual))
    4840        
    4841                #Check first value
    4842         stage = fid.variables['stage'][:]
    4843         xmomentum = fid.variables['xmomentum'][:]
    4844         ymomentum = fid.variables['ymomentum'][:]
    4845         elevation = fid.variables['elevation'][:]
    4846         assert num.allclose(stage[0,0], e +tide)  #Meters
    4847 
    4848         #Check the momentums - ua
    4849         #momentum = velocity*(stage-elevation)
    4850         # elevation = - depth
    4851         #momentum = velocity_ua *(stage+depth)
    4852         # = n*(e+tide+n) based on how I'm writing these files
    4853         #
    4854         answer_x = n*(e+tide+n)
    4855         actual_x = xmomentum[0,0]
    4856         #print "answer_x",answer_x
    4857         #print "actual_x",actual_x
    4858         assert num.allclose(answer_x, actual_x)  #Meters
    4859        
    4860         #Check the momentums - va
    4861         #momentum = velocity*(stage-elevation)
    4862         # elevation = - depth
    4863         #momentum = velocity_va *(stage+depth)
    4864         # = e*(e+tide+n) based on how I'm writing these files
    4865         #
    4866         answer_y = -1*e*(e+tide+n)
    4867         actual_y = ymomentum[0,0]
    4868         #print "answer_y",answer_y
    4869         #print "actual_y",actual_y
    4870         assert num.allclose(answer_y, actual_y)  #Meters
    4871 
    4872         # check the stage values, first time step.
    4873         # These arrays are equal since the Easting values were used as
    4874         # the stage
    4875         assert num.allclose(stage[0], x +tide)  #Meters
    4876         # check the elevation values.
    4877         # -ve since urs measures depth, sww meshers height,
    4878         # these arrays are equal since the northing values were used as
    4879         # the elevation
    4880         assert num.allclose(-elevation, y)  #Meters
    4881        
    4882         fid.close()
    4883         self.delete_mux(files)
    4884         os.remove(sww_file)
    4885        
    4886     def test_urs_ungridded2sww_mint_maxtII (self):
    4887        
    4888         #Zone:   50   
    4889         #Easting:  240992.578  Northing: 7620442.472
    4890         #Latitude:   -21  30 ' 0.00000 ''  Longitude: 114  30 ' 0.00000 ''
    4891         lat_long = [[-21.5,114.5],[-21,114.5],[-21,115]]
    4892         time_step_count = 6
    4893         time_step = 100
    4894         tide = 9000000
    4895         base_name, files = self.write_mux(lat_long,
    4896                                           time_step_count, time_step)
    4897         urs_ungridded2sww(base_name, mean_stage=tide, origin =(50,23432,4343),
    4898                           mint=0, maxt=100000,
    4899                           verbose=self.verbose)
    4900        
    4901         # now I want to check the sww file ...
    4902         sww_file = base_name + '.sww'
    4903        
    4904         #Let's interigate the sww file
    4905         # Note, the sww info is not gridded.  It is point data.
    4906         fid = NetCDFFile(sww_file)
    4907        
    4908         # Make x and y absolute
    4909         geo_reference = Geo_reference(NetCDFObject=fid)
    4910         points = geo_reference.get_absolute(map(None, fid.variables['x'][:],
    4911                                                 fid.variables['y'][:]))
    4912         points = ensure_numeric(points)
    4913         x = points[:,0]
    4914        
    4915         #Check the time vector
    4916         times = fid.variables['time'][:]
    4917        
    4918         times_actual = [0,100,200,300,400,500]
    4919         assert num.allclose(ensure_numeric(times),
    4920                             ensure_numeric(times_actual))
    4921        
    4922         #Check first value
    4923         stage = fid.variables['stage'][:]
    4924         assert num.allclose(stage[0], x +tide)
    4925        
    4926         fid.close()
    4927         self.delete_mux(files)
    4928         os.remove(sww_file)
    4929        
    4930     def test_urs_ungridded2sww_mint_maxtIII (self):
    4931        
    4932         #Zone:   50   
    4933         #Easting:  240992.578  Northing: 7620442.472
    4934         #Latitude:   -21  30 ' 0.00000 ''  Longitude: 114  30 ' 0.00000 ''
    4935         lat_long = [[-21.5,114.5],[-21,114.5],[-21,115]]
    4936         time_step_count = 6
    4937         time_step = 100
    4938         tide = 9000000
    4939         base_name, files = self.write_mux(lat_long,
    4940                                           time_step_count, time_step)
    4941         try:
    4942             urs_ungridded2sww(base_name, mean_stage=tide,
    4943                           origin =(50,23432,4343),
    4944                           mint=301, maxt=399,
    4945                               verbose=self.verbose)
    4946         except:
    4947             pass
    4948         else:
    4949             self.failUnless(0 ==1, 'Bad input did not throw exception error!')
    4950 
    4951         self.delete_mux(files)
    4952        
    4953     def test_urs_ungridded2sww_mint_maxt_bad (self):       
    4954         #Zone:   50   
    4955         #Easting:  240992.578  Northing: 7620442.472
    4956         #Latitude:   -21  30 ' 0.00000 ''  Longitude: 114  30 ' 0.00000 ''
    4957         lat_long = [[-21.5,114.5],[-21,114.5],[-21,115]]
    4958         time_step_count = 6
    4959         time_step = 100
    4960         tide = 9000000
    4961         base_name, files = self.write_mux(lat_long,
    4962                                           time_step_count, time_step)
    4963         try:
    4964             urs_ungridded2sww(base_name, mean_stage=tide,
    4965                           origin =(50,23432,4343),
    4966                           mint=301, maxt=301,
    4967                               verbose=self.verbose)
    4968         except:
    4969             pass
    4970         else:
    4971             self.failUnless(0 ==1, 'Bad input did not throw exception error!')
    4972 
    4973         self.delete_mux(files)
    4974 
    4975        
    4976     def test_URS_points_needed_and_urs_ungridded2sww(self):
    4977         # This doesn't actually check anything
    4978         # 
    4979         ll_lat = -21.5
    4980         ll_long = 114.5
    4981         grid_spacing = 1./60.
    4982         lat_amount = 30
    4983         long_amount = 30
    4984         time_step_count = 2
    4985         time_step = 400
    4986         tide = -200000
    4987         zone = 50
    4988 
    4989         boundary_polygon = [[250000,7660000],[280000,7660000],
    4990                              [280000,7630000],[250000,7630000]]
    4991         geo=URS_points_needed(boundary_polygon, zone,
    4992                               ll_lat, ll_long, grid_spacing,
    4993                               lat_amount, long_amount,
    4994                               verbose=self.verbose)
    4995         lat_long = geo.get_data_points(as_lat_long=True)
    4996         base_name, files = self.write_mux(lat_long,
    4997                                           time_step_count, time_step)
    4998         urs_ungridded2sww(base_name, mean_stage=tide,
    4999                           verbose=self.verbose)
    5000         self.delete_mux(files)
    5001         os.remove( base_name + '.sww')
    5002    
    5003     def cache_test_URS_points_needed_and_urs_ungridded2sww(self):
    5004        
    5005         ll_lat = -21.5
    5006         ll_long = 114.5
    5007         grid_spacing = 1./60.
    5008         lat_amount = 30
    5009         long_amount = 30
    5010         time_step_count = 2
    5011         time_step = 400
    5012         tide = -200000
    5013         zone = 50
    5014 
    5015         boundary_polygon = [[250000,7660000],[270000,7650000],
    5016                              [280000,7630000],[250000,7630000]]
    5017         geo=URS_points_needed(boundary_polygon, zone,
    5018                               ll_lat, ll_long, grid_spacing,
    5019                               lat_amount, long_amount, use_cache=True,
    5020                               verbose=True)
    5021        
    5022     def visual_test_URS_points_needed_and_urs_ungridded2sww(self):
    5023        
    5024         ll_lat = -21.5
    5025         ll_long = 114.5
    5026         grid_spacing = 1./60.
    5027         lat_amount = 30
    5028         long_amount = 30
    5029         time_step_count = 2
    5030         time_step = 400
    5031         tide = -200000
    5032         zone = 50
    5033 
    5034         boundary_polygon = [[250000,7660000],[270000,7650000],
    5035                              [280000,7630000],[250000,7630000]]
    5036         geo=URS_points_needed(boundary_polygon, zone,
    5037                               ll_lat, ll_long, grid_spacing,
    5038                               lat_amount, long_amount)
    5039         lat_long = geo.get_data_points(as_lat_long=True)
    5040         base_name, files = self.write_mux(lat_long,
    5041                                           time_step_count, time_step)
    5042         urs_ungridded2sww(base_name, mean_stage=tide)
    5043         self.delete_mux(files)
    5044         os.remove( base_name + '.sww')
    5045         # extend this so it interpolates onto the boundary.
    5046         # have it fail if there is NaN
    50472091
    50482092    def test_triangulation(self):
  • trunk/anuga_core/source/anuga/shallow_water/test_shallow_water_domain.py

    r7736 r7769  
    11#!/usr/bin/env python
    22
    3 import unittest, os
     3import unittest, os, time
    44import os.path
    55from math import pi, sqrt
     
    77
    88from anuga.shallow_water import Domain
     9
     10from Scientific.IO.NetCDF import NetCDFFile
     11from anuga.file.sww import extent_sww
    912
    1013from anuga.config import g, epsilon
     
    1417from anuga.coordinate_transforms.geo_reference import Geo_reference
    1518from anuga.geospatial_data.geospatial_data import Geospatial_data
    16 from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular_cross
     19from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular_cross, \
     20                                            rectangular
    1721from anuga.abstract_2d_finite_volumes.quantity import Quantity
    1822from anuga.shallow_water.forcing import Inflow, Cross_section
     
    14651469        This run tries it with georeferencing and with elevation = -1
    14661470        """
    1467 
    1468         import time, os
    1469         from Scientific.IO.NetCDF import NetCDFFile
    1470         from mesh_factory import rectangular
    14711471
    14721472        # Create basic mesh (20m x 3m)
     
    52625262        was in March-April 2007
    52635263        """
    5264         import time, os
    5265         from Scientific.IO.NetCDF import NetCDFFile
    5266         from data_manager import extent_sww
    5267         from mesh_factory import rectangular
    52685264
    52695265        # Create basic mesh
Note: See TracChangeset for help on using the changeset viewer.