def open_new_cs_file(self,
                     file_name,
                     info=None,
                     var_name='X',
                     long_name='Unknown',
                     units_name='None',
                     dtype='float32',
                     time_units='minutes',
                     nx=None,
                     ny=None,
                     dx=None,
                     dy=None):

    #---------------------------
    # Was grid info provided ?
    #---------------------------
    if (info is not None):
        info.file_name = file_name
        info.data_type = rti_files.get_rti_data_type(dtype)
        if (nx is not None): info.ncols = nx
        if (ny is not None): info.nrows = ny
        if (dx is not None): info.xres = dx
        if (dy is not None): info.yres = dy
    else:
        if (nx is not None) and (ny is not None) and \
           (dx is not None) and (dy is not None):
            info = rti_files.make_info(file_name, nx, ny, dx, dy)
        else:
            print 'ERROR during open_new_cs_file().'
            print '      Grid info not provided.'
            print ' '
            ## return -1

    #--------------------------------------------
    # Open new netCDF file to write curbe stacks
    # using var_name to build variable names
    #--------------------------------------------
    try:
        nccs_unit_str = "self." + var_name + "_nccs_unit"
        nccs_file_str = "self." + var_name + "_nccs_file"
        cs_file_str = "self." + var_name + "_cs_file"

        exec(nccs_file_str + "= file_utils.replace_extension(" + cs_file_str +
             ", '.nc')")
        exec(nccs_unit_str + "=" + "nccs_files.nccs_file()")
        exec(nccs_unit_str + ".open_new_file(" + nccs_file_str +
             ", self.rti, var_name, long_name, units_name, " +
             "dtype=dtype, " +  ## (11/5/13)
             "time_units=time_units)")
        MAKE_RT3 = False
    except:
        print 'ERROR: Unable to open new netCDF file:'
        exec("print '      ', self." + var_name + "_nccs_file")
        print ' '
        print 'Will write cube stack in generic RT3 format.'
        print ' '
        MAKE_RT3 = True
Example #2
0
def open_new_cs_file(self, file_name, info=None,
                     var_name='X',
                     long_name='Unknown',
                     units_name='None',
                     dtype='float32',
                     time_units='minutes',
                     nx=None, ny=None, dx=None, dy=None):

    #---------------------------
    # Was grid info provided ?
    #---------------------------
    if (info != None):
        info.file_name = file_name
        info.data_type = rti_files.get_rti_data_type( dtype )
        if (nx != None): info.ncols = nx
        if (ny != None): info.nrows = ny
        if (dx != None): info.xres  = dx
        if (dy != None): info.yres  = dy
    else:
        if (nx != None) and (ny != None) and \
           (dx != None) and (dy != None):
            info = rti_files.make_info( file_name, nx, ny, dx, dy )
        else:
            print 'ERROR during open_new_cs_file().'
            print '      Grid info not provided.'
            print ' '
            ## return -1
        
    #--------------------------------------------
    # Open new netCDF file to write curbe stacks
    # using var_name to build variable names
    #--------------------------------------------
    try:
        nccs_unit_str = "self." + var_name + "_nccs_unit"
        nccs_file_str = "self." + var_name + "_nccs_file"
        cs_file_str   = "self." + var_name + "_cs_file"
            
        exec( nccs_file_str + "= file_utils.replace_extension(" +
              cs_file_str + ", '.nc')" )
        exec( nccs_unit_str + "=" + "nccs_files.nccs_file()" )
        exec( nccs_unit_str + ".open_new_file(" + nccs_file_str +
              ", self.rti, var_name, long_name, units_name, " +
              "dtype=dtype, " +  ## (11/5/13)
              "time_units=time_units)" )
        MAKE_RT3 = False
    except:
        print 'ERROR: Unable to open new netCDF file:'
        exec( "print '      ', self." + var_name + "_nccs_file" )
        print ' '
        print 'Will write cube stack in generic RT3 format.'
        print ' '
        MAKE_RT3 = True
Example #3
0
def open_new_gs_file(self, file_name, info=None,
                     var_name='X',
                     long_name='Unknown',
                     units_name='None',
                     dtype='float32',
                     time_units='minutes',
                     nx=None, ny=None, dx=None, dy=None):

    #---------------------------
    # Was grid info provided ?
    #---------------------------
    if (info != None):
        info.file_name = file_name
        info.data_type = rti_files.get_rti_data_type( dtype )
        if (nx != None): info.ncols = nx
        if (ny != None): info.nrows = ny
        if (dx != None): info.xres  = dx
        if (dy != None): info.yres  = dy
    else:
        if (nx != None) and (ny != None) and \
           (dx != None) and (dy != None):
            info = rti_files.make_info( file_name, nx, ny, dx, dy )
        else:
            print 'ERROR during open_new_gs_file().'
            print '      Grid info not provided.'
            print ' '
            ## return -1

    #---------------------------------------------
    # Get long_name and units_name from var_name
    #---------------------------------------------
#     long_name  = self.get_var_long_name( var_name )
#     units_name = self.get_var_units( var_name )
#     dtype      = self.get_var_type( var_name )  
           
    #---------------------------------
    # Build strings to be used below 
    #---------------------------------
    ncgs_unit_str = "self." + var_name + "_ncgs_unit"
    ncgs_file_str = "self." + var_name + "_ncgs_file"
    gs_file_str   = "self." + var_name + "_gs_file"
    #-------------------
    # For testing only
    #-------------------
    # print 'ncgs_unit_str =', ncgs_unit_str
    # print 'ncgs_file_str =', ncgs_file_str
    # print 'gs_file_str   =', gs_file_str
    # print 'var_name      =', var_name
    # print 'long_name     =', long_name
    # print 'units_name    =', units_name
    # print 'time_units    =', time_units
    # print ' '

    #--------------------------------------------
    # Open new netCDF file to write grid stacks
    # using var_name to build variable names
    #--------------------------------------------
    # Note that "dtype" was not included in the
    # last TopoFlow version (always float32),
    # but Erode needs other types.
    #--------------------------------------------
    try:
        exec( ncgs_file_str + "= file_utils.replace_extension(" +
              gs_file_str + ", '.nc')" )
        exec( ncgs_unit_str + "=" + "ncgs_files.ncgs_file()" )
        exec( ncgs_unit_str + ".open_new_file(" + ncgs_file_str +
              ", self.rti, var_name, long_name, units_name, " +
              "dtype=dtype, " +
              "time_units=time_units)" )
        MAKE_RTS = False
    except:
        print 'ERROR: Unable to open new netCDF file:'
        exec( "print '      '," + ncgs_file_str )
        print ' '
        print 'Will write grid stack in generic RTS format.'
        print ' '
        MAKE_RTS = True

    #-------------------------------------------
    # Always save grid stacks in an RTS file ?
    #--------------------------------------------------
    # Note that if PyNIO is missing, we don't get
    # an exception above and MAKE_RTS is still False.
    #--------------------------------------------------
    MAKE_RTS = True   #####
