/[CCFs]/bin/mlovccf
ViewVC logotype

Contents of /bin/mlovccf

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.25 - (show annotations)
Mon Nov 8 13:23:09 2021 UTC (3 years ago) by ccflib
Branch: MAIN
Changes since 1.24: +27 -8 lines
Added checking for writability of ccflib to VALIDCONSTITUENTSDIR and VALIDCCFDIRINTRANET before removing files

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.24 2020/11/13 16:09:22 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 export DEBUG="T"
28
29 adminmail="eduardo.ojero@sciops.esa.int"
30
31 VALID_CCF="$HOME/processing_valid_ccf"
32
33 mailfile="${VALID_CCF}/mailfile"
34
35 host=`hostname | cut -d. -f1`
36
37 now=`date +'%Y%m%d_%H%M'`
38 now_table=`date +'%Y-%m-%dT%H:%M:%S'`
39
40
41 # +++ Function mailnotify
42 #
43 # This function is used to notify adminmail on any issue while runnning this procedure
44 #
45 # Needs a subject as $1 and body as local file to be send.
46
47 mailnotify ()
48 {
49
50 [ "$#" -ne "2" ] && return
51
52 subject=$1
53
54 bodyfile=$2
55
56 [ ! -f "${bodyfile}" ] && return
57
58 echo ; echo "Sending message to ${adminmail} with subject ${subject}" ; echo
59
60 if [ "$DEBUG" ] ; then
61
62 echo "DEBUG: mlovccf: mailnotify: Going to send mail as "
63 echo "mailx -v -s ${subject} ${adminmail} < ${bodyfile}"
64 fi
65
66 mailx -v -s "${subject}" "${adminmail}" < ${bodyfile}
67
68 if [ "$DEBUG" ] ; then
69 echo "DEBUG: mlovccf: mailnotify: Mail sent!"
70 fi
71 }
72
73
74
75 # +++ Function getxsaobslog
76 #
77 # We need to get the list of all observed ODFs at the current date.
78 # This is obtained from http://nxsa.esac.esa.int/ftp_public/cds_obslog/xsaobslog.txt.
79
80 getxsaobslog ()
81 {
82
83 XSAOBSURL="http://nxsa.esac.esa.int/ftp_public/cds_obslog/xsaobslog.txt"
84
85 # Remove any xsaobslog.txt previously downloaded.
86
87 [ -f "${VALID_CCF}/xsaobslog.txt" ] && rm -rf ${VALID_CCF}/xsaobslog.txt
88
89 # Get the list latest list of ODFs available from nXSA server.
90
91 wget --quiet ${XSAOBSURL} -O ${VALID_CCF}/xsaobslog.txt
92
93 # Rename xsaobslog.txt to xsaobslog_${now}.txt just to have a reference
94 # of which list of ODFs was used to compute the list of valid CCFs.
95
96 mv ${VALID_CCF}/xsaobslog.txt ${VALID_CCF}/xsaobslog_${now}.txt
97
98 if [ "$DEBUG" ] ; then
99 echo "DEBUG: mlovccf: getxsaobslog: XSA Obs. log file obtained successfully and renamed to ${VALID_CCF}/xsaobslog_${now}.txt"
100 fi
101
102 }
103
104
105
106 # +++Function get_ccf_list to list on stdout the table of CCFs in a given CIF.
107 #
108 # Requires that Heasoft is initialised. Otherwise exits with error.
109
110 get_ccf_list()
111 {
112 [ -z "$1" ] && return
113
114 cif_to_process="$1"
115
116 noversion=`which fversion | grep -c no`
117
118 if [ "${noversion}" != "0" ] ; then
119 echo "Error: Heasoft not initialised !"
120 [ -f "${mailfile}" ] && rm -rf ${mailfile}
121 echo "Running of fversion produced the following output:" >> $mailfile
122 echo "$noversion" >> $mailfile
123 mailnotify "mvloccf: Error: Heasoft not initialised!" $mailfile
124 rm ${mailfile}
125 exit
126 fi
127
128 fdump ${cif_to_process} prhead=no showcol=no showrow=no page=no \
129 columns="SCOPE TYPEID ISSUE" \
130 rows=- STDOUT
131
132 }
133
134
135
136
137 # +++ Fill in the valid_constituents directory with the Valid CCF set
138 #
139 # The directory to be filled in is ${VALIDCONSTITUENTSDISR}.
140 # This directory is synchronized with the similar directory in the
141 # external ftp server.
142 #
143 # In addition to that, we also use this function to fill in the
144 # /ccf/valid directory available for users in the intranet.
145
146 fillvalidccfdir()
147 {
148
149 [ -z "$1" ] && return
150
151 validccflist=$1
152
153 if [ ! -f "${VALID_CCF}/${validccflist}" -o ! -s "${VALID_CCF}/${validccflist}" ] ; then
154 echo "Error: ${VALID_CCF}/${validccflist} not found or empty - Abort!"
155 [ -f "${mailfile}" ] && rm -rf ${mailfile}
156 echo "The list of valid CCF ${VALID_CCF}/${validccflist} is not found or it is empty" >> $mailfile
157 mailnotify "mvloccf: Error: ${VALID_CCF}/${validccflist} not found or it is empty - Abort!" $mailfile
158 rm ${mailfile}
159 exit
160 fi
161
162 VALIDCONSTITUENTSDIR="/home/ccflib/ftp-area/valid_constituents"
163 # Checks that the VALIDCONSTITUENTSDIR directory is writable by me
164 touch ${VALIDCONSTITUENTSDIR}/test 2> /dev/null
165 if [ "$?" != "0" ] ; then
166 [ -f "${mailfile}" ] && rm -rf ${mailfile}
167 echo "${VALIDCONSTITUENTSDIR} is not writable. Can not work" >> $mailfile
168 mailnotify "mvloccf: Error: ${VALIDCONSTITUENTSDIR} is not writable - Abort!" $mailfile
169 rm ${mailfile}
170 exit
171 fi
172 rm -rf ${VALIDCONSTITUENTSDIR}/*
173
174 # This operation can only succeed because the subdir valid has enable the write permission
175 # for the group "sas" (g=rwx), provided that CCFs belong to sasbuild(5153):sas.
176 VALIDCCFDIRINTRANET="/ccf/valid"
177 # Checks that the VALIDCCFDIRINTRANET directory is writable by me
178 touch ${VALIDCCFDIRINTRANET}/test 2> /dev/null
179 if [ "$?" != "0" ] ; then
180 [ -f "${mailfile}" ] && rm -rf ${mailfile}
181 echo "${VALIDCCFDIRINTRANET} is not writable. Can not work" >> $mailfile
182 mailnotify "mvloccf: Error: ${VALIDCCFDIRINTRANET} is not writable - Abort!" $mailfile
183 rm ${mailfile}
184 exit
185 fi
186
187 rm -rf ${VALIDCCFDIRINTRANET}/*
188
189 CONSTITUENTSDIR="/home/ccflib/ftp-area/constituents"
190 n=0
191 while read ccf
192 do
193 n=$((n + 1))
194 echo "$n cp -a ${CONSTITUENTSDIR}/${ccf} ${VALIDCONSTITUENTSDIR}/"
195 cp -a ${CONSTITUENTSDIR}/${ccf} ${VALIDCONSTITUENTSDIR}/
196 echo "$n cp -a ${CONSTITUENTSDIR}/${ccf} ${VALIDCCFDIRINTRANET}/"
197 cp -a ${CONSTITUENTSDIR}/${ccf} ${VALIDCCFDIRINTRANET}/
198 done < ${VALID_CCF}/${validccflist}
199
200 if [ "$DEBUG" ] ; then
201 echo "DEBUG: mlovccf: fillvalidccfdir: Processed $validccflist"
202 fi
203 }
204
205
206 #
207 # M a i n p r o g r a m
208 #
209 # 1. Get the list of all ODFs
210
211
212 xsaobslogsize="0"
213
214 getxsaobslog
215
216 xsaobslogsize=`stat --format=%s ${VALID_CCF}/xsaobslog_${now}.txt`
217
218 if [ "$xsaobslogsize" = "0" ] ; then
219 echo "Error: Failure to download the XSA Obs. Log file - Abort"
220 [ -f "${mailfile}" ] && rm -rf ${mailfile}
221 echo "The XSA Obs. Log file has 0 size - Not downloaded?" >> $mailfile
222 echo "." >> $mailfile
223 if [ "$DEBUG" ] ; then
224 echo "DEBUG: mlovccf: main: xsaobslogsize has 0 size. Mail notification being sent!"
225 fi
226 mailnotify "mlovccf: Error: Failure to download the XSA Obs. Log file - Abort" "$mailfile"
227 rm ${mailfile}
228 exit
229 fi
230
231 #
232 # 2. Initialises HEADAS and SAS
233 #
234
235 # Defines SASBUILD_LOCAL depending on host.
236 # Then uses it to build up HEADAS and setup LD_LIBRARY_PATH accordingly.
237
238 case "$host" in
239 sasbld01n|sasbld02n)
240 export SASBUILD_LOCAL="/sasbuild/local/${host}/GNU_CC_CXX_9.2.0"
241 ;;
242 sciggw|scigrid-*)
243 export SASBUILD_LOCAL="/sasbuild/local/sasbld04n/GNU_CC_CXX_9.2.0"
244 ;;
245 *) echo "Error: Unsupported host $host" ; exit 1 ;;
246 esac
247
248 export HEADAS=${SASBUILD_LOCAL}/headas/architecture
249
250 headas_output="${VALID_CCF}/headas_output"
251 source $HEADAS/headas-init.sh > ${headas_output} 2>&1
252
253 [ "$DEBUG" ] && echo "DEBUG: mlovccf: main: Executed Headas initialisation script for $HEADAS"
254
255 # The next statement should not be necessary because the SAS initialisation
256 # provides under $SAS_DIR/libextra all the necessary libraries from GNU C/C++
257 # which were required to build Heasoft. But, in order to make the HEADAS
258 # initialisation independent of the SAS initialisation, we set the
259 # LD_LIBRARY_PATH as it has to be to run Headas by itself without SAS been
260 # ready.
261
262 export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${SASBUILD_LOCAL}/gcc/lib64
263
264
265 headas_ok=`cat ${headas_output}`
266
267 if [ -n "${headas_ok}" ] ; then
268 echo "mlovccf: HEADAS initialisation failed with error" >> ${mailfile}
269 cat ${headas_output} >> ${mailfile}
270 mailnotify "mlovccf: HEADAS initilisation failed" ${mailfile}
271 if [ "$DEBUG" ] ; then
272 echo "DEBUG: mlovccf: main: Headas initialization failed. Notification mail sent!"
273 fi
274 rm -v ${mailfile}
275 rm -v ${headas_output}
276 exit 1
277 fi
278
279 # To be able to mix several jobs on the same ccflib account
280 [ ! -d "${HOME}/pfiles/${host}" ] && mkdir ${HOME}/pfiles/${host}
281 export PFILES="${HOME}/pfiles/${host};${HEADAS}/syspfiles"
282 if [ "$DEBUG" ] ; then
283 echo -n "DEBUG: mlovccf: main: HEADAS version: "`fversion`
284 fi
285
286 #
287 # 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
288 #
289 # The SAS initialisation produced by setsas.sh sets SAS_CCFPATH to /ccf/pub.
290 # Hence, if we set SAS_CCFPATH before running setsas.sh, we will always
291 # use /ccf/pub as repository for CCF. This is right as long as /ccf/pub is in
292 # strict sync with /home/ccflib/ccf_ftp/constituents. But if this is not true,
293 # the resulting valid CCFs might be wrong. Therefore, we set it here explicitly.
294 #
295
296 confsas_output="${VALID_CCF}/confsas_output"
297 source /sas/bin/confsas > ${confsas_output}
298 confsas_ok=`cat ${confsas_output}`
299
300 if [ "$DEBUG" ] ; then
301 echo "DEBUG: mlovccf: main: Executed /sas/bin/confsas with output: ${confsas_ok}"
302 fi
303
304 if [ -n "${confsas_ok}" ] ; then
305 echo "mlovccf: SAS initialization failed with error" >> ${mailfile}
306 cat ${confsas_output} >> ${mailfile}
307 mailnotify "mlovccf: SAS initialisation failed" ${mailfile}
308 if [ "$DEBUG" ] ; then
309 echo "DEBUG: mlovccf: main: SAS initialization failed. Notification mail sent!"
310 fi
311 rm -v ${mailfile}
312 rm -v ${confsas_output}
313 exit 1
314 fi
315
316 source $HOME/setsas.sh 1> /dev/null
317
318 if [ "$DEBUG" ] ; then
319 echo "DEBUG: mlovccf: main: SAS setsas.sh script executed"
320 echo
321 echo "DEBUG: mlovccf: main: SAS Version: "`sasversion -V 1`
322 fi
323
324
325 # Set SAS vervbosity to 0 from now onwards
326
327 export SAS_VERBOSITY=0
328
329
330 # Re-sets SAS_CCFPATH
331
332 export SAS_CCFPATH="/home/ccflib/ftp-area/constituents"
333
334
335
336 # 3. XMM_CALINDEX file from SAS_CCFPATH
337
338 MIF=`ls -1 ${SAS_CCFPATH}/XMM_CALINDEX_*.CCF`
339
340 # 4. For each OBDS ID registered in the XSA observations list, gets the start
341 # time of the observation. Then runs cifbuild to get the respective CIF.
342 # all_ccfs_${now}.txt is filled recursively on each iteration.
343 # Therefore we start with an empty file. The final list will be this one.
344
345 # Output file : ${VALID_CCF}/all_ccfs_${now}.txt
346
347 touch ${VALID_CCF}/all_ccfs_${now}.txt
348
349 n=0
350
351 while read line
352 do
353 obsid=`echo $line | awk -F"|" '{print $2}'`
354 stime=`echo $line | awk -F"|" '{print $9}'`
355 stime=`echo $stime | tr " " "T"`
356 n=$((n+1))
357 echo "($n) Processing $obsid $stime" ; echo
358
359 # Run cifbuild
360
361 cif_file=${VALID_CCF}/${obsid}.cif
362
363 cifbuild --withmasterindexset='yes' \
364 --masterindexset=${MIF} \
365 --withobservationdate='yes' \
366 --observationdate=${stime} \
367 --analysisdate='now' \
368 --calindexset=${cif_file}
369
370
371 # Gets the list of CCFs included in the CIF
372
373 get_ccf_list "${cif_file}" > ${VALID_CCF}/${obsid}_ccfs.tmp
374
375 while read line
376 do
377 [ "$line" = "" ] && continue
378 class=`echo $line | awk -F" " '{print $1"_"$2}'`
379 issue=`echo $line | awk -F" " '{print $3}'`
380 issue=`printf "%04d" ${issue}`
381 echo ${class}_${issue}.CCF >> ${VALID_CCF}/${obsid}_ccfs.txt
382
383 done < ${VALID_CCF}/${obsid}_ccfs.tmp
384
385 rm ${VALID_CCF}/${obsid}_ccfs.tmp
386
387 mv ${VALID_CCF}/all_ccfs_${now}.txt ${VALID_CCF}/all_ccfs_${now}.tmp
388
389 cat ${VALID_CCF}/${obsid}_ccfs.txt ${VALID_CCF}/all_ccfs_${now}.tmp | sort -u > ${VALID_CCF}/all_ccfs_${now}.txt
390
391 comm -1 -3 --nocheck-order ${VALID_CCF}/all_ccfs_${now}.tmp ${VALID_CCF}/all_ccfs_${now}.txt > ${VALID_CCF}/diff_iteration_${n}.txt
392
393 diffsize=`stat --format=%s ${VALID_CCF}/diff_iteration_${n}.txt`
394
395 if [ "$diffsize" != "0" ] ; then
396 echo "CCF added in this iteration: "
397 cat ${VALID_CCF}/diff_iteration_${n}.txt
398 fi
399
400 echo
401 echo "Number of valid CCFs found so far: "`cat ${VALID_CCF}/all_ccfs_${now}.txt | wc -l` ; echo
402
403 rm ${VALID_CCF}/diff_iteration_${n}.txt
404 rm ${cif_file}
405 rm ${VALID_CCF}/${obsid}_ccfs.txt
406 rm ${VALID_CCF}/all_ccfs_${now}.tmp
407
408 done < ${VALID_CCF}/xsaobslog_${now}.txt
409
410 # 5. Make a table of classes and issues with the style "Pipeline Release Notes"
411 # To get a classification, we need first to obtain all available classes.
412 #
413
414
415 # Lists all CCF classes
416
417 ccf_classes="${VALID_CCF}/ccf_classes.txt"
418
419 while read line
420 do
421 class=`echo $line | awk -F"_" '{print $1"_"$2}'`
422 echo $class >> ${ccf_classes}
423 done < ${VALID_CCF}/all_ccfs_${now}.txt
424
425 cat ${ccf_classes} | sort -u > ${VALID_CCF}/kk.txt
426 mv ${VALID_CCF}/kk.txt ${ccf_classes}
427
428
429
430 # Output final file is named all_ccfs_${now}_table.txt
431
432 total_number_of_ccfs=0
433
434 all_ccfs_table="${VALID_CCF}/all_ccfs_${now}_table.txt"
435
436 [ -f "${all_ccfs_table}" ] && rm ${all_ccfs_table}
437
438 echo "Table of valid CCFs at $now_table" >> ${all_ccfs_table}
439 echo >> ${all_ccfs_table}
440 echo "|================================|=============|=======|" >> ${all_ccfs_table}
441 echo "| Calibration File | Issue range | Count |" >> ${all_ccfs_table}
442 echo "|================================|=============|=======|" >> ${all_ccfs_table}
443
444 while read ccf_class
445 do
446 echo -n "Processing class $ccf_class ..."
447 issues_file="${VALID_CCF}/ccf_issues_for_${ccf_class}.txt"
448 [ -f "${issues_file}" ] && rm ${issues_file}
449 while read line
450 do
451 class=`echo $line | awk -F"_" '{print $1"_"$2}'`
452 [ "$class" != "$ccf_class" ] && continue
453 issue=`echo $line | awk -F"_" '{print $3}' | cut -d. -f1`
454 echo $issue >> ${issues_file}
455 done < ${VALID_CCF}/all_ccfs_${now}.txt
456
457 cat ${issues_file} | sort -u > kk.txt
458 mv kk.txt ${issues_file}
459 real_number_of_issues_for_this_class=`wc -l ${issues_file} | awk -F" " '{print $1}'`
460
461 first_issue=`head -1 ${issues_file}`
462 last_issue=`tail -1 ${issues_file}`
463
464 li=$((10#$last_issue))
465 fi=$((10#$first_issue))
466
467 sequential_number_of_issues_for_this_class=$((li - fi + 1))
468
469 asterisk=" "
470 [ "${real_number_of_issues_for_this_class}" -lt "${sequential_number_of_issues_for_this_class}" ] && asterisk="(*)"
471
472 total_number_of_ccfs=$((real_number_of_issues_for_this_class + total_number_of_ccfs))
473
474 rn=$real_number_of_issues_for_this_class
475
476 if [ "${first_issue}" != "${last_issue}" ] ; then
477 if [ "$asterisk" = "(*)" ] ; then
478 printf "| %-30s | %4s-%4s(*)| %4s |\n" ${ccf_class} ${first_issue} ${last_issue} ${rn} >> ${all_ccfs_table}
479 else
480 printf "| %-30s | %4s-%4s | %4s |\n" ${ccf_class} ${first_issue} ${last_issue} ${rn} >> ${all_ccfs_table}
481 fi
482 else
483 printf "| %-30s | %4s | %4s |\n" ${ccf_class} ${first_issue} ${rn} >> ${all_ccfs_table}
484 fi
485
486
487 echo "done"
488 done < ${ccf_classes}
489
490 echo "|================================|=============|=======|" >> ${all_ccfs_table}
491 echo >> ${all_ccfs_table}
492 echo "(*): Incomplete range." >> ${all_ccfs_table}
493 echo "" >> ${all_ccfs_table}
494 echo "Total number of valid CCFs required : ${total_number_of_ccfs}" >> ${all_ccfs_table}
495 rm ${VALID_CCF}/ccf_classes.txt
496 rm ${VALID_CCF}/ccf_issues_for*.txt
497
498
499
500 # 6. Fill in the Valid CCF Set dir with the proper CCFs
501
502 fillvalidccfdir "all_ccfs_${now}.txt"
503
504
505 [ -f "${mailfile}" ] && rm -rf ${mailfile}
506 echo "Valid CCF Set successfully copied to ${VALIDCONSTITUENTSDIR}" >> $mailfile
507 echo "Valid CCF Set successfully copied to ${VALIDCCFDIRINTRANET}" >> $mailfile
508 mailnotify "mvloccf: Valid CCF Set created successfully" $mailfile
509 rm ${mailfile}

  ViewVC Help
Powered by ViewVC 1.1.27