Ejemplo n.º 1
0
def plot_phase_diagrams(filenames,fileout):
    for i in range(len(filenames)):
        print 'i: ',i
        ds = yt.load(filenames[i])
        center_guess = initial_center_guess(ds,track_name)
        halo_center = get_halo_center(ds,center_guess)
        rb = sym_refine_box(ds,halo_center)

        args = filenames[i].split('/')
        sim_label = args[-3]

        dens = np.log10(rb['H_nuclei_density'])
        temp = np.log10(rb['Temperature'])

        df = pd.DataFrame({'temp':temp, 'dens':dens})
        phase_scatter = hv.Scatter(df,kdims=['dens'],vdims=['temp'],label=sim_label)

        #phase_data = np.zeros((len(rb['H_nuclei_density']),2))
        #phase_data[:,0] = np.log10(rb['H_nuclei_density'])
        #phase_data[:,1] = np.log10(rb['Temperature'])

        #points = hv.Points(phase_data,kdims=['nH','temp'],label=sim_label)
        hv.opts({'Histogram': {'style': {'alpha':0.3, 'fill_color':'k'}}})
        xhist = (histogram(phase_scatter, bin_range=(-7.5, 1), dimension='dens',normed=True)) #,alpha=0.3, fill_color='k'))
        yhist = (histogram(phase_scatter, bin_range=(3, 8.5), dimension='temp',normed=True)) #,alpha=0.3, fill_color='k'))

        if i == 0:
            phase_plot = (datashade(phase_scatter,cmap=cm.plasma, dynamic=False,x_range=(-7.5,1),y_range=(3,8.5))) << yhist(plot=dict(width=125)) << xhist(plot=dict(height=125))
        else:
            plot2 = (datashade(phase_scatter,cmap=cm.plasma, dynamic=False,x_range=(-7.5,1),y_range=(3,8.5))) << yhist(plot=dict(width=125)) << xhist(plot=dict(height=125))
            phase_plot = phase_plot + plot2

        renderer = hv.renderer('bokeh').instance(fig='html')
        renderer.save(phase_plot, fileout)
    return
Ejemplo n.º 2
0
def plot_radial_profiles(filenames,fileout):
    for i in range(len(filenames)):
        print 'i: ',i
        ds = yt.load(filenames[i])
        center_guess = initial_center_guess(ds,track_name)
        halo_center = get_halo_center(ds,center_guess)
        rb = sym_refine_box(ds,halo_center)

        args = filenames[i].split('/')
        sim_label = args[-3]

        dens = np.log10(rb['H_nuclei_density'])
        temp = np.log10(rb['Temperature'])
        Zgas = np.log10(rb['metallicity'])
        x = rb['x']
        y = rb['y']
        z = rb['z']

        halo_center = ds.arr(halo_center,'code_length')
        dist = np.sqrt((halo_center[0]-rb['x'])**2.+(halo_center[1]-rb['y'])**2.+(halo_center[2]-rb['z'])**2.).in_units('kpc')

        df = pd.DataFrame({'temp':temp, 'dens':dens, 'Zgas':Zgas,
                            'x':x,'y':y,'z':z,'dist':dist})

        temp_dist = hv.Scatter(df,kdims=['dist'],vdims=['temp'],label="Temperature "+sim_label)
        dens_dist = hv.Scatter(df,kdims=['dist'],vdims=['dens'],label='Hydrogen Number Density')
        metal_dist = hv.Scatter(df,kdims=['dist'],vdims=['Zgas'],label='Metallicity')

        if i == 0:
            dist_plots = (datashade(temp_dist,cmap=cm.Reds, dynamic=False,x_range=(0,60),y_range=(2,8.4))
		          + datashade(dens_dist,cmap=cm.Blues, dynamic=False,x_range=(0,60),y_range=(-8,2))
		          + datashade(metal_dist,cmap=cm.BuGn, dynamic=False,x_range=(0,60),y_range=(-5,1.4)))
        else:
            dist_plots2 = (datashade(temp_dist,cmap=cm.Reds, dynamic=False,x_range=(0,60),y_range=(2,8.4))
		          + datashade(dens_dist,cmap=cm.Blues, dynamic=False,x_range=(0,60),y_range=(-8,2))
		          + datashade(metal_dist,cmap=cm.BuGn, dynamic=False,x_range=(0,60),y_range=(-5,1.4)))
            dist_plots = dist_plots + dist_plots2

    renderer = hv.renderer('bokeh').instance(fig='html')
    renderer.save(dist_plots.cols(3), fileout)
    return