##    print '#################################'
##    print 'MAKE_RTS =', MAKE_RTS
##    print '#################################'
    
    #---------------------------------
    # Build strings to be used below 
    #---------------------------------
    rts_unit_str = "self." + var_name + "_rts_unit"
    rts_file_str = "self." + var_name + "_rts_file"
    gs_file_str  = "self." + var_name + "_gs_file"

    #------------------------------------------
    # Open new RTS files to write grid stacks
    #------------------------------------------
    if (MAKE_RTS):
        try:
            exec( rts_file_str + "= file_utils.replace_extension(" +
                  gs_file_str + ", '.rts')" )
            exec( rts_unit_str + " = rts_files.rts_file()" )
            exec( rts_unit_str + ".open_new_file(" + rts_file_str +
                  ", self.rti, var_name, " +
                  "dtype=dtype, " +
                  "MAKE_BOV=True)" )
        except:
            print 'ERROR: Unable to open new RTS file:'
            exec( "print '      '," + rts_file_str )
            # exec( "print '      ', self." + var_name + "_rts_file" )
            print ' '
Example #4
0
def unit_test(nx=4, ny=5, VERBOSE=False,
              file_name="TEST_FILE.rtg"):

    print 'Running unit_test()...'

    #------------------------------------
    # Make instance of rtg_file() class
    #------------------------------------
    rtg = rtg_file()  
    dx = 100
    dy = 100

    #---------------------------------
    # These are unused for RTG files
    #---------------------------------
##    grid_name  = "depth"
##    long_name  = "depth of water"
##    units_name = "meters"

    info = rti_files.make_info( file_name, nx, ny, dx, dy )
    OK   = rtg.open_new_file( file_name, info, ADD_INDEX=True )

    #----------------------------------------------
    # This also works if RTI file already exists.
    #----------------------------------------------
    # OK = rtg.open_new_file( file_name )
    
    if not(OK):
        print 'ERROR during open_new_file().'
        return
    
    grid = numpy.arange(nx * ny, dtype='Float32')
    grid = grid.reshape( (ny, nx) )
    ### print 'AT START: (nx, ny) =', nx, ny
    
    #---------------------------
    # Write a grid to the file
    #---------------------------
    rtg.write_grid( grid, VERBOSE=True )                             

    #-------------------------------------
    # Re-open the file and read the grid
    #-----------------------------------------------------------
    # The file_name of the file we created is not "file_name",
    # but now includes a time index.  It was saved in rtg, so
    # call rtg.open_file() with no argument to use that name.
    #-----------------------------------------------------------    
    OK = rtg.open_file()
    ## print 'NOW: (nx, ny) =', rtg.nx, rtg.ny
    
    if not(OK): return
    print 'rtg.byte_swap_needed() =', rtg.byte_swap_needed()
    print ' '
    grid = rtg.read_grid( VERBOSE=True )
    print 'grid = '
    print grid  

    #-------------------------------------------
    # Write another grid, with next time index
    #-------------------------------------------
    OK = rtg.open_new_file( file_name, info, ADD_INDEX=True)
    rtg.write_grid( grid + 1, VERBOSE=True)
    def save_matlab_dem_as_rtg(self, prefix='Animas_200'):

        import scipy.io
        import rti_files

        print 'Saving MatLab file DEM to RTG file...'
        mat_file = (prefix + '.mat')
        rtg_file = (prefix + '_DEM.rtg')

        vars = scipy.io.loadmat(mat_file)
        cellsize = np.float64(vars['cellsize'])
        easting = np.float64(vars['easting'])
        northing = np.float64(vars['northing'])
        topo = np.float64(vars['topo'])
        ny, nx = topo.shape
        dx = cellsize
        dy = cellsize
        print '   (nx, ny) =', nx, ny
        print '   (dx, dy) =', dx, dy
        file_unit = open(rtg_file, 'wb')
        topo = np.float32(topo)
        data_type = 'FLOAT'
        ##        topo = np.float64(topo)
        ##        data_type = 'DOUBLE'
        topo.tofile(file_unit)
        file_unit.close()

        ##        class info:
        ##            grid_file    = rtg_file
        ##            data_source  = 'Converted from Animas_200.mat, M. Kessler'
        ##            ncols        = nx
        ##            nrows        = ny
        ##            data_type    = 'FLOAT'
        ##            byte_order   = 'LSB'
        ##            pixel_geom   = 1       # (ASSUMES fixed-length, e.g. UTM)
        ##            xres         = dx      # [meters]
        ##            yres         = dy      # [meters]
        ##            zres         = 1.0     #######################  CHECK THIS
        ##            z_units      = 'METERS'
        ##            y_south_edge = northing.min()
        ##            y_north_edge = y_south_edge + (ny * dy)
        ##            x_west_edge  = easting.min()
        ##            x_east_edge  = x_west_edge  + (nx * dx)
        ##            box_units    = 'METERS'
        ##            gmin         = topo.min()
        ##            gmax         = topo.max()
        ##            UTM_zone     = 'UNKNOWN'

        #------------------------------------
        # Create an RTI file with grid info
        #------------------------------------
        rti_files.make_info(
            rtg_file,
            ncols=nx,
            nrows=ny,
            xres=dx,
            yres=dy,
            data_source='Converted from Animas_200.mat, M. Kessler',
            y_south_edge=northing.min(),
            x_west_edge=easting.min(),
            gmin=topo.min(),
            gmax=topo.max())
        ### box_units='METERS', z_units='METERS')
        RTI_filename = (prefix + '.rti')
        rti_files.write_info(RTI_filename, info)
        print 'Finished.'
        print ' '
