File mttroot/mtt/bin/trans/txt2m artifact 55eece0e52 part of check-in 08597d1206


#! /bin/sh

     ###################################### 
     ##### Model Transformation Tools #####
     ######################################

# Bourne shell script: txt2m
# Converts txt file to matlab file (for numpar and state)

# Copyright (c) P.J.Gawthrop 1998


# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# %% Version control history
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# %% $Id$
# %% $Log$
# %% Revision 1.14  1999/02/16 04:38:22  peterg
# %% Now forces creation of _smx file if METHOD=IMPLICIT in simpar.txt
# %%
# %% Revision 1.13  1998/08/31 11:43:37  peterg
# %% Now lower cases globals in numpar files
# %%
# %% Revision 1.12  1998/08/14 10:47:31  peterg
# %% Put ImplicitS sub before Implicit!!
# %%
# %% Revision 1.11  1998/08/14 10:43:44  peterg
# %% Added ImplicitS - sparse integration
# %%
# %% Revision 1.10  1998/08/11 13:27:51  peterg
# %% Lowercase mttLAST etc
# %%
# %% Revision 1.9  1998/07/30 15:07:17  peterg
# %% Added _ to the disallowed chars around t
# %%
# %% Revision 1.8  1998/07/30 12:52:38  peterg
# %% Adds ; to end of statements
# %% Translates ' to " before removal
# %%
# %% Revision 1.7  1998/07/30 09:32:33  peterg
# %% Replaces:
# %% 	euler by 1
# %% 	implicitl by 1
# %% 	implicit  by 3
# %%
# %% Revision 1.6  1998/07/27 18:59:11  peterg
# %% Added WMIN etc
# %%
# %% Revision 1.5  1998/07/26 11:54:20  peterg
# %% Added  mtt to variables
# %%
# %% Revision 1.4  1998/07/26 09:50:12  peterg
# %% More forgiving of txt sytax.
# %%
# %% -- can use () or ()
# %% -- can use mttx() or x()
# %% -- can use mttu() or u()
# %%
# %% Revision 1.3  1998/07/25 20:40:35  peterg
# %% All vars in lower case now
# %%
# %% Revision 1.2  1998/07/25 09:48:31  peterg
# %% Tidied up for Pascal version
# %%
# %% Revision 1.1  1998/02/25 22:10:25  peterg
# %% Initial revision
# %%
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

system=$1
representation=$2

if [ "$representation" = "state" ]; then
    name='mttx';
    arg='';
fi
if [ "$representation" = "input" ]; then
    name='mttu';
    arg='(mttx,mttt)'
fi
if [ "$representation" = "simpar" ]; then
    # Check if implicit method and generate xmx.m if
    implicit=`grep -i METHOD $1_simpar.txt | grep -i -c IMPLICIT`
    if [ "$implicit" = "1"  ]; then
	mtt -q $1 smx m
    else
echo Creating dummy $1_smx.m
cat<<EOF > $1_smx.m
%% No code as implicit method not used here %%
EOF
    fi
fi

# Inform user
echo Creating $1_$2.m

#Create the $2 file complete with headers.
case $representation in
    simpar)
      echo "% Script file  $1_$2.m" > $1_$2.m
      echo "%% $2 file ($1_$2.m)" >> $1_$2.m
      echo "%% Generated by MTT at `date`" >> $1_$2.m
      echo '% Dummy globals' >>  $1_$2.m
      echo 'global ...' >>  $1_$2.m
      echo 'mtt_dummy_global;' >>  $1_$2.m
      ;;
    numpar)
      echo "% Script file  $1_$2.m" > $1_$2.m
      echo "%% $2 file ($1_$2.m)" >> $1_$2.m
      echo "%% Generated by MTT at `date`" >> $1_$2.m
      echo "% Global variable list" >>  $1_$2.m
      sympar2global_txt2m $1 >> $1_$2.m
      ;;
    *)
      echo "function $name = $1_$2$arg" > $1_$2.m
      echo "%% $2 file ($1_$2.m)" >> $1_$2.m
      echo "%% Generated by MTT at `date`" >> $1_$2.m
      echo "% Global variable list" >>  $1_$2.m
      sympar2global_txt2m $1 >> $1_$2.m
      ;;
esac

#Write out the variables in m format.
cat  $1_$2.txt |\
awk -F#  'BEGIN{
quote = "\047";
doublequote = "\042";
}
{
  N=split($1,a,"=");
  if (N==2) {
    LHS = a[1];
    RHS = a[2];
    gsub(quote, doublequote, RHS);
    sub(/^LAST/, "mttLAST", LHS);
    sub(/^DT/, "mttDT", LHS);
    sub(/^STEPFACTOR/, "mttSTEPFACTOR", LHS);
    sub(/^METHOD/, "mttMETHOD", LHS);
    sub(/^WMIN/, "mttWMIN", LHS);
    sub(/^WMAX/, "mttWMAX", LHS);
    sub(/^WSTEPS/, "mttWSTEPS", LHS);
    sub(/^INPUT/, "mttinput", LHS);
    LHS = tolower(LHS);
    RHS = tolower(RHS);
    sub(/["]*euler["]*/, 1, RHS);   
    sub(/["]*implicit["]*/, 2, RHS);  
    statement = sprintf("%s= %s",LHS,RHS);
    if ( (match(statement,";")==0)&&\
         ((match(statement,"if ")==0))&&\
         ((match(statement,"for ")==0)) )
      statement = sprintf("%s;", statement);
  }
  else if (match($1,"global")==1) {
    statement = tolower($1); # Lower case globals as well
  }
  else
    statement = $1;

  if (NF<2) print statement
  if (NF>1)  print statement " # " $2
}' | sed\
     -e 's/\[\([0-9]*\)\]/(\1)/g' \
     -e 's/\([^a-zA-Z_]\)t\([^a-zA-Z_]\)/\1mttt\2/g' \
     -e 's/x(/mttx(/g' \
     -e 's/u(/mttu(/g' \
     -e 's/mttmtt/mtt/g' \
     -e 's/#/%/g' \
>> $1_$2.m






MTT: Model Transformation Tools
GitHub | SourceHut | Sourceforge | Fossil RSS ]