Ejemplo n.º 1
0
        public ClusterProcessor(string filename)
        {
            this.filename = filename;
            int linenumber = 0;

            foreach (var line in File.ReadLines(filename).Where(x => x.Length > 0 && x[0] != '/'))
            {
                if (!DateTime.TryParseExact(line.Substring(9, 8), "ddMMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out var endDate))
                {
                    throw new Exception($"invalid date {line.Substring(9, 8)} in cluster file {filename} at line {linenumber + 1}");
                }
                if (!DateTime.TryParseExact(line.Substring(17, 8), "ddMMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out var startDate))
                {
                    throw new Exception($"invalid date {line.Substring(17, 8)} in cluster file {filename} at line {linenumber + 1}");
                }
                if (endDate < startDate)
                {
                    throw new Exception($"End date before start date in cluster file {filename} at line {linenumber + 1}");
                }
                var clusterId  = line.Substring(1, 4);
                var stationNlc = line.Substring(5, 4);

                if (endDate >= DateTime.Today)
                {
                    DictUtils.AddEntryToList(stationToClusters, stationNlc, clusterId);
                    DictUtils.AddEntryToList(clusterToStations, clusterId, stationNlc);
                }

                linenumber++;
            }
        }
Ejemplo n.º 2
0
        private static void Main(string[] args)
        {
            try
            {
                if (args.Length == 0)
                {
                    throw new Exception("Please supply a cluster ID or a station NLC and optionally -r <directory> to set the fares feed directory.");
                }
                var    optionIndex   = args.ToList().FindIndex(x => x == "-r");
                string rjisDirectory = @"s:\";
                if (optionIndex >= 0)
                {
                    if (optionIndex == args.Length - 1)
                    {
                        throw new Exception("the -r option must be followed by a directory name.");
                    }
                    rjisDirectory = args[optionIndex + 1];
                }
                var clusterFilenames = Directory.GetFiles(rjisDirectory, "RJFAF*.FSC").ToList();
                clusterFilenames.RemoveAll(x => !Regex.Match(x, @"RJFAF\d\d\d.FSC").Success);
                if (!clusterFilenames.Any())
                {
                    throw new Exception($"No cluster (.FSC) files in the directory {rjisDirectory}");
                }
                if (clusterFilenames.Count() > 1)
                {
                    throw new Exception($"More than one cluster (.FSC) files in the directory {rjisDirectory}");
                }

                WriteAllStations();

                var clusterInfo          = new ClusterProcessor(clusterFilenames.First());
                var clusterToGridPoints  = new Dictionary <string, List <(int, int)> >();
                var clusterToStationList = new Dictionary <string, List <StationInfo> >();

                for (var i = 0; i < args.Length; i++)
                {
                    if (optionIndex == -1 || (i != optionIndex && i != optionIndex + 1))
                    {
                        var(list, isCluster) = clusterInfo.GetInfo(args[i]);
                        if (list == null)
                        {
                            Console.Error.WriteLine($"{args[i]} nothing found.");
                        }
                        else if (isCluster)
                        {
                            foreach (var station in list)
                            {
                                var name = StationCodeConverter.GetNameFromNlc(station) ?? "name of station unknown.";
                                Console.WriteLine($"{station} {name}");
                                var crs = StationCodeConverter.GetCrsFromNlc(station);
                                if (!string.IsNullOrEmpty(crs))
                                {
                                    var grid = Naptan.GetGridPoint(crs);
                                    if (grid.eastings != int.MaxValue)
                                    {
                                        DictUtils.AddEntryToList(clusterToGridPoints, args[i], grid);
                                        DictUtils.AddEntryToList(clusterToStationList, args[i], new StationInfo
                                        {
                                            Nlc       = station,
                                            Crs       = StationCodeConverter.GetCrsFromNlc(station),
                                            Eastings  = grid.eastings,
                                            Northings = grid.northings,
                                            Name      = StationCodeConverter.GetNameFromNlc(station),
                                        });
                                    }
                                }
                            }
                            var directory = Directory.GetCurrentDirectory();
                            var outfile   = Path.Combine(directory, "clusterpoints.js");
                            var json1     = JsonConvert.SerializeObject(clusterToGridPoints, Formatting.Indented);
                            var json2     = JsonConvert.SerializeObject(clusterToStationList, Formatting.Indented);
                            using (var strw = new StreamWriter(outfile))
                            {
                                strw.WriteLine("clusterInfo=");
                                strw.WriteLine(json1);
                                strw.WriteLine("stationInfo=");
                                strw.WriteLine(json2);
                            }

                            var outFilename    = Path.Combine(directory, "clustersizes.txt");
                            var sortedClusters = clusterInfo.GetAllClusters.OrderByDescending(x => x.Value.Count);
                            using (var str = new StreamWriter(outFilename))
                            {
                                foreach (var cluster in sortedClusters)
                                {
                                    str.WriteLine($"{cluster.Key}, {cluster.Value.Count}");
                                }
                            }
                        }
                        else
                        {
                            foreach (var cluster in list)
                            {
                                Console.WriteLine($"{cluster}");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                var codeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
                var progname = Path.GetFileNameWithoutExtension(codeBase);
                Console.Error.WriteLine(progname + ": Error: " + ex.Message);
            }
        }