示例#1
0
        /// <summary>
        /// Prints a progress bar.
        /// </summary>
        private void PrintProgress()
        {
            var asterisk = new string('*', Pct);
            var dash     = new string('-', 100 - Pct);

            WorkflowWriter.WriteLine($"|{asterisk}{Pct}%{dash}|");

            var now       = DateTime.UtcNow.Ticks;
            var span      = now - startTime;
            var perFile   = (double)span / currentCount;
            var remaining = (totalCount - currentCount) * perFile;

            WorkflowWriter.WriteLine($"{Remaining(TimeSpan.FromTicks((long)remaining))}");
        }
        /// <summary>
        /// Process the files.
        /// </summary>
        /// <param name="options">The options.</param>
        /// <param name="files">The files to process.</param>
        /// <returns>The filtered files.</returns>
        public AstroFile[] Process(Options options, AstroFile[] files)
        {
            int counter  = 0;
            int filtered = 0;
            int rejected = 0;
            int accepted = 0;

            bool IsJpegFilter(AstroFile file) =>
            file.FileExtension.StartsWith("jp");

            bool JpegFilter(AstroFile file) =>
            options.HasIgnoreFlag(IgnoreFlags.Jpeg) &&
            IsJpegFilter(file);

            bool IsTiffFilter(AstroFile file) =>
            file.FileExtension.StartsWith("tif");

            bool TiffFilter(AstroFile file) =>
            options.HasIgnoreFlag(IgnoreFlags.Tiff) &&
            IsTiffFilter(file);

            var(lastJpeg, lastTiff) = (string.Empty, string.Empty);

            bool ignoreAllButLast = options.HasIgnoreFlag(IgnoreFlags.AllButLast);

            foreach (var file in files)
            {
                counter++;
                if (file.IsProcessed)
                {
                    if (JpegFilter(file) || TiffFilter(file))
                    {
                        file.Valid = false;
                        filtered++;
                        continue;
                    }

                    if (ignoreAllButLast)
                    {
                        if (IsJpegFilter(file))
                        {
                            lastJpeg = file.SourcePath;
                        }
                        else if (IsTiffFilter(file))
                        {
                            lastTiff = file.SourcePath;
                        }
                    }
                }

                if (options.HasIgnoreFlag(IgnoreFlags.Rejection) == false &&
                    file.IsRaw)
                {
                    var match = files.Any(f => f.FileName == file.FileNameMatch);
                    file.Rejected = !match;
                    if (file.Rejected)
                    {
                        rejected++;
                        if (options.HasIgnoreFlag(IgnoreFlags.Rejected))
                        {
                            filtered++;
                            file.Valid = false;
                        }
                    }
                    else
                    {
                        accepted++;
                    }
                }
                else
                {
                    accepted++;
                }
            }

            if (ignoreAllButLast)
            {
                var filesToIgnore = files.Where(
                    f => f.IsProcessed &&
                    ((IsJpegFilter(f) && f.SourcePath != lastJpeg) ||
                     (IsTiffFilter(f) && f.SourcePath != lastTiff)));
                foreach (var file in filesToIgnore)
                {
                    file.Valid = false;
                }
            }

            WorkflowWriter.WriteLine($"Processed {counter} files: {accepted} accepted, {rejected} rejected, {filtered} filtered.");

            return(files);
        }
