-
Notifications
You must be signed in to change notification settings - Fork 6
/
AUSPOS_fetch.py
143 lines (111 loc) · 3.89 KB
/
AUSPOS_fetch.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
# ----------------------------------------------------------------------
# AUSPOS_fetch.py
# ----------------------------------------------------------------------
# Author: Nicholas Gowans
# Date: 23 AUG 2019
# Purpose: To read output from AUSPOS_submission.py, and retrieve the
# AUSPOS .snx and .pdf results from the ftp server.
# ----------------------------------------------------------------------
# Usage: CMD:\>python AUSPOS_fetch.py <AUSPOS_submission.py_results.csv>
# ----------------------------------------------------------------------
# Notes:
# ----------------------------------------------------------------------
import os
import ftplib
import shutil
import time
import timeit
import datetime
start = timeit.default_timer()
# ----------------------------------------------------------------------
# Consume AUSPOS_submission results
# ----------------------------------------------------------------------
results_name = os.sys.argv[1]
results_dict = {}
results_fh = open(results_name, 'r')
# read input file to dictionary
for line in results_fh:
items = line.split(',')
rnx_file = items[0]
HI = items[1]
ant = items[2]
job_ref = items[3].strip()
results_dict[rnx_file] = {
'HI': HI,
'ant': ant,
'job_ref': job_ref
}
# ----------------------------------------------------------------------
# Create results directory
# ----------------------------------------------------------------------
results_dir = 'AUSPOS_fetch'
# check if directory already exists, prompt user to delete
if os.path.isdir(results_dir):
print('')
print(' *** AUSPOS_fetch directory exists already ***')
answer = input(' Delete and continue? Y/N\n ')
if answer.upper() == 'Y':
shutil.rmtree(results_dir, ignore_errors=True)
time.sleep(2)
else:
print()
print(' Exiting script...')
print()
exit()
os.mkdir(results_dir)
os.chdir(results_dir)
# ----------------------------------------------------------------------
# Connect to ftp and search for AUSPOS results
# ----------------------------------------------------------------------
# initialise counters
sessionsFd = 0
sessionsNotFd = 0
# connect to ftp
ftp = ftplib.FTP('ftp.ga.gov.au')
ftp.login()
AUSPOS_address = 'geodesy-outgoing/apps/ausposV2/'
ftp.cwd(AUSPOS_address)
# retrieve results and move to relevant folders
for s in results_dict:
# create sub-directory for each session
job_ref = results_dict[s]['job_ref'][-4:]
directoryName = '{:s}_{:s}'.format(s, job_ref)
os.mkdir(directoryName)
os.chdir(directoryName)
# look for results on ftp, else rename local directory as not found
try:
ftp.cwd(job_ref)
sessionsFd += 1
except:
sessionsNotFd += 1
os.chdir('..')
os.rename(directoryName, directoryName + '_ftp_notFound')
# create listing of ftp files
ftp_data = []
ftp.dir(ftp_data.append)
# sift through ftp listing, and download snx and pdf files
for line in ftp_data:
ftp_file = line[55:].strip()
if '.SNX' in ftp_file:
ftp.retrbinary('RETR ' + ftp_file, open(ftp_file, 'wb').write)
elif '.pdf' in ftp_file:
pdf_name = s + '_AUSPOS_Report.pdf'
ftp.retrbinary('RETR ' + ftp_file, open(pdf_name, 'wb').write)
# move back to parent directories
ftp.cwd('..')
os.chdir('..')
# ----------------------------------------------------------------------
# Print Summary
# ----------------------------------------------------------------------
stop = timeit.default_timer()
time_diff = stop - start
run_time = str(datetime.timedelta(seconds=time_diff))
print()
print('-'*50)
print(' AUSPOS_fetch.py Summary Report')
print('-'*50)
print()
print(' Completed in {:s}'.format(run_time[:-4]))
print()
print(' {:d} of {:d} session results retrieved successfully'.format(sessionsFd, len(results_dict)))
print()