-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
156 lines (142 loc) · 5.67 KB
/
Program.cs
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
using System;
using System.Collections;
using System.Globalization;
using CommandLine;
namespace DriftsHelper // Note: actual namespace depends on the project name.
{
internal class Program
{
static void ProcessFolder(Options o, string folderPath, string fileName)
{
Console.WriteLine($"Scanning input dir {folderPath}");
CsvProvider p = new(folderPath);
Console.WriteLine("Running preprocessor...");
Processing e = new(p);
List<IntegrationResult>? results = null;
if (o.Regions != null && o.Regions.Any())
{
Console.WriteLine("Integrating...");
results = new();
foreach (var item in o.Regions)
{
try
{
var splt = item.Split(',');
var reg = new Region(splt[0], splt[1]);
Console.Write("Method: ");
Console.WriteLine(o.PeakInsteadOfIntegrate ? "Peak" : "Integrate");
results.Add(o.PeakInsteadOfIntegrate ?
e.PeakSpectra(reg.Start, reg.Stop) :
e.IntegrateSpectra(reg.Start, reg.Stop));
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
List<Spectrum>? diffSpectra = null;
if (o.DifferenceSpectraPairs != null && o.DifferenceSpectraPairs.Any())
{
Console.WriteLine("Subtracting...");
diffSpectra = new List<Spectrum>();
foreach (var item in o.DifferenceSpectraPairs)
{
try
{
var spltNameIndexes = item.Split('=');
var spltIndexes = spltNameIndexes[1].Split(',');
if (spltIndexes.Length < 2) throw new ArgumentException($"Warning: malformed diff spectra argument '{item}'!");
diffSpectra.Add(e.SubtractSpectra(int.Parse(spltIndexes[0]), int.Parse(spltIndexes[1]), spltNameIndexes[0]));
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
Console.WriteLine("Writing output file...");
try
{
if (results != null) Storage.StoreIntegralCurves(folderPath, fileName, results);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
try
{
if (diffSpectra != null) Storage.StoreDiffSpectra(folderPath, fileName, diffSpectra);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
static void Main(string[] args)
{
Parser.Default.ParseArguments<Options>(args).WithParsed((o) => {
if (o.ParentFolder != null)
{
o.FolderPath = Directory.GetDirectories(o.ParentFolder);
}
if (o.FolderPath != null)
{
foreach (var item in o.FolderPath)
{
const string checkForInnerFolder = "spectra";
try
{
string p;
var subdirs = Directory.EnumerateDirectories(item);
if (subdirs.Any(x => x.EndsWith(checkForInnerFolder)))
{
Console.WriteLine("Folder was found to contain a subfolder 'spectra', assuming nested folders.");
p = Path.Combine(item, checkForInnerFolder);
}
else
{
p = item;
}
ProcessFolder(o, p, o.OutputFileName ?? $"{new DirectoryInfo(item).Name}.csv");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
Console.WriteLine("Done.");
});
}
}
class Region
{
public Region(string start, string stop) :
this(double.Parse(start, CultureInfo.InvariantCulture), double.Parse(stop, CultureInfo.InvariantCulture))
{
}
public Region(double start, double stop)
{
Start = start;
Stop = stop;
}
public double Start {get;}
public double Stop {get;}
}
class Options
{
[Option('p', "parent", Required = false)]
public string? ParentFolder {get;set;}
[Option('f', "folder", Required = false)]
public IEnumerable<string>? FolderPath {get;set;}
[Option('r', "regions", Required = false, Default = null)]
public IEnumerable<string>? Regions {get;set;}
[Option('o', "output", Required = false)]
public string? OutputFileName {get;set;}
[Option('m', "method")]
public bool PeakInsteadOfIntegrate {get;set;}
[Option('d', "diff", Required = false, Default = null)]
public IEnumerable<string>? DifferenceSpectraPairs {get;set;}
}
}