public static RangeHz[] CollapseRangesHzIntersections(RangeHz[] ranges) { List <RangeHz> rngs = new List <RangeHz>(ranges); rngs = rngs.FindAll((r) => { return(r.Min != r.Max); }); rngs.Sort((r1, r2) => { if (r1.MinOpen) { return(-1); } if (r2.MinOpen) { return(1); } if (r1.MaxOpen) { return(1); } if (r2.MaxOpen) { return(-1); } return(r1.Min.CompareTo(r2.Min)); }); double start = rngs.Count > 0 ? rngs[0].Min : 0; double end = rngs.Count > 0 ? rngs[0].Max : 0; List <RangeHz> results = new List <RangeHz>(); for (int i = 0; i < rngs.Count; i++) { RangeHz r = rngs[i]; if (r.Min > end) { results.Add(new RangeHz(start, end)); start = r.Min; end = r.Max; } else { end = Math.Max(end, r.Max); } } if (rngs.Count > 0) { results.Add(new RangeHz(start, end)); } return(results.ToArray()); }
private void buttonCompute_Click(object sender, EventArgs e) { if (computing) { t.Abort(); buttonCompute.Text = "Рассчитать"; labelComputationStatus.Text = "Не исполняется"; computing = false; return; } List <string> files = new List <string>(); for (int i = 0; i < dataGridViewSelected.RowCount - 1; i++) { string val = dataGridViewSelected.Rows[i].Cells[0].Value.ToString(); if (!string.IsNullOrWhiteSpace(val)) { files.Add(val); } } List <RangeHz> ranges = new List <RangeHz>(); for (int i = 0; i < flowLayoutPanel.Controls.Count; i++) { RangeSelect rs = (RangeSelect)flowLayoutPanel.Controls[i]; RangeHz r = new RangeHz(rs.StartValue, rs.EndValue, rs.StartOpen, rs.EndOpen); ranges.Add(r); } double discretization = (double)numericUpDownDiscretization.Value; string saveDir = textBoxSavePath.Text; if (!Directory.Exists(saveDir)) { MessageBox.Show("Папка для сохранения результатов \"" + saveDir + "\" не найдена", "Обратите внимание"); return; } if (f != null) { f.Close(); } f = new VerificationForm(this, files.ToArray(), ranges.ToArray(), discretization); f.Show(); buttonCompute.Enabled = false; disableAllChangableControlls(); f.Confirmed += () => { buttonCompute.Enabled = true; Queue <string> filesQueue = new Queue <string>(f.FilesToPerform.Count); foreach (var file in f.FilesToPerform) { filesQueue.Enqueue(file); } var dict = Utils.GroupFilesByFolders(f.FilesToPerform.ToArray()); var dirs = dict.Keys.ToList(); Dictionary <string, int> dirsIndexes = new Dictionary <string, int>(); int startIndex = -1; string[] dirsInSave = Directory.GetDirectories(saveDir); string programIdentifier = "FreqsSelector"; Regex r = new Regex(programIdentifier + "[\\d]+"); for (int i = 0; i < dirsInSave.Length; i++) { if (r.IsMatch(dirsInSave[i])) { int ind; string baseDir = Utils.GetFilename(dirsInSave[i]); string strInd = baseDir.Substring(programIdentifier.Length); if (int.TryParse(strInd, out ind)) { startIndex = Math.Max(startIndex, ind); } } } startIndex++; for (int i = 0; i < dirs.Count; i++) { dirsIndexes[dirs[i]] = i + startIndex; } t = new Thread(() => { FTCoumputer c = new DFTCumputer(); int countFiles = filesQueue.Count; computing = true; DateTime now = DateTime.Now; string timeString = now.ToString("dd/MM/yy HH:mm:ss"); int counted = 0; string logPath = ".\\Log " + timeString.Replace(':', '_') + ".txt"; var s = File.Create(logPath); s.Close(); var simplifiedRanges = f.simplifiedRanges; List <string> resRangesList = new List <string>(); foreach (var range in simplifiedRanges) { resRangesList.Add("[" + range.Min + ";" + (range.Max == double.MaxValue ? "+Inf)" : range.Max.ToString() + "]")); } string resRanges = string.Join(" U ", resRangesList.ToArray()); using (var writer = new StreamWriter(logPath, true)) { writer.WriteLine("Time:" + timeString); writer.WriteLine("Restore ranges: " + resRanges); writer.WriteLine("Analazing files:" + countFiles + ":"); foreach (var path in filesQueue.ToList()) { writer.WriteLine(path); } writer.WriteLine("Analization start"); } while (filesQueue.Count != 0) { string file = filesQueue.Peek(); this.Invoke((Action)(() => { labelComputationStatus.Text = "Обрабатываеся " + (countFiles - filesQueue.Count + 1) + "/" + countFiles + ":\n" + file; })); using (var writer = new StreamWriter(logPath, true)) { string time = DateTime.Now.ToString("dd.MM.yy HH:mm:ss"); writer.WriteLine("Start:" + time); writer.WriteLine("File(" + (countFiles - filesQueue.Count + 1) + "/" + countFiles + "):\t" + file); } FTCoumputer.Result res; try { res = Compute(f, c, file, discretization, ranges.ToArray()); } catch (FileComputeError err) { using (var writer = new StreamWriter(logPath, true)) { string time = DateTime.Now.ToString("dd.MM.yy HH:mm:ss"); switch (err.error) { case FileComputeError.Error.FAILED_TO_OPEN: WriteFileStatusToVerificationForm(err.file, "Не удалось открыть"); writer.WriteLine("Done:" + time); writer.WriteLine("Result:FAILED_TO_OPEN"); break; case FileComputeError.Error.INVALID_FORMAT: WriteFileStatusToVerificationForm(err.file, "Не верный формат"); writer.WriteLine("Done:" + time); writer.WriteLine("Result:INVALID_FORMAT"); break; } continue; } } if (res == null) { filesQueue.Dequeue(); continue; } counted++; string baseFileDir = Utils.GetBaseDir(file); string fileSaveDir = Utils.JoinPathParts(saveDir, programIdentifier + dirsIndexes[baseFileDir].ToString()); string ident = Utils.GetFilename(file); string[] savedFiles = WriteResults(res, fileSaveDir, timeString, baseFileDir, ident); filesQueue.Dequeue(); using (var writer = new StreamWriter(logPath, true)) { string time = DateTime.Now.ToString("dd.MM.yy HH:mm:ss"); List <string> rangesIndxsList = new List <string>(); foreach (var range in res.Ranges) { rangesIndxsList.Add("[" + range.Min + ";" + range.Max + "]"); } string rangesIndxsStr = string.Join(" U ", rangesIndxsList.ToArray()); writer.WriteLine("Done:" + time); writer.WriteLine("Result:OK"); writer.WriteLine("Ranges:" + rangesIndxsStr); writer.WriteLine("SavedTo:" + fileSaveDir); writer.WriteLine("FT:" + savedFiles[0]); writer.WriteLine("IFT:" + savedFiles[1]); writer.WriteLine("MINS:" + savedFiles[2]); writer.WriteLine("MAXS:" + savedFiles[3]); } } this.Invoke((Action)(() => { labelComputationStatus.Text = "Не исполняется"; MessageBox.Show("Файлов рассчитано успешно: " + counted + " из " + countFiles, "Готово!"); buttonCompute.Text = "Рассчитать"; computing = false; enableAllChangableControlls(); })); }); t.Start(); }; f.Rejected += () => { buttonCompute.Enabled = true; enableAllChangableControlls(); }; }
public static RangeIndexes GetDataIndexesRange(int dataLen, double discretization, RangeHz range) { int start = (int)(dataLen * 1.0 / discretization * range.Min); double end_d = Math.Min(dataLen, (dataLen * 1.0 / discretization * range.Max)); if ((int)end_d == start) { end_d += 1; } return(new RangeIndexes(start, (int)end_d)); }