download_hsm 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #!/usr/bin/env bash
  2. set -exf
  3. ###########################################
  4. # get USER ID on BSC for differnet CFU users #
  5. # You have to be able to login in to BSC with the command "ssh mn" without using password
  6. ###########################################
  7. case $USER in
  8. fdoblasreyes)
  9. MN_USER='ecm86998'
  10. ;;
  11. huidu)
  12. MN_USER='ecm86133'
  13. ;;
  14. jgarcia)
  15. MN_USER='ecm86734'
  16. ;;
  17. masif)
  18. MN_USER='ecm86010'
  19. ;;
  20. omula)
  21. MN_USER='ecm86843'
  22. ;;
  23. vguemas)
  24. MN_USER='ecm86859'
  25. ;;
  26. acarrassi)
  27. MN_USER='ecm86014'
  28. ;;
  29. esac
  30. #######################
  31. ## get arguments
  32. ######################
  33. while getopts e:s:m:t:f:g: option
  34. do
  35. case $option in
  36. e) exp_id=$OPTARG;;
  37. s) sdate=$OPTARG;;
  38. m) mem=$OPTARG;;
  39. t) data_type=$OPTARG;;
  40. f) file=$OPTARG;;
  41. g) filetype=$OPTARG;;
  42. \?) exit 1;;
  43. esac
  44. done
  45. ######################
  46. ## functions
  47. ######################
  48. get_sdate(){
  49. sdate_lst=`ssh mn ssh login8 ls $BASE_DIR/$1`
  50. }
  51. get_member(){
  52. member_lst=`ssh mn ssh login8 ls $BASE_DIR/$1/$2`
  53. }
  54. grab_data(){ # this will download all the data for one specific member
  55. sdate=$1
  56. mem=$2
  57. file_type=$3
  58. HSM_DIR=/HSM/ecm86/exp/${exp_id}/${sdate}/${mem}/outputs
  59. TARGET_DIR=/cfunas/exp/ecearth/${exp_id}/${sdate}/${mem}/outputs
  60. mkdir -p ${TARGET_DIR}
  61. file_list=`ssh mn ssh login8 find $HSM_DIR -type f -iname "ICM${file_type}*.grb"`
  62. if [[ ${#file_list[@]} == 0 ]]; then #IV
  63. echo "There are no elements in the given PATH: $1"
  64. return 1
  65. fi #IV
  66. for f in ${file_list[@]};do
  67. f1=`basename $f`
  68. ssh mn rsync login7:$f $MN_CONTAINER/ # HSM to SCRATCH of BSC
  69. rsync mn:$MN_CONTAINER/$f1 ${TARGET_DIR}/ # SCRATCH of BSC to cfunas
  70. ssh mn rm $MN_CONTAINER/$f1 # clean BSC
  71. done #loop for files
  72. }
  73. grab_one_file(){
  74. sdate=$1
  75. mem=$2
  76. f=$3
  77. HSM_DIR=/HSM/ecm86/exp/${exp_id}/${sdate}/${mem}/outputs
  78. TARGET_DIR=/cfunas/exp/ecearth/${exp_id}/${sdate}/${mem}/outputs
  79. mkdir -p ${TARGET_DIR}
  80. ssh mn rsync login7:$HSM_DIR/$f $MN_CONTAINER/ # HSM to SCRATCH of BSC
  81. rsync mn:$MN_CONTAINER/$f ${TARGET_DIR}/ # SCRATCH of BSC to cfunas
  82. ssh mn rm $MN_CONTAINER/$f # clean BSC
  83. }
  84. print_help(){
  85. echo
  86. echo "File type (netcdf or grb) have to be specified: -g <filetype>, where filetype is either netcdf or grb"
  87. echo "USAGE: $(basename $0) -g <filetype> -e <exp_id> -s <startdate> -m <mem> -t <data_type>"
  88. echo "If <filetype> is grb file, <data_type> has to be either sh or gg"
  89. echo "OR: $(basename $0) -f <filename>"
  90. echo "if no filename (which have to including the information of sdates and member) is specify, <exp_id> is required, in this case, all outputs, restart and log files will be downloaded"
  91. echo "<data_type> can be mm/mmo/mma/rest/resta/resto/rest/log/diags/sh/gg"
  92. echo
  93. }
  94. if [[ $filetype == '' ]];then
  95. print_help
  96. exit 1
  97. else
  98. case $filetype in
  99. nc)
  100. if [[ $file == '' ]] && [[ $exp_id == '' ]];then
  101. print_help
  102. exit 1
  103. fi
  104. ;;
  105. grb)
  106. if [[ $file != '' ]]; then
  107. if [[ $sdate == '' ]] || [[ $mem == '' ]];then
  108. echo
  109. echo "You have to specify both the starting date and member for this grb file"
  110. echo
  111. fi
  112. fi
  113. ;;
  114. esac
  115. fi
  116. ##################
  117. # Constants #
  118. ##################
  119. BASE_DIR='/HSM/ecm86/exp'
  120. MN_CONTAINER="/gpfs/scratch/ecm86/${MN_USER}/container/"
  121. HSM_NODE='login8'
  122. HSM_DIR=/${BASE_DIR}/${exp_id}/${sdate}/${mem}/
  123. #####################
  124. # create directoris #yy
  125. #####################
  126. ssh mn "mkdir -p ${MN_CONTAINER}"
  127. ##################
  128. # main scripts
  129. ##################
  130. case $filetype in
  131. grb)
  132. if [[ $file == '' ]]; then
  133. if [[ $sdate == '' ]];then #I
  134. get_sdate ${exp_id}
  135. if [[ $mem == '' ]];then #II
  136. for sdate in ${sdate_lst[@]};do
  137. get_member ${exp_id} ${sdate}
  138. for mem in ${member_lst};do
  139. grab_data ${sdate} ${mem} ${data_type}
  140. done # loop for member
  141. done #loop for starting dates
  142. else
  143. for sdate in ${sdate_lst[@]};do
  144. grab_data ${sdate} ${mem} ${data_type}
  145. done #loop for starting dates
  146. fi #II
  147. else #I
  148. if [[ $mem == '' ]];then
  149. get_member ${exp_id} $sdate
  150. for mem in ${member_lst};do
  151. grab_data ${sdate} ${mem} ${data_type}
  152. done # loop for member
  153. else
  154. grab_data ${sdate} ${mem} ${data_type}
  155. fi
  156. fi #I
  157. else
  158. if [[ $sdate == '' ]] || [[ $mem == '' ]];then
  159. echo "You have to specify both sdate and mem in this casei"
  160. return 1
  161. else
  162. grab_one_file $sdate $mem $file
  163. fi
  164. fi
  165. ;;
  166. nc)
  167. if [ -z "$file" ]; then
  168. file_list=`ssh mn ssh login8 find $HSM_DIR -type f -iname "$data_type*.tar"`
  169. else
  170. exp_id=`echo $file|cut -f2 -d'_'`
  171. file_list=`ssh mn ssh login8 find $BASE_DIR/${expid} -type f -iname "$file"`
  172. fi
  173. # file_list=`ssh mn ssh login8 find $HSM_DIR -type f -iname "$data_type*"`
  174. if [[ ${#file_list[@]} == 0 ]]; then
  175. echo "here are no elements in the given MMO PATH: $1"
  176. return 1
  177. fi
  178. for f in ${file_list[@]};do
  179. f1=`basename $f`
  180. type=`echo $f1|cut -f1 -d'_'`
  181. sdate=`echo $f1|cut -f3 -d'_'`
  182. mem=`echo $f1|cut -f4 -d'_'`
  183. echo $type $sdate $mem
  184. case $type in
  185. 'RESTA'|'RESTO')
  186. TARGET_DIR=/cfunas/exp/ecearth/${exp_id}/${sdate}/${mem}/restarts
  187. ;;
  188. 'MMA'|'MMO'|'diags')
  189. TARGET_DIR=/cfunas/exp/ecearth/${exp_id}/${sdate}/${mem}/outputs
  190. ;;
  191. 'logfile')
  192. TARGET_DIR=/cfunas/exp/ecearth/${exp_id}/${sdate}/${mem}/log
  193. ;;
  194. esac
  195. if [[ ! -d "$TARGET_DIR" ]]; then
  196. mkdir -p $TARGET_DIR
  197. fi
  198. ssh mn rsync login7:$f $MN_CONTAINER/ # HSM to SCRATCH of BSC
  199. rsync mn:$MN_CONTAINER/$f1 ${TARGET_DIR}/ # SCRATCH of BSC to cfunas
  200. ssh mn rm $MN_CONTAINER/$f1 # clean BSC
  201. done
  202. ;;
  203. esac