/// <summary> /// Opens a FITS File handle from a file on a local disk. /// </summary> /// <param name="Path">Path to where the image is stored.</param> /// <param name="OutputImage">Specifies whether the image is an input or an output one.</param> /// <param name="numberGetter">Delegate that generates the image numbers in a MEF FITS.</param> private MMapFitsFile(string Path, bool OutputImage, MEFImageNumberGetter numberGetter, FitsFileBuilder Headers, MemoryMappedFile Handle, MemoryMappedFileAccess Access) : base(Path, OutputImage, numberGetter, Headers) { OpenViews = new Dictionary <IntPtr, MemoryMappedViewAccessor>(); mmap = Handle; access = Access; }
protected FitsFile(string Path, bool OutputImage, MEFImageNumberGetter numberGetter, FitsFileBuilder Headers) { OutputFile = OutputImage; this.Path = Path; PrimaryTable = Headers.PrimaryTable; PrimaryDataPointer = Headers.PrimaryDataPointer; ExtensionDataPointers = Headers.ExtensionDataPointers; MEFHeaderTable = Headers.MEFHeaderTable; MEFDataPointers = Headers.MEFDataPointers; }
/// <summary> /// Open file for reading. /// </summary> /// <returns>The opened file.</returns> /// <param name="Path">Path to the file.</param> /// <param name="numberGetter">Delegate that generates the image numbers in a MEF FITS.</param> public static MMapFitsFile OpenReadFile(string Path, MEFImageNumberGetter numberGetter = null) { FileInfo info = new FileInfo(Path); MemoryMappedFile mmap = MemoryMappedFile.CreateFromFile(Path, FileMode.Open, Guid.NewGuid().ToString(), 0, MemoryMappedFileAccess.Read); Stream stream = mmap.CreateViewStream(0, 0, MemoryMappedFileAccess.Read); FitsFileBuilder builder = HeaderIO.ReadFileHeaders(stream, info.Length, numberGetter); stream.Dispose(); return(new MMapFitsFile(Path, false, numberGetter, builder, mmap, MemoryMappedFileAccess.Read)); }
/// <summary> /// Opens a file from the given stream. /// </summary> /// <returns>The opened file.</returns> /// <param name="str">Input stream.</param> /// <param name="Length">Data length.</param> /// <param name="Path">Path to the data.</param> /// <param name="numberGetter">MEF naming policy.</param> public static NSStreamFitsFile OpenFile(Stream str, int Length, string Path, MEFImageNumberGetter numberGetter = null) { byte[] Data = new byte[Length]; str.Read(Data, 0, Data.Length); FitsFileBuilder Headers; using (MemoryStream ms = new MemoryStream(Data)) Headers = HeaderIO.ReadFileHeaders(ms, Length, numberGetter); return(new NSStreamFitsFile(Data, Path, false, numberGetter, Headers)); }
/// <summary> /// Reads the FITS headers from a stream. /// </summary> /// <param name="stream">Input stream.</param> /// <param name="Length">Expected stream length.</param> /// <param name="numberGetter">The function which assigns image numbers to FITS images in file.</param> /// <returns>A FitsFileBuilder containing the information from the headers.</returns> internal static FitsFileBuilder ReadFileHeaders(Stream stream, long Length, MEFImageNumberGetter numberGetter) { FitsFileBuilder builder = new FitsFileBuilder(); var R1 = ReadHeaderFromStream(stream, Length); builder.PrimaryHeader = R1.Item1; builder.PrimaryTable = R1.Item2; /* Setup primary data array. Start looking for extensions. */ builder.PrimaryDataPointer = (int)stream.Position; int ALength = ComputeDataArrayLength(builder.PrimaryTable); /* Align to 2880 */ int AALength = (ALength + 2879) / 2880 * 2880; long NewPosition = stream.Position + AALength; if (NewPosition > Length) { throw new FITSFormatException("Data array longer than file length."); } stream.Position = NewPosition; /* Check if file is MEF. */ if (stream.Position != Length) { if (numberGetter == null) { numberGetter = DefaultGetter; } builder.ExtensionHeaders = new List <List <MetadataRecord> >(); builder.ExtensionDataPointers = new List <int>(); builder.MEFDataPointers = new Dictionary <int, int>(); builder.MEFImagesHeaders = new Dictionary <int, List <MetadataRecord> >(); builder.MEFHeaderTable = new Dictionary <int, HeaderTable>(); /* Read extension headers. */ for (int exn = 0; stream.Position < Length; exn++) { var R2 = ReadHeaderFromStream(stream, Length); int Loc = (int)stream.Position; ALength = ComputeDataArrayLength(R2.Item2); builder.ExtensionHeaders.Add(R2.Item1); builder.ExtensionDataPointers.Add(Loc); MetadataRecord img; try { img = R2.Item2["XTENSION"]; } catch (Exception ex) { throw new FITSFormatException("Extension headers do not follow standard.", ex); } if (img.AsString == "IMAGE ") { int ImNr = numberGetter(exn, R2.Item2); builder.MEFImagesHeaders.Add(ImNr, R2.Item1); builder.MEFHeaderTable.Add(ImNr, R2.Item2); builder.MEFDataPointers.Add(ImNr, Loc); } AALength = (ALength + 2879) / 2880 * 2880; NewPosition = stream.Position + AALength; if (NewPosition > Length) { throw new FITSFormatException("Data array longer than file length."); } stream.Position = NewPosition; } } return(builder); }
/// <summary>Wrapper for underlying constructor.</summary> protected NSStreamFitsFile(byte[] Data, string Path, bool OutputImage, MEFImageNumberGetter numberGetter, FitsFileBuilder Headers) : base(Path, OutputImage, numberGetter, Headers) { this.Data = Data; }