Пример #1
0
        /// <summary>
        /// Example on how to extract dfs0 data from a 2D dfsu file for certain elements. All items
        /// from dfsu file are extracted.
        /// </summary>
        /// <param name="dfsuFileNamePath">Name, including path, of 2D dfsu file</param>
        /// <param name="elmtsIndices">Indices of elements to extract data from</param>
        /// <param name="useStream">Use stream when writing dfs0 files - then more than 400 files can be created simultaneously</param>
        public static void ExtractDfs0FromDfsu(string dfsuFileNamePath, IList <int> elmtsIndices, bool useStream)
        {
            // If not using stream approach, at most 400 elements at a time can be processed.
            // There is a limit on how many files you can have open at the same time using
            // the standard approach. It will fail in a nasty way, if the maximum number of
            // file handles are exceeded. This is not an issue when using .NET streams.
            if (!useStream && elmtsIndices.Count > 400)
            {
                throw new ArgumentException("At most 400 elements at a time");
            }

            // Open source dfsu file
            IDfsuFile source;
            Stream    stream = null;

            if (useStream)
            {
                stream = new FileStream(dfsuFileNamePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                source = DfsuFile.Open(stream);
            }
            else
            {
                source = DfsuFile.Open(dfsuFileNamePath);
            }

            // Figure out "basic" dfs0 file name
            string dfsuFilename     = Path.GetFileNameWithoutExtension(dfsuFileNamePath);
            string path             = Path.GetDirectoryName(dfsuFileNamePath);
            string dfs0BaseFilename = Path.Combine(path, "test_" + dfsuFilename + "-");

            // Factory for creating dfs objects
            DfsFactory factory = new DfsFactory();

            // Create a dfs0 file for each element in elmtsIndices
            DfsFile[] dfs0Files   = new DfsFile[elmtsIndices.Count];
            Stream[]  dfs0Streams = new Stream [elmtsIndices.Count];
            double    timeSpan    = source.TimeStepInSeconds * source.NumberOfTimeSteps;

            for (int k = 0; k < elmtsIndices.Count; k++)
            {
                // Index of element to create dfs0 for
                int elmtsIndex = elmtsIndices[k];

                // Calculate element center coordinates, to be stored in dfs0 items.
                // Stored as float in dfs0, hence possible loss of precision...
                float x = 0, y = 0, z = 0;
                int[] nodeNumbers = source.ElementTable[elmtsIndex];
                for (int i = 0; i < nodeNumbers.Length; i++)
                {
                    int nodeIndex = nodeNumbers[i] - 1; // from number to index
                    x += (float)source.X[nodeIndex];
                    y += (float)source.Y[nodeIndex];
                    z += source.Z[nodeIndex];
                }
                x /= nodeNumbers.Length;
                y /= nodeNumbers.Length;
                z /= nodeNumbers.Length;

                // Start building dfs0 file header
                DfsBuilder builder = DfsBuilder.Create("fileTitle", "appTitle", 1);
                builder.SetDataType(1); // standard dfs0 value
                builder.SetGeographicalProjection(source.Projection);
                builder.SetTemporalAxis(factory.CreateTemporalEqCalendarAxis(eumUnit.eumUsec, source.StartDateTime, 0, source.TimeStepInSeconds));

                // Add all dynamic items from dfsu file to dfs0 file
                for (int j = 0; j < source.ItemInfo.Count; j++)
                {
                    IDfsSimpleDynamicItemInfo sourceItem  = source.ItemInfo[j];
                    DfsDynamicItemBuilder     itemBuilder = builder.CreateDynamicItemBuilder();
                    itemBuilder.Set(sourceItem.Name, sourceItem.Quantity, sourceItem.DataType);
                    itemBuilder.SetAxis(factory.CreateAxisEqD0());
                    itemBuilder.SetValueType(sourceItem.ValueType);
                    itemBuilder.SetReferenceCoordinates(x, y, z); // optional
                    builder.AddDynamicItem(itemBuilder.GetDynamicItemInfo());
                }


                // Create and get file, store them in dfs0s array
                string dfs0Filename = dfs0BaseFilename + (elmtsIndex).ToString("000000") + ".dfs0";
                if (useStream)
                {
                    // Create file using C# streams - necessary to provie number of time steps and timespan of data
                    builder.SetNumberOfTimeSteps(source.NumberOfTimeSteps);
                    builder.SetTimeInfo(0, timeSpan);
                    Stream dfs0FileStream = new FileStream(dfs0Filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                    builder.CreateStream(dfs0FileStream);
                    dfs0Streams[k] = dfs0FileStream;
                }
                else
                {
                    // Create file in the ordinary way. Will include statistics (of delete values etc).
                    builder.CreateFile(dfs0Filename);
                }
                dfs0Files[k] = builder.GetFile();
            }

            // For performance, use predefined itemdata objects when reading data from dfsu
            IDfsItemData <float>[] dfsuItemDatas = new IDfsItemData <float> [source.ItemInfo.Count];
            for (int j = 0; j < source.ItemInfo.Count; j++)
            {
                dfsuItemDatas[j] = (IDfsItemData <float>)source.ItemInfo[j].CreateEmptyItemData();
            }

            // Read data from dfsu and store in dfs0
            float[] dfs0Data = new float[1];
            for (int i = 0; i < source.NumberOfTimeSteps; i++)
            {
                for (int j = 0; j < source.ItemInfo.Count; j++)
                {
                    // Read data from dfsu
                    IDfsItemData <float> dfsuItemData = dfsuItemDatas[j];
                    bool    ok     = source.ReadItemTimeStep(dfsuItemData, i);
                    float[] floats = dfsuItemData.Data;

                    // write data to dfs0's
                    for (int k = 0; k < elmtsIndices.Count; k++)
                    {
                        int elmtsIndex = elmtsIndices[k];
                        dfs0Data[0] = floats[elmtsIndex];
                        dfs0Files[k].WriteItemTimeStepNext(0, dfs0Data);
                    }
                }
            }

            // Close dfsu files
            source.Close();
            if (stream != null)
            {
                stream.Close();
            }
            // Close all dfs0 files
            for (int k = 0; k < elmtsIndices.Count; k++)
            {
                dfs0Files[k].Close();
                if (dfs0Streams[k] != null)
                {
                    dfs0Streams[k].Close();
                }
            }
        }