def make_fractal_surface(n_levels,
                         H=1.5,
                         RTG_file=None,
                         sigma=float64(1),
                         scale=float64(1),
                         seed=168993,
                         X_WRAP=False,
                         Y_WRAP=False,
                         SILENT=False):

    #---------------------------------------------------------
    # Notes: Can apply SCALE at very end.  A value of about
    #        0.01 should give results similar to Howard's
    #        MATRIX_2D with ERROR=0.02.

    #        H is a fractal exponent of some kind.

    #        Set the X_WRAP or Y_WRAP keywords in order to
    #        impose periodic boundaries on the left & right
    #        or top & bottom, respectively.

    #        If n_levels = 7,  nx = 129
    #        If n_levels = 8,  nx = 257
    #        If n_levels = 9,  nx = 513
    #        If n_levels = 10, nx = 1025
    #        If n_levels = 11, nx = 2049
    #----------------------------------------------------------

    if (n_levels > 11):
        print '********************************************'
        print ' ERROR: Max number of levels is 11,'
        print ' which gives a grid size of 2049 x 2049.'
        print '********************************************'
        print ' '
        return

    if not (SILENT):
        print 'Creating a fractal surface...'

    #------------------
    # Initialize vars
    #------------------
    factor = float64(1) / sqrt(float64(2)**H)  #############
    nx = (int32(2)**n_levels) + 1
    ny = nx
    step = nx - 1
    if not (SILENT):
        print 'nx, ny =', nx, ',', ny
    #----------------------------------------------
    x_vec = numpy.arange(nx, dtype='Int16')
    y_vec = numpy.arange(ny, dtype='Int16')
    cols, rows = numpy.meshgrid(x_vec, y_vec)
    ## rows = reshape(repeat(y_vec, nx), (ny, nx))
    ## cols = rot90(rows)  # (will work if nx=ny)

    sum_grid = (cols + rows)
    #----------------------------------------------
    DONE = zeros([ny, nx], dtype='UInt8')
    DONE[0, 0] = 1
    DONE[0, nx - 1] = 1
    DONE[ny - 1, 0] = 1
    DONE[ny - 1, nx - 1] = 1
    #----------------------------------------------
    EDGE = zeros([ny, nx], dtype='UInt8')
    EDGE[:, 0] = 1
    EDGE[:, nx - 1] = 1
    EDGE[0, :] = 1
    EDGE[ny - 1, :] = 1

    #------------------------------
    # Initialize grid of z-values
    #------------------------------
    numpy.random.seed(seed)
    v = random.normal(loc=0.0, scale=1.0, size=(2, 2))
    z = zeros([ny, nx], dtype='Float64')
    z[0, 0] = v[0, 0]
    z[0, nx - 1] = v[0, 1]
    z[ny - 1, 0] = v[1, 0]
    z[ny - 1, nx - 1] = v[1, 1]
    #------------------------------------
    if (X_WRAP):
        z[0, nx - 1] = z[0, 0]
        z[ny - 1, nx - 1] = z[ny - 1, 0]
    if (Y_WRAP):
        z[ny - 1, 0] = z[0, 0]
        z[ny - 1, nx - 1] = z[0, nx - 1]
    #------------------------------------
    zF = z.flat  ## (an iterator to allow 1D indexing)  ##########

    for k in xrange(1, (n_levels + 1)):

        if not (SILENT):
            print 'Working on level', k

        step = (step / 2)

        #---------------------------------------
        # Get midpoint locations of this level
        #---------------------------------------
        w = where(logical_and(logical_and(logical_and(((cols.flat % step) == 0), \
                                                      ((rows.flat % step) == 0)),
                                          logical_not(DONE.flat)), logical_not(EDGE.flat)))
        n_mid = size(w[0])
        #########################
        #     Need this !!
        #########################
        w = w[0]

        #-----------------------------------------
        # Break these into two groups, w1 and w2
        #-----------------------------------------
        a1 = where((sum_grid.flat[w] % (2 * step)) == 0)  # (1D array)
        n1 = size(a1[0])
        a2 = where((sum_grid.flat[w] % (2 * step)) != 0)  # (1D array)
        n2 = size(a2[0])

        if (n1 != 0):
            w1 = w[a1[0]]
        if (n2 != 0):
            w2 = w[a2[0]]

        #---------------------------------------------
        # Compute midpoint elevations as the average
        # of the diagonal neighbor elevations plus
        # a rescaled Gaussian random variable
        #---------------------------------------------
        UL = w1 - step * (nx + 1)
        UR = w1 - step * (nx - 1)
        LL = w1 + step * (nx - 1)
        LR = w1 + step * (nx + 1)
        #---------------------------
        ### numpy.random.seed(seed)
        ran = factor * sigma * random.normal(loc=0.0, scale=1.0, size=n1)
        zF[w1] = ((zF[UL] + zF[UR] + zF[LL] + zF[LR]) / float64(4)) + ran
        DONE.flat[w1] = 1

        #----------------------------------------------
        # Compute midpoint elevations of remaining
        # pixels at this scale as the average of the
        # nearest neighbor elevations plus a rescaled
        # Gaussian random variable.  n2=0 at start.
        #----------------------------------------------
        if (n2 != 0):
            T = w2 - (step * nx)
            B = w2 + (step * nx)
            R = w2 + step
            L = w2 - step
            #----------------------------
            ### numpy.random.seed(seed)
            ran = factor * sigma * random.normal(loc=0.0, scale=1.0, size=n2)
            zF[w2] = ((zF[T] + zF[B] + zF[L] + zF[R]) / float64(4)) + ran
            DONE.flat[w2] = 1

        #--------------------------------------------
        # Compute elevations of edge pixels at this
        # scale as average of 3 nearest neighbors
        # plus a rescaled Gaussian random variable.
        #--------------------------------------------
        jump = (step * nx)
        #----------------------------
        L = where(logical_and(logical_and((cols.flat == 0), \
                                          ((rows.flat % step) == 0)), \
                              logical_not(DONE.flat)))
        nL = size(L[0])
        T = L - jump
        B = L + jump
        R = L + step
        ### numpy.random.seed(seed)
        ran = factor * sigma * random.normal(loc=0.0, scale=1.0, size=nL)
        zF[L] = ((zF[T] + zF[B] + zF[R]) / float64(3)) + ran
        DONE.flat[L] = 1
        #-----------------------------------------------------------------------------
        R = where(logical_and(logical_and((cols.flat == (nx - 1)), \
                                          ((rows.flat % step) == 0)), \
                              logical_not(DONE.flat)))
        nR = size(R[0])
        if not (X_WRAP):
            L = R - step
            T = R - jump
            B = R + jump
            ### numpy.random.seed(seed)
            ran = factor * sigma * random.normal(loc=0.0, scale=1.0, size=nR)
            zF[R] = ((zF[L] + zF[T] + zF[B]) / float64(3)) + ran
        else:
            zF[R] = zF[L]
        DONE.flat[R] = 1
        #-----------------------------------------------------------------------------
        T = where(logical_and(logical_and((rows.flat == 0), \
                                          ((cols.flat % step) == 0)), \
                              logical_not(DONE.flat)))
        nT = size(T[0])
        L = T - step
        R = T + step
        B = T + jump
        ### numpy.random.seed(seed)
        ran = factor * sigma * random.normal(loc=0.0, scale=1.0, size=nT)
        zF[T] = ((zF[L] + zF[R] + zF[B]) / float64(3)) + ran
        DONE.flat[T] = 1
        #-----------------------------------------------------------------------------
        B  = where(logical_and(logical_and((rows.flat == (ny - 1)), \
                                           ((cols.flat % step) == 0)), \
                               logical_not(DONE.flat)))
        nB = size(B[0])
        if not (Y_WRAP):
            L = B - step
            R = B + step
            T = B - jump
            ### numpy.random.seed(seed)
            ran = factor * sigma * random.normal(loc=0.0, scale=1.0, size=nB)
            zF[B] = ((zF[L] + zF[R] + zF[T]) / float64(3)) + ran
        else:
            zF[B] = zF[T]
        DONE.flat[B] = 1
        #-----------------------------------------------------------------------------

    #-----------------------
    # Rescale the values ?
    #-----------------------
    if (scale != 1.0):
        z = (z * scale)

    #-------------------------------------------
    # Option to save to RTG file with RTI file
    #-------------------------------------------
    if (RTG_file is not None):
        ###############################
        # CHECK FOR OVERWRITE HERE !
        ###############################
        ###############################
        info = rti_files.make_info(RTG_file,
                                   nx,
                                   ny,
                                   xres=100.0,
                                   yres=100.0,
                                   gmin=z.min(),
                                   gmax=z.max(),
                                   data_source="Midpoint displacement method")
        rti_files.write_info(RTG_file, info)
        RTG_type = 'FLOAT'
        ## RTG_type = 'DOUBLE'
        rtg_files.write_grid(z, RTG_file, info, RTG_type=RTG_type)

    #----------------------
    # Print final message
    #----------------------
    if not (SILENT):
        print 'Finished.'
        print ' '

    return z
