Commit db3731c2 authored by Mario Chirinos's avatar Mario Chirinos

git library

parent ff043eca
......@@ -8,13 +8,14 @@ Install:
Example:
$ python -m doctest -v APISentinel.py
$ python3 -m doctest -v APISentinel.py
"""
import os
from sentinelsat.sentinel import SentinelAPI
from datetime import date
from collections import OrderedDict
import time
#from osgeo import ogr
......@@ -23,8 +24,8 @@ class APISentinel(object):
Test Case
>>> sentinel = APISentinel('asalazarg', 'geo135asg')
>>> products = sentinel.getProducts("POLYGON((-89.99450683593753 21.279137394108716,-89.13757324218751 21.2996106049456,-89.30236816406251 20.68418377935238,-90.0494384765625 20.715015145512098,-89.99450683593753 21.279137394108716))", ('20151219', date(2015,12,29)), {"platformname":"Sentinel-2"})
>>> print len(products)
3
>>> print(len(products))
6
"""
......@@ -93,19 +94,31 @@ class APISentinel(object):
os.chdir(dir)
# self.api.download_all(products)
longTermArchiveList=[]
for p in products:
print(products[p]['filename'])
self.api.download(p)
try:
info = self.api.download(p)
if not info["Online"]:
print("Adding producto to long term list")
longTermArchiveList.append(p)
time.sleep(1)
except:
def filterProducts(self, productList):
#, user_footprint, threshold):
print ("ERROR")
products = productList.copy()
for p in productList:
if productList[p]['filename'].find("OPER_PRD") != -1:
del products[p]
return longTermArchiveList
return products
# def filterProducts(self, productList):
# #, user_footprint, threshold):
# products = productList.copy()
# for p in productList:
# if productList[p]['filename'].find("OPER_PRD") != -1:
# del products[p]
# return products
......@@ -4,54 +4,37 @@
import sys, os
import json
############ lee archivo de configuración ################
dirname = os.path.dirname(__file__)
configfile = os.path.join(dirname, '../config/config.json')
if __name__ == "__main__": # Local Run
import APISentinel
import polygonToBox
else: # Module Run, When going production - delete if/else
from . import APISentinel
from . import polygonToBox
with open(configfile, 'r') as f:
config = json.load(f)
#print(config['PATHS']['PATH_GEOSENTINEL'])
SENTINEL_PATH = config['PATHS']['PATH_GEOSENTINEL']
###########################################################
sys.path.append(SENTINEL_PATH)
from geosentinel import APISentinel
from geosentinel import polygonToBox
from osgeo import ogr
from datetime import date
from collections import OrderedDict
#def findSentinelProducts(wkt, startDate, endDate, platform, cloud):
# sentinel = APISentinel.APISentinel('asalazarg', 'geo135asg')
# products = sentinel.getProducts(wkt, (startDate, endDate), {"platformname":platform, "cloudcoverpercentage":"[0 TO "+str(cloud)+"]"})
# return products
#productsCodes = {"L1C":"L1C","L2A":"L1C"}
productType = {"L1C":"S2MSI1C", "L2A":"S2MSI2A"}
def main(argv):
if len(sys.argv) != 2:
print("Usage: " + argv[0] + " <JSON File>")
else:
jsonFile=open(argv[1]).read()
cfg = json.loads(jsonFile)
def findSentinelProducts(cfg, uinput = False):#main(argv):
sentinel = APISentinel.APISentinel(cfg['username'], cfg['password'])
geometry = ogr.CreateGeometryFromWkt(cfg['wkt'])
wktList = []
if geometry.GetGeometryType() == ogr.wkbPolygon:
print("POLYGON")
wkt = polygonToBox.getWKTPolygonBoundingBox(cfg['wkt'], True)
wktList.append(wkt)
print(wkt)
elif geometry.GetGeometryType() == ogr.wkbMultiPolygon:
print("MULTIPOLYGON")
for i in range(0, geometry.GetGeometryCount()):
g = geometry.GetGeometryRef(i).exportToWkt()
g = geometry.GetGeometryRef(i).ExportToWkt()
wkt = polygonToBox.getWKTPolygonBoundingBox(g, True)
print(wkt)
wktList.append(wkt)
productList = OrderedDict()
for wkt in wktList:
tmpList =sentinel.getProducts(wkt, (cfg['startDate'], cfg['endDate']), {"producttype":productType[cfg["productLevel"]], "platformname":cfg['platform'], "cloudcoverpercentage":"[0 TO "+str(cfg['clouds'])+"]"}, "Contains")
......@@ -61,7 +44,7 @@ def main(argv):
tmpList = sentinel.getProducts(wkt, (cfg['startDate'], cfg['endDate']), {"producttype":productType[cfg["productLevel"]], "platformname":cfg['platform'], "cloudcoverpercentage":"[0 TO "+str(cfg['clouds'])+"]"})
productList.update(tmpList)
# fileNames = [productList[k]['filename'].replace("SAFE", "zip").replace(productsCodes[cfg['productLevel']], cfg['productLevel']) for k in productList.keys() ]
fileNames = [productList[k]['filename'].replace("SAFE", "zip") for k in productList.keys() ]
downloadDir = cfg["productsDir"]+cfg["productLevel"]+"/"
......@@ -69,14 +52,18 @@ def main(argv):
matchingProducts = set(fileNames).intersection(set(dirList))
print ( str(len(matchingProducts))+" of " + str(len(fileNames)) +" "+ cfg["productLevel"] + " products found.")
# rawDir = inDir.replace(cfg['productLevel'],productsCodes[cfg['productLevel']])
text =""
if uinput==True:
while text != "yes" and text != "no":
text = input("Do you want to download this products to "+downloadDir+ " ? (yes, no)")
if text=="yes":
sentinel.downloadProducts(productList,downloadDir)
if uinput==False or text=="yes":
longTermArchiveList = sentinel.downloadProducts(productList,downloadDir)
if len(longTermArchiveList)>0:
print( str(len(longTermArchiveList)) + " products are in the long term archive" )
with open(cfg["projectDir"]+'longTermArchiv.json', 'w') as outfile:
json.dump(longTermArchiveList, outfile)
L2ADir = cfg["productsDir"]+"L2A/"
if cfg["productLevel"]=="L1C":
......@@ -99,18 +86,20 @@ def main(argv):
print ( str(len(matchingProducts))+" of " + str(len(fileNames)) +" L1C products found in L2A.")
text =""
if uinput==True:
while text != "yes" and text != "no":
text = input("Do you want to convert this products and save them to "+downloadDir+ " ? (yes, no)")
if text=="yes":
if uinput==False or text=="yes":
#print("L1CProductListToL2A.sh "+rawDir+" "+inDir+" 1")
os.system("L1CProductListToL2A.sh "+linksDir+" "+L2ADir+" 1")
text =""
linksDir=cfg["projectDir"]+"L2A/"
if uinput==True:
while text != "yes" and text != "no":
text = input("Do you want to link this products to "+linksDir+ " ? (yes, no)")
if text=="yes":
if uinput==False or text=="yes":
fileNames = [productList[k]['filename'].replace("SAFE", "zip").replace("L1C", "L2A") for k in productList.keys() ]
dirList = os.listdir(L2ADir)
......@@ -121,6 +110,14 @@ def main(argv):
os.system("ln -s " + L2ADir+f + " " + linksDir+f)
print (str(len(matchingProducts)) + " Linked to " + linksDir)
#===============================================================================
def main(argv):
if len(sys.argv) != 2:
print("Usage: " + argv[0] + " <JSON File>")
else:
with open(argv[1]) as jsonFile:
cfg = json.load(jsonFile)
findSentinelProducts(cfg, uinput=True)
if __name__ == "__main__":
main(sys.argv)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment