Ejemplo n.º 1
0
def run_model_grid(target_spec,target_err=None):
    mask_list= [lyman_alpha]+[oxygen]+[seg_gap]+[nitrogen]
    target_spec = spt.clean_spectrum(target_spec, min_wave, max_wave, mask_list)
    target_err = spt.clean_spectrum(target_err, min_wave, max_wave, mask_list)
    model_file_list = glob(model_path+'*'+model_extension)
    dist_list = []
    for model_file in model_file_list:
        model_spec = get_model_fromfile(model_file)
        #model_spec= spt.trim_spec(model_spec, np.min(target_spec[0]), np.max(target_spec[0]))
        model_spec= spt.clean_spectrum(model_spec, min_wave, max_wave, mask_list)
        scaling_coefficient= get_scale_factor(target_spec, model_spec, scale_wave_range)
        model_spec[1]=model_spec[1]*scaling_coefficient
        new_dist = calc_sq_dist(target_spec, model_spec, error_spec = target_err)
        dist_list.append(new_dist)
    teff_array, logg_array = make_model_params_arrays(model_file_list)
    dist_array = np.array(dist_list)
    for mod_file, dist_mod in zip(model_file_list, dist_list):
        print("model_file:", mod_file, "difference:", dist_mod)
    min_index = np.argmin(dist_list)
    min_model = model_file_list[min_index]
    min_dist = dist_array[min_index]
    print("best fit model:", min_model)
    model_spec= get_model_fromfile(min_model)
    #model_spec = spt.trim_spec(model_spec, np.min(target_spec[0]), np.max(target_spec[0]))
    model_spec= spt.clean_spectrum(model_spec, np.min(target_spec[0]), np.max(target_spec[0]), mask_list)
    scaling_coefficient= get_scale_factor(target_spec, model_spec, scale_wave_range)
    model_spec[1]= model_spec[1]*scaling_coefficient
    calc_rdist(scaling_coefficient)
    plot_overlays(target_spec, model_spec, target_err)
    chi_square_countours(teff_array,logg_array, dist_array)
Ejemplo n.º 2
0
def retrieve_target_spec(target_name):
    """
    Input:
    -target_name : the directory name of the target in HST_COS. It should exclude the slash
    """
    target_path = target_name+'/' 
    target_x1ds= glob(target_path+'*x1dsum.fits')
    target_file= target_x1ds[-1] #I only want the first one for now; when I eventually make this include all the visits for a given target I'll introduce some sort of function for combining them or whatever
    target_hdu= fits.open(target_file)
    cenwave=target_hdu[0].header['CENWAVE']
    grating = target_hdu[0].header['OPT_ELEM']
    seg_gap = config.seg_gap_dict[str(grating)][str(cenwave)]
    lyman_alpha = config.lyman_mask
    oxygen= config.oxygen_mask
    nitrogen = config.nitrogen_mask
    print("grating (OPT_ELEM):", grating, "Centwave: ", target_hdu[0].header['CENWAVE'])
    #exposure_time = target_hdu[0].header['EXPTIME']
    target_waves = np.copy(target_hdu[1].data['wavelength'].ravel())
    target_flux= np.copy(target_hdu[1].data['flux'].ravel())
    target_error = np.copy(target_hdu[1].data['ERROR'].ravel())
    target_spec= np.vstack([target_waves, target_flux])
    target_err = np.vstack([target_waves, target_error])
    mask_list= [lyman_alpha]+[oxygen]+[seg_gap]+[nitrogen]
    target_spec = spt.clean_spectrum(target_spec, min_wave, max_wave, mask_list)
    target_err = spt.clean_spectrum(target_err, min_wave, max_wave, mask_list)
    return target_spec, target_err