def open_new_gs_file(self,
                     file_name,
                     info=None,
                     var_name='X',
                     long_name='Unknown',
                     units_name='None',
                     dtype='float32',
                     time_units='minutes',
                     nx=None,
                     ny=None,
                     dx=None,
                     dy=None):

    #---------------------------
    # Was grid info provided ?
    #---------------------------
    if (info is not None):
        info.file_name = file_name
        info.data_type = rti_files.get_rti_data_type(dtype)
        if (nx is not None): info.ncols = nx
        if (ny is not None): info.nrows = ny
        if (dx is not None): info.xres = dx
        if (dy is not None): info.yres = dy
    else:
        if (nx is not None) and (ny is not None) and \
           (dx is not None) and (dy is not None):
            info = rti_files.make_info(file_name, nx, ny, dx, dy)
        else:
            print 'ERROR during open_new_gs_file().'
            print '      Grid info not provided.'
            print ' '
            ## return -1

    #---------------------------------------------
    # Get long_name and units_name from var_name
    #---------------------------------------------
#     long_name  = self.get_var_long_name( var_name )
#     units_name = self.get_var_units( var_name )
#     dtype      = self.get_var_type( var_name )

#---------------------------------
# Build strings to be used below
#---------------------------------
    ncgs_unit_str = "self." + var_name + "_ncgs_unit"
    ncgs_file_str = "self." + var_name + "_ncgs_file"
    gs_file_str = "self." + var_name + "_gs_file"
    #-------------------
    # For testing only
    #-------------------
    # print 'ncgs_unit_str =', ncgs_unit_str
    # print 'ncgs_file_str =', ncgs_file_str
    # print 'gs_file_str   =', gs_file_str
    # print 'var_name      =', var_name
    # print 'long_name     =', long_name
    # print 'units_name    =', units_name
    # print 'time_units    =', time_units
    # print ' '

    #--------------------------------------------
    # Open new netCDF file to write grid stacks
    # using var_name to build variable names
    #--------------------------------------------
    # Note that "dtype" was not included in the
    # last TopoFlow version (always float32),
    # but Erode needs other types.
    #--------------------------------------------
    try:
        #         gs_filename = getattr( self, (var_name + '_gs_file') )
        #         setattr( self, (var_name + '_ncgs_file'),
        #                  file_utils.replace_extension( gs_filename, '.nc') )
        #         setattr( self, (var_name + '_ncgs_unit'), ncgs_files.ncgs_file() )
        #         setattr( self, (var_name + '_ncgs_unit'),
        #
        #         exec( ncgs_unit_str + ".open_new_file(" + ncgs_file_str +
        #               ", self.rti, var_name, long_name, units_name, " +
        #               "dtype=dtype, " +
        #               "time_units=time_units)" )
        #--------------------------------------------------------------
        exec(ncgs_file_str + "= file_utils.replace_extension(" + gs_file_str +
             ", '.nc')")
        exec(ncgs_unit_str + "=" + "ncgs_files.ncgs_file()")
        exec(ncgs_unit_str + ".open_new_file(" + ncgs_file_str +
             ", self.rti, var_name, long_name, units_name, " +
             "dtype=dtype, " + "time_units=time_units)")
        MAKE_RTS = False
    except:
        print 'ERROR: Unable to open new netCDF file:'
        exec("print '      '," + ncgs_file_str)
        print ' '
        print 'Will write grid stack in generic RTS format.'
        print ' '
        MAKE_RTS = True

    #-------------------------------------------
    # Always save grid stacks in an RTS file ?
    #--------------------------------------------------
    # Note that if PyNIO is missing, we don't get
    # an exception above and MAKE_RTS is still False.
    #--------------------------------------------------
    MAKE_RTS = True  #####
    ##    print '#################################'
    ##    print 'MAKE_RTS =', MAKE_RTS
    ##    print '#################################'

    #---------------------------------
    # Build strings to be used below
    #---------------------------------
    rts_unit_str = "self." + var_name + "_rts_unit"
    rts_file_str = "self." + var_name + "_rts_file"
    gs_file_str = "self." + var_name + "_gs_file"

    #------------------------------------------
    # Open new RTS files to write grid stacks
    #------------------------------------------
    if (MAKE_RTS):
        try:
            exec(rts_file_str + "= file_utils.replace_extension(" +
                 gs_file_str + ", '.rts')")
            exec(rts_unit_str + " = rts_files.rts_file()")
            exec(rts_unit_str + ".open_new_file(" + rts_file_str +
                 ", self.rti, var_name, " + "dtype=dtype, " + "MAKE_BOV=True)")
        except:
            print 'ERROR: Unable to open new RTS file:'
            exec("print '      '," + rts_file_str)
            # exec( "print '      ', self." + var_name + "_rts_file" )
            print ' '
Example #8
0
def unit_test(nx=4, ny=5, VERBOSE=False, file_name="TEST_FILE.rtg"):

    print 'Running unit_test()...'

    #------------------------------------
    # Make instance of rtg_file() class
    #------------------------------------
    rtg = rtg_file()
    dx = 100
    dy = 100

    #---------------------------------
    # These are unused for RTG files
    #---------------------------------
    ##    grid_name  = "depth"
    ##    long_name  = "depth of water"
    ##    units_name = "meters"

    info = rti_files.make_info(file_name, nx, ny, dx, dy)
    OK = rtg.open_new_file(file_name, info, ADD_INDEX=True)

    #----------------------------------------------
    # This also works if RTI file already exists.
    #----------------------------------------------
    # OK = rtg.open_new_file( file_name )

    if not (OK):
        print 'ERROR during open_new_file().'
        return

    grid = numpy.arange(nx * ny, dtype='Float32')
    grid = grid.reshape((ny, nx))
    ### print 'AT START: (nx, ny) =', nx, ny

    #---------------------------
    # Write a grid to the file
    #---------------------------
    rtg.write_grid(grid, VERBOSE=True)

    #-------------------------------------
    # Re-open the file and read the grid
    #-----------------------------------------------------------
    # The file_name of the file we created is not "file_name",
    # but now includes a time index.  It was saved in rtg, so
    # call rtg.open_file() with no argument to use that name.
    #-----------------------------------------------------------
    OK = rtg.open_file()
    ## print 'NOW: (nx, ny) =', rtg.nx, rtg.ny

    if not (OK): return
    print 'rtg.byte_swap_needed() =', rtg.byte_swap_needed()
    print ' '
    grid = rtg.read_grid(VERBOSE=True)
    print 'grid = '
    print grid

    #-------------------------------------------
    # Write another grid, with next time index
    #-------------------------------------------
    OK = rtg.open_new_file(file_name, info, ADD_INDEX=True)
    rtg.write_grid(grid + 1, VERBOSE=True)
