/// <summary> /// /// </summary> /// <param name="reader"></param> public void ReadXml(XmlReader reader) { XDocument doc = XDocument.Load(reader); //Get the list of models XElement projectElement = doc.Element("Project"); //Set the project properties CreatedBy = XMLUtilities.readXMLAttribute(projectElement.Attribute("CreatedBy")); DateTime?createdDate = DateUtilities.TryParseDate(XMLUtilities.readXMLAttribute(projectElement.Attribute("CreationDate")).Split(new char[] { ' ' })[0], "dd/MM/yyyy"); CreatedDate = createdDate == null ? new DateTime(1, 1, 1) : createdDate.Value; ContactDetails = XMLUtilities.readXMLAttribute(projectElement.Attribute("ContactDetails")); ModifiedBy = XMLUtilities.readXMLAttribute(projectElement.Attribute("ModifiedBy")); Name = projectElement.Element("Name").Value.ToString(); //Read all of the climate data models List <XElement> ClimateDatalements = new List <XElement>(projectElement.Elements("ClimateData").Elements("DataFile")); //Read all of the models List <XElement> TemplateElements = new List <XElement>(projectElement.Elements().Where(x => x.Name.ToString().Contains("Templates"))); List <XElement> TypeElements = new List <XElement>(); foreach (XElement te in TemplateElements) { foreach (XElement xe in te.Elements()) { TypeElements.Add(xe); } } //Read all of the simualtions SimulationElements = new List <XElement>(); foreach (XElement simChild in projectElement.Elements("Simulations").Elements()) { if (simChild.Name.ToString() == "SimulationObject") { SimulationElements.Add(simChild); } else if (simChild.Name.ToString() == "Folder") { SimulationElements.AddRange(simChild.Elements("SimulationObject")); } } InputDataModels = new List <InputModel>(); //Create input models from the xml elements foreach (XElement xe in TypeElements) { InputDataModels.Add(RawInputModelFactory.GenerateRawInputModel(xe)); } //Create the Climate models - these aren't deserialised so don't come out of the factory foreach (XElement xe in ClimateDatalements) { ClimateInputModel cim = new ClimateInputModel(); cim.FileName = xe.Attribute("href").Value.ToString(); InputDataModels.Add(cim); } //Initialise the models foreach (InputModel im in InputDataModels) { im.Init(); } //Create the simualtions foreach (XElement xe in SimulationElements) { Simulations.Add(SimulationFactory.GenerateSimulationXML(this, xe, InputDataModels)); } //Just one for testing //Simulations = new List<Simulation>(); //Simulations.Add(SimulationFactory.GenerateSimulationXML(SimulationElements[0], InputDataModels)); OutputDataElements = OutputModelController.GetProjectOutputs(this); }
public static Simulation GenerateSimulationXML(Project Project, XElement simElement, List <InputModel> allModels) { List <InputModel> simModels = new List <InputModel>(); int startYear = 0; int endYear = 0; //Get the models from the filenames of the simualtion pointers foreach (XElement element in simElement.Elements()) { if (element.Name.ToString() == "StartYear") { startYear = element.Value.ToString() == "default" ? 0 : int.Parse(element.Value.ToString()); } else if (element.Name.ToString() == "EndYear") { endYear = element.Value.ToString() == "default" ? 0 : int.Parse(element.Value.ToString()); } else { try { string fileName = element.Attribute("href").Value.ToString().Replace("\\", "/"); if (fileName.Contains("./")) { fileName = Path.GetDirectoryName(Project.FileName).Replace("\\", "/") + "/" + fileName; } InputModel model = allModels.Where(im => im.FileName == fileName).FirstOrDefault(); InputModel model2 = Cloner.DeepClone <InputModel>(model); string modelDescription = model.GetType().Name; modelDescription += (":" + model.Name); //Check for child nodes foreach (XElement childElement in element.Elements()) { if (childElement.Name == "OverrideParameter") { if (childElement.Attribute("Keyword").Value.ToString() == "" && childElement.Attribute("Active").Value.ToString() != "false") { //Add the override to the model model2.Overrides.Add(childElement.Attribute("Keyword").Value, childElement.Element("Value").Value); modelDescription += (";" + childElement.Attribute("Keyword").Value + "=" + childElement.Element("Value").Value); } } else { //Probably a climate file override if (element.Name == "ptrStation") { if (childElement.Attribute("index") != null) { model2.Overrides.Add(childElement.Name.ToString(), childElement.Attribute("index").Value); modelDescription += (";" + childElement.Name.ToString() + "=" + childElement.Attribute("index").Value); } else { model2.Overrides.Add(childElement.Name.ToString(), childElement.Value); modelDescription += (";" + childElement.Name.ToString() + "=" + childElement.Value); } } } } if (element.Name == "ptrStation") { //Map the data to the original model ClimateInputModel cimNew = (ClimateInputModel)model2; ClimateInputModel cimOrig = (ClimateInputModel)model; cimNew.Rain = cimOrig.Rain; cimNew.MaxT = cimOrig.MaxT; cimNew.MinT = cimOrig.MinT; cimNew.PanEvap = cimOrig.PanEvap; cimNew.Radiation = cimOrig.Radiation; cimNew.VP = cimOrig.VP; //cimNew.StartDate = cimOrig.StartDate; //cimNew.EndDate = cimOrig.EndDate; } model2.ApplyOverrides(); model2.LongName = modelDescription; simModels.Add(model2); } catch (Exception e) { } } } return(new Simulation(Project, simModels, startYear, endYear)); }