* PACKAGE IRWSTR  !" BARO 初期値入出力(NETCDF 版)
*
*"  [HIS] 96/09/11(takepiro)
*
**********************************************************************
      SUBROUTINE IRSTRT      !"  初期値の読み込み 
     O         ( GAU   , GAV   , GAVOR , 
     O           GBU   , GBV   , GBVOR , 
     O           ITA   , ITB   , IDATEA, IDATEB, ISTEP ,
     I           ITSTRT )
*
#if   SYS_IBMS
      INCLUDE   (ZCDIM)                  !" 格子点数, 波数
      INCLUDE   (ZHDIM)                  !" 文字列文字数
#else
#include        "zcdim.F"                    !" 格子点数, 波数
#include        "zhdim.F"                    !" 文字列文字数
#endif
*
#include 'netcdf.inc'
*
      REAL       GAU   ( IDIM, JDIM, KMAX )  !" 西風  ｕ(t)
      REAL       GBU   ( IDIM, JDIM, KMAX )  !" 西風  ｕ(t-Δt)
      REAL       GAV   ( IDIM, JDIM, KMAX )  !" 南風  ｖ(t)
      REAL       GBV   ( IDIM, JDIM, KMAX )  !" 南風  ｖ(t-Δt)
      REAL       GAVOR ( IDIM, JDIM, KMAX )  !" 渦度  ζ(t)
      REAL       GBVOR ( IDIM, JDIM, KMAX )  !" 渦度  ζ(t-Δt)
*
      INTEGER    ISTEP                       !" 通しステップ数
      INTEGER    ITA                         !" 通し時間(t)
      INTEGER    ITB                         !" 通し時間(t-Δt)
      INTEGER    IDATEA( 6 )                 !" 時刻：年月日時分秒
      INTEGER    IDATEB( 6 )                 !" 時刻：年月日時分秒
      INTEGER    ITSTRT                      !" 積分スタート時間
*
*    [NETCDF PARAM]
      INTEGER    IVDIMS(3)
      DATA       IVDIMS / IDIM, JDIM, NCUNLIM /
      INTEGER    ISTART(3)
      DATA       ISTART / 1, 1, 0 /
      INTEGER    ICOUNT(3)
      DATA       ICOUNT / IDIM, JDIM, 1 /
*
*   [INTERNAL WORK]
      INTEGER    ITINIT                      !" 初期データ時刻
      INTEGER    IFPAR, JFPAR                !" NAMELIST 装置番号
      INTEGER    IRCODE                      !" NETCDF RETURN CODE
*
*   [INTERNAL PARAM]
      CHARACTER  FILE *(NFILN)         !" 入力ファイル：初期値
      CHARACTER  TUNIT  *4             !" 時刻単位
      INTEGER    TINIT                 !" 初期データ時刻
*
      NAMELIST /NMINIT/ FILE, TUNIT, TINIT
*
      DATA       FILE   / ' ' /
      DATA       TUNIT  / 'DAY ' /
      DATA       TINIT  / -999 /
*
*"          < 0.  初期値データの設定 >
*
      CALL   REWNML ( IFPAR , JFPAR )
      READ   ( IFPAR, NMINIT, END=190 )
  190 WRITE  ( JFPAR, NMINIT )
*
      IF ( TINIT .NE. -999 ) THEN
         CALL ACTIME ( TINIT, TUNIT, '#', ITINIT  )
      ELSE
         ITINIT = ITSTRT
      ENDIF
*
      NCFINI = NCOPN( FILE, NCNOWRIT, IRCODE )
      IF ( IRCODE .GT. 0 ) THEN
         WRITE ( 6, * ) 'IOSTAT = ', IRCODE
         CALL MSGDMP ( 'E','IWRSTR','CANNOT OPEN A INIT FILE')
      ENDIF
*
*--- 配列次元取得
      NCDLON = NCDID( NCFINI, 'longitude', IRCODE )
      NCDLAT = NCDID( NCFINI, 'latitude' , IRCODE )
      NCDTIM = NCDID( NCFINI, 'time'    , IRCODE )
*
*--- 変数情報取得
      NCVU   = NCVID( NCFINI, 'u-velocity', IRCODE )
      NCVV   = NCVID( NCFINI, 'v-velocity', IRCODE )
      NCVVOR = NCVID( NCFINI, 'vorticity',  IRCODE )
*
      NCVLON = NCVID( NCFINI, 'longitude', IRCODE )
      NCVLAT = NCVID( NCFINI, 'latitude' , IRCODE )
      NCVTIM = NCVID( NCFINI, 'time',      IRCOED )
