コード例 #1
0
        // Constructor given a file (pass by reference!)
        public fileHandler(ref System.IO.StreamReader myFile, string myFileName)
        {
            //*************************************//
            // Metadata format
            // ---------------
            //
            // "Spectroscopy data file"
            // date
            // "Trap frequency:"
            // trapFrequency
            // "Trap voltage:"
            // trapVoltage
            // "AOM Start frequency:"
            // startFrequency
            // "Step size:"
            // stepSize
            // "Number of repeats per frequency:"
            // repeats
            // "File contains interleaved spectra:"
            // numberInterleaved
            // "This is sideband:"
            // sidebandNumber
            // "Starting pulse length (fixed):"
            // startLength
            // "Number of steps (fixed):"
            // numberOfSteps (fixed)
            // "Spectrum i name":
            // spectrumName[i]
            // "Notes:"
            // Notes section - all lines should start with a #
            // "Data:"


            // Two "name" labels - one in file, one for displaying on graph
            // Name spectra on creation rather than when loading file?
            // Include notes section - parse & display in a window

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

            // String to temporarily store data from the file
            string myString = myFile.ReadLine();              // Read first line of file

            // Make sure it is a valid data file - check for metadata
            if (myString == "Spectroscopy data file")
            {
                //******************************//
                // Processing metadata
                // Just dump it into an array of strings

                metadata = new string[25];

                // Next 36 lines are misc metadata with titles
                for (int i = 0; i < 18; i++)
                {
                    metadata[i] = myFile.ReadLine();               // First line is actual metadata
                    myString    = myFile.ReadLine();               // Alternating lines are just a title (throw away)
                }

                float stepSizekHz, startFrequencyMHz;

                // Store crucial metadata - no. of repeats, no. interleaved, step size, start freq
                // int.TryParse(myString, out myInt) converts myString to an int and stores it in myInt
                // then returns true if it was successful, false otherwise

                if (int.TryParse(metadata[13], out repeats) && int.TryParse(metadata[14], out numberInterleaved) &&
                    float.TryParse(metadata[9], out stepSizekHz) && float.TryParse(metadata[7], out startFrequencyMHz))
                {
                    if (metadata[1] == "Fixed")
                    {
                        // Make sure starting pulse length is an int
                        if (int.TryParse(metadata[16], out startLength))
                        {
                            // Don't convert - this is a number of ticks
                            stepSize = (int)stepSizekHz;
                        }
                        else
                        {
                            MessageBox.Show("Error reading metadata (pulse length not an int)");
                        }
                    }
                    else
                    {
                        // These need converting to Hz and storing as ints
                        stepSize = (int)(stepSizekHz * 1000);
                    }
                    // Always convert start freq to int
                    startFrequency = (int)(startFrequencyMHz * 1000000);
                }
                else
                {
                    MessageBox.Show("Error reading metadata");
                }

                spectrumNames = new string[numberInterleaved];

                // Depending on number of interleaved spectra, store the names in the array
                for (int i = 0; i < numberInterleaved; i++)
                {
                    if (i < 5) // Make sure we don't take more than 5 spectra from file
                    {
                        // Put spectrum name into arrays for both metadata and spectrumNames
                        // ( Need spectrumNames for spectrumSelect dialog)
                        // Bit messy but it's easiest to code this way for now, maybe tidy later
                        // using substrings
                        spectrumNames[i] = myFile.ReadLine();
                        metadata[i + 18] = spectrumNames[i];
                        myString         = myFile.ReadLine();
                    }
                }



                myString = myFile.ReadLine();               // Read first line of notes section
                // Keep reading lines while each line begins with a #
                while (myString[0] == '#')
                {
                    notes   += myString.Substring(1);
                    notes   += System.Environment.NewLine;
                    myString = myFile.ReadLine();
                }


                // NB this will read one line PAST the end of the notes section. This should be the line that says "Data:"
                // Check this - if not then there will be errors reading the data
                if (myString != "Data:")
                {
                    MessageBox.Show("Error: File corrupted (wrong metadata format?)");
                    return;
                }

                // Store in array of metadata
                if (18 + numberInterleaved < 25 && numberInterleaved != 0)
                {
                    metadata[18 + numberInterleaved] = notes;
                }
                else
                {
                    Console.WriteLine("Too many interleaved spectra - gone beyond the bounds of metadata array (line 184 fileHandler.cs)");
                }

                // Process the actual numerical data
                this.processData(ref myFile);
            }   // If there is no metadata
            else if (myString == "Spectroscopy data file (no metadata)")
            {
                // Open a form requesting metadata (start freq, repeats, step size, number of spectra)
                // & wait for it to be closed before continuing
                requestMetadata myRequestMetadata = new requestMetadata(ref myFileName);
                myRequestMetadata.ShowDialog();

                // Check that user has pressed ok
                if (myRequestMetadata.DialogResult == DialogResult.OK)
                {
                    // Set metadata from user input on form
                    startFrequency    = myRequestMetadata.startFreq;
                    stepSize          = myRequestMetadata.stepSize;
                    repeats           = myRequestMetadata.repeats;
                    numberInterleaved = myRequestMetadata.numberInterleaved;

                    // Need to initialise this array
                    spectrumNames = new string[numberInterleaved];
                    for (int i = 0; i < numberInterleaved; i++)
                    {
                        spectrumNames[i] = "Default";

                        // Make sure we are not outside the bounds of the array
                        if (i + 18 < 24)
                        {
                            // Store default name in metadata
                            metadata[i + 18] = spectrumNames[i];
                        }
                    }


                    // Just process the raw data
                    this.processData(ref myFile);

                    // Store what we have in the metadata array
                    metadata[8]  = stepSize.ToString();
                    metadata[9]  = stepSize.ToString();
                    metadata[13] = repeats.ToString();
                    metadata[14] = numberInterleaved.ToString();
                }
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("File not recognised");
            }
        }
