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 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("");
            }
        }
Esempio n. 3
0
        public static List <IoffeVesselDualNavDataConverted> ReadNavFile(string InputFilename) //, bool readOnlyThe1stRecord = false)
        {
            int bytesCountPerObj = Marshal.SizeOf(typeof(IoffeVesselDualNavData));

            if ((new FileInfo(InputFilename)).Length < (4 + bytesCountPerObj))
            {
                return(null);
            }
            BinaryReader reader = new BinaryReader(File.Open(InputFilename, FileMode.Open, FileAccess.Read, FileShare.Read));

            char[] dualmode = reader.ReadChars(4);
            if (new string(dualmode) != "Dual")
            {
                // неправильный файл навигации - данные не в том формате
                return(null);
            }
            reader.Close();

            byte[] fileData = File.ReadAllBytes(InputFilename);

            reader = new BinaryReader(new MemoryStream(fileData));
            reader.ReadChars(4);

            List <IoffeVesselDualNavData> lFileNavData = new List <IoffeVesselDualNavData>();

            int structCount = Convert.ToInt32((fileData.Count() - 4) / bytesCountPerObj);

            if (structCount == 0)
            {
                return(null);
            }
            int readStructs = 0;

            while (true)
            {
                byte[] readBuffer = new byte[bytesCountPerObj];
                readBuffer = reader.ReadBytes(bytesCountPerObj);
                GCHandle handle = GCHandle.Alloc(readBuffer, GCHandleType.Pinned);
                IoffeVesselDualNavData curRecord =
                    (IoffeVesselDualNavData)
                    Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(IoffeVesselDualNavData));
                readStructs++;
                lFileNavData.Add(curRecord);
                //if ((readStructs >= structCount) || (readOnlyThe1stRecord))
                if (readStructs >= structCount)
                {
                    break;
                }
            }



            List <IoffeVesselDualNavDataConverted> lRetList = lFileNavData.ConvertAll(navData =>
            {
                IoffeVesselDualNavDataConverted retNavData = new IoffeVesselDualNavDataConverted();

                #region     // -> ExtractDateTime()
                //string dateStr = navData.DateGPS.ToString();
                //string timeStr = navData.curtime.ToString("D4");
                //DateTime dt;
                //if (navData.curtime >= 2400)
                //{
                //    int daysToAdd = 0;
                //    while (navData.curtime >= 2400)
                //    {
                //        navData.curtime -= 2400;
                //        daysToAdd ++;
                //    }
                //    timeStr = (navData.curtime).ToString("D4");
                //    dt = new DateTime(2000 + Convert.ToInt32(dateStr.Substring(dateStr.Length - 2, 2)),
                //        Convert.ToInt32(dateStr.Substring(dateStr.Length - 4, 2)),
                //        Convert.ToInt32(dateStr.Substring(0, dateStr.Length - 4)),
                //        Convert.ToInt32(timeStr.Substring(0, 2)),
                //        Convert.ToInt32(timeStr.Substring(2, 2)),
                //        0);
                //    dt = dt.AddDays(daysToAdd);
                //}
                //else
                //{
                //    dt = new DateTime(2000 + Convert.ToInt32(dateStr.Substring(dateStr.Length - 2, 2)),
                //        Convert.ToInt32(dateStr.Substring(dateStr.Length - 4, 2)),
                //        Convert.ToInt32(dateStr.Substring(0, dateStr.Length - 4)),
                //        Convert.ToInt32(timeStr.Substring(0, 2)),
                //        Convert.ToInt32(timeStr.Substring(2, 2)),
                //        0);
                //}
                #endregion     // -> ExtractDateTime()

                retNavData.dateTime = ExtractDateTime(navData);
                GPSdata curGPS      = new GPSdata()
                {
                    GPSstring            = "",
                    lat                  = (double)navData.Lat,
                    latHemisphere        = "" + navData.SimLat,
                    lon                  = (double)navData.Lon,
                    lonHemisphere        = "" + navData.SimLon,
                    dateTimeUTC          = retNavData.dateTime,
                    validGPSdata         = true,
                    IOFFEdataHeadingTrue = navData.trueHead,
                    IOFFEdataHeadingGyro = navData.Gyro,
                    IOFFEdataSpeedKnots  = navData.Speed,
                    IOFFEdataDepth       = navData.Depth,
                    dataSource           = GPSdatasources.IOFFEvesselDataServer
                };
                retNavData.gps    = curGPS;
                retNavData.Course = navData.Course;
                return(retNavData);
            });


            return(lRetList);
        }
