Beispiel #1
0
        static void Main(string[] args)
        {
            //if (args.Length < 1)
            //{
            //    throw new Exception("Result file name should be provided!");
            //}
            //string Resultfilename = args[0];
            int  evalCount;
            int  runId;
            bool success = int.TryParse(args[0], out evalCount);

            if (success)
            {
                //parsing runId
                bool success2 = int.TryParse(args[1], out runId);
                if (success2)
                {
                    var    thisPath       = System.IO.Directory.GetCurrentDirectory();
                    string Resultfilename = thisPath + "\\AutomaticXSCal\\Model\\run" + runId.ToString() + "\\SonghuaHDv2-" + evalCount.ToString() + ".mhydro - Result Files\\HD_Songhua.res1d";
                    // load a result file
                    IResultData resultData = new ResultData();
                    resultData.Connection = Connection.Create(Resultfilename);
                    Diagnostics resultDiagnostics = new Diagnostics("Diag");
                    resultData.Load(resultDiagnostics);
                    if (resultDiagnostics.ErrorCountRecursive > 0) //Report error messages if errors found in result file
                    {
                        throw new Exception("Result file could not be loaded.");
                    }

                    IRes1DReaches reaches = resultData.Reaches; //Load reaches data. A reach is a branch, or a part of a branch between two branch connections.
                    if (reaches.Count == 0)
                    {
                        throw new Exception("The selected file doesn't contain any branch to export data from. Please select another file.");                     //Report error message if no branch exists in file (e.g. for a catchment result file)
                    }
                    if (reaches[0].DataItems.Count == 0)
                    {
                        throw new Exception("The selected file doesn't contain any distributed item on branches and cannot be processed. Please select another file.");                                                                                                       //Report error if no result item exists in grid points (e.g. for a result file containing only structure results)
                    }
                    int          ExportItemIndex = 0;                                                                                                                                                                                                                         // DataItem = 0, for WaterLevel; DataItem = 1, for Discharge.
                    string       Outputfilename  = thisPath + "\\AutomaticXSCal\\Model\\run" + runId.ToString() + "\\ExportMain" + evalCount.ToString() + ".txt";
                    StreamWriter SW = File.CreateText(Outputfilename);                                                                                                                                                                                                        //Create the ouptut text file
                    SW.WriteLine(reaches[0].DataItems[ExportItemIndex].Quantity.Description.ToString() + " results (" + reaches[0].DataItems[ExportItemIndex].Quantity.EumQuantity.UnitDescription.ToString() + ") exported from " + Resultfilename + " on " + DateTime.Now); //Write input information (file name, item, unit) and date in the output text file
                    SW.WriteLine("Branch name" + "\t" + "Chainage" + "\t" + "X coordinate" + "\t" + "Y coordinate" + "\t" + "Min. value" + "\t" + "Max. value" + "\t" + "Mark 1 level" + "\t" + "Mark 2 level" + "\t" + "Mark 3 level");                                      //Write header to the output file


                    // Loop over all reaches
                    for (int j = 0; j < reaches.Count; j++)
                    {
                        IRes1DReach reach          = reaches[j];                       //Load reach number j
                        IDataItem   ResultDataItem = reach.DataItems[ExportItemIndex]; //Load data item number k (for example, number 0 for water level, in a standard HD result file)
                        int[]       indexList      = ResultDataItem.IndexList;         //Load the list of indexes for the current reach and the current data item. Each calculation point has its own index in the reach

                        //export water level
                        //StreamWriter SWWL = File.CreateText("WaterLevel.txt");
                        //using csmatio
                        string   matFpath = thisPath + "\\AutomaticXSCal\\Model\\run" + runId.ToString() + "\\WaterLevelTS" + evalCount.ToString() + ".mat";
                        int[]    dim_WL   = new int[] { 2922, ResultDataItem.NumberOfElements };// 2922 is the time series length at daily step
                        MLDouble mlWL     = new MLDouble("WLsim", dim_WL);

                        // Loop over all calculation points from the current reach
                        for (int i = 0; i < ResultDataItem.NumberOfElements; i++)
                        {
                            if (indexList != null)                                                  //Check if there is a calculation point
                            {
                                float[] TSDataForElement  = ResultDataItem.CreateTimeSeriesData(i); //Get the time series for calculation point i
                                double  MaxDataForElement = TSDataForElement.Max();                 //Get the maximum value from this time series
                                double  MinDataForElement = TSDataForElement.Min();                 //Get the minimum value from this time series
                                // too slow
                                //string wl_all = "";
                                //foreach (double wl in TSDataForElement)
                                //{
                                //    wl_all = wl_all + wl.ToString() + ",";
                                //}
                                //SWWL.WriteLine(wl_all.Substring(0,wl_all.Length - 1));
                                int ielement = 0;
                                foreach (double wl in TSDataForElement)
                                {
                                    mlWL.Set(wl, ielement, i);
                                    ielement++;
                                }

                                int             gridPointIndex = ResultDataItem.IndexList[i];      //Get the index of calculation point i
                                IRes1DGridPoint gridPoint      = reach.GridPoints[gridPointIndex]; //Load calculation point

                                if (gridPoint is IRes1DHGridPoint)                                 //Processing of h-points
                                {
                                    IRes1DHGridPoint   hGridPoint   = gridPoint as IRes1DHGridPoint;
                                    IRes1DCrossSection crossSection = hGridPoint.CrossSection;
                                    if (crossSection is IRes1DOpenCrossSection) //Check if calculation point has an open cross section, in which case extra information will be extracted
                                    {
                                        IRes1DOpenCrossSection openXs = crossSection as IRes1DOpenCrossSection;
                                        double M1 = openXs.Points[openXs.LeftLeveeBank].Z;                                                                                                                                                                                                                                   //Get Z elevation for marker 1
                                        double M2 = openXs.Points[openXs.LowestPoint].Z;                                                                                                                                                                                                                                     //Get Z elevation for marker 2
                                        double M3 = openXs.Points[openXs.RightLeveeBank].Z;                                                                                                                                                                                                                                  //Get Z elevation for marker 3
                                        SW.WriteLine(reach.Name + "\t" + hGridPoint.Chainage.ToString() + "\t" + hGridPoint.X.ToString() + "\t" + hGridPoint.Y.ToString() + "\t" + MinDataForElement.ToString() + "\t" + MaxDataForElement.ToString() + "\t" + M1.ToString() + "\t" + M2.ToString() + "\t" + M3.ToString()); //Write all information including marker levels to output file
                                    }
                                    else
                                    {
                                        SW.WriteLine(reach.Name + "\t" + hGridPoint.Chainage.ToString() + "\t" + hGridPoint.X.ToString() + "\t" + hGridPoint.Y.ToString() + "\t" + MinDataForElement.ToString() + "\t" + MaxDataForElement.ToString()); //For other calculation points (without cross sections), write information to output file
                                    }
                                }
                                else if (gridPoint is IRes1DQGridPoint) //Processing of regular Q-points
                                {
                                    IRes1DQGridPoint QGridPoint = gridPoint as IRes1DQGridPoint;
                                    SW.WriteLine(reach.Name + "\t" + QGridPoint.Chainage.ToString() + "\t" + QGridPoint.X.ToString() + "\t" + QGridPoint.Y.ToString() + "\t" + MinDataForElement.ToString() + "\t" + MaxDataForElement.ToString()); //Write information to output file
                                }
                                else if (gridPoint is IRes1DStructureGridPoint)                                                                                                                                                                     //Processing of structure Q-points
                                {
                                    IRes1DStructureGridPoint QGridPoint = gridPoint as IRes1DStructureGridPoint;
                                    SW.WriteLine(reach.Name + "\t" + QGridPoint.Chainage.ToString() + "\t" + QGridPoint.X.ToString() + "\t" + QGridPoint.Y.ToString() + "\t" + MinDataForElement.ToString() + "\t" + MaxDataForElement.ToString()); //Write information to output file
                                }
                                else
                                {
                                    SW.WriteLine("WARNING: a calculation point with a non-supported type could not be exported."); //Ensure that if a specific type of point is not supported by the current program, a message is returned and the program can proceed with the other points
                                }
                            }
                        }
                        //Save .mat files
                        List <MLArray> mlList = new List <MLArray>();
                        mlList.Add(mlWL);
                        MatFileWriter mfw = new MatFileWriter(matFpath, mlList, false);
                    }
                    SW.Close();           //Release the ouput file
                    resultData.Dispose(); //Release the result file
                }
            }
        }
        /// <summary>
        /// Example stepping through the structure of the
        /// ResultData object
        /// </summary>
        /// <param name="resultFilepath">Path to a result file</param>
        public static void FirstExample(string resultFilepath)
        {
            // load a result file
            IResultData resultData = new ResultData();

            resultData.Connection = Connection.Create(resultFilepath);
            Diagnostics resultDiagnostics = new Diagnostics("Example");

            resultData.Load(resultDiagnostics);

            if (resultDiagnostics.ErrorCountRecursive > 0)
            {
                // Do some error reporting
                throw new Exception("File could not be loaded");
            }

            // Time definition of the time step stored
            int      numtimesteps = resultData.NumberOfTimeSteps;
            DateTime startTime    = resultData.StartTime;
            DateTime endTime      = resultData.EndTime;
            // times for all timesteps
            IListDateTimes times = resultData.TimesList;

            // Results are stored in DataItem’s, where one DataItem contains e.g. water level.
            // Nodes, Reaches, Catchments and GlobalData of the resultData object will have a
            // number of data items.
            // A data item will store data for a number of elements and for a number
            // of time steps.
            // In reaches there will be more than one "element", since a reach will store
            // data for several grid points. Nodes and catchments will always only have
            // one element per data item

            IRes1DNodes      nodes      = resultData.Nodes;
            IRes1DReaches    reaches    = resultData.Reaches;
            IRes1DCatchments catchments = resultData.Catchments;
            IRes1DGlobalData globals    = resultData.GlobalData;

            // Generic loop over catchments/nodes/reaches (just replace catchments with nodes)
            foreach (IRes1DDataSet dataSet in catchments)
            {
                // Each dataset has a number of data items
                IDataItems dataItems = dataSet.DataItems;

                // Loop over all data items
                foreach (IDataItem dataItem in dataItems)
                {
                    // A dataitem contains data for one quantity
                    IQuantity quantity = dataItem.Quantity;

                    // You can check the type of quantity in the data item
                    if (quantity.Equals(Quantity.Create(PredefinedQuantity.WaterLevel)))
                    {
                    }
                }
            }

            // The example will now show how to extract data from a reach.

            // Get the first reach in the list of reaches
            IRes1DReach reach = reaches[0];

            // Extract first data item for reach (there will be more than one)
            // For a HD result file, this will be a water level.
            IDataItem wlDataItem = reach.DataItems[0];

            // Take data for all elements (subset of grid points) for the initial time step
            // For water level quantity: One value for each H-grid point
            float[] wlValues = wlDataItem.TimeData.GetValues(0);

            // For a reach, if the IndexList is defined (not null), this
            // gives the relation to the grid point where the values in
            // a data item belongs to.
            // For each value in the data item, it indicates the index of the grid point
            // it belongs to, i.e. the size matches the number of elements in the data item.
            // i.g. for water level: 0, 2, 4, 6, 8,...
            // and  for discharge  : 1, 3, 5, 7,...
            // The IndexList can be null, in which case information on the geometry of the
            // data in the data item exist in the element set
            int[] indexList = wlDataItem.IndexList;

            // Loop over all elements (subset of grid points) in the data item
            for (int i = 0; i < wlDataItem.NumberOfElements; i++)
            {
                // Time series values for this element (grid point)
                float[] wlDataForElement = wlDataItem.CreateTimeSeriesData(i);

                // Check if there is a grid-point relation
                if (indexList != null)
                {
                    // Index of grid point in list of grid opints
                    int gridPointIndex = wlDataItem.IndexList[i];

                    // Grid point where values belong to.
                    IRes1DGridPoint gridPoint = reach.GridPoints[gridPointIndex];

                    // More detailed information exist in the derived grid point classes
                    if (gridPoint is IRes1DHGridPoint)
                    {
                        IRes1DHGridPoint hGridPoint = gridPoint as IRes1DHGridPoint;

                        // An H-grid point also has a cross section.
                        IRes1DCrossSection crossSection = hGridPoint.CrossSection;

                        // More detailed information on the cross section exist in the derived types
                        if (crossSection is IRes1DOpenCrossSection)
                        {
                            // The open cross section has all the raw points and cross section markers
                            IRes1DOpenCrossSection  openXs        = crossSection as IRes1DOpenCrossSection;
                            IRes1DCrossSectionPoint lowestXsPoint = openXs.Points[openXs.LowestPoint];
                        }
                    }
                }
            }
        }