#!/bin/bash if [ ! -x /usr/bin/awk ]; then echo ">> msr2data: Please make sure you have installed awk!" exit 1 fi if [ "$#" -lt 2 ] ; then cat <> msr2data: Syntax error - you used the list specification without closing bracket (])! Quitting now." echo exit 1 fi EXTENSION=$( eval echo \${$((RIGHTBRACKET + 1))} ) FIRST_OPT_PAR=$((RIGHTBRACKET + 2)) LAST_OPT_PAR=$# RUNLIST_USED=0 NUM_OF_IND_VAR=1 # 1 for the run number else ISDIGIT1=$( echo $1 | awk '{ if (( $1 + 0 ) == $1 ) {print 1} else {print 0}}' ) # check if the first parameter is a number or not ISDIGIT2=$( echo $2 | awk '{ if (( $1 + 0 ) == $1 ) {print 1} else {print 0}}' ) # check if the second parameter is a number or not if [ "$ISDIGIT1" -eq 1 ] && [ "$ISDIGIT2" -eq 1 ]; then # start and end-runs are given if [ "$1" -le "$2" ]; then # runs are given with increasing run numbers for (( j=0; j<=$2-$1; j++ )) do RUNARRAY[$j]=$(( $1 + $j )) done LASTRUN=$(( $2 - $1 )) # RUNARRAY-index of the last run else # runs are given with decreasing run numbers for (( j=0; j<=$1-$2; j++ )) do RUNARRAY[$j]=$(( $1 - $j )) done LASTRUN=$(( $1 - $2 )) # RUNARRAY-index of the last run fi EXTENSION=$3 FIRST_OPT_PAR=4 LAST_OPT_PAR=$# RUNLIST_USED=0 NUM_OF_IND_VAR=1 # 1 for the run number elif [ "$ISDIGIT1" -eq 1 ] && [ "$ISDIGIT2" -eq 0 ]; then # only one run number is given RUNARRAY[0]=$1 LASTRUN=0 # RUNARRAY-index of the last run EXTENSION=$2 FIRST_OPT_PAR=3 LAST_OPT_PAR=$# RUNLIST_USED=0 NUM_OF_IND_VAR=1 # 1 for the run number else # assume a runlist-file with "independent variables" is given if [ ! -r $1 ]; then echo echo ">> msr2data: The specified runlist file $1 is not present... Bye bye!" echo exit 1 fi numberOfReadInLines=0 numberOfComments=0 exec 3< $1 # open file for reading in the runlist-file the first time and get the runnumbers to be processed while read -u 3 -a runList do if [ "$numberOfReadInLines" -eq 0 ]; then NUM_OF_IND_VAR=${#runList[@]} elif [ "${runList[0]:0:1}" == "#" ] || [ "${runList[0]}" == "" ]; then ((numberOfComments++)) else RUNARRAY[(( numberOfReadInLines - numberOfComments - 1 ))]=${runList[0]} fi ((numberOfReadInLines++)) done exec 3<&- # close file LASTRUN=$(( ${#RUNARRAY[@]} - 1 )) # RUNARRAY-index of the last run EXTENSION=$2 FIRST_OPT_PAR=3 LAST_OPT_PAR=$# RUNLIST_USED=1 fi fi # Check if fitting should be done TEMP="" for PAR in "$@" do if [ "${PAR:0:3}" == "fit" ]; then TEMP=${PAR:4} break fi done # define which file (msr or mlog) contains the musrfit-output INPUTEXT=mlog OUTPUTEXT=msr if [ "$TEMP" != "" ]; then # runs should be fitted using musrfit SETBATCH=0 # check if the TEMPLATE file should ALWAYS be the template or only for the first run if [ "${TEMP:(-1)}" == "!" ]; then SETBATCH=1 TEMP=${TEMP%"!"} fi # Check for musrfit on the PATH and if found continue with searching the fit-template PATHTOMUSRFIT=$(echo `which musrfit`) if [ "$PATHTOMUSRFIT" == "" ]; then echo echo ">> msr2data: No musrfit executable was found on the PATH or in the current directory!" echo ">> msr2data: Please install musrfit first!" echo exit 1 else if [ -r $TEMP$EXTENSION.$OUTPUTEXT ]; then TEMPLATE=$TEMP$EXTENSION.$OUTPUTEXT elif [ -r $TEMP$EXTENSION.$INPUTEXT ]; then TEMPLATE=$TEMP$EXTENSION.$INPUTEXT fi fi fi for (( q=0; q<=LASTRUN; q++ )) # loop over all RUNARRAY entries - the actual data-processing do COUNT=${RUNARRAY[$q]} NEXTCOUNT=${RUNARRAY[ (( q + 1 )) ]} NEXTINPUT=$NEXTCOUNT$EXTENSION.msr FIRSTINPUT=${RUNARRAY[0]}$EXTENSION.msr if [ "$TEMP" != "" ]; then # if fitting should be done if [ "$TEMPLATE" == "" ]; then echo echo ">> msr2data: The specified musrfit template files $TEMP$EXTENSION.$OUTPUTEXT and $TEMP$EXTENSION.$INPUTEXT do not exist!" echo exit 1 fi # if it's the first run to be fitted, take the template and substitute the runnumber to match the first run if [ "$q" -eq 0 ] && [ "$TEMPLATE" != "$FIRSTINPUT" ]; then awk -v count=$TEMP -v nextcount=${RUNARRAY[0]} -v nextinput=$FIRSTINPUT '{ if(NR==1) print nextcount > nextinput else if($1 == "RUN") { if (substr(count,1,1) != "_"){ count = sprintf("_%04u", count); nextcount = sprintf("_%04u", nextcount);} if ($2 !~ count){ print " " print ">> msr2data: ATTENTION The run-numbers in the filename and the RUN-block of the template file do not match!" print ">> msr2data: The run-number substitution is most likely going wrong!" print " " } sub(count, nextcount, $0); print $0 >> nextinput } else print $0 >> nextinput }' $TEMPLATE fi MUSRFITPARAM="" for PAR in "$@" do if [ "$PAR" == "-k" ]; then MUSRFITPARAM="-k" break fi done echo echo ">> msr2data: musrfit $COUNT$EXTENSION.msr $MUSRFITPARAM" echo musrfit $COUNT$EXTENSION.msr $MUSRFITPARAM # the fit if [ "$q" -ne "$LASTRUN" ]; then # create the next musrfit input file if not the last specified run was fitted already if [ "$SETBATCH" -eq 1 ]; then # check if the TEMPLATE should be ALWAYS used or only the first time and use the according file COUNTX=$TEMP TEMPLATEX=$TEMPLATE else COUNTX=$COUNT TEMPLATEX=$COUNT$EXTENSION.$OUTPUTEXT fi if [ "$TEMPLATEX" != "$NEXTINPUT" ]; then awk -v count=$COUNTX -v nextcount=$NEXTCOUNT -v nextinput=$NEXTINPUT '{ if(NR==1) print nextcount > nextinput else if($1 == "RUN") { if (substr(count,1,1) != "_"){ count = sprintf("_%04u", count); nextcount = sprintf("_%04u", nextcount);} if ($2 !~ count){ print " " print ">> msr2data: ATTENTION The run-numbers in the filename and the RUN-block of the file do not match!" print ">> msr2data: The run-number substitution is most likely going wrong!" print " " } sub(count, nextcount, $0); print $0 >> nextinput } else print $0 >> nextinput }' $TEMPLATEX fi fi fi if [ ! -r $COUNT$EXTENSION.$OUTPUTEXT ]; then echo echo ">> msr2data: The specified musrfit output file $COUNT$EXTENSION.$OUTPUTEXT does not exist!" echo exit 1 fi # if the "nosummary"-option is not set and the summary files are present, get some information from there NOSUMMARY=0 for PAR in "$@" do if [ "$PAR" == "nosummary" ]; then NOSUMMARY=1 break fi done if [ "$NOSUMMARY" -eq 0 ]; then if [ -d $SUMMDIR ]; then # extract runnumber from mlog/msr-file RUNNUMBER=$( awk '{ if(NR==1) X=0 if($1 ~ /^RUN/ && X==0) { split($2, runNumber, "_") if(runNumber[1] ~ /lem/ && runNumber[2] != "his") {print runNumber[2] } if(runNumber[1] ~ /lem/ && runNumber[2] == "his") {print substr(runNumber[3],1,4) } X=1 } }' $COUNT$EXTENSION.$OUTPUTEXT ) # extract runyear from mlog/msr-file in case of LEM-data RUNYEAR=$( awk '{ if(NR==1) X=0 if($1 ~ /^RUN/ && X==0) { split($2, runNumber, "_") if(runNumber[1] ~ /lem/) {print substr(runNumber[1],length(runNumber[1])-1)} else print "XX" X=1 } }' $COUNT$EXTENSION.$OUTPUTEXT ) if [ "$RUNYEAR" != "XX" ]; then # in case of LEM-data search the summary-file SUMMFILE=$SUMMDIR/20$RUNYEAR/lem$RUNYEAR\_$RUNNUMBER.summ TITLE=$( awk '{ if(NR==4) print $0 }' $SUMMFILE ) ENERGY=$( awk '{ if($0 ~ /implantation energy/) print $(NF-1)}' $SUMMFILE ) TEMPERATURE=$( awk '{ if($1 == "Sample_CF1") print $(NF-1)}' $SUMMFILE ) RALRAR=$( awk '{ if($5 == "RA-L") RAL=$7; if($1 == "RA-R") { RAR=$3; print RAL-RAR; nextfile } }' $SUMMFILE ) RATRAB=$( awk '{ if($5 == "RA-T") RAT=$7; if($1 == "RA-B") { RAB=$3; print RAT-RAB; nextfile } }' $SUMMFILE ) TRANSPORT=$( awk '{ if($1 == "Moderator") print $3}' $SUMMFILE ) fi fi fi # determine the script-output file format, default: db FORMAT=db OUTFILE="out.db" for PAR in "$@" do if [ "$PAR" == "data" ]; then FORMAT=dat OUTFILE="out.dat" break fi done for (( l="$FIRST_OPT_PAR"; l<="$LAST_OPT_PAR"; l++ )) do CUR_PAR=$( eval echo \${$l} ) NEXT_PAR=$( eval echo \${$((l+1))} ) if [ "${CUR_PAR:0:2}" == "-o" ]; then if [ "$CUR_PAR" == "-o" ]; then if [ "$l" -ne "$LAST_OPT_PAR" ] && [ "$NEXT_PAR" != "noheader" ] && [ "$NEXT_PAR" != "nosummary" ] \ && [ "${NEXT_PAR:0:3}" != "fit" ] && [ "$NEXT_PAR" != "-k" ]; then OUTFILE=$NEXT_PAR break else echo echo ">> msr2data: You did not specify an output file! The default one ($OUTFILE) will be used." echo fi else OUTFILE=${CUR_PAR:2} break fi fi done # determine, if the header should be written to the output file, default: YES NOHEADER=0 for PAR in "$@" do if [ "$PAR" == "noheader" ]; then NOHEADER=1 break fi done # put together all data to db- or column-format and write it to the file awk -v outfile=$OUTFILE -v noheader=$NOHEADER -v runList=$RUNLIST_USED -v numIndVar=$NUM_OF_IND_VAR -v format=$FORMAT '{ if(tolower(outfile) == "none") exit 0 FS = " " OFS = " " if(NR==1){title=$0; X=0; Y=0; i=1} if(NR>1){ if(X == 0){ if($1 == i){ parArray[i] = $2 dataArray[i] = $3 if($4 ~ /^-/) negErrArray[i] = substr($4, 2) else negErrArray[i] = $4 if($5 ~ /none/) posErrArray[i] = $4 else posErrArray[i] = $5 i++ } if($1 ~ /^THEORY/) X=1 } } if($1 ~ /^RUN/ && X==1){ split($2, runNumber, "_"); X=2 } if(($1 ~ /^chisq/ || $1 ~ /^maxLH/) && X==2){ if($1 ~ /^maxLH/){ parArray[i] = "maxLH" parArray[i+2] = "maxLHred" } else { parArray[i] = "CHISQ" parArray[i+2] = "CHISQred" } parArray[i+1] = "NDF" parArray[i+3] = "RUN" sub(/,/, "", $3); sub(/,/, "", $6); dataArray[i] = $3 dataArray[i+1] = $6 dataArray[i+2] = $9 for (j in runNumber) { if ( runNumber[j] ~ /^0/ ) { sub(/0+/, "", runNumber[j]) } if (int(runNumber[j]) == runNumber[j]) { dataArray[i+3] = runNumber[j] break } else dataArray[i+3] = "0000" } negErrArray[i] = "0" negErrArray[i+1] = "0" negErrArray[i+2] = "0" negErrArray[i+3] = "0" posErrArray[i] = "0" posErrArray[i+1] = "0" posErrArray[i+2] = "0" posErrArray[i+3] = "0" # Read in the runlist-variable names if(runList == 1){ getline line < "'"$1"'" split(line, indVar, " ") } # Output to db-file in the case, the LEM summary file is accessible and the option "nosummary" is not set if(format != "dat") { if("'"$TEMPERATURE"'" != "") { # Write the db-header if("'"$q"'" == 0 && noheader != 1){ print "TITLE" >> outfile print ">>>Put your title here<<<\n" >> outfile print "Abstract" >> outfile print ">>>Put your abstract here<<<\n" >> outfile print "LABELS" >> outfile print "T (K)" >> outfile print "Tr (kV)" >> outfile print "E (keV)" >> outfile print "RAL-RAR (kV)" >> outfile print "RAT-RAB (kV)" >> outfile if(runList == 1){ for(k=2;k<=numIndVar;k++){print indVar[k] >> outfile} } for(k=1;k> outfile } $1 = "Data" $2 = "T" $3 = "Tr" $4 = "E" $5 = "RALRAR" $6 = "RATRAB" for(k=1;k> outfile print "\\-e" >> outfile } # Write the data-block print "T = " "'"$TEMPERATURE"'" ",,,\\" >> outfile print "Tr = " "'"$TRANSPORT"'" ",,,\\" >> outfile if ("'"$ENERGY"'" != "") print "E = " "'"$ENERGY"'" ",,,\\" >> outfile else print "E = -999,,,\\" >> outfile if ("'"$RALRAR"'" != "") print "RALRAR = " "'"$RALRAR"'" ",,,\\" >> outfile else print "RALRAR = -999,,,\\" >> outfile if ("'"$RATRAB"'" != "") print "RATRAB = " "'"$RATRAB"'" ",,,\\" >> outfile else print "RATRAB = -999,,,\\" >> outfile if(runList == 1) { # process the "independent variables" from the file while((getline line < "'"$1"'") > 0){ split(line, indVarValue, " ") if (indVarValue[1] == "'"$COUNT"'"){ for(k=2;k<=numIndVar;k++){print indVar[k] " = " indVarValue[k] ",,,\\" >> outfile} break } } } for(l=1;l> outfile } print dataArray[i+3] ",,, " "'"$TITLE"'" >> outfile if("'"$q"'" == "'"$LASTRUN"'") print "\n" >> outfile } # If we do not have the information from the summary file or "nosummary" is set else { if("'"$q"'" == 0 && noheader != 1){ print "TITLE" >> outfile print ">>>Put your title here<<<\n" >> outfile print "Abstract" >> outfile print ">>>Put your abstract here<<<\n" >> outfile print "LABELS" >> outfile if(runList == 1){ for(k=2;k<=numIndVar;k++){print indVar[k] >> outfile} } for(k=1;k> outfile } $1 = "Data" for(k=1;k> outfile print "\\-e" >> outfile } if(runList == 1) { while((getline line < "'"$1"'") > 0){ split(line, indVarValue, " ") if (indVarValue[1] == "'"$COUNT"'"){ for(k=2;k<=numIndVar;k++){print indVar[k] " = " indVarValue[k] ",,,\\" >> outfile} break } } } for(l=1;l> outfile } print dataArray[i+3] ",,, " title >> outfile if("'"$q"'" == "'"$LASTRUN"'") print "\n" >> outfile } } # output to a data-file with simple column-structure # first if the LEM-summary files are present and "nosummary" has not been set else { if("'"$TEMPERATURE"'" != "") { # Write the dat-header if("'"$q"'" == 0 && noheader != 1){ $1 = "T" $2 = "Tr" $3 = "E" $4 = "RALRAR" $5 = "RATRAB" for(k=1;k> outfile } # Write the data-block $1 = "'"$TEMPERATURE"'" $2 = "'"$TRANSPORT"'" if ("'"$ENERGY"'" != "") { $3 = "'"$ENERGY"'" } else { $3 = "-999" } if ("'"$RALRAR"'" != "") { $4 = "'"$RALRAR"'" } else { $4 = "-999" } if ("'"$RATRAB"'" != "") { $5 = "'"$RATRAB"'" } else { $5 = "-999" } if(runList == 1) { # process the "independent variables" from the file while((getline line < "'"$1"'") > 0){ split(line, indVarValue, " ") if (indVarValue[1] == "'"$COUNT"'"){ for(k=2;k<=numIndVar;k++){ $(k+4) = indVarValue[k] } break } } } for(l=1;l> outfile if("'"$q"'" == "'"$LASTRUN"'") print "\n" >> outfile } # If we do not have the information from the summary file or "nosummary" is set # Writing to column-data file else { if("'"$q"'" == 0 && noheader != 1){ for(k=1;k> outfile } # Write the data-block if(runList == 1) { # process the "independent variables" from the file while((getline line < "'"$1"'") > 0){ split(line, indVarValue, " ") if (indVarValue[1] == "'"$COUNT"'"){ for(k=2;k<=numIndVar;k++){ $(k-1) = indVarValue[k] } break } } } for(l=1;l> outfile if("'"$q"'" == "'"$LASTRUN"'") print "\n" >> outfile } } print " " print ">> msr2data: The parameter data of " ARGV[1] " have been appended to " outfile } if($0 ~ /*** FIT DID NOT CONVERGE ***/ && X==2){ print " " print ">> msr2data: ATTENTION At least one of the performed fits has not been converged!" print ">> msr2data: The data of " ARGV[1] " has not been included in " outfile "!" print " " } }' $COUNT$EXTENSION.$OUTPUTEXT done fi