Esempio n. 4
0
        public override int DecodeAVL(List <byte> receiveBytes, string IMEI)
        {
            string hexDataLength = string.Empty;

            receiveBytes.Skip(4).Take(4).ToList().ForEach(delegate(byte b) { hexDataLength += String.Format("{0:X2}", b); });
            int dataLength = Convert.ToInt32(hexDataLength, 16);

            // byte[] test = receiveBytes.Skip(4).Take(4).ToArray();
            // Array.Reverse(test);
            // int dataLength = BitConverter.ToInt32(test, 0);

            ShowDiagnosticInfo("Data Length: -----".PadRight(40, '-') + " " + dataLength);
            int codecId = Convert.ToInt32(receiveBytes.Skip(8).Take(1).ToList()[0]);

            ShowDiagnosticInfo("Codec ID: -----".PadRight(40, '-') + " " + codecId);
            int numberOfData = Convert.ToInt32(receiveBytes.Skip(9).Take(1).ToList()[0]);;

            ShowDiagnosticInfo("Number of data: ----".PadRight(40, '-') + " " + numberOfData);

            int tokenAddress = 10;

            for (int n = 0; n < numberOfData; n++)
            {
                GPSdata gpsData      = new GPSdata();
                string  hexTimeStamp = string.Empty;
                receiveBytes.Skip(tokenAddress).Take(8).ToList().ForEach(delegate(byte b) { hexTimeStamp += String.Format("{0:X2}", b); });
                long timeSt = Convert.ToInt64(hexTimeStamp, 16);

                DateTime origin    = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                DateTime timestamp = origin.AddMilliseconds(Convert.ToDouble(timeSt));

                ShowDiagnosticInfo("Timestamp: -----".PadRight(40, '-') + " " + timestamp.ToLongDateString() + " " + timestamp.ToLongTimeString());

                int priority = Convert.ToInt32(receiveBytes.Skip(tokenAddress + 8).Take(1).ToList()[0]);
                ShowDiagnosticInfo("Priority: ------------".PadRight(40, '-') + " " + priority);

                string longt = string.Empty;
                receiveBytes.Skip(tokenAddress + 9).Take(4).ToList().ForEach(delegate(byte b) { longt += String.Format("{0:X2}", b); });
                double longtitude = ((double)Convert.ToInt32(longt, 16)) / 10000000;
                ShowDiagnosticInfo("Longtitude: -----".PadRight(40, '-') + " " + longtitude);

                string lat = string.Empty;
                receiveBytes.Skip(tokenAddress + 13).Take(4).ToList().ForEach(delegate(byte b) { lat += String.Format("{0:X2}", b); });
                double latitude = ((double)Convert.ToInt32(lat, 16)) / 10000000;
                ShowDiagnosticInfo("Latitude: -----".PadRight(40, '-') + " " + latitude);

                string alt = string.Empty;
                receiveBytes.Skip(tokenAddress + 17).Take(2).ToList().ForEach(delegate(byte b) { alt += String.Format("{0:X2}", b); });
                int altitude = Convert.ToInt32(alt, 16);
                ShowDiagnosticInfo("Altitude: -----".PadRight(40, '-') + " " + altitude);

                string ang = string.Empty;
                receiveBytes.Skip(tokenAddress + 19).Take(2).ToList().ForEach(delegate(byte b) { ang += String.Format("{0:X2}", b); });
                int angle = Convert.ToInt32(ang, 16);
                ShowDiagnosticInfo("Angle: -----".PadRight(40, '-') + " " + angle);

                int satellites = Convert.ToInt32(receiveBytes.Skip(tokenAddress + 21).Take(1).ToList()[0]);
                ShowDiagnosticInfo("Satellites: -----".PadRight(40, '-') + " " + satellites);

                string sp = string.Empty;
                receiveBytes.Skip(tokenAddress + 22).Take(2).ToList().ForEach(delegate(byte b) { sp += String.Format("{0:X2}", b); });
                int speed = Convert.ToInt32(sp, 16);
                ShowDiagnosticInfo("Speed: -----".PadRight(40, '-') + " " + speed);

                byte event_IO_element_ID = (byte)Convert.ToInt32(receiveBytes.Skip(tokenAddress + 24).Take(1).ToList()[0]);
                gpsData.Event_IO_element_ID = event_IO_element_ID;
                ShowDiagnosticInfo("IO element ID of Event generated: ------".PadRight(40, '-') + " " + event_IO_element_ID);

                int IO_element_in_record = Convert.ToInt32(receiveBytes.Skip(tokenAddress + 25).Take(1).ToList()[0]);
                ShowDiagnosticInfo("IO_element_in_record: --------".PadRight(40, '-') + " " + IO_element_in_record);


                if (IO_element_in_record != 0)
                {
                    int currentCursor = tokenAddress + 26;

                    int IO_Elements_1B_Quantity = Convert.ToInt32(receiveBytes.Skip(currentCursor).Take(1).ToList()[0]);
                    ShowDiagnosticInfo("1 byte IO element in record: --------".PadRight(40, '-') + " " + IO_Elements_1B_Quantity);


                    for (int IO_1 = 0; IO_1 < IO_Elements_1B_Quantity; IO_1++)
                    {
                        var parameterID   = (byte)Convert.ToInt32(receiveBytes.Skip(currentCursor + 1 + IO_1 * 2).Take(1).ToList()[0]);
                        var IO_Element_1B = (byte)Convert.ToInt32(receiveBytes.Skip(currentCursor + 2 + IO_1 * 2).Take(1).ToList()[0]);
                        gpsData.IO_Elements_1B.Add(parameterID, IO_Element_1B);
                        ShowDiagnosticInfo("IO element 1B ID: --------".PadRight(40, '-') + " " + parameterID);
                        ShowDiagnosticInfo(IO_1 + "'st 1B IO element value: --------".PadRight(40 - IO_1.ToString().Length, '-') + " " + IO_Element_1B);
                    }
                    currentCursor += IO_Elements_1B_Quantity * 2 + 1;

                    int IO_Elements_2B_Quantity = Convert.ToInt32(receiveBytes.Skip(currentCursor).Take(1).ToList()[0]);
                    ShowDiagnosticInfo("2 byte IO element in record: --------".PadRight(40, '-') + " " + IO_Elements_2B_Quantity);

                    for (int IO_2 = 0; IO_2 < IO_Elements_2B_Quantity; IO_2++)
                    {
                        var    parameterID = (byte)Convert.ToInt32(receiveBytes.Skip(currentCursor + 1 + IO_2 * 3).Take(1).ToList()[0]);
                        string value       = string.Empty;
                        receiveBytes.Skip(currentCursor + 2 + IO_2 * 3).Take(2).ToList().ForEach(delegate(byte b) { value += String.Format("{0:X2}", b); });
                        var IO_Element_2B = Convert.ToInt16(value, 16);
                        gpsData.IO_Elements_2B.Add(parameterID, IO_Element_2B);
                        ShowDiagnosticInfo("IO element 2B ID: --------".PadRight(40, '-') + " " + parameterID);
                        ShowDiagnosticInfo(IO_2 + "'st 2B IO element value: --------".PadRight(40 - IO_2.ToString().Length, '-') + " " + IO_Element_2B);
                    }
                    currentCursor += IO_Elements_2B_Quantity * 3 + 1;

                    int IO_Elements_4B_Quantity = Convert.ToInt32(receiveBytes.Skip(currentCursor).Take(1).ToList()[0]);
                    ShowDiagnosticInfo("4 byte IO element in record: --------".PadRight(40, '-') + " " + IO_Elements_4B_Quantity);

                    for (int IO_4 = 0; IO_4 < IO_Elements_4B_Quantity; IO_4++)
                    {
                        var    parameterID = (byte)Convert.ToInt32(receiveBytes.Skip(currentCursor + 1 + IO_4 * 5).Take(1).ToList()[0]);
                        string value       = string.Empty;
                        receiveBytes.Skip(currentCursor + 2 + IO_4 * 5).Take(4).ToList().ForEach(delegate(byte b) { value += String.Format("{0:X2}", b); });
                        var IO_Element_4B = Convert.ToInt32(value, 16);
                        gpsData.IO_Elements_4B.Add(parameterID, IO_Element_4B);
                        ShowDiagnosticInfo("IO element 4B ID: --------".PadRight(40, '-') + " " + parameterID);
                        ShowDiagnosticInfo(IO_4 + "'st 4B IO element value: --------".PadRight(40 - IO_4.ToString().Length, '-') + " " + IO_Element_4B);
                    }
                    currentCursor += IO_Elements_4B_Quantity * 5 + 1;

                    int IO_Elements_8B_Quantity = Convert.ToInt32(receiveBytes.Skip(currentCursor).Take(1).ToList()[0]);
                    ShowDiagnosticInfo("8 byte IO element in record: --------".PadRight(40, '-') + " " + IO_Elements_8B_Quantity);

                    for (int IO_8 = 0; IO_8 < IO_Elements_8B_Quantity; IO_8++)
                    {
                        var    parameterID = (byte)Convert.ToInt32(receiveBytes.Skip(currentCursor + 1 + IO_8 * 9).Take(1).ToList()[0]);
                        string value       = string.Empty;
                        receiveBytes.Skip(currentCursor + 2 + IO_8 * 9).Take(8).ToList().ForEach(delegate(byte b) { value += String.Format("{0:X2}", b); });
                        var IO_Element_8B = Convert.ToInt64(value, 16);
                        gpsData.IO_Elements_8B.Add(parameterID, IO_Element_8B);
                        ShowDiagnosticInfo("IO element 8B ID: --------".PadRight(40, '-') + " " + parameterID);
                        ShowDiagnosticInfo(IO_8 + "'st 8B IO element value: --------".PadRight(40 - IO_8.ToString().Length, '-') + " " + IO_Element_8B);
                    }

                    tokenAddress += 30 + IO_Elements_1B_Quantity * 2 +
                                    IO_Elements_2B_Quantity * 3 + IO_Elements_4B_Quantity * 5
                                    + IO_Elements_8B_Quantity * 9;
                }
                else
                {
                    tokenAddress += 30;
                }

                //  Data dt = new Data();

                gpsData.Altitude  = (short)altitude;
                gpsData.Direction = (short)angle;
                gpsData.Lat       = latitude;
                gpsData.Long      = longtitude;
                gpsData.Priority  = (byte)priority;
                gpsData.Satellite = (byte)satellites;
                gpsData.Speed     = (short)speed;
                gpsData.Timestamp = timestamp;
                gpsData.IMEI      = IMEI.Substring(0, 15);
                //  dt.SaveGPSPositionFMXXXX(gpsData);
            }
            //CRC for check of data correction and request again data from device if it not correct
            string crcString = string.Empty;

            receiveBytes.Skip(dataLength + 8).Take(4).ToList().ForEach(delegate(byte b) { crcString += String.Format("{0:X2}", b); });
            int CRC = Convert.ToInt32(crcString, 16);

            ShowDiagnosticInfo("CRC: -----".PadRight(40, '-') + " " + CRC);
            //We must skeep first 8 bytes and last 4 bytes with CRC value.
            int calculatedCRC = GetCRC16(receiveBytes.Skip(8).Take(receiveBytes.Count - 12).ToArray());

            ShowDiagnosticInfo("Calculated CRC: -------".PadRight(40, '-') + " " + calculatedCRC);
            ShowDiagnosticInfo("||||||||||||||||||||||||||||||||||||||||||||||||");
            if (calculatedCRC == CRC)
            {
                return(numberOfData);
            }
            else
            {
                ShowDiagnosticInfo("Incorect CRC ");
                return(0);
            }
        }
