|
| 1 | +using System; |
| 2 | +using System.CommandLine; |
| 3 | +using System.CommandLine.Invocation; |
| 4 | +using System.CommandLine.Parsing; |
| 5 | +using System.Globalization; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using NedfReader; |
| 9 | + |
| 10 | +class Program |
| 11 | +{ |
| 12 | + static void Main(string[] args) |
| 13 | + { |
| 14 | + var filesArg = new Argument<FileInfo[]>("files", "NEDF files to process").ExistingOnly(); |
| 15 | + filesArg.Arity = ArgumentArity.OneOrMore; |
| 16 | + var rootCommand = new RootCommand("Export header / marker data for NEDF files") |
| 17 | + { |
| 18 | + new Option<StreamWriter>(new string[]{"--statsfile", "-o" }, "File to save stats to instead of stdout"), |
| 19 | + new Option<StreamWriter>("--errlog", "File to log errors to instead of stderr"), |
| 20 | + new Option<uint>("--maxsamples", ()=>int.MaxValue, "Quit after this many samples, useful for sanity checks"), |
| 21 | + new Option<bool>("--markercsv", "Write a CSV file with markers for each supplied nedf file"), |
| 22 | + filesArg |
| 23 | + }; |
| 24 | + rootCommand.Handler = CommandHandler.Create((StreamWriter statsfile, StreamWriter errlog, uint maxsamples, bool markercsv, FileInfo[] files) => |
| 25 | + { |
| 26 | + var stderr = errlog ?? Console.Error; |
| 27 | + var statsout = statsfile ?? Console.Out; |
| 28 | + |
| 29 | + statsout.WriteLine("File;NEDFversion;nchan;nacc;nsample;nstim;StartDate_firstEEGTimestamp"); |
| 30 | + foreach (var file in files) |
| 31 | + try |
| 32 | + { |
| 33 | + using var r = new NedfFile(file.FullName, (str) => errlog.WriteLine(str)); |
| 34 | + string basename = Path.GetFileNameWithoutExtension(file.Name); |
| 35 | + int markercount = 0; |
| 36 | + if (markercsv) |
| 37 | + { |
| 38 | + var outfile = markercsv ? new StreamWriter(file.DirectoryName + '/' + basename + "_markers.csv") : null; |
| 39 | + outfile?.WriteLine("sample;marker"); |
| 40 | + foreach ((uint pos, uint value) in r.GetMarkerPairs(maxsamples)) |
| 41 | + { |
| 42 | + outfile?.WriteLine($"{pos};{value}"); |
| 43 | + markercount++; |
| 44 | + } |
| 45 | + outfile?.Dispose(); |
| 46 | + } |
| 47 | + CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; |
| 48 | + statsout.WriteLine(string.Join(";", new object[]{ |
| 49 | + Path.GetFileName(file.Name), |
| 50 | + r.NEDFversion, |
| 51 | + r.NChan, |
| 52 | + r.NAcc, |
| 53 | + r.NSample, |
| 54 | + markercount, |
| 55 | + r.hdr.StepDetails.StartDate_firstEEGTimestamp |
| 56 | + }.Select(obj=>obj.ToString()))); |
| 57 | + } |
| 58 | + catch (Exception e) |
| 59 | + { |
| 60 | + Console.Error.WriteLine($"{file}: Error: {e.Message}"); |
| 61 | + } |
| 62 | + }); |
| 63 | + rootCommand.Invoke(args); |
| 64 | + } |
| 65 | +} |
0 commit comments