def unit_test(nx=4,
              ny=5,
              n_grids=6,
              VERBOSE=False,
              file_name="NCGS_Grid_Test.nc"):

    print 'Running unit_test()...'

    #-------------------------------------
    # Make instance of ncgs_file() class
    #-------------------------------------
    ncgs = ncgs_file()
    dx = 100
    dy = 100
    var_name = "depth"

    info = rti_files.make_info(file_name, nx, ny, dx, dy)
    OK = ncgs.open_new_file(file_name,
                            info,
                            dtype='float32',
                            var_name=var_name,
                            long_name="depth of water",
                            units_name="meters",
                            comment="Created by TopoFlow 3.0.")
    if not (OK):
        print 'ERROR during open_new_file().'
        return

    grid = np.arange(nx * ny, dtype='Float32')
    grid = grid.reshape((ny, nx))

    #-----------------------------------------------
    # (6/10/10) Can use this to test new ability
    # of add_grid() to convert from scalar to grid
    #-----------------------------------------------
    # grid = np.float32(0)

    #----------------------------------
    # Add some test grids to the file
    #----------------------------------
    print 'Writing grids to NCGS file...'
    for time_index in xrange(n_grids):
        ncgs.add_grid(grid, var_name)
        ## ncgs.add_grid( grid, var_name, time_index )
        grid = (grid + 1)

    if (VERBOSE):
        print self.ncgs_unit  # (print a summary)

    ncgs.close_file()
    print 'Finished writing NCGS file: ' + file_name
    print ' '

    #---------------------------------------------
    # Re-open the file and read grids one-by-one
    #---------------------------------------------
    OK = ncgs.open_file(file_name)
    if not (OK): return
    print 'Reading grids from NCGS file: '

    for time_index in xrange(n_grids):
        grid = ncgs.get_grid(var_name, time_index)
        print 'grid[' + str(time_index) + '] = '
        print grid
        print '-----------------------------------------------'
    ncgs.close_file()
    print 'Finished reading NCGS file: ' + file_name
    print ' '
