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);
        }
 public static float byteToFloat(byte[] bytes)
 {
     if (bytes.Length > 1)
     {
         return(BitConverter.ToSingle(NetCDFTools.endianConversion(bytes), 0));
     }
     else
     {
         return(Convert.ToSingle(bytes[0]));
     }
 }
 public static float byteToFloat(byte[] bytes, bool isLittleEndian)
 {
     if (bytes.Length > 1)
     {
         return(BitConverter.ToSingle(NetCDFTools.endianConversion(bytes, isLittleEndian != ComputerIsLittleEndian), 0));
     }
     else
     {
         return(Convert.ToSingle(bytes[0]));
     }
 }
        /// <summary>
        /// Convers a byte string to a float array
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="isLittleEndian">true if the bytes are little endian</param>
        /// <returns></returns>
        public static float[] byteStringToFloatArray(byte[] bytes, bool isLittleEndian)
        {
            float[] ret = new float[bytes.Length / 4];

            int counter = 0;

            for (int i = 0; i < bytes.Length; i += 4)
            {
                byte[] f = { bytes[i], bytes[i + 1], bytes[i + 2], bytes[i + 3] };
                ret[counter++] = NetCDFTools.byteToFloat(f, isLittleEndian);
            }

            return(ret);
        }
        /// <summary>
        /// Reads in a "name" field
        /// </summary>
        /// <param name="fs">the stream to read from</param>
        /// <returns>the name string</returns>
        public static string name(Stream fs)
        {
            int length = NetCDFTools.int4(fs);

            string str = "";

            for (int i = 0; i < length; i++)
            {
                str += Convert.ToChar(fs.ReadByte());
            }

            padding(str.Length, fs);

            return(str);
        }
        // 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));
        }
Exemple #7
0
        public override string ToString()
        {
            string ret = "Attribute:[\"" + name + "\"|" + type.ToString() + "|";

            // if it's
            if (type == NC_TYPE.NC_CHAR)
            {
                ret += "\"" + NetCDFTools.byteToString(values) + "\"";
            }
            else
            {
                ret += values.Length;
            }

            ret += "]";
            return(ret);
        }
        private void ReadData(Stream fs)
        {
            foreach (Variable v in this.var_list)
            {
                if (fs.Position != v.begin)
                {
                    throw new FileFormatException("The variable " + v.name + " begins at " + v.begin + " but the stream is at " + fs.Position + " probably because the file is malformed.");
                }
                byte[][] data = NetCDFTools.values(v.type, v.length, fs, true);

                uint typeLength = NetCDFTools.getTypeLength(v.type);

                // too hard to organise the data into its actual shape until it's time to use it

                v.data = data;
            }
        }
        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 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 short byteToShort(byte[] bytes)
 {
     return(BitConverter.ToInt16(NetCDFTools.endianConversion(bytes), 0));
 }
 public static uint byteToUint(byte[] bytes)
 {
     return(BitConverter.ToUInt32(NetCDFTools.endianConversion(bytes), 0));
 }
 public static double byteToDouble(byte[] bytes)
 {
     return(BitConverter.ToDouble(NetCDFTools.endianConversion(bytes), 0));
 }
        /// <summary>
        /// Constructor for interpreting from an NC file
        /// </summary>
        /// <param name="head">the raw NC data</param>
        public GeoSpatialData(Header head)
        {
            Variable latVar  = null;
            Variable lngVar  = null;
            Variable timeVar = null;
            Variable dataVar = null;

            Dimension[] dataDimensions = null;

            int       latDimID  = 0;
            Dimension latDim    = null;
            int       lngDimID  = 0;
            Dimension lngDim    = null;
            int       timeDimID = 0;
            Dimension timeDim   = null;

            dataDimensions = new Dimension[3];

            foreach (Variable v in head.var_list)
            {
                if (v.name.Equals("latitude") || v.name.Equals("lat"))
                {
                    latVar   = v;
                    latDimID = v.dimid[0];
                    latDim   = head.dim_list.ElementAt(latDimID);
                    dataDimensions[latDimID] = latDim;
                }
                else if (v.name.Equals("longitude") || v.name.Equals("lng") || v.name.Equals("long"))
                {
                    lngVar   = v;
                    lngDimID = v.dimid[0];
                    lngDim   = head.dim_list.ElementAt(lngDimID);
                    dataDimensions[lngDimID] = lngDim;
                }
                else if (v.name.Equals("time"))
                {
                    timeVar   = v;
                    timeDimID = v.dimid[0];
                    timeDim   = head.dim_list.ElementAt(timeDimID);
                    dataDimensions[timeDimID] = timeDim;
                }
                else
                {
                    dataVar = v;
                }
            }

            // pull out the relevant attributes from the dataVar
            foreach (Attribute a in dataVar.vatt_list)
            {
                if (a.name == "long_name")
                {
                    this.data_name = NetCDFTools.byteToString(a.values);
                }
                else if (a.name == "units")
                {
                    this.units = NetCDFTools.byteToString(a.values);
                }
            }
            this.data_type          = dataVar.name;
            this.data_missing_value = dataVar.missing_value;
            this.data_max           = dataVar.valid_max;
            this.data_min           = dataVar.valid_min;

            // convert each set of data into its real format
            this.data = new float[timeVar.length, latVar.length, lngVar.length];
            for (int i = 0; i < dataDimensions[0].length; i++)
            {
                for (int j = 0; j < dataDimensions[1].length; j++)
                {
                    for (int k = 0; k < dataDimensions[2].length; k++)
                    {
                        int index = i * dataDimensions[1].length * dataDimensions[2].length +
                                    j * dataDimensions[2].length +
                                    k;

                        data[i, j, k] = NetCDFTools.byteToFloat(dataVar.data[index], dataVar.type);
                    }
                }
            }

            this.lat = new decimal[latVar.length];
            for (int i = 0; i < this.lat.Length; i++)
            {
                this.lat[i] = (decimal)NetCDFTools.byteToFloat(latVar.data[i], latVar.type);
            }

            this.lng = new decimal[lngVar.length];
            for (int i = 0; i < this.lng.Length; i++)
            {
                this.lng[i] = (decimal)NetCDFTools.byteToFloat(lngVar.data[i], lngVar.type);
            }

            DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

            this.time     = new int[timeVar.length];
            this.dateTime = new DateTime[timeVar.length];
            for (int i = 0; i < this.time.Length; i++)
            {
                this.time[i]     = NetCDFTools.byteToInt(timeVar.data[i]);
                this.dateTime[i] = epoch.AddSeconds(this.time[i]);
            }
        }