Exemple #1
0
def rtg2rts(RTG_prefix, new_RTS_file, nx=None, ny=None, dtype='float32'):

    #--------------------------------------------------------------
    # Notes:  This function "bundles" a set of binary grid files
    #         (generic RTG format) with the same file_name prefix
    #         and embedded time index into a single RTS file.
    #--------------------------------------------------------------
    # Note:   The machine byte order on beach is "little" or LSB.
    #         However, the "bin" files that Mark wants to merge
    #         into a single file are apparently "big" or MSB.
    #         In cases like this, change SWAP_BYTES to True.
    #--------------------------------------------------------------
    SWAP_BYTES = True  #################

    #--------------------
    # Open new RTS file
    #--------------------
    RTS_EXISTS = os.path.exists(new_RTS_file)
    if (RTS_EXISTS):
        print 'SORRY, An RTS file with the name'
        print new_RTS_file
        print 'already exists.'
        return

    RTG_list = glob.glob(RTG_prefix + '*')
    RTG_list = numpy.sort(RTG_list)
    print 'Number of RTG files to merge =', len(RTG_list)

    #--------------------------------
    # Try to get info from RTI file
    #--------------------------------
    if (nx is None) and (ny is None):
        RTG_file1 = RTG_list[0]
        RTI_file = rti_files.try_to_find_rti_file(RTG_file1)
        if (RTI_file != 'none'):
            info = rti_files.read_info(RTI_file)
            nx = info.ncols
            ny = info.nrows
        else:
            print 'ERROR: Could not find RTI file and nx and ny not provided.'
            return
    else:
        #--------------------------------------------
        # For case when used as a script under Unix
        #--------------------------------------------
        nx = eval(nx)
        ny = eval(ny)

    RTS_unit = open(new_RTS_file, 'wb')

    #------------------------------
    # Write RTG grids to RTS file
    #------------------------------
    for RTG_file in RTG_list:
        print 'Reading values from:', RTG_file
        RTG_unit = open(RTG_file, 'rb')
        grid = numpy.fromfile(RTG_unit, count=nx * ny, dtype=dtype)
        if (SWAP_BYTES): grid.byteswap()
        RTG_unit.close()
        grid.tofile(RTS_unit)

    #---------------------
    # Close new RTS file
    #---------------------
    RTS_unit.close()
    print 'Finished writing RTG files to RTS format.'
    print ' '
Exemple #2
0
    def check_and_store_info(self, file_name, info=None,
                             var_name='UNKNOWN',
                             dtype='float32',
                             MAKE_RTI=True, MAKE_BOV=False):

        #-----------------------------------------------------
        # Note: This object (self) may be new or it may have
        #       been used previously.  In the latter case,
        #       "info" should still be available in "self".
        #       We only need info if MAKE_RTI or MAKE_BOV.
        #-----------------------------------------------------
        self.format = 'RTG'
        self.file_name = file_name
        if not(MAKE_RTI or MAKE_BOV): return

        #---------------------------------
        # Was "info" argument provided ?
        #---------------------------------
        NEW_INFO = True
        if (info == None):
            try:
                info = self.info
                NEW_INFO = False
                ## print 'Found info in state.'
            except:
                #------------------------------------------
                # Try to find RTI file to copy info from.
                # Don't create a new RTI file.
                #------------------------------------------
                RTI_file = rti_files.try_to_find_rti_file( file_name )
                if (RTI_file != 'none'):
                    info = rti_files.read_info( RTI_file )
                    ## print 'Reading info from: ' + RTI_file
                else:
                    print 'ERROR during open_new_file():'
                    print '   Could not find RTI file and "info"'
                    print '   argument was not provided.'
                    print ' '
                    return

        #-----------------------------
        # Update "info" as necessary
        #-----------------------------
        info.grid_file   = file_name
        info.data_type   = rti_files.get_rti_data_type( dtype )
        info.data_source = 'TopoFlow 3.0'
        info.gmin        = -9999.0
        info.gmax        = -9999.0
        
        #---------------------------------------
        # If new "info" was provided, store it
        #---------------------------------------
        if (NEW_INFO):
            self.info = info
            self.nx   = info.ncols
            self.ny   = info.nrows           
            ## print 'Stored new info in state.'
            