Esempio n. 5
0
        public override int DecodeAVL(List <byte> receiveBytes, string IMEI)
        {
            string hexDataLength = string.Empty;

            receiveBytes.Skip(4).Take(4).ToList().ForEach(delegate(byte b) { hexDataLength += String.Format("{0:X2}", b); });
            int dataLength = Convert.ToInt32(hexDataLength, 16);

            ShowDiagnosticInfo("Data Length: ----- " + dataLength);
            int codecId = Convert.ToInt32(receiveBytes.Skip(8).Take(1).ToList()[0]);

            ShowDiagnosticInfo("Codec ID: ----- " + codecId);
            int numberOfData = Convert.ToInt32(receiveBytes.Skip(9).Take(1).ToList()[0]);;

            ShowDiagnosticInfo("Number of data: ---- " + numberOfData);

            int  nextPacketStartAddress = 10;
            Data dt = new Data();

            for (int n = 0; n < numberOfData; n++)
            {
                string hexTimeStamp = string.Empty;
                receiveBytes.Skip(nextPacketStartAddress).Take(4).ToList().ForEach(delegate(byte b) { hexTimeStamp += String.Format("{0:X2}", b); });

                //ShowDiagnosticInfo(bit_30_timestamp);
                var  result = Convert.ToInt64(hexTimeStamp, 16) & 0x3FFFFFFF;
                long timeSt = Convert.ToInt64(result);
                // long timeSt = Convert.ToInt64(Convert.ToString(Convert.ToInt32(hexTimeStamp, 16), 2).Substring(2, 30), 2);
                //long timeSt = Convert.ToInt64(hexTimeStamp.Substring(2, 30), 16);

                // For GH3000 time is seconds from 2007.01.01 00:00:00
                DateTime origin    = new DateTime(2007, 1, 1, 0, 0, 0, 0);
                DateTime timestamp = origin.AddSeconds(Convert.ToDouble(timeSt));

                //DateTime timestamp = DateTime.FromBinary(timeSt);
                ShowDiagnosticInfo("Timestamp: ----- " + timestamp.ToLongDateString() + " " + timestamp.ToLongTimeString());

                int priority = (Convert.ToByte(hexTimeStamp.Substring(0, 2), 16) & 0xC0) / 64;//Convert.ToInt32(receiveBytes.Skip(nextPacketStartAddress + 8).Take(1));
                ShowDiagnosticInfo("Priority: ------------ " + priority);

                // If ALARM send SMS
                // if (priority == 2)
                //     SMSsender.SendSms(dt.GetAlarmNumberFromModemId(IMEI), "5555555", "Alarm button pressed", 3, true);

                GlobalMask globalMask = (GlobalMask)receiveBytes.Skip(nextPacketStartAddress + 4).Take(1).First();
                GPSmask    gpsMask    = (GPSmask)receiveBytes.Skip(nextPacketStartAddress + 5).Take(1).First();

                GPSdata gpsData = new GPSdata();
                gpsData.Priority  = (byte)priority;
                gpsData.Timestamp = timestamp;
                int gpsElementDataAddress = 0;
                if ((globalMask & GH3000Parser.GlobalMask.GPSelement) != 0)
                {
                    if ((gpsMask & GH3000Parser.GPSmask.LatAndLong) != 0)
                    {
                        gpsElementDataAddress = 6;
                        string longt = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + gpsElementDataAddress + 4).Take(4).ToList().ForEach(delegate(byte b) { longt += String.Format("{0:X2}", b); });
                        float longtitude = GetFloatIEE754(receiveBytes.Skip(nextPacketStartAddress + gpsElementDataAddress + 4).Take(4).ToArray());
                        ShowDiagnosticInfo("Longtitude: ----- " + longtitude);

                        string lat = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + gpsElementDataAddress).Take(4).ToList().ForEach(delegate(byte b) { lat += String.Format("{0:X2}", b); });
                        float latitude = GetFloatIEE754(receiveBytes.Skip(nextPacketStartAddress + gpsElementDataAddress).Take(4).ToArray());
                        ShowDiagnosticInfo("Latitude: ----- " + latitude);
                        gpsElementDataAddress += 8;
                        gpsData.Lat            = latitude;
                        gpsData.Long           = longtitude;
                    }
                    if ((gpsMask & GH3000Parser.GPSmask.Altitude) != 0)
                    {
                        string alt = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + gpsElementDataAddress).Take(2).ToList().ForEach(delegate(byte b) { alt += String.Format("{0:X2}", b); });
                        int altitude = Convert.ToInt32(alt, 16);
                        ShowDiagnosticInfo("Altitude: ----- " + altitude);
                        gpsElementDataAddress += 2;
                        gpsData.Altitude       = (short)altitude;
                    }

                    if ((gpsMask & GH3000Parser.GPSmask.Angle) != 0)
                    {
                        string ang = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + gpsElementDataAddress).Take(1).ToList().ForEach(delegate(byte b) { ang += String.Format("{0:X2}", b); });
                        int angle = Convert.ToInt32(ang, 16);
                        angle = Convert.ToInt32(angle * 360.0 / 256.0);
                        ShowDiagnosticInfo("Angle: ----- " + angle);
                        gpsElementDataAddress += 1;
                        gpsData.Direction      = (short)angle;
                    }

                    if ((gpsMask & GH3000Parser.GPSmask.Speed) != 0)
                    {
                        string sp = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + gpsElementDataAddress).Take(1).ToList().ForEach(delegate(byte b) { sp += String.Format("{0:X2}", b); });
                        int speed = Convert.ToInt32(sp, 16);
                        ShowDiagnosticInfo("Speed: ----- " + speed);
                        gpsElementDataAddress += 1;
                        gpsData.Speed          = (short)speed;
                    }

                    if ((gpsMask & GH3000Parser.GPSmask.Sattelites) != 0)
                    {
                        int satellites = Convert.ToInt32(receiveBytes.Skip(nextPacketStartAddress + gpsElementDataAddress).Take(1).ToList()[0]);
                        ShowDiagnosticInfo("Satellites: ----- " + satellites);
                        gpsElementDataAddress += 1;
                        gpsData.Satellite      = (byte)satellites;
                    }

                    if ((gpsMask & GH3000Parser.GPSmask.LocalAreaCodeAndCellID) != 0)
                    {
                        string localArea = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + gpsElementDataAddress).Take(2).ToList().ForEach(delegate(byte b) { localArea += String.Format("{0:X2}", b); });
                        int localAreaCode = Convert.ToInt32(localArea, 16);
                        ShowDiagnosticInfo("Local area code: ----- " + localAreaCode);
                        gpsElementDataAddress += 2;
                        gpsData.LocalAreaCode  = (short)localAreaCode;

                        string cell_ID = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + gpsElementDataAddress).Take(2).ToList().ForEach(delegate(byte b) { cell_ID += String.Format("{0:X2}", b); });
                        int cellID = Convert.ToInt32(cell_ID, 16);
                        ShowDiagnosticInfo("Cell ID: ----- " + localAreaCode);
                        gpsElementDataAddress += 2;
                        gpsData.CellID         = (short)cellID;
                    }

                    if ((gpsMask & GH3000Parser.GPSmask.SignalQuality) != 0)
                    {
                        string gsmQua = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + gpsElementDataAddress).Take(1).ToList().ForEach(delegate(byte b) { gsmQua += String.Format("{0:X2}", b); });
                        int gsmSignalQuality = Convert.ToInt32(gsmQua, 16);
                        ShowDiagnosticInfo("GSM signal quality: ----- " + gsmSignalQuality);
                        gpsElementDataAddress   += 1;
                        gpsData.GsmSignalQuality = (byte)gsmSignalQuality;
                    }

                    if ((gpsMask & GH3000Parser.GPSmask.OperatorCode) != 0)
                    {
                        string opCode = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + gpsElementDataAddress).Take(4).ToList().ForEach(delegate(byte b) { opCode += String.Format("{0:X2}", b); });
                        int operatorCode = Convert.ToInt32(opCode, 16);
                        ShowDiagnosticInfo("Operator code: ----- " + operatorCode);
                        gpsElementDataAddress += 4;
                        gpsData.OperatorCode   = operatorCode;
                    }
                }
                nextPacketStartAddress += gpsElementDataAddress;
                if ((globalMask & GH3000Parser.GlobalMask.IO_Element_1B) != 0)
                {
                    byte quantityOfIOelementData = receiveBytes.Skip(nextPacketStartAddress).Take(1).First();
                    nextPacketStartAddress += 1;
                    for (int i = 0; i < quantityOfIOelementData; i++)
                    {
                        byte parameterID = receiveBytes.Skip(nextPacketStartAddress).Take(1).First();
                        ShowDiagnosticInfo("IO element 1B ID: -----".PadRight(40, '-') + " " + parameterID);
                        byte parameterValue = receiveBytes.Skip(nextPacketStartAddress + 1).Take(1).First();
                        ShowDiagnosticInfo("IO element 1B value: -----".PadRight(40, '-') + " " + parameterValue);
                        gpsData.IO_Elements_1B.Add(parameterID, parameterValue);
                        nextPacketStartAddress += 2;

                        //--------------alarm send by sms
                        if (parameterID == 222)
                        {
                            string message = string.Empty;

                            //switch (parameterValue)
                            //{
                            //    case 5:
                            //        message = "Man-down sensor activated from " + IMEI + " at this place " + "http://maps.google.com/maps?q=" + gpsData.Lat.Value.ToString(CultureInfo.InvariantCulture) + "," + gpsData.Long.Value.ToString(CultureInfo.InvariantCulture);
                            //        SMSsender.SendSms(dt.GetAlarmNumberFromModemId(IMEI), "55555555", message, 3, true);
                            //        break;
                            //    case 1:
                            //        message = "Emergency button activated from " + IMEI + " at this place " + "http://maps.google.com/maps?q=" + gpsData.Lat.Value.ToString(CultureInfo.InvariantCulture) + "," + gpsData.Long.Value.ToString(CultureInfo.InvariantCulture);
                            //        SMSsender.SendSms(dt.GetAlarmNumberFromModemId(IMEI), "55555555", message, 3, true);
                            //        break;
                            //}
                        }

                        //------------------- end
                    }
                }

                if ((globalMask & GH3000Parser.GlobalMask.IO_Element_2B) != 0)
                {
                    byte quantityOfIOelementData = receiveBytes.Skip(nextPacketStartAddress).Take(1).First();
                    nextPacketStartAddress += 1;
                    for (int i = 0; i < quantityOfIOelementData; i++)
                    {
                        byte parameterID = receiveBytes.Skip(nextPacketStartAddress).Take(1).First();
                        ShowDiagnosticInfo("IO element 2B ID: -----".PadRight(40, '-') + " " + parameterID);
                        string value = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + 1).Take(2).ToList().ForEach(delegate(byte b) { value += String.Format("{0:X2}", b); });
                        short parameterValue = (short)Convert.ToInt32(value, 16);
                        ShowDiagnosticInfo("IO element 2B value: -----".PadRight(40, '-') + " " + parameterValue);
                        gpsData.IO_Elements_2B.Add(parameterID, parameterValue);
                        nextPacketStartAddress += 3;
                    }
                }
                if ((globalMask & GH3000Parser.GlobalMask.IO_Element_4B) != 0)
                {
                    byte quantityOfIOelementData = receiveBytes.Skip(nextPacketStartAddress).Take(1).First();
                    nextPacketStartAddress += 1;
                    for (int i = 0; i < quantityOfIOelementData; i++)
                    {
                        byte parameterID = receiveBytes.Skip(nextPacketStartAddress).Take(1).First();
                        ShowDiagnosticInfo("IO element 4B ID: -----".PadRight(40, '-') + " " + parameterID);
                        string value = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + 1).Take(4).ToList().ForEach(delegate(byte b) { value += String.Format("{0:X2}", b); });
                        int parameterValue = Convert.ToInt32(value, 16);
                        ShowDiagnosticInfo("IO element 4B value: -----".PadRight(40, '-') + " " + parameterValue);
                        gpsData.IO_Elements_4B.Add(parameterID, parameterValue);
                        nextPacketStartAddress += 5;
                    }
                }

                gpsData.IMEI = IMEI.Substring(0, 15);
                dt.SaveGPSPositionGH3000(gpsData);
            }
            int numberOfData1 = Convert.ToInt32(receiveBytes.Skip(nextPacketStartAddress).Take(1).ToList()[0]);

            //CRC for check of data correction and request again data from device if it not correct
            string crcString = string.Empty;

            receiveBytes.Skip(dataLength + 8).Take(4).ToList().ForEach(delegate(byte b) { crcString += String.Format("{0:X2}", b); });
            int CRC = Convert.ToInt32(crcString, 16);

            ShowDiagnosticInfo("CRC: -----".PadRight(40, '-') + " " + CRC);
            //We must skeep first 8 bytes and last 4 bytes with CRC value.
            int calculatedCRC = GetCRC16(receiveBytes.Skip(8).Take(receiveBytes.Count - 12).ToArray());

            ShowDiagnosticInfo("Calculated CRC: -------".PadRight(40, '-') + " " + calculatedCRC);
            ShowDiagnosticInfo("||||||||||||||||||||||||||||||||||||||||||||||||");
            if (calculatedCRC == CRC)
            {
                return(numberOfData);
            }
            else
            {
                ShowDiagnosticInfo("Incorect CRC ");
                return(0);
            }
        }
