Back to home page

MITgcm

 
 

    


Warning, /utils/scripts/extract_StD is written in an unsupported language. File is not indexed.

view on githubraw file Latest commit 9acfcdc5 on 2020-02-12 18:01:28 UTC
9acfcdc58c Jeff*0001 #! /usr/bin/env bash
                0002 
                0003 # $Header: /u/gcmpack/MITgcm_contrib/jmc_script/extract_StD,v 1.4 2020/02/11 22:17:43 jmc Exp $
                0004 # $Name:  $
                0005 
                0006 add=0 ; mut=0 ;
                0007 if [ $# -ge 3 ]
                0008 then
                0009  if test $1 = '-a' ; then add=1; shift; fi
                0010  if test $1 = '-s' ; then mut=1; shift; fi
                0011  if test $add = 0  ; then if test $1 = '-a' ; then add=1; shift; fi ; fi
                0012 fi
                0013 
                0014 case $# in
                0015 0|1|2) echo "Usage: `basename $0` [-a][-s] input_fil out_pfix out_sfix [field_name_list]"
                0016        echo "   from ASCII Diagnostics-Stats file (=input_fil) and for each field"
                0017        echo "   (in field_name_list), extract the associated numerical values and"
                0018        echo "   put them in a separated file: {out_pfix}_{VAR}.{out_sfix}"
                0019        echo "   -a : append to output_file ; -s : silent mode "
                0020        echo "   '=' as out_pfix -> take {input_fil} basename "
                0021        exit 9 ;;
                0022     *) ;;
                0023 esac
                0024 
                0025 inpFil=$1
                0026 if test $2 = '='
                0027 then prfx=`basename $inpFil | sed 's/\..*\.txt//'`
                0028 else prfx=$2 ; fi
                0029 sufx=$3
                0030 tmpFil='TTT.'$$
                0031 tmpfil='ttt.'$$
                0032 
                0033 echo 'output pre,sufix:' $prfx','$sufx '; input file:' $inpFil
                0034 if test $# = '3'
                0035 then listV=`sed -n '/^# Fields /s/# Fields       ://p' $inpFil`
                0036 else
                0037  shift; shift; shift; listV=$*
                0038 fi
                0039 if test $mut = 0 ; then
                0040  grep '^# Fields ' $inpFil
                0041  echo 'selected var:' $listV
                0042 #echo 'sufx, tmpFil:' $sufx $tmpFil
                0043 fi
                0044 
                0045 flag=1;
                0046 for vv in $listV
                0047 do
                0048  case $vv in
                0049    'Eta') var='ETAN'     ; vfl=$vv ;;
                0050               'ETAN'     ) var=$vv ; vfl='Eta';;
                0051      'T') var='THETA'    ; vfl=$vv ;;
                0052               'THETA'    ) var=$vv ; vfl='T' ;;
                0053      'S') var='SALT'     ; vfl=$vv  ;;
                0054               'SALT'     ) var=$vv ; vfl='S' ;;
                0055      'U') var='UVEL'     ; vfl=$vv  ;;
                0056               'UVEL'     ) var=$vv ; vfl='U' ;;
                0057      'V') var='VVEL'     ; vfl=$vv  ;;
                0058               'VVEL'     ) var=$vv ; vfl='V' ;;
                0059      'W') var='WVEL'     ; vfl=$vv  ;;
                0060               'WVEL'     ) var=$vv ; vfl='W' ;;
                0061    'Phi') var='PHIHYD'   ; vfl=$vv  ;;
                0062               'PHIHYD'   ) var=$vv ; vfl='Phi';;
                0063    'Et2') var='ETANSQ'   ; vfl=$vv ;;
                0064               'ETANSQ'   ) var=$vv ; vfl='Et2';;
                0065     'T2') var='THETASQ'  ; vfl=$vv ;;
                0066               'THETASQ'  ) var=$vv ; vfl='T2' ;;
                0067     'S2') var='SALTSQ'   ; vfl=$vv  ;;
                0068               'SALTSQ'   ) var=$vv ; vfl='S2' ;;
                0069     'U2') var='UVELSQ'   ; vfl=$vv  ;;
                0070               'UVELSQ'   ) var=$vv ; vfl='U2' ;;
                0071     'V2') var='VVELSQ'   ; vfl=$vv  ;;
                0072               'VVELSQ'   ) var=$vv ; vfl='V2' ;;
                0073     'W2') var='WVELSQ'   ; vfl=$vv  ;;
                0074               'WVELSQ'   ) var=$vv ; vfl='W2' ;;
                0075        *)  var=$vv ; vfl=$vv ;;
                0076  esac
                0077 #echo 'grep -m 1' "^ field : ${var} " $inpFil
                0078 #grep -m 1 "^ field : ${var} " $inpFil
                0079  nLev=`grep -m 1 "^ field : ${var} " $inpFil | sed 's/.*=//'`
                0080  if test ${nLev}'xx' = 'xx'
                0081  then
                0082   echo 'WARNING variable:' $var 'not found in file' $inpFil
                0083  else
                0084 #echo 'nLev=' $nLev
                0085 #- first variable: extract Header, then Iter Numbers:
                0086   if test $flag = 1
                0087   then flag=0
                0088     outFil=${prfx}'_head'.$sufx
                0089     nLin=`sed -n "/^# end of header/=" $inpFil`
                0090     head -$nLin $inpFil > $tmpFil
                0091     if test -f $outFil -a $add = 0 ; then rm -f $outFil ; fi
                0092     if test -f $outFil
                0093     then
                0094       diff $tmpFil $outFil > /dev/null
                0095       out=$?
                0096       if test $out != '0'
                0097       then
                0098         echo 'WARNNING : get Different header file:' $outFil
                0099         cat $tmpFil >> $outFil
                0100       fi
                0101       rm -f $tmpFil
                0102     else mv $tmpFil $outFil
                0103       if test $mut = 0 ; then echo 'extract Header (' $nLin 'lines )  ==> ' $outFil ; fi
                0104     fi
                0105 #----
                0106     outFil=${prfx}'_Iter'.$sufx
                0107     if test $mut = 0 ; then echo 'extract Iteration Nb ==> outFil=' $outFil ; fi
                0108     if test -f $outFil -a $add = 0 ; then rm -f $outFil ; fi
                0109     touch $outFil
                0110     rList=`sed -n '/^# Regions /s/# Regions      ://p' $inpFil`
                0111     for j in $rList ; do jj=$j ; done
                0112     grep "^ field : ${var} " $inpFil | grep "$j ; nb.Lev =" | \
                0113                   sed 's/.*Iter =//' | sed 's/; region.*//g' >> $outFil
                0114   fi
                0115 #----
                0116   outFil=${prfx}'_'$vfl.$sufx
                0117   if test $mut = 0 ; then echo 'extract variable:' $var ', outFil=' $outFil ; fi
                0118 #- echo set of sed instruction to file sedFil :
                0119   sedFil=$tmpFil.$vv
                0120   rm -f $sedFil
                0121   echo "/^ field : $var /{" > $sedFil
                0122   if test $nLev = 1 ; then k=0 ; else k=-1 ; fi
                0123   while [ $k -le $nLev ]
                0124   do
                0125    echo "N" >> $sedFil
                0126    k=`expr $k + 1`
                0127   done
                0128   echo "p" >> $sedFil
                0129   echo "}" >> $sedFil
                0130 #- extract records of variable "var" using sed command & sedFil
                0131   sed -n -f $sedFil $inpFil > $tmpFil
                0132   if test $mut = 0 ; then head -1 $tmpFil ; fi
                0133   if test -f $outFil -a $add = 0 ; then rm -f $outFil ; fi
                0134   touch $outFil
                0135   sed '/^ k /d' $tmpFil | sed '/^ field /d' >> $outFil
                0136   rm -f $sedFil $tmpFil
                0137  fi
                0138 done
                0139 exit