/*************************************
*                                    *
*    外枠システム  simlqkyos.c       *
*  （旧共済被保険者数の推計、制度別）*
*                                    *
*************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "set.h"
#include "cntl.h"
#include "kyos.h"
#include "file_open.h"

void simlqkyos(void) {

/*  変数定義  */
  double  TMP      ;
  double  TMQ      ;
  double  TMR      ;
  double  BUPT     ;
  int     XEND     ;
  int     ii       ;
  int     jj       ;



      sprintf(filename, "/rev26/wakuc/data/kyos/kou26-kisor.csv" ) ;
      if ((fp_setakyo = fopen(filename, "r")) == NULL) {
          fprintf(stderr, "旧共済の基礎率データファイルを開けません!\n");
          exit (2);
      }

      karayomi( fp_setakyo );
      karayomi( fp_setakyo );

    for ( nenrei=15; nenrei<=69; nenrei++ ) {
        read_csv( buffer, fp_setakyo, &data_number );
        ii = (int)buffer[0] ; 
        dattai[4][KIJUN-STARTY][1][ii] = buffer[1] ; 
        dattai[4][KIJUN-STARTY][2][ii] = buffer[2] ; 
        jj = (int)buffer[3] ; 
        bup[4][KIJUN-STARTY][1][jj] = buffer[4] ; 
        bup[4][KIJUN-STARTY][2][jj] = buffer[5] ; 
    }

    fclose( fp_setakyo ) ;

      for ( nendo=KIJUN+1; nendo<=KE; nendo++ ) {
        for ( sei=1; sei<=2; sei++ ) {
          for ( nenrei=15; nenrei<=84; nenrei++ ) {
            dattai[4][nendo-STARTY][sei][nenrei] = dattai[4][KIJUN-STARTY][sei][nenrei] ;
          }
        }
      }

      for ( sei=1; sei<=2; sei++ ) {
        BUPT = 0.0 ;
        for ( nenrei=15; nenrei<=69; nenrei++ ) {
            BUPT += bup[4][KIJUN-STARTY][sei][nenrei] ;
        }
        for ( nenrei=15; nenrei<=69; nenrei++ ) {
            bup[4][KIJUN-STARTY][sei][nenrei] /= BUPT ;
            for ( nendo=KIJUN+1; nendo<=KE; nendo++ ) {
              bup[4][nendo-STARTY][sei][nenrei] = bup[4][KIJUN-STARTY][sei][nenrei] ;
            }
        }
      }



  for ( seido=4; seido<=7 ; seido++ ) {

      for ( nendo=STARTY+1; nendo<=KIJUN ; nendo++ ) {
        for ( sei=1; sei<=2; sei++ ) {
          for ( nenrei=15; nenrei<=100; nenrei++ ) {
              akyos[seido][nendo-STARTY][sei] += kyosai[seido][nendo-STARTY][sei][nenrei] ;
          }
        }
      }

      for ( nendo=KIJUN+1; nendo<=KE-1; nendo++ ){
        for ( sei=1; sei<=2; sei++ ) {
          TMP = 0.0 ; TMQ = 0.0 ;
          for ( nenrei=15; nenrei<=69; nenrei++ ) {  
            TMP += kou12syu[nendo-STARTY][sei][nenrei] ;
            TMQ += kou12syu[KIJUN-STARTY][sei][nenrei] ;
          }
          if ( TMQ > 0.0 ) {
            akyos[seido][nendo-STARTY][sei] = akyos[seido][KIJUN-STARTY][sei] * TMP / TMQ ;
          }
          else {
            fprintf(fp_err, "旧共済総人数エラー\n" );
            akyos[seido][nendo-STARTY][sei] = akyos[seido][KIJUN-STARTY][sei] ;
          }
        }
      }


    for ( nendo=KIJUN+1; nendo<=KE; nendo++ ) {
      for ( sei=1; sei<=2; sei++ ) {

        XEND = 70 ;
        TMP = 0 ; TMR = 0 ; 

        for ( nenrei=XEND-1; nenrei>=15; nenrei-- ) {
            zanzon[seido][nendo-STARTY][sei][nenrei] 
             = kyosai[seido][nendo-1-STARTY][sei][nenrei-1]
                * exp( -dattai[4][nendo-STARTY][sei][nenrei] ) ;
            TMP += zanzon[seido][nendo-STARTY][sei][nenrei] ;
            TMR += kyosai[seido][nendo-1-STARTY][sei][nenrei-1]
                * ( 1.0 - exp( -dattai[4][nendo-STARTY][sei][nenrei] )) ;
        }
        for ( nenrei=XEND; nenrei<=110; nenrei++ ) {
            zanzon[seido][nendo-STARTY][sei][nenrei] = 0 ;
            TMR += kyosai[seido][nendo-1-STARTY][sei][nenrei-1] ;
        }

        sinki[seido][nendo-STARTY][sei] = akyos[seido][nendo-STARTY][sei] 
                                 - akyos[seido][nendo-1-STARTY][sei] + TMR ;
        TMQ = sinki[seido][nendo-STARTY][sei] ;

        if ( TMQ < 0.0 ) { 
            for ( nenrei=XEND-1; nenrei>=15; nenrei-- ) {
                kyosai[seido][nendo-STARTY][sei][nenrei]
                = zanzon[seido][nendo-STARTY][sei][nenrei] 
                  * akyos[seido][nendo-STARTY][sei] / TMP ;
            }
        }
        else { 
            for ( nenrei=XEND-1; nenrei>=15; nenrei-- ) {
                kyosai[seido][nendo-STARTY][sei][nenrei]
                = zanzon[seido][nendo-STARTY][sei][nenrei] 
                  + sinki[seido][nendo-STARTY][sei] * bup[4][nendo-STARTY][sei][nenrei] ;
            }
        }
        for ( nenrei=XEND; nenrei<=110; nenrei++ ) {
            kyosai[seido][nendo-STARTY][sei][nenrei] = 0.0 ;
        }

        TMP = 0.0 ;
        for ( nenrei=14; nenrei<=XEND; nenrei++ ) {
            TMP += kyosai[seido][nendo-STARTY][sei][nenrei] ;
        }
        if ( nendo > KIJUN ) {
          if (( TMP - akyos[seido][nendo-STARTY][sei] < -2.0 ) ||
              ( TMP - akyos[seido][nendo-STARTY][sei] >  2.0 ) ) {
              fprintf(stderr, "共済%dの性%dの年度%dエラー\n", seido, sei, nendo );
              fprintf(fp_err, "共済%dの性%dの年度%dエラー\n", seido, sei, nendo );
          }
        }
      }
    }

    for ( nendo=KIJUN+1; nendo<=KE; nendo++ ) {
      for ( nenrei=15; nenrei<=100; nenrei++ ) {
        if ( kou12syu[nendo-STARTY][2][nenrei] > 0.0 ) {
          kyosai[seido][nendo-STARTY][3][nenrei] = kyosai[seido][nendo-STARTY][2][nenrei]
            * kou12syu[nendo-STARTY][3][nenrei] / kou12syu[nendo-STARTY][2][nenrei] ;
          kyosai[seido][nendo-STARTY][4][nenrei] = kyosai[seido][nendo-STARTY][2][nenrei]
            * ( 1.0 - kou12syu[nendo-STARTY][3][nenrei] / kou12syu[nendo-STARTY][2][nenrei] );
        }
        kyosai[seido][nendo-STARTY][0][nenrei]
          = kyosai[seido][nendo-STARTY][1][nenrei] + kyosai[seido][nendo-STARTY][2][nenrei] ;
      }
    }

  }


  for ( nendo=KIJUN+1; nendo<=KE; nendo++ ) {
    for ( sei=0; sei<=4; sei++ ) {
      for ( nenrei=15; nenrei<=100; nenrei++ ) {

        qkou12syu[nendo-STARTY][sei][nenrei] = kou12syu[nendo-STARTY][sei][nenrei] ;
        qkounen[nendo-STARTY][sei][nenrei] = kounen[nendo-STARTY][sei][nenrei] ;
        for ( seido=4; seido<=6; seido++ ) { 
          qkou12syu[nendo-STARTY][sei][nenrei] -= kyosai[seido][nendo-STARTY][sei][nenrei] ;
          qkounen[nendo-STARTY][sei][nenrei] -= kyosai[seido][nendo-STARTY][sei][nenrei] ;
        }
        if ( nendo>=2002 ) { 
          qkou12syu[nendo-STARTY][sei][nenrei] -= kyosai[7][nendo-STARTY][sei][nenrei] ;
          qkounen[nendo-STARTY][sei][nenrei] -= kyosai[7][nendo-STARTY][sei][nenrei] ;
        }

      }
    }
  }

}

