source: BAORadio/AmasNancay/hdrfitsextractor.sh @ 510

Last change on this file since 510 was 510, checked in by campagne, 13 years ago

change scriptpath (general) + minor things

File size: 4.4 KB
Line 
1#!/bin/sh
2#download SCA file, proceed to introspection, and upload results
3DATE=/bin/date
4GREP=/bin/grep
5AWK=/bin/awk
6ECHO=/bin/echo
7WC=/usr/bin/wc
8CAT=/bin/cat
9PRINTF=/usr/bin/printf
10MKDIR=/bin/mkdir
11XARGS=/usr/bin/xargs
12SORT=/bin/sort
13RM=/bin/rm
14defaultIFS=$IFS
15
16#set Irods environment   
17. /usr/local/shared/bin/irods_env.sh -noverbose
18
19scriptName="`basename $0`"
20$ECHO "Processing script ${scriptName} at `date`"
21
22#which source to analyse
23sourceRadio=$1
24
25#Path to public backupable path
26publicpath="/afs/in2p3.fr/home/c/campagne/public"
27
28
29#Path where the job will do temporary IO
30if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
31    iojobpath="/sps/baoradio/AmasNancay/JEC"
32    $MKDIR -p $iojobpath
33elif [ ${ENVIRONMENT} = "BATCH" ] ; then
34    iojobpath=${TMPBATCH}
35else
36    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
37    exit 1
38fi
39cd ${iojobpath}
40
41localpath="${sourceRadio}"
42$MKDIR -p ./${localpath}
43cd ./${localpath}
44
45
46#Path of the utility scripts
47scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
48
49
50#get the daq current irod status
51tag=`${DATE} +%F`
52OUT1=${publicpath}/baodaqstatus-${tag}.txt
53
54if [ ! -e ${OUT1} ]; then
55    $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
56    ${scriptpath}/statusdaq.sh > ${OUT1}
57fi
58
59if [ ! "<${sourceRadio}>" = "<>" ]; then
60    $ECHO "You have selected sourceRadio = $sourceRadio"
61    tableau=( `$GREP -i ${sourceRadio} ${OUT1} | $AWK '( NF==4 ) { print $2 }' ` )
62else
63    tableau=( `$CAT ${OUT1} | $AWK '( NF==4 ) { print $2 }'`  )
64fi
65IFS='
66'
67tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
68
69
70workArea="headers"
71$MKDIR -p ./${workArea}
72cd ./${workArea}
73
74
75nloop4dbg=0
76
77
78for i in ${tableau[@]}
79do
80#look if the headers have been already extracted
81  $ECHO "DEBUG (${scriptName}): irods root dir $i"
82  for fibdir in Fiber1 Fiber2; do
83      $ECHO "DEBUG (${scriptName}): Fiber $fibdir"
84#NB: care should be taken to the fact that ILS returns all the instance of the file (HPSS, disk...)
85#    so one should use a trick to count unique instance
86      listOfsig=( `ils ${i}/${fibdir} | $GREP -i "signal" | $XARGS -i basename {} | $SORT -k1.7n` )
87      IFS='
88'
89      listOfsig=( $( $PRINTF "%s\n" "${listOfsig[@]}" | $AWK 'x[$0]++ == 0' ) )
90      nblinesSIG=${#listOfsig[@]}
91      listOfhdr=( `ils ${i}/${fibdir} | $GREP -i "header" | $XARGS -i basename {} | $SORT -k1.7n` )
92      IFS='
93'
94      listOfhdr=( $( $PRINTF "%s\n" "${listOfhdr[@]}" | $AWK 'x[$0]++ == 0' ) )
95      nblinesHDR=${#listOfhdr[@]}
96      $ECHO "(DEBUG) (${scriptName}:  test if $nblinesHDR != $nblinesSIG "
97      if [ $nblinesHDR != $nblinesSIG ]; then
98#do the header extraction
99#build signal files list
100#loop on signal files
101          $ECHO "(DEBUG) (${scriptName}):  ${listOfsig[@]}"
102
103          for sigfname in ${listOfsig[@]}
104            do
105            filename=$(basename $sigfname)
106            extension=${filename##*.}
107            filename=${filename%.*}
108            number=`$ECHO $filename | $AWK '{len=length("signal");len=len+1;a=substr($1,len);print a}'`
109            hdrname="header${number}.txt"
110#look if header file already exists: if so skip it
111            hdrIsthere=`ils ${i}/${fibdir}/${hdrname} 2>&1 | $GREP -i "^ERROR" | $WC -l`
112            $ECHO "(DEBUG) (${scriptName}): look if ${hdrname}  exist (${hdrIsthere})"
113            if [ $hdrIsthere -eq 0 ]; then 
114                continue
115            fi
116#get the signal file
117            $ECHO "DEBUG (${scriptName}): iget $sigfname (dgg = ${nloop4dbg})"
118            OUT3=./igetlog${nloop4dbg}.$$
119            iget -f -v -K ${i}/${fibdir}/${sigfname} . >${OUT3} 2>&1
120            igetStatus=`$GREP "^ERROR" ${OUT3}`
121            if [ "<$igetStatus>" != "<>" ]; then
122                $ECHO "INFO (${scriptName}): error while iget:"
123                $ECHO $igetStatus
124                continue
125            fi
126            $RM $OUT3
127#extract the header part of the  signal file
128            $ECHO "DEBUG (${scriptName}): get header from ${sigfname} and put it in ${hdrname}"
129            IFS=$defaultIFS
130            while read -n80 L
131              do
132              [ "${L::3}" = "END" ] && break
133              $ECHO $L
134            done < ${sigfname} > ${hdrname}
135            $RM ${sigfname}
136            nhdrlines=`$WC -l ${hdrname} | $AWK '{print $1}' `
137            if [  "${nhdrlines}" = "0"  ]; then
138                $ECHO "WARNING (${scriptName}): ${sigfname} has no valid header"
139            else
140#transfert to irod the header
141                $ECHO "INFO (${scriptName}): transfert ${hdrname} to ${i}/${fibdir}"
142#use -f option ONLY to force override
143                iput  -v -K ${hdrname} ${i}/${fibdir}
144            fi
145            $RM ${hdrname}
146            nloop4dbg=$(expr $nloop4dbg + 1)
147          done
148      else
149          $ECHO "INFO (${scriptName}): nothing to do for ${i}/${fibdir}"
150      fi
151  done
152done
Note: See TracBrowser for help on using the repository browser.