Esempio n. 1
0
        /// <summary>
        /// Creates a backup of the current data object and stores it in _original,
        /// for use in determining if an object was changed inside of a GUI.
        /// </summary>
        public virtual void createBackup()
        {
            _original = clone();
            ((StateCU_PenmanMonteith)_original)._isClone = false;
            _isClone = true;

            StateCU_PenmanMonteith pm = (StateCU_PenmanMonteith)_original;

            __nGrowthStages = pm.__nGrowthStages;
            int ncpgs = getNCoefficientsPerGrowthStage();

            __kcday = new double[__nGrowthStages][];
            __kcb   = new double[__nGrowthStages][];
            for (int igs = 0; igs < __nGrowthStages; igs++)
            {
                __kcday[igs] = new double[ncpgs];
                __kcb[igs]   = new double[ncpgs];
                // Default these to simplify setting in DMI and other code...
                for (int i = 0; i < ncpgs; i++)
                {
                    __kcday[igs][i] = pm.__kcday[igs][i];
                    __kcb[igs][i]   = pm.__kcb[igs][i];
                }
            }
        }
        /// <summary>
        /// Creates backups of all the data objects in the Vector so that changes can later be cancelled if necessary.
        /// </summary>
        protected internal override void createDataBackup()
        {
            StateCU_PenmanMonteith station = null;
            int size = _data.Count;

            for (int i = 0; i < size; i++)
            {
                station = (StateCU_PenmanMonteith)_data[i];
                station.createBackup();
            }
        }
        /// <summary>
        /// Called when the cancel button is pressed.  This discards any changes made to the data objects.
        /// </summary>
        protected internal override void cancel()
        {
            StateCU_PenmanMonteith station = null;
            int size = _data.Count;

            for (int i = 0; i < size; i++)
            {
                station = (StateCU_PenmanMonteith)_data[i];
                station.restoreOriginal();
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Cancels any changes made to this object within a GUI since createBackup()
        /// was called and sets _original to null.
        /// </summary>
        public override void restoreOriginal()
        {
            StateCU_PenmanMonteith bc = (StateCU_PenmanMonteith)_original;

            base.restoreOriginal();

            __kcb           = bc.__kcb;
            __kcday         = bc.__kcday;
            __nGrowthStages = bc.__nGrowthStages;
            _isClone        = false;
            _original       = null;
        }
        /// <summary>
        /// Sets up internal arrays. </summary>
        /// <param name="data"> the list of data (non-null) that will be displayed in the table model. </param>
        private void initialize(System.Collections.IList data)
        {
            int size = data.Count;

            __cropFirstRows = new int[size];

            int row = 0;
            StateCU_PenmanMonteith kpm;

            for (int i = 0; i < size; i++)
            {
                kpm = (StateCU_PenmanMonteith)data[i];
                __cropFirstRows[i] = row;
                // The number of rows per crop is the number of growth stages times the number of values per stage
                row += kpm.getNGrowthStages() * StateCU_PenmanMonteith.getNCoefficientsPerGrowthStage();
            }
            _rows = row;
        }
        /// <summary>
        /// Inserts the specified value into the table at the given position. </summary>
        /// <param name="value"> the object to store in the table cell. </param>
        /// <param name="row"> the row of the cell in which to place the object. </param>
        /// <param name="col"> the column of the cell in which to place the object. </param>
        public virtual void setValueAt(object value, int row, int col)
        {
            if (_sortOrder != null)
            {
                row = _sortOrder[row];
            }

            int dataPos = lookupVectorPositionForRow(row);

            StateCU_PenmanMonteith pm = (StateCU_PenmanMonteith)_data.get(dataPos);

            // Row position in the data object...
            int num = row - __cropFirstRows[dataPos];
            // Which growth stage (0+ index)
            int igs = num / StateCU_PenmanMonteith.getNCoefficientsPerGrowthStage();
            // Which value in the growth stage
            int ipos = num - igs * StateCU_PenmanMonteith.getNCoefficientsPerGrowthStage();

            switch (col)
            {
            case __COL_CROP_NAME:
                pm.setName((string)value);
                break;

            case __COL_GROWTH_STAGE:
                /* TODO SAM 2010-03-31 Not editable...
                 * int gsval = ((Integer)value).intValue();
                 * pm.setKtsw( gsval );
                 */
                break;

            case __COL_PERCENT:
                double percent = ((double?)value).Value;
                pm.setCurvePosition(igs, ipos, percent);
                break;

            case __COL_COEFF:
                double coeff = ((double?)value).Value;
                pm.setCurveValue(igs, ipos, coeff);
                break;
            }

            base.setValueAt(value, row, col);
        }
        /// <summary>
        /// From AbstractTableMode.  Returns the data that should be placed in the JTable
        /// at the given row and column. </summary>
        /// <param name="row"> the row for which to return data. </param>
        /// <param name="col"> the column for which to return data. </param>
        /// <returns> the data that should be placed in the JTable at the given row and col. </returns>
        public virtual object getValueAt(int row, int col)
        {
            // make sure the row numbers are never sorted ...
            if (_sortOrder != null)
            {
                row = _sortOrder[row];
            }

            // Position of the data object...
            int dataPos = lookupVectorPositionForRow(row);

            StateCU_PenmanMonteith pm = (StateCU_PenmanMonteith)_data.get(dataPos);

            // Row position in the data object...
            int num = row - __cropFirstRows[dataPos];
            // Which growth stage (0+ index)
            int igs = num / StateCU_PenmanMonteith.getNCoefficientsPerGrowthStage();
            // Which value in the growth stage
            int ipos = num - igs * StateCU_PenmanMonteith.getNCoefficientsPerGrowthStage();

            switch (col)
            {
            case __COL_CROP_NAME:
                return(pm.getName());

            case __COL_GROWTH_STAGE:
                return(new int?(igs + 1));

            case __COL_PERCENT:
                return(new double?(pm.getKcday(igs, ipos)));

            case __COL_COEFF:
                return(new double?(pm.getKcb(igs, ipos)));
            }
            return("");
        }
Esempio n. 8
0
        /// <summary>
        /// Writes a list of StateCU_PenmanMonteith objects to a list file.  A header is
        /// printed to the top of the file, containing the commands used to generate the file.
        /// Any strings in the body of the file that contain the field delimiter will be wrapped in "...". </summary>
        /// <param name="filename"> the name of the file to which the data will be written. </param>
        /// <param name="delimiter"> the delimiter to use for separating field values. </param>
        /// <param name="update"> whether to update an existing file, retaining the current
        /// header (true) or to create a new file with a new header. </param>
        /// <param name="data"> the list of objects to write. </param>
        /// <exception cref="Exception"> if an error occurs. </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static void writeListFile(String filename, String delimiter, boolean update, java.util.List<StateCU_PenmanMonteith> data, java.util.List<String> outputComments) throws Exception
        public static void writeListFile(string filename, string delimiter, bool update, IList <StateCU_PenmanMonteith> data, IList <string> outputComments)
        {
            string routine = "StateCU_PenmanMonteith.writeListFile";
            int    size    = 0;

            if (data != null)
            {
                size = data.Count;
            }

            IList <string> fields = new List <string>();

            fields.Add("Name");
            fields.Add("GrowthStage");
            fields.Add("Percent");
            fields.Add("Coefficient");
            int fieldCount = fields.Count;

            string[] names   = new string[fieldCount];
            string[] formats = new string[fieldCount];
            int      comp    = StateCU_DataSet.COMP_PENMAN_MONTEITH;
            string   s       = null;

            for (int i = 0; i < fieldCount; i++)
            {
                s          = fields[i];
                names[i]   = StateCU_Util.lookupPropValue(comp, "FieldName", s);
                formats[i] = StateCU_Util.lookupPropValue(comp, "Format", s);
            }

            string oldFile = null;

            if (update)
            {
                oldFile = IOUtil.getPathUsingWorkingDir(filename);
            }

            int            j             = 0;
            int            k             = 0;
            PrintWriter    @out          = null;
            IList <string> commentString = new List <string>(1);

            commentString.Add("#");
            IList <string> ignoreCommentString = new List <string>(1);

            ignoreCommentString.Add("#>");
            string[]      line   = new string[fieldCount];
            StringBuilder buffer = new StringBuilder();

            try
            {
                // Add some basic comments at the top of the file.  However, do this to a copy of the
                // incoming comments so that they are not modified in the calling code.
                IList <string> newComments2 = null;
                if (outputComments == null)
                {
                    newComments2 = new List <string>();
                }
                else
                {
                    newComments2 = new List <string>(outputComments);
                }
                newComments2.Insert(0, "");
                newComments2.Insert(1, "StateCU Penman-Monteith crop coefficients as a delimited list file.");
                newComments2.Insert(2, "");
                @out = IOUtil.processFileHeaders(oldFile, IOUtil.getPathUsingWorkingDir(filename), newComments2, commentString, ignoreCommentString, 0);

                for (int i = 0; i < fieldCount; i++)
                {
                    if (i > 0)
                    {
                        buffer.Append(delimiter);
                    }
                    buffer.Append("\"" + names[i] + "\"");
                }

                @out.println(buffer.ToString());
                StateCU_PenmanMonteith kpm;
                for (int i = 0; i < size; i++)
                {
                    kpm = data[i];
                    int ngs   = kpm.getNGrowthStages();
                    int ncpgs = StateCU_PenmanMonteith.getNCoefficientsPerGrowthStage();
                    for (int igs = 0; igs < ngs; igs++)
                    {
                        for (j = 0; j < ncpgs; j++)
                        {
                            line[0] = StringUtil.formatString(kpm.getName(), formats[0]).Trim();
                            line[1] = StringUtil.formatString((igs + 1), formats[1]).Trim();
                            line[2] = StringUtil.formatString(kpm.getKcday(igs, j), formats[2]).Trim();
                            line[3] = StringUtil.formatString(kpm.getKcb(igs, j), formats[3]).Trim();

                            buffer = new StringBuilder();
                            for (k = 0; k < fieldCount; k++)
                            {
                                if (k > 0)
                                {
                                    buffer.Append(delimiter);
                                }
                                if (line[k].IndexOf(delimiter, StringComparison.Ordinal) > -1)
                                {
                                    line[k] = "\"" + line[k] + "\"";
                                }
                                buffer.Append(line[k]);
                            }
                            @out.println(buffer.ToString());
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Message.printWarning(3, routine, e);
                throw e;
            }
            finally
            {
                if (@out != null)
                {
                    @out.flush();
                    @out.close();
                }
                @out = null;
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Write a list of StateCU_PenmanMonteith to an opened file. </summary>
        /// <param name="data_Vector"> A list of StateCU_PenmanMonteith to write. </param>
        /// <param name="out"> output PrintWriter. </param>
        /// <param name="props"> Properties to control the output.  Currently only the
        /// optional Precision property can be set, indicating how many digits after the
        /// decimal should be printed (default is 3). </param>
        /// <exception cref="IOException"> if an error occurs. </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private static void writeVector(java.util.List<StateCU_PenmanMonteith> data_Vector, java.io.PrintWriter out, System.Nullable<int> precision) throws java.io.IOException
        private static void writeVector(IList <StateCU_PenmanMonteith> data_Vector, PrintWriter @out, int?precision)
        {
            string cmnt = "#>";

            // Missing data are handled by formatting all as strings (blank if necessary).
            if (precision == null)
            {
                precision = new int?(3);         // Make this agree with the Blaney-Criddle default
            }
            int precision2 = precision.Value;

            @out.println(cmnt);
            @out.println(cmnt + " StateCU Penman-Monteith Crop Coefficient (KPM) File");
            @out.println(cmnt);
            @out.println(cmnt + " Record 1 format (a80)");
            @out.println(cmnt);
            @out.println(cmnt + "  Title     remark:  Title");
            @out.println(cmnt);
            @out.println(cmnt + " Record 2 format (free format)");
            @out.println(cmnt);
            @out.println(cmnt + "  NumCurves     nc:  Number of crop coefficient curves");
            @out.println(cmnt);
            @out.println(cmnt + " Record 3 format (free format)");
            @out.println(cmnt);
            @out.println(cmnt + "  ID            id:  Crop number (not used by StateCU)");
            @out.println(cmnt + "  CropName   cropn:  Crop name (e.g., ALFALFA)");
            @out.println(cmnt);
            @out.println(cmnt + " Record 4 format (free format)");
            @out.println(cmnt);
            @out.println(cmnt + "  Percent    kcday:  Time from start of growth to effective cover (%)");
            @out.println(cmnt + "                     or % of next growth stage.");
            @out.println(cmnt + "  Coeff        kcb:  Crop coefficient for alfalfa-based ET");
            @out.println(cmnt);
            @out.println(cmnt + "  33 day/crop coefficient pairs for alfalfa.");
            @out.println(cmnt + "  11 day/crop coefficient pairs for grass pasture.");
            @out.println(cmnt + "  22 day/crop coefficient pairs for all other crop types.");
            @out.println(cmnt);
            @out.println(cmnt + "Title");
            @out.println(cmnt + "NumCurves");
            @out.println(cmnt + "ID CropName");
            @out.println(cmnt + "Percent Coeff");
            @out.println(cmnt + "----------------------------");
            @out.println(cmnt + "EndHeader");
            // Default title
            @out.println("Crop Coefficient Curves for Penman-Monteith");

            int num = 0;

            if (data_Vector != null)
            {
                num = data_Vector.Count;
            }
            @out.println(num);
            // Width allows precision to be increased some...
            string value_format = "%8." + precision2 + "f";
            int    i            = 0;

            foreach (StateCU_PenmanMonteith kpm in data_Vector)
            {
                ++i;
                if (kpm == null)
                {
                    continue;
                }
                // Crop number (not used by StateCU) and name
                string name = kpm.getName();
                // Since free format, the ID must always have something.  If
                // we don't know, put -999...
                string id = "" + i;         // Default to sequential number
                @out.println(id + " " + name);

                // Loop through the number of growth stages per crop
                int nGrowthStages = kpm.getNGrowthStages();
                int ncpgs         = StateCU_PenmanMonteith.getNCoefficientsPerGrowthStage();
                for (int igs = 0; igs < nGrowthStages; igs++)
                {
                    for (int j = 0; j < ncpgs; j++)
                    {
                        @out.println(StringUtil.formatString(kpm.getKcday(igs, j), "%3.0f") + StringUtil.formatString(kpm.getKcb(igs, j), value_format));
                    }
                }
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Read the StateCU KPM file and return as a list of StateCU_PenmanMonteith. </summary>
        /// <param name="filename"> filename containing KPM records. </param>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static java.util.List<StateCU_PenmanMonteith> readStateCUFile(String filename) throws java.io.IOException
        public static IList <StateCU_PenmanMonteith> readStateCUFile(string filename)
        {
            string rtn   = "StateCU_PenmanMonteith.readStateCUFile";
            string iline = null;
            StateCU_PenmanMonteith         kpm     = null;
            IList <StateCU_PenmanMonteith> kpmList = new List <StateCU_PenmanMonteith>(25);
            StreamReader @in     = null;
            int          lineNum = 0;

            Message.printStatus(1, rtn, "Reading StateCU KPM file: " + filename);
            try
            {
                // The following throws an IOException if the file cannot be opened...
                @in = new StreamReader(filename);
                int    nc    = -1;
                string title = null;         // The title is currently read but not stored since it is never really used for anything.
                while (!string.ReferenceEquals((iline = @in.ReadLine()), null))
                {
                    ++lineNum;
                    // check for comments
                    if (iline.StartsWith("#", StringComparison.Ordinal) || iline.Trim().Length == 0)
                    {
                        continue;
                    }
                    if (string.ReferenceEquals(title, null))
                    {
                        title = iline;
                    }
                    else if (nc < 0)
                    {
                        // Assume that the line contains the number of crops
                        nc = int.Parse(iline.Trim());
                        break;
                    }
                }

                // Now loop through the number of curves...

                // TODO SAM 2010-03-30 Evaluate if needed
                //String id;
                string         cropn;
                IList <string> tokens;
                int            j = 0;
                // Read the number of curves (crops)
                for (int i = 0; i < nc; i++)
                {
                    // Read a free format line...
                    iline = @in.ReadLine();
                    ++lineNum;

                    tokens = StringUtil.breakStringList(iline.Trim(), " \t", StringUtil.DELIM_SKIP_BLANKS);
                    // TODO SAM 2007-02-18 Evaluate if needed
                    //id = tokens.elementAt(0);
                    cropn = tokens[1];

                    // Allocate new StateCU_PenmanMonteith instance...

                    int ngs = getNGrowthStagesFromCropName(cropn);
                    kpm = new StateCU_PenmanMonteith(ngs);
                    // Number of coefficients per growth stage...
                    int ncpgs = StateCU_PenmanMonteith.getNCoefficientsPerGrowthStage();
                    kpm.setName(cropn);
                    // TODO SAM 2005-05-22 Ignore the old ID and use the crop name - this facilitates
                    // sorting and other standard StateCU_Data features.
                    //kbc.setID ( id );
                    kpm.setID(cropn);

                    // Read the coefficients...
                    for (int igs = 0; igs < ngs; igs++)
                    {
                        for (j = 0; j < ncpgs; j++)
                        {
                            iline = @in.ReadLine();
                            ++lineNum;
                            tokens = StringUtil.breakStringList(iline.Trim(), " \t", StringUtil.DELIM_SKIP_BLANKS);
                            kpm.setCurvePosition(igs, j, double.Parse(tokens[0]));
                            kpm.setCurveValue(igs, j, double.Parse(tokens[1]));
                        }
                    }

                    // add the StateCU_PenmanMonteith to the list...
                    kpmList.Add(kpm);
                }
            }
            catch (Exception e)
            {
                Message.printWarning(3, rtn, "Error reading file (" + e + ").");
                Message.printWarning(3, rtn, e);
                throw new IOException("Error reading file \"" + filename + "\" near line " + lineNum);
            }
            finally
            {
                if (@in != null)
                {
                    @in.Close();
                }
            }
            return(kpmList);
        }