Changeset 6058


Ignore:
Timestamp:
Dec 11, 2008, 1:14:46 PM (15 years ago)
Author:
rwilson
Message:

Removed old 'run from commandline' code, added comments, pep8'd it.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/shallow_water/most2nc.py

    r3564 r6058  
    11"""This has to do with creating elevation data files for use with ferret2sww.
    2 It reads a bathymetry ascii file and creates a NetCDF (nc) file similar to MOSTs output.
     2It reads a bathymetry ascii file and creates a NetCDF (nc) file similar to
     3MOSTs output.
    34
    45 $Author: Peter Row
     
    67"""
    78
     9import sys
     10from Scientific.IO.NetCDF import NetCDFFile
    811
    9 def most2nc(input_file=None,output_file=None,inverted_bathymetry = False,\
    10             verbose = True):
    11     #input_file = 'small.txt'
    12     #output_file = 'small_e.nc'
    1312
     13##
     14# @brief Convert a MOST file to NetCDF format.
     15# @param input_file The input file to convert.
     16# @param output_file The name of the oputput NetCDF file to create,
     17# @param inverted_bathymetry ??
     18# @param verbose True if the function is to be verbose.
     19def most2nc(input_file, output_file, inverted_bathymetry=False, verbose=True):
     20    # variable names
    1421    long_name = 'LON'
    1522    lat_name = 'LAT'
     23    elev_name = 'ELEVATION'
     24
     25    # set up bathymetry
    1626    if inverted_bathymetry:
    1727        up = -1.
     
    1929        up = +1.
    2030
    21     from Scientific.IO.NetCDF import NetCDFFile
    22     import sys
     31    # read data from the MOST file
     32    in_file = open(input_file,'r')
    2333
    24     try:
    25         if input_file is None:
    26             input_file = sys.argv[1]
    27         if output_file is None:
    28             output_file = sys.argv[2]
    29     except:
    30         raise 'usage is: most2nc input output'
     34    if verbose: print 'reading header'
    3135
    32     in_file = open(input_file,'r')
    33     if verbose: print 'reading header'
    3436    nx_ny_str = in_file.readline()
    3537    nx_str,ny_str = nx_ny_str.split()
     
    6062            #j = k/nx
    6163            out_depth_list[(k-1)/nx].append(float(string)*up)
    62             #print k,len(out_depth_list),(k-1)/nx,out_depth_list[(k-1)/nx][-1],len(out_depth_list[(k-1)/nx])
    6364            if k==nx*ny:
    6465                break
     
    7172    depth_list = out_depth_list
    7273
     74    # write the NetCDF file
    7375    if verbose: print 'writing results'
    7476
     
    7678
    7779    out_file.createDimension(long_name,nx)
     80
    7881    out_file.createVariable(long_name,'d',(long_name,))
    7982    out_file.variables[long_name].point_spacing='uneven'
     
    8790    out_file.variables[lat_name].assignValue(h2_list)
    8891
    89     out_file.createVariable('ELEVATION','d',(lat_name,long_name))
    90     out_file.variables['ELEVATION'].point_spacing='uneven'
    91     out_file.variables['ELEVATION'].units='meters'
    92     out_file.variables['ELEVATION'].assignValue(depth_list)
     92    out_file.createVariable(elev_name,'d',(lat_name,long_name))
     93    out_file.variables[elev_name].point_spacing='uneven'
     94    out_file.variables[elev_name].units='meters'
     95    out_file.variables[elev_name].assignValue(depth_list)
    9396
    9497    out_file.close()
Note: See TracChangeset for help on using the changeset viewer.