/// <summary>
        /// Write data in given time range as ArcSWAT dbf file
        /// </summary>
        /// <param name="startYear"></param>
        /// <param name="endYear"></param>
        /// <param name="destinationFolder"></param>
        /// <returns></returns>
        private bool save2ArcSWATdbf(int startYear, int endYear, string destinationFolder)
        {
            string timeAffix = getTimeAffix();
            string pFile = string.Format("{0}\\P{1}{2}.dbf", Path.GetFullPath(destinationFolder), _id, timeAffix);  //precipitation
            string tFile = string.Format("{0}\\T{1}{2}.dbf", Path.GetFullPath(destinationFolder), _id, timeAffix);  //temperature

            this.setProgress(0,string.Format("Processing station {0}", _id));
            this.setProgress(0, pFile);
            this.setProgress(0, tFile);

            //create the dbf structure based on ArcSWAT document
            DbfFile pDBF = new DbfFile();
            pDBF.Open(pFile, FileMode.Create);
            pDBF.Header.AddColumn(new DbfColumn("DATE", DbfColumn.DbfColumnType.Date));
            pDBF.Header.AddColumn(new DbfColumn("PCP", DbfColumn.DbfColumnType.Number, 5, 1));

            DbfFile tDBF = new DbfFile();
            tDBF.Open(tFile, FileMode.Create);
            tDBF.Header.AddColumn(new DbfColumn("DATE", DbfColumn.DbfColumnType.Date));
            tDBF.Header.AddColumn(new DbfColumn("MAX", DbfColumn.DbfColumnType.Number, 5, 1));
            tDBF.Header.AddColumn(new DbfColumn("MIN", DbfColumn.DbfColumnType.Number, 5, 1));

            DbfRecord pRec = new DbfRecord(pDBF.Header);
            DbfRecord tRec = new DbfRecord(tDBF.Header);

            int processPercent = 0;
            bool hasResults = false;
            clearFailureYears();
            clearUncompletedYears();
            for (int i = startYear; i <= endYear; i++)
            {
                setProgress(processPercent, string.Format("Downloading data for station: {0}, year: {1}", _id, i));
                string resultsForOneYear = this.retrieveAnnualDailyClimateData(i, true);
                if (resultsForOneYear.Length == 0)
                {
                    addFailureYear(i);
                    continue;
                }

                processPercent += 1;
                setProgress(processPercent, "Writing data");

                using (CachedCsvReader csv = new CachedCsvReader(new StringReader(resultsForOneYear), true))
                {
                    if (csv.FieldCount >= 27)
                    {
                        hasResults = true;

                        string date = "";
                        while (csv.ReadNextRecord())
                        {
                            date = csv[0];
                            double p = ClimateString2Double(csv[TOTAL_PRECIPITATION_COL_INDEX]);
                            pRec[0] = date;
                            pRec[1] = p.ToString();
                            pDBF.Write(pRec, true);

                            double t_max = ClimateString2Double(csv[MAX_T_COL_INDEX]);
                            double t_min = ClimateString2Double(csv[MIN_T_COL_INDEX]);
                            tRec[0] = date;
                            tRec[1] = t_max.ToString();
                            tRec[2] = t_min.ToString();
                            tDBF.Write(tRec, true);
                        }
                        checkLastDayofYear(date);
                    }
                }
                processPercent += 1;
            }
            pDBF.Close();
            tDBF.Close();

            return hasResults;
        }
예제 #2
0
        private static void TestWriteNewDbf()
        {
            //create a simple DBF file and output to args[0]
            var odbf = new DbfFile(Encoding.GetEncoding(1252));
            odbf.Open(Path.Combine(TestPath,"TestNew2.dbf"), FileMode.Create);

            //create a header
            odbf.Header.AddColumn(new DbfColumn("StrCol", DbfColumn.DbfColumnType.Character, 20, 0));
            odbf.Header.AddColumn(new DbfColumn("DecCol1", DbfColumn.DbfColumnType.Number, 5, 1));
            odbf.Header.AddColumn(new DbfColumn("DecCol2", DbfColumn.DbfColumnType.Number, 5, 2));
            odbf.Header.AddColumn(new DbfColumn("DecCol3", DbfColumn.DbfColumnType.Number, 5, 3));
            odbf.Header.AddColumn(new DbfColumn("DecCol4", DbfColumn.DbfColumnType.Number, 15, 5));
            odbf.Header.AddColumn(new DbfColumn("NumCol1", DbfColumn.DbfColumnType.Number, 5, 0));
            odbf.Header.AddColumn(new DbfColumn("NumCol2", DbfColumn.DbfColumnType.Number, 10, 0));
            odbf.Header.AddColumn(new DbfColumn("DateCol1", DbfColumn.DbfColumnType.Date));
            odbf.Header.AddColumn(new DbfColumn("BoolCol1", DbfColumn.DbfColumnType.Boolean));

            //add some records...
            var orec = new DbfRecord(odbf.Header) {AllowDecimalTruncate = true};
            orec[0] = "Ahmed Test";
            orec[1] = "123.5";
            orec[2] = "12.35";
            orec[3] = "1.235";
            orec[4] = "1235.123456";
            orec[5] = "1235";
            orec[6] = "123567890";
            orec[7] = "11/07/2007";
            orec[8] = "f";
            odbf.Write(orec, true);

            orec[0] = "Stéfanié Singer";
            orec[1] = "-1.5";
            orec[2] = "-1.35";
            orec[3] = "1.235";
            orec[4] = "-1235.123";
            orec[5] = "15";
            orec[6] = "12345"; //put a decimal in integer, we won't throw an exception beacuse we do not test for that.
            orec[7] = "2008-12-21";
            orec[8] = "f";
            odbf.Write(orec, true);

            orec[0] = "Stéfanié Singer longer than fits in the DBF record!";
            orec[1] = "0.1";
            orec[2] = ".12";
            orec[3] = ".1";
            orec[4] = "";
            orec[5] = "-15";
            orec[6] = "-12345"; //put a decimal in integer, we won't throw an exception beacuse we do not test for that.
            orec[7] = "";
            orec[8] = "no";
            odbf.Write(orec);

            //overwrite first record with last record's data...
            orec.RecordIndex = 0;
            odbf.Write(orec);

            //odbf.Header.RecordCount = 50;
            odbf.WriteHeader();

            odbf.Close();

            //open the same DBF file we just output, and append a few records to it...
            odbf.Open(Path.Combine(TestPath,"TestNew2.dbf"), FileMode.Open);

            orec.Clear();
            orec[0] = "New record added!";
            orec[6] = "100";
            orec[8] = "t";
            odbf.Write(orec, true);

            orec[0] = "New record 2";
            orec[6] = "104";
            orec[8] = "y";
            odbf.Write(orec, true);

            orec[0] = "New record 3";
            orec[6] = "104";
            orec[8] = "TRUE";
            odbf.Write(orec, true);

            if (odbf.Read(0, orec))
            {
                orec[0] = "modified first record";
                odbf.Write(orec, true);
            }

            //read 3rd record and output to console...
            Console.WriteLine(odbf.Read(2).ToString());

            //now add a new record, forcing seek to end of file...
            orec.Clear();
            orec[0] = "New record 4";
            orec[6] = "500";
            orec[8] = "FALSE";
            odbf.Write(orec, true);

            odbf.Close();

            Console.ReadKey();
        }