##        if (info != None):
##            #------------------------------
##            # Save info to a new RTI file
##            #------------------------------
##            prefix   = rti_files.get_file_prefix( file_name )
##            RTI_file = (prefix + '.rti')
##            rti_files.write_info( RTI_file, info )          
##
##        else:
##            #------------------------------------------
##            # Try to find RTI file to copy info from.
##            # Don't create a new RTI file.
##            #------------------------------------------
##            RTI_file = rti_files.try_to_find_rti_file( file_name )
##            if (RTI_file != 'none'):
##                info = rti_files.read_info( RTI_file )
##                info.file_name = file_name
##                info.data_type = rti_files.get_rti_data_type( dtype )
##            else:
##                print 'ERROR during open_new_file():'
##                print '   Could not find RTI file and "info"'
##                print '   argument was not provided.'
##                print ' '
##                return

        #-------------------
        # Write RTI file ?
        #-------------------
        if (MAKE_RTI):
            prefix   = rti_files.get_file_prefix( file_name )
            RTI_file = (prefix + '.rti')
            rti_files.write_info( RTI_file, info )
            # print 'Wrote grid info to: ' + RTI_file   ######
            
        #-------------------
        # Write BOV file ?
        #-------------------
        if (MAKE_BOV):
            bov_files.write_info_as_bov( file_name, info, var_name)
Exemple #3
0
def RTG_to_RTS(RTG_prefix, new_RTS_file, nx=None, ny=None, dtype='float32'):

    #--------------------------------------------------------------
    # Notes:  This function "bundles" a set of binary grid files
    #         (generic RTG format) with the same file_name prefix
    #         and embedded time index into a single RTS file.
    #--------------------------------------------------------------
    import glob
    import os.path
    import numpy
    import rti_files

    #--------------------
    # Open new RTS file
    #--------------------
    RTS_EXISTS = os.path.exists(new_RTS_file)
    if (RTS_EXISTS):
        print 'SORRY, An RTS file with the name'
        print new_RTS_file
        print 'already exists.'
        return

    RTG_list = glob.glob(RTG_prefix + '*')
    RTG_list = np.sort(RTG_list)
    print 'Number of RTG files to merge =', len(RTG_list)

    #--------------------------------
    # Try to get info from RTI file
    #--------------------------------
    if (nx is None) and (ny is None):
        RTG_file1 = RTG_list[0]
        RTI_file = rti_files.try_to_find_rti_file(RTG_file1)
        if (RTI_file != 'none'):
            info = rti_files.read_info(RTI_file)
            nx = info.ncols
            ny = info.nrows
        else:
            print 'ERROR: Could not find RTI file and nx and ny not provided.'
            return
##    else:
##        #--------------------------------------------
##        # For case when used as a script under Unix
##        # (used in the file rtg2rts.py)
##        #--------------------------------------------
##        nx = eval(nx)
##        ny = eval(ny)

    RTS_unit = open(new_RTS_file, 'wb')

    #------------------------------
    # Write RTG grids to RTS file
    #------------------------------
    for RTG_file in RTG_list:
        print 'Reading values from:', RTG_file
        RTG_unit = open(RTG_file, 'rb')
        grid = np.fromfile(RTG_unit, count=nx * ny, dtype=dtype)
        RTG_unit.close()
        grid.tofile(RTS_unit)

    #---------------------
    # Close new RTS file
    #---------------------
    RTS_unit.close()
    print 'Finished writing RTG files to RTS format.'
    print ' '
def RTG_to_RTS( RTG_prefix, new_RTS_file,
                nx=None, ny=None, dtype='float32' ):

    #--------------------------------------------------------------
    # Notes:  This function "bundles" a set of binary grid files
    #         (generic RTG format) with the same file_name prefix
    #         and embedded time index into a single RTS file.
    #--------------------------------------------------------------
    import glob
    import os.path
    import numpy
    import rti_files

    #--------------------
    # Open new RTS file
    #--------------------
    RTS_EXISTS = os.path.exists( new_RTS_file )
    if (RTS_EXISTS):
        print 'SORRY, An RTS file with the name'
        print   new_RTS_file
        print 'already exists.'
        return

    RTG_list = glob.glob( RTG_prefix + '*' )
    RTG_list = np.sort( RTG_list )
    print 'Number of RTG files to merge =', len(RTG_list)
    
    #--------------------------------
    # Try to get info from RTI file
    #--------------------------------
    if (nx == None) and (ny == None):
        RTG_file1 = RTG_list[0]
        RTI_file  = rti_files.try_to_find_rti_file( RTG_file1 )
        if (RTI_file != 'none'):
            info = rti_files.read_info( RTI_file )
            nx   = info.ncols
            ny   = info.nrows
        else:
            print 'ERROR: Could not find RTI file and nx and ny not provided.'
            return