*
*"          < 1.  初期値の読み込み (GB) >
*
 10   CONTINUE                            !" <---- Loop ここから
          ISTART(3) = ISTART(3) + 1 
          CALL NCVGT1( NCFRST, NCVTIM, ISTART(3), RITB, IRCODE )
          ITB = RITB
          IF( IRCODE .NE. 0 ) THEN
             CALL MSGDMP( 'E','IRSTRT','NO INITIAL DATA' )
          ELSE IF( ITB .EQ. ITINIT ) THEN
             WRITE ( 6,* ) ' TRY TO READ HIST AT T = ', ITB
             GOTO 99
          ENDIF
          GOTO 10                           !" ----> Loop ここまで
*
 99   CONTINUE 
*
      CALL NCVGT( NCFINI, NCVU, ISTART, ICOUNT, GBU, IRCODE )
      CALL NCVGT( NCFINI, NCVV, ISTART, ICOUNT, GBV, IRCODE )
      CALL NCVGT( NCFINI, NCVVOR, ISTART, ICOUNT, GBVOR, IRCODE )
*
*"          < 2. 初期値の読み込み (GA) >
*
      ISTART(3) = ISTART(3) + 1
      CALL NCVGT1( NCFRST, NCVTIM, ISTART(3), RITA, IRCODE )
      ITA = RITA
      WRITE( 6, * )' TRY TO READ HIST AT T = ', ITA
*
      CALL NCVGT( NCFINI, NCVU, ISTART, ICOUNT, GBU, IRCODE )
      CALL NCVGT( NCFINI, NCVV, ISTART, ICOUNT, GBV, IRCODE )
      CALL NCVGT( NCFINI, NCVVOR, ISTART, ICOUNT, GBVOR, IRCODE )
*
*--- ファイルクローズ
      CALL NCCLOS( NCFINI, IRCODE )
*
      RETURN
      END
***********************************************************************
      SUBROUTINE IWRSTR      !"  リスタート・ファイルの書き込み 
     I         ( GAU   , GAV   , GAVOR , 
     I           GBU   , GBV   , GBVOR ,
     I           ITA   , ITB   , IDATEA, IDATEB, ISTEP )
*
#if   SYS_IBMS
      INCLUDE   (ZCDIM)                      !" 格子点数，波数
      INCLUDE   (ZHDIM)                  !" 文字列文字数
#else
#include        "zcdim.F"                    !" 格子点数，波数
#include        "zhdim.F"                    !" 文字列文字数
#endif
*
#include 'netcdf.inc'
*
      REAL       GAU   ( IDIM, JDIM, KMAX )  !" 西風  ｕ(t)
      REAL       GBU   ( IDIM, JDIM, KMAX )  !" 西風  ｕ(t-Δt)
      REAL       GAV   ( IDIM, JDIM, KMAX )  !" 南風  ｖ(t)
      REAL       GBV   ( IDIM, JDIM, KMAX )  !" 南風  ｖ(t-Δt)
      REAL       GAVOR ( IDIM, JDIM, KMAX )  !" 渦度  ζ(t)
      REAL       GBVOR ( IDIM, JDIM, KMAX )  !" 渦度  ζ(t-Δt)
*
      INTEGER    ISTEP                       !" 通しステップ数
      INTEGER    ITA                         !" 通し時間(t)
      INTEGER    ITB                         !" 通し時間(t-Δt)
      INTEGER    IDATEA( 6 )                 !" 時刻：年月日時分秒
      INTEGER    IDATEB( 6 )                 !" 時刻：年月日時分秒
*
*    [INTERNAL WORK]
      INTEGER    IFPAR, JFPAR                !" NAMELIST 装置番号
      INTEGER    IRCODE                      !" NETCDF RETURN CODE
*
      REAL       ALON  ( IDIM )              !" 経度
      REAL       ALAT  ( JDIM )              !" 緯度
      REAL       DLON  ( IDIM )              !" 経度荷重
      REAL       DLAT  ( JDIM )              !" 緯度荷重
*
*    [NETCDF PARAM]
      INTEGER    IVDIMS(3)
      INTEGER    ISTART(3)
      DATA       ISTART / 1, 1, 0 /
      INTEGER    ICOUNT(3)
      DATA       ICOUNT / IDIM, JDIM, 1 /
*
*    [INTERNAL PARAM]
      CHARACTER  FILE   *(NFILN)       !" 出力ファイル：再出発
      CHARACTER  DFMT  *(NCC)          !" 出力フォーマット
      DATA       FILE   / 'baro.rst' /
      DATA       DFMT   / 'UR8' /
*
      LOGICAL    OFIRST
      DATA       OFIRST / .TRUE. /
      SAVE
*
      NAMELIST  /NMRSTR/ FILE, DFMT
*    
      IF ( OFIRST ) THEN
         OFIRST = .FALSE.
*
         CALL   REWNML ( IFPAR , JFPAR )
         READ   ( IFPAR, NMRSTR, END=1090 )
 1090    WRITE  ( JFPAR, NMRSTR )         
