public int Read(IntPtr pItem) { int eumT = 0, eumU = 0; DfsSimpleType dataT = DfsSimpleType.Int; DfsDLLWrapper.dfsGetItemInfo(pItem, out eumT, out EUMTypeString, out Name, out eumU, out EUMUnitString, out dataT); EUMType = (eumItem)eumT; EUMUnit = (eumUnit)eumU; dataType = (DfsSimpleType)dataT; //if (dataType != UfsSimpleType.UFS_FLOAT)return err("Only float dataType supported."); dim = DfsDLLWrapper.dfsGetItemDim(pItem); dataValType = DfsDLLWrapper.dfsGetItemValueType(pItem); sAxisType = (SpaceAxisType)DfsDLLWrapper.dfsGetItemAxisType(pItem); switch (sAxisType) { case SpaceAxisType.EqD0: DfsDLLWrapper.dfsGetItemAxisEqD0(pItem, out eumU, out axisEUMUnitString); nPointsX = 1; break; case SpaceAxisType.EqD1: DfsDLLWrapper.dfsGetItemAxisEqD1(pItem, out eumU, out axisEUMUnitString, out m_nPointsX, out XMinLimit, out DX); break; case SpaceAxisType.EqD2: DfsDLLWrapper.dfsGetItemAxisEqD2(pItem, out eumU, out axisEUMUnitString, out m_nPointsX, out m_nPointsY, out XMinLimit, out YMinLimit, out DX, out DY); break; case SpaceAxisType.EqD3: DfsDLLWrapper.dfsGetItemAxisEqD3(pItem, out eumU, out axisEUMUnitString, out m_nPointsX, out m_nPointsY, out m_nPointsZ, out XMinLimit, out YMinLimit, out ZMinLimit, out DX, out DY, out DZ); break; default: return(_err("Unsupported space axis " + sAxisType.ToString())); } axisEUMUnit = (eumUnit)eumU; return(0); }
/// <summary> /// Introductory example of how to load a dfs0 file. /// <para> /// The method assumes that the Rain_stepaccumulated.dfs0 test file /// is the input file. /// </para> /// </summary> /// <param name="filename">path and name of Rain_stepaccumulated.dfs0 test file</param> public static double ReadDfs0File(string filename) { // Open the file as a generic dfs file IDfsFile dfs0File = DfsFileFactory.DfsGenericOpen(filename); // Header information is contained in the IDfsFileInfo IDfsFileInfo fileInfo = dfs0File.FileInfo; int steps = fileInfo.TimeAxis.NumberOfTimeSteps; // 19 // Information on each of the dynamic items, here the first one IDfsSimpleDynamicItemInfo dynamicItemInfo = dfs0File.ItemInfo[0]; string nameOfFirstDynamicItem = dynamicItemInfo.Name; // "Rain" DfsSimpleType typeOfFirstDynamicItem = dynamicItemInfo.DataType; // Double ValueType valueType = dynamicItemInfo.ValueType; // StepAccumulated // Read data of first item, third time step (items start by 1, timesteps by 0), IDfsItemData datag = dfs0File.ReadItemTimeStep(1, 2); double value1 = System.Convert.ToDouble(datag.Data.GetValue(0)); // 0.36 // Assuming this is a double value, the item data object can be converted to the correct type IDfsItemData <double> data = (IDfsItemData <double>)datag; double value2 = data.Data[0]; // 0.36 // This iterates through all timesteps and items in the file // For performance reasons it is important to iterate over time steps // first and items second. double sum = 0; for (int i = 0; i < steps; i++) { for (int j = 1; j <= dfs0File.ItemInfo.Count; j++) { data = (IDfsItemData <double>)dfs0File.ReadItemTimeStep(j, i); double value = data.Data[0]; sum += value; } } dfs0File.Close(); return(sum); }
/// <summary> /// Introductory example of how to load a dfs2 file. /// <para> /// The method assumes that the OresundHD.dfs2 test file /// is the input file. /// </para> /// </summary> /// <param name="filename">path and name of OresundHD.dfs2 test file</param> public static void ReadingDfs2File(string filename) { // Open the file as a dfs2 file Dfs2File dfs2File = DfsFileFactory.Dfs2FileOpen(filename); // Spatial axis for this file is a 2D equidistant axis IDfsAxisEqD2 axisEqD2 = ((IDfsAxisEqD2)dfs2File.SpatialAxis); double dx = axisEqD2.Dx; // 900 double dy = axisEqD2.Dy; // 900 // Header information is contained in the IDfsFileInfo IDfsFileInfo fileInfo = dfs2File.FileInfo; int steps = fileInfo.TimeAxis.NumberOfTimeSteps; // 13 string projectionString = fileInfo.Projection.WKTString; // "UTM-33" // Information on each of the dynamic items, here the first one IDfsSimpleDynamicItemInfo dynamicItemInfo = dfs2File.ItemInfo[0]; string nameOfFirstDynamicItem = dynamicItemInfo.Name; // "H Water Depth m" DfsSimpleType typeOfFirstDynamicItem = dynamicItemInfo.DataType; // Float // Read data of first item, third time step (items start by 1, timesteps by 0), // assuming data is of type float. IDfsItemData2D <float> data2D = (IDfsItemData2D <float>)dfs2File.ReadItemTimeStep(1, 2); // Get the value at (i,j) = (3,4) of the item and timestep float value = data2D[3, 4]; // 11.3634329 // This iterates through all the timesteps and items in the file // For performance reasons it is important to iterate over time steps // first and items second. for (int i = 0; i < steps; i++) { for (int j = 1; j <= dfs2File.ItemInfo.Count; j++) { data2D = (IDfsItemData2D <float>)dfs2File.ReadItemTimeStep(j, i); value = data2D[3, 4]; } } }
/// <summary> /// Introductory example of how to load a dfs0 file with a non-time axis /// as the primary axis. The important part here is to NOT call /// the <code>data.TimeInSeconds()</code>, because that will fail. /// </summary> /// <param name="filename">path and name of Added_Mass.dfs0 test file</param> public static double ReadNonTimeAxisDfs0(string filename) { // Open the file as a generic dfs file IDfsFile dfs0File = DfsFileFactory.DfsGenericOpen(filename); // Header information is contained in the IDfsFileInfo IDfsFileInfo fileInfo = dfs0File.FileInfo; // The TimeAxis is not a time axis, but a regular axis int steps = fileInfo.TimeAxis.NumberOfTimeSteps; // 256 TimeAxisType timeAxisType = fileInfo.TimeAxis.TimeAxisType; // TimeNonEquidistant eumUnit timeUnit = fileInfo.TimeAxis.TimeUnit; // radian-per-second // Information on each of the dynamic items, here the first one IDfsSimpleDynamicItemInfo dynamicItemInfo = dfs0File.ItemInfo[0]; string nameOfFirstDynamicItem = dynamicItemInfo.Name; // "DOF_1-1" DfsSimpleType typeOfFirstDynamicItem = dynamicItemInfo.DataType; // Float ValueType valueType = dynamicItemInfo.ValueType; // Instantaneous // This iterates through all timesteps and items in the file // For performance reasons it is important to iterate over time steps // first and items second. double sum = 0; for (int i = 0; i < steps; i++) { for (int j = 1; j <= dfs0File.ItemInfo.Count; j++) { var data = (IDfsItemData <float>)dfs0File.ReadItemTimeStep(j, i); // The Time axis value is not a time value but in radian-per-second. double axisValue = data.Time; float value = data.Data[0]; sum += value; } } dfs0File.Close(); return(sum); }
/// <summary> /// Introductory example of how to load a dfs1 file. /// <para> /// The method assumes that the wln.dfs1 test file /// is the input file. /// </para> /// </summary> /// <param name="filename">path and name of wln.dfs1 test file</param> public static void ReadingDfs1File(string filename) { // Open the file as a dfs1 file Dfs1File dfs1File = DfsFileFactory.Dfs1FileOpen(filename); // Spatial axis for this file is a 2D equidistant axis IDfsAxisEqD1 axisEqD1 = ((IDfsAxisEqD1)dfs1File.SpatialAxis); double dx = axisEqD1.Dx; // 900 // Header information is contained in the IDfsFileInfo IDfsFileInfo fileInfo = dfs1File.FileInfo; int steps = fileInfo.TimeAxis.NumberOfTimeSteps; // 577 // Information on each of the dynamic items, here the first one IDfsSimpleDynamicItemInfo dynamicItemInfo = dfs1File.ItemInfo[0]; string nameOfFirstDynamicItem = dynamicItemInfo.Name; // "WL-N (m)" DfsSimpleType typeOfFirstDynamicItem = dynamicItemInfo.DataType; // Float // Read data of first item, third time step (items start by 1, timesteps by 0), // assuming data is of type float. IDfsItemData<float> data = (IDfsItemData<float>)dfs1File.ReadItemTimeStep(1, 2); }
public int ReadDfsFile(string dfsFileName) { int rc; DfsDLLWrapper.dfsFileRead(dfsFileName, out pHeader, out pFile); m_fileName = dfsFileName; compressed = (DfsDLLWrapper.dfsIsFileCompressed(pHeader)); if (compressed) { encodeKeySize = DfsDLLWrapper.dfsGetEncodeKeySize(pHeader); if (encodeKeySize > 0) { compress_XKey = new int[encodeKeySize]; compress_YKey = new int[encodeKeySize]; compress_ZKey = new int[encodeKeySize]; DfsDLLWrapper.dfsGetEncodeKey(pHeader, compress_XKey, compress_YKey, compress_ZKey); } else { compressed = false; } } // general info about file dfsFileType = (FileType)DfsDLLWrapper.dfsGetFileType(pHeader); DataType = DfsDLLWrapper.dfsGetDataType(pHeader); //delete value delVal = DfsDLLWrapper.dfsGetDeleteValFloat(pHeader); //statisics type statType = DfsDLLWrapper.dfsGetItemStatsType(pHeader); //Custom blocks DfsSimpleType iDataType = DfsSimpleType.Float; int iMiscVarNos = 0; IntPtr pData = pHeader; IntPtr pNextBlock = pHeader; IntPtr pBlock = pHeader; pBlock = DfsDLLWrapper.dfsGetCustomBlockRef(pHeader); if (pBlock.ToInt32() != 0) { DfsDLLWrapper.dfsGetCustomBlock(pBlock, out iDataType, out CustomBlockName, out iMiscVarNos, ref pData, out pNextBlock); switch ((DfsSimpleType)iDataType) { case DfsSimpleType.Float: custBlockDataFloat = new float[iMiscVarNos]; Marshal.Copy(pData, custBlockDataFloat, 0, custBlockDataFloat.Length); // copy data from pointer to array break; case DfsSimpleType.Int: custBlockDataInt = new int[iMiscVarNos]; Marshal.Copy(pData, custBlockDataInt, 0, custBlockDataInt.Length); // copy data from pointer to array break; default: throw new Exception("Unsupported CustomBlock data tyoe encountered (" + iDataType + "."); } if (CustomBlockName == "MIKE_FM") { //dfsu if (custBlockDataInt.Length > 0) { dfsuCustBlock.NoNodesTot = custBlockDataInt[0]; } if (custBlockDataInt.Length > 1) { dfsuCustBlock.NoElemTot = custBlockDataInt[1]; } if (custBlockDataInt.Length > 2) { dfsuCustBlock.Dim = custBlockDataInt[2]; } if (custBlockDataInt.Length > 3) { dfsuCustBlock.NoLayers = custBlockDataInt[3]; } } else if (CustomBlockName == "M21_Misc") { if (custBlockDataFloat.Length > 0) { m21CustBlock.ori = custBlockDataFloat[0]; //m_LITOrientation } if (custBlockDataFloat.Length > 1) { m21CustBlock.f1 = custBlockDataFloat[1]; } if (custBlockDataFloat.Length > 2) { m21CustBlock.f2 = custBlockDataFloat[2]; } if (custBlockDataFloat.Length > 3) { m21CustBlock.f3 = custBlockDataFloat[3]; //m_LandValue } if (custBlockDataFloat.Length > 4) { m21CustBlock.f4 = custBlockDataFloat[4]; } if (custBlockDataFloat.Length > 5) { m21CustBlock.f5 = custBlockDataFloat[5]; } if (custBlockDataFloat.Length > 6) { m21CustBlock.f6 = custBlockDataFloat[6]; //m_GISLITOrientation } } } //time axis tAxisType = (TimeAxisType)DfsDLLWrapper.dfsGetTimeAxisType(pHeader); switch (tAxisType) { case TimeAxisType.CalendarEquidistant: DfsDLLWrapper.dfsGetEqCalendarAxis(pHeader, out tAxis_StartDateStr, out tAxis_StartTimeStr, out tAxis_EUMUnit, out tAxis_EUMUnitStr, out tAxis_dTStart, out tAxis_dTStep, out tAxis_nTSteps, out tAxis_indexTStart); break; case TimeAxisType.Undefined: DfsDLLWrapper.dfsGetEqCalendarAxis(pHeader, out tAxis_StartDateStr, out tAxis_StartTimeStr, out tAxis_EUMUnit, out tAxis_EUMUnitStr, out tAxis_dTStart, out tAxis_dTStep, out tAxis_nTSteps, out tAxis_indexTStart); break; case TimeAxisType.CalendarNonEquidistant: DfsDLLWrapper.dfsGetNeqCalendarAxis(pHeader, out tAxis_StartDateStr, out tAxis_StartTimeStr, out tAxis_EUMUnit, out tAxis_EUMUnitStr, out tAxis_dTStart, out tAxis_dTStep, out tAxis_nTSteps, out tAxis_indexTStart); break; case TimeAxisType.TimeEquidistant: DfsDLLWrapper.dfsGetEqTimeAxis(pHeader, out tAxis_EUMUnit, out tAxis_EUMUnitStr, out tAxis_dTStart, out tAxis_dTStep, out tAxis_nTSteps, out tAxis_indexTStart); break; case TimeAxisType.TimeNonEquidistant: DfsDLLWrapper.dfsGetNeqTimeAxis(pHeader, out tAxis_EUMUnit, out tAxis_EUMUnitStr, out tAxis_dTStart, out tAxis_dTStep, out tAxis_nTSteps, out tAxis_indexTStart); break; default: return(_err(tAxisType.ToString() + " not supported")); } //Projection Projection_type = (ProjectionType)DfsDLLWrapper.dfsGetGeoInfoType(pHeader); if (Projection_type == ProjectionType.Projection) { DfsDLLWrapper.dfsGetGeoInfoUTMProj(pHeader, out Projection, out Longitude, out Latitude, out Orientation); } //Dynamic Items int ItemCount = DfsDLLWrapper.dfsGetNoOfItems(pHeader); Items = new DfsItemInfo[ItemCount]; for (int i = 1; i < Items.Length + 1; i++) { Items[i - 1] = new DfsItemInfo(); Items[i - 1].fileInfoRef = this; Items[i - 1].Read(i); // reads header } //Static Items rc = 0; int sItemNo = 0; while (true) { sItemNo++; try { DfsDLLWrapper.dfsFindItemStatic(pHeader, pFile, sItemNo); } catch { break; }// no more static items } if (sItemNo > 0) { staticItems = new DfsItemInfo[sItemNo - 2]; for (int i = 0; i < staticItems.Length; i++) { staticItems[i] = new DfsItemInfo(); staticItems[i].fileInfoRef = this; rc = staticItems[i].ReadStatic(i + 1); // read header } if (readStaticDataOnRead) { rc = ReadStaticData(); } } return(rc); }