Ejemplo n.º 3
0
def get_center_track(first_center, latesnap, earlysnap, interval):

    ### do this way at high-redshift
    ### snaplist = np.flipud(np.arange(earlysnap,latesnap+1))

    ### do this way at later times
    snaplist = np.arange(earlysnap, latesnap + 1)
    print(snaplist)

    t = Table([[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0],
               ['       ', '       ']],
              names=('redshift', 'x0', 'y0', 'z0', 'name'))

    center_guess = first_center
    search_radius = 10.  ### COMOVING KPC

    for isnap in snaplist:
        if (isnap > 999): name = 'DD' + str(isnap)
        if (isnap <= 999): name = 'DD0' + str(isnap)
        if (isnap <= 99): name = 'DD00' + str(isnap)
        if (isnap <= 9): name = 'DD000' + str(isnap)

        print
        print
        print(name)
        ds = yt.load(name + '/' + name)
        comoving_box_size = ds.get_parameter('CosmologyComovingBoxSize')
        print('Comoving Box Size:', comoving_box_size)

        # decreased these from 500 to 100 because outputs so short spaced now
        this_search_radius = search_radius / (
            1 + ds.get_parameter('CosmologyCurrentRedshift')
        )  ## search radius is in PHYSICAL kpc
        new_center, vel_center = get_halo_center(ds,
                                                 center_guess,
                                                 radius=this_search_radius,
                                                 vel_radius=this_search_radius)
        print(new_center)

        t.add_row([
            ds.get_parameter('CosmologyCurrentRedshift'), new_center[0],
            new_center[1], new_center[2], name
        ])

        center_guess = new_center

        p = yt.ProjectionPlot(ds,
                              'x',
                              'density',
                              center=new_center,
                              width=(200., 'kpc'))
        p.set_unit(('gas', 'density'), 'Msun/pc**2')
        p.set_zlim('density', density_proj_min, density_proj_max)
        p.set_cmap(field='density', cmap=density_color_map)
        p.annotate_timestamp(corner='upper_left',
                             redshift=True,
                             draw_inset_box=True)
        p.save()
        p = yt.ProjectionPlot(ds,
                              'y',
                              'density',
                              center=new_center,
                              width=(200., 'kpc'))
        p.set_unit(('gas', 'density'), 'Msun/pc**2')
        p.set_zlim('density', density_proj_min, density_proj_max)
        p.set_cmap(field='density', cmap=density_color_map)
        p.annotate_timestamp(corner='upper_left',
                             redshift=True,
                             draw_inset_box=True)
        p.save()
        p = yt.ProjectionPlot(ds,
                              'z',
                              'density',
                              center=new_center,
                              width=(200., 'kpc'))
        p.set_unit(('gas', 'density'), 'Msun/pc**2')
        p.set_zlim('density', density_proj_min, density_proj_max)
        p.set_cmap(field='density', cmap=density_color_map)
        p.annotate_timestamp(corner='upper_left',
                             redshift=True,
                             draw_inset_box=True)
        p.save()
        print(t)
        ascii.write(t,
                    'track_temp.dat',
                    format='fixed_width_two_line',
                    overwrite=True)
        print
        print

        ### just in case memory becomes a problem
        ds.index.clear_all_data()
        del ds.index.grid_dimensions
        del ds.index.grid_left_edge
        del ds.index.grid_right_edge
        del ds.index.grid_levels
        del ds.index.grid_particle_count
        del ds.index.grids

    t = t[2:]
    print(t)

    # now interpolate the track to the interval given as a parameter
    n_points = int((np.max(t['redshift']) - np.min(t['redshift'])) / interval)
    newredshift = np.min(t['redshift']) + np.arange(n_points + 2) * interval
    newx = np.interp(newredshift, t['redshift'], t['x0'])
    newy = np.interp(newredshift, t['redshift'], t['y0'])
    newz = np.interp(newredshift, t['redshift'], t['z0'])

    tt = Table([newredshift, newx, newy, newz],
               names=('redshift', 'x', 'y', 'z'))

    t.write('track.fits', overwrite=True)
    tt.write('track_interpolate.fits', overwrite=True)

    ascii.write(t, 'track.dat', format='fixed_width_two_line', overwrite=True)
    ascii.write(tt,
                'track_interpolate.dat',
                format='fixed_width_two_line',
                overwrite=True)

    return t, tt