Example #10
0
def make_fractal_surface(n_levels, H=1.5, RTG_file=None,
                         sigma=float64(1),
                         scale=float64(1),
                         seed=168993,
                         X_WRAP=False, Y_WRAP=False,
                         SILENT=False):

    #---------------------------------------------------------
    # Notes: Can apply SCALE at very end.  A value of about
    #        0.01 should give results similar to Howard's
    #        MATRIX_2D with ERROR=0.02.

    #        H is a fractal exponent of some kind.
    
    #        Set the X_WRAP or Y_WRAP keywords in order to
    #        impose periodic boundaries on the left & right
    #        or top & bottom, respectively.

    #        If n_levels = 7,  nx = 129
    #        If n_levels = 8,  nx = 257
    #        If n_levels = 9,  nx = 513
    #        If n_levels = 10, nx = 1025
    #        If n_levels = 11, nx = 2049
    #----------------------------------------------------------

    if (n_levels > 11):    
        print '********************************************'
        print ' ERROR: Max number of levels is 11,'
        print ' which gives a grid size of 2049 x 2049.'
        print '********************************************'
        print ' '
        return
    
    if not(SILENT):
        print 'Creating a fractal surface...'
    
    #------------------
    # Initialize vars
    #------------------
    factor = float64(1) / sqrt(float64(2) ** H)    #############
    nx     = (int32(2) ** n_levels) + 1
    ny     = nx
    step   = nx - 1
    if not(SILENT):
        print 'nx, ny =', nx, ',', ny
    #----------------------------------------------
    x_vec      = numpy.arange(nx, dtype='Int16')
    y_vec      = numpy.arange(ny, dtype='Int16')
    cols, rows = numpy.meshgrid( x_vec, y_vec )
    ## rows = reshape(repeat(y_vec, nx), (ny, nx))
    ## cols = rot90(rows)  # (will work if nx=ny)
    
    sum_grid = (cols + rows)
    #----------------------------------------------
    DONE = zeros([ny, nx], dtype='UInt8')
    DONE[0,0]           = 1
    DONE[0,nx - 1]      = 1
    DONE[ny - 1,0]      = 1
    DONE[ny - 1,nx - 1] = 1
    #----------------------------------------------
    EDGE = zeros([ny, nx], dtype='UInt8')
    EDGE[:,0]      = 1
    EDGE[:,nx - 1] = 1
    EDGE[0,:]      = 1
    EDGE[ny - 1,:] = 1
    
    #------------------------------
    # Initialize grid of z-values
    #------------------------------
    numpy.random.seed(seed)
    v = random.normal(loc=0.0, scale=1.0, size=(2, 2))
    z = zeros([ny, nx], dtype='Float64')
    z[0,0]           = v[0,0]
    z[0,nx - 1]      = v[0,1]
    z[ny - 1,0]      = v[1,0]
    z[ny - 1,nx - 1] = v[1,1]
    #------------------------------------
    if (X_WRAP):    
        z[0,nx - 1]      = z[0,0]
        z[ny - 1,nx - 1] = z[ny - 1,0]
    if (Y_WRAP):    
        z[ny - 1,0]      = z[0,0]
        z[ny - 1,nx - 1] = z[0,nx - 1]
    #------------------------------------
    zF = z.flat    ## (an iterator to allow 1D indexing)  ##########
    
    for k in xrange( 1, (n_levels + 1) ):

        if not(SILENT):
            print 'Working on level', k
            
        step = (step / 2)
        
        #---------------------------------------
        # Get midpoint locations of this level
        #---------------------------------------
        w = where(logical_and(logical_and(logical_and(((cols.flat % step) == 0), \
                                                      ((rows.flat % step) == 0)),
                                          logical_not(DONE.flat)), logical_not(EDGE.flat)))
        n_mid = size(w[0])
        #########################
        #     Need this !!
        #########################
        w = w[0]
        
        #-----------------------------------------
        # Break these into two groups, w1 and w2
        #-----------------------------------------
        a1 = where((sum_grid.flat[w] % (2 * step)) == 0)  # (1D array)
        n1 = size(a1[0])
        a2 = where((sum_grid.flat[w] % (2 * step)) != 0)  # (1D array)
        n2 = size(a2[0])
    
        if (n1 != 0):
            w1 = w[a1[0]]
        if (n2 != 0):    
            w2 = w[a2[0]]

        #---------------------------------------------
        # Compute midpoint elevations as the average
        # of the diagonal neighbor elevations plus
        # a rescaled Gaussian random variable
        #---------------------------------------------
        UL = w1 - step * (nx + 1)
        UR = w1 - step * (nx - 1)
        LL = w1 + step * (nx - 1)
        LR = w1 + step * (nx + 1)
        #---------------------------
        ### numpy.random.seed(seed)
        ran = factor * sigma * random.normal(loc=0.0, scale=1.0, size=n1)
        zF[w1] = ((zF[UL] + zF[UR] + zF[LL] + zF[LR]) / float64(4)) + ran
        DONE.flat[w1] = 1
        
        #----------------------------------------------
        # Compute midpoint elevations of remaining
        # pixels at this scale as the average of the
        # nearest neighbor elevations plus a rescaled
        # Gaussian random variable.  n2=0 at start.
        #----------------------------------------------
        if (n2 != 0):    
            T = w2 - (step * nx)
            B = w2 + (step * nx)
            R = w2 + step
            L = w2 - step
            #----------------------------
            ### numpy.random.seed(seed)
            ran = factor * sigma * random.normal(loc=0.0, scale=1.0, size=n2)
            zF[w2] = ((zF[T] + zF[B] + zF[L] + zF[R]) / float64(4)) + ran
            DONE.flat[w2] = 1
        
        #--------------------------------------------
        # Compute elevations of edge pixels at this
        # scale as average of 3 nearest neighbors
        # plus a rescaled Gaussian random variable.
        #--------------------------------------------
        jump = (step * nx)
        #----------------------------
        L = where(logical_and(logical_and((cols.flat == 0), \
                                          ((rows.flat % step) == 0)), \
                              logical_not(DONE.flat)))
        nL = size(L[0])
        T  = L - jump
        B  = L + jump
        R  = L + step
        ### numpy.random.seed(seed)
        ran   = factor * sigma * random.normal(loc=0.0, scale=1.0, size=nL)
        zF[L] = ((zF[T] + zF[B] + zF[R]) / float64(3)) + ran
        DONE.flat[L] = 1
        #-----------------------------------------------------------------------------
        R = where(logical_and(logical_and((cols.flat == (nx - 1)), \
                                          ((rows.flat % step) == 0)), \
                              logical_not(DONE.flat)))
        nR = size(R[0])
        if not(X_WRAP):    
            L = R - step
            T = R - jump
            B = R + jump
            ### numpy.random.seed(seed)
            ran = factor * sigma * random.normal(loc=0.0, scale=1.0, size=nR)
            zF[R] = ((zF[L] + zF[T] + zF[B]) / float64(3)) + ran
        else:    
            zF[R] = zF[L]
        DONE.flat[R] = 1
        #-----------------------------------------------------------------------------
        T = where(logical_and(logical_and((rows.flat == 0), \
                                          ((cols.flat % step) == 0)), \
                              logical_not(DONE.flat)))
        nT = size(T[0])
        L  = T - step
        R  = T + step
        B  = T + jump
        ### numpy.random.seed(seed)
        ran = factor * sigma * random.normal(loc=0.0, scale=1.0, size=nT)
        zF[T] = ((zF[L] + zF[R] + zF[B]) / float64(3)) + ran
        DONE.flat[T] = 1
        #-----------------------------------------------------------------------------
        B  = where(logical_and(logical_and((rows.flat == (ny - 1)), \
                                           ((cols.flat % step) == 0)), \
                               logical_not(DONE.flat)))
        nB = size(B[0])
        if not(Y_WRAP):    
            L = B - step
            R = B + step
            T = B - jump
            ### numpy.random.seed(seed)
            ran = factor * sigma * random.normal(loc=0.0, scale=1.0, size=nB)
            zF[B] = ((zF[L] + zF[R] + zF[T]) / float64(3)) + ran
        else:    
            zF[B] = zF[T]
        DONE.flat[B] = 1
        #-----------------------------------------------------------------------------
    
    #-----------------------
    # Rescale the values ?
    #-----------------------
    if (scale != 1.0):   
        z = (z * scale)
    
    #-------------------------------------------
    # Option to save to RTG file with RTI file
    #-------------------------------------------
    if (RTG_file != None):
        ###############################
        # CHECK FOR OVERWRITE HERE !
        ###############################
        ###############################
        info = rti_files.make_info( RTG_file, nx, ny,
                                    xres=100.0, yres=100.0,
                                    gmin=z.min(), gmax=z.max(),
                                    data_source="Midpoint displacement method" )
        rti_files.write_info(RTG_file, info)
        RTG_type = 'FLOAT'
        ## RTG_type = 'DOUBLE'
        rtg_files.write_grid( z, RTG_file, info, RTG_type=RTG_type)

    #----------------------
    # Print final message
    #----------------------
    if not(SILENT):
        print 'Finished.'
        print ' '

    return z
Example #11
0
def unit_test(nx=4, ny=5, n_grids=6, VERBOSE=False, file_name="TEST_FILE.rts"):

    print 'Running unit_test()...'

    #------------------------------------
    # Make instance of rts_file() class
    #------------------------------------
    rts = rts_file()
    dx = 100
    dy = 100

    #---------------------------------
    # These are unused for RTS files
    #---------------------------------
    ##    grid_name  = "depth"
    ##    long_name  = "depth of water"
    ##    units_name = "meters"

    info = rti_files.make_info(file_name, nx, ny, dx, dy)
    OK = rts.open_new_file(file_name, info)

    if not (OK):
        print 'ERROR during open_new_file().'
        return

    grid = numpy.arange(nx * ny, dtype='Float32')
    grid = grid.reshape((ny, nx))

    #----------------------------------
    # Add some test grids to the file
    #----------------------------------
    for time_index in xrange(n_grids):
        rts.add_grid(grid)
        grid = (grid + 1)

    rts.close_file()
    print 'Finished writing file: ' + file_name
    print ' '

    #---------------------------------------------
    # Re-open the file and read grids one-by-one
    #---------------------------------------------
    OK = rts.open_file(file_name)
    if not (OK): return
    n_grids = rts.number_of_grids()
    print 'Reading grids from RTS file: '
    print 'rts.number_of_grids()  =', n_grids
    print 'rts.byte_swap_needed() =', rts.byte_swap_needed()
    print ' '
    for time_index in xrange(n_grids):
        grid = rts.get_grid(time_index)
        print 'grid[' + str(time_index) + '] = '
        print grid
        print '-----------------------------------------------'

    #----------------------------
    # Go back and read 2nd grid
    #----------------------------
    grid = rts.get_grid(1)
    print ' '
    print 'Reading second grid again...'
    print 'Second grid ='
    print grid
    print '-----------------------------------------------'
    rts.close_file()
    print 'Finished reading file: ' + file_name
    print ' '

    #---------------------------------------
    # Re-open the file and change one grid
    #---------------------------------------
    print 'Updating RTS file:', file_name
    grid = numpy.ones((ny, nx), dtype='Float32')
    OK = rts.open_file(file_name, UPDATE=True)
    if not (OK): return
    rts.add_grid(grid, time_index=0)
    rts.close_file()
    print 'Finished updating RTS file.'
    print ' '

    #---------------------------------------------
    # Re-open the file and read grids one-by-one
    #---------------------------------------------
    OK = rts.open_file(file_name)
    if not (OK): return
    n_grids = rts.number_of_grids()
    print 'Reading grids from RTS file: '
    print 'rts.number_of_grids()  =', n_grids
    print 'rts.byte_swap_needed() =', rts.byte_swap_needed()
    print ' '
    for time_index in xrange(n_grids):
        grid = rts.get_grid(time_index)
        print 'grid[' + str(time_index) + '] = '
        print grid
        print '-----------------------------------------------'
    rts.close_file()
    print 'Finished reading file: ' + file_name
    print ' '
