-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncfirms.py
92 lines (85 loc) · 2.88 KB
/
syncfirms.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
#!/usr/bin/env python
from __future__ import print_function, unicode_literals
import datetime as dt
import os
import glob
import shutil
import ftputil
import argparse
from credentials import FIRMS
from time import sleep
SERVER = FIRMS["server"]
UN = FIRMS['username']
PW = FIRMS['cred']
RPTH0 = "FIRMS"
PROD = {'MODIS': 'c6',
'VIIRS-I': 'viirs'
}
LOCPTH = ['Alaska', 'Canada']
LOCALDIR = "/center1/UAFSMOKE/UAFSMOKE/dat/fires_data/"
DEFAULTDAYS = 3
ARCHIVE = 'archive'
def parse_arguments():
"""Parse arguments"""
parser = argparse.ArgumentParser(description='Sync near real-time (NRT) fire products from FIRMS')
parser.add_argument('--days',
help='how many most recent days to retrieve',
default=DEFAULTDAYS,
type=int)
parser.add_argument('--archive',
help='archive all existing',
action='store_true')
parser.add_argument('--nc',
help= 'do not clobber',
action='store_true')
return parser.parse_args()
def getfiles(prod='c6', area="Alaska", days=DEFAULTDAYS, noclobber=False):
localfiles = os.listdir('.')
selectfiles = []
today = dt.datetime.utcnow()
daypatt = []
for delta in range(days):
newjulian = (today - dt.timedelta(days=delta)).strftime('%Y%j.txt')
daypatt.append(newjulian)
with ftputil.FTPHost(SERVER, UN, PW) as host:
path = os.path.join(RPTH0, prod, area)
host.chdir(path)
names = host.listdir(host.curdir)
for patt in daypatt:
for name in names:
if name.endswith(patt):
if noclobber:
if name in localfiles:
continue
selectfiles.append(name)
for fn in selectfiles:
host.download(fn, fn)
print("Retrieved {}".format(fn))
def main():
args = parse_arguments()
if args.archive:
for prodname in PROD:
workingdir = os.path.join(LOCALDIR, RPTH0+'_'+prodname)
os.chdir(workingdir)
files = glob.glob('*.txt')
print(files)
for fn in files:
if os.path.exists(os.path.join(workingdir, ARCHIVE, fn)):
os.remove(os.path.join(workingdir, ARCHIVE, fn))
shutil.move(fn, os.path.join(workingdir, ARCHIVE))
print("Archived {}.".format(fn))
while True:
try:
for prodname in PROD:
newdir = os.path.join(LOCALDIR, RPTH0+'_'+prodname)
os.chdir(newdir)
for area in LOCPTH:
print("Retrieving {}, {}".format(prodname, area))
getfiles(prod=PROD[prodname], area=area, days=args.days, noclobber=args.nc)
except ftputil.error.FTPIOError:
print("Retrying...")
sleep(5)
else:
return
if __name__ == "__main__":
main()