[5686] | 1 | """ |
---|
[5696] | 2 | A file conversion. |
---|
[5686] | 3 | |
---|
| 4 | Duncan Gray, GA - 2007 |
---|
| 5 | |
---|
| 6 | """ |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | #---------------------------------------------------------------------------- |
---|
| 10 | # Import necessary modules |
---|
| 11 | #---------------------------------------------------------------------------- |
---|
| 12 | |
---|
| 13 | from Scientific.IO.NetCDF import NetCDFFile |
---|
| 14 | from Numeric import zeros, Float |
---|
| 15 | |
---|
| 16 | from anuga.utilities.numerical_tools import ensure_numeric |
---|
| 17 | from anuga.utilities.interp import interp |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | def csv2tms(filename, offshore_bed_elevation): |
---|
[5696] | 21 | """ |
---|
| 22 | Convert Hinwood boundary file to NetCDF tms file. |
---|
[5686] | 23 | the filename is the name of the output tms file, eg 'hi.tsm'. |
---|
| 24 | There must be an equivalent .csv file, eg 'hi.csv'. |
---|
| 25 | |
---|
| 26 | """ |
---|
| 27 | print 'Creating', filename |
---|
| 28 | |
---|
| 29 | # Read the ascii (.csv) version of this file |
---|
| 30 | fid = open(filename[:-4] + '.csv') |
---|
| 31 | |
---|
[5691] | 32 | #Skip first line |
---|
| 33 | line = fid.readline() |
---|
| 34 | |
---|
[5686] | 35 | # Read remaining lines |
---|
| 36 | lines = fid.readlines() |
---|
| 37 | fid.close() |
---|
| 38 | |
---|
| 39 | N = len(lines) |
---|
| 40 | T = zeros(N, Float) #Time |
---|
| 41 | Q = zeros(N, Float) #Stage |
---|
| 42 | X = zeros(N, Float) #XMomentum |
---|
| 43 | Y = zeros(N, Float) #YMomentum |
---|
| 44 | |
---|
| 45 | for i, line in enumerate(lines): |
---|
| 46 | fields = line.split(',') |
---|
| 47 | |
---|
| 48 | T[i] = float(fields[0]) |
---|
| 49 | Q[i] = float(fields[1]) |
---|
| 50 | depth = Q[i] - offshore_bed_elevation |
---|
| 51 | X[i] = float(fields[2]) * depth |
---|
| 52 | Y[i] = float(fields[3]) * depth |
---|
| 53 | |
---|
| 54 | # Create tms NetCDF file |
---|
| 55 | fid = NetCDFFile(filename, 'w') |
---|
| 56 | fid.institution = 'Geoscience Australia' |
---|
| 57 | fid.description = 'Input wave for Benchmark 2' |
---|
| 58 | fid.starttime = 0.0 |
---|
| 59 | fid.createDimension('number_of_timesteps', len(T)) |
---|
| 60 | fid.createVariable('time', Float, ('number_of_timesteps',)) |
---|
| 61 | fid.variables['time'][:] = T |
---|
| 62 | |
---|
| 63 | fid.createVariable('stage', Float, ('number_of_timesteps',)) |
---|
| 64 | fid.variables['stage'][:] = Q[:] |
---|
| 65 | |
---|
| 66 | fid.createVariable('xmomentum', Float, ('number_of_timesteps',)) |
---|
| 67 | fid.variables['xmomentum'][:] = X[:] |
---|
| 68 | |
---|
| 69 | fid.createVariable('ymomentum', Float, ('number_of_timesteps',)) |
---|
| 70 | fid.variables['ymomentum'][:] = Y[:] |
---|
| 71 | |
---|
| 72 | fid.close() |
---|