// Spectra Profile // Analyze Rainbow Optics diffraction grating spectra. // The spectra is horziontal and to the right side of the zero mode // The Spectrum Vertical Offset is for adjusting a few pixels slant horziontal (in my case 3 pixels clockwise slant.) // This macro requres fit files in C:\Spectra\ // The defaults are setup for Quasar KUV18217+6419 in KUV18217+6419c.fit file. // This ImageJ macro was development with ImageJ 1.43u // This macro does not work if selection goes off the image edge. // Max profile size is 590 pixels. // // // Clear and dark skies, // David Haworth // http://www.stargazing.net/david userSelect = false; requires("1.43u"); versionNum = 2.1; dateTime = dateTimeStr(); run("Input/Output...", "jpeg=98 gif=-1 file=.xls copy"); objectName = "KUV18217+6419"; path = "C:\\Spectra\\"; fileName = "KUV18217+6419c.fit"; plotStartpixel= 162; selectionX = 540; selectionXwidth = 477; angstroms = 10000000000; gratingRes = 0.000005; gratingDisToCCD = 0.01832719265135; pixelSize = 0.0000068; xCentroid = 540; yCentroid = 300; selectionHeight = 6; selectionHeightOffset = 3; gratingFilter ="RO200"; specAdjustB = -2.5982; specAdjustHa = 357; specAdjustHg = 18.3653; specAdjustDelta =136; bgX=0; bgY=0; bgWidth=0; bgHeight=0; bgCount=0; bgMean=0; bgMin=0; bgMax=0; bgStd=0; bgHist=0; zoom=1; fixWindowLocation = true; if(userSelect == true) { run("Measure"); xCentroid = getResult("X"); yCentroid = getResult("Y"); } Dialog.create("Spectrum Calibration V"+versionNum+" Macro"); if(userSelect == false) { Dialog.addMessage("Defaut Values are for vega08062010-01605s.fit"); Dialog.addString("Object Name", objectName); Dialog.addString("Source Fit File in C:\\Spectra\\", fileName); } else { Dialog.addString("Object Name", objectName);} Dialog.addNumber("Zero_Mode_X_Centroid", xCentroid); Dialog.addNumber("Zero_Mode_Y_Centroid", yCentroid); Dialog.addNumber("X_Centroid_to_End_of_Spectrum_Width", selectionXwidth); Dialog.addNumber("Spectrum _Plot_Start", plotStartpixel); Dialog.addNumber("Spectrum_Selection_Height", selectionHeight); Dialog.addNumber("Spectrum_Selection_Vertical_Offset", selectionHeightOffset); Dialog.addChoice("Grating_Filter", newArray("RO200", "SA100")); Dialog.addNumber("Grating_Distance_to_CCD_in_mm", gratingDisToCCD*1000); Dialog.addNumber("CCD_Pixel_Size_in_Microns", pixelSize*1000000); Dialog.addCheckbox("Save_Spectrum_Table", true); Dialog.addCheckbox("Show_Process_Spectrum", true); Dialog.addCheckbox("Show_Spectrum_Surface_Plot", true); Dialog.addCheckbox("Show_Halpha_Emission Line", true); Dialog.addCheckbox("Show_Hbeta_Emission_Line", true); Dialog.addCheckbox("Show_Hgamma_Emission_Line", true); Dialog.addCheckbox("Show_Hdelta_Emission_Line", false); Dialog.addCheckbox("Show_Hepsilon_Emission_Line", false); Dialog.addCheckbox("Show_Hzeta_Emission_Line", false); Dialog.addCheckbox("Show _O_III_Emission_Line", true); Dialog.addCheckbox("Show_Emission_Lines_with_Redshift", true); Dialog.addNumber("Redshift", 0.297000); Dialog.addCheckbox("Fixed_Windows_Locations", true); Dialog.show(); objectName = Dialog.getString(); if(userSelect == false) { fileName = Dialog.getString();} xCentroid = Dialog.getNumber(); yCentroid = Dialog.getNumber(); selectionXwidth = Dialog.getNumber(); plotStartpixel = Dialog.getNumber(); selectionHeight = Dialog.getNumber(); selectionHeightOffset = Dialog.getNumber(); gratingFilter = Dialog.getChoice(); gratingDisToCCD = Dialog.getNumber()/1000; pixelSize = Dialog.getNumber()/1000000; saveCalibratedSpectrumTable = Dialog.getCheckbox(); showProcessSpectrum = Dialog.getCheckbox(); showSpectrumSurfacePlot = Dialog.getCheckbox(); showHalpha = Dialog.getCheckbox(); showHbeta = Dialog.getCheckbox(); showHgamma = Dialog.getCheckbox(); showHdelta = Dialog.getCheckbox(); showHepsilon = Dialog.getCheckbox(); showHzeta = Dialog.getCheckbox(); showO_III = Dialog.getCheckbox(); showRedshiftElements = Dialog.getCheckbox(); nedRedshift = Dialog.getNumber(); fixWindowLocation = Dialog.getCheckbox(); analyzeDir = path+objectName+"_"+dateTime+File.separator; File.makeDirectory(analyzeDir); fileNameSave = objectName+"_"+dateTime; pathFileNameSave = analyzeDir+fileNameSave; selectionX = xCentroid; selectionY = yCentroid; if(gratingFilter == "SA100") gratingRes = gratingRes*2; angstromsMin = sin(atan(pixelSize*(plotStartpixel)/gratingDisToCCD))*gratingRes*angstroms; angstromsMax = sin(atan(pixelSize*(selectionXwidth)/gratingDisToCCD))*gratingRes*angstroms; // Close Log and Profile window if (isOpen("Log")) { selectWindow("Log"); run("Close");} if (isOpen("Profile")) {selectWindow("Profile"); run("Close");} if(userSelect == false) { // Open file, adjust black point to min-10 and white point min+60 open(path+fileName); run("Flip Vertically"); } centroidPixelValue= getPixel(xCentroid, yCentroid); if(bitDepth()!=32) run("32-bit"); if((getWidth-xCentroid) < selectionXwidth) selectionXwidth = (getWidth-xCentroid); saveAs("FITS", pathFileNameSave); run("Set Measurements...", " min centroid redirect=None decimal=4"); run("Measure"); getRawStatistics(count, mean, min, max, std, hist); run("Brightness/Contrast..."); setMinAndMax(min-10, min+60); // Selection and get profile selectWindow(fileNameSave+".fits"); makeRectangle(xCentroid-10, yCentroid-10, 20, 20); run("Measure"); xCentroidMeas = getResult("X"); yCentroidMeas = getResult("Y"); // Background Measurements bgX=selectionX+60; bgY=selectionY+7; bgWidth=100; bgHeight=30; specTitle3 = getTitle; makeRectangle(bgX, bgY-(bgHeight/2), bgWidth, bgHeight); getRawStatistics(bgCount, bgMean, bgMin, bgMax, bgStd, bgHist); run("Capture Image"); saveAs("Tiff", pathFileNameSave+"BgSelection.tif"); bgX=selectionX-30; bgHeight=80; bgWidth=selectionXwidth+30; makeRectangle(bgX/zoom, (bgY-(bgHeight/2))/zoom, bgWidth/zoom, bgHeight/zoom); run("Crop"); saveAs("Jpeg", pathFileNameSave+"BgSelectionCrop.jpg"); if(fixWindowLocation == true) setLocation(650,700); selectWindow(specTitle3); // Spectrums Measurements makeRectangle(selectionX, selectionY-(selectionHeight/2)+selectionHeightOffset, selectionXwidth, selectionHeight); profile = getProfile(); // Create spectrum tables if(saveCalibratedSpectrumTable ==true) spec_max = createSpecTables (profile, plotStartpixel, pixelSize, gratingDisToCCD, gratingRes, angstroms, specAdjustB, specAdjustHa, specAdjustHg, specAdjustDelta, pathFileNameSave); // Plot spectrum profile starting at about 3,000 angstroms maxProfileSize = selectionXwidth-plotStartpixel; if(maxProfileSize>433) { maxProfileSize=433; angstromsMax = ((sin(atan(pixelSize*(plotStartpixel+maxProfileSize)/gratingDisToCCD))*gratingRes*angstroms)+specAdjustB)+((specAdjustHa-maxProfileSize)*specAdjustHg/specAdjustDelta); } makeRectangle(selectionX+plotStartpixel, selectionY-(selectionHeight/2)+selectionHeightOffset, maxProfileSize, selectionHeight); profile = getProfile(); xValues = getProfile(); spec_min =profile[0]; for (i=0; i profile[i]) spec_min = profile[i] ; spec_max =profile[0]; for (i=0; i profile[i]) spec_min = profile[i] ; spec_max =profile[0]; for (i=plotStartpixel; i profile[i]) spec_min = profile[i] ; spec_max =profile[plotStartpixel]; for (i=plotStartpixel; i (profile.length-1)) x1 = profile.length-1; else x1 = (x/3)+1; setPixel(x, y, profile8bit[x/3]); setPixel((x+1), y, (profile8bit[x/3]+(profile8bit[x1]-profile8bit[x/3])/3)); setPixel((x+2), y, (profile8bit[x/3]+(profile8bit[x1]-profile8bit[x/3])*2/3)); } } selectWindow("Spectrum3X"); setMinAndMax(0, 255); run("8-bit"); saveAs("Jpeg", pathFileNameSave+"Band3X.jpg"); saveAs("Tiff", pathFileNameSave+"Band3X.tif"); if(fixWindowLocation == true) setLocation(60,0); return specBandTitle; } // ********************************************************************************************************************************* // Create 3-D surface plot of spectrum function plotSpectrum (fileNameSave) { if (isOpen("Surface Plot")) {selectWindow("Surface Plot"); run("Close");} selectWindow(fileNameSave+".fits"); makeRectangle(selectionX+plotStartpixel, selectionY-(selectionHeight/2)+selectionHeightOffset, maxProfileSize, selectionHeight); run("Measure"); getRawStatistics(count, mean, min, max, std, hist); run("Brightness/Contrast..."); setMinAndMax(min-10, max); selectWindow(fileNameSave+".fits"); run("Surface Plot...", "polygon=100 shade draw_axis fill one smooth"); if(fixWindowLocation == true) setLocation(0,300); run("Capture Image"); saveAs("Jpeg", pathFileNameSave+"Surface.jpg"); saveAs("Tiff", pathFileNameSave+"Surface.tif"); run("Close"); } // ********************************************************************************************************************************* // Draw selection, crop image and save it function cropImageWithSelection (pathFileNameSave, fileNameSave) { selectWindow(fileNameSave+".fits"); run("Select None"); run("Duplicate...", "title=copy.fits"); setColor(65535); drawRect(selectionX+plotStartpixel-1, selectionY-(selectionHeight/2)+selectionHeightOffset-1, maxProfileSize+2, selectionHeight+2); run("Select None"); makeRectangle(selectionX-20, selectionY-20, selectionXwidth+40, selectionHeight+40); setPixel(xCentroid, yCentroid, 0); run("Crop"); if(fixWindowLocation == true) setLocation(650,600); saveAs("FITS", pathFileNameSave+"CropWithSelection"); run("Capture Image"); saveAs("Jpeg", pathFileNameSave+"CropWithSelection.jpg"); saveAs("Tiff", pathFileNameSave+"CropWithSelection.tif"); run("Close"); } // ********************************************************************************************************************************* // Crop image and save it function cropImage (pathFileNameSave, fileNameSave, specBandTitle) { selectWindow(fileNameSave+".fits"); run("Select None"); makeRectangle(selectionX-20, selectionY-10, selectionXwidth+40, selectionHeight+60); run("Crop"); saveAs("FITS", pathFileNameSave+"Crop"); specTitle2 = getTitle; run("Capture Image"); saveAs("Jpeg", pathFileNameSave+"Crop.jpg"); saveAs("Tiff", pathFileNameSave+"Crop.tif"); specTitle = getTitle; selectWindow(specBandTitle); run("Select All"); run("Copy"); run("Close"); setPasteMode("Transparent-zero"); selectWindow(specTitle); run("Paste"); makeRectangle(0, 0, getWidth, getHeight-10); run("Crop"); saveAs("Jpeg", pathFileNameSave+"SpecMerge.jpg"); saveAs("Tiff", pathFileNameSave+"SpecMerge.tif"); if(fixWindowLocation == true) setLocation(650,500); selectWindow(specTitle2); run("Close"); } // ********************************************************************************************************************************* // Save analysis setup info in Log function saveInfo (pathFileNameSave, fileNameSave) { print("Spectra Profile Version "+ versionNum); print("Object Name: "+ objectName); print("Source File Name: "+ fileName); print("Save Files Directory Path: "+ analyzeDir); print("Save file name: "+ fileNameSave+".fits"); print(fileNameSave+"CropWithSelection.fits Image Width: "+ getWidth +" pixels"); print(fileNameSave+"CropWithSelection.fits Image Width: "+ getHeight +" pixels"); print(" "); print("Zero Mode X Centroid Entered: "+ xCentroid+" pixels, Measured: "+xCentroidMeas+" pixels"); print("Zero Mode Y Centroid Entered: "+ yCentroid+" pixels, Measured: "+yCentroidMeas+" pixels"); print("Pixel Value at Centroid: "+ centroidPixelValue+" ADU"); print(" "); print("Zero Mode Spectrum Width: "+ selectionXwidth +" pixels"); print("Spectrum Selection Plot Start: "+ plotStartpixel +" pixels"); print("Spectrum Selection Vertical Height: "+ selectionHeight+" pixels"); print("Spectrum Selection Vertical Offset: "+ selectionHeightOffset+" pixels"); print("Zero Mode Spectrum Selection X: "+ selectionX+" pixels"); print("Zero Mode Spectrum Selection Y: "+ selectionY-(selectionHeight/2)+selectionHeightOffset+" pixels"); print(" "); print("Spectrum Plot Selection X: "+ selectionX+plotStartpixel+" pixels"); print("Spectrum Plot Selection Y: "+ selectionY-(selectionHeight/2)+selectionHeightOffset+" pixels"); print("Spectrum Plot Selection Width: "+ selectionXwidth-plotStartpixel+" pixels"); print("Spectrum Plot Selection Start: "+ plotStartpixel+" pixels"); print(" "); print("Spectrum Plot Selection Start: "+ angstromsMin+" angstroms"); print("Spectrum Plot Selection End: "+ angstromsMax+" angstroms"); print("Spectrum Plot Average: "+ (angstromsMax-angstromsMin)/(selectionXwidth-plotStartpixel)+" angstroms/pixel"); print(" "); print("Spectrum Plot Maximum: "+ spec_min+" ADU"); print("Spectrum Plot Minimum: "+ spec_max+" ADU"); print(" "); print("Grating Distance to CCD is "+ gratingDisToCCD*1000+" mm"); print("CCD pixel size is "+ pixelSize*1000000+" microns"); print(" "); print("Non linear spectrum adjustment, Halpha angstroms offset: "+ specAdjustB+" angstroms"); print("Non linear spectrum adjustment, Halpha pixel position "+ specAdjustHa+" pixels"); print("Non linear spectrum adjustment, Hgamma angstroms offset: "+ specAdjustHg+" angstroms"); print("Non linear spectrum adjustment, Hgamma to Halpha a pixel offset: "+ specAdjustDelta+" pixels"); print(" "); print("Redshift z is "+ nedRedshift); print("Halpha ("+h_alpha+" angstroms) at "+nedRedshift+" redshift is "+ h_alphaAtRedshft + " angstroms"); print("Hbeta ("+ h_beta+" angstroms) at "+nedRedshift+" redshift is "+ h_betaAtRedshft + " angstroms"); print("Hgamma ("+h_gamma+" angstroms) at "+nedRedshift+" redshift is "+ h_gammaAtRedshft + " angstroms"); print("Hdelta ("+h_delta+" angstroms) at "+nedRedshift+" redshift is "+ h_deltaAtRedshft + " angstroms"); print("Hepsilon ("+h_epsilon+" angstroms) at "+nedRedshift+" redshift is "+ h_epsilonAtRedshft + " angstroms"); print("Hzeta ("+h_zeta+" angstroms) at "+nedRedshift+" redshift is "+ h_zetaAtRedshft + " angstroms"); print("O[III] ("+o_III+" angstroms) at "+nedRedshift+" redshift is "+ o_IIIAtRedshft + " angstroms"); print(" "); if (saveCalibratedSpectrumTable== true) print("Save Calibrated Spectrum Table"); if (showProcessSpectrum == true) print("Show Process Spectrum"); if (showSpectrumSurfacePlot == true)print("Show Spectrum Surface Plot"); if (showRedshiftElements == true)print("Show Redshft Elements"); print(" "); print("Background Selection Top Left X, Y: "+ bgX+", "+bgY); print("Background Selection Width: "+ bgWidth); print("Background Selection Height: "+ bgHeight); print("BackgroundSelection Bottom Right X, Y: "+ (bgX+bgWidth)+", "+bgY+(bgHeight)); print(" "); print("Background Count: "+ bgCount); print("Background Mean: "+ bgMean); print("Background Minimum: "+ bgMin); print("Background Maximum: "+ bgMax); print("Background Standard Deviation: "+ bgStd); print("Background Mean/Standard Deviation: "+ bgMean/bgStd); print("Spectrum Maximum: "+ spec_max); print("(Spectrum Maximum Signal - Background Mean)/(Background Standard Deviation) = Spectrum to Noise Ratio: "+ (spec_max-bgMean)/bgStd); print("End"); selectWindow("Log"); saveAs("Text", pathFileNameSave+"Log.txt"); run("Close"); } // Macro End ******************************************************************************************************************