Ejemplo n.º 3
0
def plot_all_x1d(target_dir, low_lim, high_lim, log_scale):
    wave_min = low_lim
    #lyman = [1208, 1225]
    #oxygen= [1295, 1312] #airglow wavelengths to be filtered according to lightcurve
    wave_max = high_lim
    #lcbase= target_dir+ '_grid_lightcurves/' + '*step' + str(stepsize) + '_*'+str(wave_min)+',' + str(wave_max)+'*'
    #lcfile= glob(lcbase)[0]
    #dest_dir = 'dual_plots/'
    #if not os.path.exists(dest_dir):
    #os.makedirs(dest_dir)

    for dataset in glob(target_dir + '/*x1d.fits'):
        hdu = fits.open(dataset)
        try:
            print(dataset, hdu[0].header['OPT_ELEM'], hdu[0].header['CENWAVE'],
                  hdu[1].header['EXPTIME'], "LP-POS: ",
                  hdu[0].header['LIFE_ADJ'], "FP-POS: ",
                  hdu[0].header['FPPOS'])
        except KeyError as error:
            print('KeyError:', error)
            print('filename that produced error:', dataset)
            print(dataset, hdu[0].header['OPT_ELEM'], hdu[0].header['CENWAVE'],
                  hdu[1].header['EXPTIME'], "LP-POS: ", "FP-POS: ",
                  hdu[0].header['FPPOS'])

    fig = plt.figure(figsize=(200, 9))
    #silicon_lines= [1190, 1193, 1195, 1197, 1207, 1260, 1265, 1304, 1309]
    ax1 = fig.add_subplot(1, 1, 1)

    counter = 0
    for dataset in glob(target_dir + '/*x1d.fits'):
        hdu = fits.open(dataset)
        #print(hdu[1])
        #print(hdu[1]['FUVA'])
        #print(hdu[1].data[0])
        #print(hdu[1].data.shape)
        #for thing in hdu[1].data:
        #print("thing: ", thing)
        #print(thing['wavelength'])
        fppos = hdu[0].header['FPPOS']
        fppos_color = fp_pos_colors[fppos]
        wavelengths = np.copy(hdu[1].data['wavelength'].ravel())
        print(wavelengths.shape)
        fluxes = np.copy(hdu[1].data['flux'].ravel())
        #fluxes= np.copy(hdu[1].data['Gcounts'].ravel())

        #print("fluxes",  fluxes)
        arrshape = wavelengths.shape

        target_spec = np.vstack([wavelengths, fluxes])
        mask_list = [config.lyman_mask] + [config.oxygen_mask] + [
            config.nitrogen_mask
        ]  #need to add the segment gap (not implemented directly in spec_plot_tools because it will eventually be called from a dict based on grating and central wavelength
        cleaned_spec = spt.clean_spectrum(target_spec, wave_min, wave_max,
                                          mask_list)
        fluxes = cleaned_spec[1]
        wavelengths = cleaned_spec[0]
        #print("fluxes.shape: ", fluxes.shape)
        #print("wavelengths.shape" , wavelengths.shape)

        if counter == 0:
            flux_all = np.array([np.zeros(fluxes.shape)])
            print("flux_all.shape: ", flux_all.shape)
            plot_waves = wavelengths
        try:
            flux_all = np.append(flux_all, [fluxes], axis=0)
            print(flux_all.shape)
        except ValueError as error:
            print(error)
            shape_dif = flux_all.shape[1] - fluxes.shape[0]
            if shape_dif > 0:
                padarray = np.zeros(shape_dif)
                fluxes_pad = np.append(fluxes, padarray)
                flux_all = np.append(flux_all, [fluxes_pad], axis=0)
            elif shape_dif < 0:
                flux_all = np.append(flux_all, [fluxes[:shape_dif]], axis=0)
                print("truncating new spectrum to mach previous dimensions")

        counter += 1
        #print("sum fluxes: ", np.sum(fluxes))
        ax1.plot(wavelengths,
                 fluxes,
                 label=hdu[1].header['EXPSTART'],
                 color=fppos_color)
    for this_line in config.silicon_lines:
        if ((this_line >= wave_min) & (this_line <= wave_max)):
            ax1.axvline(x=this_line,
                        linestyle='-',
                        color='g',
                        ymin=0,
                        ymax=100000,
                        linewidth=1,
                        alpha=0.2)
    print(flux_all.shape)
    flux_all = flux_all[1:, :]  #remove the first row of zeros
    flux_med = np.nanmedian(flux_all, axis=0)
    #flux_med= np.sum(flux_all, axis=0)
    ax1.plot(plot_waves,
             flux_med,
             label='median combined spectra',
             color=fp_pos_colors[0])
    ax1.legend(numpoints=1, fontsize=14, loc='best')
    ax1.set_ylabel('Flux (cgs)')
    ax1.set_xlabel('Wavelength $(\AA)$')
    ax1.set_title(target_dir)
    if log_scale:
        ax1.set_yscale('log')
        #bad_inds = np.where(
    #fig.tight_layout()
    fig.savefig(target_dir + 'x1dtruesum.pdf')
    plt.show()
