/// <summary> /// Imports a Galactic SPC file into a x and an y array. The file must not be a multi spectrum file (an exception is thrown in this case). /// </summary> /// <param name="xvalues">The x values of the spectrum.</param> /// <param name="yvalues">The y values of the spectrum.</param> /// <param name="filename">The filename where to import from.</param> /// <returns>Null if successful, otherwise an error description.</returns> public static string ToArrays(string filename, out double [] xvalues, out double [] yvalues) { System.IO.Stream stream=null; SPCHDR hdr = new SPCHDR(); SUBHDR subhdr = new SUBHDR(); try { stream = new System.IO.FileStream(filename,System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.Read); System.IO.BinaryReader binreader = new System.IO.BinaryReader(stream); hdr.ftflgs = binreader.ReadByte(); // ftflgs : not-evenly spaced data hdr.fversn = binreader.ReadByte(); // fversn : new version hdr.fexper = binreader.ReadByte(); // fexper : general experimental technique hdr.fexp = binreader.ReadByte(); // fexp : fractional scaling exponent (0x80 for floating point) hdr.fnpts = binreader.ReadInt32(); // fnpts : number of points hdr.ffirst = binreader.ReadDouble(); // ffirst : first x-value hdr.flast = binreader.ReadDouble(); // flast : last x-value hdr.fnsub = binreader.ReadInt32(); // fnsub : 1 (one) subfile only binreader.ReadByte(); // Type of X axis units (see definitions below) binreader.ReadByte(); // Type of Y axis units (see definitions below) binreader.ReadByte(); // Type of Z axis units (see definitions below) binreader.ReadByte(); // Posting disposition (see GRAMSDDE.H) binreader.Read(new byte[0x1E0],0,0x1E0); // rest of SPC header // --------------------------------------------------------------------- // following the x-values array // --------------------------------------------------------------------- if(hdr.fversn!=0x4B) { if(hdr.fversn==0x4D) throw new System.FormatException(string.Format("This SPC file has the old format version of {0}, the only version supported here is the new version {1}",hdr.fversn,0x4B)); else throw new System.FormatException(string.Format("This SPC file has a version of {0}, the only version recognized here is {1}",hdr.fversn,0x4B)); } if(0!=(hdr.ftflgs & 0x80)) { xvalues = new double[hdr.fnpts]; for(int i=0;i<hdr.fnpts;i++) xvalues[i] = binreader.ReadSingle(); } else if(0==hdr.ftflgs) // evenly spaced data { xvalues = new double[hdr.fnpts]; for(int i=0;i<hdr.fnpts;i++) xvalues[i] = hdr.ffirst + i*(hdr.flast-hdr.ffirst)/(hdr.fnpts-1); } else { throw new System.FormatException("The SPC file must not be a multifile; only single file format is accepted!"); } // --------------------------------------------------------------------- // following the y SUBHEADER // --------------------------------------------------------------------- subhdr.subflgs = binreader.ReadByte(); // subflgs : always 0 subhdr.subexp = binreader.ReadByte(); // subexp : y-values scaling exponent (set to 0x80 means floating point representation) subhdr.subindx = binreader.ReadInt16(); // subindx : Integer index number of trace subfile (0=first) subhdr.subtime = binreader.ReadSingle(); // subtime; Floating time for trace (Z axis corrdinate) subhdr.subnext = binreader.ReadSingle(); // subnext; Floating time for next trace (May be same as beg) subhdr.subnois = binreader.ReadSingle(); // subnois; Floating peak pick noise level if high byte nonzero subhdr.subnpts = binreader.ReadInt32(); // subnpts; Integer number of subfile points for TXYXYS type subhdr.subscan = binreader.ReadInt32(); // subscan; Integer number of co-added scans or 0 (for collect) subhdr.subwlevel = binreader.ReadSingle(); // subwlevel; Floating W axis value (if fwplanes non-zero) subhdr.subresv = binreader.ReadInt32(); // subresv[4]; Reserved area (must be set to zero) // --------------------------------------------------------------------- // following the y-values array // --------------------------------------------------------------------- yvalues = new double[hdr.fnpts]; if(hdr.fexp==0x80) //floating point format { for(int i=0;i<hdr.fnpts;i++) yvalues[i] = binreader.ReadSingle(); } else // fixed exponent format { for(int i=0;i<hdr.fnpts;i++) yvalues[i] = binreader.ReadInt32()*Math.Pow(2,hdr.fexp-32); } } catch(Exception e) { xvalues = null; yvalues = null; return e.ToString(); } finally { if(null!=stream) stream.Close(); } return null; }
/// <summary> /// Imports a Galactic SPC file into a x and an y array. The file must not be a multi spectrum file (an exception is thrown in this case). /// </summary> /// <param name="xvalues">The x values of the spectrum.</param> /// <param name="yvalues">The y values of the spectrum.</param> /// <param name="filename">The filename where to import from.</param> /// <returns>Null if successful, otherwise an error description.</returns> public static string ToArrays(string filename, out double [] xvalues, out double [] yvalues) { System.IO.Stream stream = null; SPCHDR hdr = new SPCHDR(); SUBHDR subhdr = new SUBHDR(); try { stream = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read); System.IO.BinaryReader binreader = new System.IO.BinaryReader(stream); hdr.ftflgs = binreader.ReadByte(); // ftflgs : not-evenly spaced data hdr.fversn = binreader.ReadByte(); // fversn : new version hdr.fexper = binreader.ReadByte(); // fexper : general experimental technique hdr.fexp = binreader.ReadByte(); // fexp : fractional scaling exponent (0x80 for floating point) hdr.fnpts = binreader.ReadInt32(); // fnpts : number of points hdr.ffirst = binreader.ReadDouble(); // ffirst : first x-value hdr.flast = binreader.ReadDouble(); // flast : last x-value hdr.fnsub = binreader.ReadInt32(); // fnsub : 1 (one) subfile only binreader.ReadByte(); // Type of X axis units (see definitions below) binreader.ReadByte(); // Type of Y axis units (see definitions below) binreader.ReadByte(); // Type of Z axis units (see definitions below) binreader.ReadByte(); // Posting disposition (see GRAMSDDE.H) binreader.Read(new byte[0x1E0], 0, 0x1E0); // rest of SPC header // --------------------------------------------------------------------- // following the x-values array // --------------------------------------------------------------------- if (hdr.fversn != 0x4B) { if (hdr.fversn == 0x4D) { throw new System.FormatException(string.Format("This SPC file has the old format version of {0}, the only version supported here is the new version {1}", hdr.fversn, 0x4B)); } else { throw new System.FormatException(string.Format("This SPC file has a version of {0}, the only version recognized here is {1}", hdr.fversn, 0x4B)); } } if (0 != (hdr.ftflgs & 0x80)) { xvalues = new double[hdr.fnpts]; for (int i = 0; i < hdr.fnpts; i++) { xvalues[i] = binreader.ReadSingle(); } } else if (0 == hdr.ftflgs) // evenly spaced data { xvalues = new double[hdr.fnpts]; for (int i = 0; i < hdr.fnpts; i++) { xvalues[i] = hdr.ffirst + i * (hdr.flast - hdr.ffirst) / (hdr.fnpts - 1); } } else { throw new System.FormatException("The SPC file must not be a multifile; only single file format is accepted!"); } // --------------------------------------------------------------------- // following the y SUBHEADER // --------------------------------------------------------------------- subhdr.subflgs = binreader.ReadByte(); // subflgs : always 0 subhdr.subexp = binreader.ReadByte(); // subexp : y-values scaling exponent (set to 0x80 means floating point representation) subhdr.subindx = binreader.ReadInt16(); // subindx : Integer index number of trace subfile (0=first) subhdr.subtime = binreader.ReadSingle(); // subtime; Floating time for trace (Z axis corrdinate) subhdr.subnext = binreader.ReadSingle(); // subnext; Floating time for next trace (May be same as beg) subhdr.subnois = binreader.ReadSingle(); // subnois; Floating peak pick noise level if high byte nonzero subhdr.subnpts = binreader.ReadInt32(); // subnpts; Integer number of subfile points for TXYXYS type subhdr.subscan = binreader.ReadInt32(); // subscan; Integer number of co-added scans or 0 (for collect) subhdr.subwlevel = binreader.ReadSingle(); // subwlevel; Floating W axis value (if fwplanes non-zero) subhdr.subresv = binreader.ReadInt32(); // subresv[4]; Reserved area (must be set to zero) // --------------------------------------------------------------------- // following the y-values array // --------------------------------------------------------------------- yvalues = new double[hdr.fnpts]; if (hdr.fexp == 0x80) //floating point format { for (int i = 0; i < hdr.fnpts; i++) { yvalues[i] = binreader.ReadSingle(); } } else // fixed exponent format { for (int i = 0; i < hdr.fnpts; i++) { yvalues[i] = binreader.ReadInt32() * Math.Pow(2, hdr.fexp - 32); } } } catch (Exception e) { xvalues = null; yvalues = null; return(e.ToString()); } finally { if (null != stream) { stream.Close(); } } return(null); }