コード例 #1
0
        private FITSFileSequenceStream(string[] fitsFiles, IFITSTimeStampReader timeStampReader, int firstFrame, int?bitBix, int negPixCorrection)
        {
            m_FitsFilesList.AddRange(fitsFiles);

            var firstNumeredFrameNo = GetFirstImageNumberOfConsistentlyNamedFiles();

            if (firstFrame == 0 && firstNumeredFrameNo.HasValue)
            {
                firstFrame = firstNumeredFrameNo.Value;
            }

            FirstFrame         = firstFrame;
            LastFrame          = m_FitsFilesList.Count - 1 + firstFrame;
            CountFrames        = m_FitsFilesList.Count;
            m_TimeStampReader  = timeStampReader;
            m_NegPixCorrection = negPixCorrection;

            var fitsData = FITSHelper2.LoadFitsFile(m_FitsFilesList[0], m_TimeStampReader, m_NegPixCorrection);

            m_MinPixelValueFirstImage = fitsData.PixelStats.MinPixelValue;
            m_MaxPixelValueFirstImage = fitsData.PixelStats.MaxPixelValue;

            Width  = fitsData.Width;
            Height = fitsData.Height;
            BitPix = bitBix ?? fitsData.PixelStats.RawBitPix;

            HasUTCTimeStamps = fitsData.Timestamp.HasValue;

            VideoFileType = string.Format("FITS.{0}::SEQ", fitsData.PixelStats.RawBitPix);
        }
コード例 #2
0
        public static bool Load16BitFitsFile(string fileName, IFITSTimeStampReader timeStampReader, out uint[,] pixels, out uint medianValue, out Type pixelDataType, out bool hasNegativePixels, out short minRawValue, out uint maxValue, CheckOpenedFitsFileCallback callback, int negPixelCorrection = 0)
        {
            float exposureSeconds;

            return(LoadFitsFileInternal <uint>(fileName, timeStampReader, out pixels, out medianValue, out pixelDataType, out exposureSeconds, out hasNegativePixels, out minRawValue, out maxValue, callback,
                                               (Array dataArray, int height, int width, double bzero, out uint[,] ppx, out uint median, out Type dataType, out bool hasNegPix, out short minV, out uint maxV) =>
            {
                ppx = Load16BitImageData(dataArray, height, width, (int)bzero - negPixelCorrection, out median, out dataType, out hasNegPix, out minV, out maxV);
            }));
        }
コード例 #3
0
 private bool Load16BitFitsFile(string fileName, IFITSTimeStampReader timeStampReader,
                                out uint[,] pixels, out int width, out int height, out uint medianValue, out Type pixelDataType,
                                out bool hasNegativePixels, out short minValue, out uint maxValue,
                                FITSHelper.CheckOpenedFitsFileCallback callback)
 {
     return(Load16BitFitsFileImpl(fileName, timeStampReader,
                                  out pixels, out width, out height, out medianValue, out pixelDataType, out hasNegativePixels,
                                  out minValue, out maxValue,
                                  callback, null));
 }
コード例 #4
0
        public static FITSFileSequenceStream OpenFolder(string[] fitsFiles, IFITSTimeStampReader timeStampReader, int firstFrameNo, int?bitBix, int negPixCorrection)
        {
            UsageStats.Instance.ProcessedFitsFolderFiles++;
            UsageStats.Instance.Save();

            var rv = new FITSFileSequenceStream(fitsFiles, timeStampReader, firstFrameNo, bitBix, negPixCorrection);

            rv.FileName = Path.GetDirectoryName(fitsFiles[0]);
            return(rv);
        }
コード例 #5
0
 public static DateTime?ParseExposure(string fileName, Header header, IFITSTimeStampReader timeStampReader, out bool isMidPoint, out double?fitsExposure)
 {
     if (timeStampReader == null)
     {
         return(ParseExposureInternal(header, out isMidPoint, out fitsExposure));
     }
     else
     {
         return(timeStampReader.ParseExposure(fileName, header, out isMidPoint, out fitsExposure));
     }
 }
