Changeset 1122


Ignore:
Timestamp:
Mar 22, 2005, 4:19:10 PM (20 years ago)
Author:
prow
Message:
 
Location:
inundation/ga/storm_surge/pyvolution
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pyvolution/most2nc.py

    r1120 r1122  
    1 input_file = 'small.txt'
    2 output_file = 'small_e.nc'
     1def most2nc(input_file=None,output_file=None,inverted_bathymetry = False):
     2    #input_file = 'small.txt'
     3    #output_file = 'small_e.nc'
    34
    4 long_name = 'LON'
    5 lat_name = 'LAT'
     5    long_name = 'LON'
     6    lat_name = 'LAT'
     7    if inverted_bathymetry:
     8        up = +1.
     9    else:
     10        up = -1.
     11   
     12    from Scientific.IO.NetCDF import NetCDFFile
     13    import sys
     14   
     15    try:
     16        if input_file is None:
     17            input_file = sys.argv[1]
     18        if output_file is None:
     19            output_file = sys.argv[2]
     20    except:
     21        raise 'usage is: most2nc input output'
     22   
     23    in_file = open(input_file,'r')
     24    print 'reading header'
     25    nx_ny_str = in_file.readline()
     26    nx_str,ny_str = nx_ny_str.split()
     27    nx = int(nx_str)
     28    ny = int(ny_str)
     29    h1_list=[]
     30    for i in range(nx):
     31        h1_list.append(float(in_file.readline()))
    632
    7 up = +1.
     33    h2_list=[]
     34    for j in range(ny):
     35        h2_list.append(float(in_file.readline()))
    836
    9 from Scientific.IO.NetCDF import NetCDFFile
     37    h2_list.reverse()
    1038
    11 in_file = open(input_file,'r')
    12 print 'reading header'
    13 nx_ny_str = in_file.readline()
    14 nx_str,ny_str = nx_ny_str.split()
    15 nx = int(nx_str)
    16 ny = int(ny_str)
    17 h1_list=[]
    18 for i in range(nx):
    19     h1_list.append(float(in_file.readline()))
     39    print 'reading depths'
     40    in_depth_list = in_file.readlines()
     41    in_file.close()
    2042
    21 h2_list=[]
    22 for j in range(ny):
    23     h2_list.append(float(in_file.readline()))
     43    out_depth_list = [[]]
    2444
    25 h2_list.reverse()
     45    print 'processing depths'
     46    k=1
     47    for in_line in in_depth_list:
     48        for string in in_line.split():
     49            #j = k/nx
     50            out_depth_list[(k-1)/nx].append(float(string)*up)
     51            #print k,len(out_depth_list),(k-1)/nx,out_depth_list[(k-1)/nx][-1],len(out_depth_list[(k-1)/nx])
     52            if k==nx*ny:
     53                break
     54            if k-(k/nx)*nx ==0:
     55                out_depth_list.append([])
     56            k+=1
    2657
    27 print 'reading depths'
    28 in_depth_list = in_file.readlines()
    29 in_file.close()
     58    in_file.close()
     59    out_depth_list.reverse()
     60    depth_list = out_depth_list
     61    print out_depth_list
    3062
    31 out_depth_list = [[]]
     63    print 'writing results'
     64    out_file = NetCDFFile(output_file,'w')
    3265
    33 print 'processing depths'
    34 k=1
    35 for in_line in in_depth_list:
    36     for string in in_line.split():
    37         #j = k/nx
    38         out_depth_list[(k-1)/nx].append(float(string)*up)
    39         #print k,len(out_depth_list),(k-1)/nx,out_depth_list[(k-1)/nx][-1],len(out_depth_list[(k-1)/nx])
    40         if k==nx*ny:
    41             break
    42         if k-(k/nx)*nx ==0:
    43             out_depth_list.append([])
    44         k+=1
     66    out_file.createDimension(long_name,nx)
     67    out_file.createVariable(long_name,'d',(long_name,))
     68    out_file.variables[long_name].point_spacing='uneven'
     69    out_file.variables[long_name].units='degrees_east'
     70    out_file.variables[long_name].assignValue(h1_list)
    4571
    46 in_file.close()
    47 out_depth_list.reverse()
    48 depth_list = out_depth_list
    49 print out_depth_list
     72    out_file.createDimension(lat_name,ny)
     73    out_file.createVariable(lat_name,'d',(lat_name,))
     74    out_file.variables[lat_name].point_spacing='uneven'
     75    out_file.variables[lat_name].units='degrees_north'
     76    out_file.variables[lat_name].assignValue(h2_list)
    5077
    51 print 'writing results'
    52 out_file = NetCDFFile(output_file,'w')
     78    out_file.createVariable('ELEVATION','d',(lat_name,long_name))
     79    out_file.variables['ELEVATION'].point_spacing='uneven'
     80    out_file.variables['ELEVATION'].units='meters'
     81    out_file.variables['ELEVATION'].assignValue(depth_list)
    5382
    54 out_file.createDimension(long_name,nx)
    55 out_file.createVariable('LON','d',(long_name,))
    56 out_file.variables['LON'].point_spacing='uneven'
    57 out_file.variables['LON'].units='degrees_east'
    58 out_file.variables['LON'].assignValue(h1_list)
    59 
    60 out_file.createDimension(lat_name,ny)
    61 out_file.createVariable('LAT','d',(lat_name,))
    62 out_file.variables['LAT'].point_spacing='uneven'
    63 out_file.variables['LAT'].units='degrees_north'
    64 out_file.variables['LAT'].assignValue(h2_list)
    65 
    66 out_file.createVariable('ELEVATION','d',(lat_name,long_name))
    67 out_file.variables['ELEVATION'].point_spacing='uneven'
    68 out_file.variables['ELEVATION'].units='meters'
    69 out_file.variables['ELEVATION'].assignValue(depth_list)
    70 
    71 out_file.close()
     83    out_file.close()
Note: See TracChangeset for help on using the changeset viewer.