pro pca2aref,sqlambf,pcompf,avgarff,defarff,areffil,$ vthresh=vthresh,ncomp=ncomp,verbose=verbose, _extra=e ;+ ;procedure pca2aref ; reads in the PCA decomposition of the calibration library ARFs ; and writes it out to a FITS file in the AREF format ; ;syntax ; pca2aref,sqlambf,pcompf,avgarff,defarff,areffil,vthresh=vthresh,ncomp=ncomp ; ;parameters [all required] ; sqlambf [INPUT] full path and name of sqrt(lambda) file ; pcompf [INPUT] full path and name of principal components file ; avgarff [INPUT] full path and name of average ARFs file ; defarff [INPUT] full path and name of default ARF used in generation of cal library ARFs ; areffil [INPUT] full path and name of output AREF file ; ;keywords ; vthresh [INPUT] how much of the total variance should be included in the AREF? ; * if 0 or not given, uses NCOMP, or if that is not given, then is set to 0.99 ; * if <0, abs() value is used ; * if <1, assumes fractional variance ; * if >1, assumes actual variance value (do not use unless you know what you are doing) ; ncomp [INPUT] number of components to include, starting from one that accounts for most variance ; * default is number of components or 20, whichever is smaller ; * if -ve, reverse order and exclude, starting from one that accounts for least variance ; * in all cases, keeps at least one component ; verbose [INPUT] control chatter ; _extra [JUNK] here only to prevent crashing the program ; ;history ; vinay kashyap (2014apr) ;- ; usage ok='ok' & np=n_params() nsf=n_elements(sqlambf) & npf=n_elements(pcompf) & ndf=n_elements(defarff) & naf=n_elements(avgarff) & nof=n_elements(areffil) ssf=size(sqlambf,/type) & spf=size(pcompf,/type) & sdf=size(defarff,/type) & saf=size(avgarff,/type) & sof=size(areffil,/type) ; if np lt 5 then ok='Insufficient parameters' else $ if nsf eq 0 then ok='SQLAMBF: not defined' else $ if nsf gt 1 then ok='SQLAMBF: must be a scalar' else $ if ssf ne 7 then ok='SQLAMBF must be a string' else $ if npf eq 0 then ok='PCOMPF: not defined' else $ if npf gt 1 then ok='PCOMPF: must be a scalar' else $ if spf ne 7 then ok='PCOMPF must be a string' else $ if ndf eq 0 then ok='DEFARFF: not defined' else $ if ndf gt 1 then ok='DEFARFF: must be a scalar' else $ if sdf ne 7 then ok='DEFARFF must be a string' else $ if naf eq 0 then ok='AVGARFF: not defined' else $ if naf gt 1 then ok='AVGARFF: must be a scalar' else $ if saf ne 7 then ok='AVGARFF must be a string' else $ if nof eq 0 then ok='AREFFIL: not defined' else $ if nof gt 1 then ok='AREFFIL: must be a scalar' else $ if sof ne 7 then ok='AREFFIL must be a string' if ok ne 'ok' then begin print,'Usage: pca2aref,sqlambf,pcompf,avgarff,defarff,areffil,vthresh=vthresh,ncomp=ncomp' print,' convert output of run_pca_on_arflib.r to AREF format FITS file' if np ne 0 then message,ok,/informational return endif ; VERBOSE vv=0L & if keyword_set(verbose) then vv=long(verbose[0])>1L fsf=file_search(sqlambf[0],count=nfsf) fpf=file_search(pcompf[0],count=nfpf) fdf=file_search(defarff[0],count=nfdf) faf=file_search(avgarff[0],count=nfaf) if nfsf eq 0 then ok=SQLAMBF+': file not found' else $ if nfpf eq 0 then ok=PCOMPF+': file not found' else $ if nfdf eq 0 then ok=DEFARFF+': file not found' else $ if nfaf eq 0 then ok=AVGARFF+': file not found' if ok ne 'ok' then begin message,ok,/informational return endif fof=file_search(areffil[0],count=nfof) if nfof ne 0 then begin message,fof[0]+': file exists, and will be overwritten',/informational wait,5 endif outfile=areffil[0] ; read in the inputs ; SQLAMB spawn,'wc -l '+sqlambf[0],tmp & kcomp=long(tmp[0]) sqlamb=fltarr(kcomp) & openr,uu,sqlambf[0],/get_lun & readf,uu,sqlamb & close,uu & free_lun,uu ; PCOMP spawn,'wc -w '+pcompf[0],tmp & ncol=long(tmp[0])/kcomp pcomp=fltarr(ncol,kcomp) & openr,uu,pcompf[0],/get_lun & readf,uu,pcomp & close,uu & free_lun,uu ; AVGARF avgarf=fltarr(ncol) & openr,uu,avgarff[0],/get_lun & readf,uu,avgarf & close,uu & free_lun,uu ; DEFARFF defarf0=mrdfits(defarff,0,harf0) & defarf=mrdfits(defarff,1,harf) ; figure out the columns and define the bias vector ok='ok' tagnames=tag_names(defarf) & ntags=n_elements(tagnames) oarf=where(tagnames eq 'SPECRESP',moarf) oelo=where(tagnames eq 'ENERG_LO',moelo) oehi=where(tagnames eq 'ENERG_HI',moehi) if moelo eq 0 then ok=arfeff[0]+': does not contain ENERG_LO column in extension '+sxpar(harf1,'EXTNAME') else $ if moehi eq 0 then ok=arfeff[0]+': does not contain ENERG_HI column in extension '+sxpar(harf1,'EXTNAME') if moarf eq 0 then ok=arfeff[0]+': does not contain SPECRESP column in extension '+sxpar(harf1,'EXTNAME') if ok ne 'ok' then begin message,ok,/informational message,'Quitting...',/informational return endif ; elo=defarf.ENERG_LO & ehi=defarf.ENERG_HI & arf0=defarf.SPECRESP owlo=where(tagnames eq 'BIN_LO',mowlo) owhi=where(tagnames eq 'BIN_HI',mowhi) if mowlo eq 0 then wlo=12.39851/ehi else wlo=defarf.BIN_LO if mowhi eq 0 then whi=12.39851/elo else whi=defarf.BIN_HI nbin=n_elements(arf0) ;this should be the same as NCOL if nbin ne ncol then begin message,'there is a mismatch between '+defarff[0]+' and '+pcompf[0],/informational message,'Quitting...',/informational return endif ; ; are there any extra columns? ok=intarr(ntags)-1 if moarf gt 0 then ok[oarf[0]]=oarf[0] if moelo gt 0 then ok[oelo[0]]=oelo[0] if moehi gt 0 then ok[oehi[0]]=oehi[0] if mowlo gt 0 then ok[owlo[0]]=owlo[0] if mowhi gt 0 then ok[owhi[0]]=owhi[0] oy=where(ok lt 0,moy) & if moy gt 0 then xtags=tagnames[oy] ; figure out how many of the components to keep pcafrac=sqlamb^2/total(sqlamb^2) pcacvar=total(pcafrac,/cumul) pcacfrac=pcacvar/max(pcacvar) ; ; NCOMP mcomp=20 if keyword_set(ncomp) then begin ii=long(ncomp[0]) if ii lt 0 then mcomp=(kcomp-ii)>1 ;if -ve, exclude last NCOMP if ii gt 0 then mcomp=ii0)] xevec=fltarr(nbin,mcomp+1) & for i=0L,mcomp-1L do xevec[*,i]=pcomp[*,i] & xevec[*,mcomp]=pcaresid[*] pcacompstr.eigenvec=xevec mwrfits,pcacompstr,outfile,hdr0e2 if vv gt 1000 then stop,'halting; type .CON to continue' return end ; example run sqlambf='/data/snafu/kashyap/Cal/CalErr/hrcsletg/sqlamb_hrcsletg.txt' pcompf='/data/snafu/kashyap/Cal/CalErr/hrcsletg/pcomps_hrcsletg.txt' avgarff='/data/snafu/kashyap/Cal/CalErr/hrcsletg/avgarf_hrcsletg.txt' defarff='/data/legs/rpete/flight/MC_uncertainties/vinay_hrcs_letg/sample.garf' if not keyword_set(areffil) then areffil='/data/snafu/kashyap/Cal/CalErr/hrcsletg/aref_hrcsletg.fits' if n_elements(vthresh) eq 0 then vthresh=0.95 if not keyword_set(verbose) then verbose=1 pca2aref,sqlambf,pcompf,avgarff,defarff,areffil,vthresh=vthresh,ncomp=ncomp,verbose=verbose spawn,'ls -l '+areffil end