Пример #1
0
        public static DfsFile CreateFromTemplate(IDfsFile dfsTemplate, string outputfile, int nRepeats)
        {
            IDfsFileInfo fi = dfsTemplate.FileInfo;
            //this._AnalyzeDfsInputItems(dfsTemplate.ItemInfo);
            var builder = DfsBuilder.Create(fi.FileTitle, fi.ApplicationTitle, fi.ApplicationVersion);

            CreateHeader(fi, builder);

            var isDfsu3d = _IsDfsu3d(dfsTemplate.ItemInfo);

            if (nRepeats > 1)
            {
                if (isDfsu3d)
                {
                    builder.AddDynamicItem(dfsTemplate.ItemInfo[0]);
                }

                _CreateRepeatedDynamicItems(builder, dfsTemplate.ItemInfo, nRepeats);
            }
            else
            {
                var items = Enumerable.Range(0, _NumberItems(dfsTemplate.ItemInfo)).ToList();
                CreateDynamicItems(builder, dfsTemplate.ItemInfo, items);
            }
            builder.CreateFile(outputfile);

            IDfsStaticItem staticItem;

            while ((staticItem = dfsTemplate.ReadStaticItemNext()) != null)
            {
                builder.AddStaticItem(staticItem);
            }

            return(builder.GetFile());
        }
Пример #2
0
        private DfsFile _CreateFromTemplate(IDfsFile dfsTemplate, string outputfile, IEnumerable <int> timesteps, int stride, List <int> items)
        {
            IDfsFileInfo fi = dfsTemplate.FileInfo;
            //this._AnalyzeDfsInputItems(dfsTemplate.ItemInfo);
            var builder = DfsBuilder.Create(fi.FileTitle, fi.ApplicationTitle, fi.ApplicationVersion);

            IDfsTemporalAxis timeAxis = _CorrectTimeAxis(fi.TimeAxis, timesteps.First(), stride);

            DfsOutput.CreateHeader(fi, builder, timeAxis);
            DfsOutput.CreateDynamicItems(builder, dfsTemplate.ItemInfo, items);

            builder.CreateFile(outputfile);

            IDfsStaticItem staticItem;

            while ((staticItem = dfsTemplate.ReadStaticItemNext()) != null)
            {
                builder.AddStaticItem(staticItem);
            }

            return(builder.GetFile());
        }