def calc_ang_mom_and_fluxes(halo, foggie_dir, run, **kwargs):
    outs = kwargs.get("outs", "all")
    trackname = kwargs.get("trackname", "halo_track")

    ### set up the table of all the stuff we want
    data = Table(names=('redshift', 'radius', 'nref_mode', \
                        'net_mass_flux', 'net_metal_flux', \
                        'mass_flux_in', 'mass_flux_out', \
                        'metal_flux_in', 'metal_flux_out', \
                        'net_kinetic_energy_flux','net_thermal_energy_flux','net_entropy_flux',\
                        'kinetic_energy_flux_in','kinetic_energy_flux_out',\
                        'thermal_energy_flux_in','thermal_energy_flux_out',\
                        'entropy_flux_in','entropy_flux_out',\
                        'net_cold_mass_flux', 'cold_mass_flux_in', 'cold_mass_flux_out', \
                        'net_cool_mass_flux', 'cool_mass_flux_in', 'cool_mass_flux_out', \
                        'net_warm_mass_flux', 'warm_mass_flux_in', 'warm_mass_flux_out', \
                        'net_hot_mass_flux', 'hot_mass_flux_in', 'hot_mass_flux_out', \
                        'annular_ang_mom_gas_x', 'annular_ang_mom_gas_y','annular_ang_mom_gas_z', \
                        'annular_spec_ang_mom_gas_x', 'annular_spec_ang_mom_gas_y','annular_spec_ang_mom_gas_z',\
                        'annular_ang_mom_dm_x', 'annular_ang_mom_dm_y','annular_ang_mom_dm_z', \
                        'annular_spec_ang_mom_dm_x', 'annular_spec_ang_mom_dm_y', 'annular_spec_ang_mom_dm_z', \
                        'outside_ang_mom_gas_x', 'outside_ang_mom_gas_y', 'outside_ang_mom_gas_z',  \
                        'outside_spec_ang_mom_gas_x', 'outside_spec_ang_mom_gas_y', 'outside_spec_ang_mom_gas_z', \
                        'outside_ang_mom_dm_x', 'outside_ang_mom_dm_y','outside_ang_mom_dm_z',\
                        'outside_spec_ang_mom_dm_x', 'outside_spec_ang_mom_dm_y', 'outside_spec_ang_mom_dm_z', \
                        'inside_ang_mom_stars_x', 'inside_ang_mom_stars_y', 'inside_ang_mom_stars_z', \
                        'inside_spec_ang_mom_stars_x', 'inside_spec_ang_mom_stars_y', 'inside_spec_ang_mom_stars_z'),
                  dtype=('f8', 'f8', 'i8','f8', 'f8', 'f8',
                         'f8', 'f8', 'f8','f8', 'f8', 'f8',
                         'f8', 'f8', 'f8', 'f8', 'f8', 'f8',
                         'f8', 'f8', 'f8', 'f8', 'f8', 'f8',
                         'f8', 'f8', 'f8', 'f8', 'f8', 'f8',
                         'f8', 'f8', 'f8', 'f8', 'f8', 'f8',
                         'f8', 'f8', 'f8', 'f8', 'f8', 'f8',
                         'f8', 'f8', 'f8', 'f8', 'f8', 'f8',
                         'f8', 'f8', 'f8', 'f8', 'f8', 'f8',
                         'f8', 'f8', 'f8', 'f8', 'f8', 'f8'
                        ))
    data2 = Table(names=('redshift', 'radius',
                        'net_mass_flux', 'net_metal_flux', \
                        'mass_flux_in', 'mass_flux_out', \
                        'metal_flux_in', 'metal_flux_out', \
                        'net_cold_mass_flux', 'cold_mass_flux_in', 'cold_mass_flux_out', \
                        'net_cool_mass_flux', 'cool_mass_flux_in', 'cool_mass_flux_out', \
                        'net_warm_mass_flux', 'warm_mass_flux_in', 'warm_mass_flux_out', \
                        'net_hot_mass_flux', 'hot_mass_flux_in', 'hot_mass_flux_out', \
                        'annular_ang_mom_gas_x', 'annular_ang_mom_gas_y','annular_ang_mom_gas_z', \
                        'annular_spec_ang_mom_gas_x', 'annular_spec_ang_mom_gas_y','annular_spec_ang_mom_gas_z',\
                        'outside_ang_mom_gas_x', 'outside_ang_mom_gas_y', 'outside_ang_mom_gas_z',  \
                        'outside_spec_ang_mom_gas_x', 'outside_spec_ang_mom_gas_y', 'outside_spec_ang_mom_gas_z'), \
                    dtype=('f8', 'f8', 'f8','f8', 'f8', 'f8',
                         'f8', 'f8', 'f8','f8', 'f8', 'f8',
                         'f8', 'f8', 'f8', 'f8', 'f8', 'f8',
                         'f8', 'f8', 'f8', 'f8', 'f8', 'f8',
                         'f8', 'f8', 'f8', 'f8', 'f8', 'f8',
                         'f8', 'f8'))

    print('foggie_dir = ', foggie_dir, 'run = ', run, 'trackname = ',
          trackname)
    track_name = foggie_dir + '/' + run + '/' + trackname
    if args.system == "pleiades":
        track_name = foggie_dir + "halo_008508/nref11n_nref10f_selfshield_z6/halo_track"
        output_dir = './'

    print("opening track: " + track_name)
    track = Table.read(track_name, format='ascii')
    track.sort('col1')

    ## default is do allll the snaps in the directory
    ## want to add flag for if just one
    run_dir = foggie_dir + run
    if args.system == "pleiades":
        track_name = foggie_dir + "halo_008508/nref11n_nref10f_selfshield_z6/halo_track"
        run_dir = foggie_dir + run
        output_dir = './'
    print('run_dir = ', run_dir)

    if outs == "all":
        print("looking for outputs in ", run_dir)
        outs = glob.glob(os.path.join(run_dir, '?D????/?D????'))
    else:
        print("outs = ", outs)
        new_outs = [glob.glob(os.path.join(run_dir, snap)) for snap in outs]
        print("new_outs = ", new_outs)
        new_new_outs = [snap[0] for snap in new_outs]
        outs = new_new_outs

    for snap in outs:
        # load the snapshot
        print('opening snapshot ' + snap)
        ds = yt.load(snap)

        # add the particle filters
        ds.add_particle_filter('stars')
        ds.add_particle_filter('dm')

        # create all the regions
        zsnap = ds.get_parameter('CosmologyCurrentRedshift')
        #proper_box_size = get_proper_box_size(ds)
        # another option than the function:
        proper_box_size = ds.quan(1., 'code_length').to('kpc')

        refine_box, refine_box_center, refine_width_code = get_refine_box(
            ds, zsnap, track)
        refine_width = refine_width_code * proper_box_size

        # center is trying to be the center of the halo
        halo_center, halo_velocity = get_halo_center(ds, refine_box_center)

        ### OK, now want to set up some spheres of some sizes and get the stuff
        radii = refine_width * 0.5 * np.arange(0.9, 0.1,
                                               -0.01)  # 0.5 because radius
        print('radii: ', radii)
        small_sphere = ds.sphere(halo_center,
                                 0.05 * refine_width_code)  # R=10ckpc/h
        big_sphere = ds.sphere(halo_center, 0.45 * refine_width_code)
        print('small sphere radius: ', small_sphere.radius.in_units('kpc'))

        # we want to subtract the bulk velocity from the radial velocities
        bulk_velocity = small_sphere.quantities["BulkVelocity"]()

        # find number of cells for the FRB
        # by default, it uses nref10 as the cell size for the frb
        # then create the 3D FRB for calculating the fluxes
        print('big sphere unique:',
              np.unique(big_sphere['dx'].in_units('kpc')))
        cell_size = np.unique(big_sphere['dx'].in_units('kpc'))[1]  ###
        box_width = ds.quan(0.9 * refine_width, 'kpc')
        nbins = int(np.ceil(box_width / cell_size).value)
        print('there will be ', nbins**3, ' bins')

        halo_center = ds.arr(halo_center, 'code_length')
        xL, xR = halo_center[0] - box_width / 2., halo_center[
            0] + box_width / 2.
        yL, yR = halo_center[1] - box_width / 2., halo_center[
            1] + box_width / 2.
        zL, zR = halo_center[2] - box_width / 2., halo_center[
            2] + box_width / 2.
        jnbins = complex(0, nbins)
        box = ds.r[xL:xR:jnbins, yL:yR:jnbins, zL:zR:jnbins]
        box.set_field_parameter("center", halo_center)
        box.set_field_parameter("bulk_velocity", bulk_velocity)

        print('setting up gas fields...')
        ### OK, now want to call the fields that we'll need for the fluxes
        ### otherwise, the code crashes when trying to select subsets of the data
        ## GAS FIELDS
        temperature = box['Temperature'].flatten()
        cell_mass = box['cell_mass'].to("Msun").flatten()
        metal_mass = box[('gas', 'metal_mass')].to("Msun").flatten()
        radius = box['radius'].to("kpc").flatten()
        radial_velocity = box['radial_velocity'].to('kpc/yr').flatten()
        cell_volume = box['cell_volume'].flatten()
        #grid_levels = box['index', 'grid_level']
        print('here ????')
        gas_ang_mom_x = box[('gas', 'angular_momentum_x')].flatten()
        gas_ang_mom_y = box[('gas', 'angular_momentum_y')].flatten()
        gas_ang_mom_z = box[('gas', 'angular_momentum_z')].flatten()
        print('i have gotten to here')
        gas_spec_ang_mom_x = box[('gas',
                                  'specific_angular_momentum_x')].flatten()
        gas_spec_ang_mom_y = box[('gas',
                                  'specific_angular_momentum_y')].flatten()
        gas_spec_ang_mom_z = box[('gas',
                                  'specific_angular_momentum_z')].flatten()
        kinetic_energy = box['gas', 'kinetic_energy'].flatten()
        print('and now to here')
        kinetic_energy = (kinetic_energy * cell_volume / cell_mass).to('erg/g')
        thermal_energy = box['gas', 'thermal_energy'].flatten()
        entropy = box['entropy'].flatten()
        hden = box['H_nuclei_density'].flatten()

        print('setting up star fields...')
        ## STAR PARTICLE FIELDS
        star_ang_mom_x = big_sphere['stars',
                                    'particle_angular_momentum_x'].flatten()
        star_ang_mom_y = big_sphere['stars',
                                    'particle_angular_momentum_y'].flatten()
        star_ang_mom_z = big_sphere['stars',
                                    'particle_angular_momentum_z'].flatten()
        star_spec_ang_mom_x = big_sphere[
            'stars', 'particle_specific_angular_momentum_x'].flatten()
        star_spec_ang_mom_y = big_sphere[
            'stars', 'particle_specific_angular_momentum_y'].flatten()
        star_spec_ang_mom_z = big_sphere[
            'stars', 'particle_specific_angular_momentum_z'].flatten()
        star_distance = np.sqrt(
            (big_sphere['stars', 'particle_position_x'] - halo_center[0])**2. +
            (big_sphere['stars', 'particle_position_y'] - halo_center[1])**2. +
            (big_sphere['stars', 'particle_position_z'] -
             halo_center[2])**2.).to("kpc")

        print('setting up dark matter fields...')
        ## DM PARTICLE FIELDS
        dm_ang_mom_x = big_sphere['dm',
                                  'particle_angular_momentum_x'].flatten()
        dm_ang_mom_y = big_sphere['dm',
                                  'particle_angular_momentum_y'].flatten()
        dm_ang_mom_z = big_sphere['dm',
                                  'particle_angular_momentum_z'].flatten()
        dm_spec_ang_mom_x = big_sphere[
            'dm', 'particle_specific_angular_momentum_x'].flatten()
        dm_spec_ang_mom_y = big_sphere[
            'dm', 'particle_specific_angular_momentum_y'].flatten()
        dm_spec_ang_mom_z = big_sphere[
            'dm', 'particle_specific_angular_momentum_z'].flatten()
        dm_distance = np.sqrt(
            (big_sphere['dm', 'particle_position_x'] - halo_center[0])**2. +
            (big_sphere['dm', 'particle_position_y'] - halo_center[1])**2. +
            (big_sphere['dm', 'particle_position_z'] -
             halo_center[2])**2.).to("kpc")

        for rad in radii:
            #this_sphere = ds.sphere(halo_center, rad)
            print('doing radius ', rad, '.......')
            if rad != np.max(radii):
                if rad == radii[-1]:
                    minrad, maxrad = ds.quan(1, 'kpc'), rad
                else:
                    idI = np.where(radii == rad)[0]
                    maxrad, minrad = rad, radii[idI[0] + 1]
                # some radius / geometry things
                dr = maxrad - minrad
                rad_here = (minrad + maxrad) / 2.

                # find the indices that I'm going to need
                idR = np.where((radius >= minrad) & (radius < maxrad))[0]
                idCd = np.where((radius >= minrad) & (radius < maxrad)
                                & (temperature <= 1e4))[0]
                idCl = np.where((radius >= minrad) & (radius < maxrad)
                                & (temperature > 1e4)
                                & (temperature <= 1e5))[0]
                idW = np.where((radius >= minrad) & (radius < maxrad)
                               & (temperature > 1e5) & (temperature <= 1e6))[0]
                idH = np.where((radius >= minrad) & (radius < maxrad)
                               & (temperature >= 1e6))
                idRdm = np.where((dm_distance >= minrad)
                                 & (dm_distance < maxrad))[0]
                big_annulusGAS = np.where(radius >= rad_here)[0]
                big_annulusDM = np.where(dm_distance >= rad_here)[0]
                inside = np.where(star_distance < rad_here)[0]

                # most common refinement level
                #nref_mode = stats.mode(grid_levels[idR])
                print("min,max = ", minrad, maxrad)
                sp_out, sp_in = ds.sphere(halo_center, maxrad), ds.sphere(
                    halo_center, minrad)
                shell = sp_out - sp_in
                print('nref mode = ', stats.mode(shell['index', 'grid_level']))
                nref_mode = stats.mode(shell['index', 'grid_level'])[0]
                # mass fluxes
                gas_flux = (np.sum(cell_mass[idR] * radial_velocity[idR]) /
                            dr).to("Msun/yr")
                metal_flux = (np.sum(metal_mass[idR] * radial_velocity[idR]) /
                              dr).to("Msun/yr")
                kinetic_energy_flux = (
                    np.sum(kinetic_energy[idR] * radial_velocity[idR]) /
                    dr).to("erg/(g*yr)")
                thermal_energy_flux = (
                    np.sum(thermal_energy[idR] * radial_velocity[idR]) /
                    dr).to("erg/(g*yr)")
                entropy_flux = (np.sum(entropy[idR] * radial_velocity[idR]) /
                                dr)

                ## also filter based off radial velocity
                idVin = np.where(radial_velocity[idR] <= 0.)[0]
                idVout = np.where(radial_velocity[idR] > 0.)[0]
                gas_flux_in = (np.sum(
                    cell_mass[idR][idVin] * radial_velocity[idR][idVin]) /
                               dr).to("Msun/yr")
                gas_flux_out = (np.sum(
                    cell_mass[idR][idVout] * radial_velocity[idR][idVout]) /
                                dr).to("Msun/yr")
                metal_flux_in = (np.sum(
                    metal_mass[idR][idVin] * radial_velocity[idR][idVin]) /
                                 dr).to("Msun/yr")
                metal_flux_out = (np.sum(
                    metal_mass[idR][idVout] * radial_velocity[idR][idVout]) /
                                  dr).to("Msun/yr")
                kinetic_energy_flux_in = (np.sum(
                    kinetic_energy[idR][idVin] * radial_velocity[idR][idVin]) /
                                          dr).to("erg/(g*yr)")
                kinetic_energy_flux_out = (
                    np.sum(kinetic_energy[idR][idVout] *
                           radial_velocity[idR][idVout]) / dr).to("erg/(g*yr)")
                thermal_energy_flux_in = (np.sum(
                    thermal_energy[idR][idVin] * radial_velocity[idR][idVin]) /
                                          dr).to("erg/(g*yr)")
                thermal_energy_flux_out = (
                    np.sum(thermal_energy[idR][idVout] *
                           radial_velocity[idR][idVout]) / dr).to("erg/(g*yr)")
                entropy_flux_in = (
                    np.sum(entropy[idR][idVin] * radial_velocity[idR][idVin]) /
                    dr)
                entropy_flux_out = (np.sum(
                    entropy[idR][idVout] * radial_velocity[idR][idVout]) / dr)

                ## and filter on temperature! and velocity! woo!
                idVin = np.where(radial_velocity[idH] <= 0.)[0]
                idVout = np.where(radial_velocity[idH] > 0.)[0]
                hot_gas_flux = (np.sum(cell_mass[idH] * radial_velocity[idH]) /
                                dr).to("Msun/yr")
                hot_gas_flux_in = (np.sum(
                    cell_mass[idH][idVin] * radial_velocity[idH][idVin]) /
                                   dr).to("Msun/yr")
                hot_gas_flux_out = (np.sum(
                    cell_mass[idH][idVout] * radial_velocity[idH][idVout]) /
                                    dr).to("Msun/yr")

                idVin = np.where(radial_velocity[idW] <= 0.)[0]
                idVout = np.where(radial_velocity[idW] > 0.)[0]
                warm_gas_flux = (
                    np.sum(cell_mass[idW] * radial_velocity[idW]) /
                    dr).to("Msun/yr")
                warm_gas_flux_in = (np.sum(
                    cell_mass[idW][idVin] * radial_velocity[idW][idVin]) /
                                    dr).to("Msun/yr")
                warm_gas_flux_out = (np.sum(
                    cell_mass[idW][idVout] * radial_velocity[idW][idVout]) /
                                     dr).to("Msun/yr")

                idVin = np.where(radial_velocity[idCl] <= 0.)[0]
                idVout = np.where(radial_velocity[idCl] > 0.)[0]
                cool_gas_flux = (
                    np.sum(cell_mass[idCl] * radial_velocity[idCl]) /
                    dr).to("Msun/yr")
                cool_gas_flux_in = (np.sum(
                    cell_mass[idCl][idVin] * radial_velocity[idCl][idVin]) /
                                    dr).to("Msun/yr")
                cool_gas_flux_out = (np.sum(
                    cell_mass[idCl][idVout] * radial_velocity[idCl][idVout]) /
                                     dr).to("Msun/yr")

                idVin = np.where(radial_velocity[idCd] <= 0.)[0]
                idVout = np.where(radial_velocity[idCd] > 0.)[0]
                cold_gas_flux = (
                    np.sum(cell_mass[idCd] * radial_velocity[idCd]) /
                    dr).to("Msun/yr")
                cold_gas_flux_in = (np.sum(
                    cell_mass[idCd][idVin] * radial_velocity[idCd][idVin]) /
                                    dr).to("Msun/yr")
                cold_gas_flux_out = (np.sum(
                    cell_mass[idCd][idVout] * radial_velocity[idCd][idVout]) /
                                     dr).to("Msun/yr")

                ## GAS angular momentum!
                annular_ang_mom_gas_x = np.sum(gas_ang_mom_x[idR])
                annular_ang_mom_gas_y = np.sum(gas_ang_mom_y[idR])
                annular_ang_mom_gas_z = np.sum(gas_ang_mom_z[idR])
                annular_spec_ang_mom_gas_x = np.mean(gas_spec_ang_mom_x[idR])
                annular_spec_ang_mom_gas_y = np.mean(gas_spec_ang_mom_y[idR])
                annular_spec_ang_mom_gas_z = np.mean(gas_spec_ang_mom_z[idR])

                outside_ang_mom_gas_x = np.sum(gas_ang_mom_x[big_annulusGAS])
                outside_ang_mom_gas_y = np.sum(gas_ang_mom_y[big_annulusGAS])
                outside_ang_mom_gas_z = np.sum(gas_ang_mom_z[big_annulusGAS])
                outside_spec_ang_mom_gas_x = np.mean(
                    gas_spec_ang_mom_x[big_annulusGAS])
                outside_spec_ang_mom_gas_y = np.mean(
                    gas_spec_ang_mom_y[big_annulusGAS])
                outside_spec_ang_mom_gas_z = np.mean(
                    gas_spec_ang_mom_z[big_annulusGAS])

                ## PARTICLE angular momentum calculations
                inside_ang_mom_stars_x = np.sum(star_ang_mom_x[inside])
                inside_ang_mom_stars_y = np.sum(star_ang_mom_y[inside])
                inside_ang_mom_stars_z = np.sum(star_ang_mom_z[inside])
                inside_spec_ang_mom_stars_x = np.sum(
                    star_spec_ang_mom_x[inside])
                inside_spec_ang_mom_stars_y = np.sum(
                    star_spec_ang_mom_y[inside])
                inside_spec_ang_mom_stars_z = np.sum(
                    star_spec_ang_mom_z[inside])

                annular_ang_mom_dm_x = np.sum(dm_ang_mom_x[idRdm])
                annular_ang_mom_dm_y = np.sum(dm_ang_mom_y[idRdm])
                annular_ang_mom_dm_z = np.sum(dm_ang_mom_z[idRdm])
                annular_spec_ang_mom_dm_x = np.mean(dm_spec_ang_mom_x[idRdm])
                annular_spec_ang_mom_dm_y = np.mean(dm_spec_ang_mom_y[idRdm])
                annular_spec_ang_mom_dm_z = np.mean(dm_spec_ang_mom_z[idRdm])

                outside_ang_mom_dm_x = np.sum(dm_ang_mom_x[big_annulusDM])
                outside_ang_mom_dm_y = np.sum(dm_ang_mom_y[big_annulusDM])
                outside_ang_mom_dm_z = np.sum(dm_ang_mom_z[big_annulusDM])
                outside_spec_ang_mom_dm_x = np.mean(
                    dm_spec_ang_mom_x[big_annulusDM])
                outside_spec_ang_mom_dm_y = np.mean(
                    dm_spec_ang_mom_y[big_annulusDM])
                outside_spec_ang_mom_dm_z = np.mean(
                    dm_spec_ang_mom_z[big_annulusDM])

                data.add_row([zsnap, rad, nref_mode, gas_flux, metal_flux, \
                                gas_flux_in, gas_flux_out, metal_flux_in, metal_flux_out, \
                                kinetic_energy_flux, thermal_energy_flux, entropy_flux,\
                                kinetic_energy_flux_in,kinetic_energy_flux_out,\
                                thermal_energy_flux_in,thermal_energy_flux_out,\
                                entropy_flux_in,entropy_flux_out,\
                                cold_gas_flux, cold_gas_flux_in, cold_gas_flux_out, \
                                cool_gas_flux, cool_gas_flux_in, cool_gas_flux_out, \
                                warm_gas_flux, warm_gas_flux_in, warm_gas_flux_out, \
                                hot_gas_flux, hot_gas_flux_in, hot_gas_flux_out,
                                annular_ang_mom_gas_x, annular_ang_mom_gas_y,annular_ang_mom_gas_z, \
                                annular_spec_ang_mom_gas_x, annular_spec_ang_mom_gas_y,annular_spec_ang_mom_gas_z,\
                                annular_ang_mom_dm_x, annular_ang_mom_dm_y,annular_ang_mom_dm_z, \
                                annular_spec_ang_mom_dm_x, annular_spec_ang_mom_dm_y, annular_spec_ang_mom_dm_z, \
                                outside_ang_mom_gas_x, outside_ang_mom_gas_y, outside_ang_mom_gas_z,  \
                                outside_spec_ang_mom_gas_x, outside_spec_ang_mom_gas_y, outside_spec_ang_mom_gas_z, \
                                outside_ang_mom_dm_x, outside_ang_mom_dm_y,outside_ang_mom_dm_z,\
                                outside_spec_ang_mom_dm_x, outside_spec_ang_mom_dm_y, outside_spec_ang_mom_dm_z, \
                                inside_ang_mom_stars_x, inside_ang_mom_stars_y, inside_ang_mom_stars_z, \
                                inside_spec_ang_mom_stars_x, inside_spec_ang_mom_stars_y, inside_spec_ang_mom_stars_z])

                ####### REPEATING THE CALCUATION WITHOUT THE DENSEST ISM-LIKE GAS #########
                idR = np.where((radius >= minrad) & (radius < maxrad)
                               & (hden < 0.1))[0]
                idCd = np.where((radius >= minrad) & (radius < maxrad)
                                & (temperature <= 1e4) & (hden < 0.1))[0]
                idCl = np.where((radius >= minrad) & (radius < maxrad)
                                & (temperature > 1e4) & (temperature <= 1e5)
                                & (hden < 0.1))[0]
                idW = np.where((radius >= minrad) & (radius < maxrad)
                               & (temperature > 1e5) & (temperature <= 1e6)
                               & (hden < 0.1))[0]
                idH = np.where((radius >= minrad) & (radius < maxrad)
                               & (temperature >= 1e6) & (hden < 0.1))
                big_annulusGAS = np.where((radius >= rad_here)
                                          & (hden < 0.1))[0]

                # mass fluxes
                gas_flux = (np.sum(cell_mass[idR] * radial_velocity[idR]) /
                            dr).to("Msun/yr")
                metal_flux = (np.sum(metal_mass[idR] * radial_velocity[idR]) /
                              dr).to("Msun/yr")

                ## also filter based off radial velocity
                idVin = np.where(radial_velocity[idR] <= 0.)[0]
                idVout = np.where(radial_velocity[idR] > 0.)[0]
                gas_flux_in = (np.sum(
                    cell_mass[idR][idVin] * radial_velocity[idR][idVin]) /
                               dr).to("Msun/yr")
                gas_flux_out = (np.sum(
                    cell_mass[idR][idVout] * radial_velocity[idR][idVout]) /
                                dr).to("Msun/yr")
                metal_flux_in = (np.sum(
                    metal_mass[idR][idVin] * radial_velocity[idR][idVin]) /
                                 dr).to("Msun/yr")
                metal_flux_out = (np.sum(
                    metal_mass[idR][idVout] * radial_velocity[idR][idVout]) /
                                  dr).to("Msun/yr")

                ## and filter on temperature! and velocity! woo!
                idVin = np.where(radial_velocity[idH] <= 0.)[0]
                idVout = np.where(radial_velocity[idH] > 0.)[0]
                hot_gas_flux = (np.sum(cell_mass[idH] * radial_velocity[idH]) /
                                dr).to("Msun/yr")
                hot_gas_flux_in = (np.sum(
                    cell_mass[idH][idVin] * radial_velocity[idH][idVin]) /
                                   dr).to("Msun/yr")
                hot_gas_flux_out = (np.sum(
                    cell_mass[idH][idVout] * radial_velocity[idH][idVout]) /
                                    dr).to("Msun/yr")

                idVin = np.where(radial_velocity[idW] <= 0.)[0]
                idVout = np.where(radial_velocity[idW] > 0.)[0]
                warm_gas_flux = (
                    np.sum(cell_mass[idW] * radial_velocity[idW]) /
                    dr).to("Msun/yr")
                warm_gas_flux_in = (np.sum(
                    cell_mass[idW][idVin] * radial_velocity[idW][idVin]) /
                                    dr).to("Msun/yr")
                warm_gas_flux_out = (np.sum(
                    cell_mass[idW][idVout] * radial_velocity[idW][idVout]) /
                                     dr).to("Msun/yr")

                idVin = np.where(radial_velocity[idCl] <= 0.)[0]
                idVout = np.where(radial_velocity[idCl] > 0.)[0]
                cool_gas_flux = (
                    np.sum(cell_mass[idCl] * radial_velocity[idCl]) /
                    dr).to("Msun/yr")
                cool_gas_flux_in = (np.sum(
                    cell_mass[idCl][idVin] * radial_velocity[idCl][idVin]) /
                                    dr).to("Msun/yr")
                cool_gas_flux_out = (np.sum(
                    cell_mass[idCl][idVout] * radial_velocity[idCl][idVout]) /
                                     dr).to("Msun/yr")

                idVin = np.where(radial_velocity[idCd] <= 0.)[0]
                idVout = np.where(radial_velocity[idCd] > 0.)[0]
                cold_gas_flux = (
                    np.sum(cell_mass[idCd] * radial_velocity[idCd]) /
                    dr).to("Msun/yr")
                cold_gas_flux_in = (np.sum(
                    cell_mass[idCd][idVin] * radial_velocity[idCd][idVin]) /
                                    dr).to("Msun/yr")
                cold_gas_flux_out = (np.sum(
                    cell_mass[idCd][idVout] * radial_velocity[idCd][idVout]) /
                                     dr).to("Msun/yr")

                ## GAS angular momentum!
                annular_ang_mom_gas_x = np.sum(gas_ang_mom_x[idR])
                annular_ang_mom_gas_y = np.sum(gas_ang_mom_y[idR])
                annular_ang_mom_gas_z = np.sum(gas_ang_mom_z[idR])
                annular_spec_ang_mom_gas_x = np.mean(gas_spec_ang_mom_x[idR])
                annular_spec_ang_mom_gas_y = np.mean(gas_spec_ang_mom_y[idR])
                annular_spec_ang_mom_gas_z = np.mean(gas_spec_ang_mom_z[idR])

                outside_ang_mom_gas_x = np.sum(gas_ang_mom_x[big_annulusGAS])
                outside_ang_mom_gas_y = np.sum(gas_ang_mom_y[big_annulusGAS])
                outside_ang_mom_gas_z = np.sum(gas_ang_mom_z[big_annulusGAS])
                outside_spec_ang_mom_gas_x = np.mean(
                    gas_spec_ang_mom_x[big_annulusGAS])
                outside_spec_ang_mom_gas_y = np.mean(
                    gas_spec_ang_mom_y[big_annulusGAS])
                outside_spec_ang_mom_gas_z = np.mean(
                    gas_spec_ang_mom_z[big_annulusGAS])


                data2.add_row([zsnap, rad,gas_flux, metal_flux, \
                                gas_flux_in, gas_flux_out, metal_flux_in, metal_flux_out, \
                                cold_gas_flux, cold_gas_flux_in, cold_gas_flux_out, \
                                cool_gas_flux, cool_gas_flux_in, cool_gas_flux_out, \
                                warm_gas_flux, warm_gas_flux_in, warm_gas_flux_out, \
                                hot_gas_flux, hot_gas_flux_in, hot_gas_flux_out,
                                annular_ang_mom_gas_x, annular_ang_mom_gas_y,annular_ang_mom_gas_z, \
                                annular_spec_ang_mom_gas_x, annular_spec_ang_mom_gas_y,annular_spec_ang_mom_gas_z,\
                                outside_ang_mom_gas_x, outside_ang_mom_gas_y, outside_ang_mom_gas_z,  \
                                outside_spec_ang_mom_gas_x, outside_spec_ang_mom_gas_y, outside_spec_ang_mom_gas_z])

    data = set_table_units(data)
    data2 = set_table_units(data2)
    tablename = run_dir + '/' + args.run + '_' + args.output + '_angular_momenta_and_fluxes.hdf5'
    print('writing to ', tablename)
    data.write(tablename, path='all_data', serialize_meta=True, overwrite=True)
    data2.write(tablename,
                path='noISM_data',
                serialize_meta=True,
                overwrite=False,
                append=True)

    return "whooooo angular momentum wheeeeeeee"
