Abhisek92

decomposition_plot.py

May 30th, 2022 (edited)
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.99 KB | None | 0 0
  1. import cartopy.crs as ccrs
  2. import cartopy
  3. import matplotlib.pyplot as plt
  4. from matplotlib_scalebar.scalebar import ScaleBar
  5. import geemap
  6. import rasterio
  7. import ee
  8. import geemap
  9. import os
  10. import shutil
  11. import numpy as np
  12. import pandas as pd
  13. import glob2
  14. import imageio
  15. from rasterio.plot import reshape_as_raster, reshape_as_image
  16. import matplotlib.pyplot as plt
  17. import numpy as np
  18. from scipy.stats import norm, gamma, f, chi2
  19. from astropy.convolution import convolve as ap_convolve
  20. from astropy.convolution import Box2DKernel
  21.  
  22. ee.Authenticate()
  23.  
  24. ee.Initialize()
  25.  
  26. lat = -70.53611
  27. lon = 24.04528
  28. rectangle = ee.Geometry.Rectangle([lon - 0.06, lat - 0.06, lon + 0.06, lat + 0.06])
  29. im1 = (ee.ImageCollection('COPERNICUS/S1_GRD')
  30.                        .filterBounds(rectangle)
  31.                        .filterDate(ee.Date('2021-12-01'), ee.Date('2022-01-31'))
  32.                        .filter(ee.Filter.eq('orbitProperties_pass', 'ASCENDING'))
  33.                        .filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'HH'))
  34.                        .filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'HV'))
  35.                        .filter(ee.Filter.eq('relativeOrbitNumber_start', 132))
  36.                        )
  37.  
  38. out_dir = '/content/drive/MyDrive/s1'
  39. filename = os.path.join(out_dir, 's1a.tif')
  40. #geemap.ee_export_image(im1.select(['HH', 'HV', 'angle']), filename=filename, scale=10, region=rectangle, crs='EPSG:3031', file_per_band=False)
  41. geemap.ee_export_image_collection(im1, out_dir=out_dir, scale=10, region=rectangle, crs='EPSG:3031', file_per_band=False)
  42.  
  43. out_dir = '/content/drive/MyDrive/s1'
  44. filelist2 = glob2.glob(out_dir+'/S1*tif')
  45. filelist = filelist2[0:20]
  46. i = filelist[0]
  47. date = i[49:51] + '-' + i[47:49] + '-' + i[43:47]
  48.  
  49. for i in filelist:
  50.   date = i[49:51] + '-' + i[47:49] + '-' + i[43:47]
  51.   dataset = rasterio.open(i)
  52.   bands = dataset.read((1,2))
  53.   # image = reshape_as_image(bands)
  54.   # bands_1 = dataset.read()
  55.   # image_1 = reshape_as_image(bands_1)
  56.  
  57.   #gamma_hh = image_1[:, :, 0] / np.cos(image_1[:, :, 2] * 0.01745329251)
  58.   #gamma_hv = image_1[:, :, 1] / np.cos(image_1[:, :, 2] * 0.01745329251)
  59.  
  60. # Valid Pixel Mask
  61.  
  62.   valid_mask = np.logical_not(
  63.       np.logical_and(
  64.           (bands[0, :, :] > image[1, :, :]),
  65.           (image[0, :, :] > -20)
  66.         )
  67.     )
  68.     vm2 = np.stack((valid_mask, valid_mask), 0)
  69.  
  70.   image1 = np.ma.masked_array(image, vm2, fill_value=np.nan)
  71.  
  72. # dB to Linear conversion
  73.  
  74.   lin_hh = 10.0 ** (image1[0,:,:] / 10.0)
  75.   lin_hv = 10.0 ** (image1[1,:,:] / 10.0)
  76.  
  77.   # lin_hh = 10.0 ** (gamma_hh / 10.0)
  78.   # lin_hv = 10.0 ** (gamma_hv / 10.0)
  79.  
  80. # 5 x 5 Boxcar filter for speckle removal
  81.  
  82.   box_kernel = Box2DKernel(5)
  83.   hh = ap_convolve(lin_hh, box_kernel, normalize_kernel=True)
  84.   hv = ap_convolve(lin_hv, box_kernel, normalize_kernel=True)
  85.  
  86. # Dual Polarimetric indicators
  87.  
  88.   q = lin_hv / lin_hh
  89.   x = (1 - q)
  90.   y = (1 + q)
  91.   m_c = x / y
  92.   theta_c = np.rad2deg(np.arctan((x ** 2) / (x + (q ** 2))))
  93.   p1 = 1 / y
  94.   p2 = q * p1
  95.   h_c = -(p1 * np.log2(p1)) - (p2 * np.log2(p2))
  96.  
  97.   mask = np.logical_not(np.logical_and(np.isfinite(h_c), np.isfinite(theta_c)))
  98.   mask = np.logical_and(valid_mask, mask)
  99.  
  100.   h_c = np.ma.masked_array(h_c, mask, fill_value=np.nan)
  101.   theta_c = np.ma.masked_array(theta_c, mask, fill_value=np.nan)
  102.  
  103. # H_c / Theta_c Classification Plane
  104.  
  105.   z1_mask = np.logical_and((np.logical_and((0 <= h_c), (h_c < 0.3))), np.logical_and((30<= theta_c), (theta_c <= 45)))
  106.   z2_mask = np.logical_and((np.logical_and((0.3 <= h_c), (h_c < 0.5))), np.logical_and((30<= theta_c), (theta_c <= 45)))
  107.   z3_mask = np.logical_and((np.logical_and((0.5 <= h_c), (h_c < 0.7))), np.logical_and((30<= theta_c), (theta_c <= 45)))
  108.   z4_mask = np.logical_and((np.logical_and((0.7 <= h_c), (h_c <= 1))), np.logical_and((30<= theta_c), (theta_c <= 45)))
  109.   z5_mask = np.logical_and((np.logical_and((0.7 <= h_c), (h_c <= 1))), np.logical_and((15<= theta_c), (theta_c < 30)))
  110.   z6_mask = np.logical_and((np.logical_and((0.7 <= h_c), (h_c <= 1))), np.logical_and((0<= theta_c), (theta_c < 15)))
  111.   z0_mask = np.logical_and((np.logical_and((0 <= h_c), (h_c < 0.7))), np.logical_and((0<= theta_c), (theta_c < 30)))
  112.  
  113.   r = np.zeros(h_c.shape).astype(np.uint8)
  114.   g = np.zeros(h_c.shape).astype(np.uint8)
  115.   b = np.zeros(h_c.shape).astype(np.uint8)
  116.  
  117.   r[z1_mask] = 255
  118.   r[z2_mask] = 125
  119.   r[z3_mask] = 0
  120.   r[z4_mask] = 255
  121.   r[z5_mask] = 0
  122.   r[z6_mask] = 0
  123.   r[z0_mask] = 127
  124.  
  125.   g[z1_mask] = 5
  126.   g[z2_mask] = 27
  127.   g[z3_mask] = 255
  128.   g[z4_mask] = 213
  129.   g[z5_mask] = 255
  130.   g[z6_mask] = 102
  131.   g[z0_mask] = 127
  132.  
  133.   b[z1_mask] = 180
  134.   b[z2_mask] = 96
  135.   b[z3_mask] = 34
  136.   b[z4_mask] = 113
  137.   b[z5_mask] = 255
  138.   b[z6_mask] = 5
  139.   b[z0_mask] = 127
  140.  
  141.   msk = np.logical_or(np.logical_and.reduce((z0_mask, z1_mask, z2_mask, z3_mask, z4_mask, z5_mask, z6_mask)))
  142.   mask = np.logical_and(mask, msk)
  143.  
  144.   rgb = np.stack((r, g, b), -1)
  145.   rgb = np.ma.masked_array(rgb, np.stack(((mask,) * 3), -1), fill_value=255)
  146.  
  147.  
  148. # Plotting
  149.  
  150.   plt.figure(figsize=(15,15))
  151.  
  152.   ax1 = plt.subplot(131)
  153.   ax1.imshow(theta_c, cmap= 'jet', origin='upper')
  154.   plt.text(0.5, 0.02, date, horizontalalignment='center', verticalalignment='bottom', transform=ax1.transAxes,size=25, color='black', weight='bold')
  155.   scalebar = ScaleBar(1,box_alpha=0,color='black',location='lower left')
  156.   ax1.add_artist(scalebar)
  157.  
  158.   ax2 = plt.subplot(132)
  159.   ax2.imshow(h_c, cmap= 'jet', origin='upper')
  160.   plt.text(0.5, 0.02, date, horizontalalignment='center', verticalalignment='bottom', transform=ax2.transAxes,size=25, color='black', weight='bold')
  161.   scalebar = ScaleBar(1,box_alpha=0,color='black',location='lower left')
  162.   ax2.add_artist(scalebar)
  163.  
  164.   ax3 = plt.subplot(133)
  165.   ax3.imshow(rgba, cmap= 'jet', origin='upper')
  166.   plt.text(0.5, 0.02, date, horizontalalignment='center', verticalalignment='bottom', transform=ax3.transAxes,size=25, color='black', weight='bold')
  167.   scalebar = ScaleBar(1,box_alpha=0,color='black',location='lower left')
  168.   ax3.add_artist(scalebar)
Add Comment
Please, Sign In to add comment