!>
!
! Authors::   Yuta Kawai
! Version::   $Id: icgridgen_main.f90,v 1.2 2011-07-07 03:33:16 ykawai Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2011-2012. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!
program main

  ! モジュール引用 ; Use statement
  !

  !* gtool ***********************
  !*

  ! 種類型パラメタ
  ! Kind type parameter
  !
  use dc_types, only: DP, &
    &                 TOKEN, &
    &                 STRING

  ! 文字列操作
  !
  !
  use dc_string, only: &
    & toChar, StoA, StoI, Str_to_Logical

  !* IGMBaseLib ******************
  !*

  !
  !
  use IcGrid2D_FVM_Manager, only: &
    & IcGrid2D_FVM, IcGrid2D_FVM_Init

  !
  !
  use IcGrid2D_FVM_Builder, only : &
    & construct_icosahedral_grid, set_iteration_limiter, &
    & GTYPE_STDGRID, GTYPE_STDGCGRID, GTYPE_SPRGCGRID
  
  
  ! 宣言文 ; Declaration statements
  !
  implicit none
  
  type(IcGrid2D_FVM) :: ic_grid
  
  character(TOKEN), parameter :: GTYPE_STD_GRID_NAME = 'STD-grid'
  character(TOKEN), parameter :: GTYPE_STD_GC_GRID_NAME = 'STD-GC-grid'
  character(TOKEN), parameter :: GTYPE_SPR_GC_GRID_NAME = 'SPR-GC-grid'

  character(STRING) :: grid_filename = 'SPR-GC-grid_glevel5.nc'

  character(TOKEN) :: gtype_name = 'STD-GC-grid' 
  integer :: glevel = 5
  real(DP), parameter :: ic_radius = 1.0d0
  logical :: iteration_limiter = .true. 
  
  !**************
  !
  ! main
  !
  !*************
  
  ! 引数の解析
  call analize_cmdline_arg(glevel, gtype_name, iteration_limiter)

  ! 格子生成
  grid_filename = trim(gtype_name) // '_glevel' // trim(toChar(glevel)) // '.nc'
  call construct_grid( grid_filename, gtype_name, glevel, ic_radius, ic_grid )
  

contains

!
!>
!!
!!
subroutine construct_grid(file_name, grid_type_name, glevel, ic_radius, icgrid)

  ! モジュール引用 ; Use statements
  !

  !
  !
  !
  use IcGrid_ncWriter_mod, only: &
    & IcGrid_ncWriter, &
    & IcGrid_ncWriter_Init, open_ncFile, ncdef_gridData, end_ncdef, &
    & write_GridData, close_ncFile

  ! 宣言文 ; Declaration statements
  !
  character(*), intent(in) :: file_name
  character(*), intent(in) :: grid_type_name
  integer, intent(in) :: glevel
  real(DP), intent(in)  :: ic_radius
  type(IcGrid2D_FVM), intent(out) :: icgrid


  ! 作業変数
  ! Work variables
  !
  integer :: grid_type_id
  type(IcGrid_ncWriter) :: writer
 

  !

  select case (grid_type_name)
    case (GTYPE_STD_GRID_NAME)
      write(*,*) 'Selects STD-grid Builder..'
      grid_type_id = GTYPE_STDGRID

    case (GTYPE_STD_GC_GRID_NAME)
      write(*,*) 'Selects STD-GC-grid Builder..'
      grid_type_id = GTYPE_STDGCGRID

    case (GTYPE_SPR_GC_GRID_NAME)
      write(*,*) 'Selects SPR-GC-grid Builder..'
      grid_type_id = GTYPE_SPRGCGRID

      write(*,*) 'Time integration limiter', iteration_limiter
      call set_iteration_limiter(iteration_limiter)

    case default
     write(*,*) 'Specified grid type `' // trim(grid_type_name) // '` is invalid.'
     stop

  end select

  !
  call construct_icosahedral_grid(grid_type_id, glevel, ic_radius, icgrid)

  !
  !

  call IcGrid_ncWriter_Init(writer, icgrid)
  call open_ncFile(writer, file_name)
  call ncdef_GridData(writer)
  call end_ncdef(writer)
  call write_GridData(writer)
  call close_ncFile(writer)

end subroutine construct_grid

!
!>
!!
subroutine analize_cmdline_arg(glevel, gtype_name, limiter_flag)
  ! モジュール引用 ; Use statements
  !

  !
  !
  !
  use dc_args, only: ARGS, &
    & DCArgsOpen, DCArgsClose, DCArgsOption, &
    & DCArgsPutLine, DCArgsDebug, DCArgsHelp, DCArgsStrict 

  ! 宣言文 ; Declaration statements
  !
  integer, intent(inout) :: glevel
  character(*), intent(inout) :: gtype_name
  logical, intent(inout) :: limiter_flag

  ! 作業変数
  ! Work variables
  !

  !
  type(ARGS) :: arg
  
  logical :: OPT_glevel
  character(TOKEN) :: VAL_glevel
  
  logical :: OPT_gtype_name
  character(TOKEN) :: VAL_gtype_name
  
  logical :: OPT_limiter_flag
  character(TOKEN) :: VAL_limiter_flag
  
  ! 実行文; Executable statement
  !

  !
  call DCArgsOpen(arg)
  
  call DCArgsOption(arg, StoA('-L', '--icgrid_glevel'), OPT_glevel, VAL_glevel, &
    &    help="Specify the division level of icoshedral grid. [defalut value is " // toChar(glevel) // "]." )
  
  call DCArgsOption(arg, StoA('-G', '--grid_type'), OPT_gtype_name, VAL_gtype_name, &
    &    help="Specify the name of icosahedral grid type. [defalut value is " // trim(gtype_name) // "]." )

  call DCArgsOption(arg, StoA('-I', '--iteration_limiter'), OPT_limiter_flag, VAL_limiter_flag, &
    &    help="Specify the limiter flag of the intertation for solving the spring dynamics. [defalut value is " &
    &    // toChar(limiter_flag) // "]." )
  
  call DCArgsDebug(arg)
  call DCArgsHelp(arg)
  call DCArgsStrict(arg)

  if( OPT_glevel ) then
    glevel = StoI(VAL_glevel)
  end if

  if( OPT_gtype_name ) then
    gtype_name = VAL_gtype_name
  end if

  if( OPT_limiter_flag ) then
    limiter_flag = Str_to_Logical(VAL_limiter_flag)
  end if

end subroutine analize_cmdline_arg

end program main
 
