;+ ;Print out the structure information for a single event or ;a series of events in text format. ; ;pro sagetext,file,recnum,outputfile=outputfile ;- function getindexname,specname,filetype=filetype ;construct the index file name and read that file indexfile = specname p = strpos(strupcase(indexfile),'SAGE_I') p = strpos(indexfile,'_',p+6)+1 q = strpos(indexfile,'_',p) filetype = strmid(indexfile,p,q-p) l = q-p p = strpos(indexfile,filetype) while p ne -1 do begin q = strlen(indexfile) m = p+l indexfile = strmid(indexfile,0,p)+'INDEX'+strmid(indexfile,m,q-m) p = strpos(indexfile,filetype,m) endwhile return,indexfile end pro sagetext,file,recnum,outputfile=outputfile if n_params() eq 0 then begin print,'Please call sagetext using the filename of the file to read.' print,'Also provide the record number to read, or answer the prompts.' print,'You may also use the output keyword to specify the output file.' print,'Usage examples:' print,'1. Writes record 100 of SAGE_II_SPEC_199804.6.10 to sage.dat' print,' sagetext,''SAGE_II_SPEC_199804.6.10'',100' print,'2. If the record number is left off, you will be prompted.' print,' sagetext,''SAGE_II_SPEC_199804.6.10' print,'3. In this example, output is written to ''output.dat''' print,' sagetext,''SAGE_II_SPEC_199804.6.10'',100,output=''output.dat''' return endif ;set default outputfilename if necessary if not keyword_set(outputfile) then outputfile = 'sage.dat' ;if there is no record number, prompt the user if n_params() lt 2 then begin str1 = '' str2 = '' read,str1,$ prompt='Enter the starting record number (or Enter for first record of the file): ' read,str2,$ prompt='Enter the ending record number (or Enter for the last record of the file): ' if strlen(str1) eq 0 then recstart = 0 else reads,str1,recstart if strlen(str2) eq 0 then recend = -1 else reads,str2,recend endif else begin recstart = recnum recend = recnum endelse ;create the index file name from the spec file name indexfile = getindexname(file) readstructs,indexfile,index ;now that the index is available, reset recend (if necessary) if recend eq -1 then recend = index.num_prof-1 if recstart gt index.num_prof-1 or recstart lt 0 or $ recend gt index.num_prof-1 or recend lt 0 then begin print,'The specified record number are outside the range for this file.' print,'Please try again with records between 0 and ',$ strtrim(index.num_prof-1,2),'.' return endif ;open the output file openw,lun,outputfile,/get_lun for ixrec = recstart,recend do begin readstructs,file,ixrec,spec ;get the structure tag names spectgnm = tag_names(spec) idxtgnm = tag_names(index) ;loop over index fields for lcv = 0,n_elements(idxtgnm)-1 do begin ;if there are 930 elements, just print the one for this record type = size(index.(lcv),/type) ;if it is a byte array, print the string if type eq 1 then begin printf,lun,idxtgnm(lcv),' ',string(index.(lcv)) ;if it has one element per event, print only the one for this event endif else if n_elements(index.(lcv)) eq 930 then begin printf,lun,idxtgnm(lcv),index.(lcv)(ixrec) ;if it has just one element, print all on one line endif else if n_elements(index.(lcv) eq 1) then begin printf,lun,idxtgnm(lcv),index.(lcv) ;for multiple elements, print the title on a separate line endif else begin printf,lun,idxtgnm(lcv) printf,lun,index.(lcv) endelse endfor ;skip a line printf,lun ;loop through the spec file for lcv = 0,n_elements(spectgnm)-1 do begin printf,lun,spectgnm(lcv) printf,lun,spec.(lcv) endfor endfor ;close the file close,lun & free_lun,lun end