Exemplo n.º 1
0
        public void validate_get_file_stream_from_resources()
        {
            var name         = Assembly.GetExecutingAssembly().FullName;
            var assemblyName = new AssemblyName(name).Name;
            var stream       = StreamFinder.GetFileStreamFromResources("Resources/resourcefile.txt", assemblyName);

            Assert.IsNotNull(stream);
        }
Exemplo n.º 2
0
        public void validate_get_file_stream()
        {
            var myString = new StringBuilder();

            myString.AppendLine("This is a test string");
            var stream = StreamFinder.GetFileStream("file4.txt", FileMode.Create);

            Assert.IsNotNull(stream);
        }
Exemplo n.º 3
0
        public void validate_loading_spectral_database_and_header_from_tsv_no_conversion()
        {
            Stream stream = StreamFinder.GetFileStreamFromResources("Modeling/Spectroscopy/Resources/Spectra.txt", "Vts");

            var testSpectra    = SpectralDatabase.GetSpectraFromFile(stream, false);
            var testDictionary = testSpectra.ToDictionary();

            testDictionary.WriteToJson("dictionary5.txt");
            Assert.IsTrue(FileIO.FileExists("dictionary5.txt"));
        }
Exemplo n.º 4
0
        public void validate_Loading_Spectral_Database_and_header_from_tsv_in_resources()
        {
            Stream stream = StreamFinder.GetFileStreamFromResources("Modeling/Spectroscopy/Resources/Spectra.txt", _assemblyName);

            var testSpectra    = SpectralDatabase.GetSpectraFromFile(stream, true);
            var testDictionary = testSpectra.ToDictionary();

            testDictionary.WriteToJson("dictionary4.txt");
            Assert.IsTrue(FileIO.FileExists("dictionary4.txt"));
        }
        public void validate_write_text_to_stream()
        {
            StringBuilder myString = new StringBuilder();

            myString.AppendLine("This is a test string");
            Stream stream = StreamFinder.GetFileStream("file4.txt", FileMode.Create);

            FileIO.WriteTextToStream(myString.ToString(), stream);
            Assert.IsNotNull(stream);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Reads Detector from a file in resources using given fileName.
        /// </summary>
        /// <param name="fileName">filename string of file to be read</param>
        /// <param name="folderPath">path string of folder where file to be read resides</param>
        /// <param name="projectName">project name string where file resides in resources</param>
        /// <returns>IDetector</returns>
        public static IDetector ReadDetectorFromFileInResources(string fileName, string folderPath, string projectName)
        {
            try
            {
                string filePath = folderPath + fileName;
                // allow null filePaths in case writing to isolated storage
                //string filePath;
                //if (folderPath == "")
                //{
                //    filePath = fileName;
                //}
                //else
                //{
                //    filePath = folderPath + @"/" + fileName;
                //}
                var detector = FileIO.ReadFromJsonInResources <IDetector>(filePath + ".txt", projectName);

                var binaryArraySerializers = detector.GetBinarySerializers();

                if (binaryArraySerializers == null)
                {
                    return(detector);
                }

                foreach (var binaryArraySerializer in binaryArraySerializers)
                {
                    if (binaryArraySerializer == null)
                    {
                        continue;
                    }

                    using (Stream s = StreamFinder.GetFileStreamFromResources(filePath + binaryArraySerializer.FileTag, projectName))
                    {
                        if (s == null)
                        {
                            continue;
                        }

                        using (BinaryReader br = new BinaryReader(s))
                        {
                            binaryArraySerializer.ReadData(br);
                        }
                    }
                }

                return(detector);
            }
            catch (Exception e)
            {
                Console.WriteLine("Problem reading detector information from resource file.\n\nDetails:\n\n" + e + "\n");
            }

            return(null);
        }
Exemplo n.º 7
0
 private static void LocalWriteArrayToBinary(Array dataIN, string filename, FileMode mode)
 {
     // Create a file to write binary data
     using (Stream s = StreamFinder.GetFileStream(filename, mode))
     {
         using (BinaryWriter bw = new BinaryWriter(s))
         {
             new ArrayCustomBinaryWriter().WriteToBinary(bw, dataIN);
         }
     }
 }
Exemplo n.º 8
0
        /// <summary>
        /// Reads Detector from File with given fileName.
        /// </summary>
        /// <param name="fileName">filename string of file to be read</param>
        /// <param name="folderPath">path string where file resides</param>
        /// <returns>IDetector</returns>
        public static IDetector ReadDetectorFromFile(string fileName, string folderPath)
        {
            try
            {
                // allow null filePaths in case writing to isolated storage
                string filePath;
                if (folderPath == "")
                {
                    filePath = fileName;
                }
                else
                {
                    filePath = folderPath + @"/" + fileName;
                }
                var detector = FileIO.ReadFromJson <IDetector>(filePath + ".txt");

                var binaryArraySerializers = detector.GetBinarySerializers();

                if (binaryArraySerializers == null)
                {
                    return(detector);
                }

                foreach (var binaryArraySerializer in binaryArraySerializers)
                {
                    if (binaryArraySerializer == null)
                    {
                        continue;
                    }

                    using (Stream s = StreamFinder.GetFileStream(filePath + binaryArraySerializer.FileTag, FileMode.Open))
                    {
                        if (s == null)
                        {
                            continue;
                        }

                        using (BinaryReader br = new BinaryReader(s))
                        {
                            binaryArraySerializer.ReadData(br);
                        }
                    }
                }

                return(detector);
            }
            catch (Exception e)
            {
                Console.WriteLine("Problem reading detector information from file.\n\nDetails:\n\n" + e + "\n");
            }

            return(null);
        }
Exemplo n.º 9
0
        public void validate_write_json_to_stream()
        {
            var    pos    = new Position(2, 4, 6);
            Stream stream = StreamFinder.GetFileStream("file6.txt", FileMode.Create);

            FileIO.WriteJsonToStream(pos, stream);
            var pos2 = FileIO.ReadFromJson <Position>("file6.txt");

            Assert.AreEqual(pos2.X, 2.0);
            Assert.AreEqual(pos2.Y, 4.0);
            Assert.AreEqual(pos2.Z, 6.0);
        }
Exemplo n.º 10
0
        public void validate_copy_stream()
        {
            var name         = Assembly.GetExecutingAssembly().FullName;
            var assemblyName = new AssemblyName(name).Name;
            var stream1      = StreamFinder.GetFileStreamFromResources("Resources/resourcefile.txt", assemblyName);
            var stream2      = StreamFinder.GetFileStream("file5.txt", FileMode.CreateNew);

            Assert.IsNotNull(stream1);
            FileIO.CopyStream(stream1, stream2);
            Assert.IsNotNull(stream2);
            Assert.AreEqual(stream1, stream2);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Writes Detector xml for scalar detectors, writes Detector xml and
        /// binary for 1D and larger detectors.  Detector.Name is used for filename.
        /// </summary>
        /// <param name="detector">IDetector being written.</param>
        /// <param name="folderPath">location of written file.</param>
        public static void WriteDetectorToFile(IDetector detector, string folderPath)
        {
            try
            {
                // allow null folderPath in case writing to isolated storage
                string filePath = folderPath;
                if (folderPath == "")
                {
                    filePath = detector.Name;
                }
                else
                {
                    filePath = folderPath + @"/" + detector.Name;

                    // desktop folder
                    FileIO.CreateDirectory(folderPath);
                }

                FileIO.WriteToJson(detector, filePath + ".txt");
                var binaryArraySerializers = detector.GetBinarySerializers();
                if (binaryArraySerializers == null)
                {
                    return;
                }

                foreach (var binaryArraySerializer in binaryArraySerializers)
                {
                    if (binaryArraySerializer == null)
                    {
                        continue;
                    }

                    // Create a file to write binary data
                    using (Stream s = StreamFinder.GetFileStream(filePath + binaryArraySerializer.FileTag, FileMode.OpenOrCreate))
                    {
                        if (s == null)
                        {
                            continue;
                        }

                        using (BinaryWriter bw = new BinaryWriter(s))
                        {
                            binaryArraySerializer.WriteData(bw);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Problem writing detector information to file.\n\nDetails:\n\n" + e + "\n");
            }
        }
Exemplo n.º 12
0
        public void validate_read_from_json_stream()
        {
            var name         = Assembly.GetExecutingAssembly().FullName;
            var assemblyName = new AssemblyName(name).Name;
            // create a JSON stream
            var stream = StreamFinder.GetFileStreamFromResources("Resources/fileiotest/position.txt", assemblyName);
            var pos    = FileIO.ReadFromJsonStream <Position>(stream);

            Assert.AreEqual(pos.X, 5);
            Assert.AreEqual(pos.Y, 10);
            Assert.AreEqual(pos.Z, 15);
            stream.Close();
        }
Exemplo n.º 13
0
        public void validate_write_to_xml_stream()
        {
            var    name         = Assembly.GetExecutingAssembly().FullName;
            var    assemblyName = new AssemblyName(name).Name;
            var    xmlFile      = FileIO.ReadFromXMLInResources <Position>("Resources/fileiotest/file7.xml", assemblyName);
            Stream stream       = StreamFinder.GetFileStream("file8.xml", FileMode.Create);

            FileIO.WriteToXMLStream(xmlFile, stream);
            Assert.IsNotNull(stream);
            Assert.IsTrue(FileIO.FileExists("file8.xml"));
            Assert.IsTrue(new FileInfo("file8.xml").Length != 0);
            stream.Close();
        }
Exemplo n.º 14
0
        public static void SaveUIElementToJpegImage(UIElement element)
        {
            // todo: pass myChart as a command parameter declaratively to be handled in PlotViewModel
            var b = new WriteableBitmap(element, null);

            using (var s = StreamFinder.GetLocalFilestreamFromSaveFileDialog("jpg"))
            {
                if (s != null)
                {
                    SaveBitmapToJpeg(b, s);
                }
            }
        }
Exemplo n.º 15
0
        static void Main(string[] args)
        {
            //// Examples for Own3d:

            //// Create a StreamFinder object.
            //StreamFinder own3dStreamtastic = new StreamFinder(new Own3dStreamRepository());

            //// Let's find the most viewed streamers in general.
            //var topStreamers = own3dStreamtastic.FindTopViewedStreams();
            //foreach (var stream in topStreamers)
            //{
            //    Console.WriteLine(String.Format("{0} - Viewers: {1} - URL: {2}", stream.Title, stream.ViewerCount, stream.ChannelUrl));
            //}

            //// You can find the top viewed streamers for any game Own3d supports.
            //// For example, League of Legends.
            //var topStreamersForLeagueOfLegends = own3dStreamtastic.FindTopViewedStreamsByGame("League+of+Legends");
            //foreach (var stream in topStreamersForLeagueOfLegends)
            //{
            //    Console.WriteLine(String.Format("{0} - Viewers: {1}", stream.Title, stream.ViewerCount));
            //}

            //// Or how about Dota 2.
            //var topStreamersForDota2 = own3dStreamtastic.FindTopViewedStreamsByGame("Dota+2");
            //foreach (var stream in topStreamersForDota2)
            //{
            //    Console.WriteLine(String.Format("{0} - Viewers: {1}", stream.Title, stream.ViewerCount));
            //}

            /*************************************************************/

            // Examples for Twitch.TV:

            // Create a StreamFinder object.
            StreamFinder twitchTvStreamtastic = new StreamFinder(new TwitchtvStreamRepository());

            //var topStreamersTwitch = twitchTvStreamtastic.FindTopViewedStreams();
            //foreach (var stream in topStreamersTwitch)
            //{
            //    Console.WriteLine(String.Format("{0} - Viewers: {1}", stream.Title, stream.ViewerCount));
            //}

            var topStreamersTwitchForDota2 = twitchTvStreamtastic.FindTopViewedStreamsByGame("Dota 2");
            foreach (var stream in topStreamersTwitchForDota2)
            {
                Console.WriteLine(String.Format("{0} - Viewers: {1}", stream.Title, stream.ViewerCount));
            }

            Console.ReadKey(true);
        }
Exemplo n.º 16
0
        public void validate_read_from_stream()
        {
            var      name         = Assembly.GetExecutingAssembly().FullName;
            var      assemblyName = new AssemblyName(name).Name;
            Position pos;
            // read file from resources and write it so that can be read in
            var xml = FileIO.ReadFromXMLInResources <Position>("Resources/fileiotest/file7.xml", assemblyName);

            FileIO.WriteToXML <Position>(xml, "file7.xml");
            using (Stream stream = StreamFinder.GetFileStream("file7.xml", FileMode.Open))
            {
                pos = FileIO.ReadFromStream <Position>(stream);
            }
            Assert.AreEqual(pos.X, 2);
            Assert.AreEqual(pos.Y, 4);
            Assert.AreEqual(pos.Z, 6);
        }
Exemplo n.º 17
0
        public void validate_read_from_binary_stream()
        {
            var name         = Assembly.GetExecutingAssembly().FullName;
            var assemblyName = new AssemblyName(name).Name;
            int size         = 100;
            var arrayWritten = new double[size];

            // read file from resources and write it so that can be read in
            arrayWritten = (double[])FileIO.ReadArrayFromBinaryInResources <double>
                               ("Resources/fileiotest/ROfRho", assemblyName, size);
            FileIO.WriteToBinary <double[]>(arrayWritten, "array5");
            double[] arrayRead = new double[100];
            using (Stream stream = StreamFinder.GetFileStream("array5", FileMode.Open))
            {
                arrayRead = FileIO.ReadFromBinaryStream <double[]>(stream);
            }
            Assert.IsTrue(Math.Abs(arrayRead[2] - 0.052445) < 0.000001);
        }
Exemplo n.º 18
0
        public void validate_write_to_binary_stream_and_read_from_binary_stream()
        {
            // first create stream, write array, validate written and close stream
            double[] array = new double[3] {
                10, 11, 12
            };
            Stream streamWrite = StreamFinder.GetFileStream("array4", FileMode.Create);

            FileIO.WriteToBinaryStream(array, streamWrite);
            Assert.IsNotNull(streamWrite);
            Assert.IsTrue(FileIO.FileExists("array4"));
            Assert.IsTrue(new FileInfo("array4").Length != 0);
            streamWrite.Close();
            // then open stream, read array, validate values and close stream
            Stream streamRead = StreamFinder.GetFileStream("array4", FileMode.Open);
            var    data       = FileIO.ReadFromBinaryStream <double[]>(streamRead);

            Assert.AreEqual(data[0], 10);
            streamRead.Close();
        }
Exemplo n.º 19
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="importFiles">the name(s) of the file(s) to import</param>
        /// <param name="importPath">the path of the files to import (relative or absolute)</param>
        /// <param name="outname">the name of the resulting output xml spectral dictionary</param>
        /// <param name="outpath">the output directory of the generated xml dictionary (relative or absolute)</param>
        public static ChromophoreSpectrumDictionary ImportSpectraFromFile(
            string[] importFiles = null,
            string importPath    = "",
            string outname       = "SpectralDictionary",
            string outpath       = "")
        {
            if (importFiles == null || importFiles.Length == 0 || string.IsNullOrEmpty(importFiles[0]))
            {
                importFiles = new string[] { "absorber-*.txt" };
            }

            var allFiles = importFiles.SelectMany(file => Directory.GetFiles(
                                                      importPath ?? Directory.GetCurrentDirectory(), file));

            var chromophoreDictionary = new ChromophoreSpectrumDictionary();

            logger.Info(() => "Importing spectral data files");
            foreach (var file in allFiles)
            {
                if (File.Exists(file))
                {
                    try
                    {
                        logger.Info("Importing file: " + file);
                        var stream = StreamFinder.GetFileStream(file, FileMode.Open);

                        SpectralDatabase.AppendDatabaseFromFile(chromophoreDictionary, stream);
                    }
                    catch (Exception e)
                    {
                        logger.Info("****  An error occurred while importing file: " + file);
                        logger.Info("Detailed error: " + e.Message);
                    }
                }
            }

            return(chromophoreDictionary);
        }
        private void MC_DownloadDefaultSimulationInput_Executed(object sender, ExecutedEventArgs e)
        {
            using (var stream = StreamFinder.GetLocalFilestreamFromSaveFileDialog("zip"))
            {
                if (stream != null)
                {
                    var files = SimulationInputProvider.GenerateAllSimulationInputs().Select(input =>
                                                                                             new
                    {
                        Name  = "infile_" + input.OutputName + ".txt",
                        Input = input
                    });

                    foreach (var file in files)
                    {
                        file.Input.ToFile(file.Name);
                    }
                    var allFiles = files.Concat(files);
                    FileIO.ZipFiles(files.Select(file => file.Name), "", stream);
                    logger.Info(() => "Template simulation input files exported to a zip file.\r");
                }
            }
        }
 private void Maps_ExportDataToText_Executed(object sender, SLExtensions.Input.ExecutedEventArgs e)
 {
     if (_mapData != null && _mapData.RawData != null && _mapData.XValues != null && _mapData.YValues != null)
     {
         using (var stream = StreamFinder.GetLocalFilestreamFromSaveFileDialog("txt"))
         {
             if (stream != null)
             {
                 using (StreamWriter sw = new StreamWriter(stream))
                 {
                     sw.Write("% X Values:\t");
                     _mapData.XValues.ForEach(x => sw.Write(x + "\t"));
                     sw.WriteLine();
                     sw.Write("% Y Values:\t");
                     _mapData.YValues.ForEach(y => sw.Write(y + "\t"));
                     sw.WriteLine();
                     sw.Write("% Map Values:\t");
                     _mapData.RawData.ForEach(val => sw.Write(val + "\t"));
                     sw.WriteLine();
                 }
             }
         }
     }
 }