Exemplo n.º 1
0
        static void Main(string[] argList)
        {
            var ver = AssemblyUtility.GetVersion();

            Console.Write("HydrometServer " + ver + " " + AssemblyUtility.CreationDate() + "   System Time = " + DateTime.Now);
            Console.WriteLine();
            if (argList.Length == 0)
            {
                ShowHelp();
                return;
            }

            Arguments args = new Arguments(argList);


            string      errorFileName = "errors.txt";
            Performance perf          = new Performance();
            //   try
            {
                if (args.Contains("debug"))
                {
                    Logger.EnableLogger();
                    Reclamation.TimeSeries.Parser.SeriesExpressionParser.Debug = true;
                }

                if (args.Contains("import-rating-tables"))
                {// --import-rating-tables=site_list.csv  [--generateNewTables]     : updates usgs,idahopower, and owrd rating tables
                    ApplicationTrustPolicy.TrustAll();
                    var cfg = args["import-rating-tables"];
                    if (File.Exists(cfg))
                    {
                        RatingTableDownload.UpdateRatingTables(cfg, args.Contains("generateNewTables"));
                    }
                    else
                    {
                        Console.WriteLine("Error: File not found: " + cfg);
                    }
                    return;
                }

                if (args.Contains("run-crop-charts"))
                {
                    var str_yr = args["run-crop-charts"].Trim();
                    int year   = DateTime.Now.Year;
                    if (str_yr != "")
                    {
                        year = Convert.ToInt32(str_yr);
                    }

                    var server = PostgreSQL.GetPostgresServer("agrimet", "", "agrimet");
                    CropDatesDataSet.DB = server;
                    string dir = CropDatesDataSet.GetCropOutputDirectory(year);
                    Logger.WriteLine("output dir = " + dir);

                    CropChartGenerator.CreateCropReports(year, dir, HydrometHost.PNLinux);
                    return;
                }



                var db = TimeSeriesDatabase.InitDatabase(args);

                if (args.Contains("cli"))
                {
                    TimeInterval interval = TimeInterval.Irregular;
                    if (args["cli"] == "daily")
                    {
                        interval = TimeInterval.Daily;
                    }

                    Console.WriteLine();
                    HydrometServer.CommandLine.PiscesCommandLine cmd = new CommandLine.PiscesCommandLine(db, interval);
                    cmd.PiscesPrompt();

                    return;
                }


                if (args.Contains("processAlarms"))
                {
                    try
                    {
                        Logger.EnableLogger();
                        Logger.WriteLine("Checking for new or unconfirmed Alarms ");
                        var aq = new AlarmManager(db);
                        Logger.WriteLine("Processing Alarms");
                        aq.ProcessAlarms();
                    }
                    catch (Exception e)
                    {
                        Logger.WriteLine(e.Message);
                    }
                    return;
                }


                if (args.Contains("error-log"))
                {
                    errorFileName = args["error-log"];
                    File.AppendAllText(errorFileName, "HydrometServer.exe:  Started " + DateTime.Now.ToString() + "\n");
                }

                string propertyFilter = "";
                if (args.Contains("property-filter"))
                {
                    propertyFilter = args["property-filter"];
                }

                string filter = "";
                if (args.Contains("filter"))
                {
                    filter = args["filter"];
                }

                if (args.Contains("inventory"))
                {
                    db.Inventory();
                }



                if (args.Contains("import")) // import and process data from files
                {
                    bool computeDependencies    = args.Contains("computeDependencies");
                    bool computeDailyOnMidnight = args.Contains("computeDailyOnMidnight");


                    string searchPattern = args["import"];

                    if (searchPattern == "")
                    {
                        searchPattern = "*";
                    }

                    string       incomingPath = ConfigurationManager.AppSettings["incoming"];
                    FileImporter importer     = new FileImporter(db, DatabaseSaveOptions.Upsert);
                    importer.Import(incomingPath, computeDependencies, computeDailyOnMidnight, searchPattern);
                }



                DateTime t1;
                DateTime t2;

                SetupDates(args, out t1, out t2);

                if (args.Contains("import-hydromet-instant"))
                {
                    HydrometHost host = HydrometHost.PN;
                    if (args["import-hydromet-instant"] != "")
                    {
                        host = (HydrometHost)Enum.Parse(typeof(HydrometHost), args["import-hydromet-instant"]);
                    }

                    File.AppendAllText(errorFileName, "begin: import-hydromet-instant " + DateTime.Now.ToString() + "\n");
                    ImportHydrometInstant(host, db, t1.AddDays(-2), t2.AddDays(1), filter, propertyFilter);
                }

                if (args.Contains("import-hydromet-daily"))
                {
                    HydrometHost host = HydrometHost.PN;
                    if (args["import-hydromet-daily"] != "")
                    {
                        host = (HydrometHost)Enum.Parse(typeof(HydrometHost), args["import-hydromet-daily"]);
                    }

                    File.AppendAllText(errorFileName, "begin: import-hydromet-daily " + DateTime.Now.ToString() + "\n");
                    ImportHydrometDaily(host, db, t1, t2, filter, propertyFilter);
                }

                if (args.Contains("import-hydromet-monthly"))
                {
                    File.AppendAllText(errorFileName, "begin: import-hydromet-monthly " + DateTime.Now.ToString() + "\n");
                    ImportHydrometMonthly(db, t1.AddYears(-5), t2.AddDays(1), filter, propertyFilter);
                }


                if (args.Contains("calculate-daily"))
                {
                    DailyTimeSeriesCalculator calc = new DailyTimeSeriesCalculator(db, TimeInterval.Daily,
                                                                                   filter, propertyFilter);
                    File.AppendAllText(errorFileName, "begin: calculate-daily " + DateTime.Now.ToString() + "\n");
                    calc.ComputeDailyValues(t1, t2, errorFileName);
                }

                if (args.Contains("calculate-monthly"))
                {
                    MonthlyTimeSeriesCalculator calc = new MonthlyTimeSeriesCalculator(db, TimeInterval.Monthly,
                                                                                       filter, propertyFilter);
                    File.AppendAllText(errorFileName, "begin: calculate-monthly " + DateTime.Now.ToString() + "\n");
                    //calc.ComputeDailyValues(t1, t2, errorFileName);
                    calc.ComputeMonthlyValues(t1, t2, errorFileName);
                }


                if (args.Contains("copy-daily"))
                {
                    var tablename = args["copy-daily"];

                    if (tablename == "" || args["source"] == "")
                    {
                        Console.WriteLine("Error: --copy-daily=tablename requires tablename, and requires --source=connectionString");
                        ShowHelp();
                        return;
                    }
                    bool   compare          = args.Contains("compare");
                    string connectionString = args["source"];

                    Copy(TimeInterval.Daily, connectionString, tablename, (PostgreSQL)db.Server, t1, t2, compare);
                    return;
                }


                if (args.Contains("update-period-of-record"))
                {
                    var sc = db.GetSeriesCatalog("isfolder=0");

                    var prop = db.GetSeriesProperties(true);
                    for (int i = 0; i < sc.Count; i++)
                    {
                        var s   = db.GetSeries(sc[i].id);
                        var por = s.GetPeriodOfRecord();

                        s.Properties.Set("count", por.Count.ToString());

                        if (por.Count == 0)
                        {
                            s.Properties.Set("t1", "");
                            s.Properties.Set("t2", "");
                        }
                        else
                        {
                            s.Properties.Set("t1", por.T1.ToString("yyyy-MM-dd"));
                            s.Properties.Set("t2", por.T2.ToString("yyyy-MM-dd"));
                        }
                        Console.WriteLine(s.Name);
                    }
                    db.Server.SaveTable(prop);
                }

                db.Server.Cleanup();


                File.AppendAllText(errorFileName, "HydrometServer.exe:  Completed " + DateTime.Now.ToString() + "\n");
            }
            //catch (Exception e )
            //{
            //    Logger.WriteLine(e.Message);
            //    File.AppendAllText(errorFileName, "Error: HydrometServer.exe: \n"+e.Message);
            //    // Console.ReadLine();
            //    throw e;
            //}

            var  mem = GC.GetTotalMemory(true);
            long mb  = mem / 1024 / 1024;

            Console.WriteLine("Memory Usage: " + mb.ToString() + " Mb");
            perf.Report("HydrometServer: finished ");
        }