*
*--- 新規ファイル作成
         NCFRST = NCCRE( FILE, NCCLOB, IRCODE )
         IF ( IRCODE .NE. 0.0 ) THEN
            WRITE ( 6, * ) 'IOSTAT = ', IRCODE
            CALL MSGDMP( 'E','IWRSTR','CANNOT OPEN A RESTART FILE')
         ENDIF
*
*--- 配列次元設定
         NCDLON = NCDDEF( NCFRST, 'longitude', IDIM, IRCODE )
         NCDLAT = NCDDEF( NCFRST, 'latitude' , JDIM, IRCODE )
         NCDTIM = NCDDEF( NCFRST, 'time'     , NCUNLIM, IRCODE )
*
*--- 変数設定
         IVDIMS(1) = NCDLON
         IVDIMS(2) = NCDLAT
         IVDIMS(3) = NCDTIM
*
         NCVU   = NCVDEF( NCFRST, 'u-velocity', 
     &                    NCFLOAT, 3, IVDIMS, IRCODE )
         CALL NDASET
     I           (  NCFRST  , NCVU , 
     I              'u-velocity' , 'm/s'  )
*
         NCVV   = NCVDEF( NCFRST, 'v-velocity', 
     &                    NCFLOAT, 3, IVDIMS, IRCODE )
         CALL NDASET
     I           (  NCFRST  , NCVV , 
     I              'v-velocity' , 'm/s'  )
*
         NCVVOR = NCVDEF( NCFRST, 'vorticity', 
     &                    NCFLOAT, 3, IVDIMS, IRCODE )
         CALL NDASET
     I           (  NCFRST  , NCVVOR,
     I              'vorticity' , '1/s'  )
*
         NCVLON = NCVDEF( NCFRST, 'longitude', 
     &                    NCFLOAT, 1, NCDLON  , IRCODE )
         CALL NDASET
     I           (  NCFRST  , NCVLON,
     I              'longitude' , 'rad'  )
*
         NCVLAT = NCVDEF( NCFRST, 'latitude', 
     &                    NCFLOAT, 1, NCDLAT  , IRCODE )
         CALL NDASET
     I           (  NCFRST  , NCVLAT,
     I              'latitude' , 'rad'  )
*
         NCVTIM = NCVDEF( NCFRST, 'time', 
     &                    NCFLOAT, 1, NCDTIM  , IRCODE )
         CALL NDASET
     I           (  NCFRST  , NCVTIM,
     I              'time' , 'hour'  )
*
*--- 設定終了
         CALL NCENDF( NCFRST, IRCODE )
*
*--- 座標データ出力
         CALL ACRGET
     O         ( ALON  , DLON  ,
     O           ALAT  , DLAT   )
*
         CALL NCVPT( NCFRST, NCVLON, 1, IDIM, ALON, IRCODE )
         CALL NCVPT( NCFRST, NCVLAT, 1, JDIM, ALAT, IRCODE )
      ENDIF
*
*"          < 1. ファイル出力 (GB) >
*
      ISTART(3) = ISTART(3) + 1
*
      CALL NCVPT1( NCFRST, NCVTIM, ISTART(3), REAL(ITB), IRCODE )
      CALL NCVPT( NCFRST, NCVU, ISTART, ICOUNT, GBU, IRCODE )
      CALL NCVPT( NCFRST, NCVV, ISTART, ICOUNT, GBV, IRCODE )
      CALL NCVPT( NCFRST, NCVVOR, ISTART, ICOUNT, GBVOR, IRCODE )
*
*"          < 2. ファイル出力 (GA) >
*
      ISTART(3) = ISTART(3) + 1
      CALL NCVPT1( NCFRST, NCVTIM, ISTART(3), REAL(ITA), IRCODE )
*
      CALL NCVPT( NCFRST, NCVU, ISTART, ICOUNT, GBU, IRCODE )
      CALL NCVPT( NCFRST, NCVV, ISTART, ICOUNT, GBV, IRCODE )
      CALL NCVPT( NCFRST, NCVVOR, ISTART, ICOUNT, GBVOR, IRCODE )
*
      RETURN
*======================================================================
      ENTRY  IWRSTE
*
      CALL NCCLOS( NCFRST, IRCODE )
*
      END
**********************************************************************
      SUBROUTINE NDASET
     I           (  NCID  , NCVID , 
     I              HNAME , HUNIT  )
*
#include 'netcdf.inc'
*
      INTEGER   NCID
      INTEGER   NCVID
      CHARACTER HNAME*(*)
      CHARACTER HUNIT*(*)
*
      CALL NCAPTC( NCID, NCVID, 'long_name', NCCHAR, 
     I             LENC(HNAME), HNAME, RCODE )
*
      CALL NCAPTC( NCID, NCVID, 'units', NCCHAR, 
     I             LENC(HUNIT), HUNIT, RCODE )
*
      RETURN
      END
