print('midtransit', midtransit)
        times = np.arange(midtransit - 1.5*duration,
                          midtransit + 1.5*duration, exptime.to(u.day).value)
        # times = np.arange(midtransit - 1*duration, midtransit, exptime.to(u.day).value)

        #transit = transit_model(times, trappist1(planet))
        transit = trappist1_all_transits(times)

        subgroup = group.create_group("{0}".format(Time(midtransit, format='jd').isot))
        star = Star.with_trappist1_spot_distribution()
        area = star.spotted_area(times)
        fluxes = star.fractional_flux(times)
        flares = inject_flares(wl, times)
        # flares = inject_microflares(wl, times)

        spitzer_var = spitzer_variability(times)[:, np.newaxis]

        # oot = trappist_out_of_transit(times)
        # planet_area = trappist1(planet).rp**2 * (1-trappist_out_of_transit(times).astype(int))
        spectrum_photo_flux = spectrum_photo.interp_flux(wl)
        spectrum_spots_flux = spectrum_spots.interp_flux(wl)

        # combined_spectra = ((transit - area[:, np.newaxis]) *
        #                     spectrum_photo_flux + area[:, np.newaxis] *
        #                     spectrum_spots_flux)

        combined_spectra = transit * spectrum_photo_flux

        #import ipdb; ipdb.set_trace()

        # spectra = poisson(n_photons(wl, combined_spectra, mag,
                              exptime.to(u.day).value)
            # times = np.arange(midtransit - 1*duration, midtransit, exptime.to(u.day).value)

            #transit = transit_model(times, trappist1(planet))
            transit = trappist1_all_transits(times)

            subgroup = group.create_group("{0}".format(
                Time(midtransit, format='jd').isot))
            star = Star.with_trappist1_spot_distribution()
            area = star.spotted_area(times)
            fluxes = star.fractional_flux(times)
            #flares = inject_flares(wl, times)
            flares = inject_microflares(wl, times)
            spectra = np.zeros((len(times), len(wl)))

            spitzer_var = spitzer_variability(times)

            for i in range(len(times)):
                combined_spectrum = ((1 - area[i]) * spectrum_photo +
                                     area[i] * spectrum_spots)
                spectra[i, :] = poisson(
                    combined_spectrum.n_photons(wl, exptime, mag) *
                    transit[i] * throughput(wl) * spitzer_var[i] *
                    (1 + flares[i, :]) + background(wl, exptime))

            spectral_fluxes = np.sum(spectra, axis=1)
            # plt.scatter(times, spectral_fluxes/spectral_fluxes.mean(),
            #             marker='.', s=4, label='spectrum model')
            # plt.legend()
            # plt.show()
            subgroup.attrs['spot_radii'] = [s.r for s in star.spots]