Пример #3
0
        /// <summary>
        /// Create a new file, being the difference of two files.
        /// <para>
        /// The two input files must be equal in structure, e.g. coming
        /// from the same simulation but giving different results.
        /// Header and static data must be identical, only difference
        /// must be in values of the dynamic data.
        /// </para>
        /// </summary>
        public static void CreateDiffFile(string file1, string file2, string filediff = null)
        {
            IDfsFile dfs1 = DfsFileFactory.DfsGenericOpen(file1);
            IDfsFile dfs2 = DfsFileFactory.DfsGenericOpen(file2);

            // Validate that it has the same number of items.
            if (dfs1.ItemInfo.Count != dfs2.ItemInfo.Count)
            {
                throw new Exception("Number of dynamic items does not match");
            }
            int numItems = dfs1.ItemInfo.Count;

            // In case number of time steps does not match, take the smallest.
            int numTimes = dfs1.FileInfo.TimeAxis.NumberOfTimeSteps;

            if (numTimes > dfs2.FileInfo.TimeAxis.NumberOfTimeSteps)
            {
                numTimes = dfs2.FileInfo.TimeAxis.NumberOfTimeSteps;
                Console.Out.WriteLine("Number of time steps does not match, using the smallest number");
            }

            // For recording max difference for every item
            double[] maxDiff = new double[dfs1.ItemInfo.Count];
            // Index in time (index) of maximum and first difference. -1 if no difference
            int[] maxDiffTime   = new int[dfs1.ItemInfo.Count];
            int[] firstDiffTime = new int[dfs1.ItemInfo.Count];
            for (int i = 0; i < dfs1.ItemInfo.Count; i++)
            {
                maxDiffTime[i]   = -1;
                firstDiffTime[i] = -1;
            }

            // Copy over info from the first file, assuming the second file contains the same data.
            IDfsFileInfo fileInfo = dfs1.FileInfo;

            DfsBuilder builder = null;

            if (!string.IsNullOrEmpty(filediff))
            {
                builder = DfsBuilder.Create(fileInfo.FileTitle, fileInfo.ApplicationTitle, fileInfo.ApplicationVersion);

                // Set up the header
                builder.SetDataType(fileInfo.DataType);
                builder.SetGeographicalProjection(fileInfo.Projection);
                builder.SetTemporalAxis(fileInfo.TimeAxis);
                builder.SetItemStatisticsType(fileInfo.StatsType);
                builder.DeleteValueByte        = fileInfo.DeleteValueByte;
                builder.DeleteValueDouble      = fileInfo.DeleteValueDouble;
                builder.DeleteValueFloat       = fileInfo.DeleteValueFloat;
                builder.DeleteValueInt         = fileInfo.DeleteValueInt;
                builder.DeleteValueUnsignedInt = fileInfo.DeleteValueUnsignedInt;

                // Transfer compression keys.
                if (fileInfo.IsFileCompressed)
                {
                    int[] xkey;
                    int[] ykey;
                    int[] zkey;
                    fileInfo.GetEncodeKey(out xkey, out ykey, out zkey);
                    builder.SetEncodingKey(xkey, ykey, zkey);
                }

                // Copy custom blocks
                foreach (IDfsCustomBlock customBlock in fileInfo.CustomBlocks)
                {
                    builder.AddCustomBlock(customBlock);
                }
            }

            // Copy dynamic item definitions
            bool[] floatItems = new bool[dfs1.ItemInfo.Count];
            for (int i = 0; i < dfs1.ItemInfo.Count; i++)
            {
                var itemInfo = dfs1.ItemInfo[i];

                // Validate item sizes
                var itemInfo2 = dfs2.ItemInfo[i];
                if (itemInfo.ElementCount != itemInfo2.ElementCount)
                {
                    throw new Exception("Dynamic items must have same size, item number " + (i + 1) +
                                        " has different sizes in the two files");
                }
                // Validate the data type, only supporting floats and doubles.
                if (itemInfo.DataType == DfsSimpleType.Float)
                {
                    floatItems[i] = true;
                }
                else if (itemInfo.DataType != DfsSimpleType.Double)
                {
                    throw new Exception("Dynamic item must be double or float, item number " + (i + 1) + " is of type " +
                                        (itemInfo.DataType));
                }

                builder?.AddDynamicItem(itemInfo);
            }

            // Create file
            builder?.CreateFile(filediff);

            if (builder != null)
            {
                // Copy over static items from file 1, assuming the static items of file 2 are identical
                IDfsStaticItem si1;
                while (null != (si1 = dfs1.ReadStaticItemNext()))
                {
                    builder.AddStaticItem(si1);
                }
            }

            // Get the file
            DfsFile diff = builder?.GetFile();

            // Write dynamic data to the file, being the difference between the two
            for (int i = 0; i < numTimes; i++)
            {
                for (int j = 0; j < numItems; j++)
                {
                    if (floatItems[j])
                    {
                        IDfsItemData <float> data1 = dfs1.ReadItemTimeStepNext() as IDfsItemData <float>;
                        IDfsItemData <float> data2 = dfs2.ReadItemTimeStepNext() as IDfsItemData <float>;
                        for (int k = 0; k < data1.Data.Length; k++)
                        {
                            float valuediff = data1.Data[k] - data2.Data[k];
                            data1.Data[k] = valuediff;
                            float absValueDiff = Math.Abs(valuediff);
                            if (absValueDiff > maxDiff[j])
                            {
                                maxDiff[j]     = absValueDiff;
                                maxDiffTime[j] = i;
                                if (firstDiffTime[j] == -1)
                                {
                                    firstDiffTime[j] = i;
                                }
                            }
                        }
                        diff?.WriteItemTimeStepNext(data1.Time, data1.Data);
                    }
                    else
                    {
                        IDfsItemData <double> data1 = dfs1.ReadItemTimeStepNext() as IDfsItemData <double>;
                        IDfsItemData <double> data2 = dfs2.ReadItemTimeStepNext() as IDfsItemData <double>;
                        for (int k = 0; k < data1.Data.Length; k++)
                        {
                            double valuediff = data1.Data[k] - data2.Data[k];
                            data1.Data[k] = valuediff;
                            double absValueDiff = Math.Abs(valuediff);
                            if (absValueDiff > maxDiff[j])
                            {
                                maxDiff[j]     = absValueDiff;
                                maxDiffTime[j] = i;
                                if (firstDiffTime[j] == -1)
                                {
                                    firstDiffTime[j] = i;
                                }
                            }
                        }
                        diff?.WriteItemTimeStepNext(data1.Time, data1.Data);
                    }
                }
            }

            System.Console.WriteLine("Difference statistics:");
            for (int i = 0; i < maxDiffTime.Length; i++)
            {
                if (maxDiffTime[i] < 0)
                {
                    Console.WriteLine("{0,-30}: no difference", dfs1.ItemInfo[i].Name);
                }
                else
                {
                    Console.WriteLine("{0,-30}: Max difference at timestep {1,3}: {2}. First difference at timestep {3}", dfs1.ItemInfo[i].Name, maxDiffTime[i], maxDiff[i], firstDiffTime[i]);
                }
            }

            dfs1.Close();
            dfs2.Close();
            diff?.Close();
        }
