Пример #1
0
        public void TestReadData()
        {
            WriteDummyMetFile();
            APSIMInputFile InputFile = new APSIMInputFile();

            InputFile.Open("test.met");
            DataTable MetData = InputFile.ToTable();

            InputFile.Close();

            int FirstRow = 0;

            Assert.AreEqual(MetData.Rows[FirstRow]["Site"], "DALB");
            Assert.AreEqual(MetData.Rows[FirstRow]["radn"], (double)20.74);
            Assert.AreEqual(MetData.Rows[FirstRow]["maxt"], (double)33.0);
            Assert.AreEqual(MetData.Rows[FirstRow]["mint"], (double)17.4);
            Assert.AreEqual(MetData.Rows[FirstRow]["rain"], (double)0.2);
            Assert.AreEqual(MetData.Rows[FirstRow]["evap"], (double)7.41);

            int LastRow = MetData.Rows.Count - 1;

            Assert.AreEqual(LastRow, 3);

            DeleteDummyMetFile();
        }
Пример #2
0
        public void TestReadCSV()
        {
            const string CSVContents =
                "[weather.met.weather]\r\n" +
                "site,Year,day,radn,maxt,mint,rain,evap\r\n" +
                "(),(),(),(MJ/m2),(oC),(oC),(mm),(mm)\r\n" +
                "DALB,1988,1,20.74,33.0,17.4,0.2,7.41\r\n" +
                "DALB,1988,2,23.43,33.8,23.0,0.0,7.41\r\n" +
                "DALB,1988,3,23.79,32.5,21.0,0.0,7.41\r\n" +
                "DALB,1988,4,19.14,30.8,19.7,34.0,7.41\r\n";

            StreamWriter Out = new StreamWriter("test.csv");

            Out.Write(CSVContents);
            Out.Close();

            APSIMInputFile InputFile = new APSIMInputFile();

            InputFile.Open("test.csv");
            DataTable MetData = InputFile.ToTable();

            InputFile.Close();

            Assert.AreEqual(MetData.Rows[0]["Site"], "DALB");
            Assert.AreEqual(MetData.Rows[0]["radn"], (double)20.74);
            Assert.AreEqual(MetData.Rows[0]["maxt"], (double)33.0);
            Assert.AreEqual(MetData.Rows[0]["mint"], (double)17.4);
            Assert.AreEqual(MetData.Rows[0]["rain"], (double)0.2);
            Assert.AreEqual(MetData.Rows[0]["evap"], (double)7.41);

            File.Delete("text.csv");
        }
Пример #3
0
    public void OnInitialised()
    {
        // Open the file, read in all constants and data and then close the file.
        APSIMInputFile file = new APSIMInputFile();

        if (!File.Exists(FileName))
        {
            throw new Exception("Cannot find file: " + FileName);
        }

        try
        {
            file.Open(FileName);
        }
        catch (Exception)
        {
        }
        data = file.ToTable();
        file.Close();
        Console.WriteLine("      Reading patch data from: " + FileName);

        // Convert all constants found into PatchVariables.
        patchVariables = new List <PatchVariable>();
        foreach (APSIMConstant constant in file.Constants)
        {
            ProcessConstant(constant);
        }
    }
