-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldfsl_utils.cpp
160 lines (134 loc) · 3.62 KB
/
ldfsl_utils.cpp
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
/*
* ldfsl_utils.cpp
*
* Created on: Jul 10, 2013
* Author: laughlin
*
* Utility functions for ldfsl
*/
#include <ctime>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <dirent.h>
#include "ldfsl_utils.h"
void systemCall(const std::string &command) {
int ret_val = system(command.c_str());
if (ret_val != 0) {
std::cerr << "Error: \nCommand issued: " << command << std::endl;
std::cerr << "Value returned: " << ret_val << std::endl;
exit(EXIT_FAILURE);
}
}
void systemCall(const std::string &command, const std::string &message) {
std::cout << message << std::endl;
systemCall(command);
}
void printElapsedTime(time_t start) {
using std::cout;
using std::endl;
time_t now = time(nullptr);
int elapsed = static_cast<int>(difftime(now, start));
int hours = elapsed / 3600;
elapsed -= hours * 3600;
int mins = elapsed / 60;
int secs = elapsed - mins * 60;
cout << "Time elapsed: " << hours << "h " << mins << "m " << secs
<< "s" << endl;
}
// warning this will work for 64 directions only
bool bedpost_complete(const std::string &bedpost_dir) {
using std::cerr;
using std::cout;
using std::endl;
using std::string;
using std::vector;
static bool initialised = false;
static const string logdir = bedpost_dir + "/logs";
static vector<string> logsLeft;
static int bpxNum = -1;
static int bedpostNum = -1;
static string bpxName = "bpx_postproc.o";
static string bedpostName = "bedpostx.o";
if (!initialised) {
// find the filenames of the bedpostx files
DIR *dirStream;
struct dirent *dirEntry;
if ((dirStream = opendir(logdir.c_str())) == NULL) {
cout << "Could not open log directory" << endl;
exit(1);
}
while((dirEntry = readdir(dirStream))) {
string entryString = dirEntry->d_name;
if (bpxNum == -1) {
auto found = entryString.find(bpxName);
if (found != string::npos) {
string digits = entryString.substr(bpxName.size(),
entryString.size() - bpxName.size());
bpxNum = stoi(digits);
bpxName.append(digits);
}
}
if (bedpostNum == -1) {
auto found = entryString.find(bedpostName);
if (found != string::npos) {
auto secondDot = entryString.find(".", found
+ bedpostName.size());
string digits = entryString.substr(bedpostName.size(),
secondDot - bedpostName.size());
bedpostNum = stoi(digits);
bedpostName.append(digits).append(".");
}
}
}
// set up our vector of logs to check
for (int i = 1; i <= 65; i++) {
string digit = std::to_string(i);
string filename = logdir + "/" + bedpostName + digit;
logsLeft.push_back(filename);
}
initialised = true;
}
// check each bedpostx log to see if reports done.
auto iter = logsLeft.begin();
while (iter != logsLeft.end()) {
std::ifstream file(*iter);
if (file) {
string data;
if ((file >> data) && (data == "Done")) {
string filename = *iter;
cout << filename << " complete." << endl;
iter = logsLeft.erase(iter);
} else {
++iter;
}
} else {
string s = *iter;
cerr << "Failed to open file " << s << endl;
exit(1);
}
}
cout << "logs left: " << logsLeft.size() << endl;
// if there are no bedpostx logs left to check, check bpx_postproc
if (logsLeft.size() == 0) {
string filename = logdir;
filename.append("/").append(bpxName);
std::ifstream file(filename);
if (file) {
string data;
while (file >> data) {
if (data.find("Done") != string::npos) {
cout << filename << " complete." << endl;
return true;
}
}
} else {
string s = *iter;
cerr << "Failed to open file " << s << endl;
exit(1);
}
}
return false;
}