Changeset 1066


Ignore:
Timestamp:
Mar 11, 2005, 6:10:43 PM (19 years ago)
Author:
prow
Message:

Making it save to name_time_tt_tt.sww if size is likely to exceed max_size (2G)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pyvolution/data_manager.py

    r1044 r1066  
    208208
    209209
    210     def __init__(self, domain, mode = 'w'):
     210    def __init__(self, domain, mode = 'w', filename_ext = ''):
    211211        from Scientific.IO.NetCDF import NetCDFFile
    212212        from Numeric import Int, Float, Float32
    213213
    214214        self.precision = Float32 #Use single precision
     215        self.max_size = 2000000000#file size max is 2Gig
     216        self.mode = mode
    215217
    216218        Data_format.__init__(self, domain, 'sww', mode)
     
    333335        import types
    334336        from time import sleep
     337        from os import stat
    335338
    336339
     
    358361
    359362
    360         domain = self.domain
    361 
    362         # Get the variables
     363
     364        #Check to see if the file is already too big:
    363365        time = fid.variables['time']
    364         stage = fid.variables['stage']
    365         xmomentum = fid.variables['xmomentum']
    366         ymomentum = fid.variables['ymomentum']
    367         i = len(time)
    368 
    369         #Store time
    370         time[i] = self.domain.time
    371 
    372 
    373         if type(names) not in [types.ListType, types.TupleType]:
    374             names = [names]
    375 
    376         for name in names:
    377             # Get quantity
    378             Q = domain.quantities[name]
    379             A,V = Q.get_vertex_values(xy=False,
     366        i = len(time)+1
     367        file_size = stat(self.filename)[6]
     368        file_size_increase =  file_size/i
     369        if file_size + file_size_increase > self.max_size:
     370            #in order to get the file name and start time correct,
     371            #I change the domian.filename and domain.starttime.
     372            #This is the only way to do this without changing
     373            #other modules (I think).
     374
     375            #write a filename addon that won't bread swollen
     376            filename_ext = '_time_%s'%self.domain.time           
     377            filename_ext = filename_ext.replace('.', '_')
     378            #remember the old filename, then give domain a
     379            #name with the extension
     380            old_domain_filename = self.domain.filename
     381            self.domain.filename = self.domain.filename+filename_ext
     382
     383            #change the domain starttime to the current time
     384            old_domain_starttime = self.domain.starttime
     385            self.domain.starttime = self.domain.time
     386
     387            #build a new data_structure.
     388            next_data_structure=\
     389                Data_format_sww(self.domain, mode=self.mode\
     390                                ,filename_ext=filename_ext)
     391            print '    file_size = %s'%file_size
     392            print '    saving file to %s'%self.next_data_structure.filename
     393
     394            #set up the new data_structure
     395            self.domain.writer = next_data_structure
     396
     397            #FIXME - could be cleaner to use domain.store_timestep etc.
     398            next_data_structure.store_connectivity()
     399            next_data_structure.store_timestep(names)
     400            fid.sync()
     401            fid.close()
     402
     403            #restore the old starttime and filename
     404            self.domain.starttime = old_domain_starttime
     405            self.domain.filename = old_domain_filename
     406        else:
     407            domain = self.domain
     408
     409            # Get the variables
     410            time = fid.variables['time']
     411            stage = fid.variables['stage']
     412            xmomentum = fid.variables['xmomentum']
     413            ymomentum = fid.variables['ymomentum']
     414            i = len(time)
     415
     416            #Store time
     417            time[i] = self.domain.time
     418
     419
     420            if type(names) not in [types.ListType, types.TupleType]:
     421                names = [names]
     422
     423            for name in names:
     424                # Get quantity
     425                Q = domain.quantities[name]
     426                A,V = Q.get_vertex_values(xy=False,
    380427                                      precision = self.precision)
    381428
    382             #FIXME: Make this general (see below)
    383             if name == 'stage':
    384                 stage[i,:] = A.astype(self.precision)
    385             elif name == 'xmomentum':
    386                 xmomentum[i,:] = A.astype(self.precision)
    387             elif name == 'ymomentum':
    388                 ymomentum[i,:] = A.astype(self.precision)
    389 
    390             #As in....
    391             #eval( name + '[i,:] = A.astype(self.precision)' )
    392             #FIXME: But we need a UNIT test for that before refactoring
    393 
    394 
    395 
    396         #Flush and close
    397         fid.sync()
    398         fid.close()
     429                #FIXME: Make this general (see below)
     430                if name == 'stage':
     431                    stage[i,:] = A.astype(self.precision)
     432                elif name == 'xmomentum':
     433                    xmomentum[i,:] = A.astype(self.precision)
     434                elif name == 'ymomentum':
     435                    ymomentum[i,:] = A.astype(self.precision)
     436
     437                #As in....
     438                #eval( name + '[i,:] = A.astype(self.precision)' )
     439                #FIXME: But we need a UNIT test for that before refactoring
     440
     441
     442
     443            #Flush and close
     444            fid.sync()
     445            fid.close()
    399446
    400447
Note: See TracChangeset for help on using the changeset viewer.