""" A file conversion. Duncan Gray, GA - 2007 """ #---------------------------------------------------------------------------- # Import necessary modules #---------------------------------------------------------------------------- from Scientific.IO.NetCDF import NetCDFFile from Numeric import zeros, Float from anuga.utilities.numerical_tools import ensure_numeric from anuga.utilities.interp import interp def csv2tms(filename, offshore_bed_elevation): """ Convert Hinwood boundary file to NetCDF tms file. the filename is the name of the output tms file, eg 'hi.tsm'. There must be an equivalent .csv file, eg 'hi.csv'. """ print 'Creating', filename # Read the ascii (.csv) version of this file fid = open(filename[:-4] + '.csv') #Skip first line line = fid.readline() # Read remaining lines lines = fid.readlines() fid.close() N = len(lines) T = zeros(N, Float) #Time Q = zeros(N, Float) #Stage X = zeros(N, Float) #XMomentum Y = zeros(N, Float) #YMomentum for i, line in enumerate(lines): fields = line.split(',') T[i] = float(fields[0]) Q[i] = float(fields[1]) depth = Q[i] - offshore_bed_elevation X[i] = float(fields[2]) * depth Y[i] = float(fields[3]) * depth # Create tms NetCDF file fid = NetCDFFile(filename, 'w') fid.institution = 'Geoscience Australia' fid.description = 'Input wave for Benchmark 2' fid.starttime = 0.0 fid.createDimension('number_of_timesteps', len(T)) fid.createVariable('time', Float, ('number_of_timesteps',)) fid.variables['time'][:] = T fid.createVariable('stage', Float, ('number_of_timesteps',)) fid.variables['stage'][:] = Q[:] fid.createVariable('xmomentum', Float, ('number_of_timesteps',)) fid.variables['xmomentum'][:] = X[:] fid.createVariable('ymomentum', Float, ('number_of_timesteps',)) fid.variables['ymomentum'][:] = Y[:] fid.close()