-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_active_fire_archived.py
245 lines (170 loc) · 8.16 KB
/
update_active_fire_archived.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
from http.client import responses
import json
from easydict import EasyDict as edict
from ntpath import join
import ee
import os, sys
import time
import subprocess
import urllib.request as Request
import zipfile
from pathlib import Path
from prettyprinter import pprint
ee.Initialize()
from utils.download import Downloader
from utils.check_asset_public import check_asset_permission
def download_and_upload(url, save_folder, bucket="sar4wildfire", asset_name=None):
filename = os.path.split(url)[-1][:-4]
if asset_name is None:
asset_name = os.path.split(url)[-1][:-4]
# print(f"url: {url}")
downloader = Downloader()
downloader.download(url, save_folder)
downloader.un_zip(save_folder / f"{filename}.zip")
asset_id = f"users/omegazhangpzh/NRT_AF/{asset_name}"
upload_to_bucket = f"gsutil -m cp -r {save_folder}/{filename}.zip gs://{bucket}/active_fire/{filename}.zip"
# remove_asset = f"earthengine rm {asset_id}"
ee_upload_table = f"earthengine upload table --force --asset_id={asset_id} gs://{bucket}/active_fire/{filename}.zip"
os.system(upload_to_bucket)
# if asset_id in asset_list:
# os.system(remove_asset)
# os.system(ee_upload_table)
ee_upload_response = subprocess.getstatusoutput(ee_upload_table)[1]
task_id = ee_upload_response.split("ID: ")[-1]
print(f"\n{asset_id}")
pprint(f"task id: {task_id}")
return asset_name, task_id
def upadte_active_fire(period_list = ['24h', '7d']):
nasa_website = "https://firms.modaps.eosdis.nasa.gov"
# save_folder = Path("D://firms-active-fire/outputs")
save_folder = Path(os.getcwd()) / "outputs"
if not os.path.exists(save_folder): os.makedirs(save_folder)
firms = [
"/data/active_fire/modis-c6.1/shapes/zips/MODIS_C6_1_Global_24h.zip",
"/data/active_fire/suomi-npp-viirs-c2/shapes/zips/SUOMI_VIIRS_C2_Global_24h.zip",
"/data/active_fire/noaa-20-viirs-c2/shapes/zips/J1_VIIRS_C2_Global_24h.zip"
]
NRT_AF = subprocess.getstatusoutput("earthengine ls users/omegazhangpzh/NRT_AF/")
# pprint(asset_list)
""" Download and Upload into GEE """
print("=============> Download and Upload into GEE <===============")
task_dict = {}
for period_key in period_list: # '48h', '7d
for i in range(len(firms)):
url = nasa_website + firms[i]
url = url.replace("24h", period_key)
filename, task_id = download_and_upload(url, save_folder, bucket="sar4wildfire")
task_dict.update({filename: {'id': task_id}})
""" check upload status """
print("=============> check upload status <===============")
upload_finish_flag = False
while(not upload_finish_flag):
print("-------------------------------------------------------")
time.sleep(10) # delay 30s
upload_finish_flag = True
for filename in task_dict.keys():
asset_id = f"users/omegazhangpzh/NRT_AF/{filename}"
task_id = task_dict[filename]['id']
check_upload_status = f"earthengine task info {task_id}"
response = subprocess.getstatusoutput(check_upload_status)[1]
state = response.split("\n")[1].split(": ")[-1]
# state = edict(json.loads(response))['state']
task_dict[filename].update({'state': state})
if state == "COMPLETED":
os.system(f"earthengine acl set public {asset_id}")
# elif state == "FAILED":
# url_postfix = [i for i in firms if filename in firms[i]][0]
# url = nasa_website + url_postfix
# filename, task_id = download_and_upload(url, save_folder)
# task_dict.update({filename: {'id': task_id}})
else:
upload_finish_flag = False
# check_asset_permission(asset_id)
print(f"{asset_id}: {state}")
print()
# pprint(task_dict)
""" set asset public """
print("=============> set asset public <===============")
asset_list = NRT_AF[1].replace("projects/earthengine-legacy/assets/", "").split("\n")
for asset_id in asset_list:
public_flag = check_asset_permission(asset_id)
if not public_flag:
os.system(f"earthengine acl set public {asset_id}")
def upadte_active_fire_archived(code=215922, year=2021):
nasa_website = "https://firms.modaps.eosdis.nasa.gov"
# save_folder = Path("D://firms-active-fire/outputs")
save_folder = Path(os.getcwd()) / "outputs"
if not os.path.exists(save_folder): os.makedirs(save_folder)
firms = [
f"/data/download/DL_FIRE_M-C61_{code}.zip",
f"/data/download/DL_FIRE_J1V-C2_{code+1}.zip",
f"/data/download/DL_FIRE_SV-C2_{code+2}.zip",
]
asset_names= [
f"MODIS_C6_1_Global_Archived_{year}",
f"J1_VIIRS_C2_Global_Archived_{year}",
f"SUOMI_VIIRS_C2_Global_Archived_{year}"
]
NRT_AF = subprocess.getstatusoutput("earthengine ls users/omegazhangpzh/NRT_AF/")
# pprint(asset_list)
""" Download and Upload into GEE """
print("=============> Download and Upload into GEE <===============")
task_dict = {}
for i in range(len(firms[-1:])): # -------------------------------------
url = nasa_website + firms[i]
print(url)
filename, task_id = download_and_upload(url, save_folder, bucket="sar4wildfire", asset_name=asset_names[i])
task_dict.update({filename: {'id': task_id}})
""" check upload status """
print("=============> check upload status <===============")
upload_finish_flag = False
while(not upload_finish_flag):
print("-------------------------------------------------------")
time.sleep(10) # delay 30s
upload_finish_flag = True
for filename in task_dict.keys():
asset_id = f"users/omegazhangpzh/NRT_AF/{filename}"
task_id = task_dict[filename]['id']
check_upload_status = f"earthengine task info {task_id}"
response = subprocess.getstatusoutput(check_upload_status)[1]
state = response.split("\n")[1].split(": ")[-1]
# state = edict(json.loads(response))['state']
task_dict[filename].update({'state': state})
if state == "COMPLETED":
os.system(f"earthengine acl set public {asset_id}")
# elif state == "FAILED":
# url_postfix = [i for i in firms if filename in firms[i]][0]
# url = nasa_website + url_postfix
# filename, task_id = download_and_upload(url, save_folder)
# task_dict.update({filename: {'id': task_id}})
else:
upload_finish_flag = False
# check_asset_permission(asset_id)
print(f"{asset_id}: {state}")
print()
# pprint(task_dict)
""" set asset public """
print("=============> set asset public <===============")
asset_list = NRT_AF[1].replace("projects/earthengine-legacy/assets/", "").split("\n")
for asset_id in asset_list:
public_flag = check_asset_permission(asset_id)
if not public_flag:
os.system(f"earthengine acl set public {asset_id}")
def set_AF_date(feat):
return feat.set("af_date", ee.Date(feat.get("ACQ_DATE")).format().slice(0,19))
if __name__ == "__main__":
from datetime import datetime
# while(True):
now = datetime.now()
current_time = datetime.now().strftime("%H:%M:%S")
# if current_time == "07:00:00":
# time_split = current_time.split(":")
# print(time_split)
# if (int(time_split[0]) % 3 == 0) and (int(time_split[1])==0) and (int(time_split[2])==0):
# upadte_active_fire(period_list=['24h', '7d', '48h']) #
upadte_active_fire_archived(code=351892 , year=2023)
AF_SUOMI_VIIRS = ee.FeatureCollection("users/omegazhangpzh/NRT_AF/SUOMI_VIIRS_C2_Global_24h")
AF = AF_SUOMI_VIIRS.map(set_AF_date)
print(f"\n------------------> update time: {current_time} <-------------------")
print(AF.aggregate_array("af_date").distinct().sort().getInfo()[-1])
# time.sleep(60*60) # sleep 1h