Пример #1
0
        /// <summary>
        /// This will return the variable values arrays
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public object ReadVariableValue(string name)
        {
            try
            {
                NcVar var = GetVar(name);
                if (var != null)
                {
                    // Get the number of values of the variables, This would be multiply of each dimension of the variable
                    int      numrecs = (int)var.NumValues;
                    object[] values  = new object[numrecs];
                    fs.Seek(var.Offset, SeekOrigin.Begin);
                    for (int i = 0; i < numrecs; ++i)
                    {
                        values[i] = ReadValue(var.Type);
                    }
                    return((object)values);
                }
            }
            catch (Exception)
            {
                // Do nothing.
            }

            return(null); // if variable of the name provided is not found then return null
        }
Пример #2
0
        /// <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);
            }
        }
Пример #3
0
        /// <summary>
        /// Reads one value for the variable from the nc file.
        /// </summary>
        /// <param name="varName">variable for which value is to be read</param>
        /// <param name="dimNames">name of the dimensions</param>
        /// <param name="dimValues">index for the dimension value. The indexes match with the
        /// dimNames array</param>
        /// <returns></returns>
        public object ReadVariableValue(string varName, string[] dimNames, int[] dimValues)
        {
            try
            {
                NcVar var = GetVar(varName);
                if (null == var)
                {
                    return(null);
                }

                // We want to get the dimension values for dimenstions which exist for the
                // variable.
                int[] dimIndexValArray = new int[var.Dimensions.Count];

                // Check if the value requested is within the dimensions. If the value
                // requested is for a dimension for which this variable does not vary
                // then return null.
                for (int i = 0; i < dimNames.Length; ++i)
                {
                    bool found = false;
                    for (int j = 0; j < var.Dimensions.Count; ++j)
                    {
                        // If the dimension exists move to the next
                        if (dimNames[i] == var.Dimensions[j].Name)
                        {
                            found = true;
                            dimIndexValArray[j] = dimValues[i];
                            break;
                        }
                    }
                    // If the dimension does not exist for the variable and a value is
                    // requested for this dimension then return null
                    if (!found && dimValues[i] > 0)
                    {
                        return(null);
                    }
                }

                if (null == var.ValueArr)
                {
                    object[] values = new object[var.NumValues];
                    fs.Seek(var.Offset, SeekOrigin.Begin);
                    for (int i = 0; i < var.NumValues; ++i)
                    {
                        values[i] = ReadValue(var.Type);
                    }
                    var.ValueArr = values;
                }

                return(var.GetValue(ref dimIndexValArray));
            }
            catch (Exception)
            {
            }
            return(null);
        }
Пример #4
0
        private bool IfExists(NcVar var)
        {
            foreach (NcVar myvar in this.variableList)
            {
                if (string.Compare(myvar.Name, var.Name) == 0)
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #5
0
        /// <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);
        }
Пример #6
0
        /// <summary>
        /// Reads the variables and add them to the meta data
        /// </summary>
        /// <param name="count"></param>
        private void ReadVariables(int count)
        {
            for (int i = 0; i < count; ++i)
            {
                string str          = ReadBytes(0, 4);
                int    stringLength = ReadInteger(str); //get then string lenght of the variable name
                str = ReadStringName(stringLength);

                NcVar var = new NcVar(str);

                //Now read the dimension of the variable
                str = ReadBytes(0, 4);
                int        dimCount = ReadInteger(str);
                List <int> dimList  = new List <int>();
                for (int dCounter = 0; dCounter < dimCount; ++dCounter)
                {
                    // Read the ID of the dimension. This indicates the
                    // sequence in which the dimensions are declared and that is
                    // used for this variable.
                    str = ReadBytes(0, 4);
                    int dimID = ReadInteger(str);
                    dimList.Add(dimID);
                    //If variable has only one dimension , check for co-ordinate variable
                    if (dimCount == 1)
                    {
                        //If Variable Name is same as name of Dimension
                        if (this.metadata.GetDimension(var.Name) != null)
                        {
                            var.IsCoordinate = true;
                        }
                    }
                    var.SetDimensions(dCounter, this.metadata.Dimensions[dimID]);
                }
                var.Edges = dimList;

                int varAttCount = ReadVariableAttributeCount();  // Get the variable's attribute count

                if (varAttCount > 0)
                {
                    ReadVariableAttributes(varAttCount, ref var);
                }

                // After reading all the attribs read the type, size and offset
                stringLength = ReadInteger(ReadBytes(0, 4));
                switch (stringLength)
                {
                case 1: var.Type = NcType.NcByte; break;

                case 2: var.Type = NcType.NcChar; break;

                case 3: var.Type = NcType.NcShort; break;

                case 4: var.Type = NcType.NcInt; break;

                case 5: var.Type = NcType.NcFloat; break;

                case 6: var.Type = NcType.NcDouble; break;
                }
                int varSize = ReadInteger(ReadBytes(0, 4));
                int offset  = ReadInteger(ReadBytes(0, 4));
                var.Offset = offset;
                int bCount = BytesOf(var.Type);
                if (bCount == 0)
                {
                    var.NumValues = 0;
                }
                else
                {
                    var.NumValues = varSize / BytesOf(var.Type);
                }
                // If the variable is a record variable then set the length of the
                // record var
                var.SetRecordDimSize();

                this.metadata.AddVariable(var);
            }
        }
Пример #7
0
 public void AddVariable(NcVar var)
 {
     variableList.Add(var);
 }