|
| 1 | + |
| 2 | +# IMPORTING LIBRARY |
| 3 | + |
| 4 | +import csv |
| 5 | +import os |
| 6 | +import numpy as np |
| 7 | +import pandas as pd |
| 8 | +from osgeo import gdal, gdal_array, osr |
| 9 | +import matplotlib.pyplot as plt |
| 10 | + |
| 11 | + |
| 12 | +# HELPER FUNCTION |
| 13 | +def pick(c, r, mask): # (c_number, r_number, an array of 1 amd 0) |
| 14 | + filled = set() |
| 15 | + fill = set() |
| 16 | + fill.add((c, r)) |
| 17 | + width = mask.shape[1]-1 |
| 18 | + height = mask.shape[0]-1 |
| 19 | + picked = np.zeros_like(mask, dtype=np.int8) |
| 20 | + while fill: |
| 21 | + x, y = fill.pop() |
| 22 | + if y == height or x == width or x < 0 or y < 0: |
| 23 | + continue |
| 24 | + if mask[y][x] == 1: |
| 25 | + picked[y][x] = 1 |
| 26 | + filled.add((x, y)) |
| 27 | + west = (x-1, y) |
| 28 | + east = (x+1, y) |
| 29 | + north = (x, y-1) |
| 30 | + south = (x, y+1) |
| 31 | + if west not in filled: |
| 32 | + fill.add(west) |
| 33 | + if east not in filled: |
| 34 | + fill.add(east) |
| 35 | + if north not in filled: |
| 36 | + fill.add(north) |
| 37 | + if south not in filled: |
| 38 | + fill.add(south) |
| 39 | + return picked |
| 40 | + |
| 41 | +def expand(array, n): # (an array of 1 and 0, number of additional pixels) |
| 42 | + expand = array - array |
| 43 | + for i in range(len(array)): |
| 44 | + for j in range(len(array[i])): |
| 45 | + if array[i][j] == 1: |
| 46 | + for k in range(max(0, i-n), min(i+n, len(array)-1)): |
| 47 | + for l in range(max(0, j-n), min(j+n, len(array[i])-1)): |
| 48 | + expand[k][l] = 1 |
| 49 | + continue |
| 50 | + else: |
| 51 | + continue |
| 52 | + return expand |
| 53 | + |
| 54 | + |
| 55 | +#============================================================== E-A relationship |
| 56 | +def curve_preDEM(res_name, max_wl, parent_directory, grandID): |
| 57 | + # E-A-S Curve import from GRAND (WRR paper) |
| 58 | + dtdr = os.getcwd() |
| 59 | + os.chdir(parent_directory) |
| 60 | + os.chdir('GRAND_Curves') |
| 61 | + ID = [file for file in os.listdir() if str(int(grandID)) in file] |
| 62 | + dfA = pd.read_csv(ID[0], parse_dates=True) |
| 63 | + column_names = dfA.iloc[3,0].split(';') |
| 64 | + rows = [row.iloc[0].split(';') for idx, row in dfA.iloc[4:].iterrows()] |
| 65 | + # Creating the new DataFrame with the appropriate columns and rows |
| 66 | + curve_below = pd.DataFrame(rows, columns=column_names).astype(np.float32) |
| 67 | + |
| 68 | + # caculating ABOVE reservoir surface area and storage volume coresponding to each water level |
| 69 | + Outputs_directory = dtdr + '/Outputs' |
| 70 | + os.chdir(Outputs_directory) |
| 71 | + res_dem_file = ("DEMclip.tif") |
| 72 | + res_dem = gdal_array.LoadFile(res_dem_file).astype(np.float32) |
| 73 | + res_dem[res_dem == 0] = np.nan |
| 74 | + res_area = gdal_array.LoadFile('ResIso.tif').astype(np.float32) |
| 75 | + res_areaN = expand(res_area, 3) |
| 76 | + res_dem[np.where(res_areaN==0)] = np.nan |
| 77 | + |
| 78 | + min_dem = int(np.nanmin(res_dem)) |
| 79 | + curve_ext = max_wl+20 |
| 80 | + curve_temp = [["Level (m)", "Area (sq.km)", "Storage (mcm)"]] |
| 81 | + pre_area = 0 |
| 82 | + tot_storage = 0 |
| 83 | + for i in range(min_dem, curve_ext): |
| 84 | + level = i |
| 85 | + water_px = np.copy(res_dem) |
| 86 | + water_px[res_dem > i] = 0 |
| 87 | + water_px[water_px > 0] = 1 |
| 88 | + area = np.nansum(water_px)*9/10000 |
| 89 | + storage = (area + pre_area)/2 |
| 90 | + tot_storage += storage |
| 91 | + pre_area = area |
| 92 | + curve_temp = np.append(curve_temp, [[level, round(area,4), round(tot_storage,4)]], |
| 93 | + axis=0) |
| 94 | + |
| 95 | + curve_above = pd.DataFrame(curve_temp[1:], columns=column_names).astype(np.float32) |
| 96 | + |
| 97 | + if (curve_below.iloc[-1,1] >= curve_above.iloc[-1,1]): |
| 98 | + aa = np.array(curve_above.iloc[:,1]).astype(np.float32) |
| 99 | + pos = len(np.array(np.where(aa < 10))[0]) |
| 100 | + pos += 1 |
| 101 | + valA = aa[pos] |
| 102 | + bb = np.array(curve_below.iloc[:,1]).astype(np.float32) |
| 103 | + bb1 = abs(bb-valA) |
| 104 | + pos1 = np.where(bb1==min(bb1))[0] |
| 105 | + |
| 106 | + df_below = curve_below.copy() |
| 107 | + df_below = df_below.drop(index=range(int(pos1), len(df_below))) |
| 108 | + df_above = curve_above.copy() |
| 109 | + df_above = df_above.drop(index=range(0, int(pos))) |
| 110 | + |
| 111 | + curve_total = pd.concat([df_below, df_above]) |
| 112 | + curve_total = curve_total.reset_index(drop=True) |
| 113 | + |
| 114 | + max_elev = curve_total.iloc[len(curve_total)-1,0] |
| 115 | + elevation_values = np.arange(max_elev, max_elev-len(curve_total), -1)[::-1] |
| 116 | + curve_total['Depth(m)'] = elevation_values.astype(np.float32) |
| 117 | + curve_total = round(curve_total,3) |
| 118 | + |
| 119 | + if (curve_below.iloc[-1,1] < curve_above.iloc[-1,1]): |
| 120 | + df_below = curve_below.copy() |
| 121 | + df_below = df_below.drop(len(df_below)-1) |
| 122 | + |
| 123 | + aa = np.array(curve_above.iloc[:,1]).astype(np.float32) |
| 124 | + aa1 = abs(aa-df_below.iloc[-1,1]) |
| 125 | + pos1 = np.where(aa1==min(aa1))[0] |
| 126 | + |
| 127 | + df_above = curve_above.copy() |
| 128 | + df_above = df_above.drop(index=range(0, int(pos1)+1)) |
| 129 | + |
| 130 | + curve_total = pd.concat([df_below, df_above]) |
| 131 | + curve_total = curve_total.reset_index(drop=True) |
| 132 | + |
| 133 | + max_elev = curve_total.iloc[len(curve_total)-1,0] |
| 134 | + elevation_values = np.arange(max_elev, max_elev-len(curve_total), -1)[::-1] |
| 135 | + curve_total['Depth(m)'] = elevation_values.astype(np.float32) |
| 136 | + curve_total = round(curve_total,3) |
| 137 | + |
| 138 | + curve_final = [["Level (m)", "Area (sq.km)", "Storage (mcm)"]] |
| 139 | + pre_area = 0 |
| 140 | + tot_storage = 0 |
| 141 | + for i in range(0, len(curve_total)): |
| 142 | + level = curve_total.iloc[i,0] |
| 143 | + area = curve_total.iloc[i,1] |
| 144 | + storage = (area + pre_area)/2 |
| 145 | + tot_storage += storage |
| 146 | + pre_area = area |
| 147 | + curve_final = np.append(curve_final, [[level, np.round(area,3), np.round(tot_storage,3)]], |
| 148 | + axis=0) |
| 149 | + |
| 150 | + # saving output as a csv file |
| 151 | + with open('Curve.csv',"w", newline='') as my_csv: |
| 152 | + csvWriter = csv.writer(my_csv) |
| 153 | + csvWriter.writerows(curve_final) |
| 154 | + |
| 155 | + # ==================== Plot the DEM-based Level-Storage curve |
| 156 | + data = curve_final[1:, :] |
| 157 | + data = np.array(data, dtype=np.float32) |
| 158 | + # Create the scatter plot |
| 159 | + plt.figure() |
| 160 | + plt.scatter(data[:, 0], data[:, 2], s=8, c='red') |
| 161 | + # Set labels and title |
| 162 | + plt.xlabel('Level (m)') |
| 163 | + plt.ylabel('Storage (mcm)') |
| 164 | + plt.title(res_name + ' (Minimum DEM level= '+ str(round(data[0,0]))+'m)') |
| 165 | + plt.savefig(res_name+'_storageVSelevation.png', dpi=600, bbox_inches='tight') |
| 166 | + |
| 167 | + return round(data[0,0]) |
| 168 | + |
| 169 | +#============================================================== E-A relationship |
| 170 | +def curve_postDEM(res_name, max_wl): |
| 171 | + # caculating reservoir surface area and storage volume coresponding to each water level |
| 172 | + dtdr = os.getcwd() |
| 173 | + Outputs_directory = dtdr + '/' + 'Outputs' |
| 174 | + os.chdir(Outputs_directory) |
| 175 | + res_dem_file = ("DEMclip.tif") |
| 176 | + res_dem = gdal_array.LoadFile(res_dem_file).astype(np.float32) |
| 177 | + res_dem[res_dem == 0] = np.nan |
| 178 | + |
| 179 | + res_area = gdal_array.LoadFile('ResIso.tif').astype(np.float32) |
| 180 | + res_areaN = expand(res_area, 3) |
| 181 | + |
| 182 | + res_dem[np.where(res_areaN==0)] = np.nan |
| 183 | + |
| 184 | + min_dem = int(np.nanmin(res_dem)) |
| 185 | + curve_ext = max_wl+20 |
| 186 | + results = [["Level (m)", "Area (sq.km)", "Storage (mcm)"]] |
| 187 | + pre_area = 0 |
| 188 | + tot_storage = 0 |
| 189 | + for i in range(min_dem, curve_ext): |
| 190 | + level = i |
| 191 | + water_px = np.copy(res_dem) |
| 192 | + water_px[res_dem > i] = 0 |
| 193 | + water_px[water_px > 0] = 1 |
| 194 | + area = np.nansum(water_px)*9/10000 |
| 195 | + storage = (area + pre_area)/2 |
| 196 | + tot_storage += storage |
| 197 | + pre_area = area |
| 198 | + results = np.append(results, [[level, round(area,4), round(tot_storage,4)]], |
| 199 | + axis=0) |
| 200 | + |
| 201 | + # saving output as a csv file |
| 202 | + with open('Curve.csv',"w", newline='') as my_csv: |
| 203 | + csvWriter = csv.writer(my_csv) |
| 204 | + csvWriter.writerows(results) |
| 205 | + |
| 206 | + # ==================== Plot the DEM-based Level-Storage curve |
| 207 | + data = results[1:, :] |
| 208 | + data = np.array(data, dtype=np.float32) |
| 209 | + # Create the scatter plot |
| 210 | + plt.figure() |
| 211 | + plt.scatter(data[:, 0], data[:, 2], s=8, c='red') |
| 212 | + # Set labels and title |
| 213 | + plt.xlabel('Level (m)') |
| 214 | + plt.ylabel('Storage (mcm)') |
| 215 | + plt.title(res_name + ' (Minimum DEM level= '+ str(round(data[0,0]))+'m)') |
| 216 | + plt.savefig(res_name+'_storageVSelevation.png', dpi=600, bbox_inches='tight') |
| 217 | + |
| 218 | + return round(data[0,0]) |
| 219 | + |
| 220 | + |
| 221 | + |
| 222 | + |
| 223 | + |
| 224 | + |
0 commit comments