eodags1.py 8.44 KB
Newer Older
Lozac'h Loic's avatar
Lozac'h Loic committed
1
2
3
4
5
6
7
8
9
'''
Created on 18 juin 2021

@author: loic
'''
import os
from eodag.utils.logging import setup_logging
from eodag.api.core import EODataAccessGateway
from frozenapp.models import TileEnvelope
Lozac'h Loic's avatar
Lozac'h Loic committed
10
11
12
13
from django.contrib.gis.db.models.functions import Transform
from eodag.utils import ProgressCallback
from contextlib import closing
from io import StringIO
Lozac'h Loic's avatar
eodag1    
Lozac'h Loic committed
14
15
from datetime import datetime, timedelta
import time
Lozac'h Loic's avatar
Lozac'h Loic committed
16

Lozac'h Loic's avatar
Lozac'h Loic committed
17
18
19
20
21
# os.environ["EODAG__PEPS__AUTH__CREDENTIALS__USERNAME"] = "loic.lozach"
# os.environ["EODAG__PEPS__AUTH__CREDENTIALS__PASSWORD"] = "PMUQc22$"
# os.environ["EODAG__SCIHUB__AUTH__CREDENTIALS__USERNAME"] = "loic.lozach"
# os.environ["EODAG__SCIHUB__AUTH__CREDENTIALS__PASSWORD"] = "PMUQc22$"
CONFIG_SCIHUB_ENDPOINT="https://apihub.copernicus.eu/apihub/"
Lozac'h Loic's avatar
Lozac'h Loic committed
22
23
24
25
26
27
28
29
30

class EodagS1Downloader():
    '''
    classdocs
    '''
    
    mEODataAccessGateway = None
    user_conf_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),"eodag/user_conf.yml")
    locations_conf_path = None
Lozac'h Loic's avatar
Lozac'h Loic committed
31
#     providers_conf_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),"eodag/providers.yml")
Lozac'h Loic's avatar
eodag1    
Lozac'h Loic committed
32
33
34
    productOnline = None
    productOffline = None
    
Lozac'h Loic's avatar
Lozac'h Loic committed
35
36


Lozac'h Loic's avatar
eodag1    
Lozac'h Loic committed
37
    def __init__(self, mrgstile, startdate, enddate, downdir):
Lozac'h Loic's avatar
Lozac'h Loic committed
38
39
40
        '''
        Constructor
        '''
Lozac'h Loic's avatar
eodag1    
Lozac'h Loic committed
41
42
43
        if not os.path.exists(downdir):
            os.mkdir(downdir)
            
Lozac'h Loic's avatar
Lozac'h Loic committed
44
        setup_logging(verbose=3)
Lozac'h Loic's avatar
Lozac'h Loic committed
45
        
Lozac'h Loic's avatar
Lozac'h Loic committed
46
        qs_tile =  TileEnvelope.objects.annotate(wgs84=Transform('geom',4326)).filter(tile__exact=mrgstile
Lozac'h Loic's avatar
Lozac'h Loic committed
47
48
49
50
                                                  )
        if len(qs_tile) != 1 :
            print("Can't find tile "+mrgstile)
            exit()
Lozac'h Loic's avatar
Lozac'h Loic committed
51
52
53
            
#         geomwgs84 = Transform(qs_tile[0].geom, 4326)
        print(qs_tile[0].wgs84)
Lozac'h Loic's avatar
Lozac'h Loic committed
54
        
Lozac'h Loic's avatar
Lozac'h Loic committed
55
        mpolywkt= str(qs_tile[0].wgs84.ewkt).split(";")
Lozac'h Loic's avatar
Lozac'h Loic committed
56
57
58
        print(mpolywkt[1])
        
        self.mEODataAccessGateway = EODataAccessGateway(self.user_conf_file_path)
Lozac'h Loic's avatar
eodag1    
Lozac'h Loic committed
59
        print("Search on Scihub...")
Lozac'h Loic's avatar
debug    
Lozac'h Loic committed
60
        self.mEODataAccessGateway.set_preferred_provider("scihub")
Lozac'h Loic's avatar
Lozac'h Loic committed
61
62
        
        product_type = 'S1_SAR_GRD'
Lozac'h Loic's avatar
Lozac'h Loic committed
63
        sensor_mode = 'IW'