コード例 #6
0
 private bool Load16BitFitsFileWithNegativePixels(string fileName, IFITSTimeStampReader timeStampReader,
                                                  out uint[,] pixels, out int width, out int height, out uint medianValue, out Type pixelDataType,
                                                  out bool hasNegativePixels, out short minValue, out uint maxValue,
                                                  FITSHelper.CheckOpenedFitsFileCallback callback)
 {
     m_BelowZeroCorr = Math.Max(BZero, Math.Abs(MinPixelValue) - BZero);
     return(Load16BitFitsFileImpl(fileName, timeStampReader,
                                  out pixels, out width, out height, out medianValue, out pixelDataType, out hasNegativePixels,
                                  out minValue, out maxValue,
                                  callback, m_BelowZeroCorr));
 }
コード例 #7
0
        public static bool LoadFloatingPointFitsFile(string fileName, IFITSTimeStampReader timeStampReader, out float[,] pixels, out float medianValue, out Type pixelDataType, out float exposureSeconds, out bool hasNegativePixels, CheckOpenedFitsFileCallback callback)
        {
            short minRawValue_discarded;
            uint  maxVal_discarded;

            return(LoadFitsFileInternal <float>(
                       fileName, timeStampReader, out pixels, out medianValue, out pixelDataType, out exposureSeconds, out hasNegativePixels, out minRawValue_discarded, out maxVal_discarded, callback,
                       (Array dataArray, int height, int width, double bzero, out float[,] ppx, out float median, out Type dataType, out bool hasNegPix, out short minV, out uint maxV) =>
            {
                float minFV_discarded;
                float maxFV_discarded;
                ppx = LoadFloatImageData(dataArray, height, width, (float)bzero, out median, out dataType, out hasNegPix, out minFV_discarded, out maxFV_discarded);
                minV = 0;
                maxV = 0;
            }));
        }
コード例 #8
0
        private bool Load16BitFitsFileImpl(string fileName, IFITSTimeStampReader timeStampReader,
                                           out uint[,] pixels, out int width, out int height, out uint medianValue, out Type pixelDataType, out bool hasNegativePixels, out short minValue, out uint maxValue,
                                           FITSHelper.CheckOpenedFitsFileCallback callback, int?negPixelsBZero)
        {
            float frameExposure;

            width  = m_ImageHDU.Axes[WidthIndex];
            height = m_ImageHDU.Axes[HeightIndex];

            return(FITSHelper.LoadFitsDataInternal(
                       m_ImageHDU,
                       GetFirstFramePixelArray(), fileName, timeStampReader,
                       out pixels, out medianValue, out pixelDataType, out frameExposure, out hasNegativePixels, out minValue, out maxValue, callback,
                       (Array dataArray, int h, int w, double bzero, out uint[,] ppx, out uint median, out Type dataType, out bool hasNegPix, out short minV, out uint maxV) =>
            {
                ppx = FITSHelper.Load16BitImageData(dataArray, m_ImageHDU.Axes[HeightIndex], m_ImageHDU.Axes[WidthIndex], negPixelsBZero ?? (int)bzero, out median, out dataType, out hasNegPix, out minV, out maxV);
            }));
        }
コード例 #9
0
        private static bool LoadFitsFileInternal <TData>(
            string fileName, IFITSTimeStampReader timeStampReader,
            out TData[,] pixels, out TData medianValue, out Type pixelDataType, out float frameExposure, out bool hasNegativePixels, out short minRawValue, out uint maxVal,
            CheckOpenedFitsFileCallback callback, LoadFitsDataCallback <TData> loadDataCallback)
        {
            Fits fitsFile = new Fits();

            using (BufferedFile bf = new BufferedFile(fileName, FileAccess.Read, FileShare.ReadWrite))
            {
                fitsFile.Read(bf);

                BasicHDU imageHDU    = fitsFile.GetHDU(0);
                Array    pixelsArray = (Array)imageHDU.Data.DataArray;
                return(LoadFitsDataInternal <TData>(
                           imageHDU, pixelsArray, fileName, timeStampReader,
                           out pixels, out medianValue, out pixelDataType, out frameExposure, out hasNegativePixels, out minRawValue, out maxVal,
                           callback, loadDataCallback));
            }
        }
