private void CreateFile() { WriteGeoInfo(); WriteTime(); foreach (Item I in Items) { WriteItemInfo(I); if (_spaceAxis == SpaceAxisType.EqD2) { DfsDLLWrapper.dfsSetItemAxisEqD2(I.ItemPointer, 1000, _numberOfColumns, _numberOfRows, 0, 0, (float)_gridSize, (float)_gridSize); } else if (_spaceAxis == SpaceAxisType.EqD3) { DfsDLLWrapper.dfsSetItemAxisEqD3(I.ItemPointer, 1000, _numberOfColumns, _numberOfRows, _numberOfLayers, 0, 0, 0, (float)_gridSize, (float)_gridSize, (float)_gridSize); } else if (_spaceAxis == SpaceAxisType.EqD0) { DfsDLLWrapper.dfsSetItemAxisEqD0(I.ItemPointer, 1000); } } DfsDLLWrapper.dfsFileCreate(FileName, _headerPointer, out _filePointer); }
private void _convert2Dfs2() { IntPtr headerPointer = new IntPtr(); IntPtr filePointer = new IntPtr(); try { int maxTimeStep = _getTimeSteps(); _customDFSGrid = false; // Create header System.Reflection.AssemblyName assName = this.GetType().Assembly.GetName(); //if (maxTimeStep <= 1) headerPointer = DfsDLLWrapper.dfsHeaderCreate(FileType.EqtimeFixedspaceAllitems, System.IO.Path.GetFileNameWithoutExtension(_settings.InputFileName), assName.Name, assName.Version.Major, _getItemNum(), StatType.NoStat); /*else * headerPointer = DfsDLLWrapper.dfsHeaderCreate(FileType.NeqtimeFixedspaceAllitems, * System.IO.Path.GetFileNameWithoutExtension(_settings.InputFileName), assName.Name, * assName.Version.Major, _getItemNum(), StatType.NoStat);*/ // Setup header DfsDLLWrapper.dfsSetDataType(headerPointer, 1); double x0 = 0, y0 = 0, dx = 0, dy = 0, j = 0, k = 0, lon0 = 0, lat0 = 0; _getGridOrigo(out x0, out y0, out dx, out dy, out j, out k, out lat0, out lon0); DfsDLLWrapper.dfsSetGeoInfoUTMProj(headerPointer, _settings.MZMapProjectionString, lon0, lat0, _settings.OverwriteRotation); DfsDLLWrapper.dfsSetDeleteValFloat(headerPointer, _fdel); List <DateTime> dateTimes = _util.GetTime(_settings.TimeAxisName); //compute timesteps double timestepSec = 0; for (int timeSteps = 1; timeSteps < dateTimes.Count; timeSteps++) { timestepSec = Math.Round((dateTimes[timeSteps].ToOADate() - dateTimes[timeSteps - 1].ToOADate()) * 86400); } //DfsDLLWrapper.dfsSetEqCalendarAxis(headerPointer, dateTimes[0].ToString("yyyy-MM-dd"), dateTimes[0].ToString("HH:mm:ss"), (int)eumUnit.eumUsec, 0, (int)timestepSec, 0); if (maxTimeStep <= 1) { DfsDLLWrapper.dfsSetEqCalendarAxis(headerPointer, dateTimes[0].ToString("yyyy-MM-dd"), dateTimes[0].ToString("HH:mm:ss"), (int)eumUnit.eumUsec, 0, _settings.TimeStepSeconds, 0); } else { DfsDLLWrapper.dfsSetEqCalendarAxis(headerPointer, dateTimes[0].ToString("yyyy-MM-dd"), dateTimes[0].ToString("HH:mm:ss"), (int)eumUnit.eumUsec, 0, (int)timestepSec, 0); } // Add Items by looping through selected variables int selectedItemCount = 0; for (int itemCount = 0; itemCount < _settings.Variables.Count; itemCount++) { if (_settings.IsVariablesSelected[itemCount]) { IntPtr itemPointer = DfsDLLWrapper.dfsItemD(headerPointer, selectedItemCount + 1); string itemName = _settings.Variables[itemCount]; DfsDLLWrapper.dfsSetItemInfo(headerPointer, itemPointer, _settings.VariablesMappings[itemCount].EUMItemKey, _settings.VariablesMappings[itemCount].EUMItemDesc, _settings.VariablesMappings[itemCount].EUMMappedItemUnitKey, DfsSimpleType.Float); DfsDLLWrapper.dfsSetItemValueType(itemPointer, DataValueType.Instantaneous); //get grid data from nc dimensions //swap range if dx or dy is negative if (dx <= 0) { dx = Math.Abs(dx); _invertxData = true; } if (dy <= 0) { dy = Math.Abs(dy); _invertyData = true; } DfsDLLWrapper.dfsSetItemAxisEqD2(itemPointer, (int)eumUnit.eumUdegree, (int)j, (int)k, (float)x0, (float)y0, (float)dx, (float)dy); selectedItemCount++; } } // Create file DfsDLLWrapper.dfsFileCreate(_settings.OutputFileName, headerPointer, out filePointer); //write data to file (time loop > item loop) for (int timeSteps = 0; timeSteps < dateTimes.Count; timeSteps++) { selectedItemCount = 0; for (int itemCount = 0; itemCount < _settings.Variables.Count; itemCount++) { if (_settings.IsVariablesSelected[itemCount]) { string itemName = _settings.Variables[itemCount]; double dTotalSeconds = (dateTimes[timeSteps].ToOADate() - dateTimes[0].ToOADate()) * 86400; dTotalSeconds = Math.Round(dTotalSeconds, 0, MidpointRounding.AwayFromZero); DfsDLLWrapper.dfsWriteItemTimeStep(headerPointer, filePointer, dTotalSeconds, _getFloatData(itemName, timeSteps, j, k, lat0, lon0, dx, dy)); selectedItemCount++; } } } } catch (Exception ex) { throw new Exception("Convert2Dfs2 Error: " + ex.Message); } finally { // close file and destroy header if (null != filePointer) { DfsDLLWrapper.dfsFileClose(headerPointer, ref filePointer); } if (null != headerPointer) { DfsDLLWrapper.dfsHeaderDestroy(ref headerPointer); } } }
public int WriteToFile(string dfsFileName) { //create header FileType d = dfsFileType; pHeader = DfsDLLWrapper.dfsHeaderCreate(d, FileTitle, "DfsFileInfo", 1, Items.Length, statType); DfsDLLWrapper.dfsSetDataType(pHeader, DataType); //delval DfsDLLWrapper.dfsSetDeleteValFloat(pHeader, delVal); int rc = 0; switch (CustomBlockName) { case "MIKE_FM": int[] dfsuCBData = new int[4]; dfsuCBData[0] = dfsuCustBlock.NoNodesTot; dfsuCBData[1] = dfsuCustBlock.NoElemTot; dfsuCBData[2] = dfsuCustBlock.Dim; dfsuCBData[3] = dfsuCustBlock.NoLayers; DfsDLLWrapper.dfsAddCustomBlock(pHeader, "MIKE_FM", dfsuCBData); break; case "M21_Misc": float[] dM21CBData = new float[7]; dM21CBData[0] = (float)Orientation; dM21CBData[1] = m21CustBlock.f1; dM21CBData[2] = m21CustBlock.f2; dM21CBData[3] = m21CustBlock.f3; dM21CBData[4] = m21CustBlock.f4; dM21CBData[5] = m21CustBlock.f5; dM21CBData[6] = m21CustBlock.f6; DfsDLLWrapper.dfsAddCustomBlock(pHeader, "M21_Misc", dM21CBData); break; case "Unknown": break; default: //JdfsMisc.log("Warning: unsupported CustomBlockName encountered (" + CustomBlockName + "). Custom block not written."); break; } //projection if (Projection_type == ProjectionType.Projection) { DfsDLLWrapper.dfsSetGeoInfoUTMProj(pHeader, Projection, Longitude, Latitude, Orientation); } //timeaxis switch (this.tAxisType) { case TimeAxisType.CalendarEquidistant: DfsDLLWrapper.dfsSetEqCalendarAxis(pHeader, this.tAxis_StartDateStr, this.tAxis_StartTimeStr, (int)tAxis_EUMUnit, tAxis_dTStart, this.tAxis_dTStep, this.tAxis_indexTStart); break; case TimeAxisType.CalendarNonEquidistant: DfsDLLWrapper.dfsSetNeqCalendarAxis(pHeader, this.tAxis_StartDateStr, this.tAxis_StartTimeStr, (int)tAxis_EUMUnit, this.tAxis_dTStart, this.tAxis_indexTStart); break; default: _err("write of " + tAxisType.ToString() + " not supported"); break; } if (compressed) { if ((compress_XKey.Length < 1) || (compress_XKey.Length != compress_YKey.Length || compress_XKey.Length != compress_ZKey.Length)) { _err("Compress keys does not have same length or is empty. Compression disabled."); compressed = false; } else { DfsDLLWrapper.dfsItemEnableCompression(pHeader); DfsDLLWrapper.dfsSetEncodeKey(pHeader, compress_XKey, compress_YKey, compress_ZKey, compress_XKey.Length); } } //Dynamic Items for (int i = 1; i < Items.Length + 1; i++) { Items[i - 1].fileInfoRef = this; rc = Items[i - 1].Write(i); if (rc != 0) { return(rc); } } //Static Items if (staticItems != null) { for (int i = 1; i < staticItems.Length + 1; i++) { staticItems[i - 1].fileInfoRef = this; rc = staticItems[i - 1].WriteStatic(i); if (rc != 0) { return(rc); } } } pFile = (IntPtr)0; DfsDLLWrapper.dfsFileCreate(dfsFileName, pHeader, out pFile); //write static data if (staticItems != null && staticItems.Length > 0 && writeStaticDataOnWrite) { rc = WriteStaticData(); if (rc != 0) { return(rc); } } m_fileName = dfsFileName; return(rc); }