;+
; Save the following data to CDF:
;   q_uvw2gse. Fixed for artificial wobble, Saved at 1 sec cadence.
;   r_mgse. In Re, 10 sec.
;   v_mgse. In km/s, 10 sec.
;   e_mgse. In mV/m, 10 sec, include Ex.
;   b_mgse. In nT, 10 sec.
;-

pro rbsp_efw_phasef_prepare_residue_removal_gen_file, time, probe=probe, filename=file, errmsg=errmsg

;---Check inputs.
    if n_elements(file) eq 0 then begin
        errmsg = handle_error('No output file ...')
        return
    endif

    if n_elements(probe) eq 0 then begin
        errmsg = handle_error('No input probe ...')
        return
    endif

    if n_elements(time) eq 0 then begin
        errmsg = handle_error('No input time ...')
        return
    endif


;---Constants and settings.
    secofday = 86400d
    deg = 180d/!dpi
    rad = !dpi/180
    errmsg = ''
    common_time_step = 10d      ; sec.
    xyz = constant('xyz')


    ; Derived settings.
    date = time[0]-(time[0] mod secofday)
    time_range = date+[0,secofday]
    long_time_range = time_range+[-1,1]*3600d
    prefix = 'rbsp'+probe+'_'
    common_times = make_bins(time_range+[1,0]*common_time_step, common_time_step)


;---Init file.
    out_dir = fgetpath(file)
    if file_test(out_dir,/directory) eq 0 then file_mkdir, out_dir
    data_file = file
    if file_test(data_file) eq 1 then file_delete, data_file  ; overwrite old files.
    cdf_id = cdf_create(data_file)
    ginfo = dictionary($
        'TITLE', 'RBSP EFW prepare quantities related to residue removal around perigee', $
        'TEXT', 'Generated by Sheng Tian at the University of Minnesota' )
    cdf_save_setting, ginfo, filename=cdf_id



;---q_uvw2gse.
    rbsp_read_q_uvw2gse, long_time_range, probe=probe


;---Common times.
    utname = 'time'
    data = common_times
    settings = dictionary($
        'fieldnam', 'time', $
        'unit', 'sec', $
        'var_type', 'support_data' )
    cdf_save_var, utname, value=data, filename=cdf_id, cdf_type='CDF_DOUBLE'
    cdf_save_setting, settings, var=utname, filename=cdf_id


;---r_mgse and v_mgse.
    rbsp_efw_phasef_read_r_mgse, time_range, probe=probe
    rbsp_efw_phasef_read_v_mgse, time_range, probe=probe
    vnames = prefix+['r_mgse','v_mgse']
    foreach vname, vnames do begin
        data = get_var_data(vname, in=time_range, times=orbit_times)
        data = sinterpol(data, orbit_times, common_times, /quadratic)
        store_data, vname, common_times, data
    endforeach

    vname = prefix+'r_mgse'
    data = get_var_data(vname)
    settings = dictionary($
        'display_type', 'vector', $
        'short_name', 'R', $
        'unit', 'Re', $
        'coord', 'MGSE', $
        'coord_labels', xyz, $
        'var_type', 'data', $
        'depend_0', utname )
    cdf_save_var, vname, value=data, filename=cdf_id
    cdf_save_setting, settings, var=vname, filename=cdf_id

    vname = prefix+'v_mgse'
    data = get_var_data(vname)
    settings = dictionary($
        'display_type', 'vector', $
        'short_name', 'V', $
        'unit', 'km/s', $
        'coord', 'MGSE', $
        'coord_labels', xyz, $
        'var_type', 'data', $
        'depend_0', utname )
    cdf_save_var, vname, value=data, filename=cdf_id
    cdf_save_setting, settings, var=vname, filename=cdf_id


;---b_mgse.
    rbsp_efw_phasef_read_b_mgse, long_time_range, probe=probe
    vname = prefix+'b_mgse'
    interp_time, vname, common_times
    data = get_var_data(vname)
    settings = dictionary($
        'display_type', 'vector', $
        'short_name', 'B', $
        'unit', 'nT', $
        'coord', 'MGSE', $
        'coord_labels', xyz, $
        'var_type', 'data', $
        'depend_0', utname )
    cdf_save_var, vname, value=data, filename=cdf_id
    cdf_save_setting, settings, var=vname, filename=cdf_id


;---e_mgse.
    rbsp_efw_read_e_mgse, long_time_range, probe=probe, /keep_spin_axis
    vname = prefix+'e_mgse'
    interp_time, vname, common_times
    data = get_var_data(vname)
    settings = dictionary($
        'display_type', 'vector', $
        'short_name', 'E', $
        'unit', 'mV/m', $
        'coord', 'MGSE', $
        'coord_labels', xyz, $
        'var_type', 'data', $
        'depend_0', utname )
    cdf_save_var, vname, value=data, filename=cdf_id
    cdf_save_setting, settings, var=vname, filename=cdf_id


    cdf_close, cdf_id

end


time = time_double('2012-12-31')
probe = 'a'
time = time_double('2018-09-27')
probe = 'b'

file = join_path([homedir(),'test.cdf'])
if file_test(file) then file_delete, file
rbsp_efw_phasef_prepare_residue_removal_gen_file, time, probe=probe, filename=file

stop

time_range = time_double(['2012-09-08','2012-09-10'])
;time_range = time_double(['2012-10-01','2015-10-01'])
;time_range = time_double(['2015-10-01','2019-06-01'])
probes = ['a','b']
secofday = constant('secofday')
days = make_bins(time_range, secofday)
foreach probe, probes do begin
    prefix = 'rbsp'+probe+'_'
    foreach day, days do begin
        time = day
        file = join_path([homedir(),'residue_removal_products','rbsp'+probe,time_string(time[0],tformat='YYYY'),$
            prefix+'efw_residue_removal_products_'+time_string(time,tformat='YYYY_MMDD')+'_v02.cdf'])
        rbsp_efw_phasef_prepare_residue_removal_gen_file, time, probe=probe, $
            filename=file, errmsg=errmsg
    endforeach
endforeach

end
