Process Sentinel-1 with SNAP Python api

Some example code to process data in a batch

Step 1: install the python snappy library

cd /home/user/snap/bin

./snappy-conf /usr/bin/python /home/user/snappy/

Step 2: run the python code below

import os, datetime, glob
import gc
import snappy
from sentinelsat import SentinelAPI, geojson_to_wkt, read_geojson
from snappy import ProductIO

# Get snappy Operators
snappy.GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
# HashMap Key-Value pairs
HashMap = snappy.jpy.get_type('java.util.HashMap')
gc.enable()

"""
Input:  Amplitude_VH, Intesity_VH, Amplitude_VV, Intensity_VV
output: Amplitude_VH, Intesity_VH, Amplitude_VV, Intensity_VV
"""
def applyOrbit(timestamp, sentinel_image, HashMap):

	 print "applying orbit"

	 if os.path.isfile(input_dir + "/orb_" + timestamp + ".dim"):
		print "file already exists"
	 else:
		orbFile = input_dir + "/orb_" + timestamp
		parameters = HashMap()
		orbit_param = snappy.GPF.createProduct("Apply-Orbit-File", parameters, sentinel_image)
		ProductIO.writeProduct(orbit_param, orbFile, 'BEAM-DIMAP')  

"""
Input:  Amplitude_VH, Intesity_VH, Amplitude_VV, Intensity_VV
output: beta0_VH, beta0_VV
"""
def applyCalibration(timestamp,sentinel_image, HashMap):

		print "applying calibration"

		if os.path.isfile(input_dir + "/calib_" + timestamp + ".dim"):
			print "file already exists"
		else:
			parameters = HashMap()
			parameters.put('outputSigmaBand', False)
			parameters.put('outputBetaBand', True)
			calibFile = input_dir + "/calib_" + timestamp
			calib_param = snappy.GPF.createProduct("Calibration", parameters, ProductIO.readProduct(input_dir + "orb_" + timestamp + ".dim"))
			ProductIO.writeProduct(calib_param, calibFile, 'BEAM-DIMAP')

"""
Input:  beta0_VH, beta0_VV
output: beta0_VH, beta0_VV
"""
def applyMultilook(timestamp, HashMap):

		print "applying mulilook"

		if os.path.isfile(input_dir + "/multi_" + timestamp + ".dim"):
			print "file already exisist"		

		else:
			azLooks = 3
			rgLooks = 3
			parameters = HashMap()
			multiFile = input_dir + "/multi_" + timestamp
			parameters.put('grSquarePixel', True)
			parameters.put('nRgLooks', rgLooks)
			parameters.put('nAzLooks', azLooks)
			parameters.put('outputIntensity', False)
			multi_param = snappy.GPF.createProduct("Multilook", parameters, ProductIO.readProduct(input_dir + "/calib_" + timestamp + ".dim"))
			ProductIO.writeProduct(multi_param, multiFile, 'BEAM-DIMAP')

"""
Input:  beta0_VH, beta0_VV
output: gamma0_VH, gamma0_VV
"""
def applyTerrainCorrection(timestamp, HashMap):

		print "applying terrain correction"

		if os.path.isfile(input_dir + "/terr_" + timestamp + ".dim"):
			print "file already exists"	        

		else:
			parameters = HashMap()
			terrFile = input_dir + "/terr_" + timestamp

			parameters.put('demResamplingMethod', 'BICUBIC_INTERPOLATION')
			parameters.put('demName', 'SRTM 1Sec HGT')

			terr_param = snappy.GPF.createProduct("Terrain-Flattening", parameters,ProductIO.readProduct(input_dir + "/multi_" +  timestamp + ".dim"))
			ProductIO.writeProduct(terr_param, terrFile, 'BEAM-DIMAP')
"""
Input:  gamma0_VH, gamma0_VV
output: gamma0_VH, gamma0_VV
"""
def applyOrthorectification(timestamp, HashMap):

		print "applying Orthorectification"

		if os.path.isfile(input_dir + "/orth_" + timestamp + ".dim"):
			print "file already exists"	        

		else:
			parameters = HashMap()
			orth_file = input_dir + "/orth_" + timestamp

			orth_param = snappy.GPF.createProduct("Ellipsoid-Correction-GG", parameters,ProductIO.readProduct(input_dir + "/terr_" +  timestamp + ".dim"))
			ProductIO.writeProduct(orth_param, orth_file, 'BEAM-DIMAP')

"""
Input:  gamma0_VH, gamma0_VV
output: gamma0_VH, gamma0_VV
"""
def applySpeckleFilter(timestamp, HashMap):

		print "applying terrain correction"

		if os.path.isfile(input_dir + "/speck_" + timestamp + ".dim"):
			print "file already exists"	        

		else:
			parameters = HashMap()
			speck_file = input_dir + "/speck_" + timestamp
			parameters.put('filter', 'Lee Sigma')
			parameters.put('numberofLooks', 4)
			parameters.put('windowSize', "9x9")
			parameters.put('sigma', 0.9)
			parameters.put('targetWindowSize', "9x9")

			speck_param = snappy.GPF.createProduct("Speckle-Filter", parameters,ProductIO.readProduct(input_dir + "/orth_" +  timestamp + ".dim"))
			ProductIO.writeProduct(speck_param, speck_file, 'BEAM-DIMAP')

input_dir = "/home/user/snappy/s1/"

for folder in glob.glob(input_dir + "/*.zip"):
	if folder.endswith(".zip"):
		timestamp = folder.split("_")[5]
		sentinel_image = ProductIO.readProduct(folder)

		# add the orbit file
		applyOrbit(timestamp, sentinel_image, HashMap)
		applyCalibration(timestamp, sentinel_image, HashMap)
		applyMultilook(timestamp, HashMap)
		applyTerrainCorrection(timestamp, HashMap)
		applyOrthorectification(timestamp, HashMap)
		applySpeckleFilter(timestamp, HashMap)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s