private static void WriteVariable(INetCDFVariable variable) { //Number of Characters in variable's name WriteInteger(variable.Name.Length); //write variable name WriteString(variable.Name); //Write dimension count of the variable WriteInteger(variable.DimensionIDs.Length); for (uint i = 0; i < variable.DimensionIDs.Length; i++) { WriteInteger((int)variable.DimensionIDs[i]); } #region IGNORING Variable Attribute Info WriteAttributeHeader(0); //WriteAttributeHeader(variable.Attributes.Count); //Dictionary<string, INetCDFAttribute>.Enumerator attributeEnumerator //= variable.Attributes.GetEnumerator(); //while (attributeEnumerator.MoveNext()) //{ // //int attType = (int)attributeEnumerator.Current.Value.DataType; // string attName = attributeEnumerator.Current.Key; // object[] attValue = new object[] { attributeEnumerator.Current.Value.Value }; // WriteAttribute(attType, attName, attValue); //} #endregion //write variable type WriteInteger((int)variable.DataType); //this is the position at which the nect four bytes contains // the bytes used by the variable and the offset value. This needs //to be updated everytime the new records is added to the file int variablePosition = (int)writer.BaseStream.Length; //Store this position in a structure for possible future reference offsetDic.Add(variable.Name, variablePosition); //write next 8 bytes with zero values of variable size and variable offset values //DONT REMOVE THIS TWO LINES. WriteInteger(0); //size WriteInteger(0); //offset value in file for this variable }
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 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 }
public override IEnumerable <decimal> ReadDecimalVariable(string name) { IEnumerable <decimal> values = null; INetCDFVariable var = this.Variables[name]; int offset = var.DataOffset; uint dimension_product = GetDimensionProduct(var.DimensionIDs); _reader.BaseStream.Position = offset; uint length = var.Size; switch (var.DataType) { case NetCDFDataType.NcFloat: //if (typeof(T) == typeof(float)) { values = _reader.ReadSingles(length / sizeof(float)).Select((v) => new decimal(v)); } break; case NetCDFDataType.NcDouble: //if (typeof(T) == typeof(double)) { values = _reader.ReadDoubles(length / sizeof(double)).Select((v) => new decimal(v)); } break; //case NetCDFDataType.NcChar: // if (typeof(T) == typeof(char)) // { // values = _reader.ReadChars((int)length / sizeof(char)) as T[]; // } // break; case NetCDFDataType.NcByte: //if (typeof(T) == typeof(byte)) { if (length == dimension_product * sizeof(byte)) { values = _reader.ReadBytes(length / sizeof(byte), false).Select((v) => new decimal(v)); } else { values = _reader.ReadBytes(dimension_product, true).Select((v) => new decimal(v)); } } break; case NetCDFDataType.NcInt: //if (typeof(T) == typeof(int)) { values = _reader.ReadIntegers(length / sizeof(int)).Select((v) => new decimal(v)); } break; case NetCDFDataType.NcShort: //if (typeof(T) == typeof(short)) { if (length == dimension_product * sizeof(byte)) { //No Padding values = _reader.ReadShorts(length / sizeof(short), false).Select((v) => new decimal(v)); } else { //Apply Padding values = _reader.ReadShorts(dimension_product, true).Select((v) => new decimal(v)); } } break; } return(values); }
/// <summary> /// Fetches the data values for a record variable. /// </summary> /// <typeparam name="T">Any of float, int, double, char, byte or short</typeparam> /// <param name="name">Name of a non-record variable</param> /// <param name="recordIndex">Index into the Record data</param> /// <returns>Array of data values of the specified type</returns> public override T[] ReadVariable <T>(string name, uint recordIndex) { T[] values = default(T[]); INetCDFVariable var = this.Variables[name]; int offset = var.DataOffset; uint dimension_product = GetDimensionProduct(var.DimensionIDs); _reader.BaseStream.Position = var.DataOffset + this._recordSize * recordIndex; switch (var.DataType) { case NetCDFDataType.NcFloat: if (typeof(T) == typeof(float)) { values = _reader.ReadSingles(dimension_product) as T[]; } break; case NetCDFDataType.NcDouble: if (typeof(T) == typeof(double)) { values = _reader.ReadDoubles(dimension_product) as T[]; } break; case NetCDFDataType.NcChar: if (typeof(T) == typeof(char)) { values = _reader.ReadChars((int)dimension_product) as T[]; } break; case NetCDFDataType.NcByte: if (typeof(T) == typeof(byte)) { if (((dimension_product * sizeof(byte)) % 4) == 0) { values = _reader.ReadBytes(dimension_product, false) as T[]; } else { values = _reader.ReadBytes(dimension_product, true) as T[]; } } break; case NetCDFDataType.NcInt: if (typeof(T) == typeof(int)) { values = _reader.ReadIntegers(dimension_product) as T[]; } break; case NetCDFDataType.NcShort: if (typeof(T) == typeof(short)) { if (((dimension_product * sizeof(short)) % 4) == 0) { values = _reader.ReadShorts(dimension_product, false) as T[]; } else { values = _reader.ReadShorts(dimension_product, true) as T[]; } } break; } return(values); }
/// <summary> /// Fetches the data values for a non-record variable. /// </summary> /// <typeparam name="T">Any of float, int, double, char, byte or short </typeparam> /// <param name="name">Name of a non-record variable</param> /// <returns>Array of data values of the specified type</returns> public override T[] ReadVariable <T>(string name) { T[] values = default(T[]); INetCDFVariable var = this.Variables[name]; int offset = var.DataOffset; uint dimension_product = GetDimensionProduct(var.DimensionIDs); _reader.BaseStream.Position = offset; uint length = var.Size; switch (var.DataType) { case NetCDFDataType.NcFloat: if (typeof(T) == typeof(float)) { values = _reader.ReadSingles( length / sizeof(float)) as T[]; } break; case NetCDFDataType.NcDouble: if (typeof(T) == typeof(double)) { values = _reader.ReadDoubles( length / sizeof(double)) as T[]; } break; case NetCDFDataType.NcChar: if (typeof(T) == typeof(char)) { values = _reader.ReadChars((int)length / sizeof(char)) as T[]; } break; case NetCDFDataType.NcByte: if (typeof(T) == typeof(byte)) { if (length == dimension_product * sizeof(byte)) { values = _reader.ReadBytes(length / sizeof(byte), false) as T[]; } else { values = _reader.ReadBytes(dimension_product, true) as T[]; } } break; case NetCDFDataType.NcInt: if (typeof(T) == typeof(int)) { values = _reader.ReadIntegers( length / sizeof(int)) as T[]; } break; case NetCDFDataType.NcShort: if (typeof(T) == typeof(short)) { if (length == dimension_product * sizeof(byte)) { //No Padding values = _reader.ReadShorts(length / sizeof(short), false) as T[]; } else { //Apply Padding values = _reader.ReadShorts(dimension_product, true) as T[]; } } break; } return(values); }