detector_id=__DETECTOR_ID__                                           
Nbetafac = 6000                                                       

lmax = __LMAX__
beammmax = __BEAMMMAX__
infile_beam = __INFILE_BEAM__

lmaxOut = __LMAXOUT__
order = __ORDER__
add = __ADD__
infile_sky = __INFILE_SKY__
polarisation = T
galactic = T
toast_run_file=__TOAST_RUN_FILE__

apply_flags = F

eff_dir = __EFF_DIR__
file_pattern = __FILE__PATTERN__
det_pattern = __DET_PATTERN__

binmap_file = __BINMAP_FILE__
binmap_nside = 1024

focalplane_db = __FOCALPLANE_DB__
calibrate_signal = T
scale = __SCALE__
fwhm_deconv = __FWHM_DECONV__
beam_frame = __BEAM_FRAME__

toast_dist = __TOAST_DIST__

outSky = tod.txt




#!/bin/bash                    
#PBS -q premium                
#PBS -l mppwidth=4800          
#PBS -l walltime=02:00:00      
#PBS -j oe                     
#PBS -N ffp8_hfi_fiducial      
#PBS -A planck                 

cd $PBS_O_WORKDIR
ncore=4800       

iscalm2tod=/project/projectdirs/planck/modules/edison/gnu/iscalm-5486396-20140827/bin/IScalm2TOD
madam_toast=/project/projectdirs/planck/modules/edison/gnu/madam_toast-svn2045-20140827/bin/madam_toast

ulimit -c unlimited

focalplane_db="\/project\/projectdirs\/planck\/data\/ffp8\/mission\/HFI-FPDB-4_13_dx11.fits"

#for comp in fg_bpm fg_nobpm cmb_scl cmb_ten cmb_ngc; do
for comp in fg_bpm; do                                  

