#! /bin/sh

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

# Bourne shell script: can_r2m
# Reduce canonical forsm to matlab
# P.J.Gawthrop  January 8th 1998
# Copyright (c) P.J.Gawthrop 1998

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
###############################################################



# Inform user
echo Creating $1_can.m

# Remove the old log file
rm -f can_r2m.log

# Use reduce to accomplish the transformation
reduce >can_r2m.log << EOF

%Read in the definitions file
IN "$1_def.r";

%Read the reduce state-space A,B,C and D matrices file
IN "$1_can.r";

%Set up the number of argument variables to zero in case the user has forgotten
MTTNVar := 0;

%Read in the parameter file
IN "$1_sympar.r";

OUT "$1_can.m";
%Headings - Matlab style
%(Note. The ;; are deleted by for2mat)

write "function [A_c,B_c,C_c,D_c,Con,Obs,K,L,A_comp,B_comp,C_comp] = $1_can;;";
write "%function [A_c,B_c,C_c,D_c,Con,Obs,K,L,A_comp,B_comp,C_comp] = $1_can;;";

write "%Canonical forms and controller design for system $1";
write "%File $1_can.m";
write "%Generated by MTT";
write "";

IF MTTNvar>0 THEN
BEGIN
  write "% Set the parameters";
  write "global ...;;";
  FOR i := 1:MTTNx DO
    write "alpha_c",i, " alpha_o",i, "...;;";
  
  FOR i := 1:MTTNvar DO
    BEGIN
      IF numberp(MTTVar(i,1)) 
      THEN 
      BEGIN
        % Do nowt
      END
      ELSE  
      BEGIN
        write MTTVar(i,1), " ...";
      END;
    END;
    write " ";
END;


%Fortran switches - one line expressions
OFF echo;
ON fort$
cardno!* := 1$
fortwidth!* := 100$
OFF period$

write "MTTA_c = zeros(", MTTNx, ",", MTTNx, ");";
write "MTTB_c = zeros(", MTTNx, ",", MTTNu, ");";
write "MTTC_c = zeros(", MTTNy, ",", MTTNx, ");";
write "MTTD_c = zeros(", MTTNy, ",", MTTNu, ");";

write "MTTCon = zeros(", MTTNx, ",", MTTNx, ");";
write "MTTObs = zeros(", MTTNx, ",", MTTNx, ");";

write "MTTK = zeros(", MTTNy, ",", MTTNx, ");";
write "MTTL = zeros(", MTTNx, ",", MTTNu, ");";

write "MTTA_comp = zeros(", MTTNx, ",", MTTNx, ");";
write "MTTB_comp = zeros(", MTTNx, ",", MTTNy, ");";
write "MTTC_comp = zeros(", MTTNu, ",", MTTNx, ");";


ON NERO;	% Suppress zero elements.
MTTA_c := MTTA_c;
MTTB_c := MTTB_c;
MTTC_c := MTTC_c;
MTTD_c := MTTD_c;

MTTCon := MTTCon; 
MTTObs := MTTObs; 


MTTK := MTTK; 
MTTL := MTTL;

MTTA_comp := MTTA_comp;
MTTB_comp := MTTB_comp;
MTTC_comp := MTTC_comp;
MTTD_comp := MTTD_comp;


OFF FORT;
write "";

SHUT "$1_can.m";