コード例 #10
0
        public static bool LoadFitsDataInternal <TData>(
            BasicHDU imageHDU, Array pixelsArray, string fileName, IFITSTimeStampReader timeStampReader,
            out TData[,] pixels, out TData medianValue, out Type pixelDataType, out float frameExposure, out bool hasNegativePixels, out short minRawValue, out uint maxVal,
            CheckOpenedFitsFileCallback callback, LoadFitsDataCallback <TData> loadDataCallback)
        {
            hasNegativePixels = false;

            if (callback != null && !(callback(imageHDU)))
            {
                pixels        = new TData[0, 0];
                medianValue   = default(TData);
                pixelDataType = typeof(TData);
                frameExposure = 0;
                minRawValue   = 0;
                maxVal        = 0;
                return(false);
            }

            int bzero = GetBZero(imageHDU);

            bool   isMidPoint;
            double?fitsExposure = null;

            try
            {
                ParseExposure(fileName, imageHDU.Header, timeStampReader, out isMidPoint, out fitsExposure);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.ToString());
            }
            frameExposure = fitsExposure.HasValue ? (float)fitsExposure.Value : 0;

            loadDataCallback(pixelsArray, imageHDU.Axes[0], imageHDU.Axes[1], bzero, out pixels, out medianValue, out pixelDataType, out hasNegativePixels, out minRawValue, out maxVal);

            return(true);
        }
コード例 #11
0
        public static void Load16BitFitsFile(
            string fileName, Load16BitFitsFileCallback loadFitsFileCallback, IFITSTimeStampReader timeStampReader, Action <BasicHDU> fitsFileLoadedCallback,
            out uint[] pixelsFlat, out int width, out int height, out int bpp, out DateTime?timestamp,
            out double?exposure, out short minPixelValue, out uint maxPixelValue, out bool hasNegativePixels, int negPixelCorrection = 0)
        {
            int pixWidth  = 0;
            int pixHeight = 0;
            int pixBpp    = 0;

            uint[,] pixels;
            uint medianValue;

            DateTime?fitsTimestamp = null;
            double?  fitsExposure  = null;
            Type     pixelDataType = null;

            CheckOpenedFitsFileCallback checkFileCallback = delegate(BasicHDU imageHDU)
            {
                pixWidth  = imageHDU.Axes[1];
                pixHeight = imageHDU.Axes[0];
                pixBpp    = Math.Abs(imageHDU.BitPix); /* float and double are -32 and -64 respectively */

                try
                {
                    bool isMidPoint = false;
                    fitsTimestamp = ParseExposure(fileName, imageHDU.Header, timeStampReader, out isMidPoint, out fitsExposure);
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.ToString());
                }

                if (fitsFileLoadedCallback != null)
                {
                    fitsFileLoadedCallback(imageHDU);
                }

                return(true);
            };

            if (loadFitsFileCallback != null)
            {
                int overwrittenHeight;
                int overwrittenWidth;

                loadFitsFileCallback(
                    fileName,
                    timeStampReader,
                    out pixels,
                    out overwrittenWidth,
                    out overwrittenHeight,
                    out medianValue,
                    out pixelDataType,
                    out hasNegativePixels,
                    out minPixelValue,
                    out maxPixelValue,
                    checkFileCallback
                    );

                width  = overwrittenWidth;
                height = overwrittenHeight;
            }
            else
            {
                Load16BitFitsFile(
                    fileName,
                    timeStampReader,
                    out pixels,
                    out medianValue,
                    out pixelDataType,
                    out hasNegativePixels,
                    out minPixelValue,
                    out maxPixelValue,
                    checkFileCallback,
                    negPixelCorrection
                    );

                width  = pixWidth;
                height = pixHeight;
            }

            pixelsFlat = new uint[width * height];

            timestamp = fitsTimestamp;
            exposure  = fitsExposure;

            uint mask = (uint)(((uint)1 << pixBpp) - 1);

            if (pixelDataType == typeof(float) || pixelDataType == typeof(double))
            {
                mask   = 0;
                pixBpp = 16; // Pretending float data is 16 bit. This is the maximum currently supported by Tangra
            }

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    uint val = pixels[x, y];
                    if (mask > 0)
                    {
                        val = val & mask;
                    }
                    pixelsFlat[x + y * width] = val;
                }
            }

            bpp = GetBppForMaxPixelValue(maxPixelValue);

            if (bpp == 32)
            {
                int shift = Math.Max(0, (int)Math.Ceiling(pixBpp / 16.0) - 1);

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        pixelsFlat[x + y * width] = pixelsFlat[x + y * width] >> shift;
                    }
                }

                bpp = 16;
            }
        }
