From c34829996a6a359bab3541b0afef0cddf6a1192a Mon Sep 17 00:00:00 2001
From: =?utf8?q?Agn=C3=A8s=20Li=C3=A8vre?= <agnes.lievre@education.gouv.fr>
Date: Thu, 21 Feb 2002 18:42:24 +0000
Subject: [PATCH] Error 108 if stepm is not equal to 1 (month) because
 forecasting is only possible with a former optimization with a step of one
 month.

---
 src/imach.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/src/imach.c b/src/imach.c
index d938b4a..5845ea1 100644
--- a/src/imach.c
+++ b/src/imach.c
@@ -67,6 +67,7 @@
 #define AGEBASE 40
 
 
+int erreur; /* Error number */
 int nvar;
 int cptcovn, cptcovage=0, cptcoveff=0,cptcov;
 int npar=NPARMAX;
@@ -901,7 +902,7 @@ void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, doub
   powell(p,xi,npar,ftol,&iter,&fret,func);
 
    printf("\n#Number of iterations = %d, -2 Log likelihood = %.12f\n",iter,func(p));
-  fprintf(ficres,"#Number of iterations = %d, -2 Log likelihood = %.12f ",iter,func(p));
+  fprintf(ficres,"#Number of iterations = %d, -2 Log likelihood = %.12f \n",iter,func(p));
 
 }
 
@@ -2632,6 +2633,7 @@ ij=1;
   }
    
   fclose(ficgp);
+  /* end gnuplot */
    
 chdir(path);
    
@@ -2824,9 +2826,12 @@ fclose(fichtm);
 
   fclose(ficrespij);
 
+  if(stepm == 1) {
   /*---------- Forecasting ------------------*/
   calagedate=(anproj1+mproj1/12.+jproj1/365.-dateintmean)*YEARM;
 
+  /*printf("calage= %f", calagedate);*/
+  
   prevalence(agemin, agemax, s, agev, nlstate, imx,Tvar,nbcode, ncodemax,mint,anint,dateprev1,dateprev2, calagedate);
 
 
@@ -2910,11 +2915,12 @@ fclose(fichtm);
       fprintf(ficresf,"# StartingAge FinalAge");
       for(j=1; j<=nlstate+ndeath;j++) fprintf(ficresf," P.%d",j);
       if (popforecast==1)  fprintf(ficresf," [Population]");
- 
-      for (cpt=0; cpt<=5;cpt++) { 
+   
+      for (cpt=0; cpt<4;cpt++) { 
 	fprintf(ficresf,"\n");
-  fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jproj1,mproj1,anproj1+cpt);   
-      for (agedeb=(fage-((int)calagedate %12/12.)); agedeb>=(bage-((int)calagedate %12)/12.); agedeb--){ /* If stepm=6 months */
+	fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jproj1,mproj1,anproj1+cpt);   
+
+	for (agedeb=(fage-((int)calagedate %12/12.)); agedeb>=(bage-((int)calagedate %12)/12.); agedeb--){ /* If stepm=6 months */
 	nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); 
 	nhstepm = nhstepm/hstepm; 
 	/*printf("agedeb=%.lf stepm=%d hstepm=%d nhstepm=%d \n",agedeb,stepm,hstepm,nhstepm);*/
@@ -2922,11 +2928,11 @@ fclose(fichtm);
 	p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
 	oldm=oldms;savm=savms;
 	hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);  
-		
+	
 	for (h=0; h<=nhstepm; h++){
 	  if (h==(int) (calagedate+YEARM*cpt)) {
 	    fprintf(ficresf,"\n %.f ",agedeb+h*hstepm/YEARM*stepm);
-	  }
+	  } 
 	  for(j=1; j<=nlstate+ndeath;j++) {
 	    kk1=0.;kk2=0;
 	    for(i=1; i<=nlstate;i++) {	      
@@ -2961,6 +2967,12 @@ fclose(fichtm);
   free_imatrix(s,1,maxwav+1,1,n);
   free_vector(weight,1,n);
   fclose(ficresf);
+  }/* End forecasting */
+  else{
+    erreur=108;
+    printf("Error %d!! You can only forecast the prevalences if the optimization\n  has been performed with stepm = 1 (month) instead of %d\n", erreur, stepm);
+  }
+
   /*---------- Health expectancies and variances ------------*/
 
   strcpy(filerest,"t");
@@ -3098,7 +3110,9 @@ strcpy(fileresvpl,"vpl");
   
   free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
 
-  printf("End of Imach\n");
+  if(erreur >0)
+    printf("End of Imach with error %d\n",erreur);
+  else   printf("End of Imach\n");
   /*  gettimeofday(&end_time, (struct timezone*)0);*/  /* after time */
   
   /* printf("Total time was %d Sec. %d uSec.\n", end_time.tv_sec -start_time.tv_sec, end_time.tv_usec -start_time.tv_usec);*/
-- 
2.43.0