Example #12
0
def unit_test(nx=4, ny=5, n_grids=6, VERBOSE=False,
              file_name="TEST_FILE.rts"):

    print 'Running unit_test()...'

    #------------------------------------
    # Make instance of rts_file() class
    #------------------------------------
    rts = rts_file()  
    dx = 100
    dy = 100

    #---------------------------------
    # These are unused for RTS files
    #---------------------------------
##    grid_name  = "depth"
##    long_name  = "depth of water"
##    units_name = "meters"

    info = rti_files.make_info( file_name, nx, ny, dx, dy )
    OK = rts.open_new_file( file_name, info )

    if not(OK):
        print 'ERROR during open_new_file().'
        return
    
    grid = numpy.arange(nx * ny, dtype='Float32')
    grid = grid.reshape( (ny, nx) )
    
    #----------------------------------
    # Add some test grids to the file
    #----------------------------------
    for time_index in xrange(n_grids):
        rts.add_grid( grid )                         
        grid = (grid + 1)
        
    rts.close_file()
    print 'Finished writing file: ' + file_name
    print ' '

    #---------------------------------------------
    # Re-open the file and read grids one-by-one 
    #---------------------------------------------
    OK = rts.open_file( file_name )
    if not(OK): return
    n_grids = rts.number_of_grids()
    print 'Reading grids from RTS file: '
    print 'rts.number_of_grids()  =', n_grids
    print 'rts.byte_swap_needed() =', rts.byte_swap_needed()
    print ' '
    for time_index in xrange(n_grids):
        grid = rts.get_grid( time_index )
        print 'grid[' + str(time_index) + '] = '
        print grid
        print '-----------------------------------------------'

    #----------------------------
    # Go back and read 2nd grid
    #----------------------------
    grid = rts.get_grid( 1 )
    print ' '
    print 'Reading second grid again...'
    print 'Second grid ='
    print grid
    print '-----------------------------------------------'
    rts.close_file()
    print 'Finished reading file: ' + file_name
    print ' '

    #---------------------------------------
    # Re-open the file and change one grid
    #---------------------------------------
    print 'Updating RTS file:', file_name
    grid = numpy.ones( (ny, nx), dtype='Float32' )
    OK = rts.open_file( file_name, UPDATE=True )
    if not(OK): return
    rts.add_grid( grid, time_index=0 )
    rts.close_file()
    print 'Finished updating RTS file.'
    print ' '
    
    #---------------------------------------------
    # Re-open the file and read grids one-by-one 
    #---------------------------------------------
    OK = rts.open_file( file_name )
    if not(OK): return
    n_grids = rts.number_of_grids()
    print 'Reading grids from RTS file: '
    print 'rts.number_of_grids()  =', n_grids
    print 'rts.byte_swap_needed() =', rts.byte_swap_needed()
    print ' '
    for time_index in xrange(n_grids):
        grid = rts.get_grid( time_index )
        print 'grid[' + str(time_index) + '] = '
        print grid
        print '-----------------------------------------------'
    rts.close_file()
    print 'Finished reading file: ' + file_name
    print ' '    
Example #13
0
    def save_matlab_dem_as_rtg(self, prefix='Animas_200'):

        import scipy.io
        import rti_files
        
        print 'Saving MatLab file DEM to RTG file...'
        mat_file = (prefix + '.mat')
        rtg_file = (prefix + '_DEM.rtg')
        
        vars = scipy.io.loadmat( mat_file )
        cellsize = np.float64(vars['cellsize'])
        easting  = np.float64(vars['easting'])
        northing = np.float64(vars['northing'])
        topo     = np.float64(vars['topo'])
        ny, nx   = topo.shape
        dx       = cellsize
        dy       = cellsize
        print '   (nx, ny) =', nx, ny
        print '   (dx, dy) =', dx, dy
        file_unit = open(rtg_file, 'wb')
        topo = np.float32(topo)
        data_type = 'FLOAT'
##        topo = np.float64(topo)
##        data_type = 'DOUBLE'
        topo.tofile(file_unit)
        file_unit.close()

##        class info:
##            grid_file    = rtg_file
##            data_source  = 'Converted from Animas_200.mat, M. Kessler'
##            ncols        = nx
##            nrows        = ny
##            data_type    = 'FLOAT'
##            byte_order   = 'LSB'
##            pixel_geom   = 1       # (ASSUMES fixed-length, e.g. UTM)
##            xres         = dx      # [meters]
##            yres         = dy      # [meters]
##            zres         = 1.0     #######################  CHECK THIS
##            z_units      = 'METERS'
##            y_south_edge = northing.min()
##            y_north_edge = y_south_edge + (ny * dy)
##            x_west_edge  = easting.min()
##            x_east_edge  = x_west_edge  + (nx * dx)
##            box_units    = 'METERS'
##            gmin         = topo.min()
##            gmax         = topo.max()
##            UTM_zone     = 'UNKNOWN'

        #------------------------------------
        # Create an RTI file with grid info
        #------------------------------------
        rti_files.make_info( rtg_file, ncols=nx, nrows=ny,
                             xres=dx, yres=dy,
                             data_source= 'Converted from Animas_200.mat, M. Kessler',
                             y_south_edge=northing.min(),
                             x_west_edge=easting.min(),
                             gmin=topo.min(), gmax=topo.max())
                             ### box_units='METERS', z_units='METERS')
        RTI_filename = (prefix + '.rti')
        rti_files.write_info( RTI_filename, info )
        print 'Finished.'
        print ' '
