public static int Program(string[] args, Action<RunComplexType, Diagnostics, string[]> programAction )
        {
            bool exception = false;
            RunComplexType runSettings = null;
            Diagnostics diagnostics = null;
            if (File.Exists(args[1]))
            {
                runSettings = FEWSPIProxy.ReadRunFile(args[1]);
                diagnostics = new Diagnostics(runSettings.outputDiagnosticFile, args[0]);
            }

            try
            {
                programAction(runSettings, diagnostics, args);
            }
            catch (Exception e)
            {
                diagnostics.Log(1, string.Format("Exception ({0}: {1}", e.GetType(), e.Message));
                exception = true;
            }
            finally
            {
                if(diagnostics!=null) diagnostics.Save();
            }

            return exception ? 1 : 0;
        }
        public static void Run(RunComplexType runSettings, Diagnostics diagnostics, string[] args)
        {
            var inputSeries = runSettings.AllInputSeries();

            Array.Sort(inputSeries,
                       ((x, y) =>
                           {
                               int locationCompare = String.Compare(x.header.locationId, y.header.locationId,
                                                                    StringComparison.Ordinal);
                               return locationCompare == 0
                                          ? String.Compare(x.header.parameterId, y.header.parameterId,
                                                           StringComparison.Ordinal)
                                          : locationCompare;
                           }));

            Dictionary<string, CSVFileIO> outputSets = new Dictionary<string, CSVFileIO>();
            foreach (TimeSeriesComplexType fewsTS in inputSeries)
            {
                string destinationFn = runSettings.FilenameForTimeSeriesParameter(fewsTS.header.parameterId);
                if (!outputSets.ContainsKey(destinationFn))
                    outputSets[destinationFn] = new CSVFileIO();

                TimeSeries converted = FEWSPIProxy.ConvertTimeSeriesFromFEWS(fewsTS);
                outputSets[destinationFn].use(converted.name, converted);
            }

            Directory.CreateDirectory(runSettings.workDir);
            foreach (var outputSet in outputSets)
                outputSet.Value.save(runSettings.workDir + "\\" + outputSet.Key);

            diagnostics.Log(3, "All Done");
        }
        public static void Run(RunComplexType runSettings, Diagnostics diagnostics, string[] args)
        {
            TimeSeries[] results =
                (TimeSeries[])
                NonInteractiveIO.Load(runSettings.workDir + "\\" + runSettings.Property(Keys.OUTPUT_FILE));
            diagnostics.Log(3,string.Format("Loaded {0} time series",results.Length));

            string forcedTimeStep = runSettings.Property(Keys.FORCED_TIMESTAMP);
            TimeSeriesCollectionComplexType fewsTimeSeriesCollection = TIMEProxy.FromTimeSeriesCollection(results,runSettings.timeZone,forcedTimeStep);
            string outputFn = runSettings.outputTimeSeriesFile[0];

            FEWSPIProxy.WriteTimeSeriesCollection(outputFn, fewsTimeSeriesCollection);
            diagnostics.Log(3,string.Format("Written {0} time series to {1}",fewsTimeSeriesCollection.series.Length,outputFn));

            diagnostics.Log(3,"All Done");
        }
        public static void Run(RunComplexType runSettings, Diagnostics diagnostics, string[] args)
        {
            string sourceExe = runSettings.SourceExeToUse().Replace('/', '\\');
            string registryName = "SourceFromFEWSPlugins";

            // Set RegistryPath for sourceExe to X
            RegistryKey timeKey = Registry.CurrentUser.CreateSubKey("Software").CreateSubKey("TIME");
            RegistryKey registryPaths = timeKey.CreateSubKey("RegistryPaths");
            registryPaths.SetValue(sourceExe, registryName);
            registryPaths.SetValue(sourceExe.Replace("CommandLine","Forms"),registryName);

            RegistryKey pluginsKey = timeKey.CreateSubKey(registryName);
            pluginsKey.ClearValues();

            int i = 0;
            foreach (string plugin in Directory.EnumerateFiles(runSettings.Property(Keys.PLUGIN_DIR), "*.dll"))
            {
                i++;
                pluginsKey.SetValue(String.Format("Assembly{0}",i),plugin);
            }
        }
 public static void Run(RunComplexType runSettings, Diagnostics diagnostics, string[] args)
 {
     string serverAddress = args[1];
     bool exists = SourceServiceUtils.SourceServerExists(serverAddress);
     Console.WriteLine(exists ? "OK: Server Running at {0}" : "NO: Server Not Running at {0}", serverAddress);
 }
        public static void Run(RunComplexType runSettings, Diagnostics diagnostics, string[] args)
        {
            var start = DateTimeComplexType.DateTimeFromPI(runSettings.startDateTime);
            var end = DateTimeComplexType.DateTimeFromPI(runSettings.endDateTime);

            //            string dateFormat = "dd/MM/yyyy";
            var dtf = Thread.CurrentThread.CurrentCulture.DateTimeFormat;

            string dateFormat = dtf.ShortDatePattern; // "MM/dd/yyyy";
            if (runSettings.TimeStepInSeconds != 86400)
                dateFormat += " HH:mm:ss";

            string sourceExe = runSettings.SourceExeToUse();
            string sourceProject = runSettings.Property(Keys.PROJECT_FILE);

            string sourceOutput = runSettings.Property(Keys.OUTPUT_FILE);

            if (File.Exists(sourceOutput))
            {
                diagnostics.Log(3,string.Format("Deleting old source output file {0}",sourceOutput));
                File.Delete(sourceOutput);
            }

            string mode = runSettings.ExecutionMode();

            if (mode == "")
            {
                if(runSettings.ConfiguredServer()!="")
                    diagnostics.Log(3,string.Format("Running locally because configured server ({0}) is unavailable",runSettings.ConfiguredServer()));
            }
            else
                sourceProject = "";

            string sourceCommand = string.Format("-p \"{0};;{1};{2}\" {4} -o {3}",
                                                 sourceProject, start.ToString(dateFormat),
                                                 end.ToString(dateFormat), sourceOutput,mode);
            diagnostics.Log(3, string.Format("Starting Source Run with command line {0}",sourceCommand));

            ProcessStartInfo startInfo = new ProcessStartInfo
                {
                    Arguments = sourceCommand,
                    CreateNoWindow = true,
                    FileName = sourceExe,
                    UseShellExecute = false,
                    RedirectStandardError = true,
                    RedirectStandardOutput = true
                };
            Process p = Process.Start(startInfo);
            string output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            string errors = p.StandardError.ReadToEnd();

            if (!File.Exists(sourceOutput))
            {
                File.WriteAllText(runSettings.workDir+"\\SourceErrors.txt",errors);
                File.WriteAllText(runSettings.workDir+"\\SourceOutput.txt",output);
                foreach (string line in errors.Split('\n'))
                    diagnostics.Log(1, line);
                throw new Exception(string.Format("Source run failed. No output file: {0}", sourceOutput));
            }

            diagnostics.Log(3,"All done");
        }