Example #1
0
        public double[] ComputeCoefficients(ForecastEquation eq, string pathToR)
        {
            var tbl = eq.ComputeHistoricalCoefficients(eq.StartYear, eq.EndYear);

            // perf.Report("done."); // 1.2 seconds with cache/  33 seconds without
            dataFile = FileUtility.GetTempFileName(".csv");
            CsvFile.WriteToCSV(tbl, dataFile, false);


            dataFile = dataFile.Replace("\\", "/");
            var rInput = new List <string>();

            rInput.Add("# Forecast " + eq.Name);
            rInput.Add("a <- read.csv(\"" + dataFile + "\")");
            rInput.Add("b <- subset(a, select=-Year)");
            rInput.Add("cor(b)");
            rInput.Add("summary(b)");

            string s = "fit <- lm(Y1 ~ ";

            for (int i = 1; i < tbl.Columns.Count - 1; i++)
            {
                s += " + X" + i;
            }
            s += ", data=a)";
            rInput.Add(s);
            rInput.Add("options(width=240)");
            rInput.Add("summary(fit)");
            rInput.Add("coefficients(fit)");

            string   rFile = FileUtility.GetTempFileName(".txt");
            TextFile rtf   = new TextFile(rInput.ToArray());

            rtf.SaveAs(rFile);
            rFile = rFile.Replace("\\", "/");

            var exe = Path.Combine(pathToR, "R\\bin\\R.exe");

            if (!File.Exists(exe))
            {
                throw new Exception("Could not find the R statistic program.  It should be in a sub directory R");
            }

            ProgramRunner pr = new ProgramRunner();

            pr.Run(exe, "--no-restore --no-save --quiet < \"" + rFile);
            pr.WaitForExit();

            Coefficients = GetCoefficients(pr.Output);


            Output = pr.Output;

            return(Coefficients);
        }
Example #2
0
        static void Main(string[] args)
        {
            if (args.Length != 5 && args.Length != 2)
            {
                Console.WriteLine("HydrometForecast.exe basin.csv date level look-ahead output");
                Console.WriteLine("Where: ");
                Console.WriteLine("        basin.csv      -- name of csv input file");
                Console.WriteLine("        date           -- date to run forecaset 2017-01-01");
                Console.WriteLine("        level          -- subsequent conditions  1.0 normal  0.8 for 80%, etc... ");
                Console.WriteLine("        look-ahead     -- perfect forecast 0 or 1");
                Console.WriteLine("        output         -- filename for output");
                Console.WriteLine("Example:  HydrometForecast  heise.csv 2016-1-1 1.0 0 output.txt");

                Console.WriteLine("HydrometForecast.exe basin.csv output.csv");

                return;
            }

            var filename = args[0];


            ForecastEquation eq = new ForecastEquation(filename);

            if (args.Length == 2)
            {
                var cache = new HydrometDataCache();

                cache.Add(eq.GetCbttPcodeList().ToArray(),
                          new DateTime(eq.StartYear - 1, 10, 1),
                          new DateTime(eq.EndYear, 9, 30));

                HydrometMonthlySeries.Cache = cache;

                var tbl = eq.ComputeHistoricalCoefficients(eq.StartYear, eq.EndYear);
                CsvFile.WriteToCSV(tbl, args[1], false);
            }
            else
            {
                DateTime t              = DateTime.Parse(args[1]);
                double   level          = double.Parse(args[2]);
                int      lookAhead      = int.Parse(args[3]);
                string   outputFileName = args[4];

                ForecastResult result = eq.Evaluate(t, lookAhead == 1, level);
                File.WriteAllLines(outputFileName, result.Details.ToArray());
            }
        }