예제 #1
0
        public static void forma1()
        {
            /*Lectura parcial y automatica de parte de cabezera*/

            Fits f = new Fits("cubo_ing_comp.fits");

            BasicHDU[] hdus = f.Read();

            Console.WriteLine("Longitud: " + hdus.Length);

            for (int i = 0; i < hdus.Length; i += 1)
            {
                hdus[i].Info();
            }
            try {
            } catch {
            }

            ImageData datos = (ImageData)hdus[0].Kernel;

            float[,,] cubo = (float[, , ])datos.DataArray;

            Console.WriteLine("cubo: " + cubo[0, 0, 0]);

            f.Close();
        }
예제 #2
0
        public void AddReadNoise(string biasImagePath)
        {
            var bias      = new Fits(biasImagePath);
            var hdus      = bias.Read();
            var fitsImage = hdus.OfType <ImageHDU>().Single();
            var height    = fitsImage.Axes[0];
            var width     = fitsImage.Axes[1];
            var data      = fitsImage.Data.DataArray as Array;
            var lines     = data.Cast <short[]>().ToList();

            for (var y = 0; y < Image.Height; y++)
            {
                for (var x = 0; x < Image.Width; x++)
                {
                    var value = Image.At <ushort>(y, x);
                    var noise = lines[y].ElementAt(x) + fitsImage.BZero;

                    var noised = ((double)(value + noise));

                    if (noised > ushort.MaxValue)
                    {
                        noised = ushort.MaxValue;
                    }

                    Image.Set <ushort>(y, x, (ushort)noised);
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Считывание заголовка HDU-блока
        /// </summary>
        /// <param name="file">имя файла</param>
        /// <param name="index">порядковый номер HDU-блока</param>
        /// <param name="table">список параметров из заголовка HDU-блока</param>
        /// <param name="hasImage">наличие изображения после заголовка</param>
        /// <returns></returns>
        public bool ReadHeader(string file, int index, out List <string[]> table, out bool hasImage)
        {
            fits = new Fits(file, FileAccess.Read);
            hdus = fits.Read();
            if (hdus == null)
            {
                table    = null;
                hasImage = false;
            }
            // Построчное считывание и сохранение параметров из заголовка: ключевое слово-значение-комментарий
            // сохранение в список table
            table = new List <string[]> {
            };
            var hdr = hdus[index].Header;

            for (int j = 0; j < hdr.NumberOfCards; j++)
            {
                var hc = new HeaderCard(hdr.GetCard(j));
                var sa = new string[3] {
                    hc.Key, hc.Value, hc.Comment
                };
                table.Add(sa);
            }

            // Кол-во осей в data unit
            int naxis = 0;

            if (hdr.ContainsKey("SIMPLE"))
            {
                // Для первого HDU
                if (hdr.ContainsKey("NAXIS"))
                {
                    naxis = hdr.GetIntValue("NAXIS");
                }
            }
            else if (hdr.ContainsKey("XTENSION"))
            {
                // Для остальных HDU-расширений
                if (hdr.ContainsKey("ZNAXIS"))
                {
                    naxis = hdr.GetIntValue("ZNAXIS");
                }
            }
            hasImage = (naxis == 2);
            return(table.Count != 0);
        }
예제 #4
0
        public FitsFileInfo(string filePath)
        {
            FilePath = filePath;
            Fits f    = new Fits(filePath);
            var  hdus = f.Read();

            var headers = hdus.Where(hdu => hdu is ImageHDU).Select(hdu => hdu.Header);

            headers.ToList().ForEach(header =>
            {
                var cur        = header.GetCursor();
                object cardPtr = null;
                // return (HeaderCard) ((DictionaryEntry) this.cursor.Current).Value;
                while ((cardPtr = cur.Current) != null)
                {
                    var card = (HeaderCard)((DictionaryEntry)cardPtr).Value;
                    cur.MoveNext();
                    IList <HeaderKeyValue> keyValues = null;
                    keyValues         = _header.ContainsKey(card.Key) ? _header[card.Key] : new List <HeaderKeyValue>();
                    _header[card.Key] = keyValues;
                    var val           = card.IsStringValue ? new HeaderKeyValue(card.Key, card.Value, card.Comment) : ConvertType(card);
                    keyValues.Add(val);
                }
            });
            f.Close();

            //FileStream s = null;
            //using (s = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            //{
            //    var md5 = MD5.Create();
            //    var hashBytes = md5.ComputeHash(s);
            //    FileHash = Convert.ToBase64String(hashBytes);
            //}
            // Instead of reading the whole file for hash (which will be slow, especially with large
            // files and there can be thousands to read per batch) we just read the headers,
            // and calculate a hash from them. We only care about the indexed headers anyway,
            // if they don't change, nothing in the index needs to change.

            var md5       = MD5.Create();
            var hashBytes = md5.ComputeHash(HeadersToBytes());

            FileHash = Convert.ToBase64String(hashBytes);
            FileInfo finfo = new FileInfo(filePath);

            FileSize = finfo.Length;
        }
예제 #5
0
        public bool ReadHeader(string file, int index, out List <string[]> table, out bool hasImage)
        {
            fits = new Fits(file, FileAccess.Read);
            hdus = fits.Read();

            if (hdus == null)
            {
                table = null; hasImage = false; return(false);
            }

            table = new List <string[]> {
            };
            var hdr = hdus[index].Header;

            for (int j = 0; j < hdr.NumberOfCards; j++)
            {
                var hc = new HeaderCard(hdr.GetCard(j));
                var sa = new string[3] {
                    hc.Key, hc.Value, hc.Comment
                };
                table.Add(sa);
            }

            int naxis = 0;

            if (hdr.ContainsKey("SIMPLE"))
            {
                // Простой HDU
                if (hdr.ContainsKey("NAXIS"))
                {
                    naxis = hdr.GetIntValue("NAXIS");
                }
            }
            else if (hdr.ContainsKey("XTENSION"))
            {
                // HDU-расширение
                if (hdr.ContainsKey("ZNAXIS"))
                {
                    naxis = hdr.GetIntValue("ZNAXIS");
                }
            }
            hasImage = (naxis == 2);
            return(table.Count != 0);
        }
예제 #6
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));
            }
        }
