/*************************************
*                                    *
*    外枠システム  cutout.c          *
*  （調整率ファイルの作成、出力）    *
*                                    *
*************************************/

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

void cutout(void) 
{

/*  変数定義  */
char    filename[250]    ; 
int     XEND    ;  
int     CNENDO  ;  
double  JYUMYO  ;  
double  kouteki[ENDY-STARTY+1]        ; /*  公的年金被保険者数合計（年度末） */
double  kouteki_cent[ENDY-STARTY+1]   ; /*  公的年金被保険者数合計（年度間） */
double  cutritu1[ENDY-STARTY+1]       ; 
double  cutritu1_cent[ENDY-STARTY+1]  ; 
double  cutritu2[ENDY-STARTY+1]       ; 
double  cutritu2_cent[ENDY-STARTY+1]  ; 
double  cutritu3[ENDY-STARTY+1]       ; 
double  cutritu3_cent[ENDY-STARTY+1]  ; 
double  TMP[ENDY-STARTY+1][4] ; 
double  TMQ[ENDY-STARTY+1][4] ; 
int     TMPEND ;
int     TMQEND ;



    sprintf(filename, "/rev26/wakuc/rslt/ver_5/rslt%04d/waku%04d-m.csv", BANGO, BANGO) ;
    if ((fp_cutout = fopen(filename, "w")) == NULL) {
         fprintf(stderr, "出力cutファイルを開けません!\n");
         exit (2); /* 終了 */
    }


  CNENDO = 2005   ;
  JYUMYO = 0.997  ;

  for ( nendo=STARTY; nendo<=ENDY; nendo++ ) {
    cutritu1[nendo-STARTY] = 0.0 ;
    cutritu1_cent[nendo-STARTY] = 0.0 ;
    cutritu2[nendo-STARTY] = 0.0 ;
    cutritu2_cent[nendo-STARTY] = 0.0 ;
    cutritu3[nendo-STARTY] = 0.0 ;
    cutritu3_cent[nendo-STARTY] = 0.0 ;
  }


  for ( nendo=STARTY; nendo<=ENDY; nendo++ ) {
    if ( ( nendo==PARTJJ ) || ( nendo>=1996 ) ) {
      XEND = xend[8][nendo-STARTY] ; 
      if ( nendo==PARTJJ ) { XEND = xend[8][PARTYR2-1-STARTY]; }
      if ( XEND < 60 ) {  XEND = 60; }
      kouteki[nendo-STARTY] = 0.0 ;
      for ( sei=1; sei<=2; sei++ ) {
        for ( nenrei=15; nenrei<=110; nenrei++ ) {

          if ( nenrei<70 ) {
            kouteki[nendo-STARTY] += qkounen[nendo-STARTY][sei][nenrei] ;
            if ( nendo == PARTJJ ) { 
              kouteki[PARTJJ-STARTY]
                += qkounen[PARTYR2-1-STARTY][sei][nenrei] + partnin[PARTYR2-1-STARTY][sei][nenrei][0][2] ;
            }
            else if ( nendo >= PARTYR1 ) { 
              kouteki[nendo-STARTY] += partnin[nendo-STARTY][sei][nenrei][0][0] ;
            }
          }

          for ( seido=1; seido<=7; seido++ ) {
            kouteki[nendo-STARTY] += kyosai[seido][nendo-STARTY][sei][nenrei] ;
            if ( nendo == PARTJJ ) { 
              kouteki[PARTJJ-STARTY] += kyosai[seido][PARTYR2-1-STARTY][sei][nenrei] ;
            }
          }

          if (( nenrei>=20 ) && ( nenrei<XEND )) {
            kouteki[nendo-STARTY] += qkounen3[nendo-STARTY][sei][nenrei] ;
            for ( seido=1; seido<=7; seido++ ) {
              kouteki[nendo-STARTY] += kyosai3[seido][nendo-STARTY][sei][nenrei] ;
            }
          }

          if (( nenrei>=20 ) && ( nenrei<XEND )) {
            kouteki[nendo-STARTY] += ichigouip[nendo-STARTY][sei][nenrei] ;
          }

          if (( nenrei>=20 ) && ( nenrei<70 )) {
            kouteki[nendo-STARTY] += ichigouni[nendo-STARTY][sei][nenrei] ;
          }
        }
      }
    }
  }


  for ( nendo=STARTY+1; nendo<=ENDY; nendo++ ) {
    kouteki_cent[nendo-STARTY]
      = ( kouteki[nendo-1-STARTY] + kouteki[nendo-STARTY] ) / 2.0 ;
    if ( ( PART == 2 ) && ( PARTJ2 == 1 ) ) { 
      if ( nendo == PARTYR2 ) {
        kouteki_cent[nendo-STARTY]
          = ( kouteki[PARTJJ-STARTY] + kouteki[nendo-STARTY] ) / 2.0 ;
      }
    }
  }


  for ( nendo=CNENDO; nendo<=KE; nendo++ ) {
    if ( nendo-5 >= STARTY ) {

      if ( kouteki[nendo-5-STARTY] > 0.0 ) {
        TMP[nendo-STARTY][1] = 0.0 ;  TMQ[nendo-STARTY][1] = 0.0 ;
        TMPEND = xend[8][nendo-5-STARTY] ; 
        TMQEND = xend[8][nendo-2-STARTY] ;
        if ( TMPEND < 60 ) {  TMPEND = 60; }
        if ( TMQEND < 60 ) {  TMQEND = 60; }

        if ( ( MODE == 4 ) && ( MODE45 == 1 ) ) { 
            if ( TMQEND > 60 ) { 
                for ( nenrei=60; nenrei<=TMQEND-1; nenrei++ ) {
                    TMP[nendo-STARTY][1] +=  ichigouip[nendo-5-STARTY][1][nenrei]
                                          + ichigouip[nendo-5-STARTY][2][nenrei]
                                          + sangou[nendo-5-STARTY][0][nenrei]  ;
                    TMQ[nendo-STARTY][1] +=  ichigouip[nendo-2-STARTY][1][nenrei]
                                          + ichigouip[nendo-2-STARTY][2][nenrei]
                                          + sangou[nendo-2-STARTY][0][nenrei]  ;
                }
            }
        }
        else if ( ( MODE == 4 ) && ( MODE45 == 2 ) ) { 
            if ( TMPEND < TMQEND ) { 
                for ( nenrei=TMPEND; nenrei<=TMQEND-1; nenrei++ ) {
                    TMQ[nendo-STARTY][1] +=  ichigouip[nendo-2-STARTY][1][nenrei]
                                          + ichigouip[nendo-2-STARTY][2][nenrei]
                                          + sangou[nendo-2-STARTY][0][nenrei]  ;
                }
            }
        }

        cutritu1[nendo-STARTY] = pow( ( kouteki[nendo-2-STARTY] - TMQ[nendo-STARTY][1] )
                                 / ( kouteki[nendo-5-STARTY] - TMP[nendo-STARTY][1] ) , 1.0 / 3.0 ) * JYUMYO ;
      }

      if ( kouteki_cent[nendo-5-STARTY] > 0.0 ) {
        TMP[nendo-STARTY][2] = 0.0 ;  TMQ[nendo-STARTY][2] = 0.0 ;
        TMPEND = xend[8][nendo-6-STARTY] ; 
        TMQEND = xend[8][nendo-2-STARTY] ;
        if ( TMPEND < 60 ) {  TMPEND = 60; }
        if ( TMQEND < 60 ) {  TMQEND = 60; }

        if ( ( MODE == 4 ) && ( MODE45 == 1 ) ) { 
            if ( TMQEND > 60 ) { 
                for ( nenrei=60; nenrei<=TMQEND-1; nenrei++ ) {
                    TMP[nendo-STARTY][2] += ( ichigouip[nendo-6-STARTY][1][nenrei] + ichigouip[nendo-6-STARTY][2][nenrei]
                           + ichigouip[nendo-5-STARTY][1][nenrei] + ichigouip[nendo-5-STARTY][2][nenrei]
                           + sangou[nendo-6-STARTY][0][nenrei] + sangou[nendo-5-STARTY][0][nenrei] ) / 2.0 ;
                    TMQ[nendo-STARTY][2] += ( ichigouip[nendo-3-STARTY][1][nenrei] + ichigouip[nendo-3-STARTY][2][nenrei]
                           + ichigouip[nendo-2-STARTY][1][nenrei] + ichigouip[nendo-2-STARTY][2][nenrei]
                           + sangou[nendo-3-STARTY][0][nenrei] + sangou[nendo-2-STARTY][0][nenrei] ) / 2.0 ;
                }
            }
        }
        else if ( ( MODE == 4 ) && ( MODE45 == 2 ) ) { 
            if ( TMPEND < TMQEND ) { 
                for ( nenrei=TMPEND; nenrei<=TMQEND-1; nenrei++ ) {
                    TMP[nendo-STARTY][2] += ( ichigouip[nendo-6-STARTY][1][nenrei] + ichigouip[nendo-6-STARTY][2][nenrei]
                           + ichigouip[nendo-5-STARTY][1][nenrei] + ichigouip[nendo-5-STARTY][2][nenrei]
                           + sangou[nendo-6-STARTY][0][nenrei] + sangou[nendo-5-STARTY][0][nenrei] ) / 2.0 ;
                    TMQ[nendo-STARTY][2] += ( ichigouip[nendo-3-STARTY][1][nenrei] + ichigouip[nendo-3-STARTY][2][nenrei]
                           + ichigouip[nendo-2-STARTY][1][nenrei] + ichigouip[nendo-2-STARTY][2][nenrei]
                           + sangou[nendo-3-STARTY][0][nenrei] + sangou[nendo-2-STARTY][0][nenrei] ) / 2.0 ;
                }
            }
        }

        cutritu1_cent[nendo-STARTY] = pow( ( kouteki_cent[nendo-2-STARTY] - TMQ[nendo-STARTY][2] )
                           / ( kouteki_cent[nendo-5-STARTY] - TMP[nendo-STARTY][2] ) , 1.0 / 3.0 ) * JYUMYO ;

      }
    }
  }


  for ( nendo=CNENDO; nendo<=KE; nendo++ ) {
    if ( nendo-5 >= STARTY ) {
      if ( kouteki[nendo-5-STARTY] > 0.0 ) {
        cutritu2[nendo-STARTY] = 1.0 - cutritu1[nendo-STARTY] ;
      }
      if ( kouteki_cent[nendo-5-STARTY] > 0.0 ) {
        cutritu2_cent[nendo-STARTY] = 1.0 - cutritu1_cent[nendo-STARTY] ;
      }
    }
  }


  for ( nendo=CNENDO; nendo<=KE; nendo++ ) {
    if ( nendo-5 >= STARTY ) {
      if ( kouteki[nendo-5-STARTY] > 0.0 ) {
        cutritu3[nendo-STARTY] = 1.0 / ( 1.0 + ( cutritu1[nendo-STARTY]-1.0 )) - 1.0 ;
      }
      if ( kouteki_cent[nendo-5-STARTY] > 0.0 ) {
        cutritu3_cent[nendo-STARTY] = 1.0 / ( 1.0 + ( cutritu1_cent[nendo-STARTY]-1.0 )) - 1.0 ;
      }
    }
  }



  for ( nendo=CNENDO; nendo<=KE; nendo++ ) {
    cutritu1[nendo-STARTY] = raund( cutritu1[nendo-STARTY] , 6 ) ;
    cutritu1_cent[nendo-STARTY] = raund( cutritu1_cent[nendo-STARTY] , 6 ) ;
    cutritu2[nendo-STARTY] = raund( cutritu2[nendo-STARTY] , 4 ) ;
    cutritu2_cent[nendo-STARTY] = raund( cutritu2_cent[nendo-STARTY] , 4 ) ;
    cutritu3[nendo-STARTY] = raund( cutritu3[nendo-STARTY] , 4 ) ;
    cutritu3_cent[nendo-STARTY] = raund( cutritu3_cent[nendo-STARTY] , 4 ) ;
  }


  for ( nendo=CNENDO ; nendo<=(int)min(KE,KF); nendo++ ){
    fprintf(fp_cutout , "%4d,%7.6f,%7.6f\n", nendo, cutritu3_cent[nendo-STARTY], cutritu3_cent[nendo-STARTY] );
  }


  fclose(fp_cutout);



}