Esempio n. 6
0
        private AngleSunDeviationCalcResult CalculateDevDataForImage(FileInfo finfo, Dictionary <string, object> defaultProperties, LogWindow currImageLogWindow, bool showOnlyErrors = false)
        {
            FileInfo currFileInfo = finfo;
            Dictionary <string, object> defaultProps = defaultProperties;


            AngleSunDeviationCalcResult retRes = new AngleSunDeviationCalcResult()
            {
                fileName = currFileInfo.FullName,
            };

            GPSdata gps = ServiceTools.FindProperGPSdataForImage(currFileInfo.FullName, theLogWindow, defaultProperties,
                                                                 ref NVdataFilesAlreadyReadDateTimeSpans, ref NVdataFilesAlreadyReadData);

            if (gps == null)
            {
                theLogWindow = ServiceTools.LogAText(theLogWindow, "Couldn`t find GPS data for this image.");
                retRes.calculationSucceeded = false;
                retRes.resultMessage        = "Couldn`t find GPS data for this image.";
                return(retRes);
            }

            #region // obsolete
            //// определяем дату-время файла
            //DateTime curDateTime = DateTime.UtcNow;

            //Image anImage = Image.FromFile(currFileInfo.FullName);
            //ImageInfo newIInfo = new ImageInfo(anImage);
            //int minute = 0;
            //String dateTime = (String)newIInfo.getValueByKey("ExifDTOrig");
            //if (dateTime == null)
            //{
            //    //попробуем вытащить из имени файла
            //    string strDateTime = currFileInfo.Name;
            //    strDateTime = strDateTime.Substring(4, 19);
            //    dateTime = strDateTime;
            //}

            //try
            //{
            //    curDateTime = CommonTools.DateTimeOfString(dateTime);
            //}
            //catch (Exception)
            //{
            //    retRes.calculationSucceeded = false;
            //    retRes.resultMessage = "couldn`t get date/time for file: " + Environment.NewLine + currFileInfo.Name;
            //    return retRes;
            //}
            //curDateTime = DateTime.SpecifyKind(curDateTime, DateTimeKind.Utc);

            //GPSdata neededGPSdata = new GPSdata();
            //string currPath = currFileInfo.DirectoryName;


            //string navFilesPath = defaultProps["IoffeMeteoNavFilesDirectory"] as string;
            //List<IoffeVesselDualNavDataConverted> lAllNavData = new List<IoffeVesselDualNavDataConverted>();

            //string[] sNavFilenames = Directory.GetFiles(navFilesPath, "*.nv2", SearchOption.AllDirectories);
            //if (!sNavFilenames.Any())
            //{
            //    retRes.calculationSucceeded = false;
            //    retRes.resultMessage = "Не найдено файлов данных навигации в директории " + navFilesPath;
            //    return retRes;
            //}
            //else
            //{
            //    foreach (string navFilename in sNavFilenames)
            //    {
            //        Tuple<DateTime, DateTime> timeSpan =
            //            IoffeVesselNavDataReader.GetNavFileDateTimeMargins(navFilename);
            //        if (timeSpan == null)
            //        {
            //            continue;
            //        }

            //        if ((curDateTime < timeSpan.Item1) || (curDateTime > timeSpan.Item2))
            //        {
            //            continue;
            //        }

            //        List<IoffeVesselDualNavDataConverted> dataHasBeenRead = IoffeVesselNavDataReader.ReadNavFile(navFilename);
            //        if (dataHasBeenRead == null)
            //        {
            //            continue;
            //        }
            //        Application.DoEvents();
            //        lAllNavData.AddRange(dataHasBeenRead);
            //    }
            //}

            //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;
            #endregion // obsolete

            retRes.gpsData = gps;


            #region obsolete
            //double lat = gps.LatDec;
            //double lon = gps.LonDec;


            //SPA spaCalc = new SPA(curDateTime.Year, curDateTime.Month, curDateTime.Day, curDateTime.Hour,
            //    curDateTime.Minute, curDateTime.Second, (float)lon, (float)lat,
            //    (float)SPAConst.DeltaT(curDateTime));
            //int res = spaCalc.spa_calculate();
            //AzimuthZenithAngle sunPositionSPAext = new AzimuthZenithAngle(spaCalc.spa.azimuth,
            //    spaCalc.spa.zenith);
            #endregion obsolete


            AzimuthZenithAngle sunPositionSPAext = gps.SunZenithAzimuth();

            if (!showOnlyErrors)
            {
                currImageLogWindow = ServiceTools.LogAText(currImageLogWindow,
                                                           "SPA ext sun position for " + gps.dateTimeUTC.ToString("s") + ": " + sunPositionSPAext);
            }

            retRes.sunSPAcomputedPosition = sunPositionSPAext;



            Image <Bgr, Byte> img2process = new Image <Bgr, byte>(currFileInfo.FullName);
            img2process = ImageProcessing.ImageResizer(img2process, Convert.ToInt32(defaultProps["DefaultMaxImageSize"]));
            Image <Bgr, Byte> LocalProcessingImage = ImageProcessing.SquareImageDimensions(img2process);

            RoundData sunRoundData = RoundData.nullRoundData();

            //посмотрим, нет ли уже имеющихся данных о положении и размере солнечного диска на изображении
            string sunDiskInfoFileName = ConventionalTransitions.SunDiskInfoFileName(currFileInfo.FullName);
            //string sunDiskInfoFileName = currFileInfo.DirectoryName + "\\" +
            //                             Path.GetFileNameWithoutExtension(currFileInfo.FullName) + "-SunDiskInfo.xml";

            RoundData existingRoundData             = RoundData.nullRoundData();
            Size      imgSizeUnderExistingRoundData = LocalProcessingImage.Bitmap.Size;
            object    existingRoundDataObj          = ServiceTools.ReadObjectFromXML(sunDiskInfoFileName, typeof(RoundDataWithUnderlyingImgSize));

            if (existingRoundDataObj != null)
            {
                existingRoundData             = ((RoundDataWithUnderlyingImgSize)existingRoundDataObj).circle;
                imgSizeUnderExistingRoundData = ((RoundDataWithUnderlyingImgSize)existingRoundDataObj).imgSize;
            }

            double currScale = (double)LocalProcessingImage.Width / (double)imgSizeUnderExistingRoundData.Width;
            if (currScale != 1.0d)
            {
                existingRoundData.DCenterX *= currScale;
                existingRoundData.DCenterY *= currScale;
                existingRoundData.DRadius  *= currScale;
            }
            if (!existingRoundData.IsNull)
            {
                sunRoundData = existingRoundData;
            }

            ImageProcessing imgP = new ImageProcessing(LocalProcessingImage, true);

            if (sunRoundData.IsNull)
            {
                SkyCloudClassification classificator = new SkyCloudClassification(img2process, defaultProperties);
                classificator.verbosityLevel             = 0;
                classificator.ParentForm                 = ParentForm;
                classificator.theLogWindow               = currImageLogWindow;
                classificator.ClassificationMethod       = ClassificationMethods.GrIx;
                classificator.isCalculatingUsingBgWorker = false;
                // classificator.SelfWorker = currBGWsender as BackgroundWorker;
                classificator.defaultOutputDataDirectory = (string)defaultProps["DefaultDataFilesLocation"];
                classificator.theStdDevMarginValueDefiningSkyCloudSeparation =
                    Convert.ToDouble(defaultProps["GrIxDefaultSkyCloudMarginWithoutSun"]);
                classificator.sourceImageFileName = currFileInfo.FullName;

                retRes.imageEdgesDetected = new RoundDataWithUnderlyingImgSize()
                {
                    circle  = imgP.imageRD,
                    imgSize = LocalProcessingImage.Size,
                };
                DenseMatrix dmProcessingData = (DenseMatrix)imgP.eval("grix").Clone();
                try
                {
                    sunRoundData = classificator.DetectSunWithSerieOfArcs(imgP, dmProcessingData);
                    if (!sunRoundData.IsNull)
                    {
                        RoundDataWithUnderlyingImgSize infoToSave = new RoundDataWithUnderlyingImgSize()
                        {
                            circle  = sunRoundData,
                            imgSize = LocalProcessingImage.Size,
                        };
                        ServiceTools.WriteObjectToXML(infoToSave, sunDiskInfoFileName);
                    }
                }
                catch (Exception ex)
                {
                    retRes.calculationSucceeded = false;
                    retRes.resultMessage        = ex.Message;
                    return(retRes);
                }
                ServiceTools.FlushMemory();
            }



            if (sunRoundData.IsNull)
            {
                throw new Exception(finfo.Name + ": couldn`t detect sun position");
            }
            else
            {
                retRes.sunDiskDetectedPosition = new RoundDataWithUnderlyingImgSize()
                {
                    circle  = sunRoundData,
                    imgSize = LocalProcessingImage.Size,
                };
            }



            RoundData imageDetectedRound = imgP.imageRD.Copy();
            retRes.imageEdgesDetected = new RoundDataWithUnderlyingImgSize()
            {
                circle  = imageDetectedRound,
                imgSize = LocalProcessingImage.Size,
            };

            try
            {
                double dev = retRes.computedAzimuthDeviation;
                retRes.calculationSucceeded = true;
            }
            catch (Exception ex)
            {
                retRes.calculationSucceeded = false;
                retRes.resultMessage        = ex.Message;
                return(retRes);
            }

            return(retRes);
        }
