private static object[] ReadCubeRecordData(INetCDFVariable variable, NetCDFFile ncFile, int recordCounter)
        {
            int colCount = dataCube.Schema.ColumnCount;

            object[] data     = new object[(int)variable.NumValues];
            int      colindex = -1;

            try
            {
                colindex = dataCube.Schema.LookupColumnIndex(variable.Name);
            }
            catch //Eat Exception
            {
                return(null);
            }
            try
            {
                int r = recordCounter;
                //for (int r = 0; r < recordCounter; r++)
                {
                    int counter = 0;

                    int xLen = 1; // dataCube.GetAxisLength(HyperCube.Axis.X);  //default lenght will be 1
                    int yLen = 1; // dataCube.GetAxisLength(HyperCube.Axis.Y);
                    int zLen = 1; // dataCube.GetAxisLength(HyperCube.Axis.Z);

                    for (uint ii = 0; ii < variable.DimensionIDs.Length; ii++)
                    {
                        uint dimId = variable.DimensionIDs[ii];

                        if (dataCube.GetDimensionName(HyperCube.Axis.X) == reader.Dimensions[dimId].Name)
                        {
                            xLen = (int)reader.Dimensions[dimId].Length;
                        }
                        else if (dataCube.GetDimensionName(HyperCube.Axis.Y) == reader.Dimensions[dimId].Name)
                        {
                            yLen = (int)reader.Dimensions[dimId].Length;
                        }
                        else if (dataCube.GetDimensionName(HyperCube.Axis.Z) == reader.Dimensions[dimId].Name)
                        {
                            zLen = (int)reader.Dimensions[dimId].Length;
                        }
                    }

                    //Ignore X axis here
                    for (int j = 0; j < xLen; j++)
                    {
                        for (int k = 0; k < yLen; k++)
                        {
                            for (int m = 0; m < zLen; m++)
                            {
                                object val = (object)dataCube[j, k, m, r][colindex];
                                if (val != null)
                                {
                                    data[counter] = val;
                                    counter++;
                                }
                            }
                        }
                    }
                }

                return(data);
            }
            catch (Exception)
            {
                return(null);
            }
        }
 public NetCDFFile(NetCDFReader reader, HyperCube cube)
 {
     this.reader   = reader;
     this.cube     = cube;
     this.metaData = NetCDFFile.GetMetaData(reader);
 }
        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
        }