def read_flow_grid(self): #---------------------------------------------------- # Read a grid of D8 flow codes, same size as DEM. #---------------------------------------------------- TF_Print('Reading D8 flow grid...') code_file = (self.in_directory + self.site_prefix + '_flow.rtg') self.flow_grid = rtg_files.read_grid(code_file, self.rti, RTG_type='BYTE')
def get_flow_length_grid(self, DOUBLE=False): #------------------------------------------------------------- # NOTES: This routine returns the flow lengths for each # pixel in the DEM. The extra length of flowing to a # diagonal pixel is taken into account, as well as the # latitude-dependence for DEMs with fixed-angle pixels # (Geographic lat/lon). # The only difference between this and the Flow_Widths # function is that roles of dx and dy are switched. # Flow lengths are set to dx[0] for the pixels where # (flow grid eq 0), such as on the edges of the DEM. #------------------------------------------------------------- TF_Print('Computing flow length grid...') #----------------------- # Get pixel dimensions #----------------------- dx, dy, dd = pixels.get_sizes_by_row(self.rti, METERS=True) #------------------------- # Double or Float type ? #------------------------- if (DOUBLE): ds = zeros([self.ny, self.nx], dtype='Float64') else: ds = zeros([self.ny, self.nx], dtype='Float32') dx = float32(dx) dy = float32(dy) dd = float32(dd) #---------------------------------------------- # Initialize to default value that is used # for pixels with flow code of zero. This is # done to avoid "divide by zero" errors. #---------------------------------------------- ds += dx[0] ## ds += dx.min() for row in xrange(self.ny): g = self.flow_grid[row, :] #---------------- # Diagonal flow #---------------- wd = where(logical_or(logical_or(logical_or((g == 1), (g == 4)), \ (g == 16)), (g == 64))) nwd = size(wd[0]) if (nwd != 0): ds[row, wd] = dd[row] #--------------------- # East and west flow #--------------------- wh = where(logical_or((g == 2), (g == 32))) nwh = size(wh[0]) if (nwh != 0): ds[row, wh] = dx[row] #----------------------- # North and south flow #----------------------- wv = where(logical_or((g == 8), (g == 128))) nwv = size(wv[0]) if (nwv != 0): ds[row, wv] = dy[row] #---------- # Report #---------- ds_min = ds.min() ds_max = ds.max() TF_Print(' min(ds) = ' + str(ds_min) + ' [m]') TF_Print(' max(ds) = ' + str(ds_max) + ' [m]') self.ds = ds
def get_flow_width_grid(self, DOUBLE=False, METHOD2=False): #------------------------------------------------------------- # NOTES: This routine returns the flow widths for each # pixel in the DEM. The extra width of flowing to a # diagonal pixel is taken into account, as well as the # lat/lon-dependence for DEMs with fixed-angle pixels # (Geographic lat/lon). # METHOD2 version ensures that the sum of all flow # widths around a pixel is equal to 2*(dx + dy), but # is incorrect for case of a plane and others. # Flow widths are zero where (flow grid eq 0). # Flow widths are for entire pixel and are appropriate # for overland or subsurface flow. #------------------------------------------------------------- # Is this only used by Seepage function now ??? #------------------------------------------------------------- # NB! numpy.where returns a 2-tuple, but with "empty" # second part when applied to a 1D array. This means # that nw = size(w[0]) still works. #------------------------------------------------------------- print 'Computing flow width grid...' #----------------------- # Get pixel dimensions #----------------------- dx, dy, dd = pixels.get_sizes_by_row(self.rti, METERS=True) #------------------------- # Double or Float type ? #------------------------- if (DOUBLE): dw = zeros([self.ny, self.nx], dtype='Float64') else: dw = zeros([self.ny, self.nx], dtype='Float32') dx = float32(dx) dy = float32(dy) dd = float32(dd) #---------------------------------------------- # Initialize to default value that is used # for pixels with flow code of zero. This is # done to avoid "divide by zero" errors. #---------------------------------------------- dw += dx[0] ## dw = dw + dx.min() for row in xrange(self.ny): g = self.flow_grid[row, :] #---------------- # Diagonal flow #---------------- wd = where(logical_or(logical_or(logical_or((g == 1), (g == 4)), \ (g == 16)), (g == 64))) nwd = size(wd[0]) if (nwd != 0): if not (METHOD2): dw[row, wd] = dd[row] else: dw[row, wd] = (dx[row] + dy[row]) / 4 #--------------------- # East and west flow #--------------------- wh = where(logical_or((g == 2), (g == 32))) nwh = size(wh[0]) if (nwh != 0): dw[row, wh] = dy[row] if (METHOD2): dw[row, wh] = dw[row, wh] / 2 #----------------------- # North and south flow #----------------------- wv = where(logical_or((g == 8), (g == 128))) nwv = size(wv[0]) if (nwv != 0): dw[row, wv] = dx[row] if (METHOD2): dw[row, wv] = dw[row, wv] / 2 #---------- # Report #---------- dw_min = dw.min() dw_max = dw.max() TF_Print(' min(dw) = ' + str(dw_min) + ' [m]') TF_Print(' max(dw) = ' + str(dw_max) + ' [m]') self.dw = dw
def get_da(rti, METERS=False, REPORT=False, VERBOSE=False): #------------------------------------ # Pixel dimensions; convert km to m # These are planform dimensions. #--------------------------------------- dx, dy, dd = get_sizes_by_row(rti, METERS=True) #------------------------------------------- # 7/13/06. Allow da to be scalar or grid. # For speed; was always grid before. #------------------------------------------- if (rti.pixel_geom == 1): da = (dx[0] * dy[0]) if (VERBOSE): TF_Print('dx = ' + str(dx[0]) + ' [m]') TF_Print('dy = ' + str(dy[0]) + ' [m]') TF_Print('da = ' + str(da) + ' [m^2]') else: #--------------------------------- # Convert da from 1D to 2D array # Then subscript with the wk's. #--------------------------------- TF_Print('Computing pixel area grid...') nx = rti.ncols ny = rti.nrows ###################################### # DOUBLE CHECK THIS. SEEMS CORRECT # IS THERE A MORE EFFICIENT WAY ?? ###################################### da_by_row = (dx * dy) da = reshape(repeat(da_by_row, nx), (ny, nx)) #------------------------------------------------------ ## This resulted from I2PY and doesn't seem right. ## matrixmultiply() was depracated in favor of dot(). #------------------------------------------------------ ## self.da = (transpose(matrixmultiply(transpose(repeat(1,nx)), \ ## transpose(self.da_by_row)))) if (VERBOSE): da_min = da.min() da_max = da.max() TF_Print(' min(da) = ' + str(da_min) + ' [m^2]') TF_Print(' max(da) = ' + str(da_max) + ' [m^2]') dx_min = dx.min() dx_max = dx.max() TF_Print(' min(dx) = ' + str(dx_min) + ' [m]') TF_Print(' max(dx) = ' + str(dx_max) + ' [m]') dy_min = dy.min() dy_max = dy.max() TF_Print(' min(dy) = ' + str(dy_min) + ' [m]') TF_Print(' max(dy) = ' + str(dy_max) + ' [m]') TF_Print(' ') return da