コード例 #2
0
ファイル: fileHandler.cs プロジェクト: PavH/Spectroscopy
        // Constructor given a file (pass by reference!)
        public fileHandler(ref System.IO.StreamReader myFile, string myFileName)
        {
            //*************************************//
            // Metadata format
            // ---------------
            //
            // "Spectroscopy data file"
            // date
            // "Trap frequency:"
            // trapFrequency
            // "Trap voltage:"
            // trapVoltage
            // "AOM Start frequency:"
            // startFrequency
            // "Step size:"
            // stepSize
            // "Number of repeats per frequency:"
            // repeats
            // "File contains interleaved spectra:"
            // numberInterleaved
            // "This is sideband:"
            // sidebandNumber
            // "Starting pulse length (fixed):"
            // startLength
            // "Number of steps (fixed):"
            // numberOfSteps (fixed)
            // "Spectrum i name":
            // spectrumName[i]
            // "Notes:"
            // Notes section - all lines should start with a #
            // "Data:"


            // Two "name" labels - one in file, one for displaying on graph
            // Name spectra on creation rather than when loading file?
            // Include notes section - parse & display in a window

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

            // String to temporarily store data from the file
            string myString = myFile.ReadLine();              // Read first line of file

            // Make sure it is a valid data file - check for metadata
            if (myString == "Spectroscopy data file")
            {
                //******************************//
                // Processing metadata
                // Just dump it into an array of strings

                metadata = new string[25];

                // Next 36 lines are misc metadata with titles
                for (int i = 0; i < 18; i++)
                {
                    metadata[i] = myFile.ReadLine();               // First line is actual metadata
                    myString = myFile.ReadLine();                  // Alternating lines are just a title (throw away)

                }
                
                float stepSizekHz, startFrequencyMHz;

                // Store crucial metadata - no. of repeats, no. interleaved, step size, start freq
                // int.TryParse(myString, out myInt) converts myString to an int and stores it in myInt
                // then returns true if it was successful, false otherwise

                if (int.TryParse(metadata[13], out repeats) && int.TryParse(metadata[14], out numberInterleaved)
                    && float.TryParse(metadata[9], out stepSizekHz) && float.TryParse(metadata[7], out startFrequencyMHz))
                {
                    if (metadata[1] == "Fixed")
                    {
                        // Make sure starting pulse length is an int
                        if (int.TryParse(metadata[16], out startLength) )
                        {
                            // Don't convert - this is a number of ticks
                            stepSize = (int)stepSizekHz;
                        }
                        else MessageBox.Show("Error reading metadata (pulse length not an int)");
                    }
                    else
                    {
                        // These need converting to Hz and storing as ints
                        stepSize = (int)(stepSizekHz * 1000);
                        
                    }
                    // Always convert start freq to int
                    startFrequency = (int)(startFrequencyMHz * 1000000);
                }
                else
                {
                    MessageBox.Show("Error reading metadata");
                }

                spectrumNames = new string[numberInterleaved];

                // Depending on number of interleaved spectra, store the names in the array
                for (int i = 0; i < numberInterleaved; i++)
                {
                    if (i < 5) // Make sure we don't take more than 5 spectra from file
                    {
                        // Put spectrum name into arrays for both metadata and spectrumNames
                        // ( Need spectrumNames for spectrumSelect dialog)
                        // Bit messy but it's easiest to code this way for now, maybe tidy later
                        // using substrings
                        spectrumNames[i] = myFile.ReadLine();
                        metadata[i + 18] = spectrumNames[i];
                        myString = myFile.ReadLine();
                    }
                }



                myString = myFile.ReadLine();               // Read first line of notes section
                // Keep reading lines while each line begins with a #
                while (myString[0] == '#')
                {
                    notes += myString.Substring(1);
                    notes += System.Environment.NewLine;
                    myString = myFile.ReadLine();
                }


                // NB this will read one line PAST the end of the notes section. This should be the line that says "Data:"
                // Check this - if not then there will be errors reading the data
                if (myString != "Data:")
                {
                    MessageBox.Show("Error: File corrupted (wrong metadata format?)");
                    return;
                }

                // Store in array of metadata
                if (18 + numberInterleaved < 25 && numberInterleaved != 0) metadata[18 + numberInterleaved] = notes;
                else Console.WriteLine("Too many interleaved spectra - gone beyond the bounds of metadata array (line 184 fileHandler.cs)");

                // Process the actual numerical data
                this.processData(ref myFile);
  
            }   // If there is no metadata
            else if (myString == "Spectroscopy data file (no metadata)")
            {
                // Open a form requesting metadata (start freq, repeats, step size, number of spectra)
                // & wait for it to be closed before continuing
                requestMetadata myRequestMetadata = new requestMetadata(ref myFileName);
                myRequestMetadata.ShowDialog();

                // Check that user has pressed ok
                if (myRequestMetadata.DialogResult == DialogResult.OK)
                {
                    // Set metadata from user input on form
                    startFrequency = myRequestMetadata.startFreq;
                    stepSize = myRequestMetadata.stepSize;
                    repeats = myRequestMetadata.repeats;
                    numberInterleaved = myRequestMetadata.numberInterleaved;

                    // Need to initialise this array
                    spectrumNames = new string[numberInterleaved];
                    for (int i = 0; i < numberInterleaved; i++)
                    {
                        spectrumNames[i] = "Default";

                        // Make sure we are not outside the bounds of the array
                        if (i + 18 < 24)
                        {
                            // Store default name in metadata
                            metadata[i + 18] = spectrumNames[i];
                        }
                    }

                    
                    // Just process the raw data
                    this.processData(ref myFile);

                    // Store what we have in the metadata array
                    metadata[8] = stepSize.ToString();
                    metadata[9] = stepSize.ToString();
                    metadata[13] = repeats.ToString();
                    metadata[14] = numberInterleaved.ToString();         

                }

            }
            else System.Windows.Forms.MessageBox.Show("File not recognised");
        }