Ejemplo n.º 4
0
def make_dual_plots(target_dir, stepsize, wave_limits=[1130, 1900]):
    wave_min = wave_limits[0]
    #lyman = [1208, 1225]
    #oxygen= [1295, 1312] #airglow wavelengths to be filtered according to lightcurve
    wave_max = wave_limits[1]
    lcbase = target_dir + '_grid_lightcurves/' + '*step' + str(
        stepsize) + '_*' + str(wave_min) + ',' + str(wave_max) + '*'
    lcfile = glob(lcbase)[0]
    dest_dir = 'dual_plots/' + target_dir + "/"
    if not os.path.exists(dest_dir):
        os.makedirs(dest_dir)

    for dataset in glob(target_dir + '/*x1dsum.fits'):
        hdu = fits.open(dataset)
        print(dataset, hdu[0].header['OPT_ELEM'], hdu[0].header['CENWAVE'],
              hdu[1].header['EXPTIME'])

    #moved 2019-07-15
    #fig = plt.figure(figsize=(20,9))
    ##silicon_lines= [1190, 1193, 1195, 1197, 1207, 1260, 1265, 1304, 1309]
    #ax1 = fig.add_subplot(2,1,1)

    counter = 0
    #get the mask degree used for the light curve to produce the overhead spectrum
    with open(lcfile, 'rb') as csvfile:
        reader = csv.reader(csvfile, delimiter=',')
        for row in reader:
            print('reading row')
            row = row[0]
            mask_deg = int(row.split('=')[1])
            print('mask_deg:', mask_deg)
            break

    #mask_deg= 1
    for dataset in glob(target_dir + '/*x1dsum.fits'):
        hdu = fits.open(dataset)
        cenwave = str(hdu[0].header['CENWAVE'])
        grating = str(hdu[0].header['OPT_ELEM'])
        try:
            seg_gap = config.seg_gap_dict[str(grating)][str(cenwave)]
        except KeyError as error:
            print(error)
            print("No segment gap mask for OPT_ELEM " + grating + ' at ' +
                  cenwave + ' A')
            seg_gap = [wave_max, wave_min]
        #print(hdu[1])
        #print(hdu[1]['FUVA'])
        #print(hdu[1].data[0])
        #print(hdu[1].data.shape)
        #for thing in hdu[1].data:
        #print("thing: ", thing)
        #print(thing['wavelength'])
        wavelengths = np.copy(hdu[1].data['wavelength'].ravel())
        print(wavelengths.shape)
        fluxes = np.copy(hdu[1].data['flux'].ravel())
        #print("fluxes",  fluxes)
        arrshape = wavelengths.shape
        target_spec = np.vstack([wavelengths, fluxes])
        mask_list = [config.lyman_mask_list[mask_deg]] + [
            config.oxygen_mask_list[mask_deg]
        ] + [config.nitrogen_mask_list[mask_deg]] + [seg_gap]
        #mask_list= [config.lyman_mask]+[config.oxygen_mask]+[config.nitrogen_mask] +[seg_gap]
        cleaned_spec = spt.clean_spectrum(target_spec, wave_min, wave_max,
                                          mask_list)
        wavelengths = cleaned_spec[0]
        fluxes = cleaned_spec[1]

        print("fluxes.shape: ", fluxes.shape)
        print("wavelengths.shape", wavelengths.shape)

        if counter == 0:
            flux_all = np.array([np.zeros(fluxes.shape)])
            print("flux_all.shape: ", flux_all.shape)
            plot_waves = wavelengths
        try:
            flux_all = np.append(flux_all, [fluxes], axis=0)
            print(flux_all.shape)
        except ValueError as error:
            print(error)
            shape_dif = flux_all.shape[1] - fluxes.shape[0]
            if shape_dif > 0:
                padarray = np.zeros(shape_dif)
                fluxes_pad = np.append(fluxes, padarray)
                flux_all = np.append(flux_all, [fluxes_pad], axis=0)
            elif shape_dif < 0:
                flux_all = np.append(flux_all, [fluxes[:shape_dif]], axis=0)
                print("truncating new spectrum to mach previous dimensions")

        counter += 1
        #print("sum fluxes: ", np.sum(fluxes))
        #ax1.plot(wavelengths, fluxes/np.nanmean(fluxes), label=hdu[0].header['rootname'])
    #for this_line in config.silicon_lines:
    #ax1.axvline(x= this_line ,linestyle = '-', color = 'g' , ymin = 0, ymax = 100000, linewidth = 1, alpha = 0.2)
    try:
        #moved here 2019-07-15
        fig = plt.figure(figsize=(20, 9))
        #silicon_lines= [1190, 1193, 1195, 1197, 1207, 1260, 1265, 1304, 1309]
        ax1 = fig.add_subplot(2, 1, 1)
        ##########################
        for this_line in config.silicon_lines:
            if ((this_line >= wave_min) & (this_line <= wave_max)):
                ax1.axvline(x=this_line,
                            linestyle='-',
                            color='g',
                            ymin=0,
                            ymax=100000,
                            linewidth=1,
                            alpha=0.2)
        print(flux_all.shape)
        flux_all = flux_all[1:, :]  #remove the first row of zeros
        flux_med = np.nanmedian(flux_all, axis=0)
        #ax1.plot(plot_waves, flux_med/np.nanmean(flux_med), label= 'median combined averaged spectra')
        ax1.plot(plot_waves,
                 flux_med,
                 label='median combined averaged spectra')
        ax1.legend(numpoints=1, fontsize=14, loc='best')
        #ax1.set_ylabel('Flux (normed)')
        ax1.set_ylabel('Flux (cgs units)')
        ax1.set_xlabel('Wavelength $(\AA)$')
        ax1.set_title(target_dir)
        #ax1.set_yscale('log')

        ax2 = fig.add_subplot(2, 1, 2)

        second_per_mjd = 1. / 1.15741e-5  #SECOND_PER_MJD value from lightcurve.cos.extract, but I couldn't import it for whatever reason... so I just copied and pasted

        #if unit_arg.startswith('s'):
        #print("period in seconds")
        #time_converter = second_per_mjd
        #time_string = 's'

        #elif unit_arg.startswith('h'):
        #print("period in hours")
        #time_converter= second_per_mjd/3600.
        #time_string = 'hrs'

        #elif unit_arg.startswith('d'):
        #print('period in days')
        #time_converter = 1.
        #time_string = 'days'

        #elif unit_arg.startswith('m'):
        #print('period in minutes')
        #time_converter = second_per_mjd/60.
        #time_string = 'min'

        #all_array = np.genfromtxt(lcfile, names=True)
        try:
            all_array = np.genfromtxt(lcfile, names=True, skip_header=1)
            #times= Time(all_array['mjd'], format='mjd')
            #times= np.copy(all_array['mjd'])
            times = Time(all_array['bmjd_tdb'], format='mjd', scale='tdb').mjd
        except ValueError as error:
            print("ValueError:", error)
            print(
                "probably due to this lightcurve being before the addition of comments,\nso now we won't skip any lines."
            )
            all_array = np.genfromtxt(lcfile, names=True)
            times = Time(all_array['bmjd_tdb'], format='mjd', scale='tdb').mjd
        fluxes = np.copy(all_array['flux'])
        gross = np.copy(all_array['gross'])
        flux_err = np.sqrt(gross) / gross
        time_err = stepsize / 2. / second_per_mjd
        poisson = np.sqrt(np.mean(gross)) / np.mean(
            gross)  #approximate relative poisson noise across observations
        standard_deviation = np.std(fluxes)
        #times= (times - times[0]) *time_converter
        #fold_times = times%period

        #plt.figure(figsize= (20,9))
        #ax2.axhline(y= 1, linestyle= '-', color = 'm', xmin = 0, xmax = 100000, linewidth = 1, alpha = 0.2)
        ax2.axhline(y=0,
                    linestyle='-',
                    color='k',
                    xmin=0,
                    xmax=100000,
                    linewidth=1,
                    alpha=0.5)

        def sig_barriers(sigma, color_line):
            ax2.axhline(y=3 * sigma,
                        linestyle=':',
                        color=color_line,
                        xmin=0,
                        xmax=100000,
                        linewidth=1)
            ax2.axhline(y=-3 * sigma,
                        linestyle=':',
                        color=color_line,
                        xmin=0,
                        xmax=100000,
                        linewidth=1)

        sig_barriers(poisson, 'r')
        sig_barriers(standard_deviation, 'b')
        min_time = np.min(times)
        max_time = np.max(times)
        #gain_change_list_mjd = Time(config.gain_change_list, scale= 'utc').mjd
        #for gain_change in gain_change_list_mjd:
        ##if ((fppos > times.min) & (fppos < times.max)):
        #if ((gain_change > min_time) & (gain_change< max_time)):
        #ax2.axvline(x= gain_change ,linestyle = '-', color = 'r' , ymin = -10, ymax = 10, linewidth = 1, alpha = 0.2)
        #for lpos in config.lpos_list:
        ##if ((fppos > times.min) & (fppos < times.max)):
        #if ((lpos > min_time) & (lpos < max_time)):
        #ax2.axvline(x= lpos,linestyle = '-', color = 'k' , ymin = -10, ymax = 10, linewidth = 1, alpha = 0.8)
        gain_change_list_bmjd = Time(config.gain_change_list, scale='utc')
        gain_change_list_bmjd = gain_change_list_bmjd.tdb.mjd
        for gain_change in gain_change_list_bmjd:
            #if ((fppos > times.min) & (fppos < times.max)):
            if ((gain_change > min_time) & (gain_change < max_time)):
                ax2.axvline(x=gain_change,
                            linestyle='-',
                            color='r',
                            ymin=-10,
                            ymax=10,
                            linewidth=1,
                            alpha=0.2)
        for lpos in Time(config.lpos_list, format='mjd', scale='utc').tdb.mjd:
            #if ((fppos > times.min) & (fppos < times.max)):
            if ((lpos > min_time) & (lpos < max_time)):
                ax2.axvline(x=lpos,
                            linestyle='-',
                            color='k',
                            ymin=-10,
                            ymax=10,
                            linewidth=1,
                            alpha=0.8)
        #ax2.scatter(times, fluxes)
        ax2.errorbar(times, fluxes, flux_err, time_err, fmt='o')
        #ax2.set_xlabel("Time ("+ time_string + ")")
        #ax2.set_xlabel("Time (MJD)")
        ax2.set_xlabel("Time(BMJD_TDB)")
        ax2.set_ylabel("Flux (normed and zeroed)")

        plt.text(0.1,
                 0.9,
                 "standard deviation: " + str(standard_deviation) +
                 "\navg gross counts: " + str(np.mean(gross)),
                 transform=ax2.transAxes,
                 bbox=dict(facecolor='blue', alpha=0.2))

        #ax2.set_xlim(0, period)
        #ax2.set_title(lcfile + ' Period fold '+ str(period) + ' ' + time_string)
        ax2.set_title(lcfile)
        #plt.show()

        fig.tight_layout()
        #plt.show()

        #fig.savefig(dest_dir+ target_dir + '_dual_plot_fold_'+ str(period) + unit_arg+'_step' + str(stepsize)+ '_wlim' + str(wave_min) + ',' + str(wave_max)+'.pdf', bbox_inches = 'tight')
        print('saving')
        fig.savefig(dest_dir + target_dir + '_dual_plot_step' + str(stepsize) +
                    '_wlim' + str(wave_min) + ',' + str(wave_max) + '.pdf',
                    bbox_inches='tight')
        print('saved')
        plt.close(
            fig
        )  #close the figure so we don't have memory usage get out of hand
        return ''
    except UnboundLocalError as error:
        print(error)
        print("guess we're just not working or something")
        return ""
