private static void WriteAttValue(NetCDFDataType dataType, object value)
        {
            switch (dataType)
            {
            case NetCDFDataType.NcByte:
                byte byteValue = (byte)value;
                writer.Write(byteValue);
                break;

            case NetCDFDataType.NcChar:
                string[] strValue = (string[])value;
                for (int i = 0; i < strValue.Length; i++)
                {
                    WriteInteger(strValue[i].Length);
                    WriteString(strValue[i]);
                }
                break;

            case NetCDFDataType.NcShort:
                short[] shA = (short[])value;
                WriteInteger(shA.Length);
                for (int i = 0; i < shA.Length; i++)
                {
                    WriteShort(shA[i]);
                }
                break;

            case NetCDFDataType.NcInt:
                int[] intA = (int[])value;
                WriteInteger(intA.Length);
                for (int i = 0; i < intA.Length; i++)
                {
                    WriteInteger(intA[i]);
                }
                break;

            case NetCDFDataType.NcFloat:
                float[] flA = (float[])value;
                WriteInteger(flA.Length);
                for (int i = 0; i < flA.Length; i++)
                {
                    WriteFloat(flA[i]);
                }
                break;

            case NetCDFDataType.NcDouble:
                double[] dbA = (double[])value;
                WriteInteger(dbA.Length);
                for (int i = 0; i < dbA.Length; i++)
                {
                    WriteDouble(dbA[i]);
                }
                break;
            }
        }
 /// <summary>
 /// Writes attribute
 /// </summary>
 /// <param name="attribType"></param>
 /// <param name="attribName"></param>
 /// <param name="attribVal"></param>
 public static void WriteAttribute(NetCDFDataType 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((int)dataType);
     //Write variable / attribute values as per the data type
     for (int i = 0; i < attVal.Length; i++)
     {
         WriteAttValue(dataType, attVal[i]);
     }
 }
        private static void WriteVariableValues(NetCDFDataType dataType, int offset, string variableName, int recordSize, object[] values)
        {
            //First overwrite the size bytes and offset values for the variable
            //HERE THE SIZE AND OFFSET VALUES ARE OVERWRITTEN.
            if (offset != -1)
            {
                writer.Seek(offset, SeekOrigin.Begin);
                //Size
                //WriteInteger(GetByteSize((int)dataType));
                WriteInteger(GetByteSize(recordSize));

                // update offset value only for the first time
                WriteInteger((int)writer.BaseStream.Length);
            }

            // Seek to the end of the file again if the record is updated.
            writer.Seek((int)writer.BaseStream.Length, SeekOrigin.Begin);

            //Now start writing values of the variable
            for (int i = 0; i < values.Length; i++)
            {
                WriteVarValue(dataType, values[i]);
            }
        }
