Пример #1
0
        /// <summary>
        /// Read a part of the variable from file, given a certain stride
        /// </summary>
        /// <param name="ncVariable"></param>
        /// <param name="origin"></param>
        /// <param name="shape">When shape is -1, this corresponds to reading that entire dimension</param>
        /// <param name="stride"></param>
        /// <returns></returns>
        public Array Read(NetCdfVariable ncVariable, int[] origin, int[] shape, int[] stride)
        {
            CreateShapeForFullRange(ncVariable, ref shape);
            var count = Enumerable.Range(0, origin.Length).Select(i => (shape[i] - origin[i]) / stride[i]).ToArray();
            var size  = NetCdfFileHelper.GetSize(count);

            var originPtr = NetCdfFileHelper.ConvertToIntPtr(origin);
            var stridePtr = NetCdfFileHelper.ConvertToIntPtr(stride);
            var countPtr  = NetCdfFileHelper.ConvertToIntPtr(count);

            var type = GetDataType(ncVariable);

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

            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_vars_text(id, ncVariable, originPtr, countPtr, stridePtr, charArray));
                return(NetCdfFileHelper.CreateCharArrayFromShape(charArray, count));

            case NetCdfDataType.NC_INT:
                var intArray = new int[size];
                CheckResult(NetCdfWrapper.nc_get_vars_int(id, ncVariable, originPtr, countPtr, stridePtr, intArray));
                return(NetCdfFileHelper.CreateArrayFromShape(intArray, count));

            case NetCdfDataType.NC_FLOAT:
                var floatArray = new float[size];
                CheckResult(NetCdfWrapper.nc_get_vars_float(id, ncVariable, originPtr, countPtr, stridePtr, floatArray));
                return(NetCdfFileHelper.CreateArrayFromShape(floatArray, count));

            case NetCdfDataType.NC_DOUBLE:
                var doubleArray = new double[size];
                CheckResult(NetCdfWrapper.nc_get_vars_double(id, ncVariable, originPtr, countPtr, stridePtr, doubleArray));
                return(NetCdfFileHelper.CreateArrayFromShape(doubleArray, count));

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