Ignore:
Timestamp:
Jun 9, 2010, 12:28:24 PM (13 years ago)
Author:
hudson
Message:

New filename conventions for file conversion. Filenames must always be passed in with the correct extension.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/anuga_core/source/anuga/file_conversion/sww2dem.py

    r7796 r7814  
    2929DEFAULT_BLOCK_SIZE = 10000
    3030
    31 def sww2dem(basename_in, basename_out=None,
     31def sww2dem(name_in, name_out,
    3232            quantity=None, # defaults to elevation
    3333            reduction=None,
     
    4242            origin=None,
    4343            datum='WGS84',
    44             format='ers',
    4544            block_size=None):
    4645    """Read SWW file and convert to Digitial Elevation model format
     
    9897         apply_expression_to_dictionary
    9998
    100     msg = 'Format must be either asc or ers'
    101     assert format.lower() in ['asc', 'ers'], msg
     99    basename_in, in_ext = os.path.splitext(name_in)
     100    basename_out, out_ext = os.path.splitext(name_out)
     101    out_ext = out_ext.lower()
     102
     103    if in_ext != '.sww':
     104        raise IOError('Input format for %s must be .sww' % name_in)
     105
     106    if out_ext not in ['.asc', '.ers']:
     107        raise IOError('Format for %s must be either asc or ers.' % name_out)
    102108
    103109    false_easting = 500000
     
    110116        reduction = max
    111117
    112     if basename_out is None:
    113         basename_out = basename_in + '_%s' % quantity
    114 
    115118    if quantity_formula.has_key(quantity):
    116119        quantity = quantity_formula[quantity]
     
    122125        block_size = DEFAULT_BLOCK_SIZE
    123126
    124     # Read SWW file
    125     swwfile = basename_in + '.sww'
    126     demfile = basename_out + '.' + format
    127 
    128127    # Read sww file
    129128    if verbose:
    130         log.critical('Reading from %s' % swwfile)
    131         log.critical('Output directory is %s' % basename_out)
     129        log.critical('Reading from %s' % name_in)
     130        log.critical('Output directory is %s' % name_out)
    132131
    133132    from Scientific.IO.NetCDF import NetCDFFile
    134     fid = NetCDFFile(swwfile)
     133    fid = NetCDFFile(name_in)
    135134
    136135    #Get extent and reference
     
    168167        log.critical('------------------------------------------------')
    169168        log.critical('Statistics of SWW file:')
    170         log.critical('  Name: %s' % swwfile)
     169        log.critical('  Name: %s' % name_in)
    171170        log.critical('  Reference:')
    172171        log.critical('    Lower left corner: [%f, %f]' % (xllcorner, yllcorner))
     
    212211    if missing_vars:
    213212        msg = ("In expression '%s', variables %s are not in the SWW file '%s'"
    214                % (quantity, swwfile))
     213               % (quantity, name_in))
    215214        raise Exception, msg
    216215
     
    300299
    301300    for i in xrange(nrows):
    302         if format.lower() == 'asc':
     301        if out_ext == '.asc':
    303302            yg = i * cellsize
    304303        else:
     
    336335        grid_values[i] = NODATA_value
    337336
    338     if format.lower() == 'ers':
     337    if out_ext == '.ers':
    339338        # setup ERS header information
    340339        grid_values = num.reshape(grid_values, (nrows, ncols))
     
    362361        import ermapper_grids
    363362
    364         ermapper_grids.write_ermapper_grid(demfile, grid_values, header)
     363        ermapper_grids.write_ermapper_grid(name_out, grid_values, header)
    365364
    366365        fid.close()
     
    383382        prjid.close()
    384383
    385         if verbose: log.critical('Writing %s' % demfile)
    386 
    387         ascid = open(demfile, 'w')
     384        if verbose: log.critical('Writing %s' % name_out)
     385
     386        ascid = open(name_out, 'w')
    388387
    389388        ascid.write('ncols         %d\n' %ncols)
     
    440439    See sww2dem to find out what most of the parameters do.
    441440
     441    basename_in is a path to sww file/s, without the .sww extension.
     442
    442443    Quantities is a list of quantities.  Each quantity will be
    443444    calculated for each sww file.
     
    473474                basename_out = sww_file + '_' + quantity + '_' + extra_name_out
    474475
    475             file_out = sww2dem(dir+os.sep+sww_file, dir+os.sep+basename_out,
     476            file_out = sww2dem(dir+os.sep+sww_file+'.sww',
     477                               dir+os.sep+basename_out,
    476478                               quantity,
    477479                               reduction,
Note: See TracChangeset for help on using the changeset viewer.