def run_plots(DataDirectory, Base_file): root = DataDirectory + Base_file filenames = get_filenames(root) counter = 0 # create the plot for the initial raster initial_file = filenames[0] # read in the raster raster = IO.ReadRasterArrayBlocks(initial_file) f = mlab.figure(size=(1000, 1000), bgcolor=(0.5, 0.5, 0.5)) s = mlab.surf(raster, warp_scale=0.4, colormap='gist_earth', vmax=100) #mlab.outline(color=(0,0,0)) #mlab.axes(s, color=(1,1,1), z_axis_visibility=True, y_axis_visibility=False, xlabel='', ylabel='', zlabel='', ranges=[0,500,0,1000,0,0]) #@mlab.animate(delay=10) #def anim(): # now loop through each file and update the z values for fname in filenames: this_rast = IO.ReadRasterArrayBlocks(fname) s.mlab_source.scalars = this_rast #f.scene.render() # mlab.savefig(fname[:-4] + '_3d.png')
def PlotBasinsWithHillshade(DataDirectory, OutDirectory, fname_prefix, stream_order=1): """ Read in the basins and plot them over a hillshade coloured by their cluster ID """ df = pd.read_csv(OutDirectory+fname_prefix+'_profiles_clustered_SO{}.csv'.format(stream_order)) clusters = df.cluster_id.unique() # make a figure fig = plt.figure(1, facecolor='white') gs = plt.GridSpec(100,100,bottom=0.1,left=0.1,right=0.9,top=0.9) ax = fig.add_subplot(gs[5:100,5:100]) # plot the raster hs_raster = IO.ReadRasterArrayBlocks(DataDirectory+fname_prefix+'_hs.bil') extent = LSDP.GetRasterExtent(DataDirectory+fname_prefix+'_hs.bil') plt.imshow(hs_raster, cmap=cm.gray, extent=extent) means = {} for i,cl in enumerate(clusters): this_df = df[df.cluster_id == cl] # get the polygons polygons = ReadBasinPolygons(DataDirectory, OutDirectory, fname_prefix+'_basins_SO{}_CL{}'.format(stream_order,int(cl))) for p in polygons: #print(list(p.exterior.coords)) patch = PolygonPatch(p, facecolor=this_df.iloc[0]['colour'], alpha=1.0, zorder=2, lw=0.2) ax.add_patch(patch) #print(polygons) # for each plt.savefig(OutDirectory+'polygons.png', FigFormat='png', dpi=500)
def BoxPlotByCluster(DataDirectory, OutDirectory, fname_prefix, raster_name, stream_order=1): """ Make a boxplot of the results of the clustering compared to the raster specified by raster_name """ #df = pd.read_csv(OutDirectory+fname_prefix+'_profiles_clustered_SO{}.csv'.format(stream_order)) # read in the raster raster_ext = '.bil' this_raster = IO.ReadRasterArrayBlocks(DataDirectory+raster_name) EPSG_string = IO.GetUTMEPSG(DataDirectory+raster_name) NDV, xsize, ysize, GeoT, Projection, DataType = IO.GetGeoInfo(DataDirectory+raster_name) CellSize,XMin,XMax,YMin,YMax = IO.GetUTMMaxMin(DataDirectory+raster_name) pts = PT.LSDMap_PointData(OutDirectory+fname_prefix+'_profiles_clustered_SO{}.csv'.format(stream_order),data_type ='csv') easting, northing = pts.GetUTMEastingNorthing(EPSG_string=EPSG_string) cluster_id = pts.QueryData('cluster_id', PANDEX=True) clusters = list(set(cluster_id)) # dict for the data data = {k: [] for k in clusters} for x, (i, j) in enumerate(zip(northing, easting)): # convert to rows and cols X_coordinate_shifted_origin = j - XMin; Y_coordinate_shifted_origin = i - YMin; col_point = int(X_coordinate_shifted_origin/CellSize); row_point = (ysize - 1) - int(round(Y_coordinate_shifted_origin/CellSize)); # check for data at this cell this_value = this_raster[row_point][col_point] if not np.isnan(this_value): if this_value < 10: # get the cluster id data[cluster_id[x]].append(this_value) print(data) # now make a boxplot labels, these_data = [*zip(*data.items())] # 'transpose' items to parallel key, value lists plt.boxplot(these_data) plt.xticks(range(1, len(labels) + 1), labels) plt.show()
def GetLithologyPercentages(DataDirectory, OutDirectory, fname_prefix, raster_name, stream_order=1): """ Get the percentage of the nodes in each cluster that drain each lithology """ from collections import Counter # read in the raster raster_ext = '.bil' this_raster = IO.ReadRasterArrayBlocks(DataDirectory+raster_name) EPSG_string = IO.GetUTMEPSG(DataDirectory+raster_name) NDV, xsize, ysize, GeoT, Projection, DataType = IO.GetGeoInfo(DataDirectory+raster_name) CellSize,XMin,XMax,YMin,YMax = IO.GetUTMMaxMin(DataDirectory+raster_name) pts = PT.LSDMap_PointData(OutDirectory+fname_prefix+'_profiles_clustered_SO{}.csv'.format(stream_order),data_type ='csv') easting, northing = pts.GetUTMEastingNorthing(EPSG_string=EPSG_string) cluster_id = pts.QueryData('cluster_id', PANDEX=True) clusters = list(set(cluster_id)) # dict for the data data = {k: [] for k in clusters} for x, (i, j) in enumerate(zip(northing, easting)): # convert to rows and cols X_coordinate_shifted_origin = j - XMin; Y_coordinate_shifted_origin = i - YMin; col_point = int(X_coordinate_shifted_origin/CellSize); row_point = (ysize - 1) - int(round(Y_coordinate_shifted_origin/CellSize)); # check for data at this cell this_value = this_raster[row_point][col_point] if not np.isnan(this_value): data[cluster_id[x]].append(this_value) # you have the values. now what percentage are each? for key, liths in data.items(): c = Counter(liths) n_ndv = c[0.0] print(c) [print(x,": ",vals/len(liths) * 100) for x, vals in c.items()]
""" Created on Sat Feb 11 11:40:41 2017 @author: dav """ import LSDPlottingTools.LSDMap_GDALIO as lsdio import numpy as np import scipy as sp Zenith = 45 Azimuth = 315 ZFactor = 1 Directory = "/mnt/SCRATCH/Analyses/HydrogeomorphPaper/erode_diff/test_raster_diff_func/" BackgroundRasterName = "BoscastleElevations0.asc" File = Directory + BackgroundRasterName RasterData = lsdio.ReadRasterArrayBlocks(File) def Hillshade_Smooth(RasterData, altitude, azimuth, z_factor): """Plots a Hillshade a la LSDRaster""" zenith_rad = sp.deg2rad(altitude) azimuth_rad = sp.deg2rad(azimuth)
def MakeBoxPlotsKsnLithology(DataDirectory, fname_prefix, raster_name, theta=0.45, label_list=[]): """ Make boxplots of ksn compared to lithology raster. Lithology should have integer values for the different rock types (rock type with 0 is excluded). Pass in list of labels for the different units, which must be the same length as the number of lithology codes. If none is passed then just use the integer values for labelling. """ from scipy import stats # read in the raster raster_ext = '.bil' this_raster = IO.ReadRasterArrayBlocks(DataDirectory+raster_name) #EPSG_string = IO.GetUTMEPSG(DataDirectory+raster_name) EPSG_string='epsg:32611' print(EPSG_string) NDV, xsize, ysize, GeoT, Projection, DataType = IO.GetGeoInfo(DataDirectory+raster_name) CellSize,XMin,XMax,YMin,YMax = IO.GetUTMMaxMin(DataDirectory+raster_name) pts = PT.LSDMap_PointData(DataDirectory+fname_prefix+'_ksn.csv',data_type ='csv') print(pts) easting, northing = pts.GetUTMEastingNorthing(EPSG_string=EPSG_string) ksn = pts.QueryData('ksn', PANDEX=True) #print(ksn) # get the unique values in the raster raster_values = np.unique(this_raster) raster_values = raster_values[1:] # dict for the data data = {k: [] for k in raster_values} for x, (i, j) in enumerate(zip(northing, easting)): # convert to rows and cols X_coordinate_shifted_origin = j - XMin; Y_coordinate_shifted_origin = i - YMin; col_point = int(X_coordinate_shifted_origin/CellSize); row_point = (ysize - 1) - int(round(Y_coordinate_shifted_origin/CellSize)); # check for data at this cell this_raster_value = this_raster[row_point][col_point] if not np.isnan(this_raster_value) and this_raster_value != 0: data[this_raster_value].append(ksn[x]) # set up a figure fig,ax = plt.subplots(nrows=1,ncols=1, figsize=(5,5), sharex=True, sharey=True) labels, dict = [*zip(*data.items())] # 'transpose' items to parallel key, value lists print(label_list) if label_list: labels = label_list print(labels) box = plt.boxplot(dict, patch_artist=True) plt.xticks(range(1, len(labels) + 1), labels) plt.ylabel('$k_{sn}$', fontsize=14) # get the medians for plotting as an upper label medians = [] print("========SOME KSN STATISTICS=========") for key, value in data.items(): print("Key {}, median ksn = {}".format(key, np.median(value))) medians.append(np.median(value)) print("Key {}, IQR = {}".format(key, stats.iqr(value))) print("========================================") pos = np.arange(len(labels)) + 1 upperLabels = [str(np.round(s, 2)) for s in medians] # change the colours for each lithology colors=['#60609fff', '#fdbb7fff', '#935353ff', '#f07b72ff'] for patch, color in zip(box['boxes'], colors): patch.set_facecolor(color) patch.set_alpha(0.9) patch.set_edgecolor('k') for cap in box['caps']: cap.set(color='k') for wh in box['whiskers']: wh.set(color='k') for med in box['medians']: med.set(color='k') for flier, color in zip(box['fliers'], colors): flier.set_markeredgecolor(color) flier.set_markerfacecolor(color) flier.set_markersize(2) # for tick, label in zip(range(len(labels)), ax.get_xticklabels()): # k = tick % 2 # ax.text(pos[tick], top - (top*0.05), upperLabels[tick], # horizontalalignment='center', color=colors[k]) ax.grid(color='0.8', linestyle='--', which='major', zorder=1) plt.title('Boxplots of $k_{sn}$ by lithology', fontsize=14) plt.savefig(DataDirectory+fname_prefix+'_boxplot_lith_ksn.png', dpi=300, transparent=True) plt.clf() # Do some stats, yo # KS test to see if we can distinguish the distributions at a confidence level of p = 0.05 # https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.kstest.html # relief keys = list(data.keys()) values = list(data.values()) k=0 for i in range(len(keys)-1): for j in range(i+1, len(keys)): print("KS test between {} and {}".format(keys[i], keys[j])) d, p = stats.ks_2samp(values[i], values[j]) print(d, p) k += 1
# e.g. with a setup.py script that builds your cython extension library # This MUST come before you import the C hillshade pyx file if you are doing it # this way. #################### import pyximport pyximport.install() #################### from LSDPlottingTools import fast_hillshade as fasthill import LSDPlottingTools.LSDMap_GDALIO as LSDMap_IO import LSDPlottingTools.LSDMap_BasicPlotting as LSDMap_BP Directory = "/mnt/SCRATCH/Dev/ExampleTopoDatasets/" BackgroundRasterName = "indian_creek.bil" raster = LSDMap_IO.ReadRasterArrayBlocks(Directory + BackgroundRasterName) data_res = LSDMap_IO.GetGeoInfo(Directory + BackgroundRasterName)[3][1] try: NoDataValue = float( LSDMap_IO.getNoDataValue(Directory + BackgroundRasterName)) except TypeError: NoDataValue = -9999.0 ncols, nrows = raster.shape # LSDMappingTools hillshade #hs = LSDMap_BP.Hillshade(raster) #plt.imshow(hs, cmap="gray") #plt.show() #LSDRaster Cythonised version pf hillshade
as a series of rasters and overlays of different data, e.g. Hillshade overlaid by erosion/deposition amounts, or water depth or d50 size, etc. @author: dav """ import LSDPlottingTools as lsdplt import LSDPlottingTools.LSDMap_GDALIO as mapio # fun trivia: this is Welsh for 'mapping'. import numpy as np """Plots a hillshade overlain with the erossion or deposition amount""" folder = "/mnt/DATA/DATA/VIRTUALBOX_SHARED/HydrogeomorphPaper/BOSCASTLE/PaperSimulations/Radar1km/TransLim/" laptop_folder = "" filename = "boscastle5m_bedrock_fill.asc" drapename = "waterdepth2400.txt" # Create the drape array from one of the Catchment model output rasters drape_array = mapio.ReadRasterArrayBlocks(folder + drapename) # Optional: A lot of the output rasters contain very small values for certain # things like water depth or elevation difference, so you can mask this below: low_values_index = drape_array < 0.005 drape_array[low_values_index] = np.nan # Alternatively you can just pass the name of a secondary drape raster instead # of an array below: The function takes either a string or a ready-extracted array. lsdplt.DrapedOverHillshade(folder + filename, drape_array, clim_val=(0,400), \ drape_cmap='Blues', colorbarlabel='Elevation in meters'\ ,ShowColorbar=True, ShowDrapeColorbar=True, drape_cbarlabel = "Water depth (m)")