Lozac'h Loic's avatar
Lozac'h Loic committed
64
        
Lozac'h Loic's avatar
eodag1    
Lozac'h Loic committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
        products_scihub, estimated_scihub_of_results = self.mEODataAccessGateway.search(
                productType=product_type,
                sensoroperationalmode=sensor_mode,
                start=startdate,
                end=enddate,
                geom=mpolywkt[1]
            )
        
        print(
                f"The Scihub search has found an estimated number of {estimated_scihub_of_results} products matching your criteria "
            )
        print("Search on Peps...")
        self.mEODataAccessGateway.set_preferred_provider("peps")
        
        products_peps, estimated_peps_of_results = self.mEODataAccessGateway.search(
Lozac'h Loic's avatar
Lozac'h Loic committed
80
                productType=product_type,
Lozac'h Loic's avatar
Lozac'h Loic committed
81
                sensoroperationalmode=sensor_mode,
Lozac'h Loic's avatar
Lozac'h Loic committed
82
83
84
85
86
87
                start=startdate,
                end=enddate,
                geom=mpolywkt[1]
            )
        
        print(
Lozac'h Loic's avatar
eodag1    
Lozac'h Loic committed
88
89
90
91
                f"The PEPS search has found an estimated number of {estimated_peps_of_results} products matching your criteria "
            )
        
        nbsci_online=0
Lozac'h Loic's avatar
debug    
Lozac'h Loic committed
92
        nbpeps_online=0
Lozac'h Loic's avatar
eodag1    
Lozac'h Loic committed
93
94
95
96
97
        products_availability={}
        for ps in products_scihub :
            found=False
            for pp in products_peps :
                if pp.properties["id"] == ps.properties["id"]:
Lozac'h Loic's avatar
debug    
Lozac'h Loic committed
98
99
100
101
102
103
104
105
106
107
108
109
110
                    if ps.properties["storageStatus"] == "ONLINE" and pp.properties["storageStatus"] == "ONLINE":
                        products_availability[ps.properties["id"]] = [ps,pp]
                        nbsci_online += 1
                        nbpeps_online += 1
                    elif ps.properties["storageStatus"] == "ONLINE" and pp.properties["storageStatus"] == "OFFLINE":
                        products_availability[ps.properties["id"]] = [ps,pp]
                        nbsci_online += 1
                    elif ps.properties["storageStatus"] == "OFFLINE" and pp.properties["storageStatus"] == "ONLINE":
                        products_availability[ps.properties["id"]] = [pp,ps]
                        nbpeps_online += 1
                    elif ps.properties["storageStatus"] == "OFFLINE" and pp.properties["storageStatus"] == "OFFLINE":
                        products_availability[ps.properties["id"]] = [ps,pp]
                        
Lozac'h Loic's avatar
eodag1    
Lozac'h Loic committed
111
112
113
114
                    found=True
                    break
            if found == False:
                products_availability[ps.properties["id"]] = [ps]
Lozac'h Loic's avatar
debug    
Lozac'h Loic committed
115
116
                if ps.properties["storageStatus"] == "ONLINE":
                    nbsci_online += 1
Lozac'h Loic's avatar
eodag1    
Lozac'h Loic committed
117
118
119
120
121
122
        
        for pp in products_peps :
            found=False
            
            if not pp.properties["id"] in products_availability.keys():
                products_availability[pp.properties["id"]] = [pp]
Lozac'h Loic's avatar
debug    
Lozac'h Loic committed
123
124
                if pp.properties["storageStatus"] == "ONLINE":
                    nbpeps_online += 1
Lozac'h Loic's avatar
eodag1    
Lozac'h Loic committed
125
126
        
        print(
Lozac'h Loic's avatar
debug    
Lozac'h Loic committed
127
128
                f"\nScihub has got {nbsci_online} ONLINE products over {estimated_scihub_of_results}"
                f"\nPEPS has got {nbpeps_online} ONLINE products over {estimated_peps_of_results}"
Lozac'h Loic's avatar
Lozac'h Loic committed
129
            )
Lozac'h Loic's avatar
eodag1    
Lozac'h Loic committed
130
131
        
        nb_dblav=0
Lozac'h Loic's avatar
debug    
Lozac'h Loic committed
132
        for p in sorted(products_availability, key=lambda k: len(products_availability[k]), reverse=True):
