chris35wills

PlotVarioFunction

Apr 11th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.80 KB | None | 0 0
  1. import os
  2. import time as time # for reading in a timer
  3. import numpy as np # maths functions (arrays etc.)
  4. from osgeo import gdal, gdalconst # for reading in raster
  5. from osgeo.gdalconst import * # for reading in raster
  6. from matplotlib import pyplot as plt # for ploting
  7. from scipy import signal # for convolution function
  8. from scipy import ndimage # for resampling image
  9. from matplotlib import cm # colour mapping
  10. import matplotlib as matplotlib
  11. import matplotlib.pyplot as plt
  12. import copy as cp
  13. from numpy import *
  14.  
  15. # start timing
  16. startTime = time.time()
  17.  
  18. variance = []
  19. lag = []
  20.  
  21. # Register driver
  22. driver = gdal.GetDriverByName('ENVI')
  23. driver.Register()
  24.  
  25. # Clear any previous plots
  26. plt.clf()
  27.  
  28. # Set file location
  29. file_name = r"/data/surface"
  30.  
  31. # open file
  32. inds = gdal.Open(file_name, GA_ReadOnly)
  33.  
  34. if inds is None:
  35.     print '\nPerhaps you need an ENVI .hdr file? If so, just open the binary up in ENVI and one will be created for you!'
  36.     os._exit(1)
  37. else:
  38.     print "%s opened successfully" %file_name
  39.    
  40. print '~~~~~~~~~~~~~~'
  41. print 'Get image size'
  42. print '~~~~~~~~~~~~~~'
  43. cols = inds.RasterXSize
  44. rows = inds.RasterYSize
  45. bands = inds.RasterCount
  46.  
  47. print "columns: %i" %cols
  48. print "rows: %i" %rows
  49. print "bands: %i" %bands
  50.  
  51. print '~~~~~~~~~~~~~~'
  52. print 'Get georeference information'
  53. print '~~~~~~~~~~~~~~'
  54. geotransform = inds.GetGeoTransform()
  55. originX = geotransform[0]
  56. originY = geotransform[3]
  57. pixelWidth = geotransform[1]
  58. pixelHeight = geotransform[5]
  59.  
  60. print "origin x: %i" %originX
  61. print "origin y: %i" %originY
  62. print "width: %2.2f" %pixelWidth
  63. print "height: %2.2f" %pixelHeight
  64.  
  65. print '~~~~~~~~~~~~~~'
  66. print 'Convert image to 2D array'
  67. print '~~~~~~~~~~~~~~'
  68. band = inds.GetRasterBand(1)
  69. image_array = band.ReadAsArray(0, 0, cols, rows)
  70. image_array_name = file_name
  71. print type(image_array)
  72. print shape(image_array)
  73.  
  74. print '~~~~~~~~~~~~~~'
  75. print 'Subsample 2D array'
  76. print '~~~~~~~~~~~~~~'
  77. image_array_subsample = image_array[4790:4910, 7000:7120]
  78.  
  79. for i in range (len(image_array_subsample)):
  80.     for j in range (len(image_array_subsample[i])):
  81.         pos_now_row = i
  82.         pos_now_col = j
  83.         value = image_array_subsample[i][j]
  84.         print "%i,%i" %(pos_now_row,pos_now_col) # position
  85.         print "value = %f" %(value)
  86.  
  87.         rows = len(image_array_subsample) # gets no. rows (count starts at 1)
  88.         cols = len(image_array_subsample[0]) # gets no. cols (count starts at 1)
  89.         print "Number of rows (size starting at 1): %d" %(rows)
  90.         print "Number of columns (size starting at 1): %d" %(cols)
  91.  
  92.         rows2 = len(image_array_subsample)-1 # gets no. rows in terms of position (count starts at 0)
  93.         cols2 = len(image_array_subsample[0])-1 # gets no. cols in terms of position (count starts at 0)
  94.         print "Number of rows (in terms of index starting at 0): %d" %(rows2)
  95.         print "Number of cols (in terms of index starting at 0): %d" %(cols2)
  96.  
  97.         for i2 in range (len(image_array_subsample)):
  98.             for j2 in range (len(image_array_subsample[i2])):
  99.                    
  100.                 pos_now_row_2 = i2
  101.                 pos_now_col_2 = j2
  102.                    
  103.                 var_value = value - image_array_subsample[i2][j2]
  104.                 var_value_ABSOLUTE = math.fabs(var_value)
  105.                 variance.append(var_value_ABSOLUTE)
  106.                
  107.                 lag_val_pre = ((pos_now_row_2 - pos_now_row)*(pos_now_row_2 - pos_now_row))+((pos_now_col_2 - pos_now_col)*(pos_now_col_2 - pos_now_col))
  108.                 lag_val = math.sqrt(lag_val_pre)
  109.                 lag.append(lag_val)
  110.  
  111. #### Loop through lists, set bins and create binned lists
  112.  
  113. for i in range (len(lag)):
  114.    
  115.     #print out list values
  116.     variance_value = variance[i]
  117.     lag_value = lag[i]
  118.     print 'lag: %.2f variance: %.2f' %(lag_value, variance_value)
  119.  
  120. #define bins
  121. start_bin_value = 0.0
  122. max_lag = max(lag)
  123. end_bin_value = max_lag
  124. print 'max_lag: %.4f' %(max_lag)
  125. bin_no = 20.0
  126. print 'bin_no: %d' %(bin_no)
  127. bin_size = (max_lag/bin_no)
  128. print 'bin_size: %.4f' %(bin_size)
  129.  
  130. # Returns evenly spaced values within a given interval
  131. # numpy.digitize() will "bin" the values; i.e. x_binned[i] will give the bin number of value in i-th index
  132. # converts variance list into a numpy array
  133. bins = np.arange(start_bin_value, end_bin_value, bin_size)
  134. x_binned = np.digitize(lag,bins)#,right=False) <-- deprecated from version on UNIX at Swansea?
  135. y_numpyarray = np.array(variance)
  136.  
  137. # Calc. mean var according to bin
  138. y_means = np.array([
  139.     (y_numpyarray[x_binned == 1].mean()),
  140.     (y_numpyarray[x_binned == 2].mean()),
  141.     (y_numpyarray[x_binned == 3].mean()),
  142.     (y_numpyarray[x_binned == 4].mean()),
  143.     (y_numpyarray[x_binned == 5].mean()),
  144.     (y_numpyarray[x_binned == 6].mean()),
  145.     (y_numpyarray[x_binned == 7].mean()),
  146.     (y_numpyarray[x_binned == 8].mean()),
  147.     (y_numpyarray[x_binned == 9].mean()),
  148.     (y_numpyarray[x_binned == 10].mean()),
  149.     (y_numpyarray[x_binned == 11].mean()),
  150.     (y_numpyarray[x_binned == 12].mean()),
  151.     (y_numpyarray[x_binned == 13].mean()),
  152.     (y_numpyarray[x_binned == 14].mean()),
  153.     (y_numpyarray[x_binned == 15].mean()),
  154.     (y_numpyarray[x_binned == 16].mean()),
  155.     (y_numpyarray[x_binned == 17].mean()),
  156.     (y_numpyarray[x_binned == 18].mean()),
  157.     (y_numpyarray[x_binned == 19].mean()),
  158.     (y_numpyarray[x_binned == 20].mean())])
  159.  
  160. variance_mean_value = [(y_means[i]) for i in range(len(y_means))]
  161. print 'number of mean values: %i' %(len(variance_mean_value))
  162.  
  163. ## Loop through lag and bin arrays to cross check
  164. for i in range(len(lag)):
  165.     lag_value = lag[i]
  166.     bin_value = x_binned[i]
  167.     var_value = variance[i]
  168.     print 'lag: %.2f, bin: %.2f, variance: %.2f' %(lag_value, bin_value, var_value)
  169.  
  170. #### plot vario-function
  171.  
  172. # make both arrays are of same type
  173. variance_mean_np = np.array(variance_mean_value)
  174. #variance_mean_np = np.array(y_means)
  175. bin_np = np.array(bins)
  176. #bin_mid_point_np = np.array(bin_mid_point)
  177. plt.clf()
  178. plt.plot(bin_np,variance_mean_np)
  179. plt.xlabel('Lag (binned)')
  180. plt.ylabel('Variance')
  181. test_vario_plot =  r'~/Desktop/'+ 'test_vario_plot.png'
  182. plt.savefig(test_vario_plot)
Advertisement
Add Comment
Please, Sign In to add comment