#    for freq in 100 143 217 353 545 857 ; do
    for freq in 857 ; do                     

        run=dx11_${freq}_full_escratch.bin
        add=F                             
        norm=1.0                          

        case $freq in
            100)     
                hornlist="1a 1b 2a 2b 3a 3b 4a 4b"
                units=K_CMB                       
                ;;                                
            143)                                  
                hornlist="1a 1b 2a 2b 3a 3b 4a 4b 5 6 7"
                units=K_CMB                             
                ;;                                      
            217)                                        
                hornlist="1 2 3 4 5a 5b 6a 6b 7a 7b 8a 8b"
                units=K_CMB                               
                ;;                                        
            353)                                          
                hornlist="1 2 3a 3b 4a 4b 5a 5b 6a 6b 7 8"
                units=K_CMB                               
                ;;                                        
            545)                                          
                hornlist="1 2 4"                          
                units=MJ_per_Str                          
                ;;                                        
            857)                                          
                hornlist="1 2 3 4"                        
                units=MJ_per_Str                          
                ;;                                        
        esac                                              

        for horn in $hornlist; do

            detector=${freq}-${horn}

            binmap_file="binmap_${comp}_${detector}.fits"
            if [ -e ${binmap_file} ] ; then continue ; fi

            infile_beam="\/project\/projectdirs\/planck\/data\/ffp8\/mission\/beams\/hfi\/BS_HBM_DX11v67_I5_HIGHRES_POLAR_${detector}_xp_alm.fits"                                                                        
            lmax=6000                                                                                        
            beammmax=36                                                                                      

            beam_frame=Dxx
            lmaxout=6000  
            order=5       

            case $comp in
                cmb_scl) 
                    infile_sky="\/project\/projectdirs\/planck\/data\/ffp8\/sky\/fiducial\/ffp8_cmb_scl_${freq}_alm.fits"                                                                                                 
                    fwhm_deconv=0                                                                            
                    ;;                                                                                       
                cmb_ten)                                                                                     
                    infile_sky="\/project\/projectdirs\/planck\/data\/ffp8\/sky\/fiducial\/ffp8_cmb_ten_alm.fits"                                                                                                         
                    fwhm_deconv=0                                                                            
                    ;;                                                                                       
                cmb_ngc)                                                                                     
                    infile_sky="\/project\/projectdirs\/planck\/data\/ffp8\/sky\/fiducial\/ffp8_cmb_ngc_alm.fits"                                                                                                         
                    fwhm_deconv=0                                                                            
                    ;;                                                                                       
                fg_bpm)                                                                                      
                    infile_sky="\/project\/projectdirs\/planck\/data\/ffp8\/sky\/fiducial\/ffp8_fg_${freq}_${horn}_alm.fits"                                                                                              
                    fwhm_deconv=4                                                                            
                    ;;                                                                                       
                fg_nobpm)                                                                                    
                    infile_sky="\/project\/projectdirs\/planck\/data\/ffp8\/sky\/fiducial\/ffp8_fg_${freq}_alm.fits"                                                                                                      
                    fwhm_deconv=4                                                                            
                    ;;                                                                                       
            esac                                                                                             
                                                                                                             
            eff_dir="\/scratch3\/scratchdirs\/borrill\/ffp8\/fiducial\/tod"                                  
            file_pattern=".*\/H${freq}.*fits"                                                                
            det_pattern=".*${detector}.*"                                                                    
                                                                                                             
            cat iscalm2tod_hfi.par \                                                                         
                | sed "s/__DETECTOR_ID__/$detector/g" \                                                      
                | sed "s/__LMAX__/$lmax/g" \                                                                 
                | sed "s/__BEAMMMAX__/$beammmax/g" \                                                         
                | sed "s/__INFILE_BEAM__/$infile_beam/g" \                                                   
                | sed "s/__LMAXOUT__/$lmaxout/g" \                                                           
                | sed "s/__ORDER__/$order/g" \                                                               
                | sed "s/__ADD__/$add/g" \                                                                   
                | sed "s/__INFILE_SKY__/$infile_sky/g" \                                                     
                | sed "s/__TOAST_RUN_FILE__/$run/g" \                                                        
                | sed "s/__EFF_DIR__/$eff_dir/g" \                                                           
                | sed "s/__FILE__PATTERN__/$file_pattern/g" \                                                
                | sed "s/__DET_PATTERN__/$det_pattern/g" \                                                   
                | sed "s/__BINMAP_FILE__/$binmap_file/g" \                                                   
                | sed "s/__FOCALPLANE_DB__/$focalplane_db/g" \                                               
                | sed "s/__FWHM_DECONV__/$fwhm_deconv/g" \                                                   
                | sed "s/__BEAM_FRAME__/$beam_frame/g" \                                                     
                | sed "s/__SCALE__/$norm/g" \                                                                
                > iscalm2tod_hfi_${comp}_${detector}.par                                                     
                                                                                                             
            export OMP_NUM_THREADS=2                                                                         
            time aprun -n $[ $ncore / $OMP_NUM_THREADS ] -N 12 -S 6 -d 2 $iscalm2tod iscalm2tod_hfi_${comp}_${detector}.par >& iscalm2tod_hfi_${comp}_${detector}.log                                                     
                                                                                                             
            if [ ! -e ${binmap_file} ]; then                                                                 
                echo ${binmap_file} does not exist - exiting                                                 
                exit                                                                                         
            fi                                                                                               
                                                                                                             
        done                                                                                                 

        if [ -e maps/ffp8_${comp}_${freq}_done ] ; then continue ; fi

        export OMP_NUM_THREADS=4

        n=$[ $ncore / $OMP_NUM_THREADS ]
        N=$[ 24 / $OMP_NUM_THREADS ]
        S=$[ 12 / $OMP_NUM_THREADS ]
        d=$OMP_NUM_THREADS

        time aprun -n $n -N $N -S $S -d $d $madam_toast madam_hfi.par \
            runfile=ffp8_${freq}_full.bin \
            file_root=ffp8_${comp} \
            detset=${freq}:all \
            unit_tod=${units} \
            path_output=maps >& madam_${freq}.log

        if [ ! -e maps/ffp8_${comp}_${freq}_full_map.fits ] ; then
            echo madam_tost failure - exiting
            exit
        fi

        touch maps/ffp8_${comp}_${freq}_done

    done

done
