public string GetVariableName(NetCdfVariable ncVariable) { var nameBuilder = new StringBuilder((int)NetCdfWrapper.Limits.NC_MAX_NAME); CheckResult(NetCdfWrapper.nc_inq_varname(id, ncVariable, nameBuilder)); return(nameBuilder.ToString()); }
/// <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)); } }
public NetCdfDataType GetVariableDataType(NetCdfVariable ncVariable) { NetCdfDataType type; CheckResult(NetCdfWrapper.nc_inq_vartype(id, ncVariable, out type)); return(type); }
/// <summary> /// write array to file, origin is assumed at 0, and shape is taken from array /// </summary> /// <param name="ncVariable"></param> /// <param name="array"></param> public void Write(NetCdfVariable ncVariable, Array array) { var shape = array.GetShape(); var origin = new int[array.Rank]; Write(ncVariable, origin, shape, array); }
/// <summary> /// Use this when the attribute might not exist /// </summary> /// <param name="ncVariable"></param> /// <param name="attributeName"></param> /// <returns>The attribute value as a string, or null</returns> public string GetAttributeValue(NetCdfVariable ncVariable, string attributeName) { var attribute = GetAttribute(ncVariable, attributeName); if (attribute != null) { return(attribute.Value.ToString()); } return(null); }
/// <summary> /// When shape = -1, this is replaced by the size of the dimension /// as read from the netcdf file /// </summary> /// <param name="ncVariable"></param> /// <param name="shape"></param> private void CreateShapeForFullRange(NetCdfVariable ncVariable, ref int[] shape) { for (int i = 0; i < shape.Length; ++i) { if (shape[i] != -1) { continue; } shape[i] = GetShape(ncVariable)[i]; } }
private int[] GetDimensionIds(NetCdfVariable ncVariable) { int nDims; CheckResult(NetCdfWrapper.nc_inq_varndims(id, ncVariable, out nDims)); var dimIds = new int[nDims]; NetCdfWrapper.nc_inq_vardimid(id, ncVariable, dimIds); return(dimIds); }
public IEnumerable <string> GetVariableDimensionNames(NetCdfVariable ncVariable) { var dimensions = GetDimensionIds(ncVariable); foreach (var dimId in dimensions) { var nameBuilder = new StringBuilder((int)NetCdfWrapper.Limits.NC_MAX_NAME); CheckResult(NetCdfWrapper.nc_inq_dimname(id, dimId, nameBuilder)); yield return(nameBuilder.ToString()); } }
public NetCdfVariable AddVariable(string varName, Type type, NetCdfDimension[] ncDimensions) { int varId; CheckResult(NetCdfWrapper.nc_def_var(id, varName, NetCdfWrapper.GetNetCdf3DataType(type), ncDimensions.Length, ncDimensions.Select(d => (int)d).ToArray(), out varId)); var ncVar = new NetCdfVariable(varId); // variable name is unique here, guaranteed by nc_def_var call above ncVariableLookupByName.Add(varName, ncVar); return(ncVar); }
/// <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)); } }
public int[] GetShape(NetCdfVariable ncVariable) { var dimIds = GetDimensionIds(ncVariable); var nDims = dimIds.Length; var shape = new int[nDims]; for (int i = 0; i < nDims; ++i) { IntPtr len; CheckResult(NetCdfWrapper.nc_inq_dimlen(id, dimIds[i], out len)); shape[i] = len.ToInt32(); } return(shape); }
public bool IsCharArray(NetCdfVariable ncVariable) { int nDims; NetCdfWrapper.nc_inq_varndims(id, ncVariable, out nDims); if (nDims < 2) { return(false); } NetCdfDataType ncType; CheckResult(NetCdfWrapper.nc_inq_vartype(id, ncVariable, out ncType)); return(ncType == NetCdfDataType.NC_CHAR); }
public bool IsVariableUnlimited(NetCdfVariable ncVariable) { int nDims; CheckResult(NetCdfWrapper.nc_inq_varndims(id, ncVariable, out nDims)); var dimIds = new int[nDims]; CheckResult(NetCdfWrapper.nc_inq_vardimid(id, ncVariable, dimIds)); int unlimitedDimId; CheckResult(NetCdfWrapper.nc_inq_unlimdim(id, out unlimitedDimId)); return(dimIds.Contains(unlimitedDimId)); }
public Dictionary <string, object> GetAttributes(NetCdfVariable ncVariable) { var nameValueDictionary = new Dictionary <string, object>(); int nAtts; CheckResult(NetCdfWrapper.nc_inq_varnatts(id, ncVariable, out nAtts)); for (int i = 0; i < nAtts; ++i) { var nameBuilder = new StringBuilder((int)NetCdfWrapper.Limits.NC_MAX_NAME); NetCdfWrapper.nc_inq_attname(id, ncVariable, i, nameBuilder); var name = nameBuilder.ToString(); var attribute = GetAttribute(ncVariable, name); nameValueDictionary.Add(attribute.Name, attribute.Value); } return(nameValueDictionary); }
/// <summary> /// Read out the entire variable from the file /// </summary> /// <param name="ncVariable"></param> /// <returns></returns> public Array Read(NetCdfVariable ncVariable) { var type = GetDataType(ncVariable); var size = GetSize(ncVariable); var shape = GetShape(ncVariable); switch (type) { case NetCdfDataType.NC_BYTE: var byteArray = new byte[size]; CheckResult(NetCdfWrapper.nc_get_var(id, ncVariable, byteArray)); return(NetCdfFileHelper.CreateArrayFromShape(byteArray, shape)); case NetCdfDataType.NC_CHAR: var charArray = new byte[size]; CheckResult(NetCdfWrapper.nc_get_var_text(id, ncVariable, charArray)); return(NetCdfFileHelper.CreateCharArrayFromShape(charArray, shape)); case NetCdfDataType.NC_INT: var intArray = new int[size]; CheckResult(NetCdfWrapper.nc_get_var_int(id, ncVariable, intArray)); return(NetCdfFileHelper.CreateArrayFromShape(intArray, shape)); case NetCdfDataType.NC_FLOAT: var floatArray = new float[size]; CheckResult(NetCdfWrapper.nc_get_var_float(id, ncVariable, floatArray)); return(NetCdfFileHelper.CreateArrayFromShape(floatArray, shape)); case NetCdfDataType.NC_DOUBLE: var doubleArray = new double[size]; CheckResult(NetCdfWrapper.nc_get_var_double(id, ncVariable, 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)); } }
public int GetSize(NetCdfVariable ncVariable) { var shape = GetShape(ncVariable); return NetCdfFileHelper.GetSize(shape); }
/// <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)); } }
public int GetSize(NetCdfVariable ncVariable) { var shape = GetShape(ncVariable); return(NetCdfFileHelper.GetSize(shape)); }
public bool IsCharArray(NetCdfVariable ncVariable) { int nDims; NetCdfWrapper.nc_inq_varndims(id, ncVariable, out nDims); if (nDims < 2) return false; NetCdfDataType ncType; CheckResult(NetCdfWrapper.nc_inq_vartype(id, ncVariable, out ncType)); return (ncType == NetCdfDataType.NC_CHAR); }
public void SetCaching(NetCdfVariable ncVariable, bool caching) { // todo: what should we do here? }
public NetCdfVariable AddVariable(string varName, Type type, NetCdfDimension[] ncDimensions) { int varId; CheckResult(NetCdfWrapper.nc_def_var(id, varName, NetCdfWrapper.GetNetCdf3DataType(type), ncDimensions.Length, ncDimensions.Select(d => (int)d).ToArray(), out varId)); var ncVar = new NetCdfVariable(varId); // variable name is unique here, guaranteed by nc_def_var call above ncVariableLookupByName.Add(varName, ncVar); return ncVar; }
public NetCdfDataType GetVariableDataType(NetCdfVariable ncVariable) { NetCdfDataType type; CheckResult(NetCdfWrapper.nc_inq_vartype(id, ncVariable, out type)); return type; }
public void AddAttribute(NetCdfVariable ncVariable, NetCdfAttribute ncAttribute) { WriteAttribute(ncVariable, ncAttribute); }
public bool IsVariableUnlimited(NetCdfVariable ncVariable) { int nDims; CheckResult(NetCdfWrapper.nc_inq_varndims(id, ncVariable, out nDims)); var dimIds = new int[nDims]; CheckResult(NetCdfWrapper.nc_inq_vardimid(id, ncVariable, dimIds)); int unlimitedDimId; CheckResult(NetCdfWrapper.nc_inq_unlimdim(id, out unlimitedDimId)); return dimIds.Contains(unlimitedDimId); }
public int[] GetShape(NetCdfVariable ncVariable) { var dimIds = GetDimensionIds(ncVariable); var nDims = dimIds.Length; var shape = new int[nDims]; for (int i = 0; i < nDims; ++i) { IntPtr len; CheckResult(NetCdfWrapper.nc_inq_dimlen(id, dimIds[i], out len)); shape[i] = len.ToInt32(); } return shape; }
private int[] GetDimensionIds(NetCdfVariable ncVariable) { int nDims; CheckResult(NetCdfWrapper.nc_inq_varndims(id, ncVariable, out nDims)); var dimIds = new int[nDims]; NetCdfWrapper.nc_inq_vardimid(id, ncVariable, dimIds); return dimIds; }
public IEnumerable<NetCdfDimension> GetDimensions(NetCdfVariable ncVariable) { return GetVariableDimensionNames(ncVariable).Select(GetDimension); }
public Dictionary<string, object> GetAttributes(NetCdfVariable ncVariable) { var nameValueDictionary = new Dictionary<string, object>(); int nAtts; CheckResult(NetCdfWrapper.nc_inq_varnatts(id, ncVariable, out nAtts)); for (int i = 0; i < nAtts; ++i) { var nameBuilder = new StringBuilder((int)NetCdfWrapper.Limits.NC_MAX_NAME); NetCdfWrapper.nc_inq_attname(id, ncVariable, i, nameBuilder); var name = nameBuilder.ToString(); var attribute = GetAttribute(ncVariable, name); nameValueDictionary.Add(attribute.Name, attribute.Value); } return nameValueDictionary; }
/// <summary> /// Read out the entire variable from the file /// </summary> /// <param name="ncVariable"></param> /// <returns></returns> public Array Read(NetCdfVariable ncVariable) { var type = GetDataType(ncVariable); var size = GetSize(ncVariable); var shape = GetShape(ncVariable); switch (type) { case NetCdfDataType.NC_BYTE: var byteArray = new byte[size]; CheckResult(NetCdfWrapper.nc_get_var(id, ncVariable, byteArray)); return NetCdfFileHelper.CreateArrayFromShape(byteArray, shape); case NetCdfDataType.NC_CHAR: var charArray = new byte[size]; CheckResult(NetCdfWrapper.nc_get_var_text(id, ncVariable, charArray)); return NetCdfFileHelper.CreateCharArrayFromShape(charArray, shape); case NetCdfDataType.NC_INT: var intArray = new int[size]; CheckResult(NetCdfWrapper.nc_get_var_int(id, ncVariable, intArray)); return NetCdfFileHelper.CreateArrayFromShape(intArray, shape); case NetCdfDataType.NC_FLOAT: var floatArray = new float[size]; CheckResult(NetCdfWrapper.nc_get_var_float(id, ncVariable, floatArray)); return NetCdfFileHelper.CreateArrayFromShape(floatArray, shape); case NetCdfDataType.NC_DOUBLE: var doubleArray = new double[size]; CheckResult(NetCdfWrapper.nc_get_var_double(id, ncVariable, 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)); } }
public string GetVariableName(NetCdfVariable ncVariable) { var nameBuilder = new StringBuilder((int)NetCdfWrapper.Limits.NC_MAX_NAME); CheckResult(NetCdfWrapper.nc_inq_varname(id, ncVariable, nameBuilder)); return nameBuilder.ToString(); }
/// <summary> /// Use this when the attribute might not exist /// </summary> /// <param name="ncVariable"></param> /// <param name="attributeName"></param> /// <returns>The attribute value as a string, or null</returns> public string GetAttributeValue(NetCdfVariable ncVariable, string attributeName) { var attribute = GetAttribute(ncVariable, attributeName); if (attribute != null) return attribute.Value.ToString(); return null; }
public IEnumerable<string> GetVariableDimensionNames(NetCdfVariable ncVariable) { var dimensions = GetDimensionIds(ncVariable); foreach (var dimId in dimensions) { var nameBuilder = new StringBuilder((int) NetCdfWrapper.Limits.NC_MAX_NAME); CheckResult(NetCdfWrapper.nc_inq_dimname(id, dimId, nameBuilder)); yield return nameBuilder.ToString(); } }
public IEnumerable <NetCdfDimension> GetDimensions(NetCdfVariable ncVariable) { return(GetVariableDimensionNames(ncVariable).Select(GetDimension)); }
/// <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)); } }
/// <summary> /// When shape = -1, this is replaced by the size of the dimension /// as read from the netcdf file /// </summary> /// <param name="ncVariable"></param> /// <param name="shape"></param> private void CreateShapeForFullRange(NetCdfVariable ncVariable, ref int[] shape) { for (int i = 0; i < shape.Length; ++i) { if (shape[i] != -1) continue; shape[i] = GetShape(ncVariable)[i]; } }