Exemplo n.º 1
0
 public NetCDFFile(NetCDFReader reader, HyperCube cube)
 {
     this.reader   = reader;
     this.cube     = cube;
     this.metaData = NetCDFFile.GetMetaData(reader);
 }
Exemplo n.º 2
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);
        }
        public static void CreateFile(string fileName, NetCDFFile ncFile)
        {
            NetCDFWriter.dataCube  = null;
            NetCDFWriter.fs        = null;
            NetCDFWriter.offsetDic = null;
            NetCDFWriter.reader    = null;
            NetCDFWriter.writer    = null;

            using (NetCDFWriter.fs = new FileStream(fileName, FileMode.Create))
            {
                NetCDFWriter.offsetDic = new Dictionary <string, int>();
                NetCDFWriter.writer    = new BinaryWriter(fs);
                NetCDFWriter.reader    = ncFile.NetCDFMetadata;
                NetCDFWriter.dataCube  = ncFile.HyperCube;

                int fileFormat = 0;
                if (reader is ClassicNetCDFFileReader)
                {
                    fileFormat = 1;
                }
                else if (reader is NetCDF64BitOffsetFileReader)
                {
                    fileFormat = 2;
                }

                WriteFileHeader(fileFormat);  //Magic number

                //get the record count
                int recCount = (int)reader.NumberOfRecords;

                WriteRecordCount(recCount);
                //write dimension number
                int dimCount = (int)reader.NumberOfDimensions;
                //int dimCount = ncMD.Dimensions.Count;

                WriteDimensionHeader(dimCount);

                Dictionary <uint, INetCDFDimension> .Enumerator dimEnumerator
                    = reader.Dimensions.GetEnumerator();
                while (dimEnumerator.MoveNext())
                {
                    WriteDimension(dimEnumerator.Current.Value.Name, (int)dimEnumerator.Current.Value.Length);
                }
                //NcDim recDim = null;  //THIS is required while seperating the records variable and no record variable writing below
                //foreach (NcDim dim in ncMD.Dimensions)
                //{
                //    WriteDimension(dim.Name, dim.Size);   //SIZE == LENGTH ?? Check
                //    //if(dim.IsUnlimited)
                //    //    recDim = dim;
                //}

                #region Ignoring Global Attributes

                WriteAttributeHeader(0);

                ////WriteAttributeHeader((int)reader.NumberOfGlobalAttributes);
                //WriteAttributeHeader(ncMD.GlobalAttributes.Count);
                ////Dictionary<string, INetCDFAttribute>.Enumerator attrEnumerator
                ////= reader.Attributes.GetEnumerator();
                ////while (attrEnumerator.MoveNext())
                ////{
                ////    int attType = (int)attrEnumerator.Current.Value.DataType;
                ////    string attName = attrEnumerator.Current.Key; //Get Attname here
                ////    object[] attValue = new object[] { attrEnumerator.Current.Value.Value };
                ////    WriteAttribute(attType, attName, attValue);
                ////
                ////}
                //foreach (NcAtt gAtt in ncMD.GlobalAttributes)
                //{
                //    NcType type = gAtt.Type;
                //    WriteAttribute(type, gAtt.Name, gAtt.Values);
                //}

                #endregion

                #region Commented : Writing All the Variables defined in Reader

                //WriteVariableHeader((int)reader.NumberOfVariables);

                //Dictionary<string, INetCDFVariable>.Enumerator varEnumerator
                //= reader.Variables.GetEnumerator();

                //#region Write Variable MetaData
                //while (varEnumerator.MoveNext())
                //{
                //    WriteVariable(varEnumerator.Current.Value);
                //}
                //#endregion

                //#region write variable values

                //varEnumerator = reader.Variables.GetEnumerator();

                //while (varEnumerator.MoveNext())
                //{
                //    string variableName = varEnumerator.Current.Value.Name;
                //    int numval = (int)varEnumerator.Current.Value.NumValues;
                //    int variableOffset = offsetDic[variableName];
                //    object[] variableValue = ReadCubeData(variableName, numval);
                //    WriteVariableValues(varEnumerator.Current.Value.DataType, variableOffset, variableName, variableValue);

                //}
                //#endregion
                #endregion

                #region Writing variables which are defined in HyperCube

                WriteVariableHeader(dataCube.Schema.ColumnCount);
                //WriteVariableHeader(ncMD.Variables.Count);   //Add variable count


                string columnname = string.Empty;
                List <INetCDFVariable> varlist = new List <INetCDFVariable>();

                for (int index = 0; index < dataCube.Schema.ColumnCount; index++)
                {
                    columnname = dataCube.Schema.GetColumnName(index);
                    INetCDFVariable myvariable = GetNetCDFvariable(columnname);
                    if (null != myvariable)
                    {
                        varlist.Add(myvariable);
                    }
                }

                //Write metadata for All variables
                foreach (INetCDFVariable myvar in varlist)
                {
                    WriteVariable(myvar);
                }

                #region write variable values

                //First write non record variables
                foreach (INetCDFVariable myVariable1 in varlist)
                {
                    if (!myVariable1.IsRecordVariable)
                    {
                        int      variableOffset = offsetDic[myVariable1.Name];
                        object[] variableValue  = ReadCubeNonRecordData(myVariable1);
                        int      recSize        = (int)myVariable1.Size;
                        WriteVariableValues(myVariable1.DataType, variableOffset, myVariable1.Name, recSize, variableValue);
                    }
                }

                //Write Data for Record Variables
                for (int r = 0; r < (int)reader.NumberOfRecords; r++)
                {
                    foreach (INetCDFVariable myVariable1 in varlist)
                    {
                        if (myVariable1.IsRecordVariable)
                        {
                            int variableOffset = offsetDic[myVariable1.Name];
                            if (r > 0)
                            {
                                variableOffset = -1;
                                // means no need to update the offset for next record of the same variable
                                // This is handled in WriteVariableValues() function called below
                            }

                            //First Dimension is the Record Dimension
                            //uint recordDimensionIndex = (uint)myVariable1.DimensionIDs.GetValue(0);

                            object[] variableValue = ReadCubeRecordData(myVariable1, ncFile, r);
                            int      recSize       = (int)(myVariable1.Size);
                            WriteVariableValues(myVariable1.DataType, variableOffset, myVariable1.Name, recSize, variableValue);
                        }
                    }
                }
                #endregion

                #endregion

                NetCDFWriter.Close();
            }// end of using
        }