Пример #4
0
        /// <summary>
        /// Example of how to copy a Dfs file.
        /// <para>
        /// This example is intended to show how to generically copy a file. In
        /// case a copy with modified data is required, this could be used as a base
        /// for the copy.
        /// </para>
        /// </summary>
        /// <param name="sourceFilename">Path and name of the source dfs file</param>
        /// <param name="filename">Path and name of the new file to create</param>
        public static void CopyDfsFile(string sourceFilename, string filename)
        {
            IDfsFile     source   = DfsFileFactory.DfsGenericOpen(sourceFilename);
            IDfsFileInfo fileInfo = source.FileInfo;

            DfsBuilder builder = DfsBuilder.Create(fileInfo.FileTitle, fileInfo.ApplicationTitle, fileInfo.ApplicationVersion);

            // Set up the header
            builder.SetDataType(fileInfo.DataType);
            builder.SetGeographicalProjection(fileInfo.Projection);
            builder.SetTemporalAxis(fileInfo.TimeAxis);
            builder.SetItemStatisticsType(fileInfo.StatsType);
            builder.DeleteValueByte        = fileInfo.DeleteValueByte;
            builder.DeleteValueDouble      = fileInfo.DeleteValueDouble;
            builder.DeleteValueFloat       = fileInfo.DeleteValueFloat;
            builder.DeleteValueInt         = fileInfo.DeleteValueInt;
            builder.DeleteValueUnsignedInt = fileInfo.DeleteValueUnsignedInt;

            // Transfer compression keys - if any.
            if (fileInfo.IsFileCompressed)
            {
                int[] xkey;
                int[] ykey;
                int[] zkey;
                fileInfo.GetEncodeKey(out xkey, out ykey, out zkey);
                builder.SetEncodingKey(xkey, ykey, zkey);
            }

            // Copy custom blocks - if any
            foreach (IDfsCustomBlock customBlock in fileInfo.CustomBlocks)
            {
                builder.AddCustomBlock(customBlock);
            }

            // Copy dynamic items
            foreach (var itemInfo in source.ItemInfo)
            {
                builder.AddDynamicItem(itemInfo);
            }

            // Create file
            builder.CreateFile(filename);

            // Copy static items
            IDfsStaticItem sourceStaticItem;

            while (null != (sourceStaticItem = source.ReadStaticItemNext()))
            {
                builder.AddStaticItem(sourceStaticItem);
            }

            // Get the file
            DfsFile file = builder.GetFile();

            // Copy dynamic item data
            IDfsItemData sourceData;

            while (null != (sourceData = source.ReadItemTimeStepNext()))
            {
                file.WriteItemTimeStepNext(sourceData.Time, sourceData.Data);
            }

            source.Close();
            file.Close();
        }