예제 #7
0
        public static ThreeAxisFITSCubeFrameStream OpenFile(string fileName, VideoController videoController)
        {
            var fitsFile     = new Fits();
            var bufferedFile = new BufferedFile(fileName, FileAccess.Read, FileShare.ReadWrite);

            fitsFile.Read(bufferedFile);

            var imageHDU = fitsFile.GetHDU(0);
            ThreeAxisFITSCubeFrameStream fitsStream = null;

            videoController.SetCursor(Cursors.WaitCursor);

            try
            {
                var hasher = new SHA1CryptoServiceProvider();
                hasher.Initialize();
                byte[] combinedFileNamesBytes = Encoding.UTF8.GetBytes(fileName);
                var    hash         = hasher.ComputeHash(combinedFileNamesBytes, 0, combinedFileNamesBytes.Length);
                var    fitsFileHash = Convert.ToBase64String(hash);

                var frm = new frmDefineFitsCube3D(imageHDU, fitsFileHash, videoController);
                if (videoController.ShowDialog(frm) == DialogResult.OK)
                {
                    videoController.SetFlipSettings(frm.FlipVertically, frm.FlipHorizontally);

                    fitsStream = new ThreeAxisFITSCubeFrameStream(
                        fileName, fitsFile, bufferedFile, imageHDU, frm.TimeStampReader,
                        frm.WidthIndex, frm.HeightIndex, frm.FrameIndex,
                        frm.MinPixelValue, frm.MaxPixelValue, frm.BitPix, frm.NegPixCorrection);
                }
            }
            finally
            {
                if (fitsStream == null)
                {
                    bufferedFile.Dispose();
                }
            }

            return(fitsStream);
        }
예제 #8
0
        public static void GetFrameInfo(string directory, StarCatalogContext db)
        {
            string[] files = Directory.GetFiles(directory);

            foreach (string file in files)
            {
                string key = Path.GetFileName(file).Replace(".fit", "");

                if (db.Frames.Where(x => x.File == key).Count() == 0)
                {
                    D_Frame frame = new D_Frame()
                    {
                        File = key
                    };
                    db.Frames.Add(frame);

                    Fits       f    = new Fits(file);
                    BasicHDU[] hdus = f.Read();

                    DateTime date = File.GetCreationTime(file);
                    double   jd   = Math.Round(DateTimeToJulian(date), 5);

                    for (int i = 0; i < hdus.Length; i += 1)
                    {
                        Header hdr = hdus[i].Header;

                        date           = DateTime.Parse(hdr.GetStringValue("DATE-OBS") + "Z");
                        jd             = Math.Round(DateTimeToJulian(date), 5);
                        frame.Time     = jd;
                        frame.Duration = (int)hdr.GetDoubleValue("EXPOSURE");
                        frame.ISO      = (int)hdr.GetDoubleValue("GAIN");
                        frame.LocLat   = GlobalVariables.LocLat;
                        frame.LocLng   = GlobalVariables.LocLng;
                        frame.Camera   = hdr.GetStringValue("INSTRUME");
                        frame.Reported = 0;
                    }
                }
            }

            db.SaveChanges();
        }
