Ignore:
Timestamp:
Jul 4, 2008, 11:48:37 AM (15 years ago)
Author:
ole
Message:

John and Ole implemented urs2sts using an ordering file creating smaller and pre-ordered sts files for use with boundary conditions.

File:
1 edited

Legend:

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

    r5452 r5462  
    49034903def urs2sts(basename_in, basename_out = None, weights=None,
    49044904            verbose = False, origin = None,
    4905             mean_stage=0.0,zscale=1.0,
     4905            mean_stage=0.0, zscale=1.0,
     4906            ordering_filename = None,
    49064907            minlat = None, maxlat = None,
    49074908            minlon = None, maxlon = None):
     
    49134914    origin is a 3-tuple with geo referenced
    49144915    UTM coordinates (zone, easting, northing)
     4916   
     4917    input:
     4918    basename_in: list of source file prefixes
     4919   
     4920    These are combined with the extensions:
     4921    WAVEHEIGHT_MUX2_LABEL = '_waveheight-z-mux2' for stage
     4922    EAST_VELOCITY_MUX2_LABEL =  '_velocity-e-mux2' xmomentum
     4923    NORTH_VELOCITY_MUX2_LABEL =  '_velocity-n-mux2' and ymomentum
     4924   
     4925    to create a 2D list of mux2 file. The rows are associated with each quantity and must have the above extensions
     4926    the columns are the list of file prefixes.
     4927   
     4928    ordering: a .txt file name specifying which mux2 gauge points are sto be stored. This is indicated by the index of
     4929    the gauge in the ordering file.
     4930   
     4931    ordering file format:
     4932    header
     4933    index,longitude,latitude
     4934   
     4935    header='index,longitude,latitude\n'
     4936    If ordering is None all points are taken in the order they appear in the mux2 file subject to rectangular clipping box (see below).
     4937   
     4938   
     4939    minlat, minlon, maxlat, maxlon define a rectangular clipping box and can be used to extract gauge information in a rectangular box. This is useful for extracting
     4940    information at gauge points not on the boundary.
     4941    if ordering_filename is specified clipping box cannot be used
     4942   
     4943   
     4944    output:
     4945    baename_out: name of sts file in which mux2 data is stored.
     4946   
     4947   
    49154948    """
    49164949    import os
     
    49444977    precision = Float
    49454978
     4979    if ordering_filename is not None:
     4980        msg = 'If ordering_filename is specified,'
     4981        msg += ' rectangular clipping box cannot be specified as well. '
     4982        assert minlat is None and maxlat is None and\
     4983            minlon is None and maxlon is None, msg
     4984   
     4985   
    49464986    msg = 'Must use latitudes and longitudes for minlat, maxlon etc'
    4947 
    49484987    if minlat != None:
    49494988        assert -90 < minlat < 90 , msg
     
    49594998            assert maxlon > minlon
    49604999
     5000
     5001    # Check output filename   
    49615002    if basename_out is None:
    4962         stsname = basename_in[0] + '.sts'
    4963     else:
    4964         stsname = basename_out + '.sts'
     5003        msg = 'STS filename must be specified'
     5004        raise Exception, msg
     5005   
     5006    stsname = basename_out + '.sts'
    49655007
    49665008    files_in=[[],[],[]]
     
    49915033    mux={}
    49925034    for i,quantity in enumerate(quantities):
    4993         times_urs, latitudes_urs, longitudes_urs, elevation, mux[quantity],starttime = read_mux2_py(files_in[i],weights)
     5035        # For each quantity read the associated list of source mux2 file with extenstion associated with that quantity
     5036        times, latitudes_urs, longitudes_urs, elevation, mux[quantity],starttime = read_mux2_py(files_in[i],weights)
    49945037        if quantity!=quantities[0]:
    49955038            msg='%s, %s and %s have inconsitent gauge data'%(files_in[0],files_in[1],files_in[2])
    4996             assert allclose(times_urs,times_urs_old),msg
     5039            assert allclose(times,times_old),msg
    49975040            assert allclose(latitudes_urs,latitudes_urs_old),msg
    49985041            assert allclose(longitudes_urs,longitudes_urs_old),msg
    49995042            assert allclose(elevation,elevation_old),msg
    50005043            assert allclose(starttime,starttime_old)
    5001         times_urs_old=times_urs
     5044        times_old=times
    50025045        latitudes_urs_old=latitudes_urs
    50035046        longitudes_urs_old=longitudes_urs
    50045047        elevation_old=elevation
    50055048        starttime_old=starttime
    5006        
    5007     if (minlat is not None) and (minlon is not None) and (maxlat is not None) and (maxlon is not None):
     5049
     5050       
     5051    if ordering_filename is not None:
     5052        # Read ordering file
     5053       
     5054        try:
     5055            fid=open(ordering_filename, 'r')
     5056            file_header=fid.readline().split(',')
     5057            ordering_lines=fid.readlines()
     5058            fid.close()
     5059        except:
     5060            msg = 'Cannot open %s'%ordering_filename
     5061            raise Exception, msg
     5062
     5063        reference_header = 'index, longitude, latitude\n'.split(',')
     5064        for i in range(3):
     5065            if not file_header[i] == reference_header[i]:
     5066                msg = 'File must contain header\n'+header+'\n'
     5067                raise Exception, msg
     5068
     5069        if len(ordering_lines)<2:
     5070            msg = 'File must contain at least two points'
     5071            raise Exception, msg
     5072
     5073   
     5074       
     5075        permutation = [int(line.split(',')[0]) for line in ordering_lines]
     5076   
     5077        latitudes = take(latitudes_urs, permutation)
     5078        longitudes = take(longitudes_urs, permutation)
     5079       
     5080        # Self check - can be removed to improve speed
     5081        ref_longitudes = [float(line.split(',')[1]) for line in ordering_lines]               
     5082        ref_latitudes = [float(line.split(',')[2]) for line in ordering_lines]       
     5083       
     5084        msg = 'Longitudes specified in ordering file do not match those found in mux files'
     5085        msg += 'I got %s instead of %s (only beginning shown)'\
     5086            %(str(longitudes[:10]) + '...',
     5087              str(ref_longitudes[:10]) + '...')       
     5088        assert allclose(longitudes, ref_longitudes), msg       
     5089       
     5090        msg = 'Latitudes specified in ordering file do not match those found in mux files. '
     5091        msg += 'I got %s instead of %s (only beginning shown)'\
     5092            %(str(latitudes[:10]) + '...',
     5093              str(ref_latitudes[:10]) + '...')               
     5094        assert allclose(latitudes, ref_latitudes), msg
     5095       
     5096    elif (minlat is not None) and (minlon is not None) and (maxlat is not None) and (maxlon is not None):
     5097        #FIXME - this branch is probably not working   
    50085098        if verbose: print 'Cliping urs data'
    50095099        latitudes = compress((latitudes_urs>=minlat)&(latitudes_urs<=maxlat)&(longitudes_urs>=minlon)&(longitudes_urs<=maxlon),latitudes_urs)
    50105100        longitudes = compress((latitudes_urs>=minlat)&(latitudes_urs<=maxlat)&(longitudes_urs>=minlon)&(longitudes_urs<=maxlon),longitudes_urs)
    5011         times = compress((latitudes_urs>=minlat)&(latitudes_urs<=maxlat)&(longitudes_urs>=minlon)&(longitudes_urs<=maxlon),times_urs)
     5101       
     5102        permutation = range(latitudes.shape[0])       
     5103       
     5104        msg = 'Clipping is not done yet'
     5105        raise Exception, msg
     5106       
    50125107    else:
    50135108        latitudes=latitudes_urs
    50145109        longitudes=longitudes_urs
    5015         times=times_urs
     5110        permutation = range(latitudes.shape[0])               
     5111       
    50165112
    50175113    msg='File is empty and or clipped region not in file region'
     
    50325128    #starttime = times[0]
    50335129    sts = Write_sts()
    5034     sts.store_header(outfile, times+starttime,number_of_points, description=description,
    5035                      verbose=verbose,sts_precision=Float)
     5130    sts.store_header(outfile,
     5131                     times+starttime,
     5132                     number_of_points,
     5133                     description=description,
     5134                     verbose=verbose,
     5135                     sts_precision=Float)
    50365136   
    50375137    # Store
     
    50675167    stage = outfile.variables['stage']
    50685168    xmomentum = outfile.variables['xmomentum']
    5069     ymomentum =outfile.variables['ymomentum']
     5169    ymomentum = outfile.variables['ymomentum']
    50705170
    50715171    if verbose: print 'Converting quantities'
    50725172    for j in range(len(times)):
    5073         for i in range(number_of_points):
    5074             w = zscale*mux['HA'][i,j] + mean_stage
    5075             h=w-elevation[i]
     5173        for i, index in enumerate(permutation):
     5174            w = zscale*mux['HA'][index,j] + mean_stage
     5175            h=w-elevation[index]
    50765176            stage[j,i] = w
    5077             #delete following two lines once velcotu files are read in.
    5078             xmomentum[j,i] = mux['UA'][i,j]*h
    5079             ymomentum[j,i] = mux['VA'][i,j]*h
     5177
     5178            xmomentum[j,i] = mux['UA'][index,j]*h
     5179            ymomentum[j,i] = mux['VA'][index,j]*h
    50805180
    50815181    outfile.close()
     
    51195219    xllcorner = fid.xllcorner[0]
    51205220    yllcorner = fid.yllcorner[0]
    5121     #Points stored in sts file are normailsed to [xllcorner,yllcorner] but
     5221    #Points stored in sts file are normalised to [xllcorner,yllcorner] but
    51225222    #we cannot assume that boundary polygon will be. At least the
    51235223    #additional points specified by the user after this function is called
     
    51325232    yllcorner = fid.yllcorner[0]
    51335233
     5234    # Walk through ordering file
    51345235    boundary_polygon=[]
    51355236    for line in lines:
     
    51395240            zone,easting,northing=redfearn(float(fields[1]),float(fields[2]))
    51405241            if not zone == fid.zone[0]:
    5141                 msg = 'Inconsitent zones'
     5242                msg = 'Inconsistent zones'
    51425243                raise Exception, msg
    51435244        else:
    51445245            easting = float(fields[1])
    51455246            northing = float(fields[2])
     5247           
    51465248        if not allclose(array([easting,northing]),sts_points[index]):
    51475249            msg = "Points in sts file do not match those in the"+\
    5148                 ".txt file spcifying the order of the boundary points"
     5250                ".txt file specifying the order of the boundary points"
    51495251            raise Exception, msg
    51505252        boundary_polygon.append(sts_points[index].tolist())
Note: See TracChangeset for help on using the changeset viewer.