Ejemplo n.º 4
0
        public static Type GetClrType(NetCDFDataType ncType)
        {
            Type clrType = null;

            switch (ncType)
            {
            case NetCDFDataType.NcByte:
                clrType = typeof(byte);
                break;

            case NetCDFDataType.NcChar:
                clrType = typeof(char);
                break;

            case NetCDFDataType.NcDouble:
                clrType = typeof(double);
                break;

            case NetCDFDataType.NcInt:
                clrType = typeof(Int32);
                break;

            //case NetCDFDataType.NcLong:
            //    clrType = typeof(long);
            //    break;
            case NetCDFDataType.NcShort:
                clrType = typeof(short);
                break;

            case NetCDFDataType.NcFloat:
                clrType = typeof(float);
                break;
            }

            return(clrType);
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="name">Variable name</param>
 /// <param name="dimensions"></param>
 /// <param name="attributes"></param>
 /// <param name="offset"></param>
 public NetCDFVariable(string name, uint[] dimensions, Dictionary <string, INetCDFAttribute> attributes, NetCDFDataType dataType, uint variableSize, int dataOffset, bool isARecordVariable)
 {
     this.Name              = name;
     this.DimensionIDs      = dimensions;
     this.Attributes        = attributes;
     this.DataType          = dataType;
     this.Size              = variableSize;
     this._dataOffsetInFile = dataOffset;
     this.IsRecordVariable  = isARecordVariable;
 }
        private static void WriteVarValue(NetCDFDataType dataType, object value)
        {
            switch (dataType)
            {
            case NetCDFDataType.NcByte:
                if (null != value)
                {
                    byte byteValue = (byte)value;
                    writer.Write(byteValue);
                }
                else
                {
                    writer.Write(0);
                }
                break;

            case NetCDFDataType.NcChar:

                if (null != value)
                {
                    string[] strValue = (string[])value;
                    for (int i = 0; i < strValue.Length; i++)
                    {
                        WriteInteger(strValue[i].Length);
                        WriteString(strValue[i]);
                    }
                }
                else
                {
                    WriteInteger(0);
                    WriteInteger(0);
                }
                break;

            case NetCDFDataType.NcShort:
                if (null != value)
                {
                    short shA = (short)value;
                    WriteShort(shA);
                }
                else
                {
                    WriteShort(-255);
                }
                break;

            case NetCDFDataType.NcInt:
                if (null != value)
                {
                    int intA = (int)value;
                    WriteInteger(intA);
                }
                else
                {
                    WriteInteger(int.MinValue);
                }
                break;

            case NetCDFDataType.NcFloat:
                if (null != value)
                {
                    float flA = (float)value;
                    WriteFloat(flA);
                }
                else
                {
                    WriteFloat(float.MinValue);
                }
                break;

            case NetCDFDataType.NcDouble:
                if (null != value)
                {
                    double dbA = (double)value;
                    WriteDouble(dbA);
                }
                else
                {
                    WriteDouble(double.MinValue);
                }
                break;
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Reads the attributes array from NetCDF file and places them in the
        /// Dictionary.
        /// </summary>
        /// <param name="attributes">Dictionary into which attributes are placed.</param>
        /// <exception cref="Microsoft.NetCDF.CSharpAPI.ParserException"></exception>
        /// <returns>Number of attributes read from the NetCDF file</returns>
        private uint ReadAttributes(Dictionary <string, INetCDFAttribute> attributes)
        {
            int  metadata_type_flag = _reader.ReadInt32();
            uint attributes_count   = 0;

            if ((NetCDFMetadataType)metadata_type_flag == NetCDFMetadataType.NcAttribute)
            {
                attributes_count = _reader.ReadUInt32();

                for (int i = 0; i < attributes_count; i++)
                {
                    uint           name_length = _reader.ReadUInt32();
                    string         name        = _reader.ReadString(name_length, true);
                    NetCDFDataType data_type   = (NetCDFDataType)_reader.ReadInt32();

                    uint size = _reader.ReadUInt32();
                    switch (data_type)
                    {
                    case NetCDFDataType.NcByte:
                        byte[] bytes = _reader.ReadBytes(size, true);
                        attributes.Add(name,
                                       new NetCDFAttribute <byte>(name, bytes, data_type));
                        break;

                    case NetCDFDataType.NcChar:
                        string   chars = _reader.ReadString(size, true);
                        string[] str   = new string[1];
                        str[0] = chars;
                        attributes.Add(name,
                                       new NetCDFAttribute <string>(name, str, data_type));
                        break;

                    case NetCDFDataType.NcShort:
                        short[] shorts = _reader.ReadShorts(size, true);
                        attributes.Add(name,
                                       new NetCDFAttribute <short>(name, shorts, data_type));
                        break;

                    case NetCDFDataType.NcInt:
                        int[] integers = _reader.ReadIntegers(size);
                        attributes.Add(name,
                                       new NetCDFAttribute <int>(name, integers, data_type));
                        break;

                    case NetCDFDataType.NcFloat:
                        float[] floats = _reader.ReadSingles(size);
                        attributes.Add(name,
                                       new NetCDFAttribute <float>(name, floats, data_type));
                        break;

                    case NetCDFDataType.NcDouble:
                        double[] doubles = _reader.ReadDoubles(size);
                        attributes.Add(name,
                                       new NetCDFAttribute <double>(name, doubles, data_type));
                        break;
                    }
                }
            }
            else if (metadata_type_flag == 0)
            {
                // read the next 4 bytes to position the pointer at the start
                // of the variables array.
                attributes_count = (uint)_reader.ReadInt32();

                // If this condition is met, raise a parser exception.
                if (attributes_count != 0)
                {
                    string message =
                        String.Format("Expected ZERO value for attribute count. But actual value is {0}",
                                      attributes_count);
                    throw new ParserException(message);
                }
            }
            // If we reach here, raise a parser exception.
            else
            {
                string message = "Attribute metadata type token expected, but not found.";
                throw new ParserException(message);
            }
            return(attributes_count);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Reads metadata of the variables defined in the NetCDF file.
        /// </summary>
        /// <exception cref="Microsoft.NetCDF.CSharpAPI.ParserException"></exception>
        /// <returns>Count of variables</returns>
        private uint ReadVariables()
        {
            int  metadata_type_flag = _reader.ReadInt32();
            uint variables_count    = 0;

            if ((NetCDFMetadataType)metadata_type_flag == NetCDFMetadataType.NcVariable)
            {
                variables_count = _reader.ReadUInt32();

                for (uint i = 0; i < variables_count; i++)
                {
                    uint   name_length    = _reader.ReadUInt32();
                    string name           = _reader.ReadString(name_length, true);
                    uint   dimensionality = _reader.ReadUInt32();
                    uint[] dimension_ids  = new uint[dimensionality];

                    for (uint j = 0; j < dimensionality; j++)
                    {
                        dimension_ids[j] = _reader.ReadUInt32();
                    }

                    bool is_record_variable = this.Dimensions[dimension_ids[0]].IsRecordDimension;
                    Dictionary <string, INetCDFAttribute> variable_attributes =
                        new Dictionary <string, INetCDFAttribute>();
                    this.ReadAttributes(variable_attributes);
                    NetCDFDataType data_type     = (NetCDFDataType)_reader.ReadInt32();
                    uint           variable_size = _reader.ReadUInt32();
                    _recordSize += is_record_variable ? (int)variable_size : 0;
                    int data_offset = _reader.ReadInt32();

                    switch (data_type)
                    {
                    case NetCDFDataType.NcByte:
                        NetCDFVariable <byte> var_bytes =
                            new NetCDFVariable <byte>(name,
                                                      dimension_ids, variable_attributes,
                                                      data_type, variable_size, data_offset,
                                                      is_record_variable);
                        this.variables.Add(name, var_bytes);
                        break;

                    case NetCDFDataType.NcChar:
                        NetCDFVariable <char> var_chars =
                            new NetCDFVariable <char>(name,
                                                      dimension_ids, variable_attributes,
                                                      data_type, variable_size, data_offset,
                                                      is_record_variable);
                        this.variables.Add(name, var_chars);
                        break;

                    case NetCDFDataType.NcShort:
                        NetCDFVariable <short> var_shorts =
                            new NetCDFVariable <short>(name,
                                                       dimension_ids, variable_attributes,
                                                       data_type, variable_size, data_offset,
                                                       is_record_variable);
                        this.variables.Add(name, var_shorts);
                        break;

                    case NetCDFDataType.NcInt:
                        NetCDFVariable <int> var_ints =
                            new NetCDFVariable <int>(name,
                                                     dimension_ids, variable_attributes,
                                                     data_type, variable_size, data_offset,
                                                     is_record_variable);
                        this.variables.Add(name, var_ints);
                        break;

                    case NetCDFDataType.NcFloat:
                        NetCDFVariable <float> var_floats =
                            new NetCDFVariable <float>(name,
                                                       dimension_ids, variable_attributes,
                                                       data_type, variable_size, data_offset,
                                                       is_record_variable);
                        this.variables.Add(name, var_floats);
                        break;

                    case NetCDFDataType.NcDouble:
                        NetCDFVariable <double> var_doubles =
                            new NetCDFVariable <double>(name,
                                                        dimension_ids, variable_attributes,
                                                        data_type, variable_size, data_offset,
                                                        is_record_variable);
                        this.variables.Add(name, var_doubles);
                        break;
                    }
                }
            }
            else if (metadata_type_flag == 0)
            {
                // read the next 4 bytes to position the pointer at the start
                // of the data section.
                variables_count = (uint)_reader.ReadInt32();

                // If this condition is met, raise a parser exception.
                if (variables_count != 0)
                {
                    string message =
                        String.Format("Expected ZERO value for variables count. But actual value is {0}",
                                      variables_count);
                    throw new ParserException(message);
                }
            }
            // If we reach here, raise a parser exception.
            else
            {
                string message = "Variable metadata type token expected, but not found.";
                throw new ParserException(message);
            }
            return(variables_count);
        }
 public NetCDFAttribute(string name, T[] values, NetCDFDataType dataType)
 {
     this._values   = values;
     this._name     = name;
     this._dataType = dataType;
 }