Changeset 6343


Ignore:
Timestamp:
Feb 16, 2009, 9:00:59 AM (15 years ago)
Author:
kristy
Message:

Added build_boundary

File:
1 edited

Legend:

Unmodified
Added
Removed
  • anuga_work/production/busselton/standardised_version/run_model.py

    r6329 r6343  
    2222# Standard modules
    2323import os
     24import os.path
    2425import time
     26from time import localtime, strftime, gmtime
    2527
    2628# Related major packages
     29from Scientific.IO.NetCDF import NetCDFFile
     30import Numeric as num
     31
    2732from anuga.interface import create_domain_from_regions
    2833from anuga.interface import Transmissive_stage_zero_momentum_boundary
     
    3237from anuga.interface import create_sts_boundary
    3338from anuga.interface import csv2building_polygons
     39<<<<<<< .mine
     40=======
    3441from file_length import file_length
    3542
     43>>>>>>> .r6341
    3644from anuga.shallow_water.data_manager import start_screen_catcher
    3745from anuga.shallow_water.data_manager import copy_code_files
     46from anuga.shallow_water.data_manager import urs2sts
    3847from anuga.utilities.polygon import read_polygon, Polygon_function
    39    
     48
    4049# Application specific imports
    4150from setup_model import project
    42 import build_urs_boundary as bub
    43 
     51
     52
     53#-------------------------------------------------------------------------------
     54# Get gauges (timeseries of index points)
     55#-------------------------------------------------------------------------------
     56def get_sts_gauge_data(filename, verbose=False):
     57    fid = NetCDFFile(filename+'.sts', 'r')      #Open existing file for read
     58    permutation = fid.variables['permutation'][:]
     59    x = fid.variables['x'][:] + fid.xllcorner   #x-coordinates of vertices
     60    y = fid.variables['y'][:] + fid.yllcorner   #y-coordinates of vertices
     61    points = num.transpose(num.asarray([x.tolist(), y.tolist()]))
     62    time = fid.variables['time'][:] + fid.starttime
     63    elevation = fid.variables['elevation'][:]
     64       
     65    basename = 'sts_gauge'
     66    quantity_names = ['stage', 'xmomentum', 'ymomentum']
     67    quantities = {}
     68    for i, name in enumerate(quantity_names):
     69        quantities[name] = fid.variables[name][:]
     70
     71    #---------------------------------------------------------------------------
     72    # Get maximum wave height throughout timeseries at each index point
     73    #---------------------------------------------------------------------------
     74
     75    maxname = 'max_sts_stage.csv'
     76    fid_max = open(os.path.join(project.event_sts, maxname), 'w')
     77    fid_max.write('index, x, y, max_stage \n')   
     78    for j in range(len(x)):
     79        index = permutation[j]
     80        stage = quantities['stage'][:,j]
     81        xmomentum = quantities['xmomentum'][:,j]
     82        ymomentum = quantities['ymomentum'][:,j]
     83
     84        fid_max.write('%d, %.6f, %.6f, %.6f\n' % (index, x[j], y[j], max(stage)))
     85     
     86    #---------------------------------------------------------------------------
     87    # Get minimum wave height throughout timeseries at each index point
     88    #---------------------------------------------------------------------------
     89
     90    minname = 'min_sts_stage.csv'
     91    fid_min = open(os.path.join(project.event_sts, minname), 'w')
     92    fid_min.write('index, x, y, max_stage \n')   
     93    for j in range(len(x)):
     94        index = permutation[j]
     95        stage = quantities['stage'][:,j]
     96        xmomentum = quantities['xmomentum'][:,j]
     97        ymomentum = quantities['ymomentum'][:,j]
     98
     99        fid_min.write('%d, %.6f, %.6f, %.6f\n' %(index, x[j], y[j], min(stage)))
     100
     101        out_file = os.path.join(project.event_sts,
     102                                basename+'_'+str(index)+'.csv')
     103        fid_sts = open(out_file, 'w')
     104        fid_sts.write('time, stage, xmomentum, ymomentum \n')
     105
     106        #-----------------------------------------------------------------------
     107        # End of the get gauges
     108        #-----------------------------------------------------------------------
     109        for k in range(len(time)-1):
     110            fid_sts.write('%.6f, %.6f, %.6f, %.6f\n'
     111                          % (time[k], stage[k], xmomentum[k], ymomentum[k]))
     112
     113        fid_sts.close()     
     114
     115    fid.close()
     116
     117    return quantities,elevation,time
     118
     119
     120##
     121# @brief Build boundary STS files from one or more MUX files.
     122# @param mux_dir Directory containing the MUX files.
     123# @param event_file Path to meta-file containing mux files+weight data.
     124# @param output_dir Directory to write STS data to.
     125# @note 'event_file' is produced by EventSelection.
     126def build_urs_boundary(mux_dir, event_file, output_dir):
     127    '''Build a boundary STS file from a set of MUX files.'''
     128
     129    print 'build_urs_boundary: mux_dir=%s' % mux_dir
     130    print 'build_urs_boundary: event_file=%s' % event_file
     131    print 'build_urs_boundary: output_dir=%s' % output_dir
     132
     133    # if we are using an EventSelection multi-mux file
     134    if project.multi_mux:   
     135        # get the mux+weight data from the event file
     136        mux_event_file = os.path.join(mux_dir, event_file)
     137        try:
     138            fd = open(mux_event_file, 'r')
     139            mux_data = fd.readlines()
     140            fd.close()
     141        except IOError, e:
     142            msg = 'File %s cannot be read: %s' % (mux_event_file, str(e))
     143            raise Exception, msg
     144        except:
     145            raise
     146
     147        # first line of file is # filenames+weight in rest of file
     148        num_lines = int(mux_data[0].strip())
     149        mux_data = mux_data[1:]
     150        print 'number of sources %d' % num_lines
     151
     152        # quick sanity check on input mux meta-file
     153        if num_lines != len(mux_data):
     154            msg = ('Bad file %s: %d data lines, but line 1 count is %d'
     155                   % (event_file, len(mux_data), num_lines))
     156            raise Exception, msg
     157
     158        # Create filename and weights lists.
     159        # Must chop GRD filename just after '*.grd'.
     160        mux_filenames = []
     161        for line in mux_data:
     162            muxname = line.strip().split()[0]
     163            split_index = muxname.index('.grd')
     164            muxname = muxname[:split_index+len('.grd')]
     165            muxname = os.path.join(mux_dir, muxname)
     166            mux_filenames.append(muxname)
     167       
     168        mux_weights = [float(line.strip().split()[1]) for line in mux_data]
     169
     170        # Call legacy function to create STS file.
     171        # This should be replaced in the future.
     172        print 'reading', project.urs_order
     173        print 'creating STS file'
     174        print 'mux_filenames=%s' % str(mux_filenames)
     175        print 'basename_out=%s' % str(output_dir)
     176        print 'ordering_filename=%s' % str(project.urs_order)
     177        print 'weights=%s' % str(mux_weights)
     178        print 'mean_stage=%s' % str(project.tide)
     179        urs2sts(mux_filenames,
     180                basename_out=output_dir,
     181                ordering_filename=project.urs_order,
     182                weights=mux_weights,
     183                mean_stage=project.tide,
     184                verbose=True)
     185    else:                           # a single mux stem file
     186        urs_filenames = [os.path.join(mux_dir, event_file)]
     187
     188        weight_factor = 1.0
     189        weights = weight_factor*num.ones(len(urs_filenames), num.float)
     190           
     191        order_filename = os.path.join(project.order_filename_dir)
     192
     193        print 'reading', order_filename
     194        # Create ordered sts file
     195        print 'creating sts file'
     196        urs2sts(urs_filenames,
     197                basename_out=os.path.join(project.boundaries_dir,
     198                                          project.scenario_name),
     199                ordering_filename=order_filename,
     200                weights=weights,
     201                mean_stage=project.tide,
     202                verbose=True)
     203
     204    # report on stuff so far
     205    quantities, elevation, time = get_sts_gauge_data(project.event_folder,
     206                                                     verbose=False)
     207    print len(elevation), len(quantities['stage'][0,:])
    44208
    45209#-------------------------------------------------------------------------------
     
    62226
    63227# Create the STS file
     228<<<<<<< .mine
     229build_urs_boundary(project.mux_data_folder,
     230                   project.mux_input,
     231                   os.path.join(project.event_folder,
     232                                project.scenario_name))
     233=======
    64234print 'project.mux_data_folder=%s' % project.mux_data_folder
    65235bub.build_urs_boundary(project.mux_input_filename,
    66236                       os.path.join(project.event_folder,
    67237                                    project.scenario_name))
     238>>>>>>> .r6341
    68239
    69240# Read in boundary from ordered sts file
Note: See TracChangeset for help on using the changeset viewer.