Index: branches/UKMO/dev_r5518_v3.4_asm_nemovar_community/NEMOGCM/EXTERNAL/AGRIF/LIB/Writedeclarations.c
===================================================================
--- branches/UKMO/dev_r5518_v3.4_asm_nemovar_community/NEMOGCM/EXTERNAL/AGRIF/LIB/Writedeclarations.c	(revision 6625)
+++ branches/UKMO/dev_r5518_v3.4_asm_nemovar_community/NEMOGCM/EXTERNAL/AGRIF/LIB/Writedeclarations.c	(revision 6626)
@@ -50,60 +50,59 @@
 /*                                                                            */
 /******************************************************************************/
-void WriteBeginDeclaration(variable *v,char ligne[LONG_4C], int visibility)
-{
-  char tmpligne[LONG_4C];
+void WriteBeginDeclaration(variable *v, char line[LONG_M], int visibility)
+{
+  char tmpligne[LONG_M];
+  int precision_given ;
 
   if ( !strcasecmp(v->v_typevar,"") )
   {
-     printf("WARNING : The type of the variable %s \n",v->v_nomvar);
-     printf("          is unknown. CONV should define a type\n");
-  }
-  
-  sprintf (ligne, "%s", v->v_typevar);
-  if ( v->v_c_star == 1 ) strcat(ligne,"*");
-  
+     printf("# WARNING : The type of the variable %s is unknown.\n", v->v_nomvar);
+     printf("#           CONV should define a type\n");
+  }
+
+  sprintf(line, "%s", v->v_typevar);
+  if ( v->v_c_star == 1 ) strcat(line, "*");
+
   /* We should give the precision of the variable if it has been given        */
+  precision_given = 0;
   if ( strcasecmp(v->v_precision,"") )
   {
-     sprintf(tmpligne,"(%s)",v->v_precision);
-     Save_Length(tmpligne,49);
-     strcat(ligne,tmpligne);
-  }
-  
+     sprintf(tmpligne, "(%s)", v->v_precision);
+     Save_Length(tmpligne, 49);
+     strcat(line, tmpligne);
+     precision_given = 1;
+  }
+
   if (strcasecmp(v->v_dimchar,""))
   {
      sprintf(tmpligne,"(%s)",v->v_dimchar);
-     Save_Length(tmpligne,49);
-     strcat(ligne,tmpligne);
-  }
-  
-  if ( strcasecmp(v->v_nameinttypename,"") )
+     Save_Length(tmpligne, 49);
+     strcat(line,tmpligne);
+  }
+
+  if ((precision_given == 0) && ( strcasecmp(v->v_nameinttypename,"") ))
   {
      sprintf(tmpligne,"*%s",v->v_nameinttypename);
-     Save_Length(tmpligne,49);
-     strcat(ligne,tmpligne);
+     Save_Length(tmpligne, 49);
+     strcat(line,tmpligne);
   }
   if (strcasecmp (v->v_IntentSpec, ""))
   {
-     sprintf(tmpligne,",INTENT(%s) ",v->v_IntentSpec);
-     Save_Length(tmpligne,49);
-     strcat(ligne,tmpligne);
-  }
-  if ( v->v_VariableIsParameter == 1 ) strcat(ligne, ", PARAMETER");
-  if (visibility == 1)
-  {
-  if ( v->v_PublicDeclare       == 1 ) strcat(ligne, ", PUBLIC");
-  if ( v->v_PrivateDeclare      == 1 ) strcat(ligne, ", PRIVATE");
-  }
-  if ( v->v_ExternalDeclare     == 1 ) strcat(ligne, ", EXTERNAL");
-  if ( v->v_allocatable         == 1)
-       {strcat(ligne,", ALLOCATABLE");
-       }
-  if ( v->v_target         == 1)
-       {strcat(ligne,", TARGET");
-       }
-  if ( v->v_optionaldeclare     == 1 ) strcat(ligne,", OPTIONAL");
-  if ( v->v_pointerdeclare      == 1 ) strcat(ligne,", POINTER");
-  Save_Length(ligne,45);
+     sprintf(tmpligne,", intent(%s)", v->v_IntentSpec);
+     Save_Length(tmpligne, 49);
+     strcat(line,tmpligne);
+  }
+  if ( v->v_VariableIsParameter ) strcat(line, ", parameter");
+  if ( visibility )
+  {
+      if ( v->v_PublicDeclare  )  strcat(line, ", public");
+      if ( v->v_PrivateDeclare )  strcat(line, ", private");
+  }
+  if ( v->v_ExternalDeclare ) strcat(line, ", external");
+  if ( v->v_allocatable     ) strcat(line, ", allocatable");
+  if ( v->v_target          ) strcat(line, ", target");
+  if ( v->v_optionaldeclare ) strcat(line, ", optional");
+  if ( v->v_pointerdeclare  ) strcat(line, ", pointer");
+  Save_Length(line, 45);
 }
 
