Beispiel #1
0
        /// <summary>
        /// Example of how to create a M21 Dfs2 Bathymetry from scratch. This method
        /// creates a file matching the OresundBathy900.dfs2 test file.
        /// </summary>
        /// <param name="bathyDataArray">Array of bathymetry data, 1D array with 2D data, size n x m</param>
        /// <param name="filename">Path and name of the new file to create</param>
        public static void CreateM21Bathymetry(float[] bathyDataArray, string filename)
        {
            DfsFactory  factory = new DfsFactory();
            Dfs2Builder builder = Dfs2Builder.Create(@"C:\0\Training\Bat1_0.dfs2", @"Grid editor", 1);

            // Set up the header
            builder.SetDataType(0);
            builder.SetGeographicalProjection(factory.CreateProjectionGeoOrigin("UTM-33", 12.438741600559911, 55.2257078424238, 327));
            builder.SetTemporalAxis(factory.CreateTemporalEqCalendarAxis(eumUnit.eumUsec, new DateTime(2003, 01, 01, 0, 0, 0), 0, 1));
            builder.SetSpatialAxis(factory.CreateAxisEqD2(eumUnit.eumUmeter, 72, 0, 900, 94, 0, 900));
            builder.DeleteValueFloat = -1e-30f;

            builder.AddCustomBlock(factory.CreateCustomBlock("Display Settings", new int[] { 1, 0, 0 }));
            builder.AddCustomBlock(factory.CreateCustomBlock("M21_Misc", new float[] { 327f, 0f, -900f, 10f, 0f, 0f, 0f }));

            // Set up dynamic items
            builder.AddDynamicItem("Bathymetry", eumQuantity.Create(eumItem.eumIWaterLevel, eumUnit.eumUmeter),
                                   DfsSimpleType.Float, DataValueType.Instantaneous);

            // Create and get file
            builder.CreateFile(filename);
            Dfs2File file = builder.GetFile();

            // Add bathymetry data
            file.WriteItemTimeStepNext(0, bathyDataArray);

            file.Close();
        }
