"""
Unit Tests for mms_part_getspec bulk velocity subtraction.
"""
import pyspedas
from pyspedas.tplot_tools import data_exists, tplot_names, tplot, get_data, tplot_copy, del_data, store_data
import numpy as np
import unittest
from pyspedas import tplot_restore
from numpy.testing import assert_allclose
from pyspedas import download
from pyspedas import mms_part_getspec
from pyspedas import get_units

global_display=False


class TestGetspecBulkv(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        #  Test tolerance
        cls.tol = 1e-10

        # Download tplot files
        # Test data was generated by projects/mms/common/validation/mms_getspec_bulkv_subtract_python_validation.pro
        remote_server = 'https://github.com/spedas/test_data/raw/refs/heads/main/'
        remote_name = 'particle_tests/mms_getspec_bulkv_validate.tplot'
        datafile = download(remote_file=remote_name,
                            remote_path=remote_server,
                            local_path='testdata',
                            no_download=False)
        if not datafile:
            # Skip tests
            raise unittest.SkipTest("Cannot download data validation file")

        # Load validation variables from the test file
        filename = datafile[0]
        #filename = '/tmp/mms_getspec_bulkv_validate.tplot'
        tplot_restore(filename)
        # Spectra
        cls.e_nobulk_subtract = get_data('mms1_dis_dist_brst_energy_no_bulk_subtract')
        cls.e_bulk_subtract = get_data('mms1_dis_dist_brst_energy_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_energy_no_bulk_subtract','e_spec_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_energy_bulk_subtract','e_spec_bulk_subtract')
        cls.e_mag_nobulk_subtract = get_data('mms1_dis_dist_brst_energy_mag_no_bulk_subtract')
        cls.e_mag_bulk_subtract = get_data('mms1_dis_dist_brst_energy_mag_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_energy_mag_no_bulk_subtract','e_mag_spec_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_energy_mag_bulk_subtract','e_mag_spec_bulk_subtract')
        cls.theta_nobulk_subtract = get_data('mms1_dis_dist_brst_theta_no_bulk_subtract')
        cls.theta_bulk_subtract = get_data('mms1_dis_dist_brst_theta_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_theta_no_bulk_subtract','theta_spec_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_theta_bulk_subtract','theta_spec_bulk_subtract')
        cls.phi_nobulk_subtract = get_data('mms1_dis_dist_brst_phi_no_bulk_subtract')
        cls.phi_bulk_subtract = get_data('mms1_dis_dist_brst_phi_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_phi_no_bulk_subtract','phi_spec_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_phi_bulk_subtract','phi_spec_bulk_subtract')
        cls.pa_nobulk_subtract = get_data('mms1_dis_dist_brst_pa_no_bulk_subtract')
        cls.pa_bulk_subtract = get_data('mms1_dis_dist_brst_pa_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_pa_no_bulk_subtract','pa_spec_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_pa_bulk_subtract','pa_spec_bulk_subtract')
        cls.gyro_nobulk_subtract = get_data('mms1_dis_dist_brst_gyro_no_bulk_subtract')
        cls.gyro_bulk_subtract = get_data('mms1_dis_dist_brst_gyro_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_gyro_no_bulk_subtract','gyro_spec_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_gyro_bulk_subtract','gyro_spec_bulk_subtract')

        # Moments
        cls.avgtemp_nobulk_subtract = get_data('mms1_dis_dist_brst_avgtemp_no_bulk_subtract')
        cls.avgtemp_bulk_subtract = get_data('mms1_dis_dist_brst_avgtemp_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_avgtemp_no_bulk_subtract','avgtemp_mom_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_avgtemp_bulk_subtract','avgtemp_mom_bulk_subtract')
        cls.density_nobulk_subtract = get_data('mms1_dis_dist_brst_density_no_bulk_subtract')
        cls.density_bulk_subtract = get_data('mms1_dis_dist_brst_density_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_density_no_bulk_subtract','density_mom_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_density_bulk_subtract','density_mom_bulk_subtract')
        cls.eflux_nobulk_subtract = get_data('mms1_dis_dist_brst_eflux_no_bulk_subtract')
        cls.eflux_bulk_subtract = get_data('mms1_dis_dist_brst_eflux_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_eflux_no_bulk_subtract','eflux_mom_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_eflux_bulk_subtract','eflux_mom_bulk_subtract')
        cls.qflux_nobulk_subtract = get_data('mms1_dis_dist_brst_qflux_no_bulk_subtract')
        cls.qflux_bulk_subtract = get_data('mms1_dis_dist_brst_qflux_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_qflux_no_bulk_subtract','qflux_mom_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_qflux_bulk_subtract','qflux_mom_bulk_subtract')
        cls.flux_nobulk_subtract = get_data('mms1_dis_dist_brst_flux_no_bulk_subtract')
        cls.flux_bulk_subtract = get_data('mms1_dis_dist_brst_flux_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_flux_no_bulk_subtract','flux_mom_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_flux_bulk_subtract','flux_mom_bulk_subtract')
        cls.mftens_nobulk_subtract = get_data('mms1_dis_dist_brst_mftens_no_bulk_subtract')
        cls.mftens_bulk_subtract = get_data('mms1_dis_dist_brst_mftens_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_mftens_no_bulk_subtract','mftens_mom_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_mftens_bulk_subtract','mftens_mom_bulk_subtract')
        cls.ptens_nobulk_subtract = get_data('mms1_dis_dist_brst_ptens_no_bulk_subtract')
        cls.ptens_bulk_subtract = get_data('mms1_dis_dist_brst_ptens_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_ptens_no_bulk_subtract','ptens_mom_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_ptens_bulk_subtract','ptens_mom_bulk_subtract')
        cls.velocity_nobulk_subtract = get_data('mms1_dis_dist_brst_velocity_no_bulk_subtract')
        cls.velocity_bulk_subtract = get_data('mms1_dis_dist_brst_velocity_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_velocity_no_bulk_subtract','velocity_mom_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_velocity_bulk_subtract','velocity_mom_bulk_subtract')
        cls.vthermal_nobulk_subtract = get_data('mms1_dis_dist_brst_vthermal_no_bulk_subtract')
        cls.vthermal_bulk_subtract = get_data('mms1_dis_dist_brst_vthermal_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_vthermal_no_bulk_subtract','vthermal_mom_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_vthermal_bulk_subtract','vthermal_mom_bulk_subtract')
        cls.t3_nobulk_subtract = get_data('mms1_dis_dist_brst_t3_no_bulk_subtract')
        cls.t3_bulk_subtract = get_data('mms1_dis_dist_brst_t3_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_t3_no_bulk_subtract','t3_mom_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_t3_bulk_subtract','t3_mom_bulk_subtract')
        cls.magt3_nobulk_subtract = get_data('mms1_dis_dist_brst_magt3_no_bulk_subtract')
        cls.magt3_bulk_subtract = get_data('mms1_dis_dist_brst_magt3_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_magt3_no_bulk_subtract','magt3_mom_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_magt3_bulk_subtract','magt3_mom_bulk_subtract')
        cls.symm_nobulk_subtract = get_data('mms1_dis_dist_brst_symm_no_bulk_subtract')
        cls.symm_bulk_subtract = get_data('mms1_dis_dist_brst_symm_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_symm_no_bulk_subtract','symm_mom_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_symm_bulk_subtract','symm_mom_bulk_subtract')
        cls.symm_theta_nobulk_subtract = get_data('mms1_dis_dist_brst_symm_theta_no_bulk_subtract')
        cls.symm_theta_bulk_subtract = get_data('mms1_dis_dist_brst_symm_theta_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_symm_theta_no_bulk_subtract','symm_theta_mom_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_symm_theta_bulk_subtract','symm_theta_mom_bulk_subtract')
        cls.symm_phi_nobulk_subtract = get_data('mms1_dis_dist_brst_symm_phi_no_bulk_subtract')
        cls.symm_phi_bulk_subtract = get_data('mms1_dis_dist_brst_symm_phi_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_symm_phi_no_bulk_subtract','symm_phi_mom_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_symm_phi_bulk_subtract','symm_phi_mom_bulk_subtract')
        cls.symm_ang_nobulk_subtract = get_data('mms1_dis_dist_brst_symm_ang_no_bulk_subtract')
        cls.symm_ang_bulk_subtract = get_data('mms1_dis_dist_brst_symm_ang_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_symm_ang_no_bulk_subtract','symm_ang_mom_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_symm_ang_bulk_subtract','symm_ang_mom_bulk_subtract')

        # Field Aligned Moments
        cls.avgtemp_mag_nobulk_subtract = get_data('mms1_dis_dist_brst_avgtemp_mag_no_bulk_subtract')
        cls.avgtemp_mag_bulk_subtract = get_data('mms1_dis_dist_brst_avgtemp_mag_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_avgtemp_mag_no_bulk_subtract','avgtemp_mom_mag_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_avgtemp_mag_bulk_subtract','avgtemp_mom_mag_bulk_subtract')
        cls.density_mag_nobulk_subtract = get_data('mms1_dis_dist_brst_density_mag_no_bulk_subtract')
        cls.density_mag_bulk_subtract = get_data('mms1_dis_dist_brst_density_mag_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_density_mag_no_bulk_subtract','density_mom_mag_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_density_mag_bulk_subtract','density_mom_mag_bulk_subtract')
        cls.eflux_mag_nobulk_subtract = get_data('mms1_dis_dist_brst_eflux_mag_no_bulk_subtract')
        cls.eflux_mag_bulk_subtract = get_data('mms1_dis_dist_brst_eflux_mag_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_eflux_mag_no_bulk_subtract','eflux_mom_mag_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_eflux_mag_bulk_subtract','eflux_mom_mag_bulk_subtract')
        cls.qflux_mag_nobulk_subtract = get_data('mms1_dis_dist_brst_qflux_mag_no_bulk_subtract')
        cls.qflux_mag_bulk_subtract = get_data('mms1_dis_dist_brst_qflux_mag_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_qflux_mag_no_bulk_subtract','qflux_mom_mag_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_qflux_mag_bulk_subtract','qflux_mom_mag_bulk_subtract')
        cls.flux_mag_nobulk_subtract = get_data('mms1_dis_dist_brst_flux_mag_no_bulk_subtract')
        cls.flux_mag_bulk_subtract = get_data('mms1_dis_dist_brst_flux_mag_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_flux_mag_no_bulk_subtract','flux_mom_mag_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_flux_mag_bulk_subtract','flux_mom_mag_bulk_subtract')
        cls.mftens_mag_nobulk_subtract = get_data('mms1_dis_dist_brst_mftens_mag_no_bulk_subtract')
        cls.mftens_mag_bulk_subtract = get_data('mms1_dis_dist_brst_mftens_mag_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_mftens_mag_no_bulk_subtract','mftens_mom_mag_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_mftens_mag_bulk_subtract','mftens_mom_mag_bulk_subtract')
        cls.ptens_mag_nobulk_subtract = get_data('mms1_dis_dist_brst_ptens_mag_no_bulk_subtract')
        cls.ptens_mag_bulk_subtract = get_data('mms1_dis_dist_brst_ptens_mag_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_ptens_mag_no_bulk_subtract','ptens_mom_mag_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_ptens_mag_bulk_subtract','ptens_mom_mag_bulk_subtract')
        cls.velocity_mag_nobulk_subtract = get_data('mms1_dis_dist_brst_velocity_mag_no_bulk_subtract')
        cls.velocity_mag_bulk_subtract = get_data('mms1_dis_dist_brst_velocity_mag_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_velocity_mag_no_bulk_subtract','velocity_mom_mag_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_velocity_mag_bulk_subtract','velocity_mom_mag_bulk_subtract')
        cls.vthermal_mag_nobulk_subtract = get_data('mms1_dis_dist_brst_vthermal_mag_no_bulk_subtract')
        cls.vthermal_mag_bulk_subtract = get_data('mms1_dis_dist_brst_vthermal_mag_bulk_subtract')
        tplot_copy('mms1_dis_dist_brst_vthermal_mag_no_bulk_subtract','vthermal_mom_mag_nobulk_subtract')
        tplot_copy('mms1_dis_dist_brst_vthermal_mag_bulk_subtract','vthermal_mom_mag_bulk_subtract')

        # HPCA Spectra
        cls.hpca_e_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_energy_no_bulk_subtract')
        cls.hpca_e_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_energy_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_energy_no_bulk_subtract','hpca_e_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_energy_bulk_subtract','hpca_e_bulk_subtract')
        cls.hpca_phi_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_phi_no_bulk_subtract')
        cls.hpca_phi_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_phi_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_phi_no_bulk_subtract','hpca_phi_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_phi_bulk_subtract','hpca_phi_bulk_subtract')
        cls.hpca_theta_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_theta_no_bulk_subtract')
        cls.hpca_theta_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_theta_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_theta_no_bulk_subtract','hpca_theta_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_theta_bulk_subtract','hpca_theta_bulk_subtract')
        
        # HPCA field aligned spectra
        cls.hpca_energy_mag_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_energy_mag_no_bulk_subtract')
        cls.hpca_energy_mag_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_energy_mag_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_energy_mag_no_bulk_subtract','hpca_energy_mag_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_energy_mag_bulk_subtract','hpca_energy_mag_bulk_subtract')

        cls.hpca_pa_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_pa_no_bulk_subtract')
        cls.hpca_pa_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_pa_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_pa_no_bulk_subtract','hpca_pa_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_pa_bulk_subtract','hpca_pa_bulk_subtract')

        cls.hpca_gyro_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_gyro_no_bulk_subtract')
        cls.hpca_gyro_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_gyro_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_gyro_no_bulk_subtract','hpca_gyro_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_gyro_bulk_subtract','hpca_gyro_bulk_subtract')

        # HPCA Moments
        cls.hpca_avgtemp_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_avgtemp_no_bulk_subtract')
        cls.hpca_avgtemp_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_avgtemp_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_avgtemp_no_bulk_subtract','hpca_avgtemp_mom_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_avgtemp_bulk_subtract','hpca_avgtemp_mom_bulk_subtract')
        cls.hpca_density_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_density_no_bulk_subtract')
        cls.hpca_density_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_density_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_density_no_bulk_subtract','hpca_density_mom_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_density_bulk_subtract','hpca_density_mom_bulk_subtract')
        cls.hpca_eflux_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_eflux_no_bulk_subtract')
        cls.hpca_eflux_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_eflux_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_eflux_no_bulk_subtract','hpca_eflux_mom_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_eflux_bulk_subtract','hpca_eflux_mom_bulk_subtract')
        cls.hpca_qflux_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_qflux_no_bulk_subtract')
        cls.hpca_qflux_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_qflux_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_qflux_no_bulk_subtract','hpca_qflux_mom_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_qflux_bulk_subtract','hpca_qflux_mom_bulk_subtract')
        cls.hpca_flux_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_flux_no_bulk_subtract')
        cls.hpca_flux_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_flux_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_flux_no_bulk_subtract','hpca_flux_mom_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_flux_bulk_subtract','hpca_flux_mom_bulk_subtract')
        cls.hpca_mftens_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_mftens_no_bulk_subtract')
        cls.hpca_mftens_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_mftens_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_mftens_no_bulk_subtract','hpca_mftens_mom_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_mftens_bulk_subtract','hpca_mftens_mom_bulk_subtract')
        cls.hpca_ptens_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_ptens_no_bulk_subtract')
        cls.hpca_ptens_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_ptens_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_ptens_no_bulk_subtract','hpca_ptens_mom_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_ptens_bulk_subtract','hpca_ptens_mom_bulk_subtract')
        cls.hpca_velocity_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_velocity_no_bulk_subtract')
        cls.hpca_velocity_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_velocity_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_velocity_no_bulk_subtract','hpca_velocity_mom_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_velocity_bulk_subtract','hpca_velocity_mom_bulk_subtract')
        cls.hpca_vthermal_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_vthermal_no_bulk_subtract')
        cls.hpca_vthermal_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_vthermal_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_vthermal_no_bulk_subtract','hpca_vthermal_mom_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_vthermal_bulk_subtract','hpca_vthermal_mom_bulk_subtract')
        cls.hpca_t3_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_t3_no_bulk_subtract')
        cls.hpca_t3_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_t3_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_t3_no_bulk_subtract','hpca_t3_mom_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_t3_bulk_subtract','hpca_t3_mom_bulk_subtract')
        cls.hpca_magt3_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_magt3_no_bulk_subtract')
        cls.hpca_magt3_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_magt3_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_magt3_no_bulk_subtract','hpca_magt3_mom_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_magt3_bulk_subtract','hpca_magt3_mom_bulk_subtract')
        cls.hpca_symm_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_symm_no_bulk_subtract')
        cls.hpca_symm_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_symm_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_symm_no_bulk_subtract','hpca_symm_mom_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_symm_bulk_subtract','hpca_symm_mom_bulk_subtract')
        cls.hpca_symm_theta_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_symm_theta_no_bulk_subtract')
        cls.hpca_symm_theta_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_symm_theta_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_symm_theta_no_bulk_subtract','hpca_symm_theta_mom_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_symm_theta_bulk_subtract','hpca_symm_theta_mom_bulk_subtract')
        cls.hpca_symm_phi_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_symm_phi_no_bulk_subtract')
        cls.hpca_symm_phi_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_symm_phi_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_symm_phi_no_bulk_subtract','hpca_symm_phi_mom_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_symm_phi_bulk_subtract','hpca_symm_phi_mom_bulk_subtract')
        cls.hpca_symm_ang_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_symm_ang_no_bulk_subtract')
        cls.hpca_symm_ang_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_symm_ang_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_symm_ang_no_bulk_subtract','hpca_symm_ang_mom_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_symm_ang_bulk_subtract','hpca_symm_ang_mom_bulk_subtract')

        # HPCA Field Aligned Moments
        cls.hpca_avgtemp_mag_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_avgtemp_mag_no_bulk_subtract')
        cls.hpca_avgtemp_mag_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_avgtemp_mag_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_avgtemp_mag_no_bulk_subtract','hpca_avgtemp_mom_mag_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_avgtemp_mag_bulk_subtract','hpca_avgtemp_mom_mag_bulk_subtract')
        cls.hpca_density_mag_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_density_mag_no_bulk_subtract')
        cls.hpca_density_mag_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_density_mag_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_density_mag_no_bulk_subtract','hpca_density_mom_mag_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_density_mag_bulk_subtract','hpca_density_mom_mag_bulk_subtract')
        cls.hpca_eflux_mag_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_eflux_mag_no_bulk_subtract')
        cls.hpca_eflux_mag_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_eflux_mag_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_eflux_mag_no_bulk_subtract','hpca_eflux_mom_mag_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_eflux_mag_bulk_subtract','hpca_eflux_mom_mag_bulk_subtract')
        cls.hpca_qflux_mag_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_qflux_mag_no_bulk_subtract')
        cls.hpca_qflux_mag_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_qflux_mag_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_qflux_mag_no_bulk_subtract','hpca_qflux_mom_mag_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_qflux_mag_bulk_subtract','hpca_qflux_mom_mag_bulk_subtract')
        cls.hpca_flux_mag_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_flux_mag_no_bulk_subtract')
        cls.hpca_flux_mag_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_flux_mag_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_flux_mag_no_bulk_subtract','hpca_flux_mom_mag_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_flux_mag_bulk_subtract','hpca_flux_mom_mag_bulk_subtract')
        cls.hpca_mftens_mag_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_mftens_mag_no_bulk_subtract')
        cls.hpca_mftens_mag_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_mftens_mag_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_mftens_mag_no_bulk_subtract','hpca_mftens_mom_mag_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_mftens_mag_bulk_subtract','hpca_mftens_mom_mag_bulk_subtract')
        cls.hpca_ptens_mag_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_ptens_mag_no_bulk_subtract')
        cls.hpca_ptens_mag_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_ptens_mag_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_ptens_mag_no_bulk_subtract','hpca_ptens_mom_mag_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_ptens_mag_bulk_subtract','hpca_ptens_mom_mag_bulk_subtract')
        cls.hpca_velocity_mag_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_velocity_mag_no_bulk_subtract')
        cls.hpca_velocity_mag_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_velocity_mag_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_velocity_mag_no_bulk_subtract','hpca_velocity_mom_mag_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_velocity_mag_bulk_subtract','hpca_velocity_mom_mag_bulk_subtract')
        cls.hpca_vthermal_mag_nobulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_vthermal_mag_no_bulk_subtract')
        cls.hpca_vthermal_mag_bulk_subtract = get_data('mms1_hpca_hplus_phase_space_density_vthermal_mag_bulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_vthermal_mag_no_bulk_subtract','hpca_vthermal_mom_mag_nobulk_subtract')
        tplot_copy('mms1_hpca_hplus_phase_space_density_vthermal_mag_bulk_subtract','hpca_vthermal_mom_mag_bulk_subtract')
        tplot_names()
        del_data('mms1*')


    def test_getspec_e_nobulkv_subtract(self):
        """Test of getspec without bulk velocity subtraction"""

        mms_part_getspec(instrument='fpi',species='i', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['energy'], units='eflux', subtract_bulk=False, center_measurement=True, suffix='_no_bulk_subtract')
        tplot(['mms1_dis_dist_brst_energy_no_bulk_subtract','e_spec_nobulk_subtract'],display=global_display,save_png='mms_getspec_e_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_energy_no_bulk_subtract')
        # Test Y values (energy bins)
        assert_allclose(self.e_nobulk_subtract.v, pydat.v, rtol=1.0e-06)
        # Test Z values
        assert_allclose(self.e_nobulk_subtract.y, pydat.y, rtol=1.0e-06)

    def test_getspec_e_bulkv_subtract(self):
        """Test of getspec with bulk velocity subtraction"""

        mms_part_getspec(instrument='fpi',species='i', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['energy'], units='eflux', subtract_bulk=True, center_measurement=True, suffix='_bulk_subtract')
        tplot(['e_spec_nobulk_subtract', 'mms1_dis_dist_brst_energy_bulk_subtract','e_spec_bulk_subtract'],display=global_display,save_png='mms_getspec_e_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_energy_bulk_subtract')
        # Test Y values (energy bins)
        assert_allclose(self.e_nobulk_subtract.v, pydat.v, rtol=1.0e-06)
        # Test Z values
        assert_allclose(self.e_bulk_subtract.y, pydat.y, rtol=1.0e-06)

    def test_getspec_e_mag_nobulkv_subtract(self):
        """Test of getspec without bulk velocity subtraction"""

        mms_part_getspec(instrument='fpi',species='i', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['fac_energy'], no_regrid=True, units='eflux', subtract_bulk=False, center_measurement=True, suffix='_no_bulk_subtract')
        tplot(['mms1_dis_dist_brst_fac_energy_no_bulk_subtract','e_mag_spec_nobulk_subtract'],display=global_display,save_png='mms_getspec_e_mag_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_fac_energy_no_bulk_subtract')
        # Test Y values (energy bins)
        assert_allclose(self.e_mag_nobulk_subtract.v, pydat.v, rtol=1.0e-06)
        # Test Z values
        assert_allclose(self.e_mag_nobulk_subtract.y, pydat.y, rtol=1.0e-06)

    def test_getspec_e_mag_bulkv_subtract(self):
        """Test of getspec with bulk velocity subtraction"""

        mms_part_getspec(instrument='fpi',species='i', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['fac_energy'], no_regrid=True, units='eflux', subtract_bulk=True, center_measurement=True, suffix='_bulk_subtract')
        tplot(['e_mag_spec_bulk_subtract', 'mms1_dis_dist_brst_fac_energy_bulk_subtract','e_mag_spec_bulk_subtract'],display=global_display,save_png='mms_getspec_e_mag_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_fac_energy_bulk_subtract')
        # Test Y values (energy bins)
        assert_allclose(self.e_mag_bulk_subtract.v, pydat.v, rtol=1.0e-06)
        # Test Z values
        assert_allclose(self.e_mag_bulk_subtract.y, pydat.y, rtol=1.0e-06)


    def test_getspec_theta_nobulkv_subtract(self):
        """Test of getspec without bulk velocity subtraction"""

        mms_part_getspec(instrument='fpi',species='i', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['theta'], units='eflux', subtract_bulk=False, center_measurement=True, suffix='_no_bulk_subtract')
        tplot(['mms1_dis_dist_brst_theta_no_bulk_subtract','theta_spec_nobulk_subtract'],display=global_display,save_png='mms_getspec_theta_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_theta_no_bulk_subtract')
        # Test Y values (energy bins)
        #assert_allclose(self.theta_nobulk_subtract.v, pydat.v, rtol=1.0e-05)
        # Test Z values
        assert_allclose(self.theta_nobulk_subtract.y, pydat.y, rtol=1.0e-06)

    def test_getspec_theta_bulkv_subtract(self):
        """Test of getspec with bulk velocity subtraction"""

        mms_part_getspec(instrument='fpi',species='i', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['theta'], units='eflux', subtract_bulk=True, center_measurement=True, suffix='_bulk_subtract')
        tplot(['theta_spec_nobulk_subtract', 'mms1_dis_dist_brst_theta_bulk_subtract','theta_spec_bulk_subtract'],display=global_display,save_png='mms_getspec_theta_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_theta_bulk_subtract')
        # Test Y values (energy bins)
        #assert_allclose(self.theta_nobulk_subtract.v, pydat.v, rtol=1.0e-05)
        # Test Z values  largest relative difference 0.00088
        assert_allclose(self.theta_bulk_subtract.y, pydat.y, rtol=1.0e-03)

    def test_getspec_phi_nobulkv_subtract(self):
        """Test of getspec without bulk velocity subtraction"""

        mms_part_getspec(instrument='fpi',species='i', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['phi'], units='eflux', subtract_bulk=False, center_measurement=True, suffix='_no_bulk_subtract')
        tplot(['mms1_dis_dist_brst_phi_no_bulk_subtract','phi_spec_nobulk_subtract'],display=global_display,save_png='mms_getspec_phi_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_phi_no_bulk_subtract')
        # Test Y values (energy bins)
        #assert_allclose(self.phi_nobulk_subtract.v, pydat.v, rtol=1.0e-05)
        # Test Z values  largest relative error about .00088
        assert_allclose(self.phi_nobulk_subtract.y, pydat.y, rtol=1.0e-04)

    def test_getspec_phi_bulkv_subtract(self):
        """Test of getspec with bulk velocity subtraction"""

        mms_part_getspec(instrument='fpi',species='i', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['phi'], units='eflux', subtract_bulk=True, center_measurement=True, suffix='_bulk_subtract')
        tplot(['phi_spec_nobulk_subtract', 'mms1_dis_dist_brst_phi_bulk_subtract','phi_spec_bulk_subtract'],display=global_display,save_png='mms_getspec_phi_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_phi_bulk_subtract')
        # Test Y values (energy bins)
        #assert_allclose(self.phi_nobulk_subtract.v, pydat.v, rtol=1.0e-05)
        # Test Z values
        assert_allclose(self.phi_bulk_subtract.y, pydat.y, rtol=1.0e-06)

    def test_getspec_pa_nobulkv_subtract(self):
        """Test of getspec without bulk velocity subtraction"""

        mms_part_getspec(instrument='fpi',species='i', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['pa'], no_regrid=True, units='eflux', subtract_bulk=False, center_measurement=True, suffix='_no_bulk_subtract')
        pydat=get_data('mms1_dis_dist_brst_pa_no_bulk_subtract')
        diff = np.abs(self.pa_nobulk_subtract.y - pydat.y)
        md = get_data('mms1_dis_dist_brst_pa_no_bulk_subtract',metadata=True)
        store_data('pa_diff', data={'x': pydat.times, 'y': diff, 'v': pydat.v}, attr_dict=md)

        tplot(['mms1_dis_dist_brst_pa_no_bulk_subtract','pa_spec_nobulk_subtract', 'pa_diff'],display=global_display,save_png='mms_getspec_pa_nobulk_subtract.png')
        # Test Y values (energy bins)
        #assert_allclose(self.pa_nobulk_subtract.v, pydat.v, rtol=1.0e-05)
        # Test Z values
        assert_allclose(self.pa_nobulk_subtract.y, pydat.y, rtol=1.0e-06)

    def test_getspec_pa_bulkv_subtract(self):
        """Test of getspec with bulk velocity subtraction"""

        mms_part_getspec(instrument='fpi',species='i', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['pa'], units='eflux', subtract_bulk=True, center_measurement=True, suffix='_bulk_subtract')
        pydat=get_data('mms1_dis_dist_brst_pa_bulk_subtract')
        tplot(['pa_spec_nobulk_subtract', 'mms1_dis_dist_brst_pa_bulk_subtract','pa_spec_bulk_subtract'],display=global_display,save_png='mms_getspec_pa_bulk_subtract.png')
        # Test Y values (energy bins)
        #assert_allclose(self.pa_nobulk_subtract.v, pydat.v, rtol=1.0e-05)
        # Test Z values   largest relative difference about 0.004
        assert_allclose(self.pa_bulk_subtract.y, pydat.y, rtol=5.0e-03)

    def test_getspec_gyro_nobulkv_subtract(self):
        """Test of getspec without bulk velocity subtraction"""

        mms_part_getspec(instrument='fpi',species='i', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['gyro'], no_regrid=True, units='eflux', subtract_bulk=False, center_measurement=True, suffix='_no_bulk_subtract')
        tplot(['mms1_dis_dist_brst_gyro_no_bulk_subtract','gyro_spec_nobulk_subtract'],display=global_display,save_png='mms_getspec_gyro_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_gyro_no_bulk_subtract')
        # Test Y values (energy bins)
        #assert_allclose(self.gyro_nobulk_subtract.v, pydat.v, rtol=1.0e-05)
        # Test Z values
        assert_allclose(self.gyro_nobulk_subtract.y, pydat.y, rtol=1.0e-06)

    def test_getspec_gyro_bulkv_subtract(self):
        """Test of getspec with bulk velocity subtraction"""

        mms_part_getspec(instrument='fpi',species='i', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['gyro'], units='eflux', subtract_bulk=True, center_measurement=True, suffix='_bulk_subtract')
        tplot(['gyro_spec_nobulk_subtract', 'mms1_dis_dist_brst_gyro_bulk_subtract','gyro_spec_bulk_subtract'],display=global_display,save_png='mms_getspec_gyro_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_gyro_bulk_subtract')
        # Test Y values (energy bins)
        #assert_allclose(self.gyro_nobulk_subtract.v, pydat.v, rtol=1.0e-05)
        # Test Z values  largest relative differnence about .008
        assert_allclose(self.gyro_bulk_subtract.y, pydat.y, rtol=9.0e-03)

    def test_getspec_moments_nobulkv_subtract(self):
        """Test of getspec without bulk velocity subtraction"""

        mms_part_getspec(instrument='fpi',species='i', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['moments'], units='eflux', subtract_bulk=False, center_measurement=True, suffix='_no_bulk_subtract')
        tplot(['mms1_dis_dist_brst_avgtemp_no_bulk_subtract','avgtemp_mom_nobulk_subtract'],display=global_display,save_png='mms_getspec_avgtemp_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_avgtemp_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_avgtemp_no_bulk_subtract')
        self.assertEqual(units,'eV')
        assert_allclose(self.avgtemp_nobulk_subtract.y, pydat.y, rtol=1.0e-06)

        tplot(['mms1_dis_dist_brst_density_no_bulk_subtract','density_mom_nobulk_subtract'],display=global_display,save_png='mms_getspec_density_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_density_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_density_no_bulk_subtract')
        self.assertEqual(units,'1/cm^3')
        assert_allclose(self.density_nobulk_subtract.y, pydat.y, rtol=1.0e-06)

        tplot(['mms1_dis_dist_brst_eflux_no_bulk_subtract','eflux_mom_nobulk_subtract'],display=global_display,save_png='mms_getspec_eflux_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_eflux_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_eflux_no_bulk_subtract')
        self.assertEqual(units,'eV/(cm^2-s)')
        assert_allclose(self.eflux_nobulk_subtract.y, pydat.y, rtol=1.0e-06)

        tplot(['mms1_dis_dist_brst_qflux_no_bulk_subtract','qflux_mom_nobulk_subtract'],display=global_display,save_png='mms_getspec_qflux_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_qflux_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_qflux_no_bulk_subtract')
        self.assertEqual(units,'eV/(cm^2-s)')
        # max relative difference .0003
        assert_allclose(self.qflux_nobulk_subtract.y, pydat.y, rtol=.0005)

        tplot(['mms1_dis_dist_brst_flux_no_bulk_subtract','flux_mom_nobulk_subtract'],display=global_display,save_png='mms_getspec_flux_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_flux_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_flux_no_bulk_subtract')
        self.assertEqual(units,'1/(cm^2-s)')
        # largest relative difference about .00015
        assert_allclose(self.flux_nobulk_subtract.y, pydat.y, rtol=1.6e-04)

        tplot(['mms1_dis_dist_brst_mftens_no_bulk_subtract','mftens_mom_nobulk_subtract'],display=global_display,save_png='mms_getspec_mftens_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_mftens_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_mftens_no_bulk_subtract')
        self.assertEqual(units,'eV/cm^3')
        # largest relative difference about .00016
        assert_allclose(self.mftens_nobulk_subtract.y, pydat.y, rtol=1.7e-04)

        tplot(['mms1_dis_dist_brst_ptens_no_bulk_subtract','ptens_mom_nobulk_subtract'],display=global_display,save_png='mms_getspec_ptens_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_ptens_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_ptens_no_bulk_subtract')
        self.assertEqual(units,'eV/cm^3')
        # largest relative difference about 0.0005
        assert_allclose(self.ptens_nobulk_subtract.y, pydat.y, rtol=6.0e-04)

        tplot(['mms1_dis_dist_brst_velocity_no_bulk_subtract','velocity_mom_nobulk_subtract'],display=global_display,save_png='mms_getspec_velocity_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_velocity_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_velocity_no_bulk_subtract')
        self.assertEqual(units,'km/s')
        assert_allclose(self.velocity_nobulk_subtract.y, pydat.y, rtol=1.0e-06)

        tplot(['mms1_dis_dist_brst_vthermal_no_bulk_subtract','vthermal_mom_nobulk_subtract'],display=global_display,save_png='mms_getspec_vthermal_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_vthermal_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_vthermal_no_bulk_subtract')
        self.assertEqual(units,'km/s')
        assert_allclose(self.vthermal_nobulk_subtract.y, pydat.y, rtol=1.0e-06)

        tplot(['mms1_dis_dist_brst_t3_no_bulk_subtract','t3_mom_nobulk_subtract'],display=global_display,save_png='mms_getspec_t3_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_t3_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_t3_no_bulk_subtract')
        self.assertEqual(units,'eV')
        # The last component (t_para) is the only one guaranteed to be in the same position in the IDL
        # and Python outputs.  The other two (t_perp1 and t_perp2) could be swapped, due to difference
        # in eigenvalue sorting between the two platforms.
        # First check t_para in column 2
        # Max relative difference 1.5e-06
        assert_allclose(self.t3_nobulk_subtract.y[:,2], pydat.y[:,2], rtol=2.0e-06)
        # To check the other two components, we'll take the element-by-element maximum of y[:,0],y[:,1]
        # for both the Python and IDL outputs.  Then we'll do the same for the minima.  The Python
        # max and min values should match the IDL values, even if some of them are swapped.
        idlmax = np.maximum(self.t3_nobulk_subtract.y[:,0], self.t3_nobulk_subtract.y[:,1])
        idlmin = np.minimum(self.t3_nobulk_subtract.y[:,0], self.t3_nobulk_subtract.y[:,1])
        pymax = np.maximum(pydat.y[:,0], pydat.y[:,1])
        pymin = np.minimum(pydat.y[:,0], pydat.y[:,1])
        assert_allclose(idlmax, pymax, rtol=2.0e-06)
        assert_allclose(idlmin, pymin, rtol=2.0e-06)

        tplot(['mms1_dis_dist_brst_magt3_no_bulk_subtract','magt3_mom_nobulk_subtract'],display=global_display,save_png='mms_getspec_magt3_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_magt3_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_magt3_no_bulk_subtract')
        self.assertEqual(units,'eV')
        # Max relative difference 1.7e-06
        assert_allclose(self.magt3_nobulk_subtract.y, pydat.y, rtol=2.0e-06)

        tplot(['mms1_dis_dist_brst_symm_no_bulk_subtract','symm_mom_nobulk_subtract'],display=global_display,save_png='mms_getspec_symm_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_symm_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_symm_no_bulk_subtract')
        # max relative error ,0003
        assert_allclose(self.symm_nobulk_subtract.y, pydat.y, rtol=5.0e-04)

        tplot(['mms1_dis_dist_brst_symm_theta_no_bulk_subtract','symm_theta_mom_nobulk_subtract'],display=global_display,save_png='mms_getspec_symm_theta_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_symm_theta_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_symm_theta_no_bulk_subtract')
        self.assertEqual(units,'degrees')
        # max relative error 1.0e-05
        assert_allclose(self.symm_theta_nobulk_subtract.y, pydat.y, rtol=2.0e-05)

        tplot(['mms1_dis_dist_brst_symm_phi_no_bulk_subtract','symm_phi_mom_nobulk_subtract'],display=global_display,save_png='mms_getspec_symm_phi_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_symm_phi_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_symm_phi_no_bulk_subtract')
        self.assertEqual(units,'degrees')
        # max relative error .0003
        assert_allclose(self.symm_phi_nobulk_subtract.y, pydat.y, rtol=5.0e-04)

        tplot(['mms1_dis_dist_brst_symm_ang_no_bulk_subtract','symm_ang_mom_nobulk_subtract'],display=global_display,save_png='mms_getspec_symm_ang_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_symm_ang_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_symm_ang_no_bulk_subtract')
        self.assertEqual(units,'degrees')
        # max relative error 6.3e-05
        assert_allclose(self.symm_ang_nobulk_subtract.y, pydat.y, rtol=7.0e-05)

    def test_getspec_moments_bulkv_subtract(self):
        """Test of getspec with bulk velocity subtraction"""

        mms_part_getspec(instrument='fpi',species='i', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['moments'], units='eflux', subtract_bulk=True, center_measurement=True, suffix='_bulk_subtract')
        tplot(['avgtemp_mom_nobulk_subtract', 'mms1_dis_dist_brst_avgtemp_bulk_subtract','avgtemp_mom_bulk_subtract'],display=global_display,save_png='mms_getspec_avgtemp_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_avgtemp_bulk_subtract')
        assert_allclose(self.avgtemp_bulk_subtract.y, pydat.y, rtol=1.0e-05)

        tplot(['mms1_dis_dist_brst_density_bulk_subtract','density_mom_bulk_subtract'],display=global_display,save_png='mms_getspec_density_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_density_bulk_subtract')
        # max relative difference 1.13e-05
        assert_allclose(self.density_bulk_subtract.y, pydat.y, rtol=1.2e-05)

        tplot(['mms1_dis_dist_brst_eflux_bulk_subtract','eflux_mom_bulk_subtract'],display=global_display,save_png='mms_getspec_eflux_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_eflux_bulk_subtract')
        # max relative difference 5.3e-05
        assert_allclose(self.eflux_bulk_subtract.y, pydat.y, rtol=6.0e-05)

        tplot(['mms1_dis_dist_brst_qflux_bulk_subtract','qflux_mom_bulk_subtract'],display=global_display,save_png='mms_getspec_qflux_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_qflux_bulk_subtract')
        # max relative difference .0002
        assert_allclose(self.qflux_bulk_subtract.y, pydat.y, rtol=.0003)

        tplot(['mms1_dis_dist_brst_flux_bulk_subtract','flux_mom_bulk_subtract'],display=global_display,save_png='mms_getspec_flux_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_flux_bulk_subtract')
        # max relative difference .0019
        assert_allclose(self.flux_bulk_subtract.y, pydat.y, rtol=2.0e-03)

        tplot(['mms1_dis_dist_brst_mftens_bulk_subtract','mftens_mom_bulk_subtract'],display=global_display,save_png='mms_getspec_mftens_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_mftens_bulk_subtract')
        # max relative difference .0003
        assert_allclose(self.mftens_bulk_subtract.y, pydat.y, rtol=3.1e-03)

        tplot(['mms1_dis_dist_brst_ptens_bulk_subtract','ptens_mom_bulk_subtract'],display=global_display,save_png='mms_getspec_ptens_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_ptens_bulk_subtract')
        # max relative difference 0.00023
        assert_allclose(self.ptens_bulk_subtract.y, pydat.y, rtol=3.0e-03)

        tplot(['mms1_dis_dist_brst_velocity_bulk_subtract','velocity_mom_bulk_subtract'],display=global_display,save_png='mms_getspec_velocity_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_velocity_bulk_subtract')
        # max relative difference 0.0019
        assert_allclose(self.velocity_bulk_subtract.y, pydat.y, rtol=2.0e-03)

        tplot(['mms1_dis_dist_brst_vthermal_bulk_subtract','vthermal_mom_bulk_subtract'],display=global_display,save_png='mms_getspec_vthermal_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_vthermal_bulk_subtract')
        # max relative difference 2.e3-06
        assert_allclose(self.vthermal_bulk_subtract.y, pydat.y, rtol=3.0e-06)

        tplot(['mms1_dis_dist_brst_t3_bulk_subtract','t3_mom_bulk_subtract'],display=global_display,save_png='mms_getspec_t3_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_t3_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_t3_bulk_subtract')
        self.assertEqual(units,'eV')
        # The last component (t_para) is the only one guaranteed to be in the same position in the IDL
        # and Python outputs.  The other two (t_perp1 and t_perp2) could be swapped, due to difference
        # in eigenvalue sorting between the two platforms.
        # First check t_para in column 2
        # Max relative difference 5.1e-06
        assert_allclose(self.t3_bulk_subtract.y[:,2], pydat.y[:,2], rtol=6.0e-06)
        # To check the other two components, we'll take the element-by-element maximum of y[:,0],y[:,1]
        # for both the Python and IDL outputs.  Then we'll do the same for the minima.  The Python
        # max and min values should match the IDL values, even if some of them are swapped.
        idlmax = np.maximum(self.t3_bulk_subtract.y[:,0], self.t3_bulk_subtract.y[:,1])
        idlmin = np.minimum(self.t3_bulk_subtract.y[:,0], self.t3_bulk_subtract.y[:,1])
        pymax = np.maximum(pydat.y[:,0], pydat.y[:,1])
        pymin = np.minimum(pydat.y[:,0], pydat.y[:,1])
        assert_allclose(idlmax, pymax, rtol=8.0e-06)
        assert_allclose(idlmin, pymin, rtol=8.0e-06)

        tplot(['mms1_dis_dist_brst_magt3_bulk_subtract','magt3_mom_bulk_subtract'],display=global_display,save_png='mms_getspec_magt3_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_magt3_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_magt3_bulk_subtract')
        self.assertEqual(units,'eV')
        # Max relative difference 9.3e-06
        assert_allclose(self.magt3_bulk_subtract.y, pydat.y, rtol=1.0e-05)

        tplot(['mms1_dis_dist_brst_symm_bulk_subtract','symm_mom_bulk_subtract'],display=global_display,save_png='mms_getspec_symm_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_symm_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_symm_bulk_subtract')
        # max relative error ,0003
        assert_allclose(self.symm_bulk_subtract.y, pydat.y, rtol=5.0e-04)

        tplot(['mms1_dis_dist_brst_symm_theta_bulk_subtract','symm_theta_mom_bulk_subtract'],display=global_display,save_png='mms_getspec_symm_theta_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_symm_theta_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_symm_theta_bulk_subtract')
        self.assertEqual(units,'degrees')
        # max relative error .00019
        assert_allclose(self.symm_theta_bulk_subtract.y, pydat.y, rtol=2.0e-04)

        tplot(['mms1_dis_dist_brst_symm_phi_bulk_subtract','symm_phi_mom_bulk_subtract'],display=global_display,save_png='mms_getspec_symm_phi_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_symm_phi_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_symm_phi_bulk_subtract')
        self.assertEqual(units,'degrees')
        # max relative error .0003
        assert_allclose(self.symm_phi_bulk_subtract.y, pydat.y, rtol=5.0e-04)

        tplot(['mms1_dis_dist_brst_symm_ang_bulk_subtract','symm_ang_mom_bulk_subtract'],display=global_display,save_png='mms_getspec_symm_ang_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_symm_ang_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_symm_ang_bulk_subtract')
        self.assertEqual(units,'degrees')
        # max relative error .0015
        assert_allclose(self.symm_ang_bulk_subtract.y, pydat.y, rtol=.002)

    def test_getspec_fac_moments_nobulkv_subtract(self):
        """Test of getspec without bulk velocity subtraction"""

        mms_part_getspec(instrument='fpi',species='i', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['fac_moments'], no_regrid=True, units='eflux', subtract_bulk=False, center_measurement=True, suffix='_no_bulk_subtract')
        tplot(['mms1_dis_dist_brst_fac_avgtemp_no_bulk_subtract','avgtemp_mom_mag_nobulk_subtract', 'avgtemp_mom_nobulk_subtract'],display=global_display,save_png='mms_getspec_avgtemp_mag_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_fac_avgtemp_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_fac_avgtemp_no_bulk_subtract')
        self.assertEqual(units,'eV')
        assert_allclose(self.avgtemp_mag_nobulk_subtract.y, pydat.y, rtol=1.0e-06)

        tplot(['mms1_dis_dist_brst_fac_density_no_bulk_subtract','density_mom_mag_nobulk_subtract'],display=global_display,save_png='mms_getspec_density_mag_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_fac_density_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_fac_density_no_bulk_subtract')
        self.assertEqual(units,'1/cm^3')
        # max relative difference 1.2e-06
        assert_allclose(self.density_mag_nobulk_subtract.y, pydat.y, rtol=2.0e-06)

        tplot(['mms1_dis_dist_brst_fac_eflux_no_bulk_subtract','eflux_mom_mag_nobulk_subtract'],display=global_display,save_png='mms_getspec_eflux_mag_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_fac_eflux_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_fac_eflux_no_bulk_subtract')
        self.assertEqual(units,'eV/(cm^2-s)')
        # max relative difference .0004
        assert_allclose(self.eflux_mag_nobulk_subtract.y, pydat.y, rtol=.0005)

        tplot(['mms1_dis_dist_brst_fac_qflux_no_bulk_subtract','qflux_mom_mag_nobulk_subtract'],display=global_display,save_png='mms_getspec_qflux_mag_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_fac_qflux_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_fac_qflux_no_bulk_subtract')
        self.assertEqual(units,'eV/(cm^2-s)')
        # max relative difference .0005
        assert_allclose(self.qflux_mag_nobulk_subtract.y, pydat.y, rtol=.0006)

        tplot(['mms1_dis_dist_brst_fac_flux_no_bulk_subtract','flux_mom_mag_nobulk_subtract'],display=global_display,save_png='mms_getspec_flux_mag_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_fac_flux_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_fac_flux_no_bulk_subtract')
        self.assertEqual(units,'1/(cm^2-s)')
        # largest relative difference about .0005
        assert_allclose(self.flux_mag_nobulk_subtract.y, pydat.y, rtol=6.0e-04)

        tplot(['mms1_dis_dist_brst_fac_mftens_no_bulk_subtract','mftens_mom_mag_nobulk_subtract'],display=global_display,save_png='mms_getspec_mftens_mag_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_fac_mftens_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_fac_mftens_no_bulk_subtract')
        self.assertEqual(units,'eV/cm^3')
        # largest relative difference about .0005
        assert_allclose(self.mftens_mag_nobulk_subtract.y, pydat.y, rtol=6.0e-04)

        tplot(['mms1_dis_dist_brst_fac_ptens_no_bulk_subtract','ptens_mom_mag_nobulk_subtract'],display=global_display,save_png='mms_getspec_ptens_mag_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_fac_ptens_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_fac_ptens_no_bulk_subtract')
        self.assertEqual(units,'eV/cm^3')
        # largest relative difference about 0.0009
        assert_allclose(self.ptens_mag_nobulk_subtract.y, pydat.y, rtol=.001)

        tplot(['mms1_dis_dist_brst_fac_velocity_no_bulk_subtract','velocity_mom_mag_nobulk_subtract'],display=global_display,save_png='mms_getspec_velocity_mag_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_fac_velocity_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_fac_velocity_no_bulk_subtract')
        self.assertEqual(units,'km/s')
        # max relative difference .0005
        assert_allclose(self.velocity_mag_nobulk_subtract.y, pydat.y, rtol=.0006)

        tplot(['mms1_dis_dist_brst_fac_vthermal_no_bulk_subtract','vthermal_mom_mag_nobulk_subtract'],display=global_display,save_png='mms_getspec_vthermal_mag_nobulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_fac_vthermal_no_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_fac_vthermal_no_bulk_subtract')
        self.assertEqual(units,'km/s')
        assert_allclose(self.vthermal_mag_nobulk_subtract.y, pydat.y, rtol=1.0e-06)

    def test_getspec_fac_moments_bulkv_subtract(self):
        """Test of getspec with bulk velocity subtraction"""

        mms_part_getspec(instrument='fpi',species='i', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['fac_moments'], no_regrid=True, units='eflux', subtract_bulk=True, center_measurement=True, suffix='_bulk_subtract')
        tplot(['mms1_dis_dist_brst_fac_avgtemp_bulk_subtract','avgtemp_mom_mag_bulk_subtract', 'avgtemp_mom_bulk_subtract'],display=global_display,save_png='mms_getspec_avgtemp_mag_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_fac_avgtemp_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_fac_avgtemp_bulk_subtract')
        self.assertEqual(units,'eV')
        # max relative difference 6.6e-06
        assert_allclose(self.avgtemp_mag_bulk_subtract.y, pydat.y, rtol=7.0e-06)

        tplot(['mms1_dis_dist_brst_fac_density_bulk_subtract','density_mom_mag_bulk_subtract'],display=global_display,save_png='mms_getspec_density_mag_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_fac_density_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_fac_density_bulk_subtract')
        self.assertEqual(units,'1/cm^3')
        # max relative difference 1.2e-05
        assert_allclose(self.density_mag_bulk_subtract.y, pydat.y, rtol=2.0e-05)

        tplot(['mms1_dis_dist_brst_fac_eflux_bulk_subtract','eflux_mom_mag_bulk_subtract'],display=global_display,save_png='mms_getspec_eflux_mag_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_fac_eflux_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_fac_eflux_bulk_subtract')
        self.assertEqual(units,'eV/(cm^2-s)')
        # max relative difference .0004
        assert_allclose(self.eflux_mag_bulk_subtract.y, pydat.y, rtol=.0005)

        tplot(['mms1_dis_dist_brst_fac_qflux_bulk_subtract','qflux_mom_mag_bulk_subtract'],display=global_display,save_png='mms_getspec_qflux_mag_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_fac_qflux_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_fac_qflux_bulk_subtract')
        self.assertEqual(units,'eV/(cm^2-s)')
        # max relative difference .0004
        assert_allclose(self.qflux_mag_bulk_subtract.y, pydat.y, rtol=.0005)

        tplot(['mms1_dis_dist_brst_fac_flux_bulk_subtract','flux_mom_mag_bulk_subtract'],display=global_display,save_png='mms_getspec_flux_mag_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_fac_flux_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_fac_flux_bulk_subtract')
        self.assertEqual(units,'1/(cm^2-s)')
        # largest relative difference about .0005
        assert_allclose(self.flux_mag_bulk_subtract.y, pydat.y, rtol=6.0e-04)

        tplot(['mms1_dis_dist_brst_fac_mftens_bulk_subtract','mftens_mom_mag_bulk_subtract'],display=global_display,save_png='mms_getspec_mftens_mag_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_fac_mftens_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_fac_mftens_bulk_subtract')
        self.assertEqual(units,'eV/cm^3')
        # largest relative difference about .0008
        assert_allclose(self.mftens_mag_bulk_subtract.y, pydat.y, rtol=.001)

        tplot(['mms1_dis_dist_brst_fac_ptens_bulk_subtract','ptens_mom_mag_bulk_subtract'],display=global_display,save_png='mms_getspec_ptens_mag_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_fac_ptens_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_fac_ptens_bulk_subtract')
        self.assertEqual(units,'eV/cm^3')
        # largest relative difference about 0.0023
        assert_allclose(self.ptens_mag_bulk_subtract.y, pydat.y, rtol=.003)

        tplot(['mms1_dis_dist_brst_fac_velocity_bulk_subtract','velocity_mom_mag_bulk_subtract'],display=global_display,save_png='mms_getspec_velocity_mag_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_fac_velocity_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_fac_velocity_bulk_subtract')
        self.assertEqual(units,'km/s')
        # max relative difference .0005
        assert_allclose(self.velocity_mag_bulk_subtract.y, pydat.y, rtol=.0006)

        tplot(['mms1_dis_dist_brst_fac_vthermal_bulk_subtract','vthermal_mom_mag_bulk_subtract'],display=global_display,save_png='mms_getspec_vthermal_mag_bulk_subtract.png')
        pydat=get_data('mms1_dis_dist_brst_fac_vthermal_bulk_subtract')
        units=get_units('mms1_dis_dist_brst_fac_vthermal_bulk_subtract')
        self.assertEqual(units,'km/s')
        # max relative difference 3.2e-06
        assert_allclose(self.vthermal_mag_bulk_subtract.y, pydat.y, rtol=4.0e-06)

    def test_hpca_getspec_e_nobulkv_subtract(self):
        """Test of getspec without bulk velocity subtraction"""

        mms_part_getspec(instrument='hpca',species='hplus', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['energy'], units='eflux', subtract_bulk=False, center_measurement=True, suffix='_no_bulk_subtract')
        tplot(['mms1_hpca_hplus_phase_space_density_energy_no_bulk_subtract','hpca_e_nobulk_subtract'],display=global_display,save_png='mms_hpca_getspec_e_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_energy_no_bulk_subtract')
        # Test Y values (energy bins)  2D vs 1D mismatch (but values probably consistent)
        #assert_allclose(self.hpca_e_nobulk_subtract.v, pydat.v, rtol=1.0e-06)
        # Test Z values
        assert_allclose(self.hpca_e_nobulk_subtract.y, pydat.y, rtol=1.0e-06)

    def test_hpca_getspec_e_bulkv_subtract(self):
        """Test of getspec with bulk velocity subtraction"""

        mms_part_getspec(instrument='hpca',subtract_bulk=True, species='hplus', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['energy'], units='eflux', center_measurement=True, suffix='_bulk_subtract')
        tplot(['hpca_e_nobulk_subtract','mms1_hpca_hplus_phase_space_density_energy_bulk_subtract','hpca_e_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_e_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_energy_bulk_subtract')
        # Test Y values (energy bins)  Probably 2d vs 1D issue
        #assert_allclose(self.hpca_e_bulk_subtract.v, pydat.v, rtol=1.0e-06)
        # Test Z values
        assert_allclose(self.hpca_e_bulk_subtract.y, pydat.y, rtol=1.0e-06)

    def test_hpca_getspec_energy_mag_nobulkv_subtract(self):
        """Test of getspec without bulk velocity subtraction"""

        mms_part_getspec(instrument='hpca',species='hplus', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['fac_energy'], no_regrid=True, units='eflux', subtract_bulk=False, center_measurement=True, suffix='_no_bulk_subtract')
        tplot(['mms1_hpca_hplus_phase_space_density_fac_energy_no_bulk_subtract','hpca_energy_mag_nobulk_subtract'],display=global_display,save_png='mms_hpca_getspec_energy_fac_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_fac_energy_no_bulk_subtract')
        # Test Y values (energy_mag bins) Probable 2d vs 1d issue
        #assert_allclose(self.hpca_energy_mag_nobulk_subtract.v, pydat.v, rtol=1.0e-06)
        # Test Z values
        assert_allclose(self.hpca_energy_mag_nobulk_subtract.y, pydat.y, rtol=1.0e-06)

    def test_hpca_getspec_energy_mag_bulkv_subtract(self):
        """Test of getspec with bulk velocity subtraction"""

        mms_part_getspec(instrument='hpca',subtract_bulk=True, species='hplus', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['fac_energy'], no_regrid=True, units='eflux', center_measurement=True, suffix='_bulk_subtract')
        tplot(['hpca_energy_mag_nobulk_subtract','mms1_hpca_hplus_phase_space_density_fac_energy_bulk_subtract','hpca_energy_mag_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_energy_mag_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_fac_energy_bulk_subtract')
        # Test Y values (energy_mag bins) 2d vs 1d
        #assert_allclose(self.hpca_energy_mag_bulk_subtract.v, pydat.v, rtol=1.0e-06)
        # Test Z values
        assert_allclose(self.hpca_energy_mag_bulk_subtract.y, pydat.y, rtol=1.0e-06)

    def test_hpca_getspec_phi_nobulkv_subtract(self):
        """Test of getspec without bulk velocity subtraction"""

        mms_part_getspec(instrument='hpca',species='hplus', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['phi'], units='eflux', subtract_bulk=False, center_measurement=True, suffix='_no_bulk_subtract')
        tplot(['mms1_hpca_hplus_phase_space_density_phi_no_bulk_subtract','hpca_phi_nobulk_subtract'],display=global_display,save_png='mms_hpca_getspec_phi_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_phi_no_bulk_subtract')
        # Test Y values (phi bins) 2d vs 1d issue
        #assert_allclose(self.hpca_phi_nobulk_subtract.v, pydat.v, rtol=1.0e-06)
        # Test Z values  Largest relative error about 1.2e-06
        assert_allclose(self.hpca_phi_nobulk_subtract.y, pydat.y, rtol=1.0e-05)

    def test_hpca_getspec_phi_bulkv_subtract(self):
        """Test of getspec with bulk velocity subtraction"""

        mms_part_getspec(instrument='hpca',subtract_bulk=True, species='hplus', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['phi'], units='eflux', center_measurement=True, suffix='_bulk_subtract')
        tplot(['hpca_phi_nobulk_subtract','mms1_hpca_hplus_phase_space_density_phi_bulk_subtract','hpca_phi_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_phi_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_phi_bulk_subtract')
        # Test Y values (phi bins) 2d vs 1d issue
        #assert_allclose(self.hpca_phi_bulk_subtract.v, pydat.v, rtol=1.0e-06)
        # Test Z values  largest relative error about 2.3e-06
        assert_allclose(self.hpca_phi_bulk_subtract.y, pydat.y, rtol=1.0e-05)

    def test_hpca_getspec_gyro_nobulkv_subtract(self):
        """Test of getspec without bulk velocity subtraction"""

        mms_part_getspec(instrument='hpca',species='hplus', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['gyro'], no_regrid=True, units='eflux', subtract_bulk=False, center_measurement=True, suffix='_no_bulk_subtract')
        tplot(['mms1_hpca_hplus_phase_space_density_gyro_no_bulk_subtract','hpca_gyro_nobulk_subtract'],display=global_display,save_png='mms_hpca_getspec_gyro_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_gyro_no_bulk_subtract')
        # Test Y values (gyro bins) 2d vs 1d issue
        #assert_allclose(self.hpca_gyro_nobulk_subtract.v, pydat.v, rtol=1.0e-05)
        # Test Z values
        assert_allclose(self.hpca_gyro_nobulk_subtract.y, pydat.y, rtol=1.0e-05)

    def test_hpca_getspec_gyro_bulkv_subtract(self):
        """Test of getspec with bulk velocity subtraction"""

        mms_part_getspec(instrument='hpca',subtract_bulk=True, species='hplus', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['gyro'], no_regrid=True, units='eflux', center_measurement=True, suffix='_bulk_subtract')
        tplot(['hpca_gyro_nobulk_subtract','mms1_hpca_hplus_phase_space_density_gyro_bulk_subtract','hpca_gyro_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_gyro_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_gyro_bulk_subtract')
        # Test Y values (gyro bins) 2d vs 1d issue
        #assert_allclose(self.hpca_gyro_bulk_subtract.v, pydat.v, rtol=1.0e-05)
        # Test Z values
        assert_allclose(self.hpca_gyro_bulk_subtract.y, pydat.y, rtol=1.0e-05)

    def test_hpca_getspec_pa_nobulkv_subtract(self):
        """Test of getspec without bulk velocity subtraction"""

        mms_part_getspec(instrument='hpca',species='hplus', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['pa'], no_regrid=True, units='eflux', subtract_bulk=False, center_measurement=True, suffix='_no_bulk_subtract')
        tplot(['mms1_hpca_hplus_phase_space_density_pa_no_bulk_subtract','hpca_pa_nobulk_subtract'],display=global_display,save_png='mms_hpca_getspec_pa_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_pa_no_bulk_subtract')
        # Test Y values (pa bins) 2d vs 1d issue
        #assert_allclose(self.hpca_pa_nobulk_subtract.v, pydat.v, rtol=1.0e-05)
        # Test Z values   largest relative error about .00036
        assert_allclose(self.hpca_pa_nobulk_subtract.y, pydat.y, rtol=1.0e-03)

    def test_hpca_getspec_pa_bulkv_subtract(self):
        """Test of getspec with bulk velocity subtraction"""

        mms_part_getspec(instrument='hpca',subtract_bulk=True, species='hplus', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['pa'], no_regrid=True, units='eflux', center_measurement=True, suffix='_bulk_subtract')
        tplot(['hpca_pa_nobulk_subtract','mms1_hpca_hplus_phase_space_density_pa_bulk_subtract','hpca_pa_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_pa_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_pa_bulk_subtract')
        # Test Y values (pa bins) 2d vs 1d issue
        #assert_allclose(self.hpca_pa_bulk_subtract.v, pydat.v, rtol=1.0e-05)
        # Test Z values
        assert_allclose(self.hpca_pa_bulk_subtract.y, pydat.y, rtol=1.0e-05)

    def test_hpca_getspec_theta_nobulkv_subtract(self):
        """Test of getspec without bulk velocity subtraction"""

        mms_part_getspec(instrument='hpca',species='hplus', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['theta'], units='eflux', subtract_bulk=False, center_measurement=True, suffix='_no_bulk_subtract')
        tplot(['mms1_hpca_hplus_phase_space_density_theta_no_bulk_subtract','hpca_theta_nobulk_subtract'],display=global_display,save_png='mms_hpca_getspec_theta_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_theta_no_bulk_subtract')
        # Test Y values (theta bins)
        #assert_allclose(self.hpca_theta_nobulk_subtract.v, pydat.v, rtol=1.0e-05)
        # Test Z values
        assert_allclose(self.hpca_theta_nobulk_subtract.y, pydat.y, rtol=1.0e-05)

    def test_hpca_getspec_theta_bulkv_subtract(self):
        """Test of getspec with bulk velocity subtraction"""

        mms_part_getspec(instrument='hpca',subtract_bulk=True, species='hplus', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['theta'], units='eflux', center_measurement=True, suffix='_bulk_subtract')
        tplot(['hpca_theta_nobulk_subtract','mms1_hpca_hplus_phase_space_density_theta_bulk_subtract','hpca_theta_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_theta_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_theta_bulk_subtract')
        # Test Y values (theta bins) 2d vs 1d issue
        #assert_allclose(self.hpca_theta_bulk_subtract.v, pydat.v, rtol=1.0e-05)
        # Test Z values   largest relative error about .00012
        assert_allclose(self.hpca_theta_bulk_subtract.y, pydat.y, rtol=2.0e-03)


    def test_hpca_getspec_moments_no_bulk_subtract(self):
        """Test of getspec with bulk velocity subtraction"""

        mms_part_getspec(instrument='hpca',species='i', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['moments'], units='eflux', subtract_bulk=False, center_measurement=True, suffix='_no_bulk_subtract')
        tplot(['mms1_hpca_hplus_phase_space_density_avgtemp_no_bulk_subtract','hpca_avgtemp_mom_no_bulk_subtract', 'hpca_avgtemp_mom_no_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_avgtemp_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_avgtemp_no_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_avgtemp_no_bulk_subtract')
        self.assertEqual(units,'eV')
        # max relative difference 6.6e-06
        assert_allclose(self.hpca_avgtemp_nobulk_subtract.y, pydat.y, rtol=7.0e-06)

        tplot(['mms1_hpca_hplus_phase_space_density_density_no_bulk_subtract','hpca_density_mom_no_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_density_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_density_no_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_density_no_bulk_subtract')
        self.assertEqual(units,'1/cm^3')
        # max relative difference 1.2e-05
        assert_allclose(self.hpca_density_nobulk_subtract.y, pydat.y, rtol=2.0e-05)

        tplot(['mms1_hpca_hplus_phase_space_density_eflux_no_bulk_subtract','hpca_eflux_mom_no_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_eflux_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_eflux_no_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_eflux_no_bulk_subtract')
        self.assertEqual(units,'eV/(cm^2-s)')
        # max relative difference .0004
        assert_allclose(self.hpca_eflux_nobulk_subtract.y, pydat.y, rtol=.0005)

        tplot(['mms1_hpca_hplus_phase_space_density_qflux_no_bulk_subtract','hpca_qflux_mom_no_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_qflux_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_qflux_no_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_qflux_no_bulk_subtract')
        self.assertEqual(units,'eV/(cm^2-s)')
        # max relative difference .0004
        assert_allclose(self.hpca_qflux_nobulk_subtract.y, pydat.y, rtol=.0005)

        tplot(['mms1_hpca_hplus_phase_space_density_flux_no_bulk_subtract','hpca_flux_mom_no_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_flux_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_flux_no_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_flux_no_bulk_subtract')
        self.assertEqual(units,'1/(cm^2-s)')
        # largest relative difference about .0005
        assert_allclose(self.hpca_flux_nobulk_subtract.y, pydat.y, rtol=6.0e-04)

        tplot(['mms1_hpca_hplus_phase_space_density_mftens_no_bulk_subtract','hpca_mftens_mom_no_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_mftens_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_mftens_no_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_mftens_no_bulk_subtract')
        self.assertEqual(units,'eV/cm^3')
        # largest relative difference about .0008
        assert_allclose(self.hpca_mftens_nobulk_subtract.y, pydat.y, rtol=.001)

        tplot(['mms1_hpca_hplus_phase_space_density_ptens_no_bulk_subtract','hpca_ptens_mom_no_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_ptens_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_ptens_no_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_ptens_no_bulk_subtract')
        self.assertEqual(units,'eV/cm^3')
        # largest relative difference about 0.0023
        assert_allclose(self.hpca_ptens_nobulk_subtract.y, pydat.y, rtol=.003)

        tplot(['mms1_hpca_hplus_phase_space_density_velocity_no_bulk_subtract','hpca_velocity_mom_no_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_velocity_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_velocity_no_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_velocity_no_bulk_subtract')
        self.assertEqual(units,'km/s')
        # max relative difference .0005
        assert_allclose(self.hpca_velocity_nobulk_subtract.y, pydat.y, rtol=.0006)

        tplot(['mms1_hpca_hplus_phase_space_density_vthermal_no_bulk_subtract','hpca_vthermal_mom_no_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_vthermal_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_vthermal_no_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_vthermal_no_bulk_subtract')
        self.assertEqual(units,'km/s')
        # max relative difference 3.2e-06
        assert_allclose(self.hpca_vthermal_nobulk_subtract.y, pydat.y, rtol=4.0e-06)

    def test_hpca_getspec_moments_bulkv_subtract(self):
        """Test of getspec with bulk velocity subtraction"""

        mms_part_getspec(instrument='hpca',species='i', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['moments'], units='eflux', subtract_bulk=True, center_measurement=True, suffix='_bulk_subtract')
        tplot(['mms1_hpca_hplus_phase_space_density_avgtemp_bulk_subtract','hpca_avgtemp_mom_bulk_subtract', 'hpca_avgtemp_mom_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_avgtemp_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_avgtemp_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_avgtemp_bulk_subtract')
        self.assertEqual(units,'eV')
        # max relative difference 6.6e-06
        assert_allclose(self.hpca_avgtemp_bulk_subtract.y, pydat.y, rtol=7.0e-06)

        tplot(['mms1_hpca_hplus_phase_space_density_density_bulk_subtract','hpca_density_mom_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_density_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_density_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_density_bulk_subtract')
        self.assertEqual(units,'1/cm^3')
        # max relative difference 1.2e-05
        assert_allclose(self.hpca_density_bulk_subtract.y, pydat.y, rtol=2.0e-05)

        tplot(['mms1_hpca_hplus_phase_space_density_eflux_bulk_subtract','hpca_eflux_mom_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_eflux_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_eflux_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_eflux_bulk_subtract')
        self.assertEqual(units,'eV/(cm^2-s)')
        # max relative difference .0004
        assert_allclose(self.hpca_eflux_bulk_subtract.y, pydat.y, rtol=.0005)

        tplot(['mms1_hpca_hplus_phase_space_density_qflux_bulk_subtract','hpca_qflux_mom_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_qflux_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_qflux_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_qflux_bulk_subtract')
        self.assertEqual(units,'eV/(cm^2-s)')
        # max relative difference .0004
        assert_allclose(self.hpca_qflux_bulk_subtract.y, pydat.y, rtol=.0005)

        tplot(['mms1_hpca_hplus_phase_space_density_flux_bulk_subtract','hpca_flux_mom_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_flux_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_flux_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_flux_bulk_subtract')
        self.assertEqual(units,'1/(cm^2-s)')
        # largest relative difference about .0005
        assert_allclose(self.hpca_flux_bulk_subtract.y, pydat.y, rtol=6.0e-04)

        tplot(['mms1_hpca_hplus_phase_space_density_mftens_bulk_subtract','hpca_mftens_mom_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_mftens_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_mftens_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_mftens_bulk_subtract')
        self.assertEqual(units,'eV/cm^3')
        # largest relative difference about .0008
        assert_allclose(self.hpca_mftens_bulk_subtract.y, pydat.y, rtol=.001)

        tplot(['mms1_hpca_hplus_phase_space_density_ptens_bulk_subtract','hpca_ptens_mom_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_ptens_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_ptens_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_ptens_bulk_subtract')
        self.assertEqual(units,'eV/cm^3')
        # largest relative difference about 0.0023
        assert_allclose(self.hpca_ptens_bulk_subtract.y, pydat.y, rtol=.003)

        tplot(['mms1_hpca_hplus_phase_space_density_velocity_bulk_subtract','hpca_velocity_mom_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_velocity_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_velocity_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_velocity_bulk_subtract')
        self.assertEqual(units,'km/s')
        # max relative difference .0005
        assert_allclose(self.hpca_velocity_bulk_subtract.y, pydat.y, rtol=.0006)

        tplot(['mms1_hpca_hplus_phase_space_density_vthermal_bulk_subtract','hpca_vthermal_mom_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_vthermal_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_vthermal_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_vthermal_bulk_subtract')
        self.assertEqual(units,'km/s')
        # max relative difference 3.2e-06
        assert_allclose(self.hpca_vthermal_bulk_subtract.y, pydat.y, rtol=4.0e-06)

    def test_hpca_getspec_fac_moments_nobulkv_subtract(self):
        """Test of getspec without bulk velocity subtraction"""

        mms_part_getspec(instrument='hpca',species='i', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['fac_moments'], no_regrid=True, units='eflux', subtract_bulk=False, center_measurement=True, suffix='_no_bulk_subtract')
        tplot(['mms1_hpca_hplus_phase_space_density_fac_avgtemp_no_bulk_subtract','hpca_avgtemp_mom_mag_nobulk_subtract', 'hpca_avgtemp_mom_nobulk_subtract'],display=global_display,save_png='mms_hpca_getspec_avgtemp_mag_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_fac_avgtemp_no_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_fac_avgtemp_no_bulk_subtract')
        self.assertEqual(units,'eV')
        assert_allclose(self.hpca_avgtemp_mag_nobulk_subtract.y, pydat.y, rtol=1.0e-06)

        tplot(['mms1_hpca_hplus_phase_space_density_fac_density_no_bulk_subtract','hpca_density_mom_mag_nobulk_subtract'],display=global_display,save_png='mms_hpca_getspec_density_mag_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_fac_density_no_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_fac_density_no_bulk_subtract')
        self.assertEqual(units,'1/cm^3')
        # max relative difference 1.2e-06
        assert_allclose(self.hpca_density_mag_nobulk_subtract.y, pydat.y, rtol=2.0e-06)

        tplot(['mms1_hpca_hplus_phase_space_density_fac_eflux_no_bulk_subtract','hpca_eflux_mom_mag_nobulk_subtract'],display=global_display,save_png='mms_hpca_getspec_eflux_mag_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_fac_eflux_no_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_fac_eflux_no_bulk_subtract')
        self.assertEqual(units,'eV/(cm^2-s)')
        # max relative difference .0004
        assert_allclose(self.hpca_eflux_mag_nobulk_subtract.y, pydat.y, rtol=.0005)

        tplot(['mms1_hpca_hplus_phase_space_density_fac_qflux_no_bulk_subtract','hpca_qflux_mom_mag_nobulk_subtract'],display=global_display,save_png='mms_hpca_getspec_qflux_mag_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_fac_qflux_no_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_fac_qflux_no_bulk_subtract')
        self.assertEqual(units,'eV/(cm^2-s)')
        # max relative difference .0004
        assert_allclose(self.hpca_qflux_mag_nobulk_subtract.y, pydat.y, rtol=.0005)

        tplot(['mms1_hpca_hplus_phase_space_density_fac_flux_no_bulk_subtract','hpca_flux_mom_mag_nobulk_subtract'],display=global_display,save_png='mms_hpca_getspec_flux_mag_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_fac_flux_no_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_fac_flux_no_bulk_subtract')
        self.assertEqual(units,'1/(cm^2-s)')
        # largest relative difference about .0005
        assert_allclose(self.hpca_flux_mag_nobulk_subtract.y, pydat.y, rtol=6.0e-04)

        tplot(['mms1_hpca_hplus_phase_space_density_fac_mftens_no_bulk_subtract','hpca_mftens_mom_mag_nobulk_subtract'],display=global_display,save_png='mms_hpca_getspec_mftens_mag_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_fac_mftens_no_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_fac_mftens_no_bulk_subtract')
        self.assertEqual(units,'eV/cm^3')
        # largest relative difference about .0005
        assert_allclose(self.hpca_mftens_mag_nobulk_subtract.y, pydat.y, rtol=6.0e-04)

        tplot(['mms1_hpca_hplus_phase_space_density_fac_ptens_no_bulk_subtract','hpca_ptens_mom_mag_nobulk_subtract'],display=global_display,save_png='mms_hpca_getspec_ptens_mag_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_fac_ptens_no_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_fac_ptens_no_bulk_subtract')
        self.assertEqual(units,'eV/cm^3')
        # largest relative difference about 0.0009
        assert_allclose(self.hpca_ptens_mag_nobulk_subtract.y, pydat.y, rtol=.001)

        tplot(['mms1_hpca_hplus_phase_space_density_fac_velocity_no_bulk_subtract','hpca_velocity_mom_mag_nobulk_subtract'],display=global_display,save_png='mms_hpca_getspec_velocity_mag_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_fac_velocity_no_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_fac_velocity_no_bulk_subtract')
        self.assertEqual(units,'km/s')
        # max relative difference .0005
        assert_allclose(self.hpca_velocity_mag_nobulk_subtract.y, pydat.y, rtol=.0006)

        tplot(['mms1_hpca_hplus_phase_space_density_fac_vthermal_no_bulk_subtract','hpca_vthermal_mom_mag_nobulk_subtract'],display=global_display,save_png='mms_hpca_getspec_vthermal_mag_nobulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_fac_vthermal_no_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_fac_vthermal_no_bulk_subtract')
        self.assertEqual(units,'km/s')
        assert_allclose(self.hpca_vthermal_mag_nobulk_subtract.y, pydat.y, rtol=1.0e-06)


    def test_hpca_getspec_fac_moments_bulkv_subtract(self):
        """Test of getspec with bulk velocity subtraction"""

        mms_part_getspec(instrument='hpca',species='i', data_rate='brst',trange=['2015-11-19/08:34:41', '2015-11-19/08:35:53'],
                        output=['fac_moments'], no_regrid=True, units='eflux', subtract_bulk=True, center_measurement=True, suffix='_bulk_subtract')
        tplot(['mms1_hpca_hplus_phase_space_density_fac_avgtemp_bulk_subtract','hpca_avgtemp_mom_mag_bulk_subtract', 'hpca_avgtemp_mom_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_avgtemp_mag_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_fac_avgtemp_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_fac_avgtemp_bulk_subtract')
        self.assertEqual(units,'eV')
        # max relative difference 6.6e-06
        assert_allclose(self.hpca_avgtemp_mag_bulk_subtract.y, pydat.y, rtol=7.0e-06)

        tplot(['mms1_hpca_hplus_phase_space_density_fac_density_bulk_subtract','hpca_density_mom_mag_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_density_mag_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_fac_density_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_fac_density_bulk_subtract')
        self.assertEqual(units,'1/cm^3')
        # max relative difference 1.2e-05
        assert_allclose(self.hpca_density_mag_bulk_subtract.y, pydat.y, rtol=2.0e-05)

        tplot(['mms1_hpca_hplus_phase_space_density_fac_eflux_bulk_subtract','hpca_eflux_mom_mag_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_eflux_mag_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_fac_eflux_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_fac_eflux_bulk_subtract')
        self.assertEqual(units,'eV/(cm^2-s)')
        # max relative difference .0004
        assert_allclose(self.hpca_eflux_mag_bulk_subtract.y, pydat.y, rtol=.0005)

        tplot(['mms1_hpca_hplus_phase_space_density_fac_qflux_bulk_subtract','hpca_qflux_mom_mag_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_qflux_mag_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_fac_qflux_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_fac_qflux_bulk_subtract')
        self.assertEqual(units,'eV/(cm^2-s)')
        # max relative difference .0004
        assert_allclose(self.hpca_qflux_mag_bulk_subtract.y, pydat.y, rtol=.0005)

        tplot(['mms1_hpca_hplus_phase_space_density_fac_flux_bulk_subtract','hpca_flux_mom_mag_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_flux_mag_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_fac_flux_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_fac_flux_bulk_subtract')
        self.assertEqual(units,'1/(cm^2-s)')
        # largest relative difference about .0005
        assert_allclose(self.hpca_flux_mag_bulk_subtract.y, pydat.y, rtol=6.0e-04)

        tplot(['mms1_hpca_hplus_phase_space_density_fac_mftens_bulk_subtract','hpca_mftens_mom_mag_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_mftens_mag_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_fac_mftens_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_fac_mftens_bulk_subtract')
        self.assertEqual(units,'eV/cm^3')
        # largest relative difference about .0008
        assert_allclose(self.hpca_mftens_mag_bulk_subtract.y, pydat.y, rtol=.001)

        tplot(['mms1_hpca_hplus_phase_space_density_fac_ptens_bulk_subtract','hpca_ptens_mom_mag_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_ptens_mag_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_fac_ptens_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_fac_ptens_bulk_subtract')
        self.assertEqual(units,'eV/cm^3')
        # largest relative difference about 0.0023
        assert_allclose(self.hpca_ptens_mag_bulk_subtract.y, pydat.y, rtol=.003)

        tplot(['mms1_hpca_hplus_phase_space_density_fac_velocity_bulk_subtract','hpca_velocity_mom_mag_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_velocity_mag_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_fac_velocity_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_fac_velocity_bulk_subtract')
        self.assertEqual(units,'km/s')
        # max relative difference .0005
        assert_allclose(self.hpca_velocity_mag_bulk_subtract.y, pydat.y, rtol=.0006)

        tplot(['mms1_hpca_hplus_phase_space_density_fac_vthermal_bulk_subtract','hpca_vthermal_mom_mag_bulk_subtract'],display=global_display,save_png='mms_hpca_getspec_vthermal_mag_bulk_subtract.png')
        pydat=get_data('mms1_hpca_hplus_phase_space_density_fac_vthermal_bulk_subtract')
        units=get_units('mms1_hpca_hplus_phase_space_density_fac_vthermal_bulk_subtract')
        self.assertEqual(units,'km/s')
        # max relative difference 3.2e-06
        assert_allclose(self.hpca_vthermal_mag_bulk_subtract.y, pydat.y, rtol=4.0e-06)


if __name__ == '__main__':
    unittest.main()

