Пример #1
0
        public void Update(RainfallModel model)
        {
            var sql = @"UPDATE rainfall SET
                            month = @month,
                            year = @year,
                            rainfall = @rainfall
                        WHERE id = @id";

            db.Execute(sql, new { model.Month, model.Year, model.Rainfall, model.Id });
        }
Пример #2
0
        public void New(RainfallModel model)
        {
            var sql = @"INSERT INTO rainfall(month,year,rainfall)
                    VALUES (@month,@year,@rainfall)
                    SELECT CAST(SCOPE_IDENTITY() AS int)";
            var id  = db.Query <int>(sql, new
            {
                model.Month,
                model.Year,
                model.Rainfall
            }).Single();

            model.Id = id;
        }
Пример #3
0
        /// <summary>
        /// Parse a block of the pre data and uses it to create an array of RainfallModels.
        /// It also uses dateTimeArray to add dates to the Models.
        /// </summary>
        /// <param name="gridRefs">An array of 2 integers representing the grid refs for the block E.g. [1,148]</param>
        /// <param name="blockData">A string array representing the block of data.</param>
        public RainfallModel[,] parsePreDataBlock(int[] gridRefs, string[] blockData)
        {
            // validate inputs
            if (gridRefs.Length != 2)
            {
                throw new Exception($"Incorrect number of gridRefs." +
                                    $"Expected 2 but got {gridRefs.Length}");
            }

            if (blockData.Length != this.numberOfLinesPerBlock)
            {
                throw new Exception($"Incorrect number of lines in block." +
                                    $"Expected {this.numberOfLinesPerBlock} but got {blockData.Length}");
            }

            int[,] blockDataParsed = new int[12, blockData.Length]; // 12 Months per year
            RainfallModel[,] rainfallMeasurements = new RainfallModel[12, blockData.Length];

            // Parse each block into a two dimensional array.
            // COmbine these two loops
            for (int yearIndex = 0; yearIndex < blockData.Length; yearIndex++)
            {
                blockData[yearIndex] = blockData[yearIndex].Trim(' ');

                string[] splitLine = blockData[yearIndex].Split(' ', StringSplitOptions.RemoveEmptyEntries);

                for (int monthIndex = 0; monthIndex < splitLine.Length; monthIndex++)
                {
                    blockDataParsed[monthIndex, yearIndex]      = int.Parse(splitLine[monthIndex]);
                    rainfallMeasurements[monthIndex, yearIndex] = new RainfallModel()
                    {
                        guid  = Guid.NewGuid(),
                        Date  = this.dateTimeArray[monthIndex, yearIndex],
                        Xref  = gridRefs[0],
                        Yref  = gridRefs[1],
                        Value = blockDataParsed[monthIndex, yearIndex]
                    };
                }
            }
            return(rainfallMeasurements);
        }
Пример #4
0
        public void testparsePreDataBlock()
        {
            PreFileFormatReader preFileFormatReader = new PreFileFormatReader();

            //Load header info before reading.
            string[] testingHeaderStringArray = new string[5]
            {
                "Tyndall Centre grim file created on 22.01.2004 at 17:57 by Dr. Tim Mitchell",
                ".pre = precipitation (mm)",
                "CRU TS 2.1",
                "[Long=-180.00, 180.00] [Lati= -90.00,  90.00] [Grid X,Y= 720, 360]",
                "[Boxes=   67420] [Years=1991-2000] [Multi=    0.1000] [Missing=-999]"
            };

            preFileFormatReader.parseHeader(testingHeaderStringArray);

            int[] gridRefs = new int[2] {
                1, 148
            };

            string[] blockData = new string[] {
                " 3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630",
                " 3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630",
                " 3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630",
                " 3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630",
                " 3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630",
                " 3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630",
                " 3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630",
                " 3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630",
                " 3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630",
                " 3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630",
            };

            RainfallModel[,] rainfallModels = preFileFormatReader.parsePreDataBlock(gridRefs, blockData);

            Assert.AreEqual(rainfallModels.Length, blockData.Length * 12, $"There should be {blockData.Length * 12} rainfall models but {rainfallModels.Length} were created.");

            //Test an example model
            RainfallModel expectedFirstRainfallModel = new RainfallModel()
            {
                guid = Guid.NewGuid(), Value = 3020, Xref = 1, Yref = 148, Date = new DateTime(1991, 1, 1)
            };

            RainfallModel actualFirstRainfallModel = rainfallModels[0, 0];

            Assert.AreEqual(expectedFirstRainfallModel.Xref, actualFirstRainfallModel.Xref);
            Assert.AreEqual(expectedFirstRainfallModel.Yref, actualFirstRainfallModel.Yref);
            Assert.AreEqual(expectedFirstRainfallModel.Date, actualFirstRainfallModel.Date);
            Assert.AreEqual(expectedFirstRainfallModel.Value, actualFirstRainfallModel.Value);

            RainfallModel expectedLastRowFirstColumnRainfallModel = new RainfallModel()
            {
                guid = Guid.NewGuid(), Value = 3020, Xref = 1, Yref = 148, Date = new DateTime(2000, 1, 1)
            };

            RainfallModel actualLastRowFirstColumnRainfallModel = rainfallModels[0, 9];

            Assert.AreEqual(expectedLastRowFirstColumnRainfallModel.Xref, actualLastRowFirstColumnRainfallModel.Xref);
            Assert.AreEqual(expectedLastRowFirstColumnRainfallModel.Yref, actualLastRowFirstColumnRainfallModel.Yref);
            Assert.AreEqual(expectedLastRowFirstColumnRainfallModel.Value, actualLastRowFirstColumnRainfallModel.Value);
            Assert.AreEqual(expectedLastRowFirstColumnRainfallModel.Date, actualLastRowFirstColumnRainfallModel.Date);
        }
Пример #5
0
        public IActionResult Rainfall()
        {
            RainfallModel result = ProcessRainfallForStation().Result;

            return(View(result));
        }