Esempio n. 7
0
        private void btnProcessAccelerationTimeSeries_Click(object sender, EventArgs e)
        {
            if ((bgwCalculate != null) && (bgwCalculate.IsBusy))
            {
                bgwCalculate.CancelAsync();
                return;
            }

            //simpleMultipleImagesShow imagesRepresentingForm = new simpleMultipleImagesShow();
            //imagesRepresentingForm.Show();



            DoWorkEventHandler bgwCalculate_DoWorkHandler = delegate(object currBGWsender, DoWorkEventArgs args)
            {
                BackgroundWorker selfWorker = currBGWsender as BackgroundWorker;

                //simpleMultipleImagesShow multImagesRepresentingForm = (simpleMultipleImagesShow)((args.Argument as object[])[0]);
                //Type theShowImagesType = multImagesRepresentingForm.GetType();
                //MethodInfo thePicturePlacingMethodInfo = theShowImagesType.GetMethod("PlaceAPicture");

                int imageRepresentingCounter = 0;

                DateTime dbgDT = new DateTime(2014, 7, 9, 9, 0, 0);
                dbgDT = dbgDT.AddMinutes(33);
                DateTime dtSeriesStart = accSubseries[0].StartTime;
                double   tsOverallSeriesDurationMillisec = (accSubseries[accSubseries.Count - 1].EndTime - accSubseries[0].StartTime).TotalMilliseconds;

                string strToWrite = " fileName ; lat ; lon ; date ; time ; time(s) since start ; period(s) ; spectrum amplitude";
                ServiceTools.logToTextFile(strOutputDirectory + "\\100sData-spectra-maximums.dat", strToWrite + Environment.NewLine, true);

                foreach (TimeSeries <double> accSubseria in accSubseries)
                {
                    if (selfWorker.CancellationPending)
                    {
                        break;
                    }

                    int startindex = 0;
                    while (true)
                    {
                        int endIndex;

                        if (selfWorker.CancellationPending)
                        {
                            break;
                        }

                        TimeSeries <double> currTimeSeria = accSubseria.SubSeria(startindex, new TimeSpan(1000000000),
                                                                                 out endIndex); //100s
                        currTimeSeria = currTimeSeria.InterpolateSeria(new TimeSpan(500000));
                        currTimeSeria = currTimeSeria.ExtractDataDeviationValues();

                        //обработать и оценить наличие выраженных периодов
                        Complex[] sourceSignalArray = currTimeSeria.DataRealValuesComplexArray();
                        Fourier.Forward(sourceSignalArray);
                        //Transform.FourierForward(sourceSignalArray);
                        List <Complex> FourierTransformedSignal           = new List <Complex>(sourceSignalArray);
                        List <double>  FourierTransformedSignalAmplitudes =
                            FourierTransformedSignal.ConvertAll <double>(
                                cVal =>
                                ((double.IsNaN(cVal.Magnitude)) || (double.IsInfinity(cVal.Magnitude)))
                                        ? (0.0d)
                                        : (cVal.Magnitude));
                        List <double> FourierTransformedSignalPeriods = new List <double>();
                        for (int ind = 0; ind < FourierTransformedSignalAmplitudes.Count; ind++)
                        {
                            FourierTransformedSignalPeriods.Add(currTimeSeria.TotalSeriaDuration.TotalSeconds /
                                                                (double)ind);
                        }

                        FourierTransformedSignalAmplitudes =
                            new List <double>(
                                FourierTransformedSignalAmplitudes.Zip <double, double, double>(
                                    FourierTransformedSignalPeriods,
                                    (amp, periodSec) =>
                                    ((double.IsNaN(periodSec)) || (double.IsInfinity(periodSec))) ? (0.0d) : (amp)));
                        FourierTransformedSignalPeriods =
                            FourierTransformedSignalPeriods.ConvertAll <double>(
                                dval => ((double.IsNaN(dval)) || (double.IsInfinity(dval))) ? (0.0d) : (dval));


                        //проанализируем этот участок - есть ли выраженные пики по амплитуде конкретных частот

                        //найти максимум в спектре и выдать данные об этом максимуме в файл

                        // сначала отфильтруем периоды меньше 1с - для данных по динамике судна они несущественны
                        FourierTransformedSignalAmplitudes =
                            new List <double>(
                                FourierTransformedSignalAmplitudes.Zip <double, double, double>(
                                    FourierTransformedSignalPeriods,
                                    (amp, periodSec) => (periodSec <= 1.0d) ? (0.0d) : (amp)));
                        FourierTransformedSignalPeriods =
                            FourierTransformedSignalPeriods.ConvertAll <double>(dVal => (dVal <= 1.0d) ? (0.0d) : (dVal));


                        DescriptiveStatistics currAmpsStat =
                            new DescriptiveStatistics(FourierTransformedSignalAmplitudes);
                        List <double> lAmpsOutstanding = FourierTransformedSignalAmplitudes.ConvertAll <double>(dVal =>
                        {
                            if (dVal / currAmpsStat.Mean >= 100.0d)
                            {
                                return(dVal);
                            }
                            return(0.0d);
                        });
                        List <double> lPeriodsOutstanding =
                            new List <double>(FourierTransformedSignalPeriods.Zip <double, double, double>(lAmpsOutstanding,
                                                                                                           (per, amp) => (amp == 0.0d) ? (0.0d) : (per)));


                        if (lAmpsOutstanding.Sum() > 0.0d)
                        {
                            MultipleScatterAndFunctionsRepresentation renderer =
                                new MultipleScatterAndFunctionsRepresentation(2048, 1536);

                            renderer.dvScatterXSpace.Add(currTimeSeria.TimeStampsValuesSeconds);
                            renderer.dvScatterFuncValues.Add(currTimeSeria.dvDoubleDataValues);
                            renderer.scatterLineColors.Add(new Bgr(255, 50, 50));
                            renderer.scatterDrawingVariants.Add(SequencesDrawingVariants.polyline);

                            renderer.dvScatterXSpace.Add(DenseVector.OfEnumerable(FourierTransformedSignalPeriods));
                            renderer.dvScatterFuncValues.Add(DenseVector.OfEnumerable(FourierTransformedSignalAmplitudes));
                            renderer.scatterLineColors.Add(new Bgr(50, 255, 50));
                            renderer.scatterDrawingVariants.Add(SequencesDrawingVariants.squares);

                            renderer.dvScatterXSpace.Add(DenseVector.OfEnumerable(lPeriodsOutstanding));
                            renderer.dvScatterFuncValues.Add(DenseVector.OfEnumerable(lAmpsOutstanding));
                            renderer.scatterLineColors.Add(new Bgr(50, 50, 255));
                            renderer.scatterDrawingVariants.Add(SequencesDrawingVariants.circles);

                            renderer.Represent();

                            if (strOutputDirectory != "")
                            {
                                double maxAmp       = lAmpsOutstanding.Max();
                                int    idx          = lAmpsOutstanding.FindIndex(dval => dval == maxAmp);
                                double maxAmpPeriod = lPeriodsOutstanding[idx];

                                GPSdata gpsMark = gpsSeriaData.GetMostClose(currTimeSeria.StartTime).Item2;

                                string fName = currTimeSeria.StartTime.ToString("s").Replace(":", "-") +
                                               "-100sData-spectrum.jpg";
                                renderer.SaveToImage(strOutputDirectory + "\\" + fName, true);

                                strToWrite  = "" + fName + " ; ";
                                strToWrite += gpsMark.LatDec + " ; ";
                                strToWrite += gpsMark.LonDec + " ; ";
                                strToWrite += currTimeSeria.StartTime.Date.ToString("yyyy-MM-dd") + " ; ";
                                strToWrite += currTimeSeria.StartTime.ToString("HH-mm-ss") + " ; ";
                                strToWrite += (currTimeSeria.StartTime - dtSeriesStart).TotalSeconds + " ; ";
                                strToWrite += maxAmpPeriod.ToString() + " ; ";
                                strToWrite += maxAmp.ToString() + " ; ";
                                ServiceTools.logToTextFile(strOutputDirectory + "\\100sData-spectra-maximums.dat", strToWrite + Environment.NewLine, true);

                                ThreadSafeOperations.SetText(lblStatusString, "processing: " + currTimeSeria.StartTime.ToString("s"), false);
                            }
                        }

                        if ((currTimeSeria.StartTime >= dbgDT) || (currTimeSeria.EndTime >= dbgDT))
                        {
                            startindex++;
                            startindex--;
                        }

                        if (endIndex == accSubseria.Count - 1)
                        {
                            break;
                        }

                        Application.DoEvents();

                        selfWorker.ReportProgress(Convert.ToInt32(100.0d * (currTimeSeria.EndTime - dtSeriesStart).TotalMilliseconds / tsOverallSeriesDurationMillisec));

                        startindex += Convert.ToInt32((endIndex - startindex) / 2.0d);
                    }
                }
            };

            RunWorkerCompletedEventHandler bgwCalculate_CompletedHandler = delegate(object currBGWCompletedSender, RunWorkerCompletedEventArgs args)
            {
                ThreadSafeOperations.ToggleButtonState(btnProcessAccelerationTimeSeries, true, "Process acceleration timeseries", false);
            };

            ProgressChangedEventHandler bgwCalculate_ProgressChanged = delegate(object bgwDataReaderSender, ProgressChangedEventArgs args)
            {
                ThreadSafeOperations.UpdateProgressBar(prbReadingProcessingData, args.ProgressPercentage);
            };



            ThreadSafeOperations.ToggleButtonState(btnProcessAccelerationTimeSeries, true, "STOP", true);

            bgwCalculate = new BackgroundWorker();
            bgwCalculate.WorkerSupportsCancellation = true;
            bgwCalculate.WorkerReportsProgress      = true;
            bgwCalculate.DoWork             += bgwCalculate_DoWorkHandler;
            bgwCalculate.RunWorkerCompleted += bgwCalculate_CompletedHandler;
            bgwCalculate.ProgressChanged    += bgwCalculate_ProgressChanged;
            //object[] bgwCalculateArgs = new object[] { imagesRepresentingForm };
            object[] bgwCalculateArgs = new object[] {  };

            bgwCalculate.RunWorkerAsync(bgwCalculateArgs);
        }
