Esempio n. 1
0
        protected NetCdfVariable(NetCDFDataSet dataSet, int varid, string name, string[] dims, bool assignID) :
            base(dataSet, name, dims, assignID)
        {
            this.initializing = true;
            this.varid        = varid;

            int res;

            // SByte can map to NC_BYTE or NC_CHAR
            if (typeof(DataType) == typeof(byte))
            {
                res = NetCDF.nc_inq_vartype(dataSet.NcId, varid, out var nativeType);
                NetCDFDataSet.HandleResult(res);
                isNcChar = NcType.NC_CHAR == nativeType;
            }
            UpdateDimIds();

            // Reading attributes
            int nattrs;

            res = NetCDF.nc_inq_varnatts(dataSet.NcId, varid, out nattrs);
            AttributeTypeMap atm = new AttributeTypeMap(dataSet.NcId, varid);

            NetCDFDataSet.HandleResult(res);
            for (int i = 0; i < nattrs; i++)
            {
                // Name
                string aname;
                res = NetCDF.nc_inq_attname(dataSet.NcId, varid, i, out aname);
                NetCDFDataSet.HandleResult(res);

                // Skip out internal attribute
                if (aname == AttributeTypeMap.AttributeName ||
                    aname == AttributeTypeMap.AttributeVarSpecialType)
                {
                    continue;
                }
                if (aname == AttributeTypeMap.AttributeVarActualShape)
                {
                    isPresentedAttrActualShape = true;
                    continue;
                }

                // Type
                object value = dataSet.ReadNetCdfAttribute(varid, aname, atm);
                if (aname == Metadata.KeyForMissingValue && typeof(DateTime) == typeof(DataType))
                {
                    if (value is string)
                    {
                        value = DateTime.Parse((string)value);
                    }
                }
                Metadata[aname] = value;
            }

            Initialize();
            initializing = false;
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        protected override int[] ReadShape()
        {
            if (initializing)
            {
                return(null);
            }

            int    res;
            IntPtr len;

            int[] shape = new int[dimids.Length];

            for (int i = 0; i < dimids.Length; i++)
            {
                res = NetCDF.nc_inq_dimlen(NcId, dimids[i], out len);
                NetCDFDataSet.HandleResult(res);

                shape[i] = (int)len;
            }

            if (isPresentedAttrActualShape)
            {
                // Actual shape of the variable is in the special attribute
                // for the NetCDF doesn't support variables like A[0,10]
                NetCDFDataSet nc = ((NetCDFDataSet)DataSet);

                NcType type;
                res = NetCDF.nc_inq_atttype(nc.NcId, varid, AttributeTypeMap.AttributeVarActualShape, out type);
                if (res == (int)ResultCode.NC_NOERR)
                {
                    if (type != NcType.NC_INT)
                    {
                        throw new NetCDFException("Reserved attribute " + AttributeTypeMap.AttributeVarActualShape + " has wrong type");
                    }

                    AttributeTypeMap atm = new AttributeTypeMap(nc.NcId, varid);
                    atm[AttributeTypeMap.AttributeVarActualShape] = typeof(int[]);
                    shape = (int[])nc.ReadNetCdfAttribute(varid, AttributeTypeMap.AttributeVarActualShape, atm);
                    if (shape.Length != Rank)
                    {
                        throw new NetCDFException("Value of reserved attribute " + AttributeTypeMap.AttributeVarActualShape + " has wrong length");
                    }
                }
                else if (res != (int)ResultCode.NC_ENOTATT)
                {
                    NetCDFDataSet.HandleResult(res);
                }
            }

            return(shape);
        }