Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="origin"></param>
        /// <param name="shape"></param>
        /// <returns></returns>
        protected override Array ReadData(int[] origin, int[] shape)
        {
            if (origin == null)
            {
                origin = new int[Rank];
            }

            if (shape == null)
            {
                shape = ReadShape();
                for (int i = 0; i < shape.Length; i++)
                {
                    shape[i] -= origin[i];
                }
            }
            if (Rank == 0)
            {
                shape = new int[] { 1 }
            }
            ;                            // the place for data

            // Total number of elements in the array
            int n = 1;

            for (int i = 0; i < shape.Length; i++)
            {
                n *= shape[i];
            }

            int res;

            switch (Type.GetTypeCode(TypeOfData))
            {
            case TypeCode.Boolean:
                if (n == 0)
                {
                    return(Array.CreateInstance(typeof(bool), Rank == 0 ? new int[1] : new int[Rank]));
                }
                byte[] blData = new byte[n];
                res = NetCDF.nc_get_vara_ubyte(NcId, varid, ConvertToIntPtr(origin), ConvertToIntPtr(shape), blData);
                NetCDFDataSet.HandleResult(res);
                return(ToMultidimArrayBool(blData, shape));

            case TypeCode.DateTime:
                double[] dtData = new double[n];
                if (n == 0)
                {
                    return(Array.CreateInstance(typeof(DateTime), Rank == 0 ? new int[1] : new int[Rank]));
                }
                res = NetCDF.nc_get_vara_double(NcId, varid, ConvertToIntPtr(origin), ConvertToIntPtr(shape), dtData);
                NetCDFDataSet.HandleResult(res);
                return(ToMultidimArrayDateTime(dtData, shape));

            case TypeCode.Double:
                double[] data = new double[n];
                if (n == 0)
                {
                    return(Array.CreateInstance(typeof(double), Rank == 0 ? new int[1] : new int[Rank]));
                }
                res = NetCDF.nc_get_vara_double(NcId, varid, ConvertToIntPtr(origin), ConvertToIntPtr(shape), data);
                NetCDFDataSet.HandleResult(res);
                return(ToMultidimArray(data, shape));

            case TypeCode.Single:
                float[] fdata = new float[n];
                if (n == 0)
                {
                    return(Array.CreateInstance(typeof(float), Rank == 0 ? new int[1] : new int[Rank]));
                }
                res = NetCDF.nc_get_vara_float(NcId, varid, ConvertToIntPtr(origin), ConvertToIntPtr(shape), fdata);
                NetCDFDataSet.HandleResult(res);
                return(ToMultidimArray(fdata, shape));

            case TypeCode.Int64:
                long[] ldata = new long[n];
                if (n == 0)
                {
                    return(Array.CreateInstance(typeof(long), Rank == 0 ? new int[1] : new int[Rank]));
                }
                res = NetCDF.nc_get_vara_longlong(NcId, varid, ConvertToIntPtr(origin), ConvertToIntPtr(shape), ldata);
                NetCDFDataSet.HandleResult(res);
                return(ToMultidimArray(ldata, shape));

            case TypeCode.UInt64:
                ulong[] uldata = new ulong[n];
                if (n == 0)
                {
                    return(Array.CreateInstance(typeof(ulong), Rank == 0 ? new int[1] : new int[Rank]));
                }
                res = NetCDF.nc_get_vara_ulonglong(NcId, varid, ConvertToIntPtr(origin), ConvertToIntPtr(shape), uldata);
                NetCDFDataSet.HandleResult(res);
                return(ToMultidimArray(uldata, shape));


            case TypeCode.Int32:
                int[] idata = new int[n];
                if (n == 0)
                {
                    return(Array.CreateInstance(typeof(int), Rank == 0 ? new int[1] : new int[Rank]));
                }
                res = NetCDF.nc_get_vara_int(NcId, varid, ConvertToIntPtr(origin), ConvertToIntPtr(shape), idata);
                NetCDFDataSet.HandleResult(res);
                return(ToMultidimArray(idata, shape));

            case TypeCode.UInt32:
                uint[] uidata = new uint[n];
                if (n == 0)
                {
                    return(Array.CreateInstance(typeof(uint), Rank == 0 ? new int[1] : new int[Rank]));
                }
                res = NetCDF.nc_get_vara_uint(NcId, varid, ConvertToIntPtr(origin), ConvertToIntPtr(shape), uidata);
                NetCDFDataSet.HandleResult(res);
                return(ToMultidimArray(uidata, shape));

            case TypeCode.Int16:
                short[] sdata = new short[n];
                if (n == 0)
                {
                    return(Array.CreateInstance(typeof(short), Rank == 0 ? new int[1] : new int[Rank]));
                }
                res = NetCDF.nc_get_vara_short(NcId, varid, ConvertToIntPtr(origin), ConvertToIntPtr(shape), sdata);
                NetCDFDataSet.HandleResult(res);
                return(ToMultidimArray(sdata, shape));

            case TypeCode.UInt16:
                ushort[] usdata = new ushort[n];
                if (n == 0)
                {
                    return(Array.CreateInstance(typeof(ushort), Rank == 0 ? new int[1] : new int[Rank]));
                }
                res = NetCDF.nc_get_vara_ushort(NcId, varid, ConvertToIntPtr(origin), ConvertToIntPtr(shape), usdata);
                NetCDFDataSet.HandleResult(res);
                return(ToMultidimArray(usdata, shape));


            case TypeCode.Byte:
                byte[] bdata = new byte[n];
                if (n == 0)
                {
                    return(Array.CreateInstance(typeof(byte), Rank == 0 ? new int[1] : new int[Rank]));
                }
                if (isNcChar)
                {
                    res = NetCDF.nc_get_vara_text(NcId, varid, ConvertToIntPtr(origin), ConvertToIntPtr(shape), bdata);
                }
                else
                {
                    res = NetCDF.nc_get_vara_ubyte(NcId, varid, ConvertToIntPtr(origin), ConvertToIntPtr(shape), bdata);
                }
                NetCDFDataSet.HandleResult(res);
                return(ToMultidimArray(bdata, shape));

            case TypeCode.SByte:
                SByte[] cdata = new SByte[n];
                if (n == 0)
                {
                    return(Array.CreateInstance(typeof(SByte), Rank == 0 ? new int[1] : new int[Rank]));
                }
                res = NetCDF.nc_get_vara_schar(NcId, varid, ConvertToIntPtr(origin), ConvertToIntPtr(shape), cdata);

                /*if (res == -56)
                 *  res = NetCDF.nc_get_vara_text(NcId, varid, ConvertToIntPtr(origin), ConvertToIntPtr(shape), cdata);*/
                NetCDFDataSet.HandleResult(res);
                return(ToMultidimArray(cdata, shape));

            case TypeCode.String:
                if (n == 0)
                {
                    return(Array.CreateInstance(typeof(string), Rank == 0 ? new int[1] : new int[Rank]));
                }
                string[] strings = new string[n];
                res = NetCDF.nc_get_vara_string(NcId, varid, ConvertToIntPtr(origin), ConvertToIntPtr(shape), strings);
                NetCDFDataSet.HandleResult(res);
                return(ToMultidimArrayString(strings, shape));

            default:
                throw new NotSupportedException("Unsupported type of data.");
            }
        }