##    else:
##        #--------------------------------------------
##        # For case when used as a script under Unix
##        # (used in the file rtg2rts.py)
##        #--------------------------------------------
##        nx = eval(nx)
##        ny = eval(ny)
        
    RTS_unit = open( new_RTS_file, 'wb' )

    #------------------------------
    # Write RTG grids to RTS file
    #------------------------------    
    for RTG_file in RTG_list:
        print 'Reading values from:', RTG_file
        RTG_unit = open( RTG_file, 'rb')
        grid     = np.fromfile( RTG_unit, count=nx*ny, dtype=dtype )
        RTG_unit.close()
        grid.tofile( RTS_unit )

    #---------------------
    # Close new RTS file
    #---------------------
    RTS_unit.close()
    print 'Finished writing RTG files to RTS format.'
    print ' '
    def check_and_store_info(self,
                             file_name,
                             info=None,
                             grid_name='UNKNOWN',
                             dtype='float32',
                             MAKE_RTI=True,
                             MAKE_BOV=False):

        #-----------------------------------------------------
        # Note: This object (self) may be new or it may have
        #       been used previously.  In the latter case,
        #       "info" should still be available in "self".
        #       We only need info if MAKE_RTI or MAKE_BOV.
        #-----------------------------------------------------
        self.format = 'NCGS'
        self.file_name = file_name
        self.time_index = 0
        self.grid_name = grid_name

        #-----------------------------------------------------
        # This was used by rts_files.check_and_store_info()
        # but is not appropriate here because we need to
        # know nx, ny, dx and dy for the netCDF file.
        #-----------------------------------------------------
        ### if not(MAKE_RTI or MAKE_BOV): return

        #---------------------------------
        # Was "info" argument provided ?
        #---------------------------------
        NEW_INFO = True
        if (info is None):
            try:
                info = self.info
                self.nx = info.ncols  ###
                self.ny = info.nrows
                NEW_INFO = False
                ## print 'Found info in state.'
            except:
                #------------------------------------------
                # Try to find RTI file to copy info from.
                # Don't create a new RTI file.
                #------------------------------------------
                RTI_file = rti_files.try_to_find_rti_file(file_name)
                if (RTI_file != 'none'):
                    print 'Reading info from: ' + RTI_file
                    info = rti_files.read_info(RTI_file)
                else:
                    print 'ERROR during open_new_file():'
                    print '   Could not find RTI file and "info"'
                    print '   argument was not provided.'
                    print ' '
                    return

        #-----------------------------
        # Update "info" as necessary
        #-----------------------------
        info.grid_file = file_name
        info.data_type = rti_files.get_rti_data_type(dtype)
        info.data_source = 'TopoFlow 3.0'
        info.gmin = -9999.0
        info.gmax = -9999.0

        #---------------------------------------
        # If new "info" was provided, store it
        #---------------------------------------
        if (NEW_INFO):
            self.info = info
            self.nx = info.ncols
            self.ny = info.nrows
            ## print 'Stored new info in state.'

        #-------------------
        # Write RTI file ?
        #-------------------
        if (MAKE_RTI):
            prefix = rti_files.get_file_prefix(file_name)
            RTI_file = (prefix + '.rti')
            rti_files.write_info(RTI_file, info)
            # print 'Wrote grid info to: ' + RTI_file   ######

        #-------------------
        # Write BOV file ?
        #-------------------
        if (MAKE_BOV):
            bov_files.write_info_as_bov(file_name, info, grid_name)
