tm5-tmm-store 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. #! /bin/sh
  2. # This script is called if added to the list of postprocessing scripts in your
  3. # main rc file. It is dedicated to the STORAGE of WRITTEN MET FIELDS. It
  4. # requires several "tmm.output.store.*" keys, "tmm.output.dir", and
  5. # "tmm.output"
  6. # It relies on the set of bin/gss* script to tar/zip/move-to-destination
  7. # -------------------------------------------------------------
  8. # --- init
  9. # -------------------------------------------------------------
  10. # leave on error
  11. set -e
  12. # set program name and location:
  13. call="$0 $*"
  14. case $0 in
  15. /* ) script=$0 ;;
  16. * ) script="`pwd`/$0" ;;
  17. esac
  18. bindir=`dirname ${script}`
  19. prog=`basename ${script}`
  20. # -------------------------------------------------------------
  21. # --- help
  22. # -------------------------------------------------------------
  23. DisplayUsage ()
  24. {
  25. ${PAGER:-less} << EOF
  26. NAME
  27. ${prog} - store meteo created by TM5
  28. USAGE
  29. ${prog} <rcfile>
  30. EOF
  31. exit 0
  32. }
  33. # err 'help text'
  34. err ()
  35. {
  36. echo "$1" 1>&2
  37. }
  38. # errit <exit-status>
  39. errit ()
  40. {
  41. err "$prog - ERROR - from : ${call}"
  42. err "$prog - ERROR - Use '$prog --help' for more info."
  43. exit $1
  44. }
  45. # -------------------------------------------------------------
  46. # --- arguments
  47. # -------------------------------------------------------------
  48. rcfile=''
  49. for arg in "$@" ; do
  50. case ${arg} in
  51. -h | --help ) DisplayUsage ;;
  52. -* )
  53. err "$prog - ERROR - unknown option '${arg}'"
  54. errit 1
  55. ;;
  56. * )
  57. if [ -z "${rcfile}" ] ; then
  58. rcfile=${arg}
  59. else
  60. err "$prog - ERROR - unknown argument '${arg}'"
  61. errit 1
  62. fi
  63. ;;
  64. esac
  65. done
  66. # not complete ?
  67. if [ -z "${rcfile}" ] ; then
  68. err "$prog - ERROR - no rcfile specified"
  69. errit 1
  70. fi
  71. # -------------------------------------------------------------
  72. # --- settings
  73. # -------------------------------------------------------------
  74. # info ...
  75. echo "$prog - start"
  76. # location of readrc script:
  77. go_readrc="${bindir}/go_readrc"
  78. # are meteo files actually written ?
  79. tmm_output=`${go_readrc} ${rcfile} 'tmm.output' 'F'`
  80. # if not ...
  81. if [ "${tmm_output}" != "T" ] ; then
  82. # info ...
  83. echo "$prog - no meteo files written; return"
  84. # return without problems ...
  85. exit 0
  86. fi
  87. # directory where TM5 has stored the files:
  88. tmm_output_dir=`${go_readrc} ${rcfile} 'tmm.output.dir' './tmm_output_dir'`
  89. # archive directories (gss)
  90. archives=`${go_readrc} ${rcfile} 'tmm.output.store.archives' ' '`
  91. archives_fc=`${go_readrc} ${rcfile} 'tmm.output.store.archives.fc' ' '`
  92. # messages ?
  93. verboseTF=`${go_readrc} ${rcfile} 'tmm.output.store.verbose' ''`
  94. if [ ${verboseTF} = T ] ; then verbose='true' ; else verbose='' ; fi
  95. # zipper ?
  96. zipper=`${go_readrc} ${rcfile} 'tmm.output.store.zipper' ''`
  97. # gss messages ?
  98. verb=''
  99. verb='--verbose'
  100. # -------------------------------------------------------------
  101. # --- begin
  102. # -------------------------------------------------------------
  103. echo "$prog - store meteo output ..."
  104. # output dir present ?
  105. if [ ! -d ${tmm_output_dir} ] ; then
  106. err "$prog - ERROR - output directory not found:"
  107. err "$prog - ERROR - ${tmm_output_dir}"
  108. errit 1
  109. fi
  110. # change to output directory:
  111. owd=`/bin/pwd`
  112. cd ${tmm_output_dir}
  113. # not stored in tarfiles yet ...
  114. tarfile_latest=''
  115. # loop over files in output buffer:
  116. for mfile in `/bin/ls` ; do
  117. # skip status files, and oro.lsm which never change in ERA-Interim
  118. case ${mfile} in
  119. *.hdf.status ) continue ;;
  120. *.nc.status ) continue ;;
  121. #*oro.nc* ) continue ;;
  122. #*lsm.nc* ) continue ;;
  123. esac
  124. echo "$prog - ${mfile} ..."
  125. # no status file ? then skip
  126. mstatfile="${mfile}.status"
  127. if [ ! -f ${mstatfile} ]; then
  128. echo "$prog - no status file; skip"
  129. continue
  130. fi
  131. # completed files should always replace older ones;
  132. # uncompleted files should only be added if older files do not exist:
  133. if [ "`/bin/cat ${mstatfile}`" = "completed" ]; then
  134. verb_keep='replace'
  135. keep_old_files=''
  136. else
  137. verb_keep='keep'
  138. keep_old_files='--keep-old-files'
  139. fi
  140. # zip the file ?
  141. mfileZ="${mfile}"
  142. if [ -n "${zipper}" ]; then
  143. case ${zipper} in
  144. 'gzip' )
  145. gzip ${mfile}
  146. mfileZ="${mfile}.gz"
  147. ;;
  148. 'bzip2' )
  149. gzip ${mfile}
  150. mfileZ="${mfile}.bz2"
  151. ;;
  152. 'internal' )
  153. # This may be the best option for netCDF files. Instead of
  154. # compressing in the Fortran code, it is more flexible to do it
  155. # here: we can for example remove the unlimited dimension, switch
  156. # to netcdf4_classic, ...
  157. # Switch to netcdf4_classic with efficient (using -s) L1 compression
  158. #err "Internal compression not supported yet"
  159. rm -f aap.nc
  160. # note that we cannot use "nccopy" without specifying the path
  161. # depending on the library loaded (if any!) this can be problematic
  162. # /opt/cray/netcdf/4.4.0/bin/nccopy -k 4 -d 1 -s ${mfile} aap.nc
  163. # here is what works with: load cray-hdf5-parallel/1.8.14 & cray-netcdf-hdf5parallel/4.3.3.1
  164. /opt/cray/netcdf/4.3.0/bin/nccopy -k 4 -d 1 -s ${mfile} aap.nc
  165. mv -f aap.nc ${mfile}
  166. ;;
  167. * )
  168. err "ERROR - unsupported zipper : ${zipper}"
  169. errit 1
  170. ;;
  171. esac
  172. fi
  173. # default destination archives:
  174. archivesX="${archives}"
  175. # forecast files might be send to different archives ...
  176. case ${mfileZ} in
  177. *_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]f[0-9]* )
  178. test "${archives_fc}" && archivesX="${archives_fc}"
  179. ;;
  180. esac
  181. # loop over archives:
  182. for archiveX in ${archivesX} ; do
  183. echo "$prog - archive in ${archiveX} ..."
  184. # different archive methods:
  185. case ${archiveX} in
  186. # if archive ends on '/*.tar'*, then store in tarfiles:
  187. */\*.tar* )
  188. # skip '*.tar' part from archive name:
  189. archive=`/usr/bin/dirname ${archiveX}`
  190. #echo "$prog - tarchive dir ${archive} ..."
  191. # split mfile in 'od-fc-...' and 'uvsp_20001201_21p06' parts:
  192. n=`echo ${mfileZ} | /usr/bin/tr '-' ' ' | /usr/bin/wc -w`
  193. mdir=`echo ${mfileZ} | /usr/bin/cut -d '-' -f -$((${n}-1))`
  194. mfil=`echo ${mfileZ} | /usr/bin/cut -d '-' -f ${n}`
  195. # extract parameter name from mfil: 'uvsp', 'T', etc:
  196. mfil_base=${mfil}
  197. mfil_base=`basename ${mfil_base} '.gz'`
  198. mfil_base=`basename ${mfil_base} '.bz2'`
  199. mfil_base=`basename ${mfil_base} '.hdf'`
  200. mfil_base=`basename ${mfil_base} '.nc'`
  201. tarname=`echo ${mfil_base} | /usr/bin/cut -d '_' -f 1`
  202. # tar file in long format:
  203. # od-fc-xxxx-oro.tar
  204. # od-fc-xxxx-uvsp_200012_21p06.tar
  205. case ${tarname} in
  206. 'oro' | 'lsm' )
  207. tarfile="${mdir}-${tarname}.tar"
  208. ;;
  209. 'srols' )
  210. # extract '200012'
  211. ccyymm=`echo ${mfil_base} | /usr/bin/cut -d '_' -f 2`
  212. tarfile="${mdir}-${tarname}_${ccyymm}.tar"
  213. ;;
  214. * )
  215. # extract '20001201':
  216. ccyymmdd=`echo ${mfil} | /usr/bin/cut -d '_' -f 2`
  217. ccyymmd=`echo ${ccyymmdd} | /usr/bin/cut -c 1-7`
  218. ccyymm=`echo ${ccyymmdd} | /usr/bin/cut -c 1-6`
  219. d=`echo ${ccyymmdd} | /usr/bin/cut -c 8`
  220. # large 3D files ?
  221. case ${tarname} in
  222. 'cld' | 'convec' | 'diffus' | 'mfuv' | 'mfw' | 'q' | 'sp' | 'sub' | 't' | 'tsp' )
  223. # tar per month, 10 days, 5 days ...
  224. case ${archiveX} in
  225. */\*.tar )
  226. # year and month:
  227. tardate=`echo ${ccyymmdd} | /usr/bin/cut -c 1-6`
  228. ;;
  229. */\*.tar10 )
  230. # year, month, 0|1|2|3
  231. tardate=`echo ${ccyymmdd} | /usr/bin/cut -c 1-7`
  232. ;;
  233. */\*.tar5 )
  234. # year, month, 00|05|10|15|20|25|30
  235. case ${d} in
  236. [0-4] ) tardate="${ccyymmd}0" ;;
  237. [5-9] ) tardate="${ccyymmd}5" ;;
  238. esac
  239. ;;
  240. * )
  241. err "$prog - ERROR - unsupported archive for large tar files :"
  242. err "$prog - ERROR - ${archive}"
  243. errit 1
  244. ;;
  245. esac
  246. ;;
  247. * )
  248. # year and month:
  249. tardate=`echo ${ccyymmdd} | /usr/bin/cut -c 1-6`
  250. ;;
  251. esac
  252. # rest of the name (time resolution, extension):
  253. mend=`echo ${mfil} | /usr/bin/cut -d '_' -f 3-`
  254. # rest of the file without extension:
  255. tarend=${mend}
  256. tarend=`basename ${tarend} '.gz'`
  257. tarend=`basename ${tarend} '.bz2'`
  258. tarend=`basename ${tarend} '.hdf'`
  259. tarend=`basename ${tarend} '.nc'`
  260. # name of tarfile:
  261. tarfile="${mdir}-${tarname}_${tardate}_${tarend}.tar"
  262. ;;
  263. esac
  264. # tarfile with full path:
  265. tarfile_full=`echo ${tarfile} | tr '-' '/'`
  266. # unlink previous tar file if necessary:
  267. if [ "${tarfile_latest}" ]; then
  268. # different from current tarfile ?
  269. if [ "${tarfile}" != "${tarfile_latest}" ]; then
  270. # unlink previous tarfile:
  271. echo "$prog - unlink ${tarfile_latest} ..."
  272. ${bindir}/gss ${verb} unlink ${tarfile_latest}
  273. fi
  274. fi
  275. # link tarfile if not present yet:
  276. if [ ! -f ${tarfile} ] ; then
  277. echo "$prog - link ${tarfile} ..."
  278. ${bindir}/gss ${verb} link --new ${archive}/${tarfile_full} ${tarfile}
  279. fi
  280. # add to (linked) tarfile:
  281. echo "$prog - add ${mfileZ} to ${tarfile} (${verb_keep}) ..."
  282. ${bindir}/gss ${verb} ${keep_old_files} tar-update ${tarfile} ${mfileZ}
  283. # store name of current tarfile:
  284. tarfile_latest=${tarfile}
  285. ;;
  286. # store directly (not in tarfile)
  287. * )
  288. # file name with full path:
  289. mfile_full=`echo ${mfileZ} | tr '-' '/'`
  290. # test on existing file ?
  291. if [ "${keep_old_files}" ]; then
  292. if ( ${bindir}/gss exist ${archiveX}/${mfile_full} ) ; then
  293. echo "$prog - keep existing ${archiveX}/${mfile_full} ..."
  294. else
  295. # copy:
  296. echo "$prog - copy to ${archiveX}/${mfile_full} (${verb_keep}) ..."
  297. ${bindir}/gss ${verb} copy --mkdir ${mfileZ} ${archiveX}/${mfile_full}
  298. fi
  299. else
  300. # copy, eventually overwrite existing file:
  301. echo "$prog - copy to ${archiveX}/${mfile_full} (${verb_keep}) ..."
  302. ${bindir}/gss ${verb} copy --mkdir ${mfileZ} ${archiveX}/${mfile_full}
  303. fi
  304. ;;
  305. esac # tarfile or not
  306. done # loop over archives
  307. # remove meteo and status file:
  308. rm -f ${mfileZ}
  309. rm -f ${mstatfile}
  310. done # loop over meteo files
  311. # unlink tarfile if necessary:
  312. if [ "${tarfile_latest}" ]; then
  313. # unlink (store on tape ?)
  314. echo "$prog - unlink ${tarfile_latest} ..."
  315. ${bindir}/gss ${verb} unlink ${tarfile_latest}
  316. fi
  317. # change back:
  318. cd ${owd}
  319. # -------------------------------------------------------------
  320. # --- end
  321. # -------------------------------------------------------------
  322. echo "$prog - end"
  323. # ok
  324. exit 0