예제 #9
0
        public static FitsCubeType GetFitsCubeType(string fileName)
        {
            Fits fitsFile = new Fits();

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

                BasicHDU imageHDU = fitsFile.GetHDU(0);
                if (fitsFile.NumberOfHDUs == 1 && imageHDU.Axes.Length == 3)
                {
                    return(FitsCubeType.ThreeAxisCube);
                }

                if (fitsFile.NumberOfHDUs > 1 && imageHDU.Axes.Length == 2)
                {
                    return(FitsCubeType.MultipleHDUsCube);
                }
            }

            return(FitsCubeType.NotACube);
        }
예제 #10
0
        public static FitsType GetFitsType(string fileName)
        {
            Fits fitsFile = new Fits();

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

                BasicHDU imageHDU = fitsFile.GetHDU(0);
                if (fitsFile.NumberOfHDUs == 1 && imageHDU.Axes.Length == 3 && imageHDU.Axes[0] == 3)
                {
                    return(FitsType.RGBFrames);
                }

                if (fitsFile.NumberOfHDUs == 1 && imageHDU.Axes.Length == 2)
                {
                    return(FitsType.SingleFrame);
                }
            }

            return(FitsType.Invalid);
        }
예제 #11
0
        private void BuildCalSpecDb()
        {
            var db       = new CalSpecDatabase();
            int totalBad = 0;

            string[] lines2 = File.ReadAllLines(@"F:\WORK\tangra3\Tangra 3\VideoOperations\Spectroscopy\AbsFluxCalibration\Standards\AbsFlux-TangraStars.csv");
            for (int i = 1; i < lines2.Length; i++)
            {
                string[] tokens = lines2[i].Split(',');

                string calSpecId    = tokens[0].Trim();
                string RA_FK5_Hours = tokens[1].Trim();
                string DEG_FK5_Deg  = tokens[2].Trim();
                string pmRA         = tokens[3].Trim();
                string pmDec        = tokens[4].Trim();
                string specType     = tokens[5].Trim();
                string magV         = tokens[6].Trim();
                string magBV        = tokens[7].Trim();
                string absFluxId    = tokens[8].Trim();
                string stisFlag     = tokens[9].Trim();
                string fitsFilePath = tokens[10].Trim();
                int    stisFrom     = int.Parse(tokens[11].Trim());
                int    stisTo       = int.Parse(tokens[12].Trim());
                string tyc2         = tokens[13].Trim();
                string ucac4        = tokens[14].Trim();

                if (!string.IsNullOrEmpty(pmRA) && !string.IsNullOrEmpty(pmDec))
                {
                    var star = new CalSpecStar()
                    {
                        CalSpecStarId  = calSpecId,
                        AbsFluxStarId  = absFluxId,
                        STIS_Flag      = stisFlag,
                        FITS_File      = fitsFilePath,
                        TYC2           = tyc2,
                        U4             = ucac4,
                        pmRA           = double.Parse(pmRA),
                        pmDE           = double.Parse(pmDec),
                        MagV           = double.Parse(magV),
                        MagBV          = double.Parse(magBV),
                        SpecType       = specType,
                        RA_J2000_Hours = AstroConvert.ToRightAcsension(RA_FK5_Hours),
                        DE_J2000_Deg   = AstroConvert.ToDeclination(DEG_FK5_Deg)
                    };

                    string filePath = Path.GetFullPath(@"Z:\CALSPEC\current_calspec\" + fitsFilePath);

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

                        BasicHDU imageHDU = fitsFile.GetHDU(1);

                        var      table         = (ColumnTable)imageHDU.Data.DataArray;
                        double[] wavelengths   = (double[])table.Columns[0];
                        float[]  fluxes        = (float[])table.Columns[1];
                        short[]  goodnessFlags = (short[])table.Columns[5];

                        for (int j = 0; j < fluxes.Length; j++)
                        {
                            if (wavelengths[j] < stisFrom)
                            {
                                continue;
                            }
                            if (wavelengths[j] > stisTo)
                            {
                                break;
                            }

                            if (goodnessFlags[j] != 0)
                            {
                                star.DataPoints.Add(wavelengths[j], fluxes[j]);
                            }
                            else
                            {
                                totalBad++;
                            }
                        }
                        fitsFile.Close();
                    }

                    db.Stars.Add(star);
                }
            }

            using (var compressedStream = new FileStream(@"F:\WORK\tangra3\Tangra 3\VideoOperations\Spectroscopy\AbsFluxCalibration\Standards\CalSpec.db", FileMode.CreateNew, FileAccess.Write))
                using (var deflateStream = new DeflateStream(compressedStream, CompressionMode.Compress, true))
                {
                    using (var writer = new BinaryWriter(deflateStream))
                    {
                        db.Serialize(writer);
                    }
                }

            using (var compressedStream = new FileStream(@"F:\WORK\tangra3\Tangra 3\VideoOperations\Spectroscopy\AbsFluxCalibration\Standards\CalSpec.db", FileMode.Open, FileAccess.Read))
                using (var deflateStream = new DeflateStream(compressedStream, CompressionMode.Decompress, true))
                {
                    using (var reader = new BinaryReader(deflateStream))
                    {
                        var db2 = new CalSpecDatabase(reader);
                        Trace.WriteLine(db2.Stars.Count);
                    }
                }

            MessageBox.Show(totalBad.ToString() + " bad entries excluded.");

            string[] fitsFiles = Directory.GetFiles(@"Z:\CALSPEC\current_calspec", "*.fit");

            var dist = new Dictionary <int, int>();

            foreach (string filePath in fitsFiles)
            {
                using (var bf = new BufferedFile(filePath, FileAccess.Read, FileShare.ReadWrite))
                {
                    var fitsFile = new Fits();
                    fitsFile.Read(bf);

                    var bld = new StringBuilder();

                    BasicHDU headerHDU = fitsFile.GetHDU(0);
                    BasicHDU imageHDU  = fitsFile.GetHDU(1);

                    for (int i = 0; i < headerHDU.Header.NumberOfCards; i++)
                    {
                        string cardString = headerHDU.Header.GetCard(i);
                        bld.AppendFormat("# {0}\r\n", cardString);
                    }

                    for (int i = 0; i < imageHDU.Header.NumberOfCards; i++)
                    {
                        string cardString = imageHDU.Header.GetCard(i);
                        bld.AppendFormat("# {0}\r\n", cardString);
                    }

                    var table = (ColumnTable)imageHDU.Data.DataArray;
                    if (table.Columns.Length == 7 &&
                        table.Columns[0] is double[] && table.Columns[1] is float[] && table.Columns[2] is float[] && table.Columns[3] is float[] &&
                        table.Columns[4] is float[] && table.Columns[5] is short[] && table.Columns[6] is float[])
                    {
                        double[] wavelengths   = (double[])table.Columns[0];
                        float[]  fluxes        = (float[])table.Columns[1];
                        float[]  col2          = (float[])table.Columns[2];
                        float[]  col3          = (float[])table.Columns[3];
                        float[]  col4          = (float[])table.Columns[4];
                        short[]  goodnessFlags = (short[])table.Columns[5];
                        float[]  exposures     = (float[])table.Columns[6];

                        for (int i = 0; i < fluxes.Length; i++)
                        {
                            if (wavelengths[i] < 2000)
                            {
                                continue;
                            }
                            if (wavelengths[i] > 15000)
                            {
                                break;
                            }

                            bld.Append(wavelengths[i].ToString().PadLeft(20));
                            bld.Append(fluxes[i].ToString().PadLeft(20));
                            bld.Append(col2[i].ToString().PadLeft(20));
                            bld.Append(col3[i].ToString().PadLeft(20));
                            bld.Append(col4[i].ToString().PadLeft(20));
                            bld.Append(goodnessFlags[i].ToString().PadLeft(15));
                            bld.Append(exposures[i].ToString().PadLeft(15));
                            bld.AppendLine();

                            int expMS = (int)Math.Round(exposures[i] * 1000);
                            if (!dist.ContainsKey(expMS))
                            {
                                dist.Add(expMS, 0);
                            }
                            dist[expMS]++;
                        }
                    }

                    string outFileName = Path.ChangeExtension(filePath, ".txt");
                    File.WriteAllText(outFileName, bld.ToString());
                    fitsFile.Close();
                }
            }

            var output = new StringBuilder();

            foreach (int key in dist.Keys)
            {
                output.AppendFormat("{0}s = {1}\r\n", (key / 1000.0).ToString("0.0"), dist[key]);
            }
            MessageBox.Show(output.ToString());
        }
