Beispiel #1
0
        /// <summary>
        /// Read stream estimate coefficients and store in a list. </summary>
        /// <param name="filename"> Name of file to read. </param>
        /// <returns> list of streamflow estimate coefficients data </returns>
        /// <exception cref="Exception"> if there is an error reading the file. </exception>
        //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        //ORIGINAL LINE: public static java.util.List<StateMod_StreamEstimate_Coefficients> readStateModFile(String filename) throws Exception
        public static IList <StateMod_StreamEstimate_Coefficients> readStateModFile(string filename)
        {
            string routine = "StateMod_StreamEstimate_Coefficients.readStateModFile";
            IList <StateMod_StreamEstimate_Coefficients> theBaseflows = new List <StateMod_StreamEstimate_Coefficients>();

            string         iline = null;
            IList <object> v     = new List <object>(2); // used to retrieve from fixedRead
            string         adnl  = null;

            int[]        format_0  = new int[] { StringUtil.TYPE_STRING, StringUtil.TYPE_SPACE, StringUtil.TYPE_INTEGER };
            int[]        format_0w = new int[] { 12, 8, 8 };
            int[]        format_1  = new int[] { StringUtil.TYPE_DOUBLE, StringUtil.TYPE_SPACE, StringUtil.TYPE_STRING };
            int[]        format_1w = new int[] { 8, 1, 12 };
            int[]        format_2  = new int[] { StringUtil.TYPE_SPACE, StringUtil.TYPE_DOUBLE, StringUtil.TYPE_INTEGER };
            int[]        format_2w = new int[] { 12, 8, 8 };
            int[]        format_3  = new int[] { StringUtil.TYPE_DOUBLE, StringUtil.TYPE_SPACE, StringUtil.TYPE_STRING };
            int[]        format_3w = new int[] { 8, 1, 12 };
            StreamReader @in       = null;
            StateMod_StreamEstimate_Coefficients aBaseflow = null;
            int i = 0;
            int num_adnl;
            int begin_pos;
            int end_pos;
            int linecount = 0;

            if (Message.isDebugOn)
            {
                Message.printDebug(30, routine, "Reading file :" + filename);
            }

            try
            {
                @in = new StreamReader(filename);
                while (!string.ReferenceEquals((iline = @in.ReadLine()), null))
                {
                    ++linecount;
                    // check for comments
                    if (iline.StartsWith("#", StringComparison.Ordinal) || iline.Trim().Length == 0)
                    {
                        continue;
                    }
                    // allocate a new baseflow
                    aBaseflow = new StateMod_StreamEstimate_Coefficients();

                    // read in first of two lines for each baseflow
                    StringUtil.fixedRead(iline, format_0, format_0w, v);
                    aBaseflow.setFlowX(((string)v[0]).Trim());
                    aBaseflow.setN((int?)v[1]);

                    num_adnl = aBaseflow.getN();
                    for (i = 0; i < num_adnl; i++)
                    {
                        // calculate begin_pos and end_pos
                        //	 8(factor to weight the gaged flow)
                        // +	 1(space)
                        // +	12(station id upstream X)
                        // --------
                        //	21(for each set of num_adnl
                        // +	28(array index after initial info)
                        // +	 1(we should start on next position)
                        // -	 1(our string starts with index 0)
                        //
                        begin_pos = 28 + (i * 21);
                        end_pos   = begin_pos + 21;
                        adnl      = iline.Substring(begin_pos, end_pos - begin_pos);
                        StringUtil.fixedRead(adnl, format_1, format_1w, v);
                        aBaseflow.addCoefn((double?)v[0]);
                        aBaseflow.addUpper((string)v[1]);
                    }

                    // read in second of two lines for each baseflow
                    iline = @in.ReadLine();
                    StringUtil.fixedRead(iline, format_2, format_2w, v);
                    aBaseflow.setProratnf((double?)v[0]);
                    aBaseflow.setM((int?)v[1]);

                    num_adnl = aBaseflow.getM();
                    for (i = 0; i < num_adnl; i++)
                    {
                        begin_pos = 28 + (i * 21);
                        end_pos   = begin_pos + 21;
                        adnl      = iline.Substring(begin_pos, end_pos - begin_pos);
                        StringUtil.fixedRead(adnl, format_3, format_3w, v);
                        aBaseflow.addCoefm((double?)v[0]);
                        aBaseflow.addFlowm((string)v[1]);
                    }

                    // add the baseflow to the vector of baseflows
                    theBaseflows.Add(aBaseflow);
                }
            }
            catch (Exception e)
            {
                Message.printWarning(3, routine, "Error reading near line " + linecount);
                Message.printWarning(3, routine, e);
                throw e;
            }
            finally
            {
                if (@in != null)
                {
                    @in.Close();
                }
                @in = null;
            }
            return(theBaseflows);
        }