1 |
#! /bin/bash |
2 |
|
3 |
# mlovccf (make list of valid ccfs) |
4 |
# |
5 |
# Makes the list of valid CCFs. These are all CCFs, among those published since the |
6 |
# beginning of the project, that are required to process any ODF at a given date, |
7 |
# typically the current date. This list will change with time as far as new |
8 |
# CCFs are issued to deal with new calibration data and/or replace obsolete ones. |
9 |
# |
10 |
# To identify the set of CCFs required to process a given ODF is the job of task cifbuild. |
11 |
# To make such identification, cifbuild needs only the start time of the observation. |
12 |
# Therefore, if we provide a list of all observed ODFs at a given date together with their |
13 |
# respective start time, we could compute all the CCFs required to process them. |
14 |
# For such a moment, that list is the list of valid CCFs required to process |
15 |
# any observed ODF. |
16 |
# |
17 |
# We need as input the list of all observed ODFs at a given date. |
18 |
# This is obtained from the report of all observations that the XSA makes to CDS, |
19 |
# xsaobslog.txt (http://nxsa.esac.esa.int/ftp_public/cfs_obslog/xsaobslog.txt). |
20 |
# |
21 |
# $Id: mlovccf,v 1.17 2016/11/29 13:44:41 ccflib Exp $ |
22 |
|
23 |
# Next two lines allow to submit this script to the grid |
24 |
# request Bourne shell as shell for job |
25 |
#$ -S /bin/bash |
26 |
|
27 |
adminmail="eduardo.ojero@sciops.esa.int" |
28 |
|
29 |
VALID_CCF="$HOME/processing_valid_ccf" |
30 |
|
31 |
mailfile="${VALID_CCF}/mailfile" |
32 |
|
33 |
host=`hostname | cut -d. -f1` |
34 |
|
35 |
now=`date +'%Y%m%d_%H%M'` |
36 |
now_table=`date +'%Y-%m-%dT%H:%M:%S'` |
37 |
|
38 |
|
39 |
# +++ Function mailnotify |
40 |
# |
41 |
# This function is used to notify adminmail on any issue while runnning this procedure |
42 |
# |
43 |
# Needs a subject as $1 and body as local file to be send. |
44 |
|
45 |
mailnotify () |
46 |
{ |
47 |
|
48 |
[ "$#" -ne "2" ] && return |
49 |
|
50 |
subject=$1 |
51 |
|
52 |
bodyfile=$2 |
53 |
|
54 |
[ ! -f "${bodyfile}" ] && return |
55 |
|
56 |
echo ; echo "Sending message to ${adminmail} with subject ${subject}" ; echo |
57 |
|
58 |
mailx -v -s "${subject}" "${adminmail}" < ${bodyfile} |
59 |
|
60 |
} |
61 |
|
62 |
|
63 |
|
64 |
# +++ Function getxsaobslog |
65 |
# |
66 |
# We need to get the list of all observed ODFs at the current date. |
67 |
# This is obtained from http://nxsa.esac.esa.int/ftp_public/cds_obslog/xsaobslog.txt. |
68 |
|
69 |
getxsaobslog () |
70 |
{ |
71 |
|
72 |
XSAOBSURL="http://nxsa.esac.esa.int/ftp_public/cds_obslog/xsaobslog.txt" |
73 |
|
74 |
# Remove any xsaobslog.txt previously downloaded. |
75 |
|
76 |
[ -f "${VALID_CCF}/xsaobslog.txt" ] && rm -rf ${VALID_CCF}/xsaobslog.txt |
77 |
|
78 |
# Get the list latest list of ODFs available from nXSA server. |
79 |
|
80 |
wget -q ${XSAOBSURL} -O ${VALID_CCF}/xsaobslog.txt |
81 |
|
82 |
# Rename xsaobslog.txt to xsaobslog_${now}.txt just to have a reference |
83 |
# of which list of ODFs was used to compute the list of valid CCFs. |
84 |
|
85 |
mv ${VALID_CCF}/xsaobslog.txt ${VALID_CCF}/xsaobslog_${now}.txt |
86 |
|
87 |
} |
88 |
|
89 |
|
90 |
|
91 |
# +++Function get_ccf_list to list on stdout the table of CCFs in a given CIF. |
92 |
# |
93 |
# Requires that Heasoft is initialised. Otherwise exits with error. |
94 |
|
95 |
get_ccf_list() |
96 |
{ |
97 |
[ -z "$1" ] && return |
98 |
|
99 |
cif_to_process="$1" |
100 |
|
101 |
noversion=`which fversion | grep -c no` |
102 |
|
103 |
if [ "${noversion}" != "0" ] ; then |
104 |
echo "Error: Heasoft not initialised !" |
105 |
[ -f "${mailfile}" ] && rm -rf ${mailfile} |
106 |
echo "Running of fversion produced the following output:" >> $mailfile |
107 |
echo "$noversion" >> $mailfile |
108 |
mailnotify "mvloccf: Error: Heasoft not initialised!" $mailfile |
109 |
rm ${mailfile} |
110 |
exit |
111 |
fi |
112 |
|
113 |
fdump ${cif_to_process} prhead=no showcol=no showrow=no page=no \ |
114 |
columns="SCOPE TYPEID ISSUE" \ |
115 |
rows=- STDOUT |
116 |
} |
117 |
|
118 |
|
119 |
|
120 |
|
121 |
# +++ Fill in the valid_constituents directory with the Valid CCF set |
122 |
# |
123 |
# The directory to be filled in is ${VALIDCONSTITUENTSDISR}. |
124 |
# This directory is synchronized with the similar directory in the |
125 |
# external ftp server. |
126 |
# |
127 |
# In addition to that, we also use this function to fill in the |
128 |
# /ccf/valid directory available for users in the intranet. |
129 |
|
130 |
fillvalidccfdir() |
131 |
{ |
132 |
|
133 |
[ -z "$1" ] && return |
134 |
|
135 |
validccflist=$1 |
136 |
|
137 |
if [ ! -f "${VALID_CCF}/${validccflist}" ] ; then |
138 |
echo "Error: ${VALID_CCF}/${validccflist} not found - Abort!" |
139 |
[ -f "${mailfile}" ] && rm -rf ${mailfile} |
140 |
echo "The list of valid CCF ${VALID_CCF}/${validccflist} is not found" >> $mailfile |
141 |
mailnotify "mvloccf: Error: ${VALID_CCF}/${validccflist} not found - Abort!" $mailfile |
142 |
rm ${mailfile} |
143 |
exit |
144 |
fi |
145 |
|
146 |
VALIDCONSTITUENTSDIR="/home/ccflib/ftp-area/valid_constituents" |
147 |
rm -rf ${VALIDCONSTITUENTSDIR}/* |
148 |
|
149 |
VALIDCCFDIRINTRANET="/ccf/valid" |
150 |
rm -rf ${VALIDCCFDIRINTRANET}/* |
151 |
|
152 |
CONSTITUENTSDIR="/home/ccflib/ftp-area/constituents" |
153 |
n=0 |
154 |
while read ccf |
155 |
do |
156 |
n=$((n + 1)) |
157 |
echo "$n cp -a ${CONSTITUENTSDIR}/${ccf} ${VALIDCONSTITUENTSDIR}/" |
158 |
cp -a ${CONSTITUENTSDIR}/${ccf} ${VALIDCONSTITUENTSDIR}/ |
159 |
echo "$n cp -a ${CONSTITUENTSDIR}/${ccf} ${VALIDCCFDIRINTRANET}/" |
160 |
cp -a ${CONSTITUENTSDIR}/${ccf} ${VALIDCCFDIRINTRANET}/ |
161 |
done < ${VALID_CCF}/${validccflist} |
162 |
|
163 |
} |
164 |
|
165 |
|
166 |
# |
167 |
# M a i n p r o g r a m |
168 |
# |
169 |
# 1. Get the list of all ODFs |
170 |
|
171 |
|
172 |
xsaobslogsize="0" |
173 |
|
174 |
getxsaobslog |
175 |
|
176 |
xsaobslogsize=`stat --format=%s ${VALID_CCF}/xsaobslog_${now}.txt` |
177 |
|
178 |
if [ "$xsaobslogsize" = "0" ] ; then |
179 |
echo "Error: Failure to download the XSA Obs. Log file - Abort" |
180 |
[ -f "${mailfile}" ] && rm -rf ${mailfile} |
181 |
echo "The XSA Obs. Log file has 0 size - Not downloaded?" >> $mailfile |
182 |
echo "." >> $mailfile |
183 |
mailnotify "mlovccf: Error: Failure to download the XSA Obs. Log file - Abort" "$mailfile" |
184 |
rm ${mailfile} |
185 |
exit |
186 |
fi |
187 |
|
188 |
# |
189 |
# 2. Initialises HEADAS and SAS |
190 |
# |
191 |
|
192 |
# Defines HEADAS depending on host |
193 |
|
194 |
case "$host" in |
195 |
sasbld01|sasbld02) |
196 |
export HEADAS=/sasbuild/local/${host}/headas/architecture |
197 |
;; |
198 |
scigrid6|cn-*) |
199 |
export HEADAS=/sasbuild/local/sasbld11/headas/architecture |
200 |
;; |
201 |
*) echo "Error: Unsupported host $host" ; exit 1 ;; |
202 |
esac |
203 |
|
204 |
|
205 |
headas_output="${VALID_CCF}/headas_output" |
206 |
source $HEADAS/headas-init.sh > ${headas_output} 2>&1 |
207 |
headas_ok=`cat ${headas_output}` |
208 |
|
209 |
if [ -n "${headas_ok}" ] ; then |
210 |
echo "mlovccf: HEADAS initialisation failed with error" >> ${mailfile} |
211 |
cat ${headas_output} >> ${mailfile} |
212 |
mailnotify "mlovccf: HEADAS initilisation failed" ${mailfile} |
213 |
rm ${headas_output} ${mailfile} |
214 |
exit 1 |
215 |
fi |
216 |
|
217 |
# To be able to mix several jobs on the same ccflib account |
218 |
[ ! -d "${HOME}/pfiles/${host}" ] && mkdir ${HOME}/pfiles/${host} |
219 |
export PFILES="${HOME}/pfiles/${host};${HEADAS}/syspfiles" |
220 |
echo -n "HEADAS version: " |
221 |
fversion |
222 |
|
223 |
# |
224 |
# W a r n i n g o n S A S i n i t i a l i s a t i o n |
225 |
# |
226 |
# The SAS initialisation produced by setsas.sh sets SAS_CCFPATH to /ccf/pub. |
227 |
# Hence, if we set SAS_CCFPATH before running setsas.sh, we will always |
228 |
# use /ccf/pub as repository for CCF. This is right as long as /ccf/pub is in |
229 |
# strict sync with /home/ccflib/ccf_ftp/constituents. But if this is not true, |
230 |
# the resulting valid CCFs might be wrong. Therefore, we set it here explicitly. |
231 |
# |
232 |
|
233 |
confsas_output="${VALID_CCF}/confsas_output" |
234 |
/sas/bin/confsas > ${confsas_output} 2>&1 |
235 |
confsas_ok=`cat ${confsas_output}` |
236 |
|
237 |
if [ -n "${confsas_ok}" ] ; then |
238 |
echo "mlovccf: SAS initialization failed with error" >> ${mailfile} |
239 |
cat ${confsas_output} >> ${mailfile} |
240 |
mailnotify "mlovccf: SAS initialisation failed" ${mailfile} |
241 |
rm ${confsas_output} ${mailfile} |
242 |
exit 1 |
243 |
fi |
244 |
|
245 |
source $HOME/setsas.sh 1> /dev/null |
246 |
|
247 |
# Re-sets SAS_CCFPATH |
248 |
|
249 |
export SAS_CCFPATH="/home/ccflib/ftp-area/constituents" |
250 |
|
251 |
echo "SAS Version: " |
252 |
sasversion -V 1 |
253 |
export SAS_VERBOSITY=0 |
254 |
|
255 |
|
256 |
# 3. XMM_CALINDEX file from SAS_CCFPATH |
257 |
|
258 |
MIF=`ls -1 ${SAS_CCFPATH}/XMM_CALINDEX_*.CCF` |
259 |
|
260 |
# 4. For each OBDS ID registered in the XSA observations list, gets the start |
261 |
# time of the observation. Then runs cifbuild to get the respective CIF. |
262 |
# all_ccfs_${now}.txt is filled recursively on each iteration. |
263 |
# Therefore we start with an empty file. The final list will be this one. |
264 |
|
265 |
# Output file : ${VALID_CCF}/all_ccfs_${now}.txt |
266 |
|
267 |
touch ${VALID_CCF}/all_ccfs_${now}.txt |
268 |
|
269 |
n=0 |
270 |
|
271 |
while read line |
272 |
do |
273 |
obsid=`echo $line | awk -F"|" '{print $2}'` |
274 |
stime=`echo $line | awk -F"|" '{print $9}'` |
275 |
stime=`echo $stime | tr " " "T"` |
276 |
n=$((n+1)) |
277 |
echo "($n) Processing $obsid $stime" ; echo |
278 |
|
279 |
# Run cifbuild |
280 |
|
281 |
cif_file=${VALID_CCF}/${obsid}.cif |
282 |
|
283 |
cifbuild --withmasterindexset='yes' \ |
284 |
--masterindexset=${MIF} \ |
285 |
--withobservationdate='yes' \ |
286 |
--observationdate=${stime} \ |
287 |
--analysisdate='now' \ |
288 |
--calindexset=${cif_file} |
289 |
|
290 |
|
291 |
# Gets the list of CCFs included in the CIF |
292 |
|
293 |
get_ccf_list "${cif_file}" > ${VALID_CCF}/${obsid}_ccfs.tmp |
294 |
|
295 |
while read line |
296 |
do |
297 |
[ "$line" = "" ] && continue |
298 |
class=`echo $line | awk -F" " '{print $1"_"$2}'` |
299 |
issue=`echo $line | awk -F" " '{print $3}'` |
300 |
issue=`printf "%04d" ${issue}` |
301 |
echo ${class}_${issue}.CCF >> ${VALID_CCF}/${obsid}_ccfs.txt |
302 |
|
303 |
done < ${VALID_CCF}/${obsid}_ccfs.tmp |
304 |
|
305 |
rm ${VALID_CCF}/${obsid}_ccfs.tmp |
306 |
|
307 |
mv ${VALID_CCF}/all_ccfs_${now}.txt ${VALID_CCF}/all_ccfs_${now}.tmp |
308 |
|
309 |
cat ${VALID_CCF}/${obsid}_ccfs.txt ${VALID_CCF}/all_ccfs_${now}.tmp | sort -u > ${VALID_CCF}/all_ccfs_${now}.txt |
310 |
|
311 |
comm -1 -3 --nocheck-order ${VALID_CCF}/all_ccfs_${now}.tmp ${VALID_CCF}/all_ccfs_${now}.txt > ${VALID_CCF}/diff_iteration_${n}.txt |
312 |
|
313 |
diffsize=`stat --format=%s ${VALID_CCF}/diff_iteration_${n}.txt` |
314 |
|
315 |
if [ "$diffsize" != "0" ] ; then |
316 |
echo "CCF added in this iteration: " |
317 |
cat ${VALID_CCF}/diff_iteration_${n}.txt |
318 |
fi |
319 |
|
320 |
echo |
321 |
echo "Number of valid CCFs found so far: "`cat ${VALID_CCF}/all_ccfs_${now}.txt | wc -l` ; echo |
322 |
|
323 |
rm ${VALID_CCF}/diff_iteration_${n}.txt |
324 |
rm ${cif_file} |
325 |
rm ${VALID_CCF}/${obsid}_ccfs.txt |
326 |
rm ${VALID_CCF}/all_ccfs_${now}.tmp |
327 |
|
328 |
done < ${VALID_CCF}/xsaobslog_${now}.txt |
329 |
|
330 |
# 5. Make a table of classes and issues with the style "Pipeline Release Notes" |
331 |
# To get a classification, we need first to obtain all available classes. |
332 |
# |
333 |
|
334 |
|
335 |
# Lists all CCF classes |
336 |
|
337 |
ccf_classes="${VALID_CCF}/ccf_classes.txt" |
338 |
|
339 |
while read line |
340 |
do |
341 |
class=`echo $line | awk -F"_" '{print $1"_"$2}'` |
342 |
echo $class >> ${ccf_classes} |
343 |
done < ${VALID_CCF}/all_ccfs_${now}.txt |
344 |
|
345 |
cat ${ccf_classes} | sort -u > ${VALID_CCF}/kk.txt |
346 |
mv ${VALID_CCF}/kk.txt ${ccf_classes} |
347 |
|
348 |
|
349 |
|
350 |
# Output final file is named all_ccfs_${now}_table.txt |
351 |
|
352 |
total_number_of_ccfs=0 |
353 |
|
354 |
all_ccfs_table="${VALID_CCF}/all_ccfs_${now}_table.txt" |
355 |
|
356 |
[ -f "${all_ccfs_table}" ] && rm ${all_ccfs_table} |
357 |
|
358 |
echo "Table of valid CCFs at $now_table" >> ${all_ccfs_table} |
359 |
echo >> ${all_ccfs_table} |
360 |
echo "|================================|=============|=======|" >> ${all_ccfs_table} |
361 |
echo "| Calibration File | Issue range | Count |" >> ${all_ccfs_table} |
362 |
echo "|================================|=============|=======|" >> ${all_ccfs_table} |
363 |
|
364 |
while read ccf_class |
365 |
do |
366 |
echo -n "Processing class $ccf_class ..." |
367 |
issues_file="${VALID_CCF}/ccf_issues_for_${ccf_class}.txt" |
368 |
[ -f "${issues_file}" ] && rm ${issues_file} |
369 |
while read line |
370 |
do |
371 |
class=`echo $line | awk -F"_" '{print $1"_"$2}'` |
372 |
[ "$class" != "$ccf_class" ] && continue |
373 |
issue=`echo $line | awk -F"_" '{print $3}' | cut -d. -f1` |
374 |
echo $issue >> ${issues_file} |
375 |
done < ${VALID_CCF}/all_ccfs_${now}.txt |
376 |
|
377 |
cat ${issues_file} | sort -u > kk.txt |
378 |
mv kk.txt ${issues_file} |
379 |
real_number_of_issues_for_this_class=`wc -l ${issues_file} | awk -F" " '{print $1}'` |
380 |
|
381 |
first_issue=`head -1 ${issues_file}` |
382 |
last_issue=`tail -1 ${issues_file}` |
383 |
|
384 |
li=$((10#$last_issue)) |
385 |
fi=$((10#$first_issue)) |
386 |
|
387 |
sequential_number_of_issues_for_this_class=$((li - fi + 1)) |
388 |
|
389 |
asterisk=" " |
390 |
[ "${real_number_of_issues_for_this_class}" -lt "${sequential_number_of_issues_for_this_class}" ] && asterisk="(*)" |
391 |
|
392 |
total_number_of_ccfs=$((real_number_of_issues_for_this_class + total_number_of_ccfs)) |
393 |
|
394 |
rn=$real_number_of_issues_for_this_class |
395 |
|
396 |
if [ "${first_issue}" != "${last_issue}" ] ; then |
397 |
if [ "$asterisk" = "(*)" ] ; then |
398 |
printf "| %-30s | %4s-%4s(*)| %4s |\n" ${ccf_class} ${first_issue} ${last_issue} ${rn} >> ${all_ccfs_table} |
399 |
else |
400 |
printf "| %-30s | %4s-%4s | %4s |\n" ${ccf_class} ${first_issue} ${last_issue} ${rn} >> ${all_ccfs_table} |
401 |
fi |
402 |
else |
403 |
printf "| %-30s | %4s | %4s |\n" ${ccf_class} ${first_issue} ${rn} >> ${all_ccfs_table} |
404 |
fi |
405 |
|
406 |
|
407 |
echo "done" |
408 |
done < ${ccf_classes} |
409 |
|
410 |
echo "|================================|=============|=======|" >> ${all_ccfs_table} |
411 |
echo >> ${all_ccfs_table} |
412 |
echo "(*): Incomplete range." >> ${all_ccfs_table} |
413 |
echo "" >> ${all_ccfs_table} |
414 |
echo "Total number of valid CCFs required : ${total_number_of_ccfs}" >> ${all_ccfs_table} |
415 |
rm ${VALID_CCF}/ccf_classes.txt |
416 |
rm ${VALID_CCF}/ccf_issues_for*.txt |
417 |
|
418 |
|
419 |
|
420 |
# 6. Fill in the Valid CCF Set dir with the proper CCFs |
421 |
|
422 |
fillvalidccfdir "all_ccfs_${now}.txt" |
423 |
|
424 |
[ -f "${mailfile}" ] && rm -rf ${mailfile} |
425 |
echo "Valid CCF Set successfully copied to ${VALIDCONSTITUENTSDIR}" >> $mailfile |
426 |
echo "Valid CCF Set successfully copied to ${VALIDCCFDIRINTRANET}" >> $mailfile |
427 |
mailnotify "mvloccf: Valid CCF Set created successfully" $mailfile |
428 |
rm ${mailfile} |