Пример #5
0
        /// <summary>
        /// Example of how to merge two or more dfs files. The merger is on dynamic item basis,
        /// i.e. add all dynamic items of a number of dfs files to a new dfs file.
        /// <para>
        /// It is assumed that all files has the same time stepping layout. It will merge
        /// as many time steps as the file with the least number of timesteps.
        /// </para>
        /// <para>
        /// If merging one of the specific types of dfs files, dfs0 or dfs1 or dfs2 or dfs3,
        /// the structure of the files must be identical, i.e. the sizes of the axis must equal.
        /// Otherwise, the outcome will not be a valid dfs0/1/2/3 file.
        /// </para>
        /// </summary>
        /// <param name="targetFilename">Path and name of the new file to create</param>
        /// <param name="sourcesFilenames">Path and name of the source dfs files</param>
        public static void MergeDfsFileItems(string targetFilename, IList <string> sourcesFilenames)
        {
            // List of sources to be merged - in case of more than one, just extend this.
            List <IDfsFile> sources = new List <IDfsFile>();

            for (int i = 0; i < sourcesFilenames.Count; i++)
            {
                sources.Add(DfsFileFactory.DfsGenericOpen(sourcesFilenames[i]));
            }

            // Use the first file as skeleton for header and static items.
            IDfsFile     source   = sources[0];
            IDfsFileInfo fileInfo = source.FileInfo;

            DfsBuilder builder = DfsBuilder.Create(fileInfo.FileTitle, fileInfo.ApplicationTitle, fileInfo.ApplicationVersion);

            // Set up the header
            builder.SetDataType(fileInfo.DataType);
            builder.SetGeographicalProjection(fileInfo.Projection);
            builder.SetTemporalAxis(fileInfo.TimeAxis);
            builder.SetItemStatisticsType(fileInfo.StatsType);
            builder.DeleteValueByte        = fileInfo.DeleteValueByte;
            builder.DeleteValueDouble      = fileInfo.DeleteValueDouble;
            builder.DeleteValueFloat       = fileInfo.DeleteValueFloat;
            builder.DeleteValueInt         = fileInfo.DeleteValueInt;
            builder.DeleteValueUnsignedInt = fileInfo.DeleteValueUnsignedInt;

            // Transfer compression keys - if any.
            if (fileInfo.IsFileCompressed)
            {
                int[] xkey;
                int[] ykey;
                int[] zkey;
                fileInfo.GetEncodeKey(out xkey, out ykey, out zkey);
                builder.SetEncodingKey(xkey, ykey, zkey);
            }

            // Copy custom blocks - if any
            foreach (IDfsCustomBlock customBlock in fileInfo.CustomBlocks)
            {
                builder.AddCustomBlock(customBlock);
            }

            int minNumTimesteps = int.MaxValue;

            // Copy dynamic items for all source files
            for (int j = 0; j < sources.Count; j++)
            {
                if (sources[j].FileInfo.TimeAxis.NumberOfTimeSteps < minNumTimesteps)
                {
                    minNumTimesteps = sources[j].FileInfo.TimeAxis.NumberOfTimeSteps;
                }

                foreach (var itemInfo in sources[j].ItemInfo)
                {
                    builder.AddDynamicItem(itemInfo);
                }
            }

            // Create file
            builder.CreateFile(targetFilename);

            // Copy static items - add only from main file
            IDfsStaticItem sourceStaticItem;

            while (null != (sourceStaticItem = source.ReadStaticItemNext()))
            {
                builder.AddStaticItem(sourceStaticItem);
            }

            // Get the file
            DfsFile file = builder.GetFile();

            // Copy dynamic item data
            IDfsItemData sourceData;

            for (int i = 0; i < minNumTimesteps; i++)
            {
                for (int j = 0; j < sources.Count; j++)
                {
                    IDfsFile sourcej = sources[j];
                    // Copy all items for this source
                    for (int k = 0; k < sourcej.ItemInfo.Count; k++)
                    {
                        sourceData = sourcej.ReadItemTimeStepNext();
                        file.WriteItemTimeStepNext(sourceData.Time, sourceData.Data);
                    }
                }
            }

            foreach (IDfsFile sourcej in sources)
            {
                sourcej.Close();
            }
            file.Close();
        }