;+ ; NAME: ; rbsp_efw_make_l2_spinfit (procedure) ; ; PURPOSE: ; Generate level-2 EFW spin-fit waveform data. The axial boom is ignored as of ; 3/27/13. ; ; CATEGORIES: ; ; CALLING SEQUENCE: ; rbsp_efw_make_l2_spinfit, sc, date, folder = folder ; ; ARGUMENTS: ; sc: IN, REQUIRED ; 'a' or 'b' ; date: IN, REQUIRED ; A date string in format like '2013-02-13' ; ; KEYWORDS: ; folder: IN, OPTIONAL ; Default is something like ; !rbsp_efw.local_data_dir/rbspa/l2/spinfit/2012/ ; ; COMMON BLOCKS: ; ; EXAMPLES: ; ; SEE ALSO: ; ; HISTORY: ; 2013-03-19: Created by Jianbao Tao (JBT), SSL, UC Berkley. ; 2013-04-24: modified by AWB to throw global flag if any of the antennas ; are saturated or if the sc is in eclipse. ; ; VERSION: ; $LastChangedBy: kersten $ ; $LastChangedDate: 2013-04-26 09:25:50 -0700 (Fri, 26 Apr 2013) $ ; $LastChangedRevision: 12203 $ ; $URL: svn+ssh://thmsvn@ambrosia.ssl.berkeley.edu/repos/ssl_general/tags/tdas_8_00/missions/rbsp/efw/l1_to_l2/rbsp_efw_make_l2_spinfit.pro $ ; ;- pro rbsp_efw_make_l2_spinfit, sc, date, folder = folder, $ magExtra = magExtra, version = version, save_flags = save_flags, $ no_spice_load = no_spice_load, no_cdf = no_cdf compile_opt idl2 rbsp_efw_init if n_elements(version) eq 0 then version = 1 vstr = string(version, format='(I02)') ;------------ Set up paths. BEGIN. ---------------------------- ; if ~keyword_set(folder) then folder = '/Users/jianbao/rbsp/idlstuff/cdf/' year = strmid(date, 0, 4) if ~keyword_set(folder) then folder = !rbsp_efw.local_data_dir + $ 'rbsp' + strlowcase(sc[0]) + path_sep() + $ 'l2' + path_sep() + $ 'spinfit' + path_sep() + $ year + path_sep() ; make sure we have the trailing slash on folder if strmid(folder,strlen(folder)-1,1) ne path_sep() then folder=folder+path_sep() if ~keyword_set(no_cdf) then file_mkdir, folder rbspx='rbsp' + strlowcase(sc[0]) rbx = rbspx + '_' ; Grab the skeleton file. skeleton=rbspx+'/l2/e-spinfit-mgse/0000/'+ $ rbspx+'_efw-l2_e-spinfit-mgse_00000000_v'+vstr+'.cdf' skeletonFile=file_retrieve(skeleton,_extra=!rbsp_efw) ; make sure we have the skeleton CDF skeletonFile=file_search(skeletonFile,count=found) ; looking for single file, so count will return 0 or 1 if ~found then begin dprint,'Could not find e-spinfit-mgse v'+vstr+' skeleton CDF, returning.' return endif ; fix single element source file array skeletonFile=skeletonFile[0] ;------------ Set up paths. END. ---------------------------- timespan, date rbsp_load_state, probe = sc, no_eclipse = no_eclipse, $ no_spice_load = no_spice_load ; tplot, rbx + 'spinper' ; stop errorNumber = 0L catch, errorNumber if (errorNumber ne 0L) then begin catch, /cancel dprint, !error_state.msg dprint, 'Error occurred. Exit processing.' return endif ; Get l1 data. rbsp_load_efw_waveform, probe = sc, datatype = 'esvy', coord = 'uvw' tvar = rbx + 'efw_esvy' if ~thm_check_tvar(tvar) then begin dprint, tvar, ' is not available. Exit processing.' return endif ;*********************************** ;*********************************** ;*****TEMPORARY...LOAD SINGLE-ENDED MEASUREMENTS TO CHECK FOR SATURATION rbsp_load_efw_waveform,probe=sc,type='calibrated',datatype='vsvy' get_data,rbx+'efw_vsvy',data=vsvy if ~is_struct(vsvy) then begin dprint,rbx+'efw_vsvy unavailable, returning.' return endif ;*********************************** ;*********************************** ;*********************************** ;rbsp_load_emfisis_quicklook, probe = sc, datatype = 'uvw', $ ; _extra = magExtra rbsp_load_emfisis,probe=sc,/quicklook,/remove_spikes tvar = rbx+'emfisis_quicklook_Mag' if ~thm_check_tvar(tvar) then begin dprint, tvar, ' is not available. Exit processing.' return endif store_data,tvar,newname=rbx+'mag_uvw' ; Get total MGSE rbsp_spinfit, rbx + 'efw_esvy', plane_dim = 0, /sigma, /npoints ; V12 rbsp_cotrans, rbx + 'efw_esvy_spinfit', rbx + 'sfit12_mgse', /dsc2mgse ; Scale y and z components of total MGSE get_data, rbx + 'sfit12_mgse', data = d d.y[*, 1] *= 1.05d d.y[*, 2] *= 1.05d store_data, rbx + 'sfit12_mgse', data = d ; tplot, rbx + ['sfit12_mgse', 'sfit12_mgse2'] ; Get spin-fit B in MGSE rbsp_decimate, rbx + 'mag_uvw', upper = 2 rbsp_spinfit, rbx + 'mag_uvw', plane_dim = 0 rbsp_cotrans, rbx + 'mag_uvw_spinfit', rbx + 'mag_mgse', /dsc2mgse ; Get velocity in MGSE ; .r rbsp_cotrans rbsp_cotrans, rbx + 'vel_gse', rbx + 'vel_mgse', /gse2mgse ; Get VxB ;-- BxV, need to reverse sign later tcrossp, rbx + 'mag_mgse', rbx + 'vel_mgse', newname = rbx + 'vxb_mgse', $ /diff_tsize_ok ;-- Reverse and account for units so that end results in mV/m get_data, rbx + 'vxb_mgse', data = d d.y *= -1d-3 store_data, rbx + 'vxb_mgse', data = d ; Make sure vxb and sfit12 share the same time tags. get_data, rbx + 'vxb_mgse', data = d2 get_data, rbx + 'sfit12_mgse', data = d y = d.y y[*,0] = interp(d2.y[*,0], d2.x, d.x, /ignore_nan) y[*,1] = interp(d2.y[*,1], d2.x, d.x, /ignore_nan) y[*,2] = interp(d2.y[*,2], d2.x, d.x, /ignore_nan) store_data, rbx + 'vxb_mgse', data = {x:d.x, y:y} ; stop ; split_vec, rbx + 'vxb_mgse' ; options, rbx + 'vxb_mgse_x', colors = [6] ; options, rbx + 'vxb_mgse_y', colors = [6] ; options, rbx + 'vxb_mgse_z', colors = [6] ; ; split_vec, rbx + 'sfit12_mgse' ; options, rbx + 'sfit12_mgse_x', colors = [0] ; options, rbx + 'sfit12_mgse_y', colors = [0] ; options, rbx + 'sfit12_mgse_z', colors = [0] ; tplot, rbx + ['sfit12_mgse_y', 'sfit12_mgse_z'] ; ; store_data, rbx + 'mgse_y', data = rbx + ['sfit12_mgse_y', 'vxb_mgse_y'] ; store_data, rbx + 'mgse_z', data = rbx + ['sfit12_mgse_z', 'vxb_mgse_z'] ; tplot, rbx + ['mgse_y', 'mgse_z'] ; get_data, rbx + 'vxb_mgse', data = d3 ; tplot, rbx + ['vxb_mgse', 'sfit12_mgse'] ; Subtract VxB from sfit12 get_data, rbx + 'sfit12_mgse', data = d get_data, rbx + 'vxb_mgse', data = d2 d.y[*, 0] = !values.f_nan d.y[*, 1] -= d2.y[*,1] d.y[*, 2] -= d2.y[*,2] store_data, rbx + 'sfit12_mgse', data = d ; tplot, rbx + 'sfit12_mgse' options, rbx + 'sfit12_mgse', labels = ['', 'y MGSE', 'z MGSE'], $ ysubtitle = '[mV/m]' options, rbx + 'mag_mgse', ysubtitle = '[nT]' if keyword_set(no_cdf) then return ; Make quality flags ;-- epoch epoch_flag_times, date, 5, epochvals, timevals nt = n_elements(epochvals) flag_arr = intarr(nt, 20) flag_arr[*,*] = -1 ; Look-up table for quality flags ; 0: global_flag ; 1: eclipse ; 2: maneuver ; 3: efw_sweep ; 4: efw_deploy ; 5: v1_saturation ; 6: v2_saturation ; 7: v3_saturation ; 8: v4_saturation ; 9: v5_saturation ; 10: v6_saturation ; 11: Espb_magnitude ; 12: Eparallel_magnitude ; 13: magnetic_wake ; 14: undefined ; 15: undefined ; 16: undefined ; 17: undefined ; 18: undefined ; 19: undefined flag_arr[*, 14:19] = -2 ; not relevant ; The following flags should be marked: ; 1: eclipse ; 11: Espb_magnitude ; 12: Eparallel_magnitude ;-- flag eclipse iflag = 1 flag_arr[*,iflag] = 0 ; it seems like no_eclipse is always equal to zero. ; we should see no_eclipse==1 if we don't have an eclipse. ; we set the flag when no_eclipse=0, so this causes an ; undefined structure error when we don't actually have ; an eclipse. at least that's what I gather. -KK ; ; as a workaround, we're going to make sure usta and uend are ; both structures before we proceed. -KK ;if no_eclipse eq 0 then begin get_data, rbx + 'umbra_sta', data = usta get_data, rbx + 'umbra_end', data = uend if is_struct(usta) and is_struct(uend) then begin dprint,'FLAGGING ECLIPSE' n_umbra = n_elements(usta.x) for i = 0, n_umbra - 1 do begin tsta = usta.x[i] tend = uend.x[i] ind = where(timevals ge tsta and timevals le tend, nind) if nind gt 0 then flag_arr[ind, 1] = 100 endfor endif ;*********************************** ;*********************************** ;********TEMPORARY******************* ;Throw global flag during eclipse times goo = where(flag_arr[*,1] eq 100) if goo[0] ne -1 then flag_arr[goo,0] = 1 ;*********************************** ;*********************************** ;**********TEMPORARY********************* ;--flag single-ended saturation maxvolts = 195. tmp_flag = replicate(0,n_elements(vsvy.x),6) offset = 5 ;position in flag_arr of "v1_saturation" for vv=0,5 do begin vbad = where(abs(vsvy.y[*,vv]) ge maxvolts) if vbad[0] ne -1 then tmp_flag[vbad,vv] = 1 ;set good values vgood = where(abs(vsvy.y[*,vv]) lt maxvolts) if vgood[0] ne -1 then tmp_flag[vgood,vv] = 0 ;Interpolate the bad data values onto the pre-defined flag value times flag_arr[*,vv+offset] = ceil(interpol(tmp_flag[*,vv],vsvy.x,timevals)) endfor ;*********************************** ;*********************************** ;****TEMPORARY CODE****** ;Throw the global flag if any of the single-ended flags are thrown. goo = where((flag_arr[*,5] eq 1) or (flag_arr[*,6] eq 1) or (flag_arr[*,7] eq 1) or (flag_arr[*,8] eq 1)) if goo[0] ne -1 then flag_arr[goo,0] = 1 ;*********************************** ;*********************************** ;************************ ;****************************************** ;-- flag Espb_magnitude iflag = 11 flag_arr[*,iflag] = 0 get_data, rbx + 'sfit12_mgse', data = d Espb = sqrt(d.y[*,0]^2 + d.y[*,1]^2) Espb = interp(Espb, d.x, timevals, /ignore_nan) ind = where(Espb gt 500, nind) if nind gt 0 then flag_arr[ind,iflag] = iflag * 100 ;-- flag Eparallel_magnitude iflag = 12 flag_arr[*,iflag] = 0 get_data, rbx + 'sfit12_mgse', data = edat get_data, rbx + 'mag_mgse', data = bdat ex = edat.y[*,0] ey = edat.y[*,1] ez = ex * 0d bx = interp(bdat.y[*,0], bdat.x, edat.x, /ignore_nan) by = interp(bdat.y[*,1], bdat.x, edat.x, /ignore_nan) bz = interp(bdat.y[*,2], bdat.x, edat.x, /ignore_nan) btot = sqrt(bx^2 + by^2 + bz^2) Epara = (ex * bx + ey * by + ez * bz) / btot ind = where(abs(Epara) gt 500, nind) if nind gt 0 then flag_arr[ind,iflag] = iflag * 100 if keyword_set(save_flags) then $ store_data, rbx + 'efw_qual', data = {x:timevals, y:flag_arr} ;--------------------------------------------------------------------- ; Put sfit12_mgse into CDF. ; Make an empty CDF from the skeleton CDF. ; folder = '/Users/jianbao/rbsp/idlstuff/cdf/' year = strmid(date, 0, 4) mm = strmid(date, 5, 2) dd = strmid(date, 8, 2) datafile = folder + rbx + 'efw-l2_e-spinfit-mgse_' + year + mm + dd + $ '_v' + vstr + '.cdf' ; skeletonFile = sktfolder + 'rbsp' + strlowcase(sc[0]) + $ ; '_efw-l2_e-spinfit-mgse_00000000_v' + vstr + '.cdf' ; filematch = file_search(datafile, count = filecount) ; if filecount eq 0 then file_copy, skeletonFile, datafile file_copy, skeletonFile, datafile, /overwrite; Force to replace old file. ; Open CDF and get a CDF id. cdfid = cdf_open(datafile) ;*********************************** ;*********************************** ;*********************************** ;**************TEMPORARY ******************** ;Set all globally-flagged data to the ISTP fill_value badvs = where(flag_arr[*,0] eq 1) if badvs[0] ne -1 then begin get_data,rbx+'sfit12_mgse',data=dtmp newflags = ceil(interpol(flag_arr[*,0],timevals,dtmp.x)) goo = where(newflags eq 1) if goo[0] ne -1 then dtmp.y[goo,*] = -1.0E31 store_data,rbx+'sfit12_mgse',data=dtmp endif ;*********************************** ;*********************************** ;*********************************** ;************************************************* ;-------------------- spinfit -------------------------- ; time tvar = rbx + 'sfit12_mgse' cdfhandle = 'epoch' get_data, tvar, data = d, dlim = dl epoch = tplot_time_to_epoch(d.x, /epoch16) cdf_varput, cdfid, cdfhandle, epoch ; Write sfit12 fill_value = -1e31 ; tvar = rbx + 'sfit12_mgse' cdfhandle = 'e12_spinfit_mgse' get_data, tvar, data = d, dlim = dl d.y[*,0] = fill_value ; fill the axial component cdf_varput, cdfid, cdfhandle, transpose(d.y) ; Write vxb tvar = rbx + 'vxb_mgse' cdfhandle = 'vxb_spinfit_mgse' get_data, tvar, data = d, dlim = dl cdf_varput, cdfid, cdfhandle, transpose(d.y) ; Write sigma tvar = rbx + 'efw_esvy_spinfit_e12_sig' cdfhandle = 'sigma12_spinfit_mgse' get_data, tvar, data = d, dlim = dl cdf_varput, cdfid, cdfhandle, d.y ; Write npoints tvar = rbx + 'efw_esvy_spinfit_e12_npoints' cdfhandle = 'npoints12_spinfit_mgse' get_data, tvar, data = d, dlim = dl cdf_varput, cdfid, cdfhandle, d.y ;-------------------- efw_qual -------------------------- cdf_varput, cdfid, 'epoch_qual',epochvals cdf_varput, cdfid, 'e_spinfit_mgse_efw_qual', transpose(flag_arr) ;-------------------- BEB/DFB config -------------------------- ; epoch_hsk tvar = rbx + 'efw_esvy_ccsds_data_BEB_config' get_data, tvar, data = d, dlim = dl epoch_hsk = d.x cdfhandle = 'epoch_hsk' cdf_varput, cdfid, cdfhandle, epoch_hsk ; BEB_config cdfhandle = 'e_spinfit_mgse_BEB_config' cdf_varput, cdfid, cdfhandle, d.y ; DFB_config tvar = rbx + 'efw_esvy_ccsds_data_DFB_config' cdfhandle = 'e_spinfit_mgse_DFB_config' get_data, tvar, data = d, dlim = dl cdf_varput, cdfid, cdfhandle, d.y cdf_close, cdfid end