Ejemplo n.º 5
0
         original_stand_flux,
         label='original model points',
         linestyle='None',
         marker='o',
         color='magenta')
plt.plot(obs_waves1, interp_model_flux, label='interpolated')
plt.plot(stand_waves1, stand_flux1, label='model')
plt.plot(obs_waves1, other_interp_flux, label='original model interpolated')
plt.legend(loc='best')
plt.show()

obs_spec = np.vstack([obs_waves1, obs_flux1])
#stand_spec= np.vstack([stand_waves1, stand_flux1])
stand_spec = np.vstack([obs_waves1, interp_model_flux])

obs_spec = spt.clean_spectrum(obs_spec, min_wave, max_wave, wavelength_masks)
unmasked_stand_spec = spt.clean_spectrum(stand_spec, min_wave, max_wave,
                                         standard_info['balmer_masks'])
stand_spec = spt.clean_spectrum(stand_spec, min_wave, max_wave,
                                wavelength_masks)
stand_waves = stand_spec[0]
stand_flux = stand_spec[1]
obs_waves = obs_spec[0]
obs_flux = obs_spec[1]

plt.title('model versus observed')
plt.plot(stand_waves, stand_flux, label='model')
plt.plot(obs_waves, obs_flux, label='observed')
#plt.legend()
#plt.show()
spt.show_plot()
            plt.axhline(y=0, linestyle='--', color='k')
            #plt.legend()
            plt.title(filename + ' & ' + filename2.split('/')[-1])
            #plt.show()
            spt.show_plot(line_id='alkali')

    if file_setting == 'all_SDSS':
        for filename1 in filenames:
            target_spec1, header1, target_noise1 = spt.retrieve_sdss_spec(
                filename1)
            target_spec1 = norm_spectrum(target_spec1, norm_range)
            for filename2 in sdss_names:
                target_spec2, header2, target_noise2 = spt.retrieve_sdss_spec(
                    filename2)
                target_spec2 = spt.clean_spectrum(target_spec2,
                                                  np.nanmin(target_spec1[0]),
                                                  np.nanmax(target_spec1[0]),
                                                  [])
                target_spec2 = norm_spectrum(target_spec2, norm_range)
                plt.ylim(top=np.nanpercentile(
                    np.hstack([target_spec2[1], target_spec1[1]]), 99.9) + 0.5)
                plot_spectrum(target_spec1,
                              filename1.split('/')[-1],
                              header1,
                              norm=True,
                              smooth=True,
                              kernel_type='box',
                              pix_width=sdss_pix_width)
                plot_spectrum(target_spec2,
                              filename2.split('/')[-1],
                              header2,
                              norm=True,
Ejemplo n.º 7
0
def run_spectral_exam(wave_limits):

    wave_min = wave_limits[0]
    #lyman = [1208, 1225]
    #oxygen= [1295, 1312] #airglow wavelengths to be filtered according to lightcurve
    wave_max = wave_limits[1]
    #lcbase= target_dir+ '_grid_lightcurves/' + '*step' + str(stepsize) + '_*'+str(wave_min)+',' + str(wave_max)+'*'
    #lcfile= glob(lcbase)[0]
    dest_dir = 'segmented_spectra/' + target_dir + '/'
    if not os.path.exists(dest_dir):
        os.makedirs(dest_dir)

    for dataset in glob(target_dir + '/*x1dsum.fits'):
        hdu = fits.open(dataset)
        print(dataset, hdu[0].header['OPT_ELEM'], hdu[0].header['CENWAVE'],
              hdu[1].header['EXPTIME'])

    def plot_element_lines(wavelength_array, axis_variable):
        for this_line in config.silicon_lines:
            if ((this_line > np.nanmin(wavelength_array)) &
                (this_line < np.nanmax(wavelength_array))):
                axis_variable.axvline(x=this_line,
                                      linestyle='-',
                                      color='g',
                                      ymin=0,
                                      ymax=100000,
                                      linewidth=1,
                                      alpha=0.9)

    def remove_range(wave_array, other_array, bound_list):
        """
        Removes wavelengths and flux values from the array that fall in the range specified by bound_list
        """
        lower_bound = bound_list[0]
        upper_bound = bound_list[1]
        low_mask = np.where(wave_array < lower_bound)
        high_mask = np.where(wave_array > upper_bound)
        low_waves = wave_array[low_mask]
        high_waves = wave_array[high_mask]
        low_other = other_array[low_mask]
        high_other = other_array[high_mask]
        merge_waves = np.append(low_waves, high_waves)
        merge_other = np.append(low_other, high_other)
        return merge_waves, merge_other

    def zero_mask(wave_array, other_array, bound_list):
        lower_bound = bound_list[0]
        upper_bound = bound_list[1]
        masking = np.where((wave_array > lower_bound)
                           & (wave_array < upper_bound))
        other_array[masking] = 0.
        return other_array

    counter = 0
    vertical_dimension = 6
    max_points_per_window = 501

    for dataset in glob(target_dir + '/*x1dsum.fits'):
        hdu = fits.open(dataset)
        #print(hdu[1])
        #print(hdu[1]['FUVA'])
        #print(hdu[1].data[0])
        #print(hdu[1].data.shape)
        #for thing in hdu[1].data:
        #print("thing: ", thing)
        #print(thing['wavelength'])
        wavelengths = np.copy(hdu[1].data['wavelength'].ravel())
        print(wavelengths.shape)
        fluxes = np.copy(hdu[1].data['flux'].ravel())
        #print("fluxes",  fluxes)
        arrshape = wavelengths.shape
        #unmasked= np.where((wavelengths < lyman[0]*np.ones(arrshape)) or ((wavelengths > lyman[1]*np.ones(arrshape)) and (wavelengths < oxygen[0])*np.ones(arrshape)) or (wavelengths > oxygen[1]*np.ones(arrshape)))
        target_spec = np.vstack([wavelengths, fluxes])
        mask_list = [config.lyman_mask] + [config.oxygen_mask] + [
            config.nitrogen_mask
        ]  #need to add the segment gap in the future
        cleaned_spec = spt.clean_spectrum(target_spec, wave_min, wave_max,
                                          mask_list)
        wavelengths = cleaned_spec[0]
        fluxes = cleaned_spec[1]
        #lower_mask = np.where(wavelengths > wave_min)
        #wavelengths= np.copy(wavelengths[lower_mask])
        #fluxes = np.copy(fluxes[lower_mask])
        #upper_mask= np.where(wavelengths < wave_max)
        #wavelengths= np.copy(wavelengths[upper_mask])
        #fluxes= np.copy(fluxes[upper_mask])
        #print("fluxes.shape: ", fluxes.shape)
        #wavelengths, fluxes = remove_range(wavelengths, fluxes, config.lyman_mask)
        #wavelengths, fluxes = remove_range(wavelengths, fluxes, config.oxygen_mask)
        #wavelengths, fluxes = remove_range(wavelengths, fluxes, config.nitrogen_mask)
        #fluxes= zero_mask(wavelengths, fluxes, config.lyman_mask)
        #fluxes= zero_mask(wavelengths, fluxes, config.oxygen_mask)

        print("fluxes.shape: ", fluxes.shape)
        print("wavelengths.shape", wavelengths.shape)

        if counter == 0:
            flux_all = np.array([np.zeros(fluxes.shape)])
            print("flux_all.shape: ", flux_all.shape)
            plot_waves = wavelengths
        print("wavelengths differences", plot_waves[:5] - wavelengths[:5])
        print(plot_waves[:5])
        print(wavelengths[:5])
        try:
            flux_all = np.append(flux_all, [fluxes], axis=0)
            print(flux_all.shape)
        except ValueError as error:
            print(error)
            shape_dif = flux_all.shape[1] - fluxes.shape[0]
            if shape_dif > 0:
                padarray = np.zeros(shape_dif)
                fluxes_pad = np.append(fluxes, padarray)
                flux_all = np.append(flux_all, [fluxes_pad], axis=0)
            elif shape_dif < 0:
                flux_all = np.append(flux_all, [fluxes[:shape_dif]], axis=0)
                print("truncating new spectrum to match previous dimensions")
        #try:
        ##flux_all = np.copy(fluxes+flux_all)
        #except ValueError as error:
        #print(error)
        #shape_dif = flux_all.shape[0] - fluxes.shape[0]
        #if shape_dif > 0 :
        #padarray= np.zeros(shape_dif)
        #flux_all= np.copy(flux_all + np.append(fluxes, padarray))
        #elif shape_dif < 0 :
        #flux_all= np.copy(flux_all + fluxes[:shape_dif])
        counter += 1
        #print("sum fluxes: ", np.sum(fluxes))
        #ax1.plot(wavelengths, fluxes/np.nanmean(fluxes), label=hdu[0].header['rootname'])
    num_segs = plot_waves.shape[0] // max_points_per_window + 1

    #silicon_lines= [1190, 1193, 1195, 1197, 1207, 1260, 1265, 1304, 1309]
    flux_all = flux_all[1:, :]  #remove the first row of zeros
    flux_med = np.nanmedian(flux_all, axis=0)
    #flux_normed = flux_med/np.nanmean(flux_med) #changed this so it's actually not normalized
    flux_normed = flux_med
    needed_zeros = max_points_per_window - plot_waves.shape[
        0] % max_points_per_window
    flux_normed = np.append(flux_normed, np.zeros(needed_zeros))
    dlambda = plot_waves[-1] - plot_waves[-2]  #find the wavelength bin size
    plus_lambda = dlambda * np.indices([
        needed_zeros,
    ])[0]  #find vaues that need to be added to get out to the appropriate range
    plus_lambda = plus_lambda + plot_waves[-1]
    print("plus_lambda.shape:", plus_lambda.shape)
    plot_waves = np.append(
        plot_waves, plus_lambda
    )  #tack on the necessary wavelength values on the far end of the spectrum to get it farther without having to deal with all of the associated noise.
    #now reshape both plot_waves and flux_normed to be multidimensional arrays that we can iterate through.
    #output a long version of the spectrum as well
    longfig = plt.figure(figsize=(200, 12))
    longax = longfig.add_subplot(1, 1, 1)
    longax.plot(plot_waves, flux_normed, color='k')
    plot_element_lines(plot_waves, longax)
    plt.grid()
    #longax.set_ylabel('Flux (normed)')
    longax.set_ylabel('Flux (cgs units)')
    longax.set_xlabel('Wavelength $(\AA)$')
    longfig.savefig(dest_dir + target_dir + 'long_spectrum_' +
                    str(wave_limits[0]) + ',' + str(wave_limits[1]) + '.pdf')

    plot_waves = plot_waves.reshape(num_segs, max_points_per_window)
    flux_normed = flux_normed.reshape(num_segs, max_points_per_window)

    num_mults_42 = num_segs // 42  #42 is the maximum number of subplots allowed

    figsaves = 0
    if num_segs > 42:
        for gorounds in range(0, num_mults_42):
            offset = 42 * gorounds
            new_max = 42
            fig = plt.figure(figsize=(20, new_max * vertical_dimension))
            for segment in range(0, new_max):
                ax = fig.add_subplot(new_max, 1, segment + 1)
                waves_for_plot = plot_waves[segment + offset]
                flux_for_plot = flux_normed[segment + offset]
                plot_element_lines(waves_for_plot, ax)
                #for this_line in config.silicon_lines:
                #if ((this_line > np.nanmin(waves_for_plot)) & (this_line< np.nanmax(waves_for_plot))):
                #ax.axvline(x= this_line ,linestyle = '-', color = 'g' , ymin = 0, ymax = 100000, linewidth = 1, alpha = 0.2)
                #print(flux_all.shape)
                ax.plot(waves_for_plot, flux_for_plot, color='k')
                #ax.set_ylabel('Flux (normed)')
                ax.set_ylabel('Flux (cgs units)')
                ax.set_xlabel('Wavelength $(\AA)$')
                ax.set_xlim(
                    [np.nanmin(waves_for_plot),
                     np.nanmax(waves_for_plot)])

            fig.tight_layout()
            plt.title(target_dir)
            fig.savefig(dest_dir + target_dir + 'segmented_spectra_wlim' +
                        str(wave_limits[0]) + ',' + str(wave_limits[1]) +
                        '_fig' + str(figsaves) + '.pdf')
            figsaves += 1
            #plt.show()
        #and now the remaining segments:
        offset = 42 * num_mults_42
        fig = plt.figure(figsize=(20, num_segs * vertical_dimension))
        for segment in range(0, num_segs % 42):
            ax = fig.add_subplot(num_segs, 1, segment + 1)
            waves_for_plot = plot_waves[segment + offset]
            flux_for_plot = flux_normed[segment + offset]
            for this_line in config.silicon_lines:
                if ((this_line > np.nanmin(waves_for_plot)) &
                    (this_line < np.nanmax(waves_for_plot))):
                    ax.axvline(x=this_line,
                               linestyle='-',
                               color='g',
                               ymin=0,
                               ymax=100000,
                               linewidth=1,
                               alpha=0.2)
            #print(flux_all.shape)
            ax.plot(waves_for_plot, flux_for_plot, color='k')
            ax.set_ylabel('Flux (normed)')
            ax.set_xlabel('Wavelength $(\AA)$')
            ax.set_xlim([np.nanmin(waves_for_plot), np.nanmax(waves_for_plot)])

        fig.tight_layout()
        plt.title(target_dir)
        fig.savefig(dest_dir + target_dir + 'segmented_spectra_wlim' +
                    str(wave_limits[0]) + ',' + str(wave_limits[1]) + '_fig' +
                    str(figsaves) + '.pdf')
        figsaves += 1
    else:
        print("Currently not well-configured to handle fewer than 42 segments")
        print("That corresponds to " + str(max_points_per_window * 42) +
              ' data points in the set.')
        fig = plt.figure(figsize=(20, num_segs * vertical_dimension))
        for segment in range(0, num_segs):
            ax = fig.add_subplot(num_segs, 1, segment + 1)
            waves_for_plot = plot_waves[segment]
            flux_for_plot = flux_normed[segment]
            for this_line in config.silicon_lines:
                if ((this_line > np.nanmin(waves_for_plot)) &
                    (this_line < np.nanmax(waves_for_plot))):
                    ax.axvline(x=this_line,
                               linestyle='-',
                               color='g',
                               ymin=0,
                               ymax=100000,
                               linewidth=1,
                               alpha=0.2)
        #print(flux_all.shape)
            ax.plot(waves_for_plot, flux_for_plot, color='k')
            ax.set_ylabel('Flux (normed)')
            ax.set_xlabel('Wavelength $(\AA)$')
            ax.set_xlim([np.nanmin(waves_for_plot), np.nanmax(waves_for_plot)])

        fig.tight_layout()
        plt.title(target_dir)
        fig.savefig(dest_dir + target_dir + 'segmented_spectra_wlim' +
                    str(wave_limits[0]) + ',' + str(wave_limits[1]) + '_fig' +
                    str(figsaves) + '.pdf')
        figsaves += 1