-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoving_tifs.cpp
More file actions
229 lines (177 loc) · 6.89 KB
/
moving_tifs.cpp
File metadata and controls
229 lines (177 loc) · 6.89 KB
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
/*
this program takes in a csv file
first creates replicate_# directories
then it fills them up with empty files indicated by the csv file.
*/
#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <sys/types.h>
#include <experimental/filesystem>
#include <string>
#include <vector>
using namespace std;
/*
File takes the given spreadsheet and rearranges items accordingly
usage:
$ g++ main.cpp
$ ./a.out
*/
void print_vector(vector<string> v) {
for (int i = 0; i < v.size(); i++) {
cout << v.at(i) << "\t";
}
cout << endl;
}
string add_zeros(string s){
int len = s.length();
for (int i = 0; i < (3 - (len % 3)) % 3 ; i++)
s = "0" + s;
return s;
}
string create_filename(string n){
// where n is a number, i.e 1, 3, 5..
string filename, prefix, suffix;
suffix = to_string(stoi(n) + 1);
prefix = add_zeros(n);
suffix = add_zeros(suffix) + ".tif";
filename = prefix + "-" + suffix;
// now to do the same with
return filename;
}
void create(vector<vector<string>> files){
/* creates replicate_# directories and "fake" .tiff text files -
for the sake of testing this code on smaller tif files. */
vector<string> directories; /* holds the strings to replicate_#*/
for (int i = 1; i <= 4; i++){
string fname = files.at(0).at(i);
directories.push_back(fname);
// cout << "fname\" "<< fname << "\" len: " << fname.length() << endl;
char fname_char[fname.length() + 1];
strcpy(fname_char, fname.c_str());
if (mkdir(fname_char, 0777) == -1)
cerr << "Error : " << strerror(errno) << endl;
// else cout << "Directory created";
}
// takes int and creates a file with it inside each replicant
// the content of the files is n, 6, or 8
// cout << "directories: ";
// print_vector(directories);
for (int row = 1; row < files.size(); row++){
string file_int = files.at(row).at(0);
for (int dir = 0; dir < directories.size(); dir++){
string file_content = files.at(row).at(dir + 1);
if (!file_content.empty()) {
string filename = directories.at(dir) + "/";
filename = filename + create_filename(file_int);
ofstream tif;
tif.open(filename);
// tif << file_content << "\n"; // putting in file content is not needed.
tif.close();
}
}
}
}
vector<vector<string>> file_to_vector(string file) {
/* takes in a string pointing to a csv file
that is then put into a 2d vector and returned */
ifstream f(file);
vector<vector<string>> v;
if(f.is_open()) {
string line, val;
// creating 2d array using vector
while(getline(f, line)){
int count = 0;
vector<string> n;
stringstream s (line);
while(getline(s, val, ',')){
count++;
if (count == 5) {
n.push_back(val.substr(0, val.length()-1));
} else {
n.push_back(val);
}
}
v.push_back(n);
// cout << line << "\n";
}
f.close(); // file has been read, can be closed
} else {
cout << "file did not open" << endl;
}
return v;
}
void create_directory(string s){
// cout << "dirname: " << s;
char dir[s.length() + 1];
strcpy(dir, s.c_str());
if (mkdir(dir, 0777) == -1)
cerr << "Error : " << strerror(errno) << endl;
// else cout << "Directory created\n";
}
void sort_files(vector<vector<string>> v){
vector<string> directories;
for (int i = 1; i <= 4; i++){
string fname = v.at(0).at(i);
directories.push_back(fname);
create_directory(fname + "/N2");
create_directory(fname + "/SYGL-1_OLLAS_DG5136");
create_directory(fname + "/SYGL-1_OLLAS_DG5249");
}
for (int row = 1; row < v.size(); row++){
string file_int = v.at(row).at(0);
for (int dir = 0; dir < directories.size(); dir++){
string file_content = v.at(row).at(dir + 1);
if (!file_content.empty()) {
// then file exists:
string filename = directories.at(dir) + "/";
string prefix = filename; // holds on to replicate_#/
string suffix = create_filename(file_int);
filename = filename + suffix;
char char_filename[filename.length()+1];
// char fname_char[fname.length() + 1];
strcpy(char_filename, filename.c_str());
// change filename to move it.
string new_filename;
if(!file_content.compare("n")) {
// set folder name to replicate_#/N2/
new_filename = prefix + "N2/" + suffix;
char char_new_filename[filename.length()+new_filename.length()];
strcpy(char_new_filename, new_filename.c_str());
if(rename(char_filename, char_new_filename) < 0){
cout << strerror(errno) << endl;
}
} else if(!file_content.compare("6")) {
// set folder name to replicate_#/N2/
new_filename = prefix + "SYGL-1_OLLAS_DG5136/" + suffix;
char char_new_filename[filename.length()+new_filename.length()];
strcpy(char_new_filename, new_filename.c_str());
if(rename(char_filename, char_new_filename) < 0){
cout << strerror(errno) << endl;
}
} else if (!file_content.compare("8")) {
// set folder name to replicate_#/N2/
new_filename = prefix + "SYGL-1_OLLAS_DG5249/" + suffix;
char char_new_filename[filename.length()+new_filename.length()];
strcpy(char_new_filename, new_filename.c_str());
if(rename(char_filename, char_new_filename) < 0){
cout << strerror(errno) << endl;
}
} else {
// do nothing
}
}
}
}
}
int main() {
// string num = "1"; // test code fo create_filename func.
// cout << "filename: " << create_filename(num) << endl;
vector<vector<string>> csv;
csv = file_to_vector("06.11.2021_DG5248_GFP_OLLAS_MSP.csv");
create(csv); /* creates fake directories and
correspoding files in them to organize*/
sort_files(csv);
return 0;
}