Exemple #6
0
def rtg2rts( RTG_prefix, new_RTS_file,
             nx=None, ny=None, dtype='float32' ):
     
    #--------------------------------------------------------------
    # Notes:  This function "bundles" a set of binary grid files
    #         (generic RTG format) with the same file_name prefix
    #         and embedded time index into a single RTS file.
    #--------------------------------------------------------------
    # Note:   The machine byte order on beach is "little" or LSB.
    #         However, the "bin" files that Mark wants to merge
    #         into a single file are apparently "big" or MSB.
    #         In cases like this, change SWAP_BYTES to True.
    #--------------------------------------------------------------    
    SWAP_BYTES = True   #################
    
    #--------------------
    # Open new RTS file
    #--------------------
    RTS_EXISTS = os.path.exists( new_RTS_file )
    if (RTS_EXISTS):
        print 'SORRY, An RTS file with the name'
        print   new_RTS_file
        print 'already exists.'
        return

    RTG_list = glob.glob( RTG_prefix + '*' )
    RTG_list = numpy.sort( RTG_list )
    print 'Number of RTG files to merge =', len(RTG_list)
    
    #--------------------------------
    # Try to get info from RTI file
    #--------------------------------
    if (nx == None) and (ny == None):
        RTG_file1 = RTG_list[0]
        RTI_file  = rti_files.try_to_find_rti_file( RTG_file1 )
        if (RTI_file != 'none'):
            info = rti_files.read_info( RTI_file )
            nx   = info.ncols
            ny   = info.nrows
        else:
            print 'ERROR: Could not find RTI file and nx and ny not provided.'
            return
    else:
        #--------------------------------------------
        # For case when used as a script under Unix
        #--------------------------------------------
        nx = eval(nx)
        ny = eval(ny)
        
    RTS_unit = open( new_RTS_file, 'wb' )

    #------------------------------
    # Write RTG grids to RTS file
    #------------------------------    
    for RTG_file in RTG_list:
        print 'Reading values from:', RTG_file
        RTG_unit = open( RTG_file, 'rb')
        grid     = numpy.fromfile( RTG_unit, count=nx*ny, dtype=dtype )
        if (SWAP_BYTES):  grid.byteswap()
        RTG_unit.close()
        grid.tofile( RTS_unit )

    #---------------------
    # Close new RTS file
    #---------------------
    RTS_unit.close()
    print 'Finished writing RTG files to RTS format.'
    print ' '
Exemple #7
0
    def check_and_store_info(
        self, file_name, info=None, grid_name="UNKNOWN", dtype="float32", MAKE_RTI=True, MAKE_BOV=False
    ):

        # -----------------------------------------------------
        # Note: This object (self) may be new or it may have
        #       been used previously.  In the latter case,
        #       "info" should still be available in "self".
        #       We only need info if MAKE_RTI or MAKE_BOV.
        # -----------------------------------------------------
        self.format = "NCGS"
        self.file_name = file_name
        self.time_index = 0
        self.grid_name = grid_name

        # -----------------------------------------------------
        # This was used by rts_files.check_and_store_info()
        # but is not appropriate here because we need to
        # know nx, ny, dx and dy for the netCDF file.
        # -----------------------------------------------------
        ### if not(MAKE_RTI or MAKE_BOV): return

        # ---------------------------------
        # Was "info" argument provided ?
        # ---------------------------------
        NEW_INFO = True
        if info == None:
            try:
                info = self.info
                self.nx = info.ncols  ###
                self.ny = info.nrows
                NEW_INFO = False
                ## print 'Found info in state.'
            except:
                # ------------------------------------------
                # Try to find RTI file to copy info from.
                # Don't create a new RTI file.
                # ------------------------------------------
                RTI_file = rti_files.try_to_find_rti_file(file_name)
                if RTI_file != "none":
                    print "Reading info from: " + RTI_file
                    info = rti_files.read_info(RTI_file)
                else:
                    print "ERROR during open_new_file():"
                    print '   Could not find RTI file and "info"'
                    print "   argument was not provided."
                    print " "
                    return

        # -----------------------------
        # Update "info" as necessary
        # -----------------------------
        info.grid_file = file_name
        info.data_type = rti_files.get_rti_data_type(dtype)
        info.data_source = "TopoFlow 3.0"
        info.gmin = -9999.0
        info.gmax = -9999.0

        # ---------------------------------------
        # If new "info" was provided, store it
        # ---------------------------------------
        if NEW_INFO:
            self.info = info
            self.nx = info.ncols
            self.ny = info.nrows
            ## print 'Stored new info in state.'

        # -------------------
        # Write RTI file ?
        # -------------------
        if MAKE_RTI:
            prefix = rti_files.get_file_prefix(file_name)
            RTI_file = prefix + ".rti"
            rti_files.write_info(RTI_file, info)
            # print 'Wrote grid info to: ' + RTI_file   ######

        # -------------------
        # Write BOV file ?
        # -------------------
        if MAKE_BOV:
            bov_files.write_info_as_bov(file_name, info, grid_name)