private static LinkedList <Attribute> att_list(Stream fs) { LinkedList <Attribute> list = new LinkedList <Attribute>(); int attribute_tag = NetCDFTools.int4(fs); // should be 0xC uint num_attrs = NetCDFTools.non_neg(fs); if (attribute_tag == 0xC && num_attrs > 0) { for (int i = 0; i < num_attrs; i++) { string name = NetCDFTools.name(fs); NC_TYPE type = (NC_TYPE)NetCDFTools.non_neg(fs); uint nelems = NetCDFTools.non_neg(fs); byte[][] valueArray = NetCDFTools.values(type, nelems, fs); list.AddLast(new Attribute(name, type, valueArray)); } } return(list); }
/// <summary> /// Reads in an array of values /// </summary> /// <param name="type">the type of values</param> /// <param name="length">the length of values</param> /// <param name="fs">the stream to read from</param> /// <param name="ignorePadding">optional. true to not ensure the stream sticks to 4-byte boundaries</param> /// <returns>a generically typed array</returns> public static byte[][] values(NC_TYPE type, uint length, Stream fs, bool ignorePadding) { uint typeLength = getTypeLength(type); // define the array as a jagged array so it is easier to pass rows of it around for conversion byte[][] valueArray = new byte[length][]; for (int i = 0; i < length; i++) { valueArray[i] = new byte[typeLength]; } // read in the values for (int i = 0; i < length; i++) { for (int j = 0; j < typeLength; j++) { valueArray[i][j] = (byte)fs.ReadByte(); } } // read in the padding bytes to the 4-byte boundary if (!ignorePadding) { padding(length * typeLength, fs); } return(valueArray); }
/// <summary> /// Gets the number of bytes that a given type requires /// </summary> /// <param name="type">the type to get</param> /// <returns>the number of bytes</returns> public static uint getTypeLength(NC_TYPE type) { switch (type) { case NC_TYPE.NC_BYTE: return(1); case NC_TYPE.NC_CHAR: return(1); case NC_TYPE.NC_DOUBLE: return(8); case NC_TYPE.NC_FLOAT: return(4); case NC_TYPE.NC_INT: return(4); case NC_TYPE.NC_SHORT: return(2); } return(0); }
/// <summary> /// Gets the C# Type for the given type enum /// </summary> /// <param name="type">the type enum to conver</param> /// <returns>the C# type</returns> public static Type getType(NC_TYPE type) { switch (type) { case NC_TYPE.NC_BYTE: return(typeof(byte)); case NC_TYPE.NC_CHAR: return(typeof(char)); case NC_TYPE.NC_DOUBLE: return(typeof(double)); case NC_TYPE.NC_FLOAT: return(typeof(float)); case NC_TYPE.NC_INT: return(typeof(int)); case NC_TYPE.NC_SHORT: return(typeof(short)); } return(null); }
// convert any numeric byte type to a float public static float byteToFloat(byte[] bytes, NC_TYPE type) { switch (type) { case NC_TYPE.NC_SHORT: return(Convert.ToSingle(NetCDFTools.byteToShort(bytes))); case NC_TYPE.NC_INT: return(Convert.ToSingle(NetCDFTools.byteToInt(bytes))); case NC_TYPE.NC_DOUBLE: return(Convert.ToSingle(NetCDFTools.byteToDouble(bytes))); } return(NetCDFTools.byteToFloat(bytes)); }
public Variable(string name, int[] dimid, LinkedList <Attribute> vatt_list, NC_TYPE type, uint vsize, uint begin) { this.name = name; this.dimid = dimid; this.vatt_list = vatt_list; this.type = type; this.vsize = vsize; this.begin = begin; this.length = this.vsize / NetCDFTools.getTypeLength(this.type); foreach (Attribute att in vatt_list) { if (att.name.Equals("units")) { this.units = NetCDFTools.byteToString(att.values); } else if (att.name.Equals("missing_value")) { this.missing_value = NetCDFTools.byteToFloat(att.values[0], att.type); } else if (att.name.Equals("valid_min")) { this.valid_min = NetCDFTools.byteToFloat(att.values[0], att.type); } else if (att.name.Equals("valid_max")) { this.valid_max = NetCDFTools.byteToFloat(att.values[0], att.type); } else if (att.name.Equals("valid_max")) { this.valid_max = NetCDFTools.byteToFloat(att.values[0], att.type); } else if (att.name.Equals("scale_factor")) { this.scale_factor = NetCDFTools.byteToFloat(att.values[0], att.type); } else if (att.name.Equals("add_offset")) { this.add_offset = NetCDFTools.byteToFloat(att.values[0], att.type); } } }
private Variable(Variable v) { this.name = String.Copy(v.name); this.dimid = (int[])v.dimid.Clone(); this.vatt_list = new LinkedList <Attribute>(); foreach (Attribute a in v.vatt_list) { this.vatt_list.AddLast(a.Clone()); } this.type = v.type; this.vsize = v.vsize; this.length = v.length; this.begin = v.begin; this.data = (byte[][])v.data.Clone(); this.units = String.Copy(v.units); this.missing_value = v.missing_value; this.valid_max = v.valid_max; this.scale_factor = v.scale_factor; this.add_offset = v.add_offset; }
public Attribute(string name, NC_TYPE type, byte[][] values) { this.name = name; this.type = type; this.values = values; }
public byte[][] values; // the values of the attribute private Attribute(Attribute a) { this.name = String.Copy(a.name); this.type = a.type; this.values = (byte[][])a.values.Clone(); }
private void ReadHeader(Stream fs) { // magic magic = ""; // should be "CDF" magic += Convert.ToChar(fs.ReadByte()); magic += Convert.ToChar(fs.ReadByte()); magic += Convert.ToChar(fs.ReadByte()); version = fs.ReadByte(); // should be 1 // numrecs uint num_recs = NetCDFTools.non_neg(fs); if (num_recs == 0xFFFFFFFF) { // streaming } // dim_list int dimension_tag = NetCDFTools.int4(fs); // should be 0xA uint num_dims = NetCDFTools.non_neg(fs); if (dimension_tag == 0xA && num_dims > 0) { for (int i = 0; i < num_dims; i++) { string name = NetCDFTools.name(fs); int dim_length = NetCDFTools.int4(fs); bool is_record = false; if (dim_length == 0) { // if dim_length is 0, then this is the record dimension is_record = true; } dim_list.AddLast(new Dimension(name, dim_length, is_record)); } } // gatt_list gatt_list = att_list(fs); // var_list int variable_tag = NetCDFTools.int4(fs); // should be 0xB uint num_vars = NetCDFTools.non_neg(fs); if (variable_tag == 0xB && num_vars > 0) { for (int i = 0; i < num_vars; i++) { string name = NetCDFTools.name(fs); uint nelems = NetCDFTools.non_neg(fs); // dimid list int[] dimid = new int[nelems]; for (int j = 0; j < nelems; j++) { dimid[j] = NetCDFTools.int4(fs); } // vatt_list LinkedList <Attribute> vatt_list = att_list(fs); NC_TYPE type = (NC_TYPE)NetCDFTools.non_neg(fs); uint vsize = NetCDFTools.non_neg(fs); uint begin = NetCDFTools.non_neg(fs); var_list.AddLast(new Variable(name, dimid, vatt_list, type, vsize, begin)); } } }
public static byte[][] values(NC_TYPE type, uint length, Stream fs) { return(values(type, length, fs, false)); }