File mttroot/mtt/bin/trans/txt2m artifact bc8b4abfab part of check-in 0115025df2


#! /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.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

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


if [ "$representation" != "simpar" ]; then
    #Create the $2 file complete with headers.
    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
else
    echo "% Script file  $1_$2.m" > $1_$2.m
    echo '% Dummy globals' >>  $1_$2.m
    echo 'global ...' >>  $1_$2.m
    echo 'mtt_dummy_global;' >>  $1_$2.m
fi


#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 = tolower(a[2]);
    gsub(quote, doublequote, RHS);
    sub(/["]*euler["]*/, 1, RHS);   
    sub(/["]*implicitl["]*/, 2, RHS);   
    sub(/["]*implicit["]*/, 3, RHS);  
    statement = sprintf("%s=%s",LHS,RHS);
    if (match(statement,";")==0)
      statement = sprintf("%s;", statement);
  }
  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/LAST/mttLAST/g' \
     -e 's/DT/mttDT/g' \
     -e 's/STEPFACTOR/mttSTEPFACTOR/g' \
     -e 's/METHOD/mttMETHOD/g' \
     -e 's/WMIN/mttWMIN/g' \
     -e 's/WMAX/mttWMAX/g' \
     -e 's/WSTEPS/mttWSTEPS/g' \
     -e 's/x(/mttx(/g' \
     -e 's/u(/mttu(/g' \
     -e 's/mttmtt/mtt/g' \
>> $1_$2.m






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