"""Thin out MOST NetCDF bathymetry and store as nc file

"""

#input_file = 'e5_bathy_corr5.cdf'
#output_file = 'SU-AU_e.nc'

#This should have a test

def thinbathy(input_file, output_file):

    lat_thin = lon_thin = 4 

    from Scientific.IO.NetCDF import NetCDFFile

    fid = NetCDFFile(input_file, 'r')
    print fid.variables

    # for first event
    latitudes = fid.variables['LAT'][::lat_thin]
    longitudes = fid.variables['LON'][::lon_thin]
    elevations = fid.variables['ELEVATION'][::lat_thin,::lon_thin]

    outfile = NetCDFFile(output_file, 'w')
    outfile.createDimension('LON', fid.dimensions['LON']/lon_thin)
    outfile.createDimension('LAT', fid.dimensions['LAT']/lat_thin)
    
##    # for second event
##    latitudes = fid.variables['LAT601_690'][::lat_thin]
##    longitudes = fid.variables['LON645_705'][::lon_thin]
##    elevations = fid.variables['ELEVATION'][::lat_thin,::lon_thin]
##
##    outfile = NetCDFFile(output_file, 'w')
##    outfile.createDimension('LON', fid.dimensions['LON645_705']/lon_thin + 1)
##    outfile.createDimension('LAT', fid.dimensions['LAT601_690']/lat_thin + 1)

    import Numeric
    outfile.createVariable('LON', Numeric.Float64, ('LON',))
    outfile.createVariable('LAT', Numeric.Float64, ('LAT',))
    outfile.createVariable('ELEVATION', Numeric.Float32, ('LAT', 'LON'))

    #Assign to NC file
    #Precision used by MOST for lat/lon is 4 or 5 decimals
    outfile.variables['LAT'][:] = Numeric.around(latitudes, 5)
    outfile.variables['LON'][:] = Numeric.around(longitudes, 5)
    outfile.variables['ELEVATION'][:] = elevations

    fid.close()
    outfile.close()
