/// <summary> /// Opens the file for writing. /// </summary> /// <returns>The opened file.</returns> /// <param name="Path">Path to the output file.</param> /// <param name="Headers">FITS Headers to be written to the file.</param> public static MMapFitsFile OpenWriteFile(string Path, HeaderTable Headers) { FileInfo info = new FileInfo(Path); int HLength = ((Headers.Count * 80) + 2879) / 2880 * 2880; int DLength = HeaderIO.ComputeDataArrayLength(Headers) + 2879; DLength = DLength / 2880 * 2880; int FLength = HLength + DLength; MemoryMappedFile mmap = MemoryMappedFile.CreateFromFile(Path, FileMode.Create, Guid.NewGuid().ToString(), FLength, MemoryMappedFileAccess.ReadWrite); Stream s = mmap.CreateViewStream(); FitsFileBuilder builder = new FitsFileBuilder() { PrimaryTable = new HeaderTable() }; foreach (var w in Headers) { builder.PrimaryTable.Add(w.Key, w.Value); s.Write(((FITSMetadataRecord)w.Value).ToRawRecord(), 0, 80); } s.Write(Encoding.UTF8.GetBytes("END".PadRight(80)), 0, 80); while (s.Position < HLength) { s.Write(Encoding.UTF8.GetBytes(string.Empty.PadRight(80)), 0, 80); } builder.PrimaryDataPointer = HLength; s.Dispose(); return(new MMapFitsFile(Path, true, null, builder, mmap, MemoryMappedFileAccess.ReadWrite)); }
/// <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> /// 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> /// 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; }
/// <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; }
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; }