@@ -120,18 +119,17 @@
 /*                                                                            */
 /******************************************************************************/
-void WriteScalarDeclaration(variable *v,char ligne[LONG_4C])
-{
-
-  strcat (ligne, " :: ");
-  strcat (ligne, v->v_nomvar);
-  if ( strcasecmp(v->v_vallengspec,"") ) strcat(ligne,v->v_vallengspec);
-  if ( v->v_VariableIsParameter == 1 )
-  {
-     strcat(ligne," = ");
-     strcat(ligne,v->v_initialvalue);
-  }
-  Save_Length(ligne,45);
-}
-
+void WriteScalarDeclaration( variable *v, char line[LONG_M])
+{
+    strcat(line, " :: ");
+    strcat(line, v->v_nomvar);
+
+    if ( strcasecmp(v->v_vallengspec, "") ) strcat(line,v->v_vallengspec);
+    if ( v->v_VariableIsParameter )
+    {
+        strcat(line," = ");
+        strcat(line, v->v_initialvalue);
+    }
+    Save_Length(line, 45);
+}
 
 /******************************************************************************/
@@ -147,48 +145,39 @@
 /*                                                                            */
 /******************************************************************************/
-void WriteTableDeclaration(variable * v,char ligne[LONG_4C],int tmpok)
-{
-  char newname[LONG_4C];
-
-  strcat (ligne, ", Dimension(");
-
-  if ( v->v_dimensiongiven == 1 && tmpok == 1 )
-  {
-                                         strcat(ligne,v->v_readedlistdimension);
-                                         }
-  if ( v->v_dimensiongiven == 1 && tmpok == 0 )
-  {
-     strcpy(newname,ChangeTheInitalvaluebyTabvarsName
-                                  (v->v_readedlistdimension,List_Global_Var,0));
-
-     if ( !strcasecmp(newname,"") ) strcat(newname,v->v_readedlistdimension);
-
-        strcpy(newname,ChangeTheInitalvaluebyTabvarsName
-                                 (newname,List_Common_Var,0));
-
-     if ( !strcasecmp(newname,"") ) strcat(newname,v->v_readedlistdimension); 
-     
-        strcpy(newname,ChangeTheInitalvaluebyTabvarsName
-                              (newname,List_ModuleUsed_Var,0));
+void WriteTableDeclaration(variable * v,char ligne[LONG_M],int tmpok)
+{
+    char newname[LONG_M];
+
+    strcat (ligne, ", dimension(");
+
+    if ( v->v_dimensiongiven == 1 && tmpok == 1 )   strcat(ligne,v->v_readedlistdimension);
+    if ( v->v_dimensiongiven == 1 && tmpok == 0 )
+    {
+        strcpy(newname,ChangeTheInitalvaluebyTabvarsName(v->v_readedlistdimension,List_Global_Var));
         if ( !strcasecmp(newname,"") ) strcat(newname,v->v_readedlistdimension);
 
-     Save_Length(newname,47);
-     strcat(ligne,newname);
-  }
-  strcat (ligne, ")");
-  strcat (ligne, " :: ");
-  strcat (ligne, v->v_nomvar);
-  if ( strcasecmp(vallengspec,"") ) strcat(ligne,v->v_vallengspec);
-
-  if ( v->v_VariableIsParameter == 1 )
-  {
-     strcat(ligne," = ");
-     strcat(ligne,v->v_initialvalue);
-  }
-  Save_Length(ligne,45);
-}
-
-/******************************************************************************/
-/*                        writevardeclaration                                 */
+        strcpy(newname,ChangeTheInitalvaluebyTabvarsName(newname,List_Common_Var));
+        if ( !strcasecmp(newname,"") ) strcat(newname,v->v_readedlistdimension);
+
+        strcpy(newname,ChangeTheInitalvaluebyTabvarsName(newname,List_ModuleUsed_Var));
+        if ( !strcasecmp(newname,"") ) strcat(newname,v->v_readedlistdimension);
+
+        Save_Length(newname,47);
+        strcat(ligne,newname);
+    }
+    strcat(ligne, ") :: ");
+    strcat(ligne, v->v_nomvar);
+    if ( strcasecmp(vallengspec,"") ) strcat(ligne,v->v_vallengspec);
+
+    if ( v->v_VariableIsParameter == 1 )
+    {
+        strcat(ligne," = ");
+        strcat(ligne,v->v_initialvalue);
+    }
+    Save_Length(ligne,45);
+}
+
+/******************************************************************************/
+/*                        WriteVarDeclaration                                 */
 /******************************************************************************/
 /* This subroutine is used to write the initial declaration in the file       */