예제 #12
0
        private void BuildAbsFlux()
        {
            var indexFile = new StringBuilder();

            indexFile.AppendLine("BD and HD CALSPEC stars with STIS fluxes between 3000 and 10000 Angstroms");
            indexFile.AppendLine("Name            FK5_Coordinates_J2000      Type    Mag    B-V   File_Name");

            foreach (CalSpecStar star in CalSpecDatabase.Instance.Stars)
            {
                if (star.AbsFluxStarId.StartsWith("TYC"))
                {
                    continue;
                }
                string modifiedId = star.AbsFluxStarId.Replace(" ", "_");
                if (modifiedId.Length < 10)
                {
                    modifiedId += "_";
                }
                while (modifiedId.Length < 10)
                {
                    int firstUnderScorePos = modifiedId.IndexOf("_");
                    modifiedId = modifiedId.Substring(0, firstUnderScorePos) + "_" + modifiedId.Substring(firstUnderScorePos);
                }
                string raStr        = AstroConvert.ToStringValue(star.RA_J2000_Hours, "HH MM SS.TTT");
                string deStr        = AstroConvert.ToStringValue(star.DE_J2000_Deg, "+DD MM SS.TT");
                string dataFileName = Path.GetFileNameWithoutExtension(star.FITS_File) + "_t1.txt";
                string line         = string.Format("{0}{1} {2}    {3}{4}  {5}  {6}\r\n",
                                                    modifiedId.PadRight(14), raStr, deStr,
                                                    star.SpecType.PadRight(7),
                                                    star.MagV.ToString("0.00").PadLeft(5),
                                                    star.MagBV.ToString("0.00").PadLeft(5),
                                                    dataFileName);
                indexFile.Append(line);

                string filePath = Path.GetFullPath(@"Z:\CALSPEC\current_calspec\" + star.FITS_File);

                double wavelengthFrom = star.DataPoints.Keys.Min();
                double wavelengthTo   = star.DataPoints.Keys.Max();
                using (var bf = new BufferedFile(filePath, FileAccess.Read, FileShare.ReadWrite))
                {
                    var fitsFile = new Fits();
                    fitsFile.Read(bf);

                    BasicHDU imageHDU = fitsFile.GetHDU(1);

                    var      table         = (ColumnTable)imageHDU.Data.DataArray;
                    double[] wavelengths   = (double[])table.Columns[0];
                    float[]  fluxes        = (float[])table.Columns[1];
                    float[]  col2          = (float[])table.Columns[2];
                    float[]  col3          = (float[])table.Columns[3];
                    float[]  col4          = (float[])table.Columns[4];
                    short[]  goodnessFlags = (short[])table.Columns[5];
                    float[]  exposures     = (float[])table.Columns[6];

                    var dataFile = new StringBuilder();
                    dataFile.AppendLine("      WAVELENGTH          FLUX     STATERROR      SYSERROR         FWHM     DATAQUAL      TOTEXP");
                    dataFile.AppendLine("              1D            1E            1E            1E           1E           1I          1E");
                    dataFile.AppendLine("       ANGSTROMS          FLAM          FLAM          FLAM    ANGSTROMS         NONE         SEC");

                    for (int j = 0; j < fluxes.Length; j++)
                    {
                        if (wavelengths[j] < wavelengthFrom)
                        {
                            continue;
                        }
                        if (wavelengths[j] > wavelengthTo)
                        {
                            break;
                        }

                        string dataLine = string.Format("{0}{1}{2}{3}{4}            {5}{6}",
                                                        ((int)Math.Round(wavelengths[j])).ToString().PadLeft(16),
                                                        fluxes[j].ToString("E4").PadLeft(14),
                                                        col2[j].ToString("E4").PadLeft(14),
                                                        col3[j].ToString("E4").PadLeft(14),
                                                        col4[j].ToString("#.0").PadLeft(13),
                                                        goodnessFlags[j].ToString(),
                                                        exposures[j].ToString("E1").PadLeft(12));

                        dataFile.AppendLine(dataLine);
                    }

                    fitsFile.Close();

                    File.WriteAllText(@"Z:\AbsFlux\v3\" + dataFileName, dataFile.ToString());
                }
            }
            File.WriteAllText(@"Z:\AbsFlux\v3\AbsFluxCALSPECstars.txt", indexFile.ToString());
        }
예제 #13
0
        /// <summary>
        /// Создание списка пар: дата - средний абсолютный поток в заданном диапазоне
        /// (также для последовательности HDU-блоков в одном FITS-файле)
        /// </summary>
        /// <param name="file">имя файла</param>
        /// <param name="min">минимальное значение</param>
        /// <param name="max">максимальное значение</param>
        /// <param name="points">пара типа</param>
        public void CalcDependencyAbsFlux(string file, double min, double max, out List <DateValue> points)
        {
            fits = new Fits(file, FileAccess.Read);
            hdus = fits.Read();

            points = new List <DateValue> {
            };
            if (hdus == null)
            {
                return;
            }

            int naxis = 0;

            for (int i = 0; i < hdus.Length; i++)
            {
                DateTime dateTime;
                double   sum = 0.0;

                var hdr = hdus[i].Header;
                if (!hdr.ContainsKey("SIMPLE"))
                {
                    break;
                }
                if (hdr.ContainsKey("NAXIS"))
                {
                    naxis = hdr.GetIntValue("NAXIS");
                }
                if (naxis != 2)
                {
                    break;
                }
                if (!hdr.ContainsKey("DATE-OBS"))
                {
                    break;
                }
                if (!DateTime.TryParse(hdr.GetStringValue("DATE-OBS"), out dateTime))
                {
                    break;
                }

                int    bits   = hdus[i].BitPix;
                BitPix bitpix = BitPix.Unknown;

                switch (bits)
                {
                case 8:
                    bitpix = BitPix.Bits8;
                    break;

                case 16:
                    bitpix = BitPix.Bits16;
                    break;

                case 32:
                    bitpix = BitPix.Bits32;
                    break;

                default:
                    bitpix = BitPix.Unknown;
                    break;
                }
                Array[] data;
                if (bitpix != BitPix.Unknown)
                {
                    data = (Array[])hdus[i].Kernel;
                }
                else
                {
                    break;
                }

                int width  = data[0].GetLength(0);
                int height = data.GetLength(0);

                if (!hdr.ContainsKey("BSCALE") || !hdr.ContainsKey("BZERO"))
                {
                    break;
                }
                var bscale = hdus[i].BScale;
                var bzero  = hdus[i].BZero;

                // Определение значения для прозрачного фона
                Int32 trnValue = 0;
                if (bitpix == BitPix.Bits8)
                {
                    trnValue = byte.MinValue;
                }
                else if (bitpix == BitPix.Bits16)
                {
                    trnValue = Int16.MinValue;
                }
                else if (bitpix == BitPix.Bits32)
                {
                    trnValue = Int32.MinValue;
                }

                // Расчет среднего значения магнитного потока в заданном диапазоне
                int pointCount = 0;
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        Int32 value = 0;
                        if (bitpix == BitPix.Bits8)
                        {
                            value = ((byte[])data[y])[x];
                        }
                        else if (bitpix == BitPix.Bits16)
                        {
                            value = ((Int16[])data[y])[x];
                        }
                        else if (bitpix == BitPix.Bits32)
                        {
                            value = ((Int32[])data[y])[x];
                        }

                        if (value != trnValue)
                        {
                            double ampl = bscale * value + bzero;
                            if ((ampl > min) && (ampl < max))
                            {
                                if (ampl > 0)
                                {
                                    sum += ampl;
                                }
                                else
                                {
                                    sum += ampl * (-1.0);
                                }
                                pointCount++;
                            }
                        }
                    }
                }

                if (pointCount != 0)
                {
                    points.Add(new DateValue(dateTime, sum / pointCount));
                }
            }
        }
