;+ ; ;Procedure: thm_l2gen_esa ; ;Purpose: Creates Level 2 data CDF file of THEMIS ESA data. ; File includes ESA (Electrostatic Analyzer) ion and electron spectrograms and ; density, velocity and temperature moments. ; File includes all metadata (global and variable attributes) necessary ; to be ISTP compliant and QSAS compatible. ; Program is intended primarily for generation of 1 day files. ; ;Inputs: probe: A string indicating probe. ; Valid names are : 'a', 'b', 'c', 'd' and 'e'. ; date: A string indicating the start time. ; Format: YYYY-MM-DD/hh:mm:ss ; dur: Number indicating the duration. ; days: A keyword that sets the units for dur to be day (default). ; hours: A keyword that sets the units for dur to be hours. ; minutes: A keyword that sets the units for dur to be minutes. ; noload: A keyword that prevents data from being loaded. ; Note: Data must already be loaded. ; mastercdf:A string indicating the location of the mastercdf to be use. ; This master contains all the L2 metadata. ; ;Outputs: File is created in local directory. ; Filename format: thx_l2_esa_YYYYMMDD_v01.cdf ; ;Example: thm_l2gen_esa,probe='a',date='2007-03-23',dur=1,/noload ; ;Written by: Matt Davis ; ;- pro thm_l2gen_esa, probe=probe, date=date, dur=dur, days=days, hours=hours, minutes=minutes, noload=noload,mastercdf=mastercdf thm_init ;take a little look at the inputs ;----------------------------------------------------------------------------- if keyword_set(probe) ne 1 then begin print,'You did not select a probe. Please select probe.' print,"i.e., thm_l2gen_esa,probe='a'" return endif probe_list=['a','b','c','d','e'] index=where(probe eq probe_list) if index eq -1 then begin print, 'You entered invalid probe ID : ',strtrim(probe,2) print, "Valid names are : 'a', 'b', 'c', 'd' and 'e'" print, "i.e., thm_l2gen_esa,probe='a'" return endif case 1 of keyword_set(hours) : dur=dur/24. keyword_set(minutes) : dur=dur/1440. else : dur=dur endcase sc='th'+probe if not keyword_set(mastercdf) then mastercdf='mastercdfs/'+sc+'_l2_esa_00000000_v01.cdf' ;load up all the data we'll be playing with ;----------------------------------------------------------------------------- timespan,date,dur if (keyword_set(noload) eq 0) then begin del_data,'*' ; give ourselves a clean slate ;load ESA data thm_load_esa_pkt,probe=probe ;ions get_dat=strjoin(sc+'_peif') name1=strjoin(sc+'_peif_en_eflux') get_en_spec,get_dat,units='eflux',retrace=1,name=name1,gap_time=1000.,t1=t1,t2=t2 name1=strjoin(sc+'_peif_density') get_2dt,'n_3d',get_dat,name=name1,gap_time=1000.,t1=t1,t2=t2,energy=[20.,21000.] name1=strjoin(sc+'_peif_velocity') get_2dt,'v_3d',get_dat,name=name1,gap_time=1000.,t1=t1,t2=t2,energy=[20.,21000.] name1=strjoin(sc+'_peif_pres') get_2dt,'p_3d',get_dat,name=name1,gap_time=1000.,t1=t1,t2=t2,energy=[20.,21000.] ;electrons get_dat=strjoin(sc+'_peef') name1=strjoin(sc+'_peef_en_eflux') get_en_spec,get_dat,units='eflux',retrace=1,name=name1,gap_time=1000.,t1=t1,t2=t2 name1=strjoin(sc+'_peef_density') get_2dt,'n_3d',get_dat,name=name1,gap_time=1000.,t1=t1,t2=t2,energy=[20.,21000.] name1=strjoin(sc+'_peef_velocity') get_2dt,'v_3d',get_dat,name=name1,gap_time=1000.,t1=t1,t2=t2,energy=[20.,21000.] name1=strjoin(sc+'_peef_pres') get_2dt,'p_3d',get_dat,name=name1,gap_time=1000.,t1=t1,t2=t2,energy=[20.,21000.] ;coordinate transform velocity from dsl to gse thm_load_state,probe=probe,/get_support copy_data,strjoin(sc+'_peif_velocity'),strjoin(sc+'_peif_velocity_dsl') copy_data,strjoin(sc+'_peef_velocity'),strjoin(sc+'_peef_velocity_dsl') thm_cotrans,strjoin(sc+'_peif_velocity'),in_suf='_dsl', out_suf='_gse', in_c='dsl', out_c='gse' thm_cotrans,strjoin(sc+'_peef_velocity'),in_suf='_dsl', out_suf='_gse', in_c='dsl', out_c='gse' thm_cotrans,strjoin(sc+'_peif_velocity'),in_suf='_gse', out_suf='_gsm', in_c='gse', out_c='gsm' thm_cotrans,strjoin(sc+'_peef_velocity'),in_suf='_gse', out_suf='_gsm', in_c='gse', out_c='gsm' ;diagonalize temperature common temp_esa_l2gen,on_peif catch,error_status if error_status eq -634 && on_peif eq 1 then goto,DIAGONALIZATION_ERROR_SKIP_PEIF if error_status eq -634 && on_peif eq 0 then goto,DIAGONALIZATION_ERROR_SKIP_PEEF on_peif=1 diag_p,strjoin(sc+'_peif_pres'),strjoin(sc+'_peif_density') copy_data,'T_diag',strjoin(sc+'_peif_t3') DIAGONALIZATION_ERROR_SKIP_PEIF: on_peif=0 diag_p,strjoin(sc+'_peef_pres'),strjoin(sc+'_peef_density') copy_data,'T_diag',strjoin(sc+'_peef_t3') DIAGONALIZATION_ERROR_SKIP_PEEF: endif ; data load ;read in master CDF file ;----------------------------------------------------------------------------- esa_l2_structure=cdf_load_vars(mastercdf,/all) ;find which tplot variables exist ;----------------------------------------------------------------------------- esa_allvars=[strjoin(sc+'_peif_en_eflux'), $ strjoin(sc+'_peef_en_eflux'), $ strjoin(sc+'_peif_density'), $ strjoin(sc+'_peef_density'), $ strjoin(sc+'_peif_velocity_dsl'), $ strjoin(sc+'_peef_velocity_dsl'), $ strjoin(sc+'_peif_velocity_gse'), $ strjoin(sc+'_peef_velocity_gse'), $ strjoin(sc+'_peif_velocity_gsm'), $ strjoin(sc+'_peef_velocity_gsm'), $ strjoin(sc+'_peif_t3'), $ strjoin(sc+'_peef_t3')] esa_vars=tnames(esa_allvars) ;strip out 'Nan' time records ;----------------------------------------------------------------------------- for i=0,n_elements(esa_vars)-1 do begin get_data,esa_vars(i),data=dd if (size(dd,/type) eq 8) then index=where(finite(dd.x) eq 1) else index=-1 if index(0) ne -1 then begin str_element,dd,'v',success=success if success eq 1 then store_data,esa_vars(i),data={x:dd.x(index),y:dd.y(index,*),v:dd.v(index,*)} $ else store_data,esa_vars(i),data={x:dd.x(index),y:dd.y(index,*)} endif endfor ;insert data into esa_l2_structure ;----------------------------------------------------------------------------- mastertags=esa_l2_structure.vars.name ;time tags ;--------- get_data,strjoin(sc+'_peif_en_eflux'),data=dd index=where(strjoin(sc+'_peif_time') eq mastertags) if size(dd,/type) eq 8 then esa_l2_structure.vars[index].dataptr=ptr_new(dd.x) get_data,strjoin(sc+'_peef_en_eflux'),data=dd index=where(strjoin(sc+'_peef_time') eq mastertags) if size(dd,/type) eq 8 then esa_l2_structure.vars[index].dataptr=ptr_new(dd.x) ;data (spectragram, density, velocity, temperature) ;-------------------------------------------------- for i=0,n_elements(esa_vars)-1 do begin get_data,esa_vars(i),data=dd index=where(esa_vars(i) eq mastertags) if size(dd,/type) eq 8 then esa_l2_structure.vars[index].dataptr=ptr_new(dd.y) endfor ;yaxis/energy levels ;------------------- get_data,strjoin(sc+'_peif_en_eflux'),data=di str_element,di,'v',success=success if success eq 1 then begin index=where(strjoin(sc+'_peif_en_eflux_yaxis') eq mastertags) esa_l2_structure.vars[index].dataptr=ptr_new(di.v) endif get_data,strjoin(sc+'_peef_en_eflux'),data=de str_element,de,'v',success=success if success eq 1 then begin index=where(strjoin(sc+'_peef_en_eflux_yaxis') eq mastertags) esa_l2_structure.vars[index].dataptr=ptr_new(de.v) endif ;range_epoch ;----------- range_epoch_vals=[time_string(date),time_string(time_double(date)+86400.*dur)] reval_strs=time_string(range_epoch_vals) year=strmid(reval_strs,0,4) month=strmid(reval_strs,5,2) day=strmid(reval_strs,8,2) hour=strmid(reval_strs,11,2) minu=strmid(reval_strs,14,2) sec=strmid(reval_strs,17,2) cdf_epoch,range_epoch_min,year(0),month(0),day(0),hour(0),minu(0),sec(0),/compute_epoch cdf_epoch,range_epoch_max,year(1),month(1),day(1),hour(1),minu(1),sec(1),/compute_epoch range_epoch_values=[range_epoch_min,range_epoch_max] index=where(strjoin('range_epoch') eq mastertags) esa_l2_structure.vars[index].dataptr=ptr_new(range_epoch_values) ; write esa_l2_structure to L2 CDF file ;----------------------------------------------------------------------------- filename=strjoin(sc+'_l2_esa_'+strmid(date,0,4)+strmid(date,5,2)+strmid(date,8,2)+'_v01.cdf') dummy=cdf_save_vars(esa_l2_structure,filename) print, 'L2 ESA CDF file written! Filename: ',filename ;clean up ;----------------------------------------------------------------------------- unused_ptrs = ptr_extract(esa_l2_structure) ptr_free,unused_ptrs end