Changeset 8063


Ignore:
Timestamp:
Nov 1, 2010, 12:04:57 PM (8 years ago)
Author:
jakeman
Message:

updated sww2csv_gauges so that it can now read multiple temporaly consecutive sww files. This was previously broken. Unit test also included in test_gauges.py called test_sww2csv_multiple_gauges

Location:
trunk/anuga_core/source/anuga/abstract_2d_finite_volumes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/anuga_core/source/anuga/abstract_2d_finite_volumes/gauge.py

    r7780 r8063  
    223223    quake_offset_time = None
    224224
     225    is_opened = [False]*len(points_array)
    225226    for sww_file in sww_files:
    226227        sww_file = join(dir_name, sww_file+'.sww')
     
    235236            quake_offset_time = callable_sww.starttime
    236237
    237     for point_i, point in enumerate(points_array):
    238         is_opened = False   
    239         for time in callable_sww.get_time():
    240             #add domain starttime to relative time.
    241             quake_time = time + quake_offset_time
    242             point_quantities = callable_sww(time, point_i) # __call__ is overridden
    243 
    244             if point_quantities[0] != NAN:
    245                 if is_opened == False:
    246                     points_writer = writer(file(dir_name + sep + gauge_file
    247                                                         + point_name[point_i] + '.csv', "wb"))
    248                     points_writer.writerow(heading)
    249                     is_opened = True
    250                 points_list = [quake_time, quake_time/3600.] +  _quantities2csv(quantities, point_quantities, callable_sww.centroids, point_i)
    251                 points_writer.writerow(points_list)
    252             else:
    253                 if verbose:
    254                     msg = 'gauge' + point_name[point_i] + 'falls off the mesh in file ' + sww_file + '.'
    255                     log.warning(msg)
     238        for point_i, point in enumerate(points_array):
     239            for time in callable_sww.get_time():
     240                # add domain starttime to relative time.
     241                quake_time = time + quake_offset_time
     242                point_quantities = callable_sww(time, point_i) # __call__ is overridden
     243
     244                if point_quantities[0] != NAN:
     245                    if is_opened[point_i] == False:
     246                        points_writer = writer(file(dir_name + sep + gauge_file
     247                                                    + point_name[point_i] + '.csv', "wb"))
     248                        points_writer.writerow(heading)
     249                        is_opened[point_i] = True
     250                    else:
     251                        points_writer = writer(file(dir_name + sep + gauge_file
     252                                                    + point_name[point_i] + '.csv', "ab"))
     253
     254                    points_list = [quake_time, quake_time/3600.] +  _quantities2csv(quantities, point_quantities, callable_sww.centroids, point_i)
     255                    points_writer.writerow(points_list)
     256                else:
     257                    if verbose:
     258                        msg = 'gauge' + point_name[point_i] + 'falls off the mesh in file ' + sww_file + '.'
     259                        log.warning(msg)
    256260##
    257261# @brief Read a .sww file and plot the time series.
  • trunk/anuga_core/source/anuga/abstract_2d_finite_volumes/test_gauge.py

    r7778 r8063  
    8484            os.remove(self.sww.filename)
    8585
    86     def _create_sww(self):
     86    def _create_sww(self,stage=10.0, timestep=2.0):
    8787        self.sww = SWW_file(self.domain)
    8888        self.sww.store_connectivity()
    8989        self.sww.store_timestep()
    90         self.domain.set_quantity('stage', 10.0) # This is automatically limited
     90        self.domain.set_quantity('stage', stage) # This is automatically limited
    9191        # so it will not be less than the elevation
    92         self.domain.time = 2.
     92        self.domain.set_time(self.domain.get_time()-self.domain.starttime+timestep)
    9393        self.sww.store_timestep()
    9494       
     
    459459        os.remove(points_file)
    460460        os.remove(point1_filename)
    461        
    462        
     461
     462    def test_sww2csv_multiple_files(self):
     463        """
     464        This is testing the sww2csv_gauges function, by creating a multiple
     465        sww file and then exporting the gauges and checking the results.
     466        """
     467        timestep=2.0
     468        domain = self.domain
     469        domain.set_starttime(0.)
     470        # Create two sww files with timestep at end. These are to be
     471        # stored consecutively in the gauge csv files
     472        basename='datatest'
     473        domain.set_name(basename)
     474        self._create_sww(stage=10.,timestep=timestep)
     475
     476        domain.set_name(basename+str(time.time()))
     477        domain.set_time(domain.get_time()+timestep)
     478        self._create_sww(stage=20.,timestep=timestep)
     479
     480        points_file = tempfile.mktemp(".csv")
     481        file_id = open(points_file,"w")
     482
     483        # test the function at these points
     484        points = [[5.0,1.],[0.5,2.]]
     485
     486        # create a csv file containing our gauge points
     487        points_file = tempfile.mktemp(".csv")
     488        file_id = open(points_file,"w")
     489        file_id.write("name,easting,northing \n\
     490point1, 5.0, 1.0\n\
     491point2, 0.5, 2.0\n")
     492        file_id.close()
     493
     494        sww2csv_gauges(basename+".sww",
     495                       points_file,
     496                       quantities=['stage', 'elevation'],
     497                       use_cache=False,
     498                       verbose=False)
     499
     500        point1_answers_array = [[0.0,1.0,-5.0], [2.0,10.0,-5.0],[4.0,10.0,-5.0],
     501                                [6.0,20.0,-5.0]]
     502        point1_filename = 'gauge_point1.csv'
     503        point1_handle = file(point1_filename)
     504        point1_reader = reader(point1_handle)
     505        point1_reader.next()
     506
     507        line=[]
     508        for i,row in enumerate(point1_reader):
     509            # note the 'hole' (element 1) below - skip the new 'hours' field
     510            line.append([float(row[0]),float(row[2]),float(row[3])])
     511            #print 'line',line[i],'point1',point1_answers_array[i]
     512            assert num.allclose(line[i], point1_answers_array[i])
     513
     514        point2_answers_array = [[0.0,1.0,-0.5], [2.0,10.0,-0.5],[4.0,10.0,-0.5],
     515                                [6.0,20.0,-0.5]]
     516        point2_filename = 'gauge_point2.csv'
     517        point2_handle = file(point2_filename)
     518        point2_reader = reader(point2_handle)
     519        point2_reader.next()
     520                       
     521        line=[]
     522        for i,row in enumerate(point2_reader):
     523            # note the 'hole' (element 1) below - skip the new 'hours' field
     524            line.append([float(row[0]),float(row[2]),float(row[3])])
     525            #print 'line',line[i],'point2',point2_answers_array[i]
     526            assert num.allclose(line[i], point2_answers_array[i])
     527                         
     528        # clean up
     529        point1_handle.close()
     530        point2_handle.close()
     531        os.remove(points_file)
     532        os.remove(point1_filename)
     533        os.remove(point2_filename)       
     534
     535        #remove second swwfile not removed by tearDown
     536        os.remove(basename+".sww")
    463537
    464538#-------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.