예제 #1
0
        /// <summary>
        /// Read a part of the variable values from file
        /// </summary>
        /// <param name="ncVariable"></param>
        /// <param name="origin"></param>
        /// <param name="shape">When shape is -1, this corresponds to reading that entire dimension</param>
        /// <returns></returns>
        public Array Read(NetCdfVariable ncVariable, int[] origin, int[] shape)
        {
            CreateShapeForFullRange(ncVariable, ref shape);
            var size      = NetCdfFileHelper.GetSize(shape);
            var originPtr = NetCdfFileHelper.ConvertToIntPtr(origin);
            var shapePtr  = NetCdfFileHelper.ConvertToIntPtr(shape);
            var type      = GetDataType(ncVariable);

            switch (type)
            {
            case NetCdfDataType.NC_BYTE:
                var byteArray = new byte[size];
                CheckResult(NetCdfWrapper.nc_get_vara(id, ncVariable, originPtr, shapePtr, byteArray));
                return(NetCdfFileHelper.CreateArrayFromShape(byteArray, shape));

            case NetCdfDataType.NC_CHAR:
                if (shape.Length != 2)
                {
                    throw new NotSupportedException(
                              "NetCdf: only char arrays for independent string variables supported");
                }
                var charArray = new byte[size];
                CheckResult(NetCdfWrapper.nc_get_vara_text(id, ncVariable, originPtr, shapePtr, charArray));
                return(NetCdfFileHelper.CreateCharArrayFromShape(charArray, shape));

            case NetCdfDataType.NC_INT:
                var intArray = new int[size];
                CheckResult(NetCdfWrapper.nc_get_vara_int(id, ncVariable, originPtr, shapePtr, intArray));
                return(NetCdfFileHelper.CreateArrayFromShape(intArray, shape));

            case NetCdfDataType.NC_FLOAT:
                var floatArray = new float[size];
                CheckResult(NetCdfWrapper.nc_get_vara_float(id, ncVariable, originPtr, shapePtr, floatArray));
                return(NetCdfFileHelper.CreateArrayFromShape(floatArray, shape));

            case NetCdfDataType.NC_DOUBLE:
                var doubleArray = new double[size];
                CheckResult(NetCdfWrapper.nc_get_vara_double(id, ncVariable, originPtr, shapePtr, doubleArray));
                return(NetCdfFileHelper.CreateArrayFromShape(doubleArray, shape));

            default:
                throw new Exception(
                          String.Format("Unknown type for reading NetCDF variable from file: type {0} from file {1}",
                                        type, path));
            }
        }