! testarray.f90 - 自分で配列を書き出してみるテスト
! Copyright (C) TOYODA Eizi, 2000.  All rights reserved.
! vi: set sw=4:

program testarray
    use gtool_data
    use gtool_attribute
    implicit none
    integer, parameter:: LATSIZE = 24, LONSIZE = 36
    integer, parameter:: LATDIV = 4, LONDIV = 3
    real, parameter:: PI = 3.141592653589793238462
    ! 緯度軸と経度軸の値とそれを書き出す変数  
    real:: lat(LATSIZE), lon(LONSIZE)
    type(GT_VARIABLE):: latvar, lonvar
    ! 緯度経度の2次元の格子値 u とそれを書き出す変数  
    real:: u(LONSIZE, LATSIZE)
    type(GT_VARIABLE):: uvar
    ! ループ変数用
    integer:: i, j
    ! 属性
    type(GT_ATTRIBUTE):: a
    ! エラーを拾う
    logical:: fail
continue

    call check1

    ! 座標軸の値の用意
    lon(:) = (/((real(i) * PI / LONSIZE), i = 1, LONSIZE)/)
    lat(:) = (/((real(j) * PI / LATSIZE), j = 1, LATSIZE)/)

    ! 次元変数を作成
    call Create(lonvar, 'gtool.nc#lon', length=size(lon))
    call Put(lonvar, lon)
    call Close(lonvar)

    call Create(latvar, 'gtool.nc#lat', length=size(lat))
    call Put(latvar, lat)
    call Close(latvar)

    call Create(uvar, 'gtool.nc#u', dims=(/lonvar, latvar/))
    ! u に値を用意して書き出す
    do, i = 1, LONSIZE
	do, j = 1, LATSIZE
	    u(i, j) = sin(lon(i) * LONDIV) * sin(lat(j) * LATDIV)
	enddo
    enddo
    call Put(uvar, u)

    call PutAttribute(uvar, 'title', 'test data')
    call PutAttribute(uvar, 'source', 'program testarray')
    call PutAttribute(uvar, 'institute', 'GFD-Dennou Club')

    ! 後始末
    call Close(uvar)

contains

    subroutine check1
	logical:: exist
	inquire(file='gtool.nc', exist=exist)
	if (exist) stop 'gtool.nc already exists.'
    end subroutine
    
end program