Esempio n. 8
0
        private void btnReadData_Click(object sender, EventArgs e)
        {
            strLogFilesDirectory = tbLogFilesPath.Text;

            if (bgwDataReader != null && bgwDataReader.IsBusy)
            {
                bgwDataReader.CancelAsync();
                return;
            }

            ThreadSafeOperations.ToggleButtonState(btnReadData, true, "CANCEL", true);


            DoWorkEventHandler currDoWorkHandler = delegate(object currBGWsender, DoWorkEventArgs args)
            {
                BackgroundWorker selfworker = currBGWsender as BackgroundWorker;

                List <double>   lTotalDataToAdd = new List <double>();
                List <DateTime> lDateTimeList   = new List <DateTime>();


                DirectoryInfo dInfo = new DirectoryInfo(strLogFilesDirectory);

                FileInfo[] fInfoArr = dInfo.GetFiles("*AccelerometerDataLog*.nc");

                int fInfoCounter = 0;

                foreach (FileInfo fInfo in fInfoArr)
                {
                    if (selfworker.CancellationPending)
                    {
                        break;
                    }
                    fInfoCounter++;
                    selfworker.ReportProgress(Convert.ToInt32((double)(fInfoCounter - 1) / (double)fInfoArr.Length));

                    ThreadSafeOperations.SetText(lblStatusString, "reading " + fInfo.FullName, false);


                    Dictionary <string, object> dictDataLoaded = NetCDFoperations.ReadDataFromFile(fInfo.FullName);

                    string varNameDateTime = "DateTime";
                    if (dictDataLoaded.Keys.Contains("DateTime"))
                    {
                        varNameDateTime = "DateTime";
                    }
                    else if (dictDataLoaded.Keys.Contains("Datetime"))
                    {
                        varNameDateTime = "Datetime";
                    }
                    List <long>     currFileDateTimeLongTicksList = new List <long>((dictDataLoaded[varNameDateTime] as long[]));
                    List <DateTime> currFileDateTimeList          = currFileDateTimeLongTicksList.ConvertAll(longVal => new DateTime(longVal));

                    string varNameAccData                  = "AccelerometerData";
                    List <AccelerometerData> lAccData      = AccelerometerData.OfDenseMatrix(dictDataLoaded[varNameAccData] as DenseMatrix);
                    List <double>            lAccDataToAdd = lAccData.ConvertAll <double>(acc => acc.AccMagnitude);

                    lTotalDataToAdd.AddRange(lAccDataToAdd);
                    lDateTimeList.AddRange(currFileDateTimeList);

                    selfworker.ReportProgress(Convert.ToInt32((double)(fInfoCounter) / (double)fInfoArr.Length));
                }

                accSeriaData.AddSubseriaData(lTotalDataToAdd, lDateTimeList);



                //теперь обработаем считанные данные
                ThreadSafeOperations.SetText(lblStatusString, "basic acceleration data processing...", false);

                accSubseries = accSeriaData.SplitWithTimeSpanCondition(dt => dt.TotalMilliseconds >= 1200);
                accSubseries.RemoveAll(theSeria => theSeria.TotalSeriaDuration.TotalSeconds < 100);

                List <double> listSeriesStats =
                    accSubseries.ConvertAll(timeseria => timeseria.TotalSeriaDuration.TotalSeconds);
                DescriptiveStatistics stats = new DescriptiveStatistics(listSeriesStats);
                string strToShow            = "Acceleration data start time: " + accSubseries[0].StartTime.ToString("s") + Environment.NewLine;
                strToShow += "Acceleration data end time: " + accSubseries[accSubseries.Count - 1].EndTime.ToString("s") + Environment.NewLine;
                strToShow += "total chunks count: " + accSubseries.Count + Environment.NewLine;
                strToShow += "mean chunk duration: " + stats.Mean.ToString("0.##e-00") + " s" + Environment.NewLine;
                strToShow += "min chunk duration: " + stats.Minimum.ToString("0.##e-00") + " s" + Environment.NewLine;
                strToShow += "max chunk duration: " + stats.Maximum.ToString("0.##e-00") + " s" + Environment.NewLine;
                strToShow += "StdDev of chunk duration: " + stats.StandardDeviation.ToString("0.##e-00") + " s" + Environment.NewLine;

                ThreadSafeOperations.SetTextTB(tbReportLog, strToShow, true);



                List <GPSdata>  lTotalGPSDataToAdd = new List <GPSdata>();
                List <DateTime> lGPSDateTimeList   = new List <DateTime>();


                dInfo = new DirectoryInfo(strLogFilesDirectory);

                fInfoArr = dInfo.GetFiles("*GPSDataLog*.nc");

                fInfoCounter = 0;

                foreach (FileInfo fInfo in fInfoArr)
                {
                    if (selfworker.CancellationPending)
                    {
                        break;
                    }
                    fInfoCounter++;
                    selfworker.ReportProgress(Convert.ToInt32((double)(fInfoCounter - 1) / (double)fInfoArr.Length));

                    ThreadSafeOperations.SetText(lblStatusString, "reading " + fInfo.FullName, false);


                    Dictionary <string, object> dictDataLoaded = NetCDFoperations.ReadDataFromFile(fInfo.FullName);

                    string varNameDateTime = "DateTime";
                    if (dictDataLoaded.Keys.Contains("DateTime"))
                    {
                        varNameDateTime = "DateTime";
                    }
                    else if (dictDataLoaded.Keys.Contains("Datetime"))
                    {
                        varNameDateTime = "Datetime";
                    }
                    List <long>     currFileDateTimeLongTicksList = new List <long>((dictDataLoaded[varNameDateTime] as long[]));
                    List <DateTime> currFileDateTimeList          = currFileDateTimeLongTicksList.ConvertAll(longVal => new DateTime(longVal));

                    string         varNameGPSData = "GPSdata";
                    List <GPSdata> lGPSData       = GPSdata.OfDenseMatrix(dictDataLoaded[varNameGPSData] as DenseMatrix);
                    //List<double> lGPSDataToAdd = lGPSData.ConvertAll<double>(acc => acc.AccMagnitude);

                    lTotalGPSDataToAdd.AddRange(lGPSData);
                    lGPSDateTimeList.AddRange(currFileDateTimeList);

                    selfworker.ReportProgress(Convert.ToInt32((double)(fInfoCounter) / (double)fInfoArr.Length));
                }

                gpsSeriaData.AddSubseriaData(lTotalGPSDataToAdd, lGPSDateTimeList);

                //теперь обработаем считанные данные
                ThreadSafeOperations.SetText(lblStatusString, "basic GPS data processing...", false);

                gpsSeriaData.RemoveValues(gpsDatum => ((gpsDatum.lat == 0.0d) && (gpsDatum.lon == 0.0d)));

                gpsSeriaData.RemoveDuplicatedTimeStamps();

                strToShow  = Environment.NewLine + "GPS data start time: " + gpsSeriaData.StartTime.ToString("s") + Environment.NewLine;
                strToShow += "GPS data end time: " + gpsSeriaData.EndTime.ToString("s") + Environment.NewLine;

                ThreadSafeOperations.SetTextTB(tbReportLog, strToShow, true);
            };

            RunWorkerCompletedEventHandler currWorkCompletedHandler = delegate(object currBGWCompletedSender, RunWorkerCompletedEventArgs args)
            {
                ThreadSafeOperations.ToggleButtonState(btnReadData, true, "Read data", true);
            };


            ProgressChangedEventHandler bgwDataReader_ProgressChanged = delegate(object bgwDataReaderSender, ProgressChangedEventArgs args)
            {
                ThreadSafeOperations.UpdateProgressBar(prbReadingProcessingData, args.ProgressPercentage);
            };



            bgwDataReader = new BackgroundWorker();
            bgwDataReader.WorkerSupportsCancellation = true;
            bgwDataReader.WorkerReportsProgress      = true;
            bgwDataReader.DoWork             += currDoWorkHandler;
            bgwDataReader.RunWorkerCompleted += currWorkCompletedHandler;
            bgwDataReader.ProgressChanged    += bgwDataReader_ProgressChanged;
            object[] BGWargs = new object[] { "", "" };
            bgwDataReader.RunWorkerAsync(BGWargs);
        }
        private void BgwSnapshotsFilteringWithSunElevation_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker selfWorker = sender as BackgroundWorker;

            object[] bgwArgs                 = e.Argument as object[];
            string   SnapshotsBasePath       = bgwArgs[0] as string;
            string   concurrentDataFilesPath = bgwArgs[1] as string;
            string   directoryToMoveFilesTo  = bgwArgs[2] as string;

            DirectoryInfo srcDir = new DirectoryInfo(SnapshotsBasePath);

            if (!srcDir.Exists)
            {
                theLogWindow = ServiceTools.LogAText(theLogWindow,
                                                     "Операция не выполнена. Не найдена директория:" + Environment.NewLine + SnapshotsBasePath +
                                                     Environment.NewLine);
                //ThreadSafeOperations.SetTextTB(tbLog, "Операция не выполнена. Не найдена директория:" + Environment.NewLine + fromPath + Environment.NewLine, true);
                return;
            }


            List <FileInfo> lFileList2Process = srcDir.GetFiles("*.jpg", SearchOption.AllDirectories).ToList();
            List <FileInfoWithSnapshotDateTime> lSnapshotsInfos =
                lFileList2Process.ConvertAll(finfo => new FileInfoWithSnapshotDateTime(finfo));


            #region read concurrent data from XML files

            theLogWindow = ServiceTools.LogAText(theLogWindow, "started concurrent data reading");

            List <Tuple <string, ConcurrentData> > lImagesConcurrentData = new List <Tuple <string, ConcurrentData> >();

            List <string> filesListConcurrentData =
                new List <string>(Directory.EnumerateFiles(concurrentDataFilesPath,
                                                           ConventionalTransitions.ImageConcurrentDataFilesNamesPattern(), SearchOption.AllDirectories));

            int totalFilesCountToRead = filesListConcurrentData.Count;
            int filesRead             = 0;
            int currProgressPerc      = 0;
            selfWorker.ReportProgress(0);
            List <Dictionary <string, object> > lDictionariesConcurrentData = new List <Dictionary <string, object> >();
            foreach (string strConcDataXMLFile in filesListConcurrentData)
            {
                Dictionary <string, object> currDict = ServiceTools.ReadDictionaryFromXML(strConcDataXMLFile);
                currDict.Add("XMLfileName", Path.GetFileName(strConcDataXMLFile));

                lDictionariesConcurrentData.Add(currDict);

                #region calculate and report progress

                filesRead++;
                double progress = 100.0d * (double)filesRead / (double)totalFilesCountToRead;
                if (progress - (double)currProgressPerc > 1.0d)
                {
                    currProgressPerc = Convert.ToInt32(progress);
                    selfWorker.ReportProgress(currProgressPerc);
                }

                if (selfWorker.CancellationPending)
                {
                    return;
                }

                #endregion calculate and report progress
            }
            lDictionariesConcurrentData.RemoveAll(dict => dict == null);


            List <ConcurrentData> lConcurrentData =
                lDictionariesConcurrentData.ConvertAll <ConcurrentData>(dict =>
            {
                ConcurrentData retVal = null;
                try
                {
                    retVal = new ConcurrentData(dict);
                    GPSdata gpsOfGPSstring = new GPSdata((string)dict["GPSdata"], GPSdatasources.CloudCamArduinoGPS,
                                                         retVal.datetimeUTC.Date);
                    if (!gpsOfGPSstring.validGPSdata)
                    {
                        throw new Exception("invalid GPS data");
                    }
                }
                catch (Exception ex)
                {
                    string strError = "couldn`t parse XML file " + dict["XMLfileName"] + " : " +
                                      Environment.NewLine + ex.Message;
                    return(null);
                }

                if (retVal.gps.validGPSdata)
                {
                    return(retVal);
                }
                else
                {
                    return(null);
                }
            });
            lConcurrentData.RemoveAll(val => val == null);



            // map obtained concurrent data to images by its datetime
            theLogWindow = ServiceTools.LogAText(theLogWindow, "started concurrent data mapping");
            int totalrecordsToMap = lSnapshotsInfos.Count;
            int recordsMapped     = 0;
            currProgressPerc = 0;
            selfWorker.ReportProgress(0);
            foreach (FileInfoWithSnapshotDateTime info in lSnapshotsInfos)
            {
                DateTime currImgDT = info.dateTime;

                ConcurrentData nearestConcurrentData = lConcurrentData.Aggregate((cDt1, cDt2) =>
                {
                    TimeSpan tspan1 = new TimeSpan(Math.Abs((cDt1.datetimeUTC - currImgDT).Ticks));
                    TimeSpan tspan2 = new TimeSpan(Math.Abs((cDt2.datetimeUTC - currImgDT).Ticks));
                    return((tspan1 <= tspan2) ? (cDt1) : (cDt2));
                });


                if (new TimeSpan(Math.Abs((nearestConcurrentData.datetimeUTC - currImgDT).Ticks)) <=
                    TimeSpanForConcurrentDataMappingTolerance)
                {
                    info.concurrentData = nearestConcurrentData;
                }

                #region calculate and report progress

                recordsMapped++;
                double progress = 100.0d * (double)recordsMapped / (double)totalrecordsToMap;
                if (progress - (double)currProgressPerc > 1.0d)
                {
                    currProgressPerc = Convert.ToInt32(progress);
                    selfWorker.ReportProgress(currProgressPerc);
                }

                if (selfWorker.CancellationPending)
                {
                    return;
                }

                #endregion calculate and report progress
            }

            #endregion read concurrent data from XML files

            List <FileInfo> filesToMove = new List <FileInfo>();
            filesToMove.AddRange(
                lSnapshotsInfos.Where(inf => inf.concurrentData == null).ToList().ConvertAll(inf => inf.finfo));
            lSnapshotsInfos.RemoveAll(inf => inf.concurrentData == null);

            filesToMove.AddRange(lSnapshotsInfos.Where(inf =>
            {
                GPSdata currGPS = new GPSdata(inf.concurrentData.GPSdata, GPSdatasources.CloudCamArduinoGPS,
                                              inf.dateTime.Date);
                var spa = currGPS.SunZenithAzimuth();
                return(spa.ZenithAngle >= 85.0);
            }).ToList().ConvertAll(inf => inf.finfo));


            totalFilesCountToRead = filesToMove.Count;
            filesRead             = 0;
            currProgressPerc      = 0;
            selfWorker.ReportProgress(0);
            foreach (FileInfo inf in filesToMove)
            {
                #region calculate and report progress

                filesRead++;
                double progress = 100.0d * (double)filesRead / (double)totalFilesCountToRead;
                if (progress - (double)currProgressPerc > 1.0d)
                {
                    currProgressPerc = Convert.ToInt32(progress);
                    selfWorker.ReportProgress(currProgressPerc);
                }

                if (selfWorker.CancellationPending)
                {
                    return;
                }

                #endregion calculate and report progress

                string strFilenameMoveTo = directoryToMoveFilesTo +
                                           ((directoryToMoveFilesTo.Last() == Path.DirectorySeparatorChar)
                    ? ("")
                    : (Path.DirectorySeparatorChar.ToString()));

                string currImgFilenameRelPath = ConventionalTransitions.MakeRelativePath(inf.FullName, SnapshotsBasePath);
                strFilenameMoveTo += currImgFilenameRelPath;

                theLogWindow = ServiceTools.LogAText(theLogWindow, "moving " + inf.FullName);

                if (!ServiceTools.CheckIfDirectoryExists(strFilenameMoveTo))
                {
                    theLogWindow = ServiceTools.LogAText(theLogWindow,
                                                         "Unable to move file " + Environment.NewLine + inf.FullName + Environment.NewLine + "to:" +
                                                         Environment.NewLine + strFilenameMoveTo + Environment.NewLine +
                                                         "Directory couldn`t be located or created");
                    continue;
                }

                File.Move(inf.FullName, strFilenameMoveTo);
            }

            selfWorker.ReportProgress(0);
        }
        private void bgwCopier_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker SelfWorker = sender as BackgroundWorker;

            object[] bgwArgs  = e.Argument as object[];
            string   fromPath = bgwArgs[0] as string;
            string   concurrentDataFilesPath = bgwArgs[1] as string;
            string   toPath = bgwArgs[2] as string;
            string   imagesStatsXMLfilesDir = bgwArgs[3] as string;

            DirectoryInfo dir           = new DirectoryInfo(fromPath);
            String        destDirectory = toPath +
                                          ((toPath.Last() == Path.DirectorySeparatorChar)
                                       ? ("")
                                       : (Path.DirectorySeparatorChar.ToString()));

            if (!dir.Exists)
            {
                theLogWindow = ServiceTools.LogAText(theLogWindow,
                                                     "Операция не выполнена. Не найдена директория:" + Environment.NewLine + fromPath +
                                                     Environment.NewLine);
                //ThreadSafeOperations.SetTextTB(tbLog, "Операция не выполнена. Не найдена директория:" + Environment.NewLine + fromPath + Environment.NewLine, true);
                return;
            }


            FileInfo[] FileList2Process = dir.GetFiles("*.jpg", SearchOption.AllDirectories);
            List <Tuple <string, string> > imagesStatsXMLfiles = new List <Tuple <string, string> >();

            if (Directory.Exists(imagesStatsXMLfilesDir))
            {
                imagesStatsXMLfiles =
                    (new DirectoryInfo(imagesStatsXMLfilesDir)).EnumerateFiles(
                        ConventionalTransitions.ImageGrIxYRGBstatsFileNamesPattern(), SearchOption.AllDirectories)
                    .ToList()
                    .ConvertAll(fInfo => new Tuple <string, string>(fInfo.Name, fInfo.FullName));
            }


            DirectoryInfo dirConcurrentDataFiles = new DirectoryInfo(concurrentDataFilesPath);
            List <Tuple <string, DateTime> > lConcurrentDataFiles =
                dirConcurrentDataFiles.EnumerateFiles(ConventionalTransitions.ImageConcurrentDataFilesNamesPattern(),
                                                      SearchOption.AllDirectories).ToList().ConvertAll(fInfo =>
            {
                // data-2015-12-15T06-12-56.0590302Z.xml
                string strDateTimeOfFile = Path.GetFileNameWithoutExtension(fInfo.Name).Substring(5, 28);
                strDateTimeOfFile        = strDateTimeOfFile.Substring(0, 11) +
                                           strDateTimeOfFile.Substring(11).Replace('-', ':');
                DateTime currFileDT = DateTime.Parse(strDateTimeOfFile, null, System.Globalization.DateTimeStyles.AdjustToUniversal);
                currFileDT          = DateTime.SpecifyKind(currFileDT, DateTimeKind.Utc);
                return(new Tuple <string, DateTime>(fInfo.FullName, currFileDT));
            });



            int filesCount = FileList2Process.Length;

            theLogWindow = ServiceTools.LogAText(theLogWindow,
                                                 "searching in directory: " + dir.FullName + Environment.NewLine);
            //ThreadSafeOperations.SetTextTB(tbLog, "searching in directory: " + dir.FullName + Environment.NewLine, true);
            theLogWindow = ServiceTools.LogAText(theLogWindow, "files found count: " + filesCount + Environment.NewLine);
            //ThreadSafeOperations.SetTextTB(tbLog, "files found count: " + filesCount + Environment.NewLine, true);

            String usedDateTimes = "";

            List <DateTime> listUsedHours = new List <DateTime>();

            int counter = 0;

            foreach (FileInfo fileInfo in FileList2Process)
            {
                if (SelfWorker.CancellationPending)
                {
                    break;
                }
                counter++;
                double percCounter = (double)counter * 1000.0d / (double)filesCount;
                SelfWorker.ReportProgress(Convert.ToInt32(percCounter));


                Image     anImage  = Image.FromFile(fileInfo.FullName);
                ImageInfo newIInfo = new ImageInfo(anImage);
                //ThreadSafeOperations.SetTextTB(tbLog, "processing file " + fileInfo.Name + Environment.NewLine, true);


                //String curDateTime = "";
                int minute = 0;
                //String dateTime = (String)newIInfo.getValueByKey("DateTime");
                String strDateTimeEXIF = (String)newIInfo.getValueByKey("ExifDTOrig");
                if (strDateTimeEXIF == null)
                {
                    //попробуем вытащить из имени файла
                    string strDateTime = fileInfo.Name;
                    strDateTime     = strDateTime.Substring(4, 19);
                    strDateTimeEXIF = strDateTime;
                }


                //curDateTime = dateTime;
                DateTime curImgDateTime;
                DateTime theHour = RoundToHour(DateTime.UtcNow);
                try
                {
                    //curImgDateTime = DateTimeOfString(strDateTimeEXIF);
                    curImgDateTime = ConventionalTransitions.DateTimeOfSkyImageFilename(fileInfo.Name);
                    theHour        = RoundToHour(curImgDateTime);
                }
                catch (Exception ex)
                {
                    continue;
                }


                //minute = Convert.ToInt32(strDateTimeEXIF.Substring(14, 2));

                //if ((minute == 0) && (!listUsedHours.Contains(theHour)))
                if (new TimeSpan(Math.Abs((theHour - curImgDateTime).Ticks)) <= filterTolerance)
                {
                    #region copy the image file itself
                    listUsedHours.Add(theHour);

                    string dateDirectorySuffix   = curImgDateTime.ToString("yyyy-MM-dd");
                    string currDateDestDirectory = destDirectory + dateDirectorySuffix + Path.DirectorySeparatorChar;
                    if (!ServiceTools.CheckIfDirectoryExists(currDateDestDirectory))
                    {
                        currDateDestDirectory = destDirectory;
                    }



                    String newFileName = currDateDestDirectory + fileInfo.Name;
                    File.Copy(fileInfo.FullName, newFileName);
                    theLogWindow = ServiceTools.LogAText(theLogWindow,
                                                         "COPY: " + fileInfo.FullName + "   >>>   " + newFileName + Environment.NewLine);
                    //ThreadSafeOperations.SetTextTB(tbLog, "COPY: " + fileInfo.FullName + "   >>>   " + newFileName + Environment.NewLine, true);
                    #endregion copy the image file itself


                    #region find and copy the GrIx,YRGB stats data file
                    if (imagesStatsXMLfiles.Any())
                    {
                        string xmlStatsFileName =
                            ConventionalTransitions.ImageGrIxYRGBstatsDataFileName(fileInfo.FullName, "", false);

                        Tuple <string, string> foundXMLfile =
                            imagesStatsXMLfiles.Find(tpl => tpl.Item1 == xmlStatsFileName);

                        if (foundXMLfile != null)
                        {
                            string sStatsXMLfilename   = foundXMLfile.Item2;
                            string newStatsXMLfilename = currDateDestDirectory + foundXMLfile.Item1;
                            File.Copy(sStatsXMLfilename, newStatsXMLfilename);
                            theLogWindow = ServiceTools.LogAText(theLogWindow,
                                                                 "COPY: " + sStatsXMLfilename + "   >>>   " + newStatsXMLfilename + Environment.NewLine);
                            //ThreadSafeOperations.SetTextTB(tbLog,
                            //    "COPY: " + sStatsXMLfilename + "   >>>   " + newStatsXMLfilename + Environment.NewLine,
                            //    true);
                        }
                        else
                        {
                            theLogWindow = ServiceTools.LogAText(theLogWindow,
                                                                 "========== ERROR: couldn`t find GrIx,YRGB stats XML file" + Environment.NewLine);
                            //ThreadSafeOperations.SetTextTB(tbLog,
                            //    "========== ERROR: couldn`t find GrIx,YRGB stats XML file" + Environment.NewLine, true);
                        }
                    }
                    #endregion find and copy the GrIx,YRGB stats data file


                    #region find and copy concurrent data XML file
                    if (lConcurrentDataFiles.Any())
                    {
                        //найдем ближайший по времени
                        List <Tuple <string, TimeSpan> > lCurrFileConcurrentDataNearest =
                            lConcurrentDataFiles.ConvertAll(
                                tpl =>
                                new Tuple <string, TimeSpan>(tpl.Item1,
                                                             new TimeSpan(Math.Abs((tpl.Item2 - curImgDateTime).Ticks))));
                        lCurrFileConcurrentDataNearest.Sort(new Comparison <Tuple <string, TimeSpan> >((tpl1, tpl2) =>
                        {
                            if (tpl1 == null)
                            {
                                if (tpl2 == null)
                                {
                                    return(0);
                                }
                                else
                                {
                                    return(-1);
                                }
                            }
                            else
                            {
                                if (tpl2 == null)
                                {
                                    return(1);
                                }
                                else
                                {
                                    return(tpl1.Item2.CompareTo(tpl2.Item2));
                                }
                            }
                        }));

                        GPSdata gps = new GPSdata();
                        Tuple <string, TimeSpan> nearestConcurrentDataFile = null;
                        int concurrentDataFileIdx = 0;

                        while (!gps.validGPSdata)
                        {
                            nearestConcurrentDataFile = lCurrFileConcurrentDataNearest[concurrentDataFileIdx];

                            Dictionary <string, object> dictSavedData = ServiceTools.ReadDictionaryFromXML(nearestConcurrentDataFile.Item1);

                            gps = new GPSdata((string)dictSavedData["GPSdata"],
                                              GPSdatasources.CloudCamArduinoGPS,
                                              DateTime.Parse((string)dictSavedData["GPSDateTimeUTC"], null,
                                                             System.Globalization.DateTimeStyles.RoundtripKind));

                            concurrentDataFileIdx++;
                        }

                        string currValidConcurrentDataFile         = nearestConcurrentDataFile.Item1;
                        string currValidConcurrentDataFileToCopyTo = currDateDestDirectory + "data-" +
                                                                     Path.GetFileNameWithoutExtension(fileInfo.FullName) + ".xml";
                        File.Copy(currValidConcurrentDataFile, currValidConcurrentDataFileToCopyTo);
                        theLogWindow = ServiceTools.LogAText(theLogWindow,
                                                             "COPY: " + currValidConcurrentDataFile + "   >>>   " + currValidConcurrentDataFileToCopyTo +
                                                             Environment.NewLine);
                        //ThreadSafeOperations.SetTextTB(tbLog,
                        //    "COPY: " + currValidConcurrentDataFile + "   >>>   " + currValidConcurrentDataFileToCopyTo +
                        //    Environment.NewLine, true);
                    }
                    else
                    {
                        theLogWindow = ServiceTools.LogAText(theLogWindow,
                                                             "========== ERROR: couldn`t find concurrent data file for " + fileInfo.FullName +
                                                             Environment.NewLine);
                        //ThreadSafeOperations.SetTextTB(tbLog, "========== ERROR: couldn`t find concurrent data file for " + fileInfo.FullName + Environment.NewLine, true);
                    }
                    #endregion find and copy concurrent data XML file


                    theLogWindow.ClearLog();
                }
            }
        }