コード例 #12
0
        //public static FITSData LoadFitsFile(string fileName, BasicHDU hdu, Array pixels, IFITSTimeStampReader timeStampReader, int negativePixelCorrection)
        //{

        //}

        public static FITSData LoadFitsFile(string fileName, IFITSTimeStampReader timeStampReader, int negativePixelCorrection)
        {
            var rv = new FITSData();

            Fits fitsFile = new Fits();

            using (BufferedFile bf = new BufferedFile(fileName, FileAccess.Read, FileShare.ReadWrite))
            {
                fitsFile.Read(bf);

                rv.HDU = fitsFile.GetHDU(0);

                rv.Width  = rv.HDU.Axes[1];
                rv.Height = rv.HDU.Axes[0];

                Array pixelsArray = (Array)rv.HDU.Data.DataArray;

                rv.PixelStats           = new PixelStats();
                rv.PixelStats.BZero     = GetBZero(rv.HDU);
                rv.PixelStats.RawBitPix = rv.HDU.BitPix;

                uint mask = (uint)(((uint)1 << rv.PixelStats.RawBitPix) - 1);
                rv.PixelStats.BitPix = rv.PixelStats.RawBitPix;
                if (rv.PixelStats.BitPix < 0)
                {
                    /* float and double are -32 and -64 respectively */
                    // Pretending float data is 16 bit. This is the maximum currently supported by Tangra
                    // Not using a mask here, and the pixel values will be converted further down
                    mask = 0;
                }

                rv.PixelsFlat = Load16BitImageData(pixelsArray, rv.Height, rv.Width, rv.PixelStats.BZero - negativePixelCorrection, mask,
                                                   out rv.PixelStats.MedianPixelValue, out rv.PixelStats.PixelType,
                                                   out rv.PixelStats.HasNegativePixels, out rv.PixelStats.MinPixelValue,
                                                   out rv.PixelStats.MaxPixelValue);


                rv.PixelStats.BitPix = GetBppForMaxPixelValue(rv.PixelStats.MaxPixelValue);


                if (rv.PixelStats.BitPix == 32)
                {
                    // Tangra only supports up to 16 bit so convert down to 16 bit
                    int shift = Math.Max(0, (int)Math.Ceiling(Math.Abs(rv.HDU.BitPix) / 16.0) - 1);

                    for (int y = 0; y < rv.Height; y++)
                    {
                        for (int x = 0; x < rv.Width; x++)
                        {
                            rv.PixelsFlat[x + y * rv.Width] = rv.PixelsFlat[x + y * rv.Width] >> shift;
                        }
                    }

                    rv.PixelStats.BitPix = 16;
                }
            }


            // Read Timestamp & Exposure
            if (timeStampReader != null)
            {
                bool isMidPoint;
                rv.Timestamp = timeStampReader.ParseExposure(fileName, rv.HDU.Header, out isMidPoint, out rv.Exposure);
            }


            // Read card from header
            rv.Cards = new Dictionary <string, string>();
            var cursor = rv.HDU.Header.GetCursor();

            while (cursor.MoveNext())
            {
                HeaderCard card = rv.HDU.Header.FindCard((string)cursor.Key);
                if (card != null && !string.IsNullOrWhiteSpace(card.Key) && card.Key != "END")
                {
                    if (rv.Cards.ContainsKey(card.Key))
                    {
                        rv.Cards[card.Key] += "\r\n" + card.Value;
                    }
                    else
                    {
                        rv.Cards.Add(card.Key, card.Value);
                    }
                }
            }

            return(rv);
        }