public FileDatetimeInfo(string _filename, ServiceTools.DatetimeExtractionMethod method = ServiceTools.DatetimeExtractionMethod.Filename, string _datetimeFilenameMask = "?????xxxxxxxxxxxxxxxxxxx*") { filename = _filename; datetimeExtractionMethod = method; datetimeFilenameMask = _datetimeFilenameMask; switch (method) { case ServiceTools.DatetimeExtractionMethod.Filename: { try { string strDateOfFile = String.Concat( Path.GetFileName(filename) .Zip(datetimeFilenameMask, (c, s) => (s == 'x') ? (c.ToString()) : ("")) .ToArray()); datetime = CommonTools.DateTimeOfString(strDateOfFile); DateTime.SpecifyKind(datetime, DateTimeKind.Utc); } catch (Exception ex) { throw ex; } break; } case ServiceTools.DatetimeExtractionMethod.FileCreation: { datetime = new FileInfo(filename).CreationTimeUtc; DateTime.SpecifyKind(datetime, DateTimeKind.Utc); break; } case ServiceTools.DatetimeExtractionMethod.Content: { Dictionary <string, object> currDict = ServiceTools.ReadDictionaryFromXML(filename); currDict.Add("XMLfileName", Path.GetFileName(filename)); ConcurrentData retVal = null; try { retVal = new ConcurrentData(currDict); datetime = retVal.datetimeUTC; DateTime.SpecifyKind(datetime, DateTimeKind.Utc); } catch (Exception ex) { throw ex; } break; } default: { break; } } }
public static GPSdata FindProperGPSdataForImage( string imgFileName, object inLogWindow, Dictionary <string, object> defaultProperties, ref List <Tuple <string, DateTimeSpan> > NVdataFilesAlreadyReadDateTimeSpans, ref List <Tuple <string, List <IoffeVesselDualNavDataConverted> > > NVdataFilesAlreadyReadData) { DateTime curDateTime = DateTime.UtcNow; LogWindow theLogWindow = (LogWindow)inLogWindow; if (NVdataFilesAlreadyReadDateTimeSpans == null) { NVdataFilesAlreadyReadDateTimeSpans = new List <Tuple <string, DateTimeSpan> >(); } if (NVdataFilesAlreadyReadData == null) { NVdataFilesAlreadyReadData = new List <Tuple <string, List <IoffeVesselDualNavDataConverted> > >(); } string ImageFileName = imgFileName; Image anImage = Image.FromFile(imgFileName); ImageInfo newIInfo = new ImageInfo(anImage); string dateTime = (String)newIInfo.getValueByKey("ExifDTOrig"); if (dateTime == null) { //попробуем вытащить из имени файла string strDateTime = Path.GetFileName(ImageFileName); strDateTime = strDateTime.Substring(4, 19); dateTime = strDateTime; } try { curDateTime = CommonTools.DateTimeOfString(dateTime); if (theLogWindow != null) { theLogWindow = ServiceTools.LogAText(theLogWindow, "picture got date/time: " + curDateTime.ToString("s")); } else if (CommonTools.console_present()) { Console.WriteLine("picture got date/time: " + curDateTime.ToString("s")); } } catch (Exception ex) { if (theLogWindow != null) { theLogWindow = ServiceTools.LogAText(theLogWindow, "couldn`t get picture date/time for file: " + Environment.NewLine + ImageFileName); } else if (CommonTools.console_present()) { Console.WriteLine("couldn`t get picture date/time for file: " + Environment.NewLine + ImageFileName); } return(null); } curDateTime = DateTime.SpecifyKind(curDateTime, DateTimeKind.Utc); string strConcurrentDataXMLfilesPath = ""; if (defaultProperties.ContainsKey("strConcurrentDataXMLfilesPath")) { strConcurrentDataXMLfilesPath = (string)defaultProperties["strConcurrentDataXMLfilesPath"]; } else { strConcurrentDataXMLfilesPath = Directory.GetCurrentDirectory(); strConcurrentDataXMLfilesPath = strConcurrentDataXMLfilesPath + ((strConcurrentDataXMLfilesPath.Last() == Path.DirectorySeparatorChar) ? ("") : (Path.DirectorySeparatorChar.ToString())); strConcurrentDataXMLfilesPath += "results" + Path.DirectorySeparatorChar.ToString(); } GPSdata neededGPSdata = new GPSdata(); string currImgPath = Path.GetDirectoryName(ImageFileName); string err = ""; string strCurrImgConcurrentXMLdataFile = ""; // поищем рядом с изображением try { strCurrImgConcurrentXMLdataFile = CommonTools.FindConcurrentDataXMLfile(ImageFileName, out err, currImgPath); } catch (Exception ex) { strCurrImgConcurrentXMLdataFile = ""; } if (strCurrImgConcurrentXMLdataFile != "") { Dictionary <string, object> dictSavedData = ServiceTools.ReadDictionaryFromXML(strCurrImgConcurrentXMLdataFile); GPSdata gps = new GPSdata((string)dictSavedData["GPSdata"], GPSdatasources.CloudCamArduinoGPS, DateTime.Parse((string)dictSavedData["GPSDateTimeUTC"], null, DateTimeStyles.RoundtripKind)); if (gps.validGPSdata) { return(gps); } } // поищем в директории, где хранятся concurrent-данные try { strCurrImgConcurrentXMLdataFile = CommonTools.FindConcurrentDataXMLfile(ImageFileName, out err, strConcurrentDataXMLfilesPath); } catch (Exception ex) { strCurrImgConcurrentXMLdataFile = ""; } if (strCurrImgConcurrentXMLdataFile != "") { Dictionary <string, object> dictSavedData = ServiceTools.ReadDictionaryFromXML(strCurrImgConcurrentXMLdataFile); GPSdata gps = new GPSdata((string)dictSavedData["GPSdata"], GPSdatasources.CloudCamArduinoGPS, DateTime.Parse((string)dictSavedData["GPSDateTimeUTC"], null, DateTimeStyles.RoundtripKind)); if (gps.validGPSdata) { return(gps); } } #region // obsolete //string[] xmlFileNames = Directory.GetFiles(currImgPath, // "*data*" + curDateTime.ToString("s").Substring(0, 13).Replace(":", "-") + "*.xml"); // с точностью до часа //if (xmlFileNames.Count() > 0) //{ // List<GPSdata> lReadGPSdata = new List<GPSdata>(); // foreach (string xmlFileName in xmlFileNames) // { // Dictionary<string, object> dictSavedData = ServiceTools.ReadDictionaryFromXML(xmlFileName); // //GPSdata gps = new GPSdata((string)dictSavedData["GPSdata"], GPSdatasources.CloudCamArduinoGPS); // GPSdata gps = // new GPSdata(new double[] { Convert.ToDouble(dictSavedData["GPSLat"]), Convert.ToDouble(dictSavedData["GPSLon"]) }); // lReadGPSdata.Add(gps); // } // lReadGPSdata.Sort((gpsRecord1, gpsRecord2) => // { // double dev1 = Math.Abs((gpsRecord1.dateTimeUTC - curDateTime).TotalMilliseconds); // double dev2 = Math.Abs((gpsRecord2.dateTimeUTC - curDateTime).TotalMilliseconds); // return (dev1 >= dev2) ? (1) : (-1); // }); // neededGPSdata = lReadGPSdata[0]; //} //else //{ #endregion // obsolete //string navFilesPath = // "D:\\_gulevlab\\SkyImagesAnalysis_appData\\images_complete\\IOFFE\\NIKON-D80\\IOFFE-Mission34-Marina-2011\\data-meteo-nav\\"; // Теперь среди данных навигации // Сначала - среди данных, которые уже были прочитаны. Если такие вообще есть. if (NVdataFilesAlreadyReadDateTimeSpans.Any(tpl => tpl.Item2.ContainsDateTime(curDateTime))) { string nvDataFileName = NVdataFilesAlreadyReadDateTimeSpans.Where(tpl => tpl.Item2.ContainsDateTime(curDateTime)).ElementAt(0).Item1; if (NVdataFilesAlreadyReadData.Any(tpl => tpl.Item1 == nvDataFileName)) { List <IoffeVesselDualNavDataConverted> currFileNVdataFilesAlreadyReadData = NVdataFilesAlreadyReadData.Where(tpl => tpl.Item1 == nvDataFileName).ElementAt(0).Item2; currFileNVdataFilesAlreadyReadData.Sort((gpsRecord1, gpsRecord2) => { double dev1 = Math.Abs((gpsRecord1.gps.dateTimeUTC - curDateTime).TotalMilliseconds); double dev2 = Math.Abs((gpsRecord2.gps.dateTimeUTC - curDateTime).TotalMilliseconds); return((dev1 >= dev2) ? (1) : (-1)); }); neededGPSdata = currFileNVdataFilesAlreadyReadData[0].gps; return(neededGPSdata); } } // если среди них не нашлось - ищем в тех, которые еще не прочитаны ранее string navFilesPath = defaultProperties["IoffeMeteoNavFilesDirectory"] as string; List <IoffeVesselDualNavDataConverted> lAllNavData = new List <IoffeVesselDualNavDataConverted>(); string[] sNavFilenames = Directory.GetFiles(navFilesPath, "*.nv2", SearchOption.AllDirectories); if (!sNavFilenames.Any()) { theLogWindow = ServiceTools.LogAText(theLogWindow, "Не найдено файлов данных навигации", true); return(null); } else { foreach (string navFilename in sNavFilenames) { if (theLogWindow != null) { theLogWindow = ServiceTools.LogAText(theLogWindow, "scanning nav data file: " + navFilename, true); } else if (CommonTools.console_present()) { Console.WriteLine("scanning nav data file: " + navFilename); } DateTimeSpan dtSpan = IoffeVesselNavDataReader.GetNavFileDateTimeMarginsDTS(navFilename); if (dtSpan.IsEmpty) { continue; } NVdataFilesAlreadyReadDateTimeSpans.Add(new Tuple <string, DateTimeSpan>(navFilename, dtSpan)); if (!dtSpan.ContainsDateTime(curDateTime)) { continue; } List <IoffeVesselDualNavDataConverted> dataHasBeenRead = IoffeVesselNavDataReader.ReadNavFile(navFilename); if (dataHasBeenRead == null) { continue; } NVdataFilesAlreadyReadData.Add(new Tuple <string, List <IoffeVesselDualNavDataConverted> >( navFilename, dataHasBeenRead)); if (theLogWindow != null) { theLogWindow = ServiceTools.LogAText(theLogWindow, "файл навигации прочитан: " + navFilename, true); } else if (CommonTools.console_present()) { Console.WriteLine("файл навигации прочитан: " + navFilename); } Application.DoEvents(); lAllNavData.AddRange(dataHasBeenRead); } } if (!lAllNavData.Any()) { if (theLogWindow != null) { theLogWindow = ServiceTools.LogAText(theLogWindow, "Не найдено файлов данных с нужными данными", true); } else if (CommonTools.console_present()) { Console.WriteLine("Не найдено файлов данных с нужными данными"); } return(null); } lAllNavData.Sort((gpsRecord1, gpsRecord2) => { double dev1 = Math.Abs((gpsRecord1.gps.dateTimeUTC - curDateTime).TotalMilliseconds); double dev2 = Math.Abs((gpsRecord2.gps.dateTimeUTC - curDateTime).TotalMilliseconds); return((dev1 >= dev2) ? (1) : (-1)); }); neededGPSdata = lAllNavData[0].gps; //} return(neededGPSdata); }
public async Task<SunDiskCondition> CalcSDC_NN(string SDC_NNconfigFile, string SDC_NNtrainedParametersFile, string NormMeansFile, string NormRangeFile) { if (!File.Exists(SDC_NNconfigFile)) { throw new FileNotFoundException("couldn`t find the file specified: " + SDC_NNconfigFile); } if (!File.Exists(SDC_NNtrainedParametersFile)) { throw new FileNotFoundException("couldn`t find the file specified: " + SDC_NNtrainedParametersFile); } if (!File.Exists(NormMeansFile)) { throw new FileNotFoundException("couldn`t find the file specified: " + NormMeansFile); } if (!File.Exists(NormRangeFile)) { throw new FileNotFoundException("couldn`t find the file specified: " + NormRangeFile); } if ((ImagesRoundMasksXMLfilesMappingList == "") || (ImagesRoundMasksXMLfilesMappingList == null)) { ImagesRoundMasksXMLfilesMappingList = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "settings" + Path.DirectorySeparatorChar + "ImagesRoundMasksXMLfilesMappingList.csv"; } if (!File.Exists(ImagesRoundMasksXMLfilesMappingList)) { throw new FileNotFoundException("couldn`t find the file specified: " + ImagesRoundMasksXMLfilesMappingList); } #region read or calculate GrIxYRGB stats if (Directory.Exists(imageYRGBstatsXMLdataFilesDirectory)) { List<string> foundXMLfiles = Directory.EnumerateFiles(imageYRGBstatsXMLdataFilesDirectory, ConventionalTransitions.ImageGrIxYRGBstatsDataFileName(ImageFilename, "", false), SearchOption.AllDirectories).ToList(); if (foundXMLfiles.Any()) { currImageStatsData = (SkyImageIndexesStatsData) ServiceTools.ReadObjectFromXML(foundXMLfiles[0], typeof(SkyImageIndexesStatsData)); } } if (currImageStatsData == null) { Task<SkyImageIndexesStatsData> tskImageStatsCalculation = new Task<SkyImageIndexesStatsData>(() => { Dictionary<string, object> optionalParameters = new Dictionary<string, object>(); optionalParameters.Add("ImagesRoundMasksXMLfilesMappingList", ImagesRoundMasksXMLfilesMappingList); Stopwatch sw = new Stopwatch(); sw.Start(); optionalParameters.Add("Stopwatch", sw); ImageStatsDataCalculationResult currImageProcessingResult = null; try { currImageProcessingResult = ImageProcessing.CalculateImageStatsData(ImageFilename, optionalParameters); } catch (Exception ex) { return null; } if (currImageProcessingResult == null) { return null; } else { string strImageGrIxYRGBDataFileName = ConventionalTransitions.ImageGrIxYRGBstatsDataFileName(ImageFilename, imageYRGBstatsXMLdataFilesDirectory); ServiceTools.WriteObjectToXML(currImageProcessingResult.grixyrgbStatsData, strImageGrIxYRGBDataFileName); // currImageProcessingResult.stopwatch.Stop(); // string currentFullFileName = currImageProcessingResult.imgFilename; // string strPerfCountersData = currentFullFileName + ";" + // currImageProcessingResult.stopwatch.ElapsedMilliseconds + ";" + // (currImageProcessingResult.procTotalProcessorTimeEnd - // currImageProcessingResult.procTotalProcessorTimeStart) // .TotalMilliseconds + // Environment.NewLine; // ServiceTools.logToTextFile(strPerformanceCountersStatsFile, strPerfCountersData, true); currImageStatsData = currImageProcessingResult.grixyrgbStatsData; return currImageProcessingResult.grixyrgbStatsData; } }); currImageStatsData = await tskImageStatsCalculation; } if (currImageStatsData == null) { // theLogWindow = ServiceTools.LogAText(theLogWindow, "ERROR searching concurrent data for image. Will not proceed."); throw new Exception("ERROR searching concurrent data for image. Will not proceed."); } #endregion read or calculate GrIxYRGB stats ConcurrentData nearestConcurrentData = null; #region search for concurrent data Task<ConcurrentData> tskImageConcurrentDataSearching = new Task<ConcurrentData>(() => { string currImgFilename = Path.GetFileNameWithoutExtension(ImageFilename); string ptrn = @"(devID\d)"; Regex rgxp = new Regex(ptrn, RegexOptions.IgnoreCase); string strCurrImgDT = rgxp.Replace(currImgFilename.Substring(4), ""); //2015-12-16T06-01-38 strCurrImgDT = strCurrImgDT.Substring(0, 11) + strCurrImgDT.Substring(11).Replace("-", ":"); DateTime currImgDT = DateTime.Parse(strCurrImgDT, null, System.Globalization.DateTimeStyles.AdjustToUniversal); List<Tuple<string, ConcurrentData>> lImagesConcurrentData = new List<Tuple<string, ConcurrentData>>(); List<string> filesListConcurrentData = Directory.EnumerateFiles(ConcurrentDataXMLfilesBasePath, ConventionalTransitions.ImageConcurrentDataFilesNamesPattern(), SearchOption.AllDirectories) .ToList(); List<Tuple<string, DateTime>> XMLfilesListConcurrentData = filesListConcurrentData.ConvertAll( strXMLfilename => { string xmlFile1DateTimeString = Path.GetFileNameWithoutExtension(strXMLfilename).Replace("data-", "").Substring(0, 19); xmlFile1DateTimeString = xmlFile1DateTimeString.Substring(0, 11) + xmlFile1DateTimeString.Substring(11).Replace("-", ":"); DateTime dt1 = DateTime.Parse(xmlFile1DateTimeString); return new Tuple<string, DateTime>(strXMLfilename, dt1); }); string nearestConcurrentDataFileName = XMLfilesListConcurrentData.Aggregate((cDt1, cDt2) => { TimeSpan tspan1 = new TimeSpan(Math.Abs((cDt1.Item2 - currImgDT).Ticks)); TimeSpan tspan2 = new TimeSpan(Math.Abs((cDt2.Item2 - currImgDT).Ticks)); return ((tspan1 <= tspan2) ? (cDt1) : (cDt2)); }).Item1; Dictionary<string, object> currDict = ServiceTools.ReadDictionaryFromXML(nearestConcurrentDataFileName); currDict.Add("XMLfileName", Path.GetFileName(nearestConcurrentDataFileName)); ConcurrentData nearestConcurrentDataObtained = new ConcurrentData(currDict); if (new TimeSpan(Math.Abs((nearestConcurrentDataObtained.datetimeUTC - currImgDT).Ticks)) >= new TimeSpan(0, 2, 0)) { //theLogWindow = ServiceTools.LogAText(theLogWindow, // "couldn`t find close enough concurrent data file for image:" + Environment.NewLine + // bgwCurrImageFInfo.FullName + Environment.NewLine + "closest concurrent data file is:" + // Environment.NewLine + nearestConcurrentData.filename + Environment.NewLine + // "with date-time value " + nearestConcurrentData.datetimeUTC.ToString("o")); nearestConcurrentDataObtained = null; } return nearestConcurrentDataObtained; }); nearestConcurrentData = await tskImageConcurrentDataSearching; if (nearestConcurrentData == null) { // theLogWindow = ServiceTools.LogAText(theLogWindow, "ERROR searching concurrent data for image. Will not proceed."); throw new Exception("ERROR searching concurrent data for image. Will not proceed."); } #endregion search for concurrent data DenseVector dvMeans = (DenseVector)((DenseMatrix)ServiceTools.ReadDataFromCSV(NormMeansFile, 0, ",")).Row(0); DenseVector dvRanges = (DenseVector)((DenseMatrix)ServiceTools.ReadDataFromCSV(NormRangeFile, 0, ",")).Row(0); DenseVector dvThetaValues = (DenseVector)ServiceTools.ReadDataFromCSV(SDC_NNtrainedParametersFile, 0, ","); List<int> NNlayersConfig = new List<double>(((DenseMatrix)ServiceTools.ReadDataFromCSV(SDC_NNconfigFile, 0, ",")).Row(0)).ConvertAll (dVal => Convert.ToInt32(dVal)); List<double> decisionProbabilities = new List<double>(); return PredictSDC_NN(currImageStatsData, nearestConcurrentData, NNlayersConfig, dvThetaValues, dvMeans, dvRanges, out decisionProbabilities); }
public static SunDiskCondition CalcSDC_NN(string statsXMLfile, string concurrentDataXMLfile, string SDC_NNconfigFile, string SDC_NNtrainedParametersFile, string NormMeansFile, string NormRangeFile, out List<double> decisionProbabilities) { if (!File.Exists(SDC_NNconfigFile)) { throw new FileNotFoundException("couldn`t find the file specified: " + SDC_NNconfigFile); } if (!File.Exists(SDC_NNtrainedParametersFile)) { throw new FileNotFoundException("couldn`t find the file specified: " + SDC_NNtrainedParametersFile); } if (!File.Exists(NormMeansFile)) { throw new FileNotFoundException("couldn`t find the file specified: " + NormMeansFile); } if (!File.Exists(NormRangeFile)) { throw new FileNotFoundException("couldn`t find the file specified: " + NormRangeFile); } if (!File.Exists(statsXMLfile)) { throw new FileNotFoundException("couldn`t find the file specified: " + statsXMLfile); } if (!File.Exists(concurrentDataXMLfile)) { throw new FileNotFoundException("couldn`t find the file specified: " + concurrentDataXMLfile); } #region read GrIxYRGB stats SkyImageIndexesStatsData currImageStatsData = null; try { currImageStatsData = (SkyImageIndexesStatsData) ServiceTools.ReadObjectFromXML(statsXMLfile, typeof(SkyImageIndexesStatsData)); } catch (Exception ex) { throw ex; } if (currImageStatsData == null) { throw new Exception("ERROR reading stats data for image. Will not proceed."); } #endregion read or calculate GrIxYRGB stats #region search for concurrent data ConcurrentData nearestConcurrentData = null; try { Dictionary<string, object> currDict = ServiceTools.ReadDictionaryFromXML(concurrentDataXMLfile); currDict.Add("XMLfileName", Path.GetFileName(concurrentDataXMLfile)); nearestConcurrentData = new ConcurrentData(currDict); } catch (Exception) { throw new Exception("ERROR reading concurrent data for image. Will not proceed."); ; } if (nearestConcurrentData == null) { // theLogWindow = ServiceTools.LogAText(theLogWindow, "ERROR searching concurrent data for image. Will not proceed."); throw new Exception("ERROR searching concurrent data for image. Will not proceed."); } #endregion search for concurrent data DenseVector dvMeans = (DenseVector)((DenseMatrix)ServiceTools.ReadDataFromCSV(NormMeansFile, 0, ",")).Row(0); DenseVector dvRanges = (DenseVector)((DenseMatrix)ServiceTools.ReadDataFromCSV(NormRangeFile, 0, ",")).Row(0); DenseVector dvThetaValues = (DenseVector)ServiceTools.ReadDataFromCSV(SDC_NNtrainedParametersFile, 0, ","); List<int> NNlayersConfig = new List<double>(((DenseMatrix)ServiceTools.ReadDataFromCSV(SDC_NNconfigFile, 0, ",")).Row(0)).ConvertAll (dVal => Convert.ToInt32(dVal)); return PredictSDC_NN(currImageStatsData, nearestConcurrentData, NNlayersConfig, dvThetaValues, dvMeans, dvRanges, out decisionProbabilities); }
public static string FindConcurrentDataXMLfile(string imgFileName, out string err, string basePath = "") { if (basePath == "") { basePath = Directory.GetCurrentDirectory(); basePath = basePath + ((basePath.Last() == Path.DirectorySeparatorChar) ? ("") : (Path.DirectorySeparatorChar.ToString())); basePath += "results" + Path.DirectorySeparatorChar; } DirectoryInfo dirConcurrentDataFiles = new DirectoryInfo(basePath); string strDateTime = Path.GetFileName(imgFileName); strDateTime = strDateTime.Substring(4, 19); DateTime currImgDateTime = CommonTools.DateTimeOfString(strDateTime); FileInfo[] concurrentDataFilesList = dirConcurrentDataFiles.GetFiles( "data-" + currImgDateTime.ToString("s").Replace(":", "-").Substring(0, 16) + "*.xml", SearchOption.TopDirectoryOnly); if (concurrentDataFilesList.Any()) { List <Tuple <string, Dictionary <string, object> > > lTplConcurrentDataFilesInfo = new List <FileInfo>( concurrentDataFilesList) .ConvertAll <Tuple <string, Dictionary <string, object> > >( finfo => { Dictionary <string, object> dictSavedData = ServiceTools.ReadDictionaryFromXML(concurrentDataFilesList[0].FullName) as Dictionary <string, object>; return(new Tuple <string, Dictionary <string, object> >(finfo.FullName, dictSavedData)); }); lTplConcurrentDataFilesInfo.RemoveAll( tpl => !(new GPSdata((string)(tpl.Item2["GPSdata"]), GPSdatasources.CloudCamArduinoGPS, DateTime.Parse((string)tpl.Item2["GPSDateTimeUTC"], null, DateTimeStyles.RoundtripKind))).validGPSdata); if (!lTplConcurrentDataFilesInfo.Any()) { err = "========== ERROR: couldn`t find concurrent data file for " + imgFileName; return(""); } List <Tuple <string, Dictionary <string, object>, long> > lTplConcurrentData = lTplConcurrentDataFilesInfo .ConvertAll( tpl => { GPSdata currDataGPS = new GPSdata((string)(tpl.Item2["GPSdata"]), GPSdatasources.CloudCamArduinoGPS, DateTime.Parse((string)tpl.Item2["GPSDateTimeUTC"], null, DateTimeStyles.RoundtripKind)); long dt = (currDataGPS.DateTimeUTC - currImgDateTime).Ticks; return(new Tuple <string, Dictionary <string, object>, long>(tpl.Item1, tpl.Item2, dt)); }); lTplConcurrentData.Sort(CompareTuplesByIntItem3); //get the first item as closest err = ""; return(lTplConcurrentData[0].Item1); } else { err = "========== ERROR: couldn`t find concurrent data file for " + imgFileName; return(""); } }
public async Task <int> CalcTCC_NN(string SDC_NNconfigFile, string SDC_NNtrainedParametersFile, string SDC_NormMeansFile, string SDC_NormRangeFile, string TCC_NNconfigFile, string TCC_NNtrainedParametersFile, string TCC_NormMeansFile, string TCC_NormRangeFile, string TCC_ExcludingVarsFile) { #region check files existence if (!File.Exists(SDC_NNconfigFile)) { throw new FileNotFoundException("couldn`t find the file specified: " + SDC_NNconfigFile); } if (!File.Exists(SDC_NNtrainedParametersFile)) { throw new FileNotFoundException("couldn`t find the file specified: " + SDC_NNtrainedParametersFile); } if (!File.Exists(SDC_NormMeansFile)) { throw new FileNotFoundException("couldn`t find the file specified: " + SDC_NormMeansFile); } if (!File.Exists(SDC_NormRangeFile)) { throw new FileNotFoundException("couldn`t find the file specified: " + SDC_NormRangeFile); } if (!File.Exists(TCC_NNconfigFile)) { throw new FileNotFoundException("couldn`t find the file specified: " + TCC_NNconfigFile); } if (!File.Exists(TCC_NNtrainedParametersFile)) { throw new FileNotFoundException("couldn`t find the file specified: " + TCC_NNtrainedParametersFile); } if (!File.Exists(TCC_NormMeansFile)) { throw new FileNotFoundException("couldn`t find the file specified: " + TCC_NormMeansFile); } if (!File.Exists(TCC_NormRangeFile)) { throw new FileNotFoundException("couldn`t find the file specified: " + TCC_NormRangeFile); } if (!File.Exists(TCC_ExcludingVarsFile)) { throw new FileNotFoundException("couldn`t find the file specified: " + TCC_ExcludingVarsFile); } if ((ImagesRoundMasksXMLfilesMappingList == "") || (ImagesRoundMasksXMLfilesMappingList == null)) { ImagesRoundMasksXMLfilesMappingList = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "settings" + Path.DirectorySeparatorChar + "ImagesRoundMasksXMLfilesMappingList.csv"; } if (!File.Exists(ImagesRoundMasksXMLfilesMappingList)) { throw new FileNotFoundException("couldn`t find the file specified: " + ImagesRoundMasksXMLfilesMappingList); } #endregion check files existence #region read or calculate GrIxYRGB stats if (Directory.Exists(imageYRGBstatsXMLdataFilesDirectory)) { List <string> foundXMLfiles = Directory.EnumerateFiles(imageYRGBstatsXMLdataFilesDirectory, ConventionalTransitions.ImageGrIxYRGBstatsDataFileName(ImageFilename, "", false), SearchOption.AllDirectories).ToList(); if (foundXMLfiles.Any()) { currImageStatsData = (SkyImageIndexesStatsData) ServiceTools.ReadObjectFromXML(foundXMLfiles[0], typeof(SkyImageIndexesStatsData)); } } if (currImageStatsData == null) { Task <SkyImageIndexesStatsData> tskImageStatsCalculation = new Task <SkyImageIndexesStatsData>(() => { Dictionary <string, object> optionalParameters = new Dictionary <string, object>(); optionalParameters.Add("ImagesRoundMasksXMLfilesMappingList", ImagesRoundMasksXMLfilesMappingList); Stopwatch sw = new Stopwatch(); sw.Start(); optionalParameters.Add("Stopwatch", sw); ImageStatsDataCalculationResult currImageProcessingResult = null; try { currImageProcessingResult = ImageProcessing.CalculateImageStatsData(ImageFilename, optionalParameters); } catch (Exception ex) { return(null); } if (currImageProcessingResult == null) { return(null); } else { string strImageGrIxYRGBDataFileName = ConventionalTransitions.ImageGrIxYRGBstatsDataFileName(ImageFilename, imageYRGBstatsXMLdataFilesDirectory); ServiceTools.WriteObjectToXML(currImageProcessingResult.grixyrgbStatsData, strImageGrIxYRGBDataFileName); currImageStatsData = currImageProcessingResult.grixyrgbStatsData; return(currImageProcessingResult.grixyrgbStatsData); } }); currImageStatsData = await tskImageStatsCalculation; } if (currImageStatsData == null) { // theLogWindow = ServiceTools.LogAText(theLogWindow, "ERROR searching concurrent data for image. Will not proceed."); throw new Exception("ERROR searching concurrent data for image. Will not proceed."); } #endregion read or calculate GrIxYRGB stats ConcurrentData nearestConcurrentData = null; #region search for concurrent data Task <ConcurrentData> tskImageConcurrentDataSearching = new Task <ConcurrentData>(() => { string currImgFilename = Path.GetFileNameWithoutExtension(ImageFilename); string ptrn = @"(devID\d)"; Regex rgxp = new Regex(ptrn, RegexOptions.IgnoreCase); string strCurrImgDT = rgxp.Replace(currImgFilename.Substring(4), ""); //2015-12-16T06-01-38 strCurrImgDT = strCurrImgDT.Substring(0, 11) + strCurrImgDT.Substring(11).Replace("-", ":"); DateTime currImgDT = DateTime.Parse(strCurrImgDT, null, System.Globalization.DateTimeStyles.AdjustToUniversal); List <Tuple <string, ConcurrentData> > lImagesConcurrentData = new List <Tuple <string, ConcurrentData> >(); List <string> filesListConcurrentData = Directory.EnumerateFiles(ConcurrentDataXMLfilesBasePath, ConventionalTransitions.ImageConcurrentDataFilesNamesPattern(), SearchOption.AllDirectories) .ToList(); List <Tuple <string, DateTime> > XMLfilesListConcurrentData = filesListConcurrentData.ConvertAll( strXMLfilename => { string xmlFile1DateTimeString = Path.GetFileNameWithoutExtension(strXMLfilename).Replace("data-", "").Substring(0, 19); xmlFile1DateTimeString = xmlFile1DateTimeString.Substring(0, 11) + xmlFile1DateTimeString.Substring(11).Replace("-", ":"); DateTime dt1 = DateTime.Parse(xmlFile1DateTimeString); return(new Tuple <string, DateTime>(strXMLfilename, dt1)); }); string nearestConcurrentDataFileName = XMLfilesListConcurrentData.Aggregate((cDt1, cDt2) => { TimeSpan tspan1 = new TimeSpan(Math.Abs((cDt1.Item2 - currImgDT).Ticks)); TimeSpan tspan2 = new TimeSpan(Math.Abs((cDt2.Item2 - currImgDT).Ticks)); return((tspan1 <= tspan2) ? (cDt1) : (cDt2)); }).Item1; Dictionary <string, object> currDict = ServiceTools.ReadDictionaryFromXML(nearestConcurrentDataFileName); currDict.Add("XMLfileName", Path.GetFileName(nearestConcurrentDataFileName)); ConcurrentData nearestConcurrentDataObtained = new ConcurrentData(currDict); if (new TimeSpan(Math.Abs((nearestConcurrentDataObtained.datetimeUTC - currImgDT).Ticks)) >= new TimeSpan(0, 2, 0)) { nearestConcurrentDataObtained = null; } return(nearestConcurrentDataObtained); }); nearestConcurrentData = await tskImageConcurrentDataSearching; if (nearestConcurrentData == null) { // theLogWindow = ServiceTools.LogAText(theLogWindow, "ERROR searching concurrent data for image. Will not proceed."); throw new Exception("ERROR searching concurrent data for image. Will not proceed."); } #endregion search for concurrent data DenseVector dvSDCmeans = (DenseVector)((DenseMatrix)ServiceTools.ReadDataFromCSV(SDC_NormMeansFile, 0, ",")).Row(0); DenseVector dvSDCranges = (DenseVector)((DenseMatrix)ServiceTools.ReadDataFromCSV(SDC_NormRangeFile, 0, ",")).Row(0); DenseVector dvSDCthetaValues = (DenseVector)ServiceTools.ReadDataFromCSV(SDC_NNtrainedParametersFile, 0, ","); List <int> SDCnnLayersConfig = new List <double>(((DenseMatrix)ServiceTools.ReadDataFromCSV(SDC_NNconfigFile, 0, ",")).Row(0)).ConvertAll (dVal => Convert.ToInt32(dVal)); DenseVector dvTCCmeans = (DenseVector)((DenseMatrix)ServiceTools.ReadDataFromCSV(TCC_NormMeansFile, 0, ",")).Row(0); DenseVector dvTCCranges = (DenseVector)((DenseMatrix)ServiceTools.ReadDataFromCSV(TCC_NormRangeFile, 0, ",")).Row(0); DenseVector dvTCCthetaValues = (DenseVector)ServiceTools.ReadDataFromCSV(TCC_NNtrainedParametersFile, 0, ","); List <int> TCCnnLayersConfig = new List <double>(((DenseMatrix)ServiceTools.ReadDataFromCSV(TCC_NNconfigFile, 0, ",")).Row(0)).ConvertAll (dVal => Convert.ToInt32(dVal)); List <int> TCCnnConfigVarsToExclude = new List <double>(((DenseMatrix)ServiceTools.ReadDataFromCSV(TCC_ExcludingVarsFile, 0, ",")).Row(0)).ConvertAll (dVal => Convert.ToInt32(dVal)); List <double> TCCdecisionProbabilities = null; return(PredictTCC_NN(currImageStatsData, nearestConcurrentData, SDCnnLayersConfig, dvSDCthetaValues, dvSDCmeans, dvSDCranges, TCCnnLayersConfig, dvTCCthetaValues, dvTCCmeans, dvTCCranges, TCCnnConfigVarsToExclude, out TCCdecisionProbabilities)); }