@@ -201,216 +190,228 @@
 /*                                                                            */
 /******************************************************************************/
-void writevardeclaration (listvar * var_record, FILE *fileout, int value, int visibility)
+void WriteVarDeclaration( variable *v, FILE *fileout, int value, int visibility )
 {
   FILE *filecommon;
+  char ligne[LONG_M];
+
+  filecommon = fileout;
+
+  if ( v->v_save == 0 || inmodulemeet == 0 )
+  {
+     WriteBeginDeclaration(v, ligne, visibility);
+
+     if ( v->v_nbdim == 0 )
+        WriteScalarDeclaration(v, ligne);
+     else
+        WriteTableDeclaration(v, ligne, value);
+
+     if ( v->v_VariableIsParameter != 1 && strcasecmp(v->v_initialvalue,"") )
+     {
+        strcat(ligne," = ");
+        strcat(ligne,v->v_initialvalue);
+     }
+     tofich(filecommon, ligne, 1);
+  }
+  else
+    printf("-- in writevardeclaration : |%s| -- MHCHECK\n", v->v_nomvar);
+  Save_Length(ligne,45);
+}
+
+
+void WriteLocalParamDeclaration(FILE* tofile)
+{
+    listvar *parcours;
+
+    parcours = List_Parameter_Var;
+    while ( parcours )
+    {
+        if ( !strcasecmp(parcours->var->v_subroutinename,subroutinename) )
+        {
+            WriteVarDeclaration(parcours->var, tofile, 0, 1);
+        }
+        parcours = parcours -> suiv;
+    }
+}
+
+void WriteFunctionDeclaration(FILE* tofile, int value)
+{
+    listvar *parcours;
+
+    parcours = List_FunctionType_Var;
+    while ( parcours )
+    {
+        if ( !strcasecmp(parcours->var->v_subroutinename, subroutinename) &&
+              strcasecmp(parcours->var->v_typevar, "") )
+        {
+            WriteVarDeclaration(parcours->var, tofile, value, 1);
+        }
+        parcours = parcours -> suiv;
+    }
+}
+
+void WriteSubroutineDeclaration(int value)
+{
+    listvar *parcours;
+    variable *v;
+
+    parcours = List_SubroutineDeclaration_Var;
+    while ( parcours )
+    {
+        v = parcours->var;
+        if ( !strcasecmp(v->v_subroutinename, subroutinename)   &&
+             (v->v_save == 0)                                   &&
+             (v->v_pointerdeclare == 0)                         &&
+             (v->v_VariableIsParameter == 0)                    &&
+             (v->v_common == 0) )
+        {
+            WriteVarDeclaration(v, fortran_out, value, 1);
+        }
+        else if ( !strcasecmp(v->v_subroutinename, subroutinename)  &&
+                  (v->v_save == 0)                                  &&
+                  (v->v_VariableIsParameter == 0)                   &&
+                  (v->v_common == 0) )
+        {
+            WriteVarDeclaration(v, fortran_out, value, 1);
+        }
+        parcours = parcours -> suiv;
+    }
+}
+
+void WriteArgumentDeclaration_beforecall()
+{
+    int position;
+    listnom *neededparameter;
+    FILE *paramtoamr;
+    listvar *parcours;
+    variable *v;
+    char ligne[LONG_M];
+
+    fprintf(fortran_out,"#include \"Param_BeforeCall_%s.h\"\n",subroutinename);
+
+    sprintf(ligne,"Param_BeforeCall_%s.h",subroutinename);
+    paramtoamr = open_for_write(ligne);
+
+    neededparameter = (listnom * )NULL;
+    position = 1;
+    parcours = List_SubroutineArgument_Var;
+
+    while ( parcours )
+    {
+        v = parcours->var;
+        if ( !strcasecmp(v->v_subroutinename, subroutinename) && (v->v_positioninblock == position) )
+        {
+            position++;
+            WriteVarDeclaration(v, fortran_out, 0, 1);
+            neededparameter = writedeclarationintoamr(List_Parameter_Var, paramtoamr,
+                                    v, v->v_subroutinename, neededparameter, subroutinename);
+            parcours = List_SubroutineArgument_Var;
+        }
+        else parcours = parcours -> suiv;
+    }
+    Save_Length(ligne,45);
+
+    // Write interface for 'Sub_Loop_machin' in 'Param_BeforeCall_machin.h' when outside a module
+    if ( IsTabvarsUseInArgument_0() && (inmodulemeet == 0) && (inprogramdeclare == 0) )
+    {
+        fprintf(paramtoamr, "      interface\n");
+        if (isrecursive) sprintf(ligne,"  recursive subroutine Sub_Loop_%s(", subroutinename);
+        else             sprintf(ligne,"  subroutine Sub_Loop_%s(", subroutinename);
+        WriteVariablelist_subloop(ligne);
+        WriteVariablelist_subloop_Def(ligne);
+        strcat(ligne,")");
+        Save_Length(ligne,45);
+        tofich(paramtoamr,ligne,1);
+
+        listusemodule *parcours_mod;
+        parcours_mod = List_NameOfModuleUsed;
+        while ( parcours_mod )
+        {
+            if ( !strcasecmp(parcours_mod->u_cursubroutine, subroutinename) )
+            {
+                fprintf(paramtoamr, "          use %s\n", parcours_mod->u_usemodule);
+            }
+            parcours_mod = parcours_mod->suiv;
+        }
+        fprintf(paramtoamr, "          implicit none\n");
+        WriteLocalParamDeclaration(paramtoamr);
+        writesub_loopdeclaration_scalar(List_UsedInSubroutine_Var, paramtoamr);
+        writesub_loopdeclaration_tab(List_UsedInSubroutine_Var, paramtoamr);
+        WriteArgumentDeclaration_Sort(paramtoamr);
+        WriteFunctionDeclaration(paramtoamr, 1);
+
+        sprintf(ligne,"  end subroutine Sub_Loop_%s\n", subroutinename);
+        tofich(paramtoamr, ligne, 1);
+        fprintf(paramtoamr, "      end interface\n");
+    }
+    fclose(paramtoamr);
+}
+
+void WriteArgumentDeclaration_Sort(FILE* tofile)
+{
+    int position = 1;
+    listvar *parcours;
+
+    parcours = List_SubroutineArgument_Var;
+    while ( parcours )
+    {
+        if ( !strcasecmp(parcours->var->v_subroutinename, subroutinename) &&
+                         parcours->var->v_positioninblock == position )
+        {
+            position = position + 1;
+            WriteVarDeclaration(parcours->var, tofile, 1, 1);
+            parcours = List_SubroutineArgument_Var;
+        }
+        else parcours = parcours -> suiv;
+    }
+
+    parcours = List_SubroutineArgument_Var;
+    while ( parcours )
+    {
+        if ( !strcasecmp(parcours->var->v_subroutinename,subroutinename) &&
+                         parcours->var->v_positioninblock == 0           &&
+                        parcours->var->v_nbdim == 0 )
+        {
+            WriteVarDeclaration(parcours->var,tofile,1,1);
+        }
+        parcours = parcours -> suiv;
+    }
+
+    parcours = List_SubroutineArgument_Var;
+    while ( parcours )
+    {
+        if ( !strcasecmp(parcours->var->v_subroutinename,subroutinename) &&
+                         parcours->var->v_positioninblock == 0           &&
+                         parcours->var->v_nbdim != 0 )
+        {
+            WriteVarDeclaration(parcours->var, tofile, 1, 1);
+        }
+        parcours = parcours -> suiv;
+    }
+}
+
+/******************************************************************************/
+/*                      writedeclarationintoamr                               */
+/******************************************************************************/
+/* This subroutine is used to write the declaration of parameters needed in   */
+/*    allocation subroutines creates in toamr.c                               */
+/******************************************************************************/
+/*                                                                            */
+/*                                                                            */
+/******************************************************************************/
+listnom *writedeclarationintoamr (listvar * deb_common, FILE *fileout,
+                              variable *var , const char *commonname,
+                           listnom *neededparameter, const char *name_common)
+{
   listvar *newvar;
   variable *v;
-  char ligne[LONG_4C];
-
-  filecommon=fileout;
-  newvar = var_record;
-  
-  if ( newvar->var->v_save == 0 || inmodulemeet == 0 )
-  {
-     v = newvar->var;
-     if (mark == 1) fprintf(fileout,"222222233333333\n");
-     WriteBeginDeclaration(v,ligne,visibility);
-
-     if ( v->v_nbdim == 0 ) WriteScalarDeclaration(v,ligne);
-     else WriteTableDeclaration(v,ligne,value);
-
-     if ( v->v_VariableIsParameter != 1 && strcasecmp(v->v_initialvalue,"") )
-     {
-        strcat(ligne," = ");
-        strcat(ligne,v->v_initialvalue);
-     }
-     
-     tofich (filecommon, ligne,1);
-     if (mark == 1) fprintf(fileout,"44444433333333\n");     
-  }
-  Save_Length(ligne,45);
-  
-}
-
-
-void WriteLocalParamDeclaration()
-{
-   listvar *parcours;
-
-   parcours = List_Parameter_Var;
-   while ( parcours )
-   {
-      if ( !strcasecmp(parcours->var->v_subroutinename,subroutinename) )
-      {
-         writevardeclaration(parcours,fortranout,0,1);
-      }
-      parcours = parcours -> suiv;
-   }
-}
-
-void WriteFunctionDeclaration(int value)
-{
-   listvar *parcours;
-
-   parcours = List_FunctionType_Var;
-   while ( parcours )
-   {
-      if ( !strcasecmp(parcours->var->v_subroutinename,subroutinename) &&
-            strcasecmp(parcours->var->v_typevar,"")
-         )
-      {
-         writevardeclaration(parcours,fortranout,value,1);
-      }
-      parcours = parcours -> suiv;
-   }
-}
-
-void WriteSubroutineDeclaration(int value)
-{
-   listvar *parcours;
-
-   parcours = List_SubroutineDeclaration_Var;
-   while ( parcours )
-   {
-      if ( !strcasecmp(parcours->var->v_subroutinename,subroutinename) &&
-           parcours->var->v_save == 0                                  &&
-           parcours->var->v_pointerdeclare == 0                        &&
-           parcours->var->v_VariableIsParameter == 0                   &&
-           parcours->var->v_common == 0
-         )
-      {
-         writevardeclaration(parcours,fortranout,value,1);
-
-      }
-      else if ( !strcasecmp(parcours->var->v_subroutinename,subroutinename) &&
-           parcours->var->v_save == 0                                  &&
-           parcours->var->v_VariableIsParameter == 0                   &&
-           parcours->var->v_common == 0
-              )
-      {
-         writevardeclaration(parcours,fortranout,value,1);
-
-      }
-      parcours = parcours -> suiv;
-   }
-}
-
-void WriteArgumentDeclaration_beforecall()
-{
-   int position;
-   listnom *neededparameter;
-   FILE *paramtoamr;
-   listvar *newvar;
-   char ligne[LONG_4C];
-
-   fprintf(fortranout,"#include \"Param_BeforeCall_%s.h\" \n",subroutinename);
-   /*                                                                         */
-   sprintf(ligne,"Param_BeforeCall_%s.h",subroutinename);
-   paramtoamr = associate (ligne);
-   /*                                                                         */
-   neededparameter = (listnom * )NULL;
-   /*                                                                         */
-   position = 1;
-   newvar = List_SubroutineArgument_Var;
-   while ( newvar )
-   {
-      if ( !strcasecmp(newvar->var->v_subroutinename,subroutinename) &&
-                       newvar->var->v_positioninblock == position
-         )
-      {
-         position = position + 1;
-
-         writevardeclaration(newvar,fortranout,0,1);
-         neededparameter = writedeclarationintoamr(List_Parameter_Var,
-                   paramtoamr,newvar->var,newvar->var->v_subroutinename,
-                   neededparameter,subroutinename);
-
-         newvar = List_SubroutineArgument_Var;
-      }
-      else newvar = newvar -> suiv;
-   }
-   Save_Length(ligne,45);
-   fclose(paramtoamr);
-}
-
-void WriteArgumentDeclaration_Sort()
-{
-   int position;
-   listvar *newvar;
-
-   /*                                                                         */
-   position = 1;
-   newvar = List_SubroutineArgument_Var;
-   while ( newvar )
-   {
-      if ( !strcasecmp(newvar->var->v_subroutinename,subroutinename) &&
-                       newvar->var->v_positioninblock == position
-         )
-      {
-         position = position + 1;
-
-         writevardeclaration(newvar,fortranout,1,1);
-         /*                                                                   */
-         newvar = List_SubroutineArgument_Var;
-      }
-      else newvar = newvar -> suiv;
-   }
-   /*                                                                         */
-   newvar = List_SubroutineArgument_Var;
-   while ( newvar )
-   {
-      if ( !strcasecmp(newvar->var->v_subroutinename,subroutinename) &&
-                       newvar->var->v_positioninblock == 0           &&
-                       newvar->var->v_nbdim == 0
-         )
-      {
-
-         writevardeclaration(newvar,fortranout,1,1);
-      }
-      newvar = newvar -> suiv;
-   }
-   /*                                                                         */
-   newvar = List_SubroutineArgument_Var;
-   while ( newvar )
-   {
-      if ( !strcasecmp(newvar->var->v_subroutinename,subroutinename) &&
-                       newvar->var->v_positioninblock == 0           &&
-                       newvar->var->v_nbdim != 0
-         )
-      {
-         writevardeclaration(newvar,fortranout,1,1);
-      }
-      newvar = newvar -> suiv;
-   }
-}
-
-/******************************************************************************/
-/*                      writedeclarationintoamr                               */
-/******************************************************************************/
-/* This subroutine is used to write the declaration of parameters needed in   */
-/*    allocation subroutines creates in toamr.c                               */
-/******************************************************************************/
-/*                                                                            */
-/*                                                                            */
-/******************************************************************************/
-listnom *writedeclarationintoamr (listvar * deb_common, FILE *fileout,
-                              variable *var , char commonname[LONG_C],
-                           listnom *neededparameter, char name_common[LONG_C])
-{
-  listvar *newvar;
-  variable *v;
-  char ligne[LONG_4C];
+  char ligne[LONG_M];
   int changeval;
   int out;
   int writeit;
   listnom *parcours;
-  listnom *parcoursprec;
-
-  parcoursprec = (listnom * )NULL;
 
   /* we should list the needed parameter                                      */
   if ( !strcasecmp(name_common,commonname) )
-     neededparameter = DecomposeTheNameinlistnom(var->v_readedlistdimension,
-                                                               neededparameter);
+     neededparameter = DecomposeTheNameinlistnom(var->v_readedlistdimension,neededparameter);
   /*                                                                          */
   parcours = neededparameter;
@@ -422,5 +423,5 @@
      while ( newvar && out == 0 )
      {
-     
+
         if ( !strcasecmp(parcours->o_nom,newvar->var->v_nomvar) && !strcasecmp(var->v_subroutinename,newvar->var->v_subroutinename))
         {
@@ -443,5 +444,5 @@
      while ( newvar && out == 0 )
      {
-        if ( !strcasecmp(parcours->o_nom,newvar->var->v_nomvar) && !strcasecmp(var->v_subroutinename,newvar->var->v_subroutinename))        
+        if ( !strcasecmp(parcours->o_nom,newvar->var->v_nomvar) && !strcasecmp(var->v_subroutinename,newvar->var->v_subroutinename))
         {
            out=1;
@@ -455,6 +456,4 @@
      parcours=parcours->suiv;
    }
-  /*                                                                          */
-  tofich (fileout, "",1);
   parcours = neededparameter;
   while (parcours)
@@ -482,9 +481,9 @@
 //           v->v_allocatable = 0;
 //        }
-        WriteBeginDeclaration(v,ligne,1);
+        WriteBeginDeclaration(v, ligne, 1);
         if ( v->v_nbdim == 0 ) WriteScalarDeclaration(v,ligne);
-        else WriteTableDeclaration(v,ligne,1);
-
-        tofich (fileout, ligne,1);
+        else WriteTableDeclaration(v, ligne, 1);
+
+        tofich(fileout, ligne, 1);
         if ( changeval == 1 )
         {
@@ -498,5 +497,5 @@
         {
            shouldincludempif = 0;
-           fprintf(fileout,"      include \'mpif.h\' \n");
+           fprintf(fileout,"      include \'mpif.h\'\n");
         }
      }
@@ -524,7 +523,7 @@
   listvar *newvar;
   variable *v;
-  char ligne[LONG_4C];
-
-  tofich (fileout, "",1);
+  char ligne[LONG_M];
+
+//   tofich (fileout, "",1);
   newvar = deb_common;
 
@@ -533,8 +532,5 @@
      if ( newvar->var->v_nbdim == 0 &&
           !strcasecmp(newvar->var->v_subroutinename,subroutinename)  &&
-/*RB*/
-           (newvar->var->v_pointerdeclare == 0  || !strcasecmp(newvar->var->v_typevar,"type"))  
-/*RBend*/
-         )
+           (newvar->var->v_pointerdeclare == 0 || !strcasecmp(newvar->var->v_typevar,"type")) )
      {
         v = newvar->var;
@@ -565,19 +561,16 @@
   listvar *newvar;
   variable *v;
-  char ligne[LONG_4C];
+  char ligne[LONG_M];
   int changeval;
 
-  tofich (fileout, "",1);
   newvar = deb_common;
   while (newvar)
   {
-  printf("newvar = %s %d %s\n",newvar->var->v_nomvar,newvar->var->v_pointerdeclare,newvar->var->v_typevar);
-     if ( newvar->var->v_nbdim != 0                                 &&
-          !strcasecmp(newvar->var->v_subroutinename,subroutinename) &&
-          (newvar->var->v_pointerdeclare == 0 || !strcasecmp(newvar->var->v_typevar,"type"))
-        )
+      v = newvar->var;
+//  printf("newvar = %s %d %s\n",newvar->var->v_nomvar,newvar->var->v_pointerdeclare,newvar->var->v_typevar);
+     if ( (v->v_nbdim != 0)  && !strcasecmp(v->v_subroutinename, subroutinename) &&
+          (v->v_pointerdeclare == 0 || !strcasecmp(v->v_typevar,"type")) )
      {
         changeval = 0;
-        v = newvar->var;
         if ( v->v_allocatable == 1)
         {
@@ -595,6 +588,6 @@
         }
 
-        WriteBeginDeclaration(v,ligne,1);
-        WriteTableDeclaration(v,ligne,1);
+        WriteBeginDeclaration(v, ligne, 1);
+        WriteTableDeclaration(v, ligne, 1);
         tofich (fileout, ligne,1);
         if ( changeval >= 1 ) v->v_allocatable = 1;
@@ -606,99 +599,70 @@
 }
 
-
 void ReWriteDeclarationAndAddTosubroutine_01(listvar *listdecl)
 {
-listvar *parcours;
-listvar *parcours2;
-listvar *parcours3;
-int out;
-
-if (insubroutinedeclare == 1)
-{
-parcours = listdecl;
-while (parcours)
-{
-/*
-parcours2 = List_SubroutineArgument_Var;
-out = 0;
-while (parcours2 && out == 0)
-{
-if (!strcasecmp(parcours2->var->v_subroutinename,subroutinename) && !strcasecmp(parcours2->var->v_nomvar,parcours->var->v_nomvar))
- {
- out = 1;
- }
-parcours2 = parcours2->suiv;
-}
-*/
-out = LookingForVariableInList(List_SubroutineArgument_Var,parcours->var);
-if (out == 0) out = VariableIsInListCommon(parcours,List_Common_Var);
-
-
-
-if (out == 0) out = LookingForVariableInList(List_Parameter_Var,parcours->var);
-if (out == 0) out = LookingForVariableInList(List_FunctionType_Var,parcours->var);
-if (out == 0) out = LookingForVariableInListGlobal(List_Global_Var,parcours->var);
-
-/*
-parcours2 = List_Common_Var;
-while (parcours2 && out == 0)
-{
-if (!strcasecmp(parcours2->var->v_commoninfile,mainfile) && !strcasecmp(parcours2->var->v_nomvar,parcours->var->v_nomvar))
- {
- out = 1;
- }
-parcours2 = parcours2->suiv;
-}
-*/
-//printf("nom = %s %d %d %d\n",parcours->var->v_nomvar,out,VariableIsParameter,SaveDeclare);
-if (firstpass == 0 && out == 0 && VariableIsParameter == 0 && SaveDeclare == 0) 
-
-{
-writevardeclaration(parcours,fortranout,1,1);
-}
-//if (firstpass == 1 && out == 1)
-if (firstpass == 1)
-  {
-  if (VariableIsParameter == 0 && SaveDeclare == 0)
-    {
-    List_SubroutineDeclaration_Var = insertvar(List_SubroutineDeclaration_Var,parcours->var);
-    }
-  }
-parcours = parcours->suiv;
-}
-}
+    listvar *parcours;
+    variable *v;
+    int out;
+
+    if ( insubroutinedeclare )
+    {
+        parcours = listdecl;
+        while ( parcours )
+        {
+            v = parcours->var;
+                          out = LookingForVariableInList(List_SubroutineArgument_Var, v);
+            if (out == 0) out = VariableIsInListCommon(parcours, List_Common_Var);
+            if (out == 0) out = LookingForVariableInList(List_Parameter_Var, v);
+            if (out == 0) out = LookingForVariableInList(List_FunctionType_Var, v);
+            if (out == 0) out = LookingForVariableInListGlobal(List_Global_Var, v);
+
+            if (firstpass == 0 && out == 0 && VariableIsParameter == 0 && SaveDeclare == 0)
+            {
+                WriteVarDeclaration(v, fortran_out, 1, 1);
+            }
+            if (firstpass == 1)
+            {
+                if (VariableIsParameter == 0 && SaveDeclare == 0)
+                {
+                    List_SubroutineDeclaration_Var = insertvar(List_SubroutineDeclaration_Var, v);
+                }
+            }
+            parcours = parcours->suiv;
+        }
+    }
 }
 
 void ReWriteDataStatement_0(FILE * filout)
 {
-listvar *parcours;
-int out;
-char ligne[LONG_C];
-char initialvalue[LONG_C];
-
-if (insubroutinedeclare == 1)
-{
-parcours = List_Data_Var_Cur ;
-while (parcours)
-{
-out = VariableIsInListCommon(parcours,List_Common_Var);
-if (out == 0) out = LookingForVariableInListGlobal(List_Global_Var,parcours->var);
-
-if (out == 0)
-{
-if (strncasecmp(parcours->var->v_initialvalue,"(/",2))
-{
-strcpy(initialvalue,parcours->var->v_initialvalue);
-}
-else
-{
-strncpy(initialvalue,&parcours->var->v_initialvalue[2],strlen(parcours->var->v_initialvalue)-4);
-strcpy(&initialvalue[strlen(parcours->var->v_initialvalue)-4],"\0");
-}
-sprintf(ligne,"data %s/%s/",parcours->var->v_nomvar,initialvalue);
-tofich(filout,ligne,1);
-}
-parcours = parcours->suiv;
-}
-}
-}
+    listvar *parcours;
+    int out;
+    char ligne[LONG_M];
+    char initialvalue[LONG_M];
+
+    if (insubroutinedeclare == 1)
+    {
+        parcours = List_Data_Var_Cur ;
+        while (parcours)
+        {
+            out = VariableIsInListCommon(parcours,List_Common_Var);
+            if (out)   break;
+
+            out = LookingForVariableInListGlobal(List_Global_Var,parcours->var);
+            if (out)   break;
+
+            if (strncasecmp(parcours->var->v_initialvalue,"(/",2))
+            {
+                strcpy(initialvalue,parcours->var->v_initialvalue);
+            }
+            else
+            {
+                strncpy(initialvalue,&parcours->var->v_initialvalue[2],strlen(parcours->var->v_initialvalue)-4);
+                strcpy(&initialvalue[strlen(parcours->var->v_initialvalue)-4],"\0");
+            }
+            sprintf(ligne,"data %s/%s/",parcours->var->v_nomvar,initialvalue);
+            tofich(filout,ligne,1);
+
+            parcours = parcours->suiv;
+        }
+    }
+}
