def open_file(self, file_name, UPDATE=False): info = rti_files.read_info(file_name) if (info == -1): return #---------------------- # Store info in state #---------------------- self.info = info self.nx = info.ncols self.ny = info.nrows self.dx = info.xres self.dy = info.yres BPE = rti_files.get_bpe(info.data_type) self.grid_size = (self.nx * self.ny * BPE) self.SWAP_ENDIAN = self.byte_swap_needed() self.file_name = file_name self.time_index = 0 #----------------------------------- # Open file to read only or update #----------------------------------- try: if (UPDATE): rts_unit = open(file_name, 'rb+') self.rts_unit = rts_unit else: rts_unit = open(file_name, 'rb') self.rts_unit = rts_unit ### return rts_unit return True except: print 'ERROR during rts.open_file().' return False
def open_file(self, file_name, UPDATE=False): info = rti_files.read_info( file_name ) if (info == -1): return #---------------------- # Store info in state #---------------------- self.info = info self.nx = info.ncols self.ny = info.nrows self.dx = info.xres self.dy = info.yres BPE = rti_files.get_bpe( info.data_type ) self.grid_size = (self.nx * self.ny * BPE) self.SWAP_ENDIAN = self.byte_swap_needed() self.file_name = file_name self.time_index = 0 #----------------------------------- # Open file to read only or update #----------------------------------- try: if (UPDATE): rts_unit = open(file_name, 'rb+') self.rts_unit = rts_unit else: rts_unit = open(file_name, 'rb') self.rts_unit = rts_unit ### return rts_unit return True except: print 'ERROR during rts.open_file().' return False
def unit_test(): import rti_files print 'Testing meters_per_degree_lon()...' MPD_lon = meters_per_degree_lon(0) print 'meters_per_degree_lon(0) =', MPD_lon print 'meters_per_3_arcsec_lon(0) =', MPD_lon / 1200.0 print ' ' #------------------------------------------------------------ print 'Testing meters_per_degree_lat()...' MPD_lat = meters_per_degree_lat(0) print 'meters_per_degree_lat(0) =', MPD_lat print 'meters_per_3_arcsec_lat(0) =', MPD_lat / 1200.0 print ' ' #------------------------------------------------------------ print 'Testing get_da() with KY_Sub (fixed-angle pixels)...' in_directory = '/Applications/RIVIX/RiverTools_3.0/basins/KY_Sub/' site_prefix = 'KY_Sub' RTI_file = (in_directory + site_prefix + '.rti') info = rti_files.read_info(RTI_file, REPORT=True) da = get_da(info, REPORT=True, VERBOSE=True) print 'da = ' print da print 'shape(da) =', shape(da) print ' ' #------------------------------------------------------------ print 'Testing get_da() with Beaver... (fixed-length pixels)' in_directory = '/Applications/RIVIX/RiverTools_3.0/basins/Beaver_Creek_KY/' site_prefix = 'Beaver' RTI_file = (in_directory + site_prefix + '.rti') info = rti_files.read_info(RTI_file, REPORT=True) da = get_da(info, REPORT=True, VERBOSE=True) print 'da = ' print da print 'shape(da) =', shape(da) print ' '
def unit_test(): import rti_files print "Testing meters_per_degree_lon()..." MPD_lon = meters_per_degree_lon(0) print "meters_per_degree_lon(0) =", MPD_lon print "meters_per_3_arcsec_lon(0) =", MPD_lon / 1200.0 print " " # ------------------------------------------------------------ print "Testing meters_per_degree_lat()..." MPD_lat = meters_per_degree_lat(0) print "meters_per_degree_lat(0) =", MPD_lat print "meters_per_3_arcsec_lat(0) =", MPD_lat / 1200.0 print " " # ------------------------------------------------------------ print "Testing get_da() with KY_Sub (fixed-angle pixels)..." in_directory = "/Applications/RIVIX/RiverTools_3.0/basins/KY_Sub/" site_prefix = "KY_Sub" RTI_file = in_directory + site_prefix + ".rti" info = rti_files.read_info(RTI_file, REPORT=True) da = get_da(info, REPORT=True, VERBOSE=True) print "da = " print da print "shape(da) =", shape(da) print " " # ------------------------------------------------------------ print "Testing get_da() with Beaver... (fixed-length pixels)" in_directory = "/Applications/RIVIX/RiverTools_3.0/basins/Beaver_Creek_KY/" site_prefix = "Beaver" RTI_file = in_directory + site_prefix + ".rti" info = rti_files.read_info(RTI_file, REPORT=True) da = get_da(info, REPORT=True, VERBOSE=True) print "da = " print da print "shape(da) =", shape(da) print " "
def open_file(self, file_name=None, UPDATE=False): if (file_name == None): #----------------------------------- # Re-open a previously opened file #----------------------------------- file_name = self.file_name info = rti_files.read_info( file_name ) if (info == -1): return #---------------------- # Store info in state #---------------------- self.info = info self.nx = info.ncols self.ny = info.nrows self.dx = info.xres self.dy = info.yres self.file_name = file_name #----------------------------------- # Open file to read only or update #----------------------------------- try: if (UPDATE): rtg_unit = open(file_name, 'rb+') self.rtg_unit = rtg_unit else: rtg_unit = open(file_name, 'rb') self.rtg_unit = rtg_unit ### return rtg_unit return True except: print 'ERROR during rtg.open_file().' return False
def open_file(self, file_name=None, UPDATE=False): if (file_name == None): #----------------------------------- # Re-open a previously opened file #----------------------------------- file_name = self.file_name info = rti_files.read_info(file_name) if (info == -1): return #---------------------- # Store info in state #---------------------- self.info = info self.nx = info.ncols self.ny = info.nrows self.dx = info.xres self.dy = info.yres self.file_name = file_name #----------------------------------- # Open file to read only or update #----------------------------------- try: if (UPDATE): rtg_unit = open(file_name, 'rb+') self.rtg_unit = rtg_unit else: rtg_unit = open(file_name, 'rb') self.rtg_unit = rtg_unit ### return rtg_unit return True except: print 'ERROR during rtg.open_file().' return False
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)
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 ' '
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)
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 ' '
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)