示例#3
0
 /// <summary>
 /// Send a message to the writer.
 /// </summary>
 /// <param name="msg">The message to send.</param>
 public static void WriteLine(string msg) => WorkflowWriter.writer(msg);
        /// <summary>
        /// Process the files to target.
        /// </summary>
        /// <param name="options">The <see cref="Options"/>.</param>
        /// <param name="files">The list of <see cref="AstroFile"/> to process.</param>
        /// <returns>The processed files.</returns>
        public AstroFile[] Process(Options options, AstroFile[] files)
        {
            var ds            = System.IO.Path.DirectorySeparatorChar;
            var directoryName = string.Empty;
            var rootDirectory = string.Empty;
            var fileName      = string.Empty;
            var imageIndex    = 1;
            var rejectedIndex = 1;

            Func <string> genFileName = options.TargetFilenameStrategy switch
            {
                TargetFilenameStrategy.New => () => options.NewFilename,
                TargetFilenameStrategy.Original => () => null,
                TargetFilenameStrategy.Ticks => () => DateTime.UtcNow.Ticks.ToString(),
                TargetFilenameStrategy.TicksHex => () => DateTime.UtcNow.Ticks.ToString("x"),
                _ => throw new InvalidOperationException("I don't know how I got here.")
            };

            var observations = files.Where(f => f.Valid)
                               .Select(f => f.Observation).Distinct();

            Action <string> log = options.QuietMode ?
                                  msg => { }
            : WorkflowWriter.WriteLine;

            foreach (var observation in observations)
            {
                imageIndex    = rejectedIndex = 1;
                fileName      = genFileName();
                directoryName = rootDirectory = observation;

                var dates = files.Where(f => f.Observation == observation)
                            .Select(f => f.ObservationDate).Distinct();

                foreach (var date in dates)
                {
                    directoryName = rootDirectory;

                    if (options.GroupStrategy != GroupStrategy.Observation)
                    {
                        var dateStr = date.ToString("yyyy-MM-dd");
                        directoryName = $"{directoryName}{ds}{dateStr}";
                        rootDirectory = directoryName;
                    }

                    if (options.GroupStrategy == GroupStrategy.Date)
                    {
                        imageIndex = rejectedIndex = 1;
                        fileName   = genFileName();
                    }

                    var captures = files.Where(f => f.Observation == observation &&
                                               f.ObservationDate == date)
                                   .Select(f => f.Capture).Distinct();

                    foreach (var capture in captures)
                    {
                        directoryName = rootDirectory;

                        if (options.GroupStrategy == GroupStrategy.Capture)
                        {
                            imageIndex    = rejectedIndex = 1;
                            fileName      = genFileName();
                            directoryName = $"{directoryName}{ds}{capture}";
                        }

                        var filesToProcess = files.Where(
                            f => f.Observation == observation &&
                            f.ObservationDate == date &&
                            f.Capture == capture &&
                            f.Valid)
                                             .OrderBy(f => f.FileExtension)
                                             .ThenBy(f => f.FileName);

                        foreach (var file in filesToProcess)
                        {
                            string subdir = "Accepted";

                            if (file.IsProcessed == false)
                            {
                                if (file.Rejected)
                                {
                                    subdir = "Rejected";
                                    var seq = string.Format("{0:0000}", rejectedIndex);
                                    rejectedIndex++;
                                    file.NewFileName = $"{file.FileName}-{seq}";
                                }
                                else
                                {
                                    if (fileName == null)
                                    {
                                        file.NewFileName = file.FileName;
                                    }
                                    else
                                    {
                                        var seq = string.Format("{0:0000}", imageIndex);
                                        imageIndex++;
                                        file.NewFileName = $"{fileName}-{seq}";
                                    }
                                }
                            }
                            else
                            {
                                file.NewFileName = file.FileName;
                                subdir           = "Processed";
                            }

                            var targetDirectory = options.TargetDirectory;

                            // check for overlap with target directory
                            var parts = directoryName.Split(
                                System.IO.Path.DirectorySeparatorChar)
                                        .ToList();

                            do
                            {
                                var stub = string.Join(
                                    System.IO.Path.DirectorySeparatorChar,
                                    parts);
                                if (targetDirectory.EndsWith(stub))
                                {
                                    var idx = targetDirectory.LastIndexOf(stub);
                                    targetDirectory = targetDirectory
                                                      .Substring(0, idx);
                                    break;
                                }

                                parts.RemoveAt(parts.Count - 1);
                            }while (parts.Any());

                            file.TargetPath = System.IO.Path.Combine(
                                targetDirectory,
                                directoryName,
                                subdir,
                                $"{file.NewFileName}.{file.FileExtension}");
                        }
                    }
                }
            }

            WorkflowWriter.WriteLine("Finishing assigning files.");

            return(files);
        }
    }