From 04d9806e9804906b26315339990f8534fa221011 Mon Sep 17 00:00:00 2001 From: Dan Howe Date: Fri, 20 Jul 2018 10:48:38 +1000 Subject: [PATCH] Save most recent profile images in 'latest' folder --- las_outputs.py | 55 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/las_outputs.py b/las_outputs.py index 284b2f2..740e990 100644 --- a/las_outputs.py +++ b/las_outputs.py @@ -32,7 +32,7 @@ def get_datestring(x): """Format a date integer into an ISO-8601 date string Args: - x: integer representation of date string + x: unformatted date Returns: formatted date string @@ -54,13 +54,20 @@ def remove_temp_files(directory): return None -def plot_profiles(profile_name, csv_output_dir, graph_loc, ch_limits, delta_vol): +def plot_profiles(profile_name, csv_output_dir, graph_loc, ch_limits, delta_vol, survey_date): csv_name = profile_name + '.csv' profiles = pd.read_csv(os.path.join(csv_output_dir, csv_name)) # Remove metadata, and extract profile coordinates profiles = profiles.loc[:, 'Chainage':].set_index('Chainage') + # Determine if current survey is the latest + current_survey_col = 'Elevation_' + survey_date + is_latest = profiles.columns[-1] == current_survey_col + + # Only plot profiles up to current survey date + profiles = profiles.loc[:, :current_survey_col] + # Find landward limit of profile (behind beach) ch_min = ch_limits.loc[profile_name, 'Landward Limit'] @@ -84,9 +91,11 @@ def plot_profiles(profile_name, csv_output_dir, graph_loc, ch_limits, delta_vol) ax.set_ylabel('Elevation (m AHD)', labelpad=10) # Show most recent volume change - ax.annotate('Most recent\nvolume change:\n{:0.1f} m$^3$/m'.format(delta_vol), - (0.05, 0.15), xycoords='axes fraction', fontsize=9, - backgroundcolor='#ffffff', linespacing=1.5) + if delta_vol is not None: + ax.annotate('Most recent\nvolume change:\n{:0.1f} m$^3$/m'.format(delta_vol), + (0.05, 0.15), xycoords='axes fraction', fontsize=9, + backgroundcolor='#ffffff', linespacing=1.5) + ax.legend(edgecolor='none', facecolor='#ffffff', fontsize=9) ax.xaxis.set_minor_locator(MultipleLocator(5)) @@ -94,8 +103,21 @@ def plot_profiles(profile_name, csv_output_dir, graph_loc, ch_limits, delta_vol) ax.xaxis.grid(True, which='minor', color='k', linewidth=0.5, alpha=0.3) ax.yaxis.grid(True,which='minor',color='k', linewidth=0.5, alpha=0.3) - png_name = os.path.join(graph_loc, profile_name + '.png') - plt.savefig(png_name, bbox_inches='tight', dpi=300) + # Save in folder with current date + png_dirs = [os.path.join(graph_loc, get_datestring(survey_date))] + if is_latest: + # Save copy in'latest' if survey is most recent + png_dirs += [os.path.join(graph_loc, 'latest')] + + for png_dir in png_dirs: + # Prepare output directory + try: + os.makedirs(png_dir) + except FileExistsError: + pass + + png_name = os.path.join(png_dir, profile_name + '.png') + plt.savefig(png_name, bbox_inches='tight', dpi=300) plt.close() @@ -117,9 +139,6 @@ def calculate_volumes(profile_name, survey_date, csv_output_dir, ch_limits, volu except FileNotFoundError: volumes = pd.DataFrame() - # Format dates - date = get_datestring(survey_date) - for current_date in profiles.columns: # Get Nielsen erosion volumes chainage = profiles.loc[:, current_date].dropna().index @@ -127,7 +146,7 @@ def calculate_volumes(profile_name, survey_date, csv_output_dir, ch_limits, volu volume = nielsen_volumes.volume_available(chainage, elevation, ch_min) # Update spreadsheet - volumes.loc[profile_name, date] = volume + volumes.loc[profile_name, 'Volume_' + survey_date] = volume # Save updated volumes spreadsheet volumes = volumes[volumes.columns.sort_values()] @@ -135,9 +154,13 @@ def calculate_volumes(profile_name, survey_date, csv_output_dir, ch_limits, volu volumes.to_csv(csv_vol) # Get most recent volume difference for current profile - previous_vol = volumes.loc[profile_name].values[-2] - current_vol = volumes.loc[profile_name].values[-1] - delta_vol = current_vol - previous_vol + try: + previous_vol = volumes.loc[profile_name].values[-2] + current_vol = volumes.loc[profile_name].values[-1] + delta_vol = current_vol - previous_vol + except IndexError: + # Return None if there is only one survey + delta_vol = None return delta_vol @@ -164,7 +187,7 @@ def main(): for i, row in params_file.iterrows(): print("Starting to process %s" % row['Beach']) beach=row['Beach'] - survey_date = row['SURVEY DATE'] + survey_date = str(row['SURVEY DATE']) original_las = row['INPUT LAS'] classified_las_dir = row['LAS CLASSIFIED FOLDER'] shp_swash_dir = row['SHP SWASH FOLDER'] @@ -234,7 +257,7 @@ def main(): csv_output_dir, ch_limits, volume_output_dir) plot_profiles(profile_name, csv_output_dir, graph_loc, ch_limits, - delta_vol) + delta_vol, survey_date) # Remove temprary files remove_temp_files(tmp_dir)