Example #14
0
def unit_test(nx=4, ny=5, n_grids=6, VERBOSE=False,
              file_name="NCGS_Grid_Test.nc"):

    print 'Running unit_test()...'

    #-------------------------------------
    # Make instance of ncgs_file() class
    #-------------------------------------
    ncgs = ncgs_file()
    dx = 100
    dy = 100
    var_name = "depth"

    info = rti_files.make_info( file_name, nx, ny, dx, dy )
    OK = ncgs.open_new_file( file_name, info,
                             dtype='float32',
                             var_name=var_name,
                             long_name="depth of water",
                             units_name="meters",
                             comment="Created by TopoFlow 3.0.")
    if not(OK):
        print 'ERROR during open_new_file().'
        return
    
    grid = numpy.arange(nx * ny, dtype='Float32')
    grid = grid.reshape( (ny, nx) )

    #-----------------------------------------------
    # (6/10/10) Can use this to test new ability
    # of add_grid() to convert from scalar to grid
    #-----------------------------------------------
    # grid = numpy.float32(0)
    
    #----------------------------------
    # Add some test grids to the file
    #----------------------------------
    print 'Writing grids to NCGS file...'
    for time_index in xrange(n_grids):
        ncgs.add_grid( grid, var_name )  
        ## ncgs.add_grid( grid, var_name, time_index )                         
        grid = (grid + 1)
        
    if (VERBOSE):
        print self.ncgs_unit  # (print a summary)

    ncgs.close_file()
    print 'Finished writing NCGS file: ' + file_name
    print ' '

    #---------------------------------------------
    # Re-open the file and read grids one-by-one 
    #---------------------------------------------
    OK = ncgs.open_file( file_name )
    if not(OK): return
    print 'Reading grids from NCGS file: '
    
    for time_index in xrange(n_grids):
        grid = ncgs.get_grid(var_name, time_index)
        print 'grid[' + str(time_index) + '] = '
        print grid
        print '-----------------------------------------------'
    ncgs.close_file()    
    print 'Finished reading NCGS file: ' + file_name
    print ' '
def open_new_gs_file(self,
                     file_name,
                     info=None,
                     var_name='X',
                     long_name='Unknown',
                     units_name='None',
                     dtype='float32',
                     time_units='minutes',
                     nx=None,
                     ny=None,
                     dx=None,
                     dy=None):

    #---------------------------
    # Was grid info provided ?
    #---------------------------
    if (info != None):
        info.file_name = file_name
        info.data_type = rti_files.get_rti_data_type(dtype)
        if (nx != None): info.ncols = nx
        if (ny != None): info.nrows = ny
        if (dx != None): info.xres = dx
        if (dy != None): info.yres = dy
    else:
        if (nx != None) and (ny != None) and \
           (dx != None) and (dy != None):
            info = rti_files.make_info(file_name, nx, ny, dx, dy)
        else:
            print 'ERROR during open_new_gs_file().'
            print '      Grid info not provided.'
            print ' '
            ## return -1

    #--------------------------------------------
    # Open new netCDF file to write grid stacks
    # using var_name to build variable names
    #--------------------------------------------
    try:
        ncgs_unit_str = "self." + var_name + "_ncgs_unit"
        ncgs_file_str = "self." + var_name + "_ncgs_file"
        gs_file_str = "self." + var_name + "_gs_file"

        exec(ncgs_file_str + "= file_utils.replace_extension(" + gs_file_str +
             ", '.nc')")
        exec(ncgs_unit_str + "=" + "ncgs_files.ncgs_file()")
        exec(ncgs_unit_str + ".open_new_file(" + ncgs_file_str +
             ", self.rti, var_name, long_name, units_name, " +
             "time_units=time_units)")
        MAKE_RTS = False
    except:
        print 'ERROR: Unable to open new netCDF file:'
        exec("print '      ', self." + var_name + "_ncgs_file")
        print ' '
        print 'Will write grid stack in generic RTS format.'
        print ' '
        MAKE_RTS = True

    #-------------------------------------------
    # Always save grid stacks in an RTS file ?
    #-------------------------------------------
    MAKE_RTS = True  #####

    #------------------------------------------
    # Open new RTS files to write grid stacks
    #------------------------------------------
    if (MAKE_RTS):
        try:
            rts_unit_str = "self." + var_name + "_rts_unit"
            rts_file_str = "self." + var_name + "_rts_file"
            gs_file_str = "self." + var_name + "_gs_file"

            exec(rts_file_str + "= file_utils.replace_extension(" +
                 gs_file_str + ", '.rts')")
            exec(rts_unit_str + " = rts_files.rts_file()")
            exec(rts_unit_str + ".open_new_file(" + rts_file_str +
                 ", self.rti, var_name, MAKE_BOV=True)")
        except:
            print 'ERROR: Unable to open new RTS file:'
            exec("print '      ', self." + var_name + "_rts_file")
            print ' '
Example #16
0
def open_new_gs_file(self, file_name, info=None,
                     var_name='X',
                     long_name='Unknown',
                     units_name='None',
                     dtype='float32',
                     time_units='minutes',
                     nx=None, ny=None, dx=None, dy=None):

    #---------------------------
    # Was grid info provided ?
    #---------------------------
    if (info is not None):
        info.file_name = file_name
        info.data_type = rti_files.get_rti_data_type( dtype )
        if (nx is not None): info.ncols = nx
        if (ny is not None): info.nrows = ny
        if (dx is not None): info.xres  = dx
        if (dy is not None): info.yres  = dy
    else:
        if (nx is not None) and (ny is not None) and \
           (dx is not None) and (dy is not None):
            info = rti_files.make_info( file_name, nx, ny, dx, dy )
        else:
            print 'ERROR during open_new_gs_file().'
            print '      Grid info not provided.'
            print ' '
            ## return -1
        
    #--------------------------------------------
    # Open new netCDF file to write grid stacks
    # using var_name to build variable names
    #--------------------------------------------
    try:
        ncgs_unit_str = "self." + var_name + "_ncgs_unit"
        ncgs_file_str = "self." + var_name + "_ncgs_file"
        gs_file_str   = "self." + var_name + "_gs_file"
            
        exec( ncgs_file_str + "= file_utils.replace_extension(" +
              gs_file_str + ", '.nc')" )
        exec( ncgs_unit_str + "=" + "ncgs_files.ncgs_file()" )
        exec( ncgs_unit_str + ".open_new_file(" + ncgs_file_str +
              ", self.rti, var_name, long_name, units_name, " +
              "time_units=time_units)" )
        MAKE_RTS = False
    except:
        print 'ERROR: Unable to open new netCDF file:'
        exec( "print '      ', self." + var_name + "_ncgs_file" )
        print ' '
        print 'Will write grid stack in generic RTS format.'
        print ' '
        MAKE_RTS = True

    #-------------------------------------------
    # Always save grid stacks in an RTS file ?
    #-------------------------------------------
    MAKE_RTS = True   #####
    
    #------------------------------------------
    # Open new RTS files to write grid stacks
    #------------------------------------------
    if (MAKE_RTS):
        try:
            rts_unit_str = "self." + var_name + "_rts_unit"
            rts_file_str = "self." + var_name + "_rts_file"
            gs_file_str  = "self." + var_name + "_gs_file"
            
            exec( rts_file_str + "= file_utils.replace_extension(" +
                  gs_file_str + ", '.rts')" )
            exec( rts_unit_str + " = rts_files.rts_file()" )
            exec( rts_unit_str + ".open_new_file(" + rts_file_str +
                  ", self.rti, var_name, MAKE_BOV=True)" )
        except:
            print 'ERROR: Unable to open new RTS file:'
            exec( "print '      ', self." + var_name + "_rts_file" )
            print ' '