/// <summary>
        /// Read data from given SWAT unit
        /// </summary>
        /// <param name="source">SWAT unit type, support subbasin, reach and HRU</param>
        /// <returns>Output message including the data reading time</returns>
        /// <remarks>Support four methods: file driver, file helper, SWAT Plot and SQLite</remarks>
        public string Read(UnitType source, bool isQueryEachYear)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = (int)(DataReadingMethodType.SQLite); i <= (int)(DataReadingMethodType.SWATPlot); i++)
            {
                DataReadingMethodType type = (DataReadingMethodType)(i);
                Console.WriteLine(string.Format("******* {0} *******", type));
                sb.AppendLine(string.Format("{0},{1},{2}", source, type, Read(source, type, isQueryEachYear)));
            }
            return(sb.ToString());
        }
        /// <summary>
        /// Read data with given method
        /// </summary>
        /// <param name="method">Data reading method</param>
        /// <returns>Output message including the data reading time</returns>
        /// <remarks>Support subbasin, reach and HRU data</remarks>
        public string Read(DataReadingMethodType method, bool isQueryEachYear)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = (int)(UnitType.SUB); i <= (int)(UnitType.HRU); i++)
            {
                UnitType source = (UnitType)i;
                Console.WriteLine(string.Format("******* {0} *******", source));
                sb.AppendLine(string.Format("{0},{1},{2}", source, method, Read(source, method, isQueryEachYear)));
            }

            return(sb.ToString());
        }
 public static ExtractSWAT ExtractFromMethod(DataReadingMethodType method, string txtinoutPath)
 {
     switch (method)
     {
         case DataReadingMethodType.SQLite:
             ExtractSWAT_Text ex_text = new ExtractSWAT_Text(txtinoutPath);
             return new ExtractSWAT_SQLite(txtinoutPath, ex_text.OutputInterval);
         case DataReadingMethodType.FileDriver:
             return new ExtractSWAT_Text_FileDriver(txtinoutPath);
         case DataReadingMethodType.FileHelper:
             return new ExtractSWAT_Text_FileHelperEngine(txtinoutPath);
         case DataReadingMethodType.SWATPlot:
             return new ExtractSWAT_Text_SWATPlot(txtinoutPath);
         default:
             throw new Exception("Not support " + method.ToString());
     }
 }
Beispiel #4
0
        public static ExtractSWAT ExtractFromMethod(DataReadingMethodType method, string txtinoutPath)
        {
            switch (method)
            {
            case DataReadingMethodType.SQLite:
                ExtractSWAT_Text ex_text = new ExtractSWAT_Text(txtinoutPath);
                return(new ExtractSWAT_SQLite(txtinoutPath, ex_text.OutputInterval));

            case DataReadingMethodType.FileDriver:
                return(new ExtractSWAT_Text_FileDriver(txtinoutPath));

            case DataReadingMethodType.FileHelper:
                return(new ExtractSWAT_Text_FileHelperEngine(txtinoutPath));

            case DataReadingMethodType.SWATPlot:
                return(new ExtractSWAT_Text_SWATPlot(txtinoutPath));

            default:
                throw new Exception("Not support " + method.ToString());
            }
        }
        /// <summary>
        /// Read data from given SWAT unit using given method
        /// </summary>
        /// <param name="source">SWAT unit type</param>
        /// <param name="method">Data reading method</param>
        /// <returns>Average reading time</returns>
        /// <remarks>Read data of all columns and all ids and then calcuate the average data reading time for one column and one id</remarks>
        public string Read(UnitType source, DataReadingMethodType method, bool isQueryEachYear)
        {
            //if (source == UnitType.HRU && method == DataReadingMethodType.SWATPlot)
            //    return 0.0;
            System.Diagnostics.Debug.WriteLine(string.Format("******* {0} {1} *******", source, method));
            using (ExtractSWAT ex = ExtractSWAT.ExtractFromMethod(method, _txtinoutPath))
            {
                //get number of record and SWAT unit
                int numofRecord = _modelInfo.NumberOfRecordForEachUnit;
                int numUnits    = _modelInfo.NumberofSubbasin;
                if (source == UnitType.HRU)
                {
                    numUnits = _modelInfo.NumberofHRU;
                }
                if (numUnits > MAX_NUM_TESTED_IDS)
                {
                    numUnits = MAX_NUM_TESTED_IDS;                                //limit the number of ids to be tested to reduce the running time, especially for HRU
                }
                //all columns
                //this should be dynamic
                string[] cols    = ExtractSWAT_SQLite.GetSQLiteColumns(source);
                int      numCols = Math.Min(cols.Length, MAX_NUM_TESTED_COLUMNS); //limit the number of ids to be tested to reduce the running time, especially for HRU

                //get number of year
                int numYears = 1;
                if (isQueryEachYear)
                {
                    numYears = Math.Min(_modelInfo.EndYear - _modelInfo.StartYear + 1, MAX_NUM_TESTED_YEARS);
                }

                //initialize
                DataTable dt           = null;
                double    reading_time = 0.0;

                //get output file
                string outputFile = getOutputFile(isQueryEachYear, _modelInfo.OutputInterval, source, method);

                using (StreamWriter file = new StreamWriter(outputFile))
                {
                    //add header
                    file.WriteLine("Column,Time");

                    //start to read
                    for (int colIndex = 0; colIndex < numCols; colIndex++)
                    {
                        string column = cols[colIndex];
                        Console.Write(column.PadRight(20));
                        double col_time = 0.0;//data reading time for current column
                        for (int id = 1; id <= numUnits; id++)
                        {
                            //ouput the id
                            if (id > 1)
                            {
                                Console.Write(",");
                            }
                            Console.Write(id);


                            if (!isQueryEachYear)   //all years
                            {
                                //read data
                                dt = ex.Extract(source, -1, id, column, true);

                                //make sure the number of record is same
                                if (dt.Rows.Count != numofRecord)
                                {
                                    throw new Exception(string.Format("Wrong number of records from {0} {1} on column {2} and id {3}!", source, method, column, id));
                                }

                                //add the reading time to the column reading time
                                col_time += ex.ExtractTime;
                            }
                            else //each year
                            {
                                for (int yearIndex = 0; yearIndex <= numYears; yearIndex++)
                                {
                                    int year = _modelInfo.StartYear + yearIndex;

                                    //read data
                                    dt = ex.Extract(source, year, id, column, true);

                                    //add the reading time to the column reading time
                                    col_time += ex.ExtractTime;
                                }
                            }
                        }
                        Console.WriteLine("");

                        //calculate the average column reading time
                        //and output in debug window to make sure all column have similar reading time
                        col_time /= numUnits * numYears;
                        file.WriteLine(string.Format("{0},{1:F4}", column, col_time));
                        System.Diagnostics.Debug.WriteLine(string.Format("{0}: {1:F4}", column, col_time));

                        //add to total reading time
                        reading_time += col_time;
                    }
                }
                return(string.Format("{0},{1}", ex.PrepareTime, reading_time / numCols));
            }
        }
 private string getOutputFile(bool isQueryEachYear, OutputIntervalType interval, UnitType source, DataReadingMethodType method)
 {
     return(Path.Combine(getOutputFolder(isQueryEachYear, interval),
                         string.Format("{0}_{1}_{2}_{3}.csv", source, method,
                                       isQueryEachYear ? "each_year" : "all_year",
                                       interval)));
 }