/// <summary> /// Read the values accrding to the values type of the variable /// </summary> /// <param name="varType"></param> /// <returns></returns> private object ReadValue(NcType varType) { object value = null; switch (varType) { case NcType.NcByte: value = ReadBytes(0, 4); break; case NcType.NcChar: value = ReadBytes(0, 4); break; case NcType.NcShort: value = ReadBytes(0, 4); break; case NcType.NcInt: value = ReadInteger(ReadBytes(0, 4)); break; case NcType.NcFloat: value = ReadFloat(0); break; case NcType.NcDouble: value = ReadDouble(0); break; default: return(null); } return(value); }
public static Type GetCLRType(NcType ncType) { switch (ncType) { case NcType.NC_BYTE: return(typeof(byte)); case NcType.NC_CHAR: return(typeof(sbyte)); case NcType.NC_SHORT: return(typeof(short)); case NcType.NC_INT: return(typeof(int)); case NcType.NC_INT64: return(typeof(long)); case NcType.NC_FLOAT: return(typeof(float)); case NcType.NC_DOUBLE: return(typeof(float)); case NcType.NC_STRING: return(typeof(string)); default: throw new ApplicationException("Unknown nc type"); } }
/// <summary> /// Read the variable attributes and fill it to Ncvar object passed as parameter /// </summary> /// <param name="count"></param> /// <param name="var"></param> private void ReadVariableAttributes(int count, ref NcVar var) { for (int i = 0; i < count; ++i) { string str = ReadBytes(0, 4); int numval = ReadInteger(str); // Get the length of the name of the variable attribute string attrName = ReadStringName(numval); int controlNum = ReadInteger(ReadBytes(0, 4)); object attrVal = null; NcType type = NcType.NcByte; switch (controlNum) { case NC_BYTE: break; case NC_CHAR: //Read variable name length str = ReadBytes(0, 4); numval = ReadInteger(str); // Get the length of the attribute value attrVal = ReadStringName(numval); // Get the attribute value type = NcType.NcChar; break; case NC_INT: str = ReadBytes(0, 4); numval = ReadInteger(str); //length of arrary break; case NC_FLOAT: str = ReadBytes(0, 4); numval = ReadInteger(str); //length of arrary float[] arrF = new float[numval]; for (int j = 0; j < numval; j++) { arrF[j] = ReadFloat(0); } attrVal = arrF; type = NcType.NcFloat; break; case NC_DOUBLE: str = ReadBytes(0, 4); numval = ReadInteger(str); //length of arrary double[] arrD = new double[numval]; for (int j = 0; j < numval; j++) { arrD[j] = ReadDouble(0); } attrVal = arrD; type = NcType.NcDouble; break; } // Add variable attributes to the varible object here. NcAtt varAtt = new NcAtt(attrName, type, attrVal); var.VariableAttributes.Add(varAtt); } }
protected static NcResult NcInqVartype(int ncid, int varid, out NcType xtype) { unsafe { fixed(NcType *xtypep = &xtype) return(nc_inq_vartype(ncid, varid, xtypep)); } }
protected static NcResult NcInqAtt(int ncid, int varid, string attname, out NcType xtype, out int len) { unsafe { fixed(NcType *xtypep = &xtype) { fixed(int *lenp = &len) return(nc_inq_att(ncid, varid, attname, xtypep, lenp)); } } }
/// <summary> /// Writes attribute name and values /// </summary> /// <param name="attribType"></param> /// <param name="attribName"></param> /// <param name="attribVal"></param> public static void WriteAttribute(NcType dataType, string attribName, object[] attVal) { //Number of Characters in name WriteInteger(attribName.Length); //write attribute name WriteString(attribName); //Write data type tag for the attribute WriteInteger(GetTypeValue(dataType)); //Write variable / attribute values as per the data type for (int i = 0; i < attVal.Length; i++) { WriteAttValue(dataType, attVal[i]); } }
private int BytesOf(NcType type) { if (type.Equals(NcType.NcByte) || type.Equals(NcType.NcChar) || type.Equals(NcType.NcShort) || type.Equals(NcType.NcFloat) || type.Equals(NcType.NcInt)) { return(4); } else if (type.Equals(NcType.NcDouble)) { return(8); } return(0); }
public bool TestTypes() { for (int i = 1; i < 13; i++) { NcType t = new NcType(i); Assert.Equals(t.GetId(), i); if (i == 1) // byte { Assert.Equals(t.GetName(), "byte"); Assert.Equals(t.GetSize(), 1); // A byte should be just one byte right? } else { Assert.NotNull(t.GetName()); Assert.True(t.GetSize() > 0); } } return(true); }
/// <summary> /// Converts the NcType to Net CDF data type values. /// </summary> /// <param name="type"></param> /// <returns></returns> private static int GetTypeValue(NcType type) { //This function can be removed later. switch (type) { case NcType.NcByte: return(1); case NcType.NcChar: return(2); case NcType.NcShort: return(3); case NcType.NcInt: return(4); case NcType.NcFloat: return(5); case NcType.NcDouble: return(6); } return(0); }
/// <summary> /// Sets Attribute name, type and its value /// </summary> /// <param name="name"></param> /// <param name="type"></param> /// <param name="value"></param> internal NcAtt(string name, NcType type, object value) { this.Name = name; this.Type = type; int length = 0; switch (type) { case NcType.NcByte: byte[] bArr = (byte[])value; length = bArr.Length; break; case NcType.NcChar: length = value.ToString().Length; // string length break; case NcType.NcShort: Int16[] iArr = (Int16[])value; length = iArr.Length; break; case NcType.NcInt: int[] iArray = (int[])value; length = iArray.Length; break; case NcType.NcFloat: float[] fArr = (float[])value; length = fArr.Length; break; case NcType.NcDouble: double[] dArr = (double[])value; length = dArr.Length; break; } this.value = new NcValues(type, length, value); }
public bool TestOpaque() { NcFile file = null; NcVar var = null; NcDim dim = null; NcType type = null; NcOpaqueType opaqueType = null; byte[] opaqueBuffer = new byte[32]; byte[] readBuffer = new byte[32]; for (int i = 0; i < 32; i++) { opaqueBuffer[i] = (byte)i; } try { file = TestHelper.NewFile(filePath); type = file.AddOpaqueType("opaque", 32); opaqueType = new NcOpaqueType(type); Assert.Equals(type.GetTypeClass(), NcTypeEnum.NC_OPAQUE); Assert.Equals(opaqueType.GetTypeSize(), 32); dim = file.AddDim("time", 1); var = file.AddVar("opaqueVar", opaqueType, dim); int iLen = 0; var.PutVar(new Int32[] { 0 }, opaqueBuffer); iLen = var.GetVar(new Int32[] { 0 }, readBuffer); Assert.Equals(iLen, 32); for (int i = 0; i < 32; i++) { Assert.Equals(readBuffer[i], opaqueBuffer[i]); } } finally { file.Close(); } CheckDelete(filePath); return(true); }
private static unsafe extern NcResult nc_inq_vartype(int ncid, int varid, NcType* xtypep);
protected static NcResult NcInqVartype(int ncid, int varid, out NcType xtype) { unsafe { fixed (NcType* xtypep = &xtype) return nc_inq_vartype(ncid, varid, xtypep); } }
public static extern int nc_inq_vartype(int ncid, int varid, out NcType xtypep);
public static extern int nc_put_att_longlong(int ncid, int varid, string name, NcType type, int len, long[] tp);
public static extern int nc_put_att_float(int ncid, int varid, string name, NcType type, int len, float[] tp);
public static extern int nc_def_var(int ncid, string name, NcType xtype, int ndims, int[] dimids, out int varidp);
protected static NcResult NcInqGlobalAtt(int ncid, string attname, out NcType xtype, out int len) { return NcInqAtt(ncid, -1, attname, out xtype, out len); }
private void FileSetup(ref NcFile file, ref NcDim dim1, ref NcVar var1, NcType type, int len = 20) { file = TestHelper.NewFile(filePath); dim1 = file.AddDim("time", len); var1 = file.AddVar("temp", type, dim1); }
private static unsafe extern NcResult nc_inq_att(int ncid, int varid, [MarshalAs(UnmanagedType.LPStr)] string attname, NcType* xtypep, int* lenp);
protected static NcResult NcInqAtt(int ncid, int varid, string attname, out NcType xtype, out int len) { unsafe { fixed (NcType* xtypep = &xtype) { fixed (int* lenp = &len) return nc_inq_att(ncid, varid, attname, xtypep, lenp); } } }
public static extern ResultCode nc_put_att_short(int ncid, int varid, string name, NcType type, int len, short[] tp);
public static extern ResultCode nc_put_att_double(int ncid, int varid, string name, NcType type, int len, double[] tp);
public static extern int nc_put_att_uchar(int ncid, int varid, string name, NcType xtype, int len, byte[] op);
public static Type GetCLRType(NcType ncType) { switch (ncType) { case NcType.NC_BYTE: return typeof(byte); case NcType.NC_CHAR: return typeof(sbyte); case NcType.NC_SHORT: return typeof(short); case NcType.NC_INT: return typeof(int); case NcType.NC_INT64: return typeof(long); case NcType.NC_FLOAT: return typeof(float); case NcType.NC_DOUBLE: return typeof(double); case NcType.NC_STRING: return typeof(string); default: throw new ApplicationException("Unknown nc type"); } }
private static unsafe extern NcResult nc_inq_atttype(int ncid, int varid, [MarshalAs(UnmanagedType.LPStr)] StringBuilder attname, NcType* xtypep);
public static int nc_inq_atttype(int ncid, int varid, string name, out NcType type) { lock (namebuf) { return NetCDFDynamic.f_nc_inq_atttype.Invoke(ncid, varid, name, out type); } }
protected NcAttGeneric(int ncId, int variableID, string attributeName, NcType type) : base(ncId, variableID, attributeName) { NcType = type; }
/// <summary> /// Fills the NcMetada with file metadata /// </summary> /// <param name="reader"></param> /// <returns></returns> public static NcMetaData GetMetaData(NetCDFReader reader) { NcMetaData metadata = new NcMetaData(); //Add No of records metadata.NumberOfRecords = (int)reader.NumberOfRecords; //Add Dimensions Dictionary <uint, INetCDFDimension> .Enumerator dimEnumerator = reader.Dimensions.GetEnumerator(); while (dimEnumerator.MoveNext()) { string name = dimEnumerator.Current.Value.Name; int size = (int)dimEnumerator.Current.Value.Length; NcDim dim = new NcDim(name, size); //dim.IsUnlimited = dimEnumerator.Current.Value.IsRecordDimension; metadata.AddDimension(dim); } //Add Global attributes Dictionary <string, INetCDFAttribute> .Enumerator attrEnumerator = reader.Attributes.GetEnumerator(); while (attrEnumerator.MoveNext()) { NcType attType = (NcType)attrEnumerator.Current.Value.DataType; string attName = attrEnumerator.Current.Key; //Get Attname here object attValue = attrEnumerator.Current.Value.Value; NcAtt att = new NcAtt(attName, attType, attValue); metadata.AddGlobalAttribute(att); } //Add Variables Dictionary <string, INetCDFVariable> .Enumerator varEnumerator = reader.Variables.GetEnumerator(); while (varEnumerator.MoveNext()) { string name = varEnumerator.Current.Value.Name; //object value = varEnumerator.Current.Value; NcType type = (NcType)varEnumerator.Current.Value.DataType; NcVar var = new NcVar(name, type); //Add dimenstions to variables var.NumValues = (int)varEnumerator.Current.Value.NumValues; for (int i = 0; i < varEnumerator.Current.Value.DimensionIDs.Length; i++) { uint dimID = varEnumerator.Current.Value.DimensionIDs[i]; NcDim dim = new NcDim(reader.Dimensions[dimID].Name, (int)reader.Dimensions[dimID].Length); var.Dimensions.Insert(i, dim); } // Add variable attributes Dictionary <string, INetCDFAttribute> .Enumerator vattEnumerator = varEnumerator.Current.Value.Attributes.GetEnumerator(); while (vattEnumerator.MoveNext()) { NcType attType = (NcType)vattEnumerator.Current.Value.DataType; string attName = vattEnumerator.Current.Key; //Get Attname here object attValue = vattEnumerator.Current.Value.Value; NcAtt att = new NcAtt(attName, attType, attValue); var.VariableAttributes.Add(att); } metadata.AddVariable(var); } return(metadata); }
public bool TestEnum() { NcFile file = null; NcType type = null; NcEnumType enumType = null; NcDim dim = null; NcVar var = null; sbyte[] sbyteBuffer = new sbyte[1]; byte[] byteBuffer = new byte[1]; Int16[] Int16Buffer = new Int16[1]; UInt16[] UInt16Buffer = new UInt16[1]; Int32[] Int32Buffer = new Int32[1]; UInt32[] UInt32Buffer = new UInt32[1]; Int64[] Int64Buffer = new Int64[1]; UInt64[] UInt64Buffer = new UInt64[1]; try { file = TestHelper.NewFile(filePath); dim = file.AddDim("time", 1); type = file.AddEnumType("sbyteenum", NcEnumType.Types.NC_BYTE); enumType = new NcEnumType(type); Assert.Equals(enumType.GetName(), "sbyteenum"); Assert.Equals(enumType.GetMemberCount(), 0); Assert.True(NcByte.Instance.Equals(enumType.GetBaseType())); enumType.AddMember("BASE", 0); enumType.AddMember("VOR", 1); enumType.AddMember("DME", 2); enumType.AddMember("TAC", 3); var = file.AddVar("enumsbyteVar", enumType, dim); var.PutVar(new sbyte[] { 3 }); var.GetVar(sbyteBuffer); Assert.Equals(sbyteBuffer[0], (sbyte)3); type = file.AddEnumType("byteenum", NcEnumType.Types.NC_UBYTE); enumType = new NcEnumType(type); Assert.Equals(enumType.GetName(), "byteenum"); Assert.Equals(enumType.GetMemberCount(), 0); Assert.True(NcUbyte.Instance.Equals(enumType.GetBaseType())); enumType.AddMember("BASE", 0); enumType.AddMember("VOR", 1); enumType.AddMember("DME", 2); enumType.AddMember("TAC", 3); var = file.AddVar("enumbyteVar", enumType, dim); var.PutVar(new byte[] { 3 }); var.GetVar(byteBuffer); Assert.Equals(byteBuffer[0], (byte)3); type = file.AddEnumType("Int16enum", NcEnumType.Types.NC_SHORT); enumType = new NcEnumType(type); Assert.Equals(enumType.GetName(), "Int16enum"); Assert.Equals(enumType.GetMemberCount(), 0); Assert.True(NcShort.Instance.Equals(enumType.GetBaseType())); enumType.AddMember("BASE", 0); enumType.AddMember("VOR", 1); enumType.AddMember("DME", 2); enumType.AddMember("TAC", 3); var = file.AddVar("enumInt16Var", enumType, dim); var.PutVar(new Int16[] { 3 }); var.GetVar(Int16Buffer); Assert.Equals(Int16Buffer[0], (Int16)3); type = file.AddEnumType("UInt16enum", NcEnumType.Types.NC_USHORT); enumType = new NcEnumType(type); Assert.Equals(enumType.GetName(), "UInt16enum"); Assert.Equals(enumType.GetMemberCount(), 0); Assert.True(NcUshort.Instance.Equals(enumType.GetBaseType())); enumType.AddMember("BASE", 0); enumType.AddMember("VOR", 1); enumType.AddMember("DME", 2); enumType.AddMember("TAC", 3); var = file.AddVar("enumUInt16Var", enumType, dim); var.PutVar(new UInt16[] { 3 }); var.GetVar(UInt16Buffer); Assert.Equals(UInt16Buffer[0], (UInt16)3); type = file.AddEnumType("Int32enum", NcEnumType.Types.NC_INT); enumType = new NcEnumType(type); Assert.Equals(enumType.GetName(), "Int32enum"); Assert.Equals(enumType.GetMemberCount(), 0); Assert.True(NcInt.Instance.Equals(enumType.GetBaseType())); enumType.AddMember("BASE", 0); enumType.AddMember("VOR", 1); enumType.AddMember("DME", 2); enumType.AddMember("TAC", 3); var = file.AddVar("enumInt32Var", enumType, dim); var.PutVar(new Int32[] { 3 }); var.GetVar(Int32Buffer); Assert.Equals(Int32Buffer[0], (Int32)3); type = file.AddEnumType("UInt32enum", NcEnumType.Types.NC_UINT); enumType = new NcEnumType(type); Assert.Equals(enumType.GetName(), "UInt32enum"); Assert.Equals(enumType.GetMemberCount(), 0); Assert.True(NcUint.Instance.Equals(enumType.GetBaseType())); enumType.AddMember("BASE", 0); enumType.AddMember("VOR", 1); enumType.AddMember("DME", 2); enumType.AddMember("TAC", 3); var = file.AddVar("enumUInt32Var", enumType, dim); var.PutVar(new UInt32[] { 3 }); var.GetVar(UInt32Buffer); Assert.Equals(UInt32Buffer[0], (UInt32)3); type = file.AddEnumType("Int64enum", NcEnumType.Types.NC_INT64); enumType = new NcEnumType(type); Assert.Equals(enumType.GetName(), "Int64enum"); Assert.Equals(enumType.GetMemberCount(), 0); Assert.True(NcInt64.Instance.Equals(enumType.GetBaseType())); enumType.AddMember("BASE", (Int64)0); enumType.AddMember("VOR", (Int64)1); enumType.AddMember("DME", (Int64)2); enumType.AddMember("TAC", (Int64)3); Assert.Equals(enumType.GetMemberCount(), 4); Assert.Equals(enumType.GetMemberNameFromValue((sbyte)1), "VOR"); Assert.Equals(enumType.GetMemberNameFromValue((byte)1), "VOR"); Assert.Equals(enumType.GetMemberNameFromValue((Int16)1), "VOR"); Assert.Equals(enumType.GetMemberNameFromValue((UInt16)1), "VOR"); Assert.Equals(enumType.GetMemberNameFromValue((Int32)1), "VOR"); Assert.Equals(enumType.GetMemberNameFromValue((UInt32)1), "VOR"); Assert.Equals(enumType.GetMemberNameFromValue((Int64)1), "VOR"); Assert.Equals(enumType.GetMemberNameFromValue((UInt64)1), "VOR"); var = file.AddVar("enumInt64Var", enumType, dim); var.PutVar(new Int64[] { 3 }); var.GetVar(Int64Buffer); Assert.Equals(Int64Buffer[0], (Int64)3); type = file.AddEnumType("UInt64enum", NcEnumType.Types.NC_UINT64); enumType = new NcEnumType(type); Assert.Equals(enumType.GetName(), "UInt64enum"); Assert.Equals(enumType.GetMemberCount(), 0); Assert.True(NcUint64.Instance.Equals(enumType.GetBaseType())); enumType.AddMember("BASE", 0); enumType.AddMember("VOR", 1); enumType.AddMember("DME", 2); enumType.AddMember("TAC", 3); var = file.AddVar("enumUInt64Var", enumType, dim); var.PutVar(new UInt64[] { 3 }); var.GetVar(UInt64Buffer); Assert.Equals(UInt64Buffer[0], (UInt64)3); } finally { file.Close(); } CheckDelete(filePath); return(true); }
public static extern int nc_put_att_byte(int ncid, int varid, string name, NcType type, int len, sbyte[] tp);
public static int nc_inq_vartype(int ncid, int varid, out NcType xtypep) { lock (namebuf) { return NetCDFDynamic.f_nc_inq_vartype.Invoke(ncid, varid, out xtypep); } }
public static extern int nc_inq_var(int ncid, int varid, StringBuilder name, out NcType type, out int ndims, int[] dimids, out int natts);
public static int nc_inq_var(int ncid, int varid, out string name, out NcType type, out int ndims, int[] dimids, out int natts) { lock (namebuf) { var r = NetCDFDynamic.f_nc_inq_var.Invoke(ncid, varid, namebuf, out type, out ndims, dimids, out natts); name = namebuf.ToString(); return r; } }
public static extern int nc_inq_att(int ncid, int varid, string name, out NcType type, out int length);
/// <summary> /// Constructor for a value block of the specified type and length. /// </summary> /// <param name="type"></param> /// <param name="length"></param> public NcValues(NcType type, int length, Object data) { this.type = type; this.length = length; this.data = data; }
protected static NcResult NcInqAtttype(int ncid, int varid, out StringBuilder attname, out NcType xtype) { attname = new StringBuilder(MaxStringLength); unsafe { fixed(NcType *xtypep = &xtype) return(nc_inq_atttype(ncid, varid, attname, xtypep)); } }
protected static NcResult NcInqVar(int ncid, int varid, out StringBuilder varname, out NcType xtype, out int ndims, ref int[] dimids, out int natts) { varname = new StringBuilder(MaxStringLength); unsafe { fixed(NcType *xtypep = &xtype) fixed(int *ndimsp = &ndims, dimidsp = &dimids[0], nattsp = &natts) return(nc_inq_var(ncid, varid, varname, xtypep, ndimsp, dimidsp, nattsp)); } }
protected static NcResult NcInqAtttype(int ncid, int varid, out StringBuilder attname, out NcType xtype) { attname = new StringBuilder(MaxStringLength); unsafe { fixed (NcType* xtypep = &xtype) return nc_inq_atttype(ncid, varid, attname, xtypep); } }
public static int nc_inq_att(int ncid, int varid, string name, out NcType type, out IntPtr length) { lock (namebuf) { return NetCDFDynamic.f_nc_inq_att.Invoke(ncid, varid, name, out type, out length); } }
protected static NcResult NcInqGlobalAtt(int ncid, string attname, out NcType xtype, out int len) { return(NcInqAtt(ncid, -1, attname, out xtype, out len)); }
protected static NcResult NcInqGlobalAtttype(int ncid, out StringBuilder attname, out NcType xtype) { return NcInqAtttype(ncid, -1, out attname, out xtype); }
protected static NcResult NcInqGlobalAtttype(int ncid, out StringBuilder attname, out NcType xtype) { return(NcInqAtttype(ncid, -1, out attname, out xtype)); }
private static unsafe extern NcResult nc_inq_var(int ncid, int varid, [MarshalAs(UnmanagedType.LPStr)] StringBuilder varname, NcType* xtypep, int* ndimsp, int* dimidsp, int* nattsp);
protected static NcResult NcInqVar(int ncid, int varid, out StringBuilder varname, out NcType xtype, out int ndims, ref int[] dimids, out int natts) { varname = new StringBuilder(MaxStringLength); unsafe { fixed (NcType* xtypep = &xtype) fixed (int* ndimsp = &ndims, dimidsp = &dimids[0], nattsp = &natts) return nc_inq_var(ncid, varid, varname, xtypep, ndimsp, dimidsp, nattsp); } }
public static int nc_def_var(int ncid, string name, NcType xtype, int[] dimids, out int varidp) { lock (namebuf) { return NetCDFDynamic.f_nc_def_var.Invoke(ncid, name, xtype, dimids.Length, dimids, out varidp); } }