Lozac'h Loic's avatar
eodag1    
Lozac'h Loic committed
133
134
135
            
            if len(products_availability[p]) == 1 :
                print(
Lozac'h Loic's avatar
debug    
Lozac'h Loic committed
136
                    f"\nProduct {p} is only available on {products_availability[p][0].provider}"
Lozac'h Loic's avatar
eodag1    
Lozac'h Loic committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
                )
            else:
                nb_dblav+=1
        

        products_downloaded={}
        passwait=[10,30,60,120,300]
        still2download = True
        nbProducts2d = len(products_availability)
        nb_allretry = 0
        nowstr = datetime.now().strftime("%Y%m%dT%H%M%S")
        allserialfile = os.path.join(downdir,nowstr+"_alldownloads.json")
        remainserialfile = os.path.join(downdir,nowstr+"_remaindownloads.json")
        while still2download:
Lozac'h Loic's avatar
debug    
Lozac'h Loic committed
151
152
            for p in sorted(products_availability, key=lambda k: len(products_availability[k]), reverse=True):
                print(f"\nStarting Download {p}...")
Lozac'h Loic's avatar
debug    
Lozac'h Loic committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
                failed=False
                try:
                    with closing(StringIO()) as tqdm_out:
                        with ProgressCallback(file=tqdm_out) as bar:
                            products_availability[p][0].download( bar, extract=False, outputs_prefix=downdir)
                except Exception as err:
                    failed=True
                    print(err)
            
                if failed and len(products_availability[p]) == 2:
                    failed=False
                    print("Download failed. Trying with PEPS")
                    try:
                        with closing(StringIO()) as tqdm_out:
                            with ProgressCallback(file=tqdm_out) as bar:
                                products_availability[p][1].download( bar, extract=False, outputs_prefix=downdir)
                    except Exception as err:
                        failed=True
                        print(err)
  
Lozac'h Loic's avatar
eodag1    
Lozac'h Loic committed
173
174
175
176
177
178
179
180
        
                if products_availability[p][0].location[:5] == "file:":
                    products_downloaded[p] = products_availability[p][0]
        
            for p in products_downloaded.keys():
                if p in products_availability.keys():
                    products_availability.pop(p)
            nb_allretry += 1
Lozac'h Loic's avatar
Lozac'h Loic committed
181
            
Lozac'h Loic's avatar
eodag1    
Lozac'h Loic committed
182
183
184
            if len(products_downloaded) == nbProducts2d :
                still2download = False
                print(
Lozac'h Loic's avatar
debug    
Lozac'h Loic committed
185
186
187
                    f"\nAll downloads successful."
                    f"\nSerializing search products to {allserialfile}"
                    f"\nExiting."
Lozac'h Loic's avatar
eodag1    
Lozac'h Loic committed
188
189
190
191
                    )
            elif nb_allretry == 5 :
                still2download = False
                print(
Lozac'h Loic's avatar
debug    
Lozac'h Loic committed
192
193
194
195
                    f"\nMax number of retries has been completed."
                    f"\nSerializing all search products to {allserialfile}"
                    f"\nSerializing remain search products to {remainserialfile}"
                    f"\nExiting."
Lozac'h Loic's avatar
eodag1    
Lozac'h Loic committed
196
197
                    )
            else:
Lozac'h Loic's avatar
debug    
Lozac'h Loic committed
198
199
                print(f"\nPass n#{nb_allretry} has reached {len(products_downloaded)} over {nbProducts2d} expected")
                print(f"\nWaiting {passwait[nb_allretry-1]} minutes before retrying...")
Lozac'h Loic's avatar
eodag1    
Lozac'h Loic committed
200
201
                restartd = datetime.now()+timedelta(minutes=passwait[nb_allretry-1])
                restartstr = restartd.strftime("%Y/%m/%d-%H:%M:%S")
Lozac'h Loic's avatar
debug    
Lozac'h Loic committed
202
                print(f"\nRestart at {restartstr}")
Lozac'h Loic's avatar
eodag1    
Lozac'h Loic committed
203
                time.sleep(passwait[nb_allretry-1]*60)
Lozac'h Loic's avatar
Lozac'h Loic committed
204
            
Lozac'h Loic's avatar
eodag1    
Lozac'h Loic committed
205
206
207