Ejemplo n.º 1
0
        /// <summary>
        /// Note that only char array can be higher dimensional here, it will then
        /// be flattened before written
        /// </summary>
        public void Write(NetCdfVariable ncVariable, int[] origin, int[] shape, Array array)
        {
            var originPtr  = NetCdfFileHelper.ConvertToIntPtr(origin);
            var shapePtr   = NetCdfFileHelper.ConvertToIntPtr(shape);
            var ncDataType = GetNetCdfDataType(array);

            switch (ncDataType)
            {
            case NetCdfDataType.NC_BYTE:
                CheckResult(NetCdfWrapper.nc_put_vara(id, ncVariable, originPtr, shapePtr, (byte[])array));
                break;

            case NetCdfDataType.NC_CHAR:
                var bytes = NetCdfFileHelper.FlattenCharArray(array, shape);
                CheckResult(NetCdfWrapper.nc_put_vara_text(id, ncVariable, originPtr, shapePtr, bytes));
                break;

            case NetCdfDataType.NC_INT:
                CheckResult(NetCdfWrapper.nc_put_vara_int(id, ncVariable, originPtr, shapePtr, (int[])array));
                break;

            case NetCdfDataType.NC_FLOAT:
                CheckResult(NetCdfWrapper.nc_put_vara_float(id, ncVariable, originPtr, shapePtr, (float[])array));
                break;

            case NetCdfDataType.NC_DOUBLE:
                CheckResult(NetCdfWrapper.nc_put_vara_double(id, ncVariable, originPtr, shapePtr, (double[])array));
                break;

            default:
                throw new Exception(
                          String.Format("Unknown type for writing NetCDF variable to file: type {0} to file {1}",
                                        ncDataType, path));
            }
        }
Ejemplo n.º 2
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));
            }
        }