-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
238 lines (196 loc) · 9.6 KB
/
Program.cs
File metadata and controls
238 lines (196 loc) · 9.6 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
230
231
232
233
234
235
236
237
238
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using BitMiracle.LibTiff.Classic;
using Warp;
using Warp.Headers;
using Warp.Tools;
namespace stacker2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Path to stacks:");
string FolderPath = Console.ReadLine();
if (FolderPath[FolderPath.Length - 1] != '\\' || FolderPath[FolderPath.Length - 1] != '/')
FolderPath += "\\";
Console.WriteLine("Reading stacks from " + FolderPath + "\n");
Console.WriteLine("Look for files recursively in subfolders? (y/n)");
bool DoRecursiveSearch = Console.ReadLine().ToLower().Contains("y");
Console.WriteLine($"Files will {(DoRecursiveSearch ? "" : "not ")}be searched recursively.\n");
Console.WriteLine("Output folder (leave empty if identical with input):");
string OutputPath = Console.ReadLine();
if (string.IsNullOrEmpty(OutputPath))
OutputPath = FolderPath;
if (OutputPath[OutputPath.Length - 1] != '\\' || OutputPath[OutputPath.Length - 1] != '/')
OutputPath += "\\";
Console.WriteLine("Writing compressed stacks to " + OutputPath + "\n");
Console.WriteLine("Compress to TIFF (c), or just move (m)? (c/m)");
bool Compress = Console.ReadLine().ToLower().Contains("c");
Console.WriteLine($"Files will be {(Compress ? "written as compressed TIFFs" : "just moved")}.\n");
string Extension = "mrc";
if (!Compress)
{
Console.WriteLine("What is the input file extension?");
Extension = Console.ReadLine().ToLower();
if (Extension[0] == '*')
Extension = Extension.Substring(1);
if (Extension[0] == '.')
Extension = Extension.Substring(1);
Console.WriteLine($"Using *.{Extension} as input file extension.\n");
}
Console.WriteLine("Number of frames:");
string FramesString = Console.ReadLine();
int NFrames;
try
{
NFrames = int.Parse(FramesString);
Console.WriteLine("Using " + NFrames + " frames.\n");
}
catch (Exception)
{
return;
}
Console.WriteLine("Delete original files after completion? (y/n)");
bool DeleteWhenDone = Console.ReadLine().ToLower().Contains("y");
Console.WriteLine($"Original files will {(DeleteWhenDone? "" : "not ")}be deleted.\n");
if (!DeleteWhenDone)
Directory.CreateDirectory(FolderPath + "original");
Console.WriteLine("Delete superfluous gain references? (y/n)");
bool DeleteExtraGain = Console.ReadLine().ToLower().Contains("y");
Console.WriteLine($"Superfluous gain references will {(DeleteWhenDone ? "" : "not ")}be deleted.\n");
if (!DeleteWhenDone)
Directory.CreateDirectory(FolderPath + "original");
Console.WriteLine("Number of stacks to be processed in parallel (4 is a good value):");
int NParallel = int.Parse(Console.ReadLine());
Console.WriteLine($"{NParallel} stacks will be processed in parallel.\n");
GC.AddMemoryPressure(20 * ((long)1 << 30));
List<string> HaveBeenProcessed = new List<string>();
while (true)
{
List<string> FrameNames = new List<string>();
List<string> GainRefNames = new List<string>();
foreach (var filename in Directory.EnumerateFiles(FolderPath, "*." + Extension,
DoRecursiveSearch ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly))
{
try
{
MapHeader Header = MapHeader.ReadFromFile(filename);
if (Header.Dimensions.Z != NFrames)
{
if (Header.Dimensions.Z == 1)
GainRefNames.Add(filename);
continue;
}
if (HaveBeenProcessed.Contains(filename))
continue;
if (DeleteWhenDone)
FrameNames.Add(filename);
else if (!filename.Contains("\\original\\"))
FrameNames.Add(filename);
}
catch
{
}
}
int NFiles = FrameNames.Count;
if (NFiles == 0)
{
Thread.Sleep(1000);
continue;
}
Console.WriteLine("Found " + NFiles + " new stacks.");
Thread.Sleep(1000);
SemaphoreSlim WritingSemaphore = new SemaphoreSlim(NParallel);
for (int f = 0; f < NFiles; f++)
{
string FrameName = FrameNames[f];
MapHeader Header = MapHeader.ReadFromFilePatient(500, 100, FrameName, new int2(1), 0, typeof(float));
if (Compress)
{
Image StackOut = new Image(Header.Dimensions);
float[][] StackOutData = StackOut.GetHost(Intent.Read);
for (int n = 0; n < NFrames; n++)
{
Image Frame = Image.FromFilePatient(50, 100, FrameName, n);
float[] FrameData = Frame.GetHost(Intent.Read)[0];
if (Compress)
for (int i = 0; i < FrameData.Length; i++)
StackOutData[n][i] = (float)Math.Max(0, Math.Min(255, Math.Round(FrameData[i])));
else
for (int i = 0; i < FrameData.Length; i++)
StackOutData[n][i] = FrameData[i];
Frame.Dispose();
Console.Write(".");
}
Console.WriteLine("");
HaveBeenProcessed.Add(FrameName);
string RootName = Helper.PathToName(FrameName);
Thread WriteThread = new Thread(() =>
{
try
{
//if (Compress)
StackOut.WriteTIFF(OutputPath + RootName + ".tif", 1, typeof(byte));
//else
// StackOut.WriteMRC(OutputPath + RootName + ".mrc", 1, true);
StackOut.Dispose();
if (DeleteWhenDone)
File.Delete(FrameName);
else
File.Move(FrameName, FolderPath + "original/" + Helper.PathToNameWithExtension(FrameName));
}
catch (Exception exc)
{
Console.WriteLine("ERROR: Could not write " + RootName);
Console.WriteLine(exc);
HaveBeenProcessed.Remove(FrameName);
}
WritingSemaphore.Release();
GC.Collect();
});
while (WritingSemaphore.CurrentCount < 1)
Thread.Sleep(100);
WritingSemaphore.Wait();
WriteThread.Start();
Console.WriteLine("Done reading: " + RootName);
}
else
{
bool Success = false;
while (!Success)
{
try
{
string NameOut = OutputPath + Helper.PathToNameWithExtension(FrameName);
if (DeleteWhenDone)
File.Move(FrameName, NameOut);
else
{
File.Copy(FrameName, NameOut);
File.Move(FrameName, FolderPath + "original/" + Helper.PathToNameWithExtension(FrameName));
}
HaveBeenProcessed.Add(FrameName);
Success = true;
Console.WriteLine("Done moving: " + Helper.PathToNameWithExtension(FrameName));
}
catch (Exception exc)
{
Console.WriteLine("Something went wrong moving " + Helper.PathToNameWithExtension(FrameName) + ":\n" + exc.ToString());
}
}
}
}
if (DeleteExtraGain)
foreach (var gainRefName in GainRefNames)
File.Delete(gainRefName);
Thread.Sleep(1000);
}
}
}
}