;+ ; ;Procedure: thm_l2gen_fgm ; ;Purpose: Creates Level 2 data CDF file of THEMIS FGM data. ; File includes FGM (Fluxgate Magnetometer) engineering (FGE), low-res (FGL), ; high-res (FGH) and spin-res (FGS). ; 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_fgm_YYYYMMDD_v01.cdf ; ;Example: thm_l2gen_fgm,probe='a',date='2007-03-23',dur=1,/noload ; ;Written by: Matt Davis ; ;- pro thm_l2gen_fgm, 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_fgm,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_fgm,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_fgm_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 FGM data thm_load_state,probe=[probe],/get_support thm_load_fit,probe=[probe],lev=1,/get_support thm_cal_fit,/verbose thm_load_fgm,lev=1,probe=[probe],/get_support_data,type='raw', suffix='_raw' ;clip time for smaller test file types=['fge_raw','fgl_raw','fgh_raw','fit_b'] for i=0,n_elements(types)-1 do begin get_data,sc+'_'+types(i),data=d if size(d,/type) eq 8 then begin index=where(d.x gt time_double(date) and d.x lt (time_double(date)+dur*86400.)) if index(0) ne -1 then store_data,'tha_'+types(i),data={x:d.x(index),y:d.y(index,*)} else begin del_data,'tha_'+types(i) endelse endif endfor thm_cal_fgm,probe=[probe],datatype='fg?',in_suffix='_raw', out_suffix='_ssl' thm_cotrans,sc+'_fg?',in_suf='_ssl',out_suf='_dsl', in_c='ssl', out_c='dsl' thm_cotrans,sc+'_fg?',in_suf='_dsl',out_suf='_gse', in_c='dsl', out_c='gse' thm_cotrans,sc+'_fg?',in_suf='_gse',out_suf='_gsm', in_c='gse', out_c='gsm' copy_data,sc+'_fgs',sc+'_fgs_dsl' thm_cotrans,sc+'_fgs',in_suf='_dsl',out_suf='_gse', in_c='dsl', out_c='gse' thm_cotrans,sc+'_fgs',in_suf='_gse',out_suf='_gsm', in_c='gse', out_c='gsm' endif ; data load ;read in master CDF file ;----------------------------------------------------------------------------- fgm_l2_structure=cdf_load_vars(mastercdf,/all) ;find which tplot variables exist ;----------------------------------------------------------------------------- fgm_allvars=[strjoin(sc+'_fge_ssl'), $ strjoin(sc+'_fge_dsl'), $ strjoin(sc+'_fge_gse'), $ strjoin(sc+'_fge_gsm'), $ strjoin(sc+'_fgl_ssl'), $ strjoin(sc+'_fgl_dsl'), $ strjoin(sc+'_fgl_gse'), $ strjoin(sc+'_fgl_gsm'), $ strjoin(sc+'_fgh_ssl'), $ strjoin(sc+'_fgh_dsl'), $ strjoin(sc+'_fgh_gse'), $ strjoin(sc+'_fgh_gsm'), $ strjoin(sc+'_fgs_dsl'), $ strjoin(sc+'_fgs_gse'), $ strjoin(sc+'_fgs_gsm')] fgm_vars=tnames(fgm_allvars) ;strip out 'Nan' time records ;----------------------------------------------------------------------------- for i=0,n_elements(fgm_vars)-1 do begin get_data,fgm_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,fgm_vars(i),data={x:dd.x(index),y:dd.y(index,*),v:dd.v(index,*)} $ else store_data,fgm_vars(i),data={x:dd.x(index),y:dd.y(index,*)} endif endfor ;insert data into fgm_l2_structure ;----------------------------------------------------------------------------- mastertags=fgm_l2_structure.vars.name ;time tags ;--------- get_data,strjoin(sc+'_fge_ssl'),data=dd index=where(strjoin(sc+'_fge_time') eq mastertags) if size(dd,/type) eq 8 then fgm_l2_structure.vars[index].dataptr=ptr_new(dd.x) get_data,strjoin(sc+'_fgl_ssl'),data=dd index=where(strjoin(sc+'_fgl_time') eq mastertags) if size(dd,/type) eq 8 then fgm_l2_structure.vars[index].dataptr=ptr_new(dd.x) get_data,strjoin(sc+'_fgh_ssl'),data=dd index=where(strjoin(sc+'_fgh_time') eq mastertags) if size(dd,/type) eq 8 then fgm_l2_structure.vars[index].dataptr=ptr_new(dd.x) get_data,strjoin(sc+'_fgs_dsl'),data=dd index=where(strjoin(sc+'_fgs_time') eq mastertags) if size(dd,/type) eq 8 then fgm_l2_structure.vars[index].dataptr=ptr_new(dd.x) ;FGE,FGL,FGH & FGS data ;---------------------- for i=0,n_elements(fgm_vars)-1 do begin get_data,fgm_vars(i),data=dd index=where(fgm_vars(i) eq mastertags) if size(dd,/type) eq 8 then fgm_l2_structure.vars[index].dataptr=ptr_new(dd.y) endfor ;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) fgm_l2_structure.vars[index].dataptr=ptr_new(range_epoch_values) ; write fgm_l2_structure to L2 CDF file ;----------------------------------------------------------------------------- filename=strjoin(sc+'_l2_fgm_'+strmid(date,0,4)+strmid(date,5,2)+strmid(date,8,2)+'_v01.cdf') dummy=cdf_save_vars(fgm_l2_structure,filename) print, 'L2 FGM CDF file written! Filename: ',filename ;clean up ;----------------------------------------------------------------------------- unused_ptrs = ptr_extract(fgm_l2_structure) ptr_free,unused_ptrs end