Ejemplo n.º 1
0
        /// <summary>
        /// Opens an existing dfs-file
        /// </summary>
        /// <param name="DFSFileName"></param>
        public DFSBase(string DFSFileName)
            : this()
        {
            _filename        = DFSFileName;
            AbsoluteFileName = Path.GetFullPath(DFSFileName);

            try
            {
                DfsDLLWrapper.dfsFileRead(AbsoluteFileName, out _headerPointer, out _filePointer);
            }
            catch (Exception e)
            {
                return; //Not a valid file.
            }
            NumberOfItems = DfsDLLWrapper.dfsGetNoOfItems(_headerPointer);


            string eum_unit  = "";
            int    unit      = 0;
            int    data_type = 0;
            int    item_type = 0;

            float x = 0;
            float y = 0;
            float z = 0;

            float dx = 0;
            float dy = 0;
            float dz = 0;

            IntPtr name = new IntPtr();


            //Reads the projection
            LastStatus = dfsGetGeoInfoUTMProj(_headerPointer, ref name, ref _xOrigin, ref _yOrigin, ref _orientation);

            //Reads the space axis
            _spaceAxis = (SpaceAxisType)DfsDLLWrapper.dfsGetItemAxisType(FirstItem.ItemPointer);

            //Now read axes info dependent on the type of axis
            switch (_spaceAxis)
            {
            case SpaceAxisType.CurveLinearD2:
                break;

            case SpaceAxisType.CurveLinearD3:
                break;

            case SpaceAxisType.EqD0:
                break;

            case SpaceAxisType.EqD1:
                break;

            case SpaceAxisType.EqD2:   //DFS2 from MikeShe
                DfsDLLWrapper.dfsGetItemAxisEqD2(FirstItem.ItemPointer, out item_type, out eum_unit, out _numberOfColumns, out _numberOfRows, out x, out y, out dx, out dy);
                break;

            case SpaceAxisType.EqD3: //DFS3 from MikeShe
                DfsDLLWrapper.dfsGetItemAxisEqD3(FirstItem.ItemPointer, out item_type, out eum_unit, out _numberOfColumns, out _numberOfRows, out _numberOfLayers, out x, out y, out z, out dx, out dy, out dz);
                break;

            case SpaceAxisType.NeqD1:
                var coords = new Coords[1];
                DfsDLLWrapper.dfsGetItemAxisNeqD1(FirstItem.ItemPointer, out unit, out eum_unit, out data_type, out coords);
                break;

            case SpaceAxisType.NeqD2:
                break;

            case SpaceAxisType.NeqD3:
                break;

            case SpaceAxisType.Undefined:
                break;

            default:
                break;
            }

            _gridSize = dx;

            //Prepares an array of floats to recieve the data
            dfsdata = new float[_numberOfColumns * _numberOfRows * _numberOfLayers];

            //Now look at time axis
            _timeAxis = (TimeAxisType)DfsDLLWrapper.dfsGetTimeAxisType(_headerPointer);
            string startdate = "";
            string starttime = "";
            double tstart    = 0;
            double tstep     = 0;
            int    nt        = 0;
            int    tindex    = 0;

            switch (_timeAxis)
            {
            case TimeAxisType.TimeEquidistant: //Some DFS2 here
                DfsDLLWrapper.dfsGetEqTimeAxis(_headerPointer, out unit, out eum_unit, out tstart, out tstep, out nt, out tindex);
                break;

            case TimeAxisType.CalendarEquidistant: //Dfs2 and dfs3 here
                DfsDLLWrapper.dfsGetEqCalendarAxis(_headerPointer, out startdate, out starttime, out unit, out eum_unit, out tstart, out tstep, out nt, out tindex);
                if (unit == 1400)
                {
                    _timeStep = TimeSpan.FromSeconds(tstep);
                }
                else if (unit == 1401) //This is a guess
                {
                    _timeStep = TimeSpan.FromMinutes(tstep);
                }
                else if (unit == 1402)
                {
                    _timeStep = TimeSpan.FromHours(tstep);
                }
                break;

            case TimeAxisType.TimeNonEquidistant: //This has not been tested
                DfsDLLWrapper.dfsGetNeqTimeAxis(_headerPointer, out unit, out eum_unit, out tstart, out tstep, out nt, out tindex);
                break;

            case TimeAxisType.CalendarNonEquidistant://Only dfs0 can have varying time steps
                DfsDLLWrapper.dfsGetNeqCalendarAxis(_headerPointer, out startdate, out starttime, out unit, out eum_unit, out tstart, out tstep, out nt, out tindex);
                break;

            case TimeAxisType.Undefined:
                break;

            default:
                break;
            }

            NumberOfTimeStepsWritten = nt;
            timeStepUnit             = (TimeInterval)unit;

            if (_timeAxis == TimeAxisType.CalendarNonEquidistant | _timeAxis == TimeAxisType.TimeEquidistant)
            {
                _times = new double[nt];
            }

            if (startdate != "" & starttime != "")
            {
                _timesteps.Add(DateTime.Parse(startdate).Add(TimeSpan.Parse(starttime)));
            }
            else //Time equidistant files enter here.
            {
                _timesteps.Add(new DateTime(2002, 1, 1));
            }

            //Now build the list of timesteps
            for (int i = 1; i < nt; i++)
            {
                if (_timeAxis == TimeAxisType.CalendarNonEquidistant) //dfs0 with time varying.
                {
                    _timesteps.Add(_timesteps[0].Add(GetTimeSpan(i)));
                }
                else
                {
                    _timesteps.Add(_timesteps[i - 1].Add(_timeStep));
                }
            }
        }
Ejemplo n.º 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);
        }