Пример #1
0
        /// <summary>
        /// Get a DateTime from a 'ddMMM' string (ie '01Jan' OR '1-Jan' OR '1 Jan' etc)
        /// </summary>
        /// <param name="ddMMM">String containing 'day of month' and at least the first 3 letters of a month's name</param>
        /// <param name="year">The year to use when constructing the DateTime object</param>
        /// <returns>A DateTime constructed from <paramref name="ddMMM"/> using <paramref name="year"/></returns>
        public static DateTime GetDate(string ddMMM, int year)
        {
            try
            {
                int posDelimiter = ddMMM.IndexOfAny(new char[] { '/', '-' });
                if (posDelimiter == -1)
                {
                    throw new ArgumentException();
                }

                int month = StringUtilities.IndexOfCaseInsensitive(LowerCaseMonths, ddMMM.Substring(posDelimiter + 1)) + 1;
                int day   = Convert.ToInt32(ddMMM.Substring(0, posDelimiter), CultureInfo.InvariantCulture);
                return(new DateTime(year, month, day));

                //return new DateTime(
                //    year,
                //    Array.IndexOf(LowerCaseMonths, rxMMM.Match(ddMMM).Value.ToLower()) + 1,
                //    int.Parse(rxDD.Match(ddMMM).Value),
                //    0,
                //    0,
                //    0
                //    );
            }
            catch
            {
                throw new Exception("Error in 'GetDate' - input string should be in form ddmmm (any delimiter may appear between dd and mmm), input string: " + ddMMM);
            }
        }
Пример #2
0
        /// <summary>
        /// Read in the APSIM header - headings/units and constants.
        /// </summary>
        /// <param name="inData">The in.</param>
        /// <exception cref="System.Exception">The number of headings and units doesn't match in file:  + _FileName</exception>
        private void ReadApsimHeader(StreamReaderRandomAccess inData)
        {
            StringCollection ConstantLines = new StringCollection();
            StringCollection HeadingLines  = new StringCollection();

            ReadApsimHeaderLines(inData, ref ConstantLines, ref HeadingLines);

            bool TitleFound = false;

            foreach (string ConstantLine in ConstantLines)
            {
                string Line    = ConstantLine;
                string Comment = StringUtilities.SplitOffAfterDelimiter(ref Line, "!");
                Comment.Trim();
                int PosEquals = Line.IndexOf('=');
                if (PosEquals != -1)
                {
                    string Name = Line.Substring(0, PosEquals).Trim();
                    if (Name.ToLower() == "title")
                    {
                        TitleFound = true;
                        Name       = "Title";
                    }
                    string Value = Line.Substring(PosEquals + 1).Trim();
                    string Unit  = string.Empty;
                    if (Name != "Title")
                    {
                        Unit = StringUtilities.SplitOffBracketedValue(ref Value, '(', ')');
                    }
                    _Constants.Add(new ApsimConstant(Name, Value, Unit, Comment));
                }
            }
            if (HeadingLines.Count >= 1)
            {
                if (IsCSVFile)
                {
                    HeadingLines[0] = HeadingLines[0].TrimEnd(',');
                    Headings        = new StringCollection();
                    Units           = new StringCollection();
                    Headings.AddRange(HeadingLines[0].Split(",".ToCharArray()));
                    for (int i = 0; i < Headings.Count; i++)
                    {
                        Headings[i] = Headings[i].Trim();
                        Headings[i] = Headings[i].Trim('"');
                        Units.Add("()");
                    }
                }
                else
                {
                    Headings = StringUtilities.SplitStringHonouringQuotes(HeadingLines[0], " \t");
                    Units    = StringUtilities.SplitStringHonouringQuotes(HeadingLines[1], " \t");
                }
                TitleFound = TitleFound || StringUtilities.IndexOfCaseInsensitive(Headings, "title") != -1;
                if (Headings.Count != Units.Count)
                {
                    throw new Exception("The number of headings and units doesn't match in file: " + _FileName);
                }
            }
            if (!TitleFound)
            {
                _Constants.Add(new ApsimConstant("Title", System.IO.Path.GetFileNameWithoutExtension(_FileName), "", ""));
            }
        }