def test_save_and_load_hex(): """Test saving and loading of a HexModelGrid.""" mg1 = HexModelGrid(3, 3, 1.0) mg1.add_zeros("node", "topographic__elevation") save_grid(mg1, "testsavedgrid.grid") mg2 = load_grid("testsavedgrid.grid") assert mg1.x_of_node[0] == mg2.x_of_node[0] assert_array_equal(mg1.status_at_node, mg2.status_at_node) for name in mg1.at_node: assert_array_equal(mg1.at_node[name], mg2.at_node[name])
def main(run_dir, results_basename): # INITIALIZE # Open a file for output of results d = datetime.datetime.today() today_str = str(d.year) + str(d.month).zfill(2) + str(d.day).zfill(2) results_file = open(results_basename + today_str + '.csv', 'w') results_file.write('Run name,Slip interval,' + 'Weathering rate parameter,Slope angle,' + 'Slope gradient,Intercept,' + 'Average erosion rate' + 'Fractional soil cover\n') # RUN for d in os.listdir(run_dir): # Assume if it starts with 'tau' it's a folder containing a run if d[:9] == 'dissolve_': try: # Read the grid g = load_grid(run_dir + '/' + d + '/' + d + '.grid') # Get the profile (x, z, _) = get_profile_and_soil_thickness(g, g.at_node['node_state']) # Fit the profile polyparams = np.polyfit(x, z, 1) S = np.abs(polyparams[0]) dip_angle = np.arctan(S) * 180. / np.pi # Calculate erosion rate (ero_mean, ero_col) = calc_ero_rate_from_topo(g, DELTA, 866.0) # Calculate fractional soil cover thickness frac_soil = calc_fractional_soil_cover(g) # Write the results to our file line_to_write = (d + ',' + str(dip_angle) + ',' + str(S) + ',' + str(polyparams[1]) + ',' + str(ero_mean) + ',' + str(frac_soil)) print(line_to_write) results_file.write(line_to_write + '\n') except FileNotFoundError: print('No grid file for ' + d) # FINALIZE results_file.close()
def main(): # INITIALIZE # Set parameters/variables run_dir = '../ModelRuns/SADW2/' results_basename = 'regolith_analysis' # Open a file for output of results d = datetime.datetime.today() today_str = str(d.year) + str(d.month).zfill(2) + str(d.day).zfill(2) results_file = open(results_basename + today_str + '.csv', 'w') results_file.write('Run name,Disturbance rate parameter,' + 'Weathering rate parameter,Proportion regolith,' + 'Proportion rock,Number regolith-air pairs,' + 'Number rock-air pairs,Number surface pairs,' + 'Total regolith cells,Regolith thickness\n') # RUN for d in os.listdir(run_dir): # Assume if it starts with 'd' it's a folder containing a run if d[0] == 'd': # Read the grid g = load_grid(run_dir + d + '/' + d + '.grid') # Get the regolith and rock proportions p = calc_rock_and_regolith(g) # Get regolith thickness num_reg = np.count_nonzero(g.at_node['node_state'] == 7) reg_thick = num_reg / (g.number_of_node_columns - 2.0) # Figure out the weathering and disturbance rates from run name f = d.find('w') dd = 10.0**-(0.1 * float(d[f - 2:f])) ww = 10.0**-(0.1 * float(d[f + 2:])) # Write the results to our file line_to_write = (d + ',' + str(dd) + ',' + str(ww) + ',' + str(p[0]) + ',' + str(p[1]) + ',' + str(p[2]) + ',' + str(p[3]) + ',' + str(p[4]) + ',' + str(num_reg) + ',' + str(reg_thick)) print(line_to_write) results_file.write(line_to_write + '\n') # FINALIZE results_file.close()
def main(run_dir, results_basename): # INITIALIZE # Open a file for output of results d = datetime.datetime.today() today_str = str(d.year) + str(d.month).zfill(2) + str(d.day).zfill(2) results_file = open(results_basename + today_str + '.csv', 'w') results_file.write('Run name,Disturbance rate parameter,' + 'Weathering rate parameter,Slope angle,' + 'Slope gradient,Intercept\n') # RUN for d in os.listdir(run_dir): # Assume if it starts with 'd-' it's a folder containing a run if d[0:2] == 'd-': # Read the grid g = load_grid(run_dir + '/' + d + '/' + d + '.grid') # Get the profile (x, z, _) = get_profile_and_soil_thickness(g, g.at_node['node_state']) # Fit the profile polyparams = np.polyfit(x, z, 1) S = np.abs(polyparams[0]) dip_angle = np.arctan(S) * 180. / np.pi #(dip_angle, S, fit_params) = fit_zero_intercept_line_to_surface(x, z) # Figure out the weathering and disturbance rates from run name f = d.find('w') dd = 10.0**-(0.1 * float(d[f - 2:f])) ww = 10.0**-(0.1 * float(d[f + 2:])) # Write the results to our file line_to_write = (d + ',' + str(dd) + ',' + str(ww) + ',' + str(dip_angle) + ',' + str(S) + ',' + str(polyparams[1])) print(line_to_write) results_file.write(line_to_write + '\n') # FINALIZE results_file.close()
def test_save(): # Make a simple-ish grid mg1 = RasterModelGrid(10, 10, 2.) z = mg1.add_zeros('node', 'topographic__elevation') z += mg1.node_x.copy() fa = FlowAccumulator(mg1, flow_director='D8') fa.run_one_step() save_grid(mg1, 'testsavedgrid.grid') mg2 = load_grid('testsavedgrid.grid') os.remove('testsavedgrid.grid') assert mg1.shape == mg2.shape assert (mg1.dy, mg1.dx) == (mg2.dy, mg2.dx) assert_array_equal(mg1.status_at_node, mg2.status_at_node) for name in mg1.at_node: assert_array_equal(mg1.at_node[name], mg2.at_node[name])
def test_save(): # Make a simple-ish grid mg1 = RasterModelGrid(10, 10, xy_spacing=2.0) z = mg1.add_zeros("node", "topographic__elevation") z += mg1.node_x.copy() fa = FlowAccumulator(mg1, flow_director="D8") fa.run_one_step() save_grid(mg1, "testsavedgrid.grid") mg2 = load_grid("testsavedgrid.grid") os.remove("testsavedgrid.grid") assert mg1.shape == mg2.shape assert (mg1.dy, mg1.dx) == (mg2.dy, mg2.dx) assert_array_equal(mg1.status_at_node, mg2.status_at_node) for name in mg1.at_node: assert_array_equal(mg1.at_node[name], mg2.at_node[name])
def test_save(): # Make a simple-ish grid mg1 = RasterModelGrid((10, 10), xy_spacing=2.0) z = mg1.add_zeros("node", "topographic__elevation") z += mg1.node_x.copy() fa = FlowAccumulator(mg1, flow_director="D8") fa.run_one_step() save_grid(mg1, "testsavedgrid.grid") mg2 = load_grid("testsavedgrid.grid") os.remove("testsavedgrid.grid") assert mg1.shape == mg2.shape assert (mg1.dy, mg1.dx) == (mg2.dy, mg2.dx) assert_array_equal(mg1.status_at_node, mg2.status_at_node) for name in mg1.at_node: assert_array_equal(mg1.at_node[name], mg2.at_node[name])
def test_save(): # Make a simple-ish grid mg1 = RasterModelGrid(10,10,2.) z = mg1.add_zeros('node', 'topographic__elevation') z += mg1.node_x.copy() fa = FlowAccumulator(mg1, flow_director='D8') fa.run_one_step() save_grid(mg1, 'testsavedgrid.grid') mg2 = load_grid('testsavedgrid.grid') os.remove('testsavedgrid.grid') assert_tuple_equal(mg1.shape, mg2.shape) assert_tuple_equal((mg1.dy, mg1.dx), (mg2.dy, mg2.dx)) assert_array_equal(mg1.status_at_node, mg2.status_at_node) for name in mg1.at_node: assert_array_equal(mg1.at_node[name], mg2.at_node[name])
def test_save(): # Make a simple-ish grid mg1 = RasterModelGrid(10,10,2.) z = mg1.add_zeros('node', 'topographic__elevation') z += mg1.node_x.copy() fa = FlowAccumulator(mg1, flow_director='D8') fa.run_one_step() save_grid(mg1, 'testsavedgrid.grid') mg2 = load_grid('testsavedgrid.grid') # compare the two len(mg1.__dict__) == len(mg2.__dict__) mg1keys = sorted(list(mg1.__dict__.keys())) mg2keys = sorted(list(mg2.__dict__.keys())) for i in range(len(mg1keys)): assert_equal(mg1keys[i], mg2keys[i]) a = compare_dictionaries(mg1.__dict__,mg2.__dict__,'m1','m2') assert_equal(a, '') os.remove('testsavedgrid.grid')