Commit db3731c2 authored by Mario Chirinos's avatar Mario Chirinos

git library

parent ff043eca
...@@ -8,13 +8,14 @@ Install: ...@@ -8,13 +8,14 @@ Install:
Example: Example:
$ python -m doctest -v APISentinel.py $ python3 -m doctest -v APISentinel.py
""" """
import os import os
from sentinelsat.sentinel import SentinelAPI from sentinelsat.sentinel import SentinelAPI
from datetime import date from datetime import date
from collections import OrderedDict from collections import OrderedDict
import time
#from osgeo import ogr #from osgeo import ogr
...@@ -23,8 +24,8 @@ class APISentinel(object): ...@@ -23,8 +24,8 @@ class APISentinel(object):
Test Case Test Case
>>> sentinel = APISentinel('asalazarg', 'geo135asg') >>> 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"}) >>> 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) >>> print(len(products))
3 6
""" """
...@@ -93,19 +94,31 @@ class APISentinel(object): ...@@ -93,19 +94,31 @@ class APISentinel(object):
os.chdir(dir) os.chdir(dir)
# self.api.download_all(products) # self.api.download_all(products)
longTermArchiveList=[]
for p in products: for p in products:
print(products[p]['filename']) 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): print ("ERROR")
#, user_footprint, threshold):
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 @@ ...@@ -4,54 +4,37 @@
import sys, os import sys, os
import json import json
############ lee archivo de configuración ################ if __name__ == "__main__": # Local Run
dirname = os.path.dirname(__file__) import APISentinel
configfile = os.path.join(dirname, '../config/config.json') 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 osgeo import ogr
from datetime import date from datetime import date
from collections import OrderedDict 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"} productType = {"L1C":"S2MSI1C", "L2A":"S2MSI2A"}
def main(argv): def findSentinelProducts(cfg, uinput = False):#main(argv):
if len(sys.argv) != 2:
print("Usage: " + argv[0] + " <JSON File>")
else:
jsonFile=open(argv[1]).read()
cfg = json.loads(jsonFile)
sentinel = APISentinel.APISentinel(cfg['username'], cfg['password']) sentinel = APISentinel.APISentinel(cfg['username'], cfg['password'])
geometry = ogr.CreateGeometryFromWkt(cfg['wkt']) geometry = ogr.CreateGeometryFromWkt(cfg['wkt'])
wktList = [] wktList = []
if geometry.GetGeometryType() == ogr.wkbPolygon: if geometry.GetGeometryType() == ogr.wkbPolygon:
print("POLYGON")
wkt = polygonToBox.getWKTPolygonBoundingBox(cfg['wkt'], True) wkt = polygonToBox.getWKTPolygonBoundingBox(cfg['wkt'], True)
wktList.append(wkt) wktList.append(wkt)
print(wkt) print(wkt)
elif geometry.GetGeometryType() == ogr.wkbMultiPolygon: elif geometry.GetGeometryType() == ogr.wkbMultiPolygon:
print("MULTIPOLYGON")
for i in range(0, geometry.GetGeometryCount()): for i in range(0, geometry.GetGeometryCount()):
g = geometry.GetGeometryRef(i).exportToWkt() g = geometry.GetGeometryRef(i).ExportToWkt()
wkt = polygonToBox.getWKTPolygonBoundingBox(g, True) wkt = polygonToBox.getWKTPolygonBoundingBox(g, True)
print(wkt) print(wkt)
wktList.append(wkt) wktList.append(wkt)
productList = OrderedDict() productList = OrderedDict()
for wkt in wktList: 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") 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): ...@@ -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'])+"]"}) tmpList = sentinel.getProducts(wkt, (cfg['startDate'], cfg['endDate']), {"producttype":productType[cfg["productLevel"]], "platformname":cfg['platform'], "cloudcoverpercentage":"[0 TO "+str(cfg['clouds'])+"]"})
productList.update(tmpList) 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() ] fileNames = [productList[k]['filename'].replace("SAFE", "zip") for k in productList.keys() ]
downloadDir = cfg["productsDir"]+cfg["productLevel"]+"/" downloadDir = cfg["productsDir"]+cfg["productLevel"]+"/"
...@@ -69,14 +52,18 @@ def main(argv): ...@@ -69,14 +52,18 @@ def main(argv):
matchingProducts = set(fileNames).intersection(set(dirList)) matchingProducts = set(fileNames).intersection(set(dirList))
print ( str(len(matchingProducts))+" of " + str(len(fileNames)) +" "+ cfg["productLevel"] + " products found.") print ( str(len(matchingProducts))+" of " + str(len(fileNames)) +" "+ cfg["productLevel"] + " products found.")
# rawDir = inDir.replace(cfg['productLevel'],productsCodes[cfg['productLevel']])
text ="" text =""
if uinput==True:
while text != "yes" and text != "no": while text != "yes" and text != "no":
text = input("Do you want to download this products to "+downloadDir+ " ? (yes, no)") text = input("Do you want to download this products to "+downloadDir+ " ? (yes, no)")
if text=="yes": if uinput==False or text=="yes":
sentinel.downloadProducts(productList,downloadDir) 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/" L2ADir = cfg["productsDir"]+"L2A/"
if cfg["productLevel"]=="L1C": if cfg["productLevel"]=="L1C":
...@@ -99,18 +86,20 @@ def main(argv): ...@@ -99,18 +86,20 @@ def main(argv):
print ( str(len(matchingProducts))+" of " + str(len(fileNames)) +" L1C products found in L2A.") print ( str(len(matchingProducts))+" of " + str(len(fileNames)) +" L1C products found in L2A.")
text ="" text =""
if uinput==True:
while text != "yes" and text != "no": while text != "yes" and text != "no":
text = input("Do you want to convert this products and save them to "+downloadDir+ " ? (yes, 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") #print("L1CProductListToL2A.sh "+rawDir+" "+inDir+" 1")
os.system("L1CProductListToL2A.sh "+linksDir+" "+L2ADir+" 1") os.system("L1CProductListToL2A.sh "+linksDir+" "+L2ADir+" 1")
text ="" text =""
linksDir=cfg["projectDir"]+"L2A/" linksDir=cfg["projectDir"]+"L2A/"
if uinput==True:
while text != "yes" and text != "no": while text != "yes" and text != "no":
text = input("Do you want to link this products to "+linksDir+ " ? (yes, 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() ] fileNames = [productList[k]['filename'].replace("SAFE", "zip").replace("L1C", "L2A") for k in productList.keys() ]
dirList = os.listdir(L2ADir) dirList = os.listdir(L2ADir)
...@@ -121,6 +110,14 @@ def main(argv): ...@@ -121,6 +110,14 @@ def main(argv):
os.system("ln -s " + L2ADir+f + " " + linksDir+f) os.system("ln -s " + L2ADir+f + " " + linksDir+f)
print (str(len(matchingProducts)) + " Linked to " + linksDir) 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__": if __name__ == "__main__":
main(sys.argv) 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