Ejemplo n.º 1
0
        public async Task ReadWriteTest <T>(object?__ = null) where T : unmanaged, IComparable <T>, IEquatable <T>
        {
            const int n         = 248;
            const int m         = 496;
            var       converter = ConverterProvider.GetImageConverter <T>();

            var data = ArrayPool <T> .Shared.Rent(n *m);

            try
            {
                for (var i = 0; i < n * m; i++)
                {
                    data[i] = Internal.UnsafeNumerics.MathOps.DangerousCast <double, T>(_r.NextDouble() * i);
                }
                var src = Image.Create <T>(data.AsSpan(0, n * m), n, m);


                await using var fs = new FileStream($"multiple_records_{typeof(T).Name}.fits", FileMode.Create, FileAccess.ReadWrite);
                await using (var writer = new FitsWriter(fs))
                {
                    await converter.WriteToFitsAsync(src, writer);

                    await converter.WriteToFitsAsync(src, writer, true);
                }

                fs.Seek(0, SeekOrigin.Begin);
                var images = new List <IImage <T> >(2);
                await using (var reader = new FitsReader(fs))
                {
                    await foreach (var block in reader.EnumerateBlocksAsync())
                    {
                        images.Add(converter.ConvertFromBlock(block, out _));
                    }
                }

                Assert.That(images, Has.Count.EqualTo(2));
                Assert.AreEqual(src, images[0]);
                Assert.AreEqual(src, images[1]);
            }
            finally
            {
                ArrayPool <T> .Shared.Return(data);
            }
        }
Ejemplo n.º 2
0
        public ValueTask WriteToFitsAsync(IImage <T> input, FitsWriter writer, IEnumerable <IFitsValue> extraKeys,
                                          bool isXtension = false, CancellationToken token = default)
        {
            if (input is null)
            {
                throw new ArgumentNullException(nameof(input), SR.NullArgument);
            }

            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer), SR.NullArgument);
            }

            AllowedTypes.ValidateDataType <T>();

            var extraKeysInst = (extraKeys ?? Enumerable.Empty <IFitsValue>()).ToList();

            // Throws if mandatory keys are present
            Extensions.CheckExtraFitsKeys(extraKeysInst);

            var bitPix = FitsCs.Extensions.ConvertTypeToBitPix <T>()
                         ?? throw new InvalidOperationException(string.Format(SR.TypeNotSupported, typeof(T)));

            var desc = new Descriptor(
                bitPix,
                new[] { input.Width, input.Height },
                isXtension ? ExtensionType.Image : ExtensionType.Primary);

            var keys = new List <IFitsValue>(extraKeysInst.Count + 10);

            keys.AddRange(desc.GenerateFitsHeader());
            keys.AddRange(extraKeysInst);
            keys.Add(FitsKey.CreateEnd());



            var block = Block <T> .CreateWithData(desc, keys, x =>
            {
                input.GetView().CopyTo(x);
            });

            return(writer.WriteBlockAsync(block, token));
        }
Ejemplo n.º 3
0
        public async Task Test_FitsReader_FitsWrite(FitsFileInfo testCaseData)
        {
            var path = Path.Combine(ReaderWriter_TestCaseDataProvider.TestDataDirectory, testCaseData.Name);

            Assume.That(File.Exists(path));
            List <Block> content;
            List <Block> reReadContent;

            await using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                await using var reader = new FitsReader(fileStream);
                content = await reader.EnumerateBlocksAsync().ToListAsync();
            }

            await using var memStr = new MemoryStream();
            {
                await using (var writer = new FitsWriter(memStr))
                    foreach (var block in content)
                    {
                        await writer.WriteBlockAsync(block);
                    }

                memStr.Seek(0, SeekOrigin.Begin);

                await using var reader = new FitsReader(memStr);

                reReadContent = await reader.EnumerateBlocksAsync().ToListAsync();
            }

            Assert.AreEqual(content.Count, reReadContent.Count);
            for (var i = 0; i < content.Count; i++)
            {
                CollectionAssert.AreEqual(content[i].Keys, reReadContent[i].Keys);
                Assert.IsTrue(content[i].RawData.SequenceEqual(reReadContent[i].RawData));
            }
        }