def calc_ang_mom_and_fluxes(halo, foggie_dir, output_dir, run, **kwargs):
    outs = kwargs.get("outs", "all")
    trackname = kwargs.get("trackname", "halo_track")

    ### set up the table of all the stuff we want
    data = Table(names=('redshift', 'radius', 'nref_mode', \
                        'net_mass_flux', 'net_metal_flux', \
                        'annular_ang_mom_gas_x', 'annular_ang_mom_gas_y','annular_ang_mom_gas_z', \
                        'annular_spec_ang_mom_gas_x', 'annular_spec_ang_mom_gas_y','annular_spec_ang_mom_gas_z',\
                        'annular_ang_mom_dm_x', 'annular_ang_mom_dm_y','annular_ang_mom_dm_z', \
                        'annular_spec_ang_mom_dm_x', 'annular_spec_ang_mom_dm_y', 'annular_spec_ang_mom_dm_z', \
                        'outside_ang_mom_gas_x', 'outside_ang_mom_gas_y', 'outside_ang_mom_gas_z',  \
                        'outside_spec_ang_mom_gas_x', 'outside_spec_ang_mom_gas_y', 'outside_spec_ang_mom_gas_z', \
                        'outside_ang_mom_dm_x', 'outside_ang_mom_dm_y','outside_ang_mom_dm_z',\
                        'outside_spec_ang_mom_dm_x', 'outside_spec_ang_mom_dm_y', 'outside_spec_ang_mom_dm_z', \
                        'inside_ang_mom_stars_x', 'inside_ang_mom_stars_y', 'inside_ang_mom_stars_z', \
                        'inside_spec_ang_mom_stars_x', 'inside_spec_ang_mom_stars_y', 'inside_spec_ang_mom_stars_z'),
                  dtype=('f8', 'f8', 'i8', 'f8', 'f8',
                         'f8', 'f8', 'f8', 'f8', 'f8', 'f8',
                         'f8', 'f8', 'f8', 'f8', 'f8', 'f8',
                         'f8', 'f8', 'f8', 'f8', 'f8', 'f8',
                         'f8', 'f8', 'f8', 'f8', 'f8', 'f8',
                         'f8', 'f8', 'f8', 'f8', 'f8', 'f8'
                        ))

    print(foggie_dir)
    track_name = foggie_dir + 'halo_00' + str(
        halo) + '/' + run + '/' + trackname
    if args.system == "pleiades":
        track_name = foggie_dir + "halo_008508/nref11f_refine200kpc_z4to2/halo_track"

    print("opening track: " + track_name)
    track = Table.read(track_name, format='ascii')
    track.sort('col1')

    ## default is do allll the snaps in the directory
    ## want to add flag for if just one
    run_dir = foggie_dir + 'halo_00' + str(halo) + '/' + run
    if halo == "8508":
        prefix = output_dir + 'plots_halo_008508/' + run + '/'
    else:
        prefix = output_dir + 'other_halo_plots/' + str(halo) + '/' + run + '/'
    if not (os.path.exists(prefix)):
        os.system("mkdir " + prefix)

    if outs == "all":
        print("looking for outputs in ", run_dir)
        outs = glob.glob(os.path.join(run_dir, '?D????/?D????'))
    else:
        print("outs = ", outs)
        new_outs = [glob.glob(os.path.join(run_dir, snap)) for snap in outs]
        print("new_outs = ", new_outs)
        new_new_outs = [snap[0] for snap in new_outs]
        outs = new_new_outs

    for snap in outs:
        # load the snapshot
        print('opening snapshot ' + snap)
        ds = yt.load(snap)

        # add all the things
        ds.add_particle_filter('stars')
        ds.add_particle_filter('dm')

        # create all the regions
        zsnap = ds.get_parameter('CosmologyCurrentRedshift')
        proper_box_size = get_proper_box_size(ds)

        refine_box, refine_box_center, refine_width_code = get_refine_box(
            ds, zsnap, track)
        refine_width = refine_width_code * proper_box_size

        # center is trying to be the center of the halo
        halo_center, halo_velocity = get_halo_center(ds, refine_box_center)

        ### OK, now want to set up some spheres of some sizes and get the stuff
        radii = refine_width_code * 0.5 * np.arange(0.9, 0.1,
                                                    -0.1)  # 0.5 because radius
        small_sphere = ds.sphere(halo_center,
                                 0.05 * refine_width_code)  # R=10ckpc/h
        big_sphere = ds.sphere(halo_center, 0.45 * refine_width_code)

        for radius in radii:
            this_sphere = ds.sphere(halo_center, radius)
            if radius != np.max(radii):
                # region (big_sphere) must be larger than surface, defined here by "radius"
                surface = ds.surface(big_sphere, 'radius',
                                     (radius, 'code_length'))

                nref_mode = stats.mode(surface[('index', 'grid_level')])
                gas_flux = surface.calculate_flux("velocity_x", "velocity_y",
                                                  "velocity_z", "density")
                metal_flux = surface.calculate_flux("velocity_x", "velocity_y",
                                                    "velocity_z",
                                                    "metal_density")

                # annuli
                big_annulus = big_sphere - this_sphere
                # note that refine_fracs is in decreasing order!
                this_annulus = last_sphere - this_sphere
                inside_ang_mom_stars_x = this_sphere[
                    'stars', 'particle_angular_momentum_x'].sum()
                inside_ang_mom_stars_y = this_sphere[
                    'stars', 'particle_angular_momentum_y'].sum()
                inside_ang_mom_stars_z = this_sphere[
                    'stars', 'particle_angular_momentum_z'].sum()
                inside_spec_ang_mom_stars_x = this_sphere[
                    'stars', 'particle_specific_angular_momentum_x'].mean()
                inside_spec_ang_mom_stars_y = this_sphere[
                    'stars', 'particle_specific_angular_momentum_y'].mean()
                inside_spec_ang_mom_stars_z = this_sphere[
                    'stars', 'particle_specific_angular_momentum_z'].mean()

                ## ok want angular momenta
                annular_ang_mom_gas_x = this_annulus[(
                    'gas', 'angular_momentum_x')].sum()
                annular_ang_mom_gas_y = this_annulus[(
                    'gas', 'angular_momentum_y')].sum()
                annular_ang_mom_gas_z = this_annulus[(
                    'gas', 'angular_momentum_z')].sum()
                annular_spec_ang_mom_gas_x = this_annulus[(
                    'gas', 'specific_angular_momentum_x')].mean()
                annular_spec_ang_mom_gas_y = this_annulus[(
                    'gas', 'specific_angular_momentum_y')].mean()
                annular_spec_ang_mom_gas_z = this_annulus[(
                    'gas', 'specific_angular_momentum_z')].mean()

                annular_ang_mom_dm_x = this_annulus[(
                    'dm', 'particle_angular_momentum_x')].sum()
                annular_ang_mom_dm_y = this_annulus[(
                    'dm', 'particle_angular_momentum_y')].sum()
                annular_ang_mom_dm_z = this_annulus[(
                    'dm', 'particle_angular_momentum_z')].sum()
                annular_spec_ang_mom_dm_x = this_annulus[(
                    'dm', 'particle_specific_angular_momentum_x')].mean()
                annular_spec_ang_mom_dm_y = this_annulus[(
                    'dm', 'particle_specific_angular_momentum_y')].mean()
                annular_spec_ang_mom_dm_z = this_annulus[(
                    'dm', 'particle_specific_angular_momentum_z')].mean()

                outside_ang_mom_gas_x = big_annulus[(
                    'gas', 'angular_momentum_x')].sum()
                outside_ang_mom_gas_y = big_annulus[(
                    'gas', 'angular_momentum_y')].sum()
                outside_ang_mom_gas_z = big_annulus[(
                    'gas', 'angular_momentum_z')].sum()
                outside_spec_ang_mom_gas_x = big_annulus[(
                    'gas', 'specific_angular_momentum_x')].mean()
                outside_spec_ang_mom_gas_y = big_annulus[(
                    'gas', 'specific_angular_momentum_y')].mean()
                outside_spec_ang_mom_gas_z = big_annulus[(
                    'gas', 'specific_angular_momentum_z')].mean()

                outside_ang_mom_dm_x = big_annulus[(
                    'dm', 'particle_angular_momentum_x')].sum()
                outside_ang_mom_dm_y = big_annulus[(
                    'dm', 'particle_angular_momentum_y')].sum()
                outside_ang_mom_dm_z = big_annulus[(
                    'dm', 'particle_angular_momentum_z')].sum()
                outside_spec_ang_mom_dm_x = big_annulus[(
                    'dm', 'particle_specific_angular_momentum_x')].mean()
                outside_spec_ang_mom_dm_y = big_annulus[(
                    'dm', 'particle_specific_angular_momentum_y')].mean()
                outside_spec_ang_mom_dm_z = big_annulus[(
                    'dm', 'particle_specific_angular_momentum_z')].mean()

                # let's add everything to the giant table!
                data.add_row([zsnap, radius, int(nref_mode[0][0]), gas_flux, metal_flux, \
                                annular_ang_mom_gas_x, annular_ang_mom_gas_y,annular_ang_mom_gas_z, \
                                annular_spec_ang_mom_gas_x, annular_spec_ang_mom_gas_y,annular_spec_ang_mom_gas_z,\
                                annular_ang_mom_dm_x, annular_ang_mom_dm_y,annular_ang_mom_dm_z, \
                                annular_spec_ang_mom_dm_x, annular_spec_ang_mom_dm_y, annular_spec_ang_mom_dm_z, \
                                outside_ang_mom_gas_x, outside_ang_mom_gas_y, outside_ang_mom_gas_z,  \
                                outside_spec_ang_mom_gas_x, outside_spec_ang_mom_gas_y, outside_spec_ang_mom_gas_z, \
                                outside_ang_mom_dm_x, outside_ang_mom_dm_y,outside_ang_mom_dm_z,\
                                outside_spec_ang_mom_dm_x, outside_spec_ang_mom_dm_y, outside_spec_ang_mom_dm_z, \
                                inside_ang_mom_stars_x, inside_ang_mom_stars_y, inside_ang_mom_stars_z, \
                                inside_spec_ang_mom_stars_x, inside_spec_ang_mom_stars_y, inside_spec_ang_mom_stars_z])

                # this apparently makes fluxes work in a loop?
                surface._vertices = None
            last_sphere = this_sphere

    # perhaps we should save the table?
    tablename = run_dir + '/' + args.run + '_angular_momenta_and_fluxes.dat'
    ascii.write(data, tablename, format='fixed_width')

    return "whooooo angular momentum wheeeeeeee"