예제 #14
0
        /// <summary>
        /// Определение минамальных и максимальных значений блока данных и дат для рассчета среднего значения силы
        /// (также для последовательности HDU-блоков в одном FITS-файле)
        /// </summary>
        /// <param name="file">имя файла</param>
        /// <param name="minVal">минимальное значение</param>
        /// <param name="maxVal">максимальное значение</param>
        /// <param name="minDateTime">самая ранняя дата записи</param>
        /// <param name="maxDateTime">самая поздняя дата записи</param>
        public void GetValueSpan(string file, out double minVal, out double maxVal, out DateTime minDateTime, out DateTime maxDateTime)
        {
            fits = new Fits(file, FileAccess.Read);
            hdus = fits.Read();

            minVal      = double.MaxValue;
            maxVal      = double.MinValue;
            minDateTime = DateTime.MaxValue;
            maxDateTime = DateTime.MinValue;
            if (hdus == null)
            {
                return;
            }

            int naxis = 0;

            for (int i = 0; i < hdus.Length; i++)
            {
                var hdr = hdus[i].Header;
                if (!hdr.ContainsKey("SIMPLE"))
                {
                    break;
                }

                // Определение границ для дат наблюдения
                minDateTime = DateTime.MaxValue;
                maxDateTime = DateTime.MinValue;

                if (!hdr.ContainsKey("DATE-OBS"))
                {
                    break;
                }
                DateTime dateTime;
                if (!DateTime.TryParse(hdr.GetStringValue("DATE-OBS"), out dateTime))
                {
                    break;
                }
                if (dateTime < minDateTime)
                {
                    minDateTime = dateTime;
                }
                if (dateTime > maxDateTime)
                {
                    maxDateTime = dateTime;
                }

                // Определение границ значений в массиве данных(data-unit)
                if (hdr.ContainsKey("NAXIS"))
                {
                    naxis = hdr.GetIntValue("NAXIS");
                }
                if (naxis != 2)
                {
                    break;
                }

                int    bits   = hdus[i].BitPix;
                BitPix bitpix = BitPix.Unknown;

                switch (bits)
                {
                case 8:
                    bitpix = BitPix.Bits8;
                    break;

                case 16:
                    bitpix = BitPix.Bits16;
                    break;

                case 32:
                    bitpix = BitPix.Bits32;
                    break;

                default:
                    bitpix = BitPix.Unknown;
                    break;
                }
                Array[] data;
                if (bitpix != BitPix.Unknown)
                {
                    data = (Array[])hdus[i].Kernel;
                }
                else
                {
                    break;
                }

                int width  = data[0].GetLength(0);
                int height = data.GetLength(0);

                if (!hdr.ContainsKey("BSCALE") ||
                    !hdr.ContainsKey("BZERO"))
                {
                    break;
                }
                var bscale = hdus[i].BScale;
                var bzero  = hdus[i].BZero;

                var minValue = Int32.MaxValue;
                var maxValue = Int32.MinValue;

                // Определение значения для прозрачного фона для разных типов данных.
                Int32 trnValue = 0;
                if (bitpix == BitPix.Bits8)
                {
                    trnValue = byte.MinValue;
                }
                else if (bitpix == BitPix.Bits16)
                {
                    trnValue = Int16.MinValue;
                }
                else if (bitpix == BitPix.Bits32)
                {
                    trnValue = Int32.MinValue;
                }

                // Определений максимума и минимума значений в массиве данных(data-unit)
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        Int32 value = 0;
                        if (bitpix == BitPix.Bits8)
                        {
                            value = ((byte[])data[y])[x];
                        }
                        else if (bitpix == BitPix.Bits16)
                        {
                            value = ((Int16[])data[y])[x];
                        }
                        else if (bitpix == BitPix.Bits32)
                        {
                            value = ((Int32[])data[y])[x];
                        }
                        if (value > maxValue)
                        {
                            maxValue = value;
                        }
                        if ((value < minValue) && (value != trnValue))
                        {
                            minValue = value;
                        }
                    }
                }
                // Перевод значений максимального и минимального значений в физическую велечину
                double crtMinVal = bscale * minValue + bzero;
                double crtMaxVal = bscale * maxValue + bzero;
                if (crtMinVal < minVal)
                {
                    minVal = crtMinVal;
                }
                if (crtMaxVal > maxVal)
                {
                    maxVal = crtMaxVal;
                }
            }
        }
예제 #15
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);
        }