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/file_conversion.py

    r7800 r7814  
    2929
    3030
    31 def sww2obj(basefilename, size):
     31def sww2obj(filename, size):
    3232    """ Convert netcdf based data output to obj
    3333
     
    3939    from Scientific.IO.NetCDF import NetCDFFile
    4040
     41    if filename[-4:] != '.sww':
     42        raise IOError('Output file %s should be of type .sww.' % sww_file)
     43
     44    basefilename = filename[:-4]
     45
    4146    # Get NetCDF
    42     FN = create_filename('.', basefilename, 'sww', size)
    43     log.critical('Reading from %s' % FN)
    44     fid = NetCDFFile(FN, netcdf_mode_r)  #Open existing file for read
     47    nc_fname = create_filename('.', basefilename, 'sww', size)
     48    log.critical('Reading from %s' % nc_fname)
     49    fid = NetCDFFile(nc_fname, netcdf_mode_r)  #Open existing file for read
    4550
    4651    # Get the variables
     
    8186        write_obj(FN, xx, yy, zz)
    8287
    83 
    84 ##
    85 # @brief
    86 # @param basefilename Stem of filename, needs size and extension added.
    87 def dat2obj(basefilename):
    88     """Convert line based data output to obj
    89     FIXME: Obsolete?
    90     """
    91 
    92     import glob, os
    93     from anuga.config import data_dir
    94 
    95     # Get bathymetry and x,y's
    96     lines = open(data_dir+os.sep+basefilename+'_geometry.dat', 'r').readlines()
    97 
    98     M = len(lines)  #Number of lines
    99     x = num.zeros((M,3), num.float)
    100     y = num.zeros((M,3), num.float)
    101     z = num.zeros((M,3), num.float)
    102 
    103     for i, line in enumerate(lines):
    104         tokens = line.split()
    105         values = map(float, tokens)
    106 
    107         for j in range(3):
    108             x[i,j] = values[j*3]
    109             y[i,j] = values[j*3+1]
    110             z[i,j] = values[j*3+2]
    111 
    112     # Write obj for bathymetry
    113     write_obj(data_dir + os.sep + basefilename + '_geometry', x, y, z)
    114 
    115     # Now read all the data files with variable information, combine with
    116     # x,y info and store as obj.
    117 
    118     files = glob.glob(data_dir + os.sep + basefilename + '*.dat')
    119     for filename in files:
    120         log.critical('Processing %s' % filename)
    121 
    122         lines = open(data_dir + os.sep + filename, 'r').readlines()
    123         assert len(lines) == M
    124         root, ext = os.path.splitext(filename)
    125 
    126         # Get time from filename
    127         i0 = filename.find('_time=')
    128         if i0 == -1:
    129             #Skip bathymetry file
    130             continue
    131 
    132         i0 += 6  #Position where time starts
    133         i1 = filename.find('.dat')
    134 
    135         if i1 > i0:
    136             t = float(filename[i0:i1])
    137         else:
    138             raise DataTimeError, 'Hmmmm'
    139 
    140         for i, line in enumerate(lines):
    141             tokens = line.split()
    142             values = map(float,tokens)
    143 
    144             for j in range(3):
    145                 z[i,j] = values[j]
    146 
    147         # Write obj for variable data
    148         write_obj(data_dir + os.sep + basefilename + '_time=%.4f' % t, x, y, z)
    149 
    15088##
    15189# @brief Convert time-series text file to TMS file.
     
    15391# @param quantity_names
    15492# @param time_as_seconds
    155 def timefile2netcdf(filename, quantity_names=None, time_as_seconds=False):
     93def timefile2netcdf(file_text, quantity_names=None, time_as_seconds=False):
    15694    """Template for converting typical text files with time series to
    15795    NetCDF tms file.
     
    180118    from anuga.utilities.numerical_tools import ensure_numeric
    181119
    182     file_text = filename + '.txt'
     120    if file_text[-4:] != '.txt':
     121        raise IOError('Input file %s should be of type .txt.' % file_text)
     122
     123    filename = file_text[:-4]
    183124    fid = open(file_text)
    184125    line = fid.readline()
     
    288229    to check if a tsh/msh file 'looks' good.
    289230    """
     231
     232    if filename[-4:] != '.tsh' and filename[-4:] != '.msh':
     233        raise IOError('Input file %s should be .tsh or .msh.' % name_out)
    290234
    291235    if verbose == True: log.critical('Creating domain from %s' % filename)
Note: See TracChangeset for help on using the changeset viewer.