Esempio n. 1
0
        public ImportResult ImportCore(string SelectedPath)
        {
            ImportResult IR = new ImportResult();

            Project Proj = new Project();

            Proj.FileName = $@"{SelectedPath}\*.*";

            if (!ATCFHelperMethods.Export_CheckDirectoryValidForImport(SelectedPath, CoordinateFormat.HURDAT2))
            {
                IR.Status = ExportResults.Error;
                return(IR);
            }
            else
            {
                List <string> FileList = Directory.EnumerateFiles(SelectedPath).ToList();

                Basin Bas = new Basin();

                for (int i = 0; i < FileList.Count; i++)
                {
                    string FileName = FileList[i];

                    List <string> Hurdat2Strings = File.ReadAllLines(FileName).ToList();

                    Storm Sto = new Storm();

                    for (int j = 0; j < Hurdat2Strings.Count(); j++)
                    {
                        string HD2String = Hurdat2Strings[j];

                        Node CN = new Node();

                        // non-header
                        if (j != 0)
                        {
                            List <string> Components = HD2String.Split(',').ToList();

                            // HURDAT2 Format components
                            // 20151020, 0600,  , TD, 13.4N,  94.0W,  25, 1007,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
                            // 0 = date (YYYYMMDD)
                            // 1 = time (YYYYMMDD) ignore this until iris when we will explictly store node dates
                            // 2 = reserved, unused
                            // 3 = category
                            // 4 = latitude
                            // 5 = longitude
                            // 6 = wind speed
                            // 7 = pressure
                            // rest are wind radii 34/50/64kt
                            // Currently we only care about 0, 1, 3, 4, 5, and 6

                            string _Date      = Components[0];
                            string _Time      = Components[1];
                            string _Category  = Components[4];
                            string _Latitude  = Components[5];
                            string _Longitude = Components[6];
                            string _WindSpeed = Components[7];
                            string _Pressure  = Components[8];

                            // Trim everything.
                            _Date      = _Date.Trim();
                            _Time      = _Time.Trim();
                            _Category  = _Category.Trim();
                            _Latitude  = _Latitude.Trim();
                            _Longitude = _Longitude.Trim();
                            _WindSpeed = _WindSpeed.Trim();
                            _Pressure  = _Pressure.Trim();

                            // first real information
                            if (j == 1)
                            {
                                string DateString = $"{_Date}, {_Time}";
                                Sto.FormationDate = ParsingUtil.ParseATCFDateTime(DateString, CoordinateFormat.HURDAT2);
                            }

                            CN.Id = j;
                            RealStormType RST = ATCFHelperMethods.Export_IdentifyRealType(_Category);

                            CN.Intensity = Convert.ToInt32(_WindSpeed);
                            CN.NodeType  = ATCFHelperMethods.Export_GetStormType(_Category);

                            if (CN.NodeType == null)
                            {
                                Error.Throw("Error!", "Invalid or unknown stormtype detected!", ErrorSeverity.Error, 322);
                                IR.Status = ExportResults.Error;
                                return(IR);
                            }

                            Coordinate Coordinate = Coordinate.FromSplitCoordinate(_Longitude, _Latitude, CoordinateFormat.HURDAT2);

                            // TEMP: MOVE WINDOW SIZE TO TRACKMAKER.CORE VOLATILESETTINGS CLASS

                            MainWindow MnWindow = (MainWindow)Application.Current.MainWindow;

                            CN.Position = Bas.FromCoordinateToRelativeNodePosition(Coordinate, new Point(MnWindow.Width, MnWindow.Height));

                            CN.Pressure = Convert.ToInt32(_Pressure);
                            Sto.AddNode(CN);
                        }
                        // this can and will be refactored
                        else
                        {
                            // HURDAT2 Format Header
                            string HD2Header = HD2String;

                            List <string> HD2HeaderComponents = HD2Header.Split(',').ToList();

                            string HD2ID            = HD2HeaderComponents[0];
                            string HD2Name          = HD2HeaderComponents[1];
                            string HD2AdvisoryCount = HD2HeaderComponents[2]; // we don't use this

                            if (HD2ID.Length != 8)
                            {
                                Error.Throw("Error!", "Invalid ID field in HURDAT2 storm header.", ErrorSeverity.Error, 323);
                                IR.Status = ExportResults.Error;
                                return(IR);
                            }

                            string BasinAbbreviation = HD2ID.Substring(0, 2);

                            // set up the basin if this is the first node of the first file

                            if (i == 0)
                            {
                                Bas = Proj.GetBasinWithAbbreviation(BasinAbbreviation);

                                if (Bas.CoordsLower == null || Bas.CoordsHigher == null)
                                {
                                    Error.Throw("Error!", "This basin is not supported by the HURDAT2 format as it does not have defined boundaries in Basins.xml.", ErrorSeverity.Error, 326);
                                    IR.Status = ExportResults.Error;
                                    return(IR);
                                }

                                // sets up a background layer for us so we do not need to create one manually
                                Proj.InitBasin(Bas);
                            }

                            Sto.Name = HD2Name;
                            continue;
                        }

                        Sto.AddNode(CN);

                        // set up the storm if this is the first node of any file (TERRIBLE SHIT NO GOOD)
                    }

                    Bas.AddStorm(Sto);
                }

                Proj.AddBasin(Bas);
            }

            IR.Project = Proj;
            IR.Status  = ExportResults.OK;
            return(IR);
        }
Esempio n. 2
0
        public ImportResult Import()
        {
            try
            {
                ImportResult IR = new ImportResult();

                System.Windows.Forms.FolderBrowserDialog OFD = new System.Windows.Forms.FolderBrowserDialog()
                {
                    Description = $"Open {GetName()} format folder",
                };

#if DANO
                StormTypeManager ST2Manager = GlobalState.GetST2Manager();
#else
                MainWindow       MnWindow   = (MainWindow)Application.Current.MainWindow;
                StormTypeManager ST2Manager = MnWindow.ST2Manager;
#endif

                if (OFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    if (OFD.SelectedPath == "")
                    {
                        return(null);
                    }

                    // check the directory
                    if (!ATCFHelperMethods.Export_CheckDirectoryValidForImport(OFD.SelectedPath))
                    {
                        IR.Status = ExportResults.Error;
                        return(IR);
                    }

                    Project Proj = ImportCore(ST2Manager, OFD.SelectedPath);

                    if (Proj == null)
                    {
                        IR.Status = ExportResults.Error;
                        return(IR);
                    }
                    else
                    {
                        IR.Project = Proj;
                        IR.Status  = ExportResults.OK;
                        return(IR);
                    }
                }
                else
                {
                    //temp
                    IR.Status = ExportResults.Cancelled;
                    return(IR);
                }
            }
            catch (DirectoryNotFoundException)
            {
                Error.Throw("Fatal Error", "Attempted to import a nonexistent ATCF DatFolder", ErrorSeverity.Error, 241);
                return(null);
            }
            catch (PathTooLongException)
            {
                Error.Throw("Error", "The path to the file is longer than 260 characters. Please shorten it.", ErrorSeverity.Error, 150);
                return(null);
            }
            catch (FormatException)
            {
                Error.Throw("Error", "Attempted to import a node with invalid pressure or intensity!", ErrorSeverity.Error, 401);
                return(null);
            }
            catch (OverflowException)
            {
                Error.Throw("Error", "Attempted to import a node with invalid pressure or intensity!", ErrorSeverity.Error, 402);
                return(null);
            }
        }