xdenisx

s1_proc

Feb 29th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.72 KB | None | 0 0
  1. #!/home/denis/miniconda/bin/python
  2. import os
  3. import glob
  4. from nansat import Nansat
  5. import time
  6. #import sys
  7. from matplotlib.patches import Polygon
  8. import matplotlib.pyplot as plt
  9. plt.switch_backend('agg')
  10. from shapely.wkt import loads
  11. from osgeo import ogr
  12. from mpl_toolkits.basemap import Basemap
  13. import numpy as np
  14. import math
  15. #from scipy.signal import wiener
  16.  
  17. ###########################################
  18. #
  19. #   Process S1 data
  20. #
  21. ###########################################
  22.  
  23. def draw_screen_poly( lats, lons, m):
  24.     x, y = m( lons, lats )
  25.     xy = zip(x,y)
  26.     poly = Polygon( xy, facecolor='red', alpha=0.5 )
  27.     l = plt.gca().add_patch(poly)
  28.     return l
  29.  
  30. def make_quicklook_extent(n, fname):
  31.     # Get scene coverage    
  32.     #poly = loads(n.get_border_wkt())
  33.     #p1 = ogr.CreateGeometryFromWkt(poly.wkt)
  34.     # Get Envelope returns a tuple (minX, maxX, minY, maxY)
  35.     #env = p1.GetEnvelope()
  36.     #lons = [env[0], env[1]]
  37.     #lats = [env[2], env[3]]
  38.    
  39.     lats = n.get_corners()[1]
  40.     lons = n.get_corners()[0]
  41.    
  42.     plt.clf()    
  43.     fig = plt.figure()
  44.  
  45.     m = Basemap(projection='npstere',lon_0=0.,boundinglat=60,\
  46.                lat_ts=90, resolution='l',rsphere=(6378273.,6356889.))
  47.  
  48.     m.drawcoastlines(linewidth=0.25)
  49.     m.fillcontinents(color='#B2B2B2')
  50.  
  51.     # Draw polygon
  52.     draw_screen_poly( lats, lons, m )
  53.    
  54.     path2file = '%s/ql/%s.jpg' % (out_dir, fname)
  55.     fig.savefig(path2file, dpi=150, transparent=True, bbox_inches='tight', pad_inches=0.1)
  56.  
  57. def reproject_gcp_to_stere(n):
  58.     ''' Change projection of GCPs to stereographic add TPS option '''
  59.     lon, lat = n.get_border()
  60.     # reproject Ground Control Points (GCPS) to stereographic projection
  61.     n.reproject_GCPs('+proj=stere +lon_0=%f +lat_0=%f +no_defs' % (lon.mean(), lat.mean()))
  62.     n.vrt.tps = True
  63.     return n
  64.  
  65. def proc_file(ifile, out_dir):
  66.     #n = Sentinel1Image(ifile)
  67.     n = Nansat(ifile)
  68.  
  69.     #n.resize(0.5, eResampleAlg=-1)
  70.     n = reproject_gcp_to_stere(n)
  71.  
  72.     fout_name = os.path.basename(ifile).split('.')[0]
  73.     start_time = time.time()
  74.    
  75.     #lats1 = n.get_corners()[1]
  76.     #lons1 = n.get_corners()[0]
  77.  
  78.     #lats = np.array([x for (y,x) in sorted(zip(lons1,lats1))])    
  79.     #lons = np.array([y for (y,x) in sorted(zip(lons1,lats1))])
  80.  
  81.     #lats = [np.min(lats1), np.max(lats1)]
  82.     #lons = [np.min(lons1), np.max(lons1)]
  83.    
  84.     # Make ql with spatial extent
  85.     #make_quicklook_extent(n, fout_name)
  86.      
  87.     #plt.savefig(path2file, dpi=150, transparent=True, bbox_inches='tight', pad_inches=0.1)    
  88.        
  89.     # HV
  90.     try:
  91.         if os.path.isfile('%s/tiff/HV_%s.tif'  % (out_dir, fout_name))==False:
  92.             # Make ql with spatial extent
  93.             plt.clf()
  94.             path2file = '%s/ql/%s.jpg' % (out_dir, fout_name)
  95.             n.write_map(path2file, pltshow=True, projection='cyl', resolution='i', continetsColor='lightgray', figureSize=(8, 8))
  96.            
  97.             print 'Start making HV tiff...'
  98.             print '%s/%s' % (out_dir, fout_name)
  99.             #n.write_figure('%s/HV_%s.tif' % (out_dir, fout_name), bands=[10], clim=[0,0.05], logarithm=True, cmapName='gray')
  100.             n.write_figure('%s/tiff/HV_%s.tif' % (out_dir, fout_name), bands=[10], clim=[0,0.05], logarithm=True, cmapName='gray')
  101.             print 'Complete!\n'
  102.     except:
  103.     print 'No HV data!'
  104.        
  105.     # HH
  106.     try:
  107.         if os.path.isfile('%s/tiff/HH_%s.tif'  % (out_dir, fout_name))==False:
  108.             # Make ql with spatial extent
  109.             plt.clf()
  110.             path2file = '%s/ql/%s.jpg' % (out_dir, fout_name)
  111.             n.write_map(path2file, pltshow=True, projection='cyl', resolution='i', continetsColor='lightgray', figureSize=(8, 8))
  112.            
  113.             print 'Start making HH...'
  114.             print '%s/%s' % (out_dir, fout_name)
  115.             #n.write_figure('%s/HH_%s.tif' % (out_dir, fout_name),bands=[8], clim=[0,0.4444], logarithm=True, cmapName='gray') #, transparency=[0,0,0])
  116.             n.write_figure('%s/tiff/HH_%s.tif' % (out_dir, fout_name),bands=[8], clim=[0,0.4444], logarithm=True, cmapName='gray') #, transparency=[0,0,0])
  117.             print 'Complete!'
  118.     except:
  119.     print 'No HH data!'
  120.  
  121.     secs = float(time.time() - start_time)
  122.     mins = secs/60.
  123.     print("--- Processed in %3.1f minutes ---" % (mins))
  124.     return 0
  125.    
  126.  
  127.  
  128. #ff = sys.argv[1]
  129.  
  130. #in_dir =  '/mnt/tiris/aux_sat_data/s1/Kara_Pechora/L1'
  131. out_dir = '/mnt/tiris/aux_sat_data/s1/Kara_Pechora/geotiff'
  132. in_dir =  '/mnt/eos/s1/L1/Kara_Pechora'
  133.  
  134.  
  135. try:
  136.     os.makedirs('%s/ql' % out_dir)
  137. except:
  138.     pass
  139.  
  140. try:
  141.     os.makedirs('%s/tiff' % out_dir)
  142. except:
  143.     pass
  144.  
  145. ffiles = glob.glob('%s/*EW*.zip' % in_dir)
  146.  
  147. for ifile in ffiles:
  148.     print ifile
  149.     proc_file(ifile, out_dir)
Advertisement
Add Comment
Please, Sign In to add comment