static public void SaveConfig(TSIModelConfig config, string pathFolder, string fileName) { string pathFileName; string root = Path.GetPathRoot(pathFolder); if (root != pathFolder) { pathFileName = @pathFolder + Path.DirectorySeparatorChar + fileName + ".xml"; } else { pathFileName = @pathFolder + fileName + ".xml"; } XmlSerializer serializer = new XmlSerializer(typeof(TSIModelConfig)); using (TextWriter writer = new StreamWriter(pathFileName)) { serializer.Serialize(writer, config); } }
// see also https://www.codeproject.com/Articles/14465/Specify-a-Configuration-File-at-Runtime-for-a-C-Co private TSModelRunner(IFilenameDateParser _parser, TSIModelConfig cfg) { _fnParser = _parser; //var set = Properties.Settings.Default; //System.Console.WriteLine("Looking for files in " + m_FileWildCard); //var d = new System.Diagnostics.DefaultTraceListener(); _maxReader = new TiffCubeReader(cfg.dataPathConfig.MaxTempFiles, _fnParser, cfg.modelRunConfig.ReadFromDate, cfg.modelRunConfig.ReadToDate); var nMax = _maxReader.Filenames.Count; Console.WriteLine("Looking for max temp files in " + cfg.dataPathConfig.MaxTempFiles + " - found " + nMax.ToString()); _minReader = new TiffCubeReader(cfg.dataPathConfig.MinTempFiles, _fnParser, cfg.modelRunConfig.ReadFromDate, cfg.modelRunConfig.ReadToDate); var nMin = _minReader.Filenames.Count; Console.WriteLine("Looking for min temp files in " + cfg.dataPathConfig.MinTempFiles + " - found " + nMin.ToString()); if (nMax == 0 || nMin == 0) { throw new ArgumentException("Can't continue without data"); // I don't see any reason not to continue if the numbers for day and night aren't actually equal } if (!_maxReader.GeoTransform.SequenceEqual(_minReader.GeoTransform)) { throw new ArgumentException("max and min temp image transforms do not match!"); } if (!GDAL_Operations.GetGeoTransform(cfg.dataPathConfig.MaskFile).SequenceEqual(_maxReader.GeoTransform)) { throw new ArgumentException("Land-sea mask doesn't match images!"); } if (!System.IO.Directory.Exists(cfg.dataPathConfig.OutputFolder)) { System.IO.Directory.CreateDirectory(cfg.dataPathConfig.OutputFolder); } _cfg = cfg; }
/// <summary> /// If a command-line argument was passed, attempt to load it as a configuration file and /// set up the model accordingly. If the input is a folder, not a file, then write a sample /// config file into that folder as a template for the user to edit. If no command-line /// argument was given, prompt for one. /// </summary> /// <param name="args"></param> /// <returns></returns> static TSIModelConfig loadConfig(string[] args) { string runtimeConfigFile; if (args.Length == 0) { Console.WriteLine("Please specify a path to a configuration xml file.\n\n" + "Alternatively specify a path to a folder, to create a sample config file \n in this folder for editing.\n\n" + "Or, just hit enter to attempt to run with application default paths."); Console.Write("> "); runtimeConfigFile = Console.ReadLine(); } else { runtimeConfigFile = args[0]; } if (runtimeConfigFile == "") { runtimeConfigFile = null; } return(TSIModelConfig.LoadOrCreateConfig(runtimeConfigFile)); }
static public TSIModelConfig LoadConfigFromSettings() { var set = Properties.Settings.Default; DataPathConfig dpc = new DataPathConfig(); try { dpc.MaskFile = set.LS_Mask_File; dpc.MaxTempFiles = set.Max_Temp_Files; dpc.MinTempFiles = set.Min_Temp_Files; dpc.OutputFolder = set.OutputFolder; } catch { Console.WriteLine("Could not find paths specification in application config file"); return(null); } SpatialLimits spc = new SpatialLimits(); try { spc.WestLimitDegrees = set.WestLim; spc.EastLimitDegrees = set.EastLim; spc.WestLimitDegrees = set.NorthLim; spc.SouthLimitDegrees = set.SouthLim; } catch { Console.WriteLine("Failed to read geographic limits from application config file"); return(null); } ModelConfig mcfg = new ModelConfig(); try { mcfg.SliceLengthHours = set.ParamSliceLengthHours; mcfg.LifespanDays = set.ParamLifespanDays; mcfg.DeathTempCelsius = set.ParamDeathTempCelsius; mcfg.SporogenesisDegreeDays = set.ParamSporogDegreeDays; mcfg.MinTempThresholdCelsius = set.ParamMinTempThreshCelsius; mcfg.MaxTSNormaliser = set.ParamMaxTSNormaliser; } catch { Console.WriteLine("Failed to read temp suitability parameters from application config file"); return(null); } ModelRunConfig rcfg = new ModelRunConfig(); try { rcfg.OutputFileTag = set.Output_File_Tag; rcfg.MaxTileSizePx = set.TileSizePx; rcfg.MaxThreads = set.MaxThreads; rcfg.MaxTempFilesAreLST = set.Max_Temp_Convert_From_LST; rcfg.MinTempFilesAreLST = set.Min_Temp_Convert_From_LST; rcfg.MinRequiredDataPoints = set.Min_Required_Data_Points; rcfg.MaskValidValue = set.MaskValidValue; rcfg.ReadFromDate = set.Read_From_Date; rcfg.ReadToDate = set.Read_To_Date; } catch { Console.WriteLine("Failed to read model execution parameters from application config file"); return(null); } var tsiCfg = new TSIModelConfig(); tsiCfg.modelConfig = mcfg; tsiCfg.spatialLimits = spc; tsiCfg.dataPathConfig = dpc; tsiCfg.modelRunConfig = rcfg; return(tsiCfg); }