Пример #4
0
        public override void OnRefresh()
        {
            ContentsBox.Text = "";

            string FullFileName = Controller.ToAbsolute(FileName);

            if (File.Exists(FullFileName))
            {
                APSIMInputFile Metfile = new APSIMInputFile();
                Metfile.Open(FullFileName);
                MetData = Metfile.ToTable();
                Metfile.Close();
                MetData.TableName = "Met";

                // Get latitude for later on.
                if (Metfile.Constant("latitude") == null)
                {
                    MessageBox.Show("A value for latitude was expected, but could not be found in this file.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    Latitude = Double.NaN;
                }
                else
                {
                    Latitude = Convert.ToDouble(Metfile.Constant("latitude").Value, new System.Globalization.CultureInfo("en-US"));
                }

                StartDate = DataTableUtility.GetDateFromRow(MetData.Rows[0]);
                EndDate   = DataTableUtility.GetDateFromRow(MetData.Rows[MetData.Rows.Count - 1]);
                PopulateRawData();
                YearStartBox.ValueChanged -= YearStartBoxChanged;
                NumYearsBox.ValueChanged  -= NumYearsBoxChanged;
                YearStartBox.Value         = StartDate.Year;
                NumYearsBox.Value          = 1;
                if (NumYears != 0)
                {
                    NumYearsBox.Value = NumYears;
                }
                if (YearStart != 0)
                {
                    YearStartBox.Value = YearStart;
                }
                YearStartBox.ValueChanged += YearStartBoxChanged;
                NumYearsBox.ValueChanged  += NumYearsBoxChanged;

                RefreshAllCharts();
            }
            YearPanel.Visible = (TabControl.SelectedIndex != 0);
            YearPanel.Parent  = this;
            YearPanel.Top     = TabControl.SelectedTab.Top + 2;
            YearPanel.BringToFront();
        }
Пример #5
0
        public void TestReadConstants()
        {
            WriteDummyMetFile();
            APSIMInputFile InputFile = new APSIMInputFile();

            InputFile.Open("test.met");
            DataTable MetData = InputFile.ToTable();

            InputFile.Close();

            ArrayList Constants = InputFile.Constants;

            Assert.AreEqual(Constants.Count, 4);
            APSIMConstant Latitude = (APSIMConstant)Constants[0];

            Assert.AreEqual(Latitude.Name, "Latitude");
            Assert.AreEqual(Latitude.Value, "-27.11");
            Assert.AreEqual(Latitude.Units, "deg min");
            Assert.AreEqual(Latitude.Comment, "my latitude");


            DeleteDummyMetFile();
        }
Пример #6
0
    private DataTable ProcessSOI(XmlNode Node)
    {
        // -------------------------------------------------
        // The XmlNode is a GDSOI so go find a
        // nested source DataTable, add an 'SOI Phase' column
        // to it and return it to caller.
        // -------------------------------------------------
        string[] DefaultPhaseNames = { "Unknown", "Negative", "Positive", "Falling", "Rising", "Zero" };

        // Go find the name of the soi phase file.
        string PhaseFile = XmlHelper.Value(Node, "PhaseFile");

        if (PhaseFile == "")
        {
            PhaseFile = Configuration.Instance.ClimateSetting("SOIFile");
        }
        if (PhaseFile == "" || !File.Exists(PhaseFile))
        {
            return(null);
        }

        // Go find the SOI month we're to use for the lookup.
        int Month = Convert.ToInt32(XmlHelper.Value(Node, "Month"));

        // Work out the name of the year column.

        // Go find source data to work with
        DataTable Data = GoFindChildDataTable(Node);

        if (Data == null || Data.Columns.Count == 0)
        {
            return(null);
        }

        // Add new SOI Phase column to Data.
        Data.Columns.Add("SOI Phase", Type.GetType("System.String"));

        // read in soi phase data.
        APSIMInputFile SOI = new APSIMInputFile();

        SOI.Open(PhaseFile);
        DataTable SOIData = SOI.ToTable();

        SOI.Close();

        int NumRows = Data.Rows.Count;

        for (int Row = 0; Row < NumRows; Row++)
        {
            string RowTitle = Data.Rows[Row]["Title"].ToString();

            DateTime RowDate = DataTableUtility.GetDateFromRow(Data.Rows[Row]);

            DataRow[] MatchingSOIData = SOIData.Select("Year = " + RowDate.Year.ToString() +
                                                       " and Month = " + Month.ToString());
            string PhaseName = "Unknown";
            if (MatchingSOIData.Length == 1)
            {
                if (SOIData.Columns.IndexOf("PhaseName") == -1)
                {
                    int PhaseNumber = Convert.ToInt32(MatchingSOIData[0]["Phase"]);
                    if (PhaseNumber < DefaultPhaseNames.Length)
                    {
                        PhaseName = DefaultPhaseNames[PhaseNumber];
                    }
                }
                else
                {
                    PhaseName = MatchingSOIData[0]["PhaseName"].ToString();
                }
            }
            Data.Rows[Row]["SOI Phase"] = PhaseName;
            Data.Rows[Row]["Title"]     = RowTitle + ", " + PhaseName;

            // We also want to make a duplicate of this row and label it 'AllYears'
            Data.ImportRow(Data.Rows[Row]);
            Data.Rows[Data.Rows.Count - 1]["SOI Phase"] = "AllYears";
            Data.Rows[Data.Rows.Count - 1]["Title"]     = RowTitle + ", AllYears";
        }
        return(Data);
    }
Пример #7
0
    private DataTable ProcessApsimFileReader(XmlNode Node)
    {
        // -------------------------------------------------
        // The XmlNode is a GDApsimFileReader so go read
        // a series of APSIM output files and return a
        // DataTable with all data.
        // -------------------------------------------------

        List <string> FileNames = XmlHelper.Values(Node, "FileName");

        if (FileNames.Count == 0)
        {
            FileNames.AddRange(DefaultFileNames);
        }

        DataTable Data = null;

        foreach (string FileSpec in FileNames)
        {
            if (FileSpec != "")
            {
                string FileSpecNoMacros = Configuration.RemoveMacros(FileSpec);
                string Dir = Path.GetDirectoryName(FileSpecNoMacros);
                if (Dir == "")
                {
                    Dir = Directory.GetCurrentDirectory();
                }

                foreach (string FileName in Directory.GetFiles(Dir,
                                                               Path.GetFileName(FileSpecNoMacros)))
                {
                    if (FileName != "" && File.Exists(FileName))
                    {
                        string CheckPointFile = Path.Combine(Path.GetDirectoryName(Path.GetFullPath(FileName)), "CheckPoint", Path.GetFileName(FileName));
                        if (File.Exists(FileName))
                        {
                            APSIMInputFile InFile = new APSIMInputFile();
                            InFile.Open(FileName);
                            DataTable FileData = InFile.ToTable();
                            InFile.Close();
                            InFile.AddConstantsToData(FileData);
                            if (Data == null)
                            {
                                Data = FileData;
                            }
                            else
                            {
                                Merge(FileData, Data);
                            }
                        }
                        if (File.Exists(CheckPointFile))
                        {
                            APSIMInputFile InFile = new APSIMInputFile();
                            InFile.Open(CheckPointFile);
                            DataTable FileData = InFile.ToTable();
                            InFile.Close();
                            InFile.SetConstant("title", "{Checkpoint} " + InFile.Constant("title").Value);
                            InFile.AddConstantsToData(FileData);
                            if (Data == null)
                            {
                                Data = FileData;
                            }
                            else
                            {
                                Merge(FileData, Data);
                            }
                        }
                    }
                }
            }
        }
        return(Data);
    }