Beispiel #2
0
        /// <summary>
        /// Example of how to create a Dfs2 file from scratch. This method
        /// creates a copy of the OresundHD.dfs2 test file.
        /// <para>
        /// Data for static and dynamic item is taken from a source dfs file,
        /// which here is the OresundHD.dfs2 test file. The data could come
        /// from any other source.
        /// </para>
        /// </summary>
        /// <param name="sourceFilename">Path and name of the OresundHD.dfs2 test file</param>
        /// <param name="filename">Path and name of the new file to create</param>
        public static void CreateDfs2File(string sourceFilename, string filename)
        {
            IDfs2File source = DfsFileFactory.Dfs2FileOpen(sourceFilename);

            DfsFactory  factory = new DfsFactory();
            Dfs2Builder builder = Dfs2Builder.Create("", @"C:\Program Files\DHI\2010\bin\nmodel.exe", 0);

            // Set up the header
            builder.SetDataType(1);
            builder.SetGeographicalProjection(factory.CreateProjectionGeoOrigin("UTM-33", 12.438741600559766, 55.225707842436385, 326.99999999999955));
            builder.SetTemporalAxis(factory.CreateTemporalEqCalendarAxis(eumUnit.eumUsec, new DateTime(1993, 12, 02, 0, 0, 0), 0, 86400));
            builder.SetSpatialAxis(factory.CreateAxisEqD2(eumUnit.eumUmeter, 71, 0, 900, 91, 0, 900));
            builder.DeleteValueFloat = -1e-30f;

            // Add custom block
            // M21_Misc : {orientation (should match projection), drying depth, -900=has projection, land value, 0, 0, 0}
            builder.AddCustomBlock(factory.CreateCustomBlock("M21_Misc", new float[] { 327f, 0.2f, -900f, 10f, 0f, 0f, 0f }));

            // Set up dynamic items
            builder.AddDynamicItem("H Water Depth m", eumQuantity.Create(eumItem.eumIWaterLevel, eumUnit.eumUmeter), DfsSimpleType.Float, DataValueType.Instantaneous);
            builder.AddDynamicItem("P Flux m^3/s/m", eumQuantity.Create(eumItem.eumIFlowFlux, eumUnit.eumUm3PerSecPerM), DfsSimpleType.Float, DataValueType.Instantaneous);
            builder.AddDynamicItem("Q Flux m^3/s/m", eumQuantity.Create(eumItem.eumIFlowFlux, eumUnit.eumUm3PerSecPerM), DfsSimpleType.Float, DataValueType.Instantaneous);

            // Create file
            builder.CreateFile(filename);

            // Add static items containing bathymetri data, use data from source
            IDfsStaticItem sourceStaticItem = source.ReadStaticItemNext();

            builder.AddStaticItem("Static item", eumQuantity.UnDefined, sourceStaticItem.Data);

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

            // Loop over all time steps
            for (int i = 0; i < source.FileInfo.TimeAxis.NumberOfTimeSteps; i++)
            {
                // Loop over all items
                for (int j = 0; j < source.ItemInfo.Count; j++)
                {
                    // Add data for all item-timesteps, copying data from source file.

                    // Read data from source file
                    IDfsItemData2D <float> sourceData = (IDfsItemData2D <float>)source.ReadItemTimeStepNext();

                    // Create empty item data, and copy over data from source
                    // The IDfsItemData2D can handle 2D indexing, on the form data2D[k,l].
                    // An ordinary array, float[], can also be used, though indexing from 2D to 1D must be
                    // handled by user code i.e. using data1D[k + l*xCount] compared to data2D[k,l]
                    IDfsItemData2D <float> itemData2D = (IDfsItemData2D <float>)file.CreateEmptyItemData(j + 1);
                    for (int k = 0; k < 71; k++)
                    {
                        for (int l = 0; l < 91; l++)
                        {
                            itemData2D[k, l] = sourceData[k, l];
                        }
                    }
                    // the itemData2D.Data is a float[], so any float[] of the correct size is valid here.
                    file.WriteItemTimeStep(j + 1, i, sourceData.Time, itemData2D.Data);
                }
            }

            source.Close();
            file.Close();
        }
        /// <summary> Write dfs2 file </summary>
        private void Write(string dfsFilename, string projectionString)
        {
            DfsFactory  factory = new DfsFactory();
            Dfs2Builder builder = new Dfs2Builder();

            // Data type
            builder.SetDataType(0);

            // Projection and spatial axis
            double lon0; double lat0; double dx; double dy;
            int    flip;

            FindAxisProperties(out lon0, out lat0, out dx, out dy, out flip);
            builder.SetGeographicalProjection(factory.CreateProjectionGeoOrigin(projectionString, lon0, lat0, 0));
            builder.SetSpatialAxis(factory.CreateAxisEqD2(eumUnit.eumUdegree, _dimensions[0], 0, dx, _dimensions[1], 0, dy));

            // Time axis
            eumUnit  timeUnit;
            DateTime startDateTime;
            double   timeStep;

            FindTimeProperties(out timeUnit, out startDateTime, out timeStep);
            builder.SetTemporalAxis(factory.CreateTemporalEqCalendarAxis(timeUnit, startDateTime, 0, timeStep));

            // Add dynamic items
            foreach (Variable item in _items)
            {
                eumQuantity quantity = GetQuantityFromItem(item);
                builder.AddDynamicItem(item.Name, quantity, DfsSimpleType.Float, DataValueType.Instantaneous);
            }

            // Create and get file (no static items there)
            builder.CreateFile(dfsFilename);
            Dfs2File dfs2File = builder.GetFile();

            // Write data to file
            int itemSize = _dimensions[0] * _dimensions[1];

            float[] values        = new float[itemSize];
            float[] valuesFlipped = new float[itemSize];
            for (int i = 0; i < _time.Array.Length; i++)
            {
                for (int j = 0; j < _items.Count; j++)
                {
                    // Time of time step
                    double time = (double)_time.Array.GetValue(i);
                    // Values for all time steps
                    float[] allfloats = (float[])_items[j].Array;
                    // Copy single time step data from allFloats to values
                    Array.Copy(allfloats, i * itemSize, values, 0, itemSize);
                    // Flip values, if necessary
                    float[] actual;
                    if (flip == 0)
                    {
                        actual = values;
                    }
                    else
                    {
                        PerformFlip(flip, values, valuesFlipped);
                        actual = valuesFlipped;
                    }
                    // Save values to file
                    dfs2File.WriteItemTimeStepNext(time, actual);
                }
            }
            dfs2File.Close();
        }