Ignore:
Timestamp:
Jun 9, 2010, 12:28:24 PM (12 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/asc2dem.py

    r7804 r7814  
    77                            netcdf_float
    88
    9 ##
    10 # @brief Convert ASC file to DEM file.
    11 # formerly convert_dem_from_ascii2netcdf
    12 # @param basename_in Stem of input filename.
    13 # @param basename_out Stem of output filename.
    14 # @param use_cache ??
    15 # @param verbose True if this function is to be verbose.
    16 # @return
    17 # @note A PRJ file with same stem basename must exist and is used to fix the
    18 #       UTM zone, datum, false northings and eastings.
    19 def asc2dem(basename_in, basename_out=None,
     9def asc2dem(name_in, name_out=None,
    2010                                  use_cache=False,
    2111                                  verbose=False):
     
    3121    138.3698 137.4194 136.5062 135.5558 ..........
    3222
    33     Convert basename_in + '.asc' to NetCDF format (.dem)
     23    Convert name_in (.asc) to NetCDF format (.dem)
    3424    mimicking the ASCII format closely.
    3525
    36     An accompanying file with same basename_in but extension .prj must exist
     26    An accompanying file with same basename but extension .prj must exist
    3727    and is used to fix the UTM zone, datum, false northings and eastings.
    3828
     
    5040    """
    5141
    52     kwargs = {'basename_out': basename_out, 'verbose': verbose}
     42    kwargs = {'name_out': name_out, 'verbose': verbose}
    5343
    5444    if use_cache is True:
    5545        from caching import cache
    56         result = cache(_convert_dem_from_ascii2netcdf, basename_in, kwargs,
    57                        dependencies=[basename_in + '.asc',
    58                                      basename_in + '.prj'],
     46        result = cache(_convert_dem_from_ascii2netcdf, name_in, kwargs,
     47                       dependencies=[name_in,
     48                                     name_in[:-4] + '.prj'],
    5949                       verbose=verbose)
    6050
    6151    else:
    62         result = apply(_convert_dem_from_ascii2netcdf, [basename_in], kwargs)
     52        result = apply(_convert_dem_from_ascii2netcdf, [name_in], kwargs)
    6353
    6454    return result
     
    7060# @param basename_out Stem of output filename.
    7161# @param verbose True if this function is to be verbose.
    72 def _convert_dem_from_ascii2netcdf(basename_in, basename_out = None,
     62def _convert_dem_from_ascii2netcdf(name_in, name_out = None,
    7363                                   verbose = False):
    7464    """Read Digital Elevation model from the following ASCII format (.asc)
     
    8171    from Scientific.IO.NetCDF import NetCDFFile
    8272
    83     root = basename_in
     73    root = name_in[:-4]
    8474
    8575    # Read Meta data
     
    122112    false_northing = float(L[1].strip())
    123113
     114    if name_in[-4:] != '.asc':
     115        raise IOError('Input file %s should be of type .asc.' % name_in)
     116
    124117    #Read DEM data
    125     datafile = open(basename_in + '.asc')
    126 
    127     if verbose: log.critical('Reading DEM from %s' % (basename_in + '.asc'))
     118    datafile = open(name_in)
     119
     120    if verbose: log.critical('Reading DEM from %s' % (name_in))
    128121
    129122    lines = datafile.readlines()
     
    163156    assert len(lines) == nrows + 6
    164157
    165     if basename_out == None:
    166         netcdfname = root + '.dem'
    167     else:
    168         netcdfname = basename_out + '.dem'
     158    if name_out == None:
     159        netcdfname = name_in[:-4]+'.dem'
     160    else:
     161        netcdfname = name_out + '.dem'
    169162
    170163    if verbose: log.critical('Store to NetCDF file %s' % netcdfname)
     
    211204           
    212205        if len(fields) != ncols:
    213             msg = 'Wrong number of columns in file "%s" line %d\n' % (basename_in + '.asc', i)
     206            msg = 'Wrong number of columns in file "%s" line %d\n' % (name_in, i)
    214207            msg += 'I got %d elements, but there should have been %d\n' % (len(fields), ncols)
    215208            raise Exception, msg
Note: See TracChangeset for help on using the changeset viewer.