Exemplo n.º 2
0
        static void Main(string[] argList)
        {
            Console.Write("HydrometServer " + Application.ProductVersion + "\n compiled: " + AssemblyUtility.CreationDate() + "\n");
            Console.WriteLine("System Time =" + DateTime.Now);

            Arguments args = new Arguments(argList);
            var       p    = new OptionSet();

            var cli = "";

            p.Add("cli=", "interface --cli=instant|daily|monthly", x => cli = x);

            try
            {
                p.Parse(argList);
            }
            catch (OptionException e)
            {
                Console.WriteLine(e.Message);
            }
            if (argList.Length == 0)
            {
                ShowHelp(p);
                return;
            }


            string      errorFileName = "errors.txt";
            Performance perf          = new Performance();
            //   try
            {
                if (args.Contains("debug"))
                {
                    Logger.EnableLogger();
                    Reclamation.TimeSeries.Parser.SeriesExpressionParser.Debug = true;
                }

                if (args.Contains("import-rating-tables"))
                {// --import-rating-tables=site_list.csv  [--generateNewTables]     : updates usgs,idahopower, and owrd rating tables
                    ApplicationTrustPolicy.TrustAll();
                    var cfg = args["import-rating-tables"];
                    if (File.Exists(cfg))
                    {
                        RatingTableDownload.UpdateRatingTables(cfg, args.Contains("generateNewTables"));
                    }
                    else
                    {
                        Console.WriteLine("Error: File not found: " + cfg);
                    }
                    return;
                }

                if (args.Contains("run-crop-charts"))
                {
                    var str_yr = args["run-crop-charts"].Trim();
                    int year   = DateTime.Now.Year;
                    if (str_yr != "")
                    {
                        year = Convert.ToInt32(str_yr);
                    }

                    string dir = CropDatesDataSet.GetCropOutputDirectory(year);
                    Logger.WriteLine("output dir = " + dir);
                    CropChartGenerator.CreateCropReports(year, dir, HydrometHost.PNLinux);
                    return;
                }



                // setup connection to Database
                if (args.Contains("create-database"))
                {
                    if (CreatePostgresDatabase(args))
                    {
                        return;
                    }
                }
                var db = TimeSeriesDatabase.InitDatabase(args);

                if (cli != "")
                {
                    TimeInterval interval = TimeInterval.Irregular;
                    if (cli == "daily")
                    {
                        interval = TimeInterval.Daily;
                    }

                    Console.WriteLine();
                    HydrometServer.CommandLine.PiscesCommandLine cmd = new CommandLine.PiscesCommandLine(db, interval);
                    cmd.PiscesPrompt();

                    return;
                }


                if (args.Contains("error-log"))
                {
                    errorFileName = args["error-log"];
                    File.AppendAllText(errorFileName, "HydrometServer.exe:  Started " + DateTime.Now.ToString() + "\n");
                }

                string propertyFilter = "";
                if (args.Contains("property-filter"))
                {
                    propertyFilter = args["property-filter"];
                }

                string filter = "";
                if (args.Contains("filter"))
                {
                    filter = args["filter"];
                }

                if (args.Contains("inventory"))
                {
                    db.Inventory();
                }



                if (args.Contains("import")) // import and process data from files
                {
                    bool computeDependencies    = args.Contains("computeDependencies");
                    bool computeDailyOnMidnight = args.Contains("computeDailyOnMidnight");


                    string searchPattern = args["import"];

                    if (searchPattern == "")
                    {
                        searchPattern = "*";
                    }

                    string       incomingPath = ConfigurationManager.AppSettings["incoming"];
                    FileImporter importer     = new FileImporter(db, DatabaseSaveOptions.Upsert);
                    importer.Import(incomingPath, RouteOptions.Outgoing, computeDependencies, computeDailyOnMidnight, searchPattern);
                }



                DateTime t1;
                DateTime t2;

                SetupDates(args, out t1, out t2);

                if (args.Contains("import-hydromet-instant"))
                {
                    File.AppendAllText(errorFileName, "begin: import-hydromet-instant " + DateTime.Now.ToString() + "\n");
                    ImportHydrometInstant(db, t1.AddDays(-2), t2.AddDays(1), filter, propertyFilter);
                }

                if (args.Contains("import-hydromet-daily"))
                {
                    File.AppendAllText(errorFileName, "begin: import-hydromet-daily " + DateTime.Now.ToString() + "\n");
                    ImportHydrometDaily(db, t1.AddDays(-500), t2, filter, propertyFilter);
                }

                if (args.Contains("import-hydromet-monthly"))
                {
                    File.AppendAllText(errorFileName, "begin: import-hydromet-monthly " + DateTime.Now.ToString() + "\n");
                    ImportHydrometMonthly(db, t1.AddYears(-5), t2.AddDays(1), filter, propertyFilter);
                }


                if (args.Contains("calculate-daily"))
                {
                    DailyTimeSeriesCalculator calc = new DailyTimeSeriesCalculator(db, TimeInterval.Daily,
                                                                                   filter, propertyFilter);
                    File.AppendAllText(errorFileName, "begin: calculate-daily " + DateTime.Now.ToString() + "\n");
                    calc.ComputeDailyValues(t1, t2, errorFileName);
                }



                if (args.Contains("update-daily"))
                {
                    string sql        = "provider = '" + db.Server.SafeSqlLiteral(args["update-daily"]) + "'";
                    var    updateList = db.GetSeriesCatalog(sql);
                    Console.WriteLine("Updating  " + updateList.Count + " Series ");

                    foreach (var item in updateList)
                    {
                        try
                        {
                            Console.Write(item.Name + " ");
                            var s = db.GetSeries(item.id);
                            s.Update(t1, t2);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }
                }

                if (args.Contains("update-period-of-record"))
                {
                    var sc = db.GetSeriesCatalog("isfolder=0");

                    var prop = db.GetSeriesProperties(true);
                    for (int i = 0; i < sc.Count; i++)
                    {
                        var s   = db.GetSeries(sc[i].id);
                        var por = s.GetPeriodOfRecord();

                        s.Properties.Set("count", por.Count.ToString());

                        if (por.Count == 0)
                        {
                            s.Properties.Set("t1", "");
                            s.Properties.Set("t2", "");
                        }
                        else
                        {
                            s.Properties.Set("t1", por.T1.ToString("yyyy-MM-dd"));
                            s.Properties.Set("t2", por.T2.ToString("yyyy-MM-dd"));
                        }
                        Console.WriteLine(s.Name);
                    }
                    db.Server.SaveTable(prop);
                }

                db.Server.Cleanup();


                File.AppendAllText(errorFileName, "HydrometServer.exe:  Completed " + DateTime.Now.ToString() + "\n");
            }
            //catch (Exception e )
            //{
            //    Logger.WriteLine(e.Message);
            //    File.AppendAllText(errorFileName, "Error: HydrometServer.exe: \n"+e.Message);
            //    // Console.ReadLine();
            //    throw e;
            //}

            var  mem = GC.GetTotalMemory(true);
            long mb  = mem / 1024 / 1024;

            Console.WriteLine("Memory Usage: " + mb.ToString() + " Mb");
            perf.Report("HydrometServer: finished ");
        }