Beispiel #1
0
        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);
        }
Beispiel #2
0
        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);
        }