This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
4.0instrument_level_availability.py
175 lines (144 loc) · 8.71 KB
/
4.0instrument_level_availability.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
#! /usr/local/bin/python
"""
Created on Mon July 25 2017
@author: leilabbb
"""
from __future__ import division
import datetime
import pandas as pd
import os
import time
start_time = time.time()
'''
This script recreate the ingestion and deployment sheets
'''
# select the platform
platform = 'CP01CNSM'
# path to baseline file
maindir = '/Users/leila/Documents/OOI_GitHub_repo/work/ingest-status/000_ingestpy_run_results/'
rootdir = maindir + platform + '/data/'
# select the ingestion file example _D00003_ingest.csv or leave it as generic _ingest.csv
key_file = '_P.csv'
infrastructure = rootdir + platform + '_infrastructure.csv'
# headers' name for ingestion files
#col_header= ['reference_designator', 'data_source', 'deployment#', 'Available', 'percenta','Missing','percentm','Active Deployment','Start','End']
col_header_info = ['reference_designator', 'data_source', 'deployment#','Automated_status','status']
df = pd.DataFrame()
df1 = pd.DataFrame()
with open(os.path.join(infrastructure), 'r') as base_file:
filebase = pd.read_csv(base_file)
refdes_list = list(pd.unique(filebase['refdes_list'].ravel()))
for item in os.listdir(rootdir):
if item.startswith(platform):
if item.endswith(key_file):
if os.path.isfile(os.path.join(rootdir, item)):
with open(os.path.join(rootdir, item), 'r') as csv_file:
filereader = pd.read_csv(csv_file)
filereader.fillna('', inplace=True)
status_list = list(pd.unique(filereader['Automated_status'].ravel()))
deploy_list = list(pd.unique(filereader['deployment#'].ravel()))
for refdesx in refdes_list:
print refdesx
indx = filebase.loc[(filebase['refdes_list'] == refdesx)]
method_list = list(pd.unique(indx['method_list'].ravel()))
#df = pd.DataFrame(columns=col_header)
df_info = pd.DataFrame(columns=col_header_info)
ind0 = filereader.loc[(filereader['reference_designator'] == refdesx)]
df_info = df_info.append(ind0)
outfile1 = maindir + platform + '/statistics/instrument/' + refdesx + '_availability_info.csv'
df_info.to_csv(outfile1, index=False, columns=col_header_info, na_rep='', encoding='utf-8')
for methodx in method_list:
dfa = pd.DataFrame(index=[methodx])
dfp = pd.DataFrame(index=[methodx])
dfm = pd.DataFrame(index=[methodx])
ind1 = ind0.loc[(ind0['data_source'] == methodx)]
for deployx in deploy_list:
ind2 = ind1.loc[(ind1['deployment#'] == deployx)]
total = len(ind2)
if total != 0:
start = ind2['startDateTime'].values[0]
end = ind2['stopDateTime'].values[0]
if deployx == deploy_list[len(deploy_list) - 1]:
try:
if end is '':
note = 'TRUE' # active deployment
except IndexError:
note = ''
else:
note = 'x'
else:
if deployx == deploy_list[len(deploy_list) - 1]:
note = 'TRUE'
indy = filereader.loc[(filereader['deployment#'] == deploy_list[len(deploy_list) - 1])]
start = indy['startDateTime'].values[0]
end = indy['stopDateTime'].values[0]
else:
note = 'Missing'
start = ''
end = ''
counta = 0
countm = 0
countp = 0
for statusx in status_list:
#print statusx
ind3 = ind2.loc[(ind2['Automated_status'] == statusx)]
count = len(ind3)
if statusx == 'Not Deployed':
# print 'Not Deployed found ', len(ind2)
counta += len(ind3)
if statusx == 'Not Expected':
# print 'Not Expected found ', len(ind2)
counta += len(ind3)
if statusx == 'Available':
# print 'Available found ', len(ind2)
counta += len(ind3)
if statusx == 'Missing':
# print 'Missing found ', len(ind2)
countm = len(ind3)
if statusx == 'Pending':
# print 'Pending found ', len(ind2)
countp = len(ind3)
if total != 0:
percent = round((count/total) * 100)
else:
percent = total
if total != 0:
percenta = round((counta/total) * 100)
percentm = round((countm/total) * 100)
percentp = round((countp/total) * 100)
else:
percenta = total
percentm = total
percentp = total
# print methodx, '...', deployx, 'Available', counta, '...', percenta, '%', '....', note
# print methodx, '...', deployx, 'Missing', countm, '...', percentm, '%', '....', note
#df1 = pd.DataFrame([[refdesx, methodx, deployx, counta, percenta, countm, percentm, note, start, end]], columns=col_header)
#df = df.append(df1)
#print 'df', df.values
print deployx, start, end
if start is not '':
start = datetime.datetime.strptime(start, "%Y-%m-%dT%H:%M:%S")
start_x = 'D0' + str(deployx) + '_' + str(start.year) + '-' + str(start.month)
else:
start_x = 'D0' + str(deployx) + '_'
if end is not '':
end = datetime.datetime.strptime(end, "%Y-%m-%dT%H:%M:%S")
end_x = 'D0' + str(deployx) + '_' + str(end.year) + '-' + str(end.month)
else:
end_x = 'D0' + str(deployx) + '_'
dfa['status'] = pd.Series(['Available'], index=[methodx])
dfa[start_x] = pd.Series([percenta], index=[methodx])
dfa[end_x] = pd.Series([percenta], index=[methodx])
dfp['status'] = pd.Series(['Pending'], index=[methodx])
dfp[start_x] = pd.Series([percentp], index=[methodx])
dfp[end_x] = pd.Series([percentp], index=[methodx])
dfm['status'] = pd.Series(['Missing'], index=[methodx])
dfm[start_x] = pd.Series([percentm], index=[methodx])
dfm[end_x] = pd.Series([percentm], index=[methodx])
df1 = df1.append(dfa)
df1 = df1.append(dfp)
df1 = df1.append(dfm)
#print df1.values
outfile0 = maindir + platform + '/statistics/instrument/' + refdesx + '_availability_state.csv'
# print outputfileind3['status'].values
df1.to_csv(outfile0, index=True, na_rep='', encoding='utf-8')