private XYDataArray calibrateSpectrum(DataReader DUTFile,DataReader REFFile,DataReader CALFile,ref XYDataArray deviceQE,
            ref XYDataArray devicePhotocurrent, ref XYDataArray calibratedREF,double lowerbound,double upperbound)
        {
            XYDataArray origCAL = new XYDataArray(ref CALFile.dataArray1, ref CALFile.dataArray2);
            //newCAL is the calibrated photodiode QE based on the wavelength scanned in reference file
            XYDataArray newCAL = origCAL.generateNewXYArray(ref REFFile.dataArray1);

            XYDataArray origREF = new XYDataArray(ref REFFile.dataArray1, ref REFFile.dataArray2);
            //newCAL = newCAL.YByACoef(0.01);
            newCAL = newCAL.Yinverse();

            double[] tempArr = newCAL.getDataArray(0);
            //the array size of newCAL may be less the origREF.Xarray,
            //so we need to generate the new array again
            calibratedREF = origREF.generateNewXYArray(ref tempArr);

            calibratedREF = calibratedREF.Yproduct(ref newCAL);

            //This data array is for keeping the for the original photocurrent data
            devicePhotocurrent = new XYDataArray(ref DUTFile.dataArray1, ref DUTFile.dataArray2);

            //This data array is for later conversion
            deviceQE = new XYDataArray(ref DUTFile.dataArray1, ref DUTFile.dataArray2);

            XYDataArray calibratedREFtoDev = calibratedREF.generateNewXYArray(ref DUTFile.dataArray1);
            tempArr = calibratedREFtoDev.getDataArray(0);
            deviceQE = deviceQE.generateNewXYArray(ref tempArr);
            calibratedREFtoDev = calibratedREFtoDev.Yinverse();
            deviceQE = deviceQE.Yproduct(ref calibratedREFtoDev);
            //deviceQE = deviceQE.YByACoef(100);
            deviceQE.showData();

            //trim the data based on the selected range
            XYDataArray tmp_deviceQE = deviceQE.selectRange(lowerbound, upperbound);
            deviceQE = tmp_deviceQE;

            XYDataArray tmp_devicePC = deviceQE.selectRange(lowerbound, upperbound);
            devicePhotocurrent = tmp_devicePC;

            XYDataArray tmp_calibratedREF = calibratedREF.selectRange(lowerbound, upperbound);
            calibratedREF = tmp_calibratedREF;

            return deviceQE;
        }
        private void convertSingleRange()
        {
            DataReader CALFile = new DataReader(@"C:\SpectralResponseAQSettingFiles\818-UV_calibration_report.csv", 0, 1);

            if (detector1SettingBox.SelectedIndex == 0)
            {
                CALFile = new DataReader(@"C:\SpectralResponseAQSettingFiles\818-UV_calibration_report.csv", 0, 1);
            }
            else if (detector1SettingBox.SelectedIndex == 1)
            {
                CALFile = new DataReader(@"C:\SpectralResponseAQSettingFiles\818-IR-L_calibration_report.csv", 0, 2);
            }

            DataReader REFFile = new DataReader(range1REFFullFilePath, LoadFileType.KHcsv);
            DataReader DUTFile = new DataReader(DUTFullFilePath, LoadFileType.KHcsv);

            string DUTFileName = System.IO.Path.GetFileNameWithoutExtension(DUTFullFilePath);
            string DUTDir = System.IO.Path.GetDirectoryName(DUTFullFilePath);
            string DUText = System.IO.Path.GetExtension(DUTFullFilePath);
            string newDUTFullPath = System.IO.Path.Combine(DUTDir, DUTFileName + "_converted" + DUText);

            XYDataArray origCAL = new XYDataArray(ref CALFile.dataArray1, ref CALFile.dataArray2);
            //newCAL is the calibrated photodiode QE based on the wavelength scanned in reference file
            XYDataArray newCAL = origCAL.generateNewXYArray(ref REFFile.dataArray1);

            XYDataArray origREF = new XYDataArray(ref REFFile.dataArray1, ref REFFile.dataArray2);
            //newCAL = newCAL.YByACoef(0.01);
            newCAL = newCAL.Yinverse();

            double[] tempArr = newCAL.getDataArray(0);
            //the array size of newCAL may be less the origREF.Xarray,
            //so we need to generate the new array again
            XYDataArray calibratedREF = origREF.generateNewXYArray(ref tempArr);
            //store calibratedREF data for writing into data file
            double[] REFPhotocurrent = calibratedREF.getDataArray(1);

            calibratedREF = calibratedREF.Yproduct(ref newCAL);

            XYDataArray deviceQE = new XYDataArray(ref DUTFile.dataArray1, ref DUTFile.dataArray2);
            XYDataArray calibratedREFtoDev = calibratedREF.generateNewXYArray(ref DUTFile.dataArray1);
            tempArr = calibratedREFtoDev.getDataArray(0);
            deviceQE = deviceQE.generateNewXYArray(ref tempArr);
            calibratedREFtoDev = calibratedREFtoDev.Yinverse();
            deviceQE = deviceQE.Yproduct(ref calibratedREFtoDev);
            deviceQE = deviceQE.YByACoef(100);
            deviceQE.showData();

            QEDataWriter dWriter = new QEDataWriter(DUTFile.headerPart);
            dWriter.addColumn("Wavelength (nm)", deviceQE.getDataArray(0));
            dWriter.addColumn("EQE (%)", deviceQE.getDataArray(1));
            dWriter.addColumn("Photocurrent (nA)", DUTFile.dataArray2);
            dWriter.addColumn("Reference (nA)", REFPhotocurrent);
            //dWriter.writeDataIntoFile(@"C:\TEMP\testoutput.csv");
            //dWriter.writeDataIntoDBFormatFile(@"C:\TEMP\testoutput2.csv");
            dWriter.writeDataAndHeaderIntoFile(newDUTFullPath);
        }