      PROGRAM WINDTOASCII
c      USE NETCDF
      CHARACTER KEYWORD*4,FLTNAME*8,STMNAME*12,RADAR*4,EXPERIMENT*32
      CHARACTER CREATIME*32,EXTRA1*28,FILNAM*80,OUTFILNAM*80
      CHARACTER INFILEFILE*80, OUTFILEFILE*80,CYES*3,INOUTCHAR*7
      INTEGER IMAX,JMAX,KMAX,KOUNT,NMOSM,
     + IUNFLD,IATTEN,FLAG,EXTRA2,EXTRA3
      REAL STIME,ETIME,OLAT,OLON,SX,SY,SZ,XZ,YZ,ZZ,ROT,RA,CO1,
     + CO2,AZMCOR,ELCOR,THRESH,POWERT,BIEL,AZBIEL,ETIME1,STIME2,
     + EXTRA6,EXTRA7
      REAL U(4000000),V(4000000),W(4000000),DBZ(4000000),WS(4000000)
      REAL X(1000),Y(1000),Z(1000),U3(4000000)
      REAL RALT(1000),RLAT(1000),RLON(1000),RAZI(1000),RSECS(1000)
      REAL UAIR(1000),VAIR(1000),WAIR(1000),VGWAIR(1000),WSAIR(1000)
      REAL U2(4000000),V2(4000000),W2(4000000),DBZ2(4000000)
      CALL GETARG(1,CYES)
      IF(CYES.EQ.'OUT'.OR.CYES.EQ.'out')THEN
         INOUT=2
      ELSE
         INOUT=1
      ENDIF
      WRITE(6,*)'INOUT = ',INOUT
      LU=80
c      WRITE(6,*)'Enter input file names file name'
c      READ(5,*)INFILEFILE
c      WRITE(6,*)'Enter output file names file name'
c      READ(5,'(A80)')OUTFILEFILE
      INFILEFILE='infiles_profile'
      OUTFILEFILE='outfiles_profile'
      OPEN(82,FILE=INFILEFILE)
      OPEN(83,FILE=OUTFILEFILE)
1     READ(82,'(A80)',END=999)FILNAM
c      READ(82,*)IDUMMY
      READ(FILNAM,7024)INOUTCHAR
 7024 FORMAT(19X,A7)
      IF(INOUTCHAR.EQ.'inbound')THEN
         INOUT=1
      ELSE
         INOUT=2
      ENDIF
      WRITE(6,'(A7,I3)')INOUTCHAR,INOUT
c      READ(82,*)INOUT
      READ(83,'(A80)',END=999)OUTFILNAM
      IF(INOUT.EQ.1)THEN
       OPEN(84,FILE='aziradin')
      ELSE
       OPEN(84,FILE='aziradout')
      ENDIF
      READ(84,*)(RAZI(I),I=1,400)
      READ(84,*)(RALT(I),I=1,400)
      READ(84,*)(RLAT(I),I=1,400)
      READ(84,*)(RLON(I),I=1,400)
      READ(84,*)(RSECS(I),I=1,400)
      READ(84,*)(UAIR(I),I=1,400)
      READ(84,*)(VAIR(I),I=1,400)
      READ(84,*)(WAIR(I),I=1,400)
      READ(84,*)(VGWAIR(I),I=1,400)
      CLOSE(84)
      DO I=1,400
         IF(UAIR(I).GT.-200.)THEN
            WSAIR(I)=SQRT(UAIR(I)*UAIR(I)+VAIR(I)*VAIR(I))
         ELSE
            WSAIR(I)=-999.9
         ENDIF
         IF(RAZI(I).GE.-360.)THEN
            RAZI(I)=90-RAZI(I)
            IF(RAZI(I).LT.0.)RAZI(I)=RAZI(I)+360.
         ENDIF
      ENDDO
      WRITE(6,180)FILNAM
      WRITE(6,181)OUTFILNAM
180   FORMAT('INPUT FILE NAME IS ',A80)
181   FORMAT('OUTPUT FILE NAME IS ',A80)
      LUOUT=81
c      OPEN(LUOUT,FILE=OUTFILNAM)
      CALL READHEADER(LU,FILNAM,KEYWORD,FLTNAME,
     + STMNAME,RADAR,EXPERIMENT,
     + CREATIME,EXTRA1,IMAX,JMAX,KMAX,KOUNT,NMOSM,
     + IUNFLD,IATTEN,FLAG,EXTRA2,EXTRA3,STIME,ETIME,OLAT,OLON,
     + SX,SY,SZ,XZ,YZ,ZZ,ROT,RA,CO1,CO2,AZMCOR,ELCOR,THRESH,
     + POWERT,BIEL,AZBIEL,ETIME1,STIME2,EXTRA6,EXTRA7)
      IF(KEYWORD.EQ.'WIND'.OR.KEYWORD.EQ.'wind')THEN
       IUV=0
       WRITE(6,*)'IUV = ',IUV
      ELSEIF(KEYWORD.EQ.'RUV '.OR.KEYWORD.EQ.'ruv ')THEN
       IUV=1
       WRITE(6,*)'IUV = ',IUV
      ELSE
       WRITE(6,89)KEYWORD
89     FORMAT('KEYWORD IS ',A4,'....STOPPING')
       STOP
      ENDIF
C      WRITE(6,*)'FLTNAME = ',FLTNAME
      WRITE(6,'(A4)')KEYWORD
      WRITE(LUOUT,'(A4)')KEYWORD
      WRITE(6,'(A8)')FLTNAME
      WRITE(LUOUT,'(A8)')FLTNAME
      WRITE(6,'(A12)')STMNAME
      WRITE(LUOUT,'(A12)')STMNAME
      WRITE(6,'(A4)')RADAR
      WRITE(LUOUT,'(A4)')RADAR
      WRITE(6,'(A32)')EXPERIMENT
      WRITE(LUOUT,'(A32)')EXPERIMENT
      WRITE(6,'(A32)')CREATIME
      WRITE(LUOUT,'(A32)')CREATIME
      WRITE(6,'(A28)')EXTRA1
      WRITE(LUOUT,'(A28)')EXTRA1
      IF(IABS(KOUNT).GT.100)KOUNT=100
      WRITE(6,'(7I10)')IMAX,JMAX,KMAX,KOUNT,NMOSM,IUNFLD,IATTEN
      WRITE(LUOUT,'(7I10)')IMAX,JMAX,KMAX,KOUNT,NMOSM,IUNFLD,IATTEN
      WRITE(6,'(3I10)')FLAG,EXTRA2,EXTRA3
      WRITE(LUOUT,'(3I10)')FLAG,EXTRA2,EXTRA3
      WRITE(6,'(4E14.6)')STIME,ETIME,OLAT,OLON
      WRITE(LUOUT,'(4E14.6)')STIME,ETIME,OLAT,OLON
      WRITE(6,'(6F10.3)')SX,SY,SZ,XZ,YZ,ZZ
      WRITE(LUOUT,'(6F10.2)')SX,SY,SZ,XZ,YZ,ZZ
      WRITE(6,'(4E14.6)')ROT,RA,CO1,CO2
      WRITE(LUOUT,'(4E14.6)')ROT,RA,CO1,CO2
      WRITE(6,'(6E13.5)')AZMCOR,ELCOR,THRESH,POWERT,BIEL,AZBIEL
      WRITE(LUOUT,'(6E13.5)')AZMCOR,ELCOR,THRESH,POWERT,BIEL,AZBIEL
      WRITE(6,'(4E14.6)')ETIME1,STIME2,EXTRA6,EXTRA7
      WRITE(LUOUT,'(4E14.6)')ETIME1,STIME2,EXTRA6,EXTRA7
      CALL LDPLN(LU,U,V,W,DBZ,IMAX,JMAX,KMAX,IUV,ZZ,SZ)
      CALL WRITENETCDF(IMAX,JMAX,KMAX,SX,SY,SZ,XZ,YZ,ZZ,
     1 U,V,W,DBZ,WS,X,Y,Z,KEYWORD,FLTNAME,STMNAME,RADAR,EXPERIMENT,
     1 CREATIME,EXTRA1,KOUNT,NMOSM,IUNFLD,IATTEN,FLAG,EXTRA2,EXTRA3,
     1 STIME,ETIME,OLAT,OLON,ROT,RA,CO1,CO2,
     1 AZMCOR,ELCOR,THRESH,POWERT,BIEL,AZBIEL,ETIME1,STIME2,EXTRA6,
     1 EXTRA7,U3,OUTFILNAM,INOUT,RALT,RLAT,RLON,RAZI,RSECS,
     1 UAIR,VAIR,WAIR,VGWAIR,WSAIR)
c      CALL WRITEDATA(LUOUT,U,V,W,DBZ,IMAX,JMAX,KMAX)
      CLOSE(80)
      CLOSE(81)
      GO TO 1
999   STOP
      END
      SUBROUTINE WRITEDATA(LU,U,V,W,DBZ,IMAX,JMAX,KMAX)
c      USE NETCDF
      REAL U(IMAX,JMAX,KMAX),V(IMAX,JMAX,KMAX)
      REAL W(IMAX,JMAX,KMAX),DBZ(IMAX,JMAX,KMAX)
      DO K=1,KMAX
       DO J=1,JMAX
        WRITE(LU,10)(DBZ(I,J,K),I=1,IMAX)
       ENDDO
       DO J=1,JMAX
        WRITE(LU,10)(U(I,J,K),I=1,IMAX)
       ENDDO
       DO J=1,JMAX
        WRITE(LU,10)(V(I,J,K),I=1,IMAX)
       ENDDO
       DO J=1,JMAX
        WRITE(LU,10)(W(I,J,K),I=1,IMAX)
       ENDDO
      ENDDO
10    FORMAT(10F7.1)
      RETURN
      END
      SUBROUTINE READHEADER(LU,FILNAM,KEYWORD,FLTNAME,
     + STMNAME,RADAR,EXPERIMENT,
     + CREATIME,EXTRA1,IMAX,JMAX,KMAX,KOUNT,NMOSM,
     + IUNFLD,IATTEN,FLAG,EXTRA2,EXTRA3,STIME,ETIME,OLAT,OLON,
     + SX,SY,SZ,XZ,YZ,ZZ,ROT,RA,CO1,CO2,AZMCOR,ELCOR,THRESH,
     + POWERT,BIEL,AZBIEL,ETIME1,STIME2,EXTRA6,EXTRA7)
      CHARACTER KEYWORD*4,FLTNAME*8,STMNAME*12,RADAR*4,EXPERIMENT*32
      CHARACTER CREATIME*32,EXTRA1*28,FILNAM*80
      INTEGER IMAX,JMAX,KMAX,KOUNT,NMOSM,
     + IUNFLD,IATTEN,FLAG,EXTRA2,EXTRA3
      REAL STIME,ETIME,OLAT,OLON,SX,SY,SZ,XZ,YZ,ZZ,ROT,RA,CO1,
     + CO2,AZMCOR,ELCOR,THRESH,POWERT,BIEL,AZBIEL,ETIME1,STIME2,
     + EXTRA6,EXTRA7
      OPEN(LU,FILE=FILNAM,STATUS='OLD',FORM='UNFORMATTED')
      READ(LU)KEYWORD,FLTNAME,STMNAME,
     + RADAR,EXPERIMENT,CREATIME,
     + EXTRA1,IMAX,JMAX,KMAX,KOUNT,NMOSM,
     + IUNFLD,IATTEN,FLAG,EXTRA2,
     + EXTRA3,STIME,ETIME,OLAT,OLON,SX,SY,SZ,XZ,YZ,ZZ,ROT,RA,CO1,
     + CO2,AZMCOR,ELCOR,THRESH,POWERT,BIEL,AZBIEL,ETIME1,STIME2,
     + EXTRA6,EXTRA7
      RETURN
      END
      SUBROUTINE LDPLN(LU,U,V,W,DBZ,IMAX,JMAX,KMAX,IUV,ZZ,SZ)
c      USE NETCDF
      REAL U(IMAX,JMAX,KMAX),V(IMAX,JMAX,KMAX),W(IMAX,JMAX,KMAX)
      REAL DBZ(IMAX,JMAX,KMAX)
      INTEGER*2 IU(400),IV(400),IW(400),IDB(400),IDV(400)
      REAL RU(400),RV(400),RW(400),RDV(400)
      FLAG=-999.9
c      WRITE(6,*)'INSIDE LDPLNW'
c      WRITE(6,*)'IMAX,JMAX,KMAX,IUV = ',IMAX,JMAX,KMAX,IUV
      DO K=1,KMAX
       DO J=1,JMAX
        IF(IUV.EQ.0)THEN
         READ(LU)(IU(I),IV(I),IW(I),IDB(I),IDV(I),I=1,IMAX)
c         write(6,*)'j,k,iu = ',(j,k,iu(i),i=1,imax)
c         write(6,*)'j,k,iv = ',(j,k,iv(i),i=1,imax)
c         write(6,*)'j,k,iw = ',j,k,(iw(i),i=1,imax)
c         write(6,*)'j,k,idb = ',j,k,(idb(i),i=1,imax)
c         write(6,*)'j,k,idv = ',(j,k,idv(i),i=1,imax)
        ELSE
         READ(LU)(RU(I),RV(I),RW(I),IDB(I),RDV(I),I=1,IMAX)
        ENDIF
        DO I=1,IMAX
         IF(IDB(I).GT.-1000)THEN
          DBZZ=IDB(I)*.1
          DBZ(I,J,K)=DBZZ
         ELSE
          DBZZ=FLAG
          DBZ(I,J,K)=FLAG
         ENDIF
         IF(IUV.EQ.0)THEN
          IF(IU(I).LT.-9990)THEN
           U(I,J,K)=FLAG
           V(I,J,K)=FLAG
          ELSE
           WDR=IU(I)*.1
           WSP=IV(I)*.1
           XU=-SIN(WDR*3.14159/180.)*WSP
           XV=-COS(WDR*3.14159/180.)*WSP
           U(I,J,K)=XU
           V(I,J,K)=XV
          ENDIF
          IF(IW(I).GT.-9990.AND.DBZZ.GT.FLAG)THEN
           W(I,J,K)=VT+IW(I)*.01
          ELSE
           U(I,J,K)=FLAG
           V(I,J,K)=FLAG
           W(I,J,K)=FLAG
          ENDIF
c          IF(U(I,J,K).GT.FLAG.AND.W(I,J,K).LE.FLAG)THEN
c           WRITE(6,*)'I,J,K,U,V,W,IU,IV,IW = ',
c     1    I,J,K,U(I,J,K),V(I,J,K),W(I,J,K),IU(I),IV(I),IW(I)
c          ENDIF
         ELSE
          IF(RU(I).GT.FLAG.AND.RV(I).GT.FLAG.AND.RW(I).GT.FLAG)THEN
           U(I,J,K)=RU(I)
           V(I,J,K)=RV(I)
           DBZ(I,J,K)=IDB(I)*.1
           W(I,J,K)=VT+RW(I)
          ELSE
           U(I,J,K)=FLAG
           V(I,J,K)=FLAG
           W(I,J,K)=FLAG
           DBZ(I,J,K)=FLAG
          ENDIF
         ENDIF
        ENDDO
c        WRITE(6,*)'J,K,U = ',J,K,(U(I,J,K),I=1,IMAX)
       ENDDO
      ENDDO
      RETURN
      END
      SUBROUTINE WRITENETCDF(IMAX,JMAX,KMAX,SX,SY,SZ,XZ,YZ,ZZ,
     1 U,V,W,DBZ,WS,X,Y,Z,KEYWORD,FLTNAME,STMNAME,RADAR,EXPERIMENT,
     1 CREATIME,EXTRA1,KOUNT,NMOSM,IUNFLD,IATTEN,FLAG,EXTRA2,EXTRA3,
     1 STIME,ETIME,OLAT,OLON,ROT,RA,CO1,CO2,
     1 AZMCOR,ELCOR,THRESH,POWERT,BIEL,AZBIEL,ETIME1,STIME2,EXTRA6,
     1 EXTRA7,U2,OUTFILNAM,INOUT,RALT,RLAT,RLON,RAZI,RSECS,
     1 UAIR,VAIR,WAIR,VGWAIR,WSAIR)
      USE NETCDF
      CHARACTER KEYWORD*4,FLTNAME*8,STMNAME*12,RADAR*4,EXPERIMENT*32
      CHARACTER CREATIME*32,EXTRA1*28,FILNAM*80,OUTFILNAM*80
      INTEGER KOUNT,NMOSM,IUNFLD,IATTEN,FLAG,EXTRA2,EXTRA3
      REAL STIME,ETIME,OLAT,OLON,SX,SY,SZ,XZ,YZ,ZZ,ROT,RA,CO1,
     + CO2,AZMCOR,ELCOR,THRESH,POWERT,BIEL,AZBIEL,ETIME1,STIME2,
     + EXTRA6,EXTRA7
      INTEGER IKEYWORD,IFLTNAME,ISTMNAME,IRADAR,IEXPERIMENT
      INTEGER ICREATIME,IKOUNT,INMOSM,IIUNFLD,IIATEN,IFLAG,IEXTRA2
      INTEGER IEXTRA3,ISTIME,IETIME,IOLAT,IOLON,ISX,ISY,ISZ
      INTEGER IXZ,IYZ,IZZ,IROT,IRA,ICO1,ICO2,IAZMCOR,IELCOR,ITHRESH
      INTEGER IPOWERT,IBIEL,IAZBIEL,IETIME1,ISTIME2,IEXTRA6,IEXTRA7
      INTEGER IMAX,JMAX,KMAX,ITMAX
      INTEGER IDIMIDS(4),ICOUNT(4),ISTART(4),NCID,ICOUNT1(1)
      INTEGER ISTART1(1)
      INTEGER ISTARTX,ISTARTY,ISTARTZ
      INTEGER ICOUNTX,ICOUNTY,ICOUNTZ
      INTEGER IXVARID,IYVARID,IZVARID,ITVARID
      INTEGER IXDIMID,IYDIMID,IZDIMID,ITDIMID
      INTEGER IUVARID,IVVARID,IWVARID,IWSVARID,IDBZVARID,ISTAT
      INTEGER IAVARID,ILAVARID,ILOVARID,IAZIVARID,ISECVARID
      INTEGER IUAIRVARID,IVAIRVARID,IWAIRVARID,IVGWAIRVARID
      INTEGER IWSAIRVARID
      INTEGER NF90_CLASSIC_MODE
      REAL X(IMAX),Y(JMAX),Z(KMAX),U(IMAX,JMAX,KMAX)
      REAL RALT(IMAX),RLAT(IMAX),RLON(IMAX),RAZI(IMAX),RSECS(IMAX)
      REAL V(IMAX,JMAX,KMAX),W(IMAX,JMAX,KMAX),WS(IMAX,JMAX,KMAX)
      REAL UAIR(IMAX),VAIR(IMAX),WAIR(IMAX),VGWAIR(IMAX),WSAIR(IMAX)
      REAL U2(KMAX,JMAX,IMAX)
      REAL DBZ(IMAX,JMAX,KMAX),TT(1),VALID_RANGE(2)
      REAL DFLAG
      REAL VALID_MIN, VALID_MAX
      CHARACTER*80 NETCDFPATH
      CHARACTER*39 WRITECHAR
c      NF90_FLOAT=32
      WRITE(6,*)'IMAX,JMAX,KMAX = ',IMAX,JMAX,KMAX
      WRITE(6,*)'NF90_FLOAT = ',NF90_FLOAT
      NETCDFMODE=0
c      NETCDFPATH=OUTFILNAM
      WRITE(6,*)'NF90_NOCLOBBER = ',NF90_NOCLOBBER
      WRITE(6,*)'NF90_NETCDF4 = ',NF90_NETCDF4
      WRITE(6,*)'NF90_CLASSIC_MODEL = ',NF90_CLASSIC_MODEL
      WRITE(6,*)'NF90_FORMAT_CLASSIC = ',NF90_FORMAT_CLASSIC
      ISTAT=NF90_CREATE(OUTFILNAM,1,NCID)
      WRITE(6,*)'NF90_FORMAT_CLASSIS = ',NF90_FORMAT_CLASSIC
      WRITE(6,*)'CREATE,ISTAT,NCID = ',ISTAT,NCID
c      PAUSE
C       ISTAT=NF90_REDEF(NCID)
c      WRITE(6,*)'REDEF,NF90_NOERR,ISTAT,NCID = ',NF90_NOERR,ISTAT,NCID
      DFLAG=-999.9
      VALID_RANGE(1)=-900.
      VALID_RANGE(2)=900.
      VALID_MIN=-900.
      VALID_MAX=900.
      Y(1)=1.0 
      Y(2)=2.0
c      DO K=1,KMAX
c         DO J=1,JMAX
c            DO I=1,IMAX
c               DBZ(I,J,K)=FLOAT(K)
c            ENDDO
c         ENDDO
c      ENDDO
      DO I=1,IMAX
         X(I)=XZ+FLOAT(I-1)*SX
      ENDDO
      JMAX1=1
      DO J=1,JMAX1
         Y(J)=SY
      ENDDO
      DO K=1,KMAX
       Z(K)=(K-1)*SZ+ZZ
      ENDDO
      ITMAX=1
      JMAX1=1
      ISTAT=NF90_DEF_DIM(NCID,"time",ITMAX,ITDIMID)
      WRITE(6,*)'IDIM1,ISTAT = ',ISTAT
      ISTAT=NF90_DEF_DIM(NCID,"height",KMAX,IZDIMID)
      WRITE(6,*)'IDIM2,ISTAT = ',ISTAT
      ISTAT=NF90_DEF_DIM(NCID,"heading",JMAX1,IYDIMID)
      WRITE(6,*)'IDIM3,ISTAT = ',ISTAT
      ISTAT=NF90_DEF_DIM(NCID,"radius",IMAX,IXDIMID)
      WRITE(6,*)'IDIM3,ISTAT = ',ISTAT
      IDIMIDS(1)=ITDIMID
      IDIMIDS(2)=IZDIMID
      IDIMIDS(3)=IYDIMID
      IDIMIDS(4)=IXDIMID
      DO K=1,KMAX
         DO J=1,JMAX
            DO I=1,IMAX
               IF(U(I,J,K).GT.-998..AND.
     +              V(I,J,K).GT.-998.)THEN
                       WS(I,J,K)=SQRT(U(I,J,K)*U(I,J,K)+
     +                                V(I,J,K)*V(I,J,K))
                    ELSE
                       WS(I,J,K)=-999.9
               ENDIF
            ENDDO
         ENDDO
      ENDDO
      CALL U1U2(U,U2,IMAX,JMAX,KMAX,INOUT)
      CALL U1U2(V,U2,IMAX,JMAX,KMAX,INOUT)
      CALL U1U2(W,U2,IMAX,JMAX,KMAX,INOUT)
      CALL U1U2(WS,U2,IMAX,JMAX,KMAX,INOUT)
      CALL U1U2(DBZ,U2,IMAX,JMAX,KMAX,INOUT)
c      DO K=1,KMAX
c         DO J=1,JMAX
c            WRITE(6,*)'K,J = ',K,J
c            WRITE(6,'(8F10.2)')(U2(K,J,I),I=1,IMAX)
c         ENDDO
c      ENDDO
      WRITE(6,*)'NCID,IDIMIDS = ',NCID,IDIMIDS
      ISTAT=NF90_DEF_VAR(NCID,"time",NF90_FLOAT,ITDIMID,ITVARID)
      READ(FLTNAME,'(3I2)')IYEAR,IMONTH,IDAY
      IF(IYEAR.GT.50.)THEN
       IYEAR=IYEAR+1900
      ELSE
       IYEAR=IYEAR+2000
      ENDIF
      WRITE(WRITECHAR,8777)IYEAR,IMONTH,IDAY
 8777 FORMAT('seconds since ',i4,'-',i2.2,'-',i2.2,' 00:00:00 00:00')
      WRITE(6,8777)IYEAR,IMONTH,IDAY
c      ISTAT=NF90_PUT_ATT(NCID,ITDIMID,'UNITS',
c     1'seconds since 2007-09-14 00:00:00 00:00')
      ISTAT=NF90_PUT_ATT(NCID,ITDIMID,'UNITS','seconds')
      ISTAT=NF90_PUT_ATT(NCID,ITDIMID,'STANDARD_NAME','time')
      ISTAT=NF90_PUT_ATT(NCID,ITDIMID,'AXIS',"T")
      ISTAT=NF90_DEF_VAR(NCID,"height",NF90_FLOAT,IZDIMID,IZVARID)
      ISTAT=NF90_PUT_ATT(NCID,IZDIMID,'UNITS','km')
      ISTAT=NF90_PUT_ATT(NCID,IZDIMID,'STANDARD_NAME','height')
      ISTAT=NF90_PUT_ATT(NCID,IZDIMID,'AXIS',"Z")
      ISTAT=NF90_DEF_VAR(NCID,"heading",NF90_FLOAT,IYDIMID,IYVARID)
      ISTAT=NF90_PUT_ATT(NCID,IYDIMID,'UNITS','degrees')
      ISTAT=NF90_PUT_ATT(NCID,IYDIMID,'STANDARD_NAME','heading')
      ISTAT=NF90_PUT_ATT(NCID,IYDIMID,'AXIS',"Y")
      ISTAT=NF90_DEF_VAR(NCID,"radius",NF90_FLOAT,IXDIMID,IXDIMID)
      ISTAT=NF90_PUT_ATT(NCID,IXDIMID,'UNITS','km')
      ISTAT=NF90_PUT_ATT(NCID,IXDIMID,'STANDARD_NAME','radius')
      ISTAT=NF90_PUT_ATT(NCID,IXDIMID,'AXIS',"X")
      ISTAT=NF90_DEF_VAR(NCID,"azimuth",NF90_FLOAT,IXDIMID,IAZIVARID)
      ISTAT=NF90_PUT_ATT(NCID,IAZIVARID,'UNITS','degrees')
      ISTAT=NF90_PUT_ATT(NCID,IAZIVARID,'STANDARD_NAME','azimuth')
      ISTAT=NF90_PUT_ATT(NCID,IAZIVARID,'AXIS',"X")
      ISTAT=NF90_DEF_VAR(NCID,"altitude",NF90_FLOAT,IXDIMID,IAVARID)
      ISTAT=NF90_PUT_ATT(NCID,IAVARID,'UNITS','km')
      ISTAT=NF90_PUT_ATT(NCID,IAVARID,'STANDARD_NAME','altitude')
      ISTAT=NF90_PUT_ATT(NCID,IAVARID,'AXIS',"X")
      ISTAT=NF90_DEF_VAR(NCID,"latitude",NF90_FLOAT,IXDIMID,ILAVARID)
      ISTAT=NF90_PUT_ATT(NCID,ILAVARID,'UNITS','degrees')
      ISTAT=NF90_PUT_ATT(NCID,ILAVARID,'STANDARD_NAME','latitude')
      ISTAT=NF90_PUT_ATT(NCID,ILAVARID,'AXIS',"X")
      ISTAT=NF90_DEF_VAR(NCID,"longitude",NF90_FLOAT,IXDIMID,ILOVARID)
      ISTAT=NF90_PUT_ATT(NCID,ILOVARID,'UNITS','degrees')
      ISTAT=NF90_PUT_ATT(NCID,ILOVARID,'STANDARD_NAME','longitude')
      ISTAT=NF90_PUT_ATT(NCID,ILOVARID,'AXIS',"X")
      ISTAT=NF90_DEF_VAR(NCID,"seconds",NF90_FLOAT,IXDIMID,ISECVARID)
      ISTAT=NF90_PUT_ATT(NCID,ISECVARID,'UNITS','degrees')
      ISTAT=NF90_PUT_ATT(NCID,ISECVARID,'STANDARD_NAME',
     + 'seconds since midnight')
      ISTAT=NF90_PUT_ATT(NCID,ISECVARID,'AXIS',"X")
      ISTAT=NF90_DEF_VAR(NCID,"u air",NF90_FLOAT,IXDIMID,IUAIRVARID)
      ISTAT=NF90_PUT_ATT(NCID,IUAIRVARID,'UNITS','m/s')
      ISTAT=NF90_PUT_ATT(NCID,IUAIRVARID,'STANDARD_NAME',
     + 'flight-level wind u-component')
      ISTAT=NF90_PUT_ATT(NCID,IUAIRVARID,'AXIS',"X")
      ISTAT=NF90_DEF_VAR(NCID,"v air",NF90_FLOAT,IXDIMID,IVAIRVARID)
      ISTAT=NF90_PUT_ATT(NCID,IVAIRVARID,'UNITS','m/s')
      ISTAT=NF90_PUT_ATT(NCID,IVAIRVARID,'STANDARD_NAME',
     + 'flight-level wind v-component')
      ISTAT=NF90_PUT_ATT(NCID,IVAIRVARID,'AXIS',"X")
      ISTAT=NF90_DEF_VAR(NCID,"w air",NF90_FLOAT,IXDIMID,IWAIRVARID)
      ISTAT=NF90_PUT_ATT(NCID,IWAIRVARID,'UNITS','m/s')
      ISTAT=NF90_PUT_ATT(NCID,IWAIRVARID,'STANDARD_NAME',
     + 'flight-level vertical wind')
      ISTAT=NF90_PUT_ATT(NCID,IWAIRVARID,'AXIS',"X")
      ISTAT=NF90_DEF_VAR(NCID,"vgw air",NF90_FLOAT,IXDIMID,IVGWAIRVARID)
      ISTAT=NF90_PUT_ATT(NCID,IVGWAIRVARID,'UNITS','m/s')
      ISTAT=NF90_PUT_ATT(NCID,IVGWAIRVARID,'STANDARD_NAME',
     + 'aircraft vertical motion')
      ISTAT=NF90_PUT_ATT(NCID,IVGWAIRVARID,'AXIS',"X")
      ISTAT=NF90_DEF_VAR(NCID,"ws air",NF90_FLOAT,IXDIMID,IWSAIRVARID)
      ISTAT=NF90_PUT_ATT(NCID,IWSAIRVARID,'UNITS','m/s')
      ISTAT=NF90_PUT_ATT(NCID,IWSAIRVARID,'STANDARD_NAME',
     + 'flight-level wind speed')
      ISTAT=NF90_PUT_ATT(NCID,IWSAIRVARID,'AXIS',"X")
      WRITE(6,*)'IXDIMID,IZDIMID,ITDIMID = ',
     1 IXDIMID,IZDIMID,ITDIMID
      ISTAT=NF90_DEF_VAR(NCID,"Radial_wind",NF90_FLOAT,IDIMIDS,IUVARID)
      WRITE(6,*)'ISTAT,IDIMIDS,IUVARID = ',ISTAT,IDIMIDS,IUVARID
      ISTAT=NF90_PUT_ATT(NCID,IUVARID,'UNITS','m/s')
      ISTAT=NF90_PUT_ATT(NCID,IUVARID,'valid_range',VALID_RANGE)
      ISTAT=NF90_PUT_ATT(NCID,IUVARID,'valid_min',VALID_MIN)
      ISTAT=NF90_PUT_ATT(NCID,IUVARID,'valid_max',VALID_MAX)
      ISTAT=NF90_DEF_VAR(NCID,"Tangential_Wind",NF90_FLOAT,IDIMIDS,
     1 IVVARID)
      WRITE(6,*)'ISTAT,IDIMIDS,IVVARID = ',ISTAT,IDIMIDS,IVVARID
      ISTAT=NF90_DEF_VAR(NCID,"Vertical_wind",
     1 NF90_FLOAT,IDIMIDS,IWVARID)
      WRITE(6,*)'ISTAT,IDIMIDS,IWVARID = ',ISTAT,IDIMIDS,IWVARID
      ISTAT=NF90_DEF_VAR(NCID,"Wind_Speed",NF90_FLOAT,IDIMIDS,IWSVARID)
      WRITE(6,*)'ISTAT,IDIMIDS,IWSVARID = ',ISTAT,IDIMIDS,IWSVARID
      ISTAT=NF90_DEF_VAR(NCID,"REFLECTIVITY",
     1 NF90_FLOAT,IDIMIDS,IDBZVARID)
      WRITE(6,*)'ISTAT,IDIMIDS,IDBZVARID = ',ISTAT,IDIMIDS,IDBZVARID
      ISTAT=NF90_PUT_ATT(NCID,IVVARID,'UNITS','m/s')
      ISTAT=NF90_PUT_ATT(NCID,IVVARID,'valid_range',VALID_RANGE)
      ISTAT=NF90_PUT_ATT(NCID,IVVARID,'valid_min',VALID_MIN)
      ISTAT=NF90_PUT_ATT(NCID,IVVARID,'valid_max',VALID_MAX)
      ISTAT=NF90_PUT_ATT(NCID,IWVARID,'UNITS','m/s')
      ISTAT=NF90_PUT_ATT(NCID,IWVARID,'valid_range',VALID_RANGE)
      ISTAT=NF90_PUT_ATT(NCID,IWVARID,'valid_min',VALID_MIN)
      ISTAT=NF90_PUT_ATT(NCID,IWVARID,'valid_max',VALID_MAX)
      ISTAT=NF90_PUT_ATT(NCID,IWSVARID,'UNITS','m/s')
      ISTAT=NF90_PUT_ATT(NCID,IWSVARID,'valid_range',VALID_RANGE)
      ISTAT=NF90_PUT_ATT(NCID,IWSVARID,'valid_min',VALID_MIN)
      ISTAT=NF90_PUT_ATT(NCID,IWSVARID,'valid_max',VALID_MAX)
      ISTAT=NF90_PUT_ATT(NCID,IDBZVARID,'UNITS','dBZ')
      ISTAT=NF90_PUT_ATT(NCID,IDBZVARID,'valid_range',VALID_RANGE)
      ISTAT=NF90_PUT_ATT(NCID,IDBZVARID,'valid_min',VALID_MIN)
      ISTAT=NF90_PUT_ATT(NCID,IDBZVARID,'valid_max',VALID_MAX)
      ISTAT=NF90_PUT_ATT(NCID,ISECVARID,'missing_value',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IUVARID,'missing_value',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IVVARID,'missing_value',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IWVARID,'missing_value',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IAZIVARID,'missing_value',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IAVARID,'missing_value',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,ILAVARID,'missing_value',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,ILOVARID,'missing_value',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IDBZVARID,'missing_value',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IUAIRVARID,'missing_value',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IVAIRVARID,'missing_value',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IWAIRVARID,'missing_value',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IVGWAIRVARID,'missing_value',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IWSAIRVARID,'missing_value',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IWSVARID,'missing_value',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,ISECVARID,'undef',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IUVARID,'undef',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IVVARID,'undef',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IWVARID,'undef',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IAZIVARID,'undef',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IAVARID,'undef',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,ILAVARID,'undef',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,ILOVARID,'undef',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IDBZVARID,'undef',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IUAIRVARID,'undef',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IVAIRVARID,'undef',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IWAIRVARID,'undef',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IVGWAIRVARID,'undef',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IWSAIRVARID,'undef',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,IWSVARID,'undef',DFLAG)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"KEYWORD",KEYWORD)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"FLTNAME",FLTNAME)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"STMNAME",STMNAME)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"RADAR",RADAR)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"EXPERIMENT",EXPERIMENT)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"CREATIME",CREATIME)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"EXTRA1",EXTRA1)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"KOUNT",KOUNT)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"NMOSM",NMOSM)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"IUNFLD",IUNFLD)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"IATTEN",IATTEN)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"EXTRA2",EXTRA2)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"EXTRA3",EXTRA3)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"START_TIME",STIME)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"END_TIME",ETIME)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"ORIGIN_LATITUDE",OLAT)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"ORIGIN_LONGITUDE",OLON)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"FLAG",FLAG)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,
     1 "EAST_WEST_RESOLUTION_KM",SX)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,
     1 "NORTH_SOUTH_RESOLUTION_KM",SY)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,
     1 "VERTICAL_RESOLUTION_KM",SZ)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,
     1 "ORIGIN_EAST_WEST_DISTANCE_FROM_LOWER_LEFT_CORNER_KM",XZ)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,
     1 "ORIGIN_NORTH_SOUTH_DISTANCE_FROM_LOWER_LEFT_CORNER_KM",YZ)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,
     1 "HEIGHT_OF_LOWEST_HORIZONTAL_LEVEL_KM",ZZ)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"ROTATION_IN_DEGREES",ROT)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"RA",RA)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"CO1",CO1)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"CO2",CO2)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"AZMCOR",AZMCOR)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"ELCOR",ELCOR)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"THRESH",THRESH)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"POWERT",POWERT)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"BIEL",BIEL)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"AZBIEL",AZBIEL)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"STIME2",STIME2)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"ETIME1",ETIME1)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"EXTRA6",EXTRA6)
      ISTAT=NF90_PUT_ATT(NCID,NF90_GLOBAL,"EXTRA7",EXTRA7)
      ISTAT=NF90_ENDDEF(NCID)
      ICOUNT1(1)=IMAX
      ISTART1(1)=1
      ISTART(1)=1
      ISTART(2)=1
      ISTART(3)=1
      ISTART(4)=1
      ICOUNT(1)=IMAX
      ICOUNT(2)=0
      ICOUNT(3)=0
c***
      ISTAT=NF90_PUT_VAR(NCID,IXDIMID,X,ISTART1,ICOUNT1)
      ICOUNT1(1)=1
      ISTAT=NF90_PUT_VAR(NCID,IYDIMID,Y,ISTART1,ICOUNT1)
      ICOUNT1(1)=KMAX
      ISTAT=NF90_PUT_VAR(NCID,IZDIMID,Z,ISTART1,ICOUNT1)
      ICOUNT1(1)=1
      IF(ETIME1.GE.STIME.AND.ETIME1.LE.ETIME)THEN
         TT(1)=ETIME1
      ELSE
         TT(1)=FLOAT(INT((STIME+ETIME)/2.))
      ENDIF
      ISTAT=NF90_PUT_VAR(NCID,ITDIMID,TT,ISTART1,ICOUNT1)
c***
      ICOUNT1(1)=IMAX
      ISTAT=NF90_PUT_VAR(NCID,IAZIVARID,RAZI,ISTART1,ICOUNT1)
      ISTAT=NF90_PUT_VAR(NCID,IAVARID,RALT,ISTART1,ICOUNT1)
      ISTAT=NF90_PUT_VAR(NCID,ILAVARID,RLAT,ISTART1,ICOUNT1)
      ISTAT=NF90_PUT_VAR(NCID,ILOVARID,RLON,ISTART1,ICOUNT1)
      ISTAT=NF90_PUT_VAR(NCID,ISECVARID,RSECS,ISTART1,ICOUNT1)
      ISTAT=NF90_PUT_VAR(NCID,IUAIRVARID,UAIR,ISTART,ICOUNT1)
      ISTAT=NF90_PUT_VAR(NCID,IVAIRVARID,VAIR,ISTART,ICOUNT1)
      ISTAT=NF90_PUT_VAR(NCID,IWAIRVARID,WAIR,ISTART,ICOUNT1)
      ISTAT=NF90_PUT_VAR(NCID,IVGWAIRVARID,VGWAIR,ISTART,ICOUNT1)
      ISTAT=NF90_PUT_VAR(NCID,IWSAIRVARID,WSAIR,ISTART,ICOUNT1)
      ICOUNT(1)=1
      ICOUNT(2)=KMAX
      ICOUNT(3)=1
      ICOUNT(4)=IMAX
      ISTART(1)=1
      ISTART(2)=1
      ISTART(3)=1
      ISTART(4)=1
      ISTAT=NF90_PUT_VAR(NCID,IUVARID,U,ISTART,ICOUNT)
      ISTAT=NF90_PUT_VAR(NCID,IVVARID,V,ISTART,ICOUNT)
      ISTAT=NF90_PUT_VAR(NCID,IWVARID,W,ISTART,ICOUNT)
      ISTAT=NF90_PUT_VAR(NCID,IWSVARID,WS,ISTART,ICOUNT)
      ISTAT=NF90_PUT_VAR(NCID,IDBZVARID,DBZ,ISTART,ICOUNT)
      ISTAT=NF90_CLOSE(NCID)
      RETURN
      END
      SUBROUTINE U1U2(U,U2,IMAX,JMAX,KMAX,INOUT)
      REAL U(IMAX,JMAX,KMAX),U2(KMAX,1,IMAX)
      DO I=1,IMAX
       DO J=1,1
        DO K=1,KMAX
         U2(K,J,I)=U(I,INOUT,K)
        ENDDO
       ENDDO
      ENDDO
      CALL RESETU1U2(U,U2,IMAX,JMAX,KMAX,INOUT)
      RETURN
      END
      SUBROUTINE RESETU1U2(U,U2,IMAX,JMAX,KMAX,INOUT)
      REAL U(IMAX,JMAX,KMAX),U2(IMAX,JMAX,KMAX)
      DO K=1,KMAX
       DO J=1,JMAX
        DO I=1,IMAX
         U(I,J,K)=U2(I,J,K)
        ENDDO
       ENDDO
      ENDDO
      RETURN
      END
      SUBROUTINE REMAP(U,U2,X,Y,SX,XZ,IMAX,JMAX,KMAX,OLON)
      REAL U(IMAX,JMAX,KMAX),U2(IMAX,JMAX,KMAX),
     1 X(IMAX),X2(1000),Y(JMAX)
      INTEGER A,B
      DO K=1,KMAX
       DO J=1,JMAX
        DO I=1,IMAX
         U2(I,J,K)=-999.9
        ENDDO
       ENDDO
      ENDDO
      DO J=1,JMAX
       DO I=1,IMAX
        X2(I)=(.5+FLOAT(I-1))*SX-XZ
        X2(I)=OLON+X2(I)/(COS(Y(J)*3.14159/180.)*111.111)
c        WRITE(6,*)'I,J,OLON,Y(J),SX,XZ,OLON,X2 = ',
c     1         I,J,OLON,Y(J),SX,XZ,OLON,X2(I)
       ENDDO
       DO I=1,IMAX
        A=1
        B=2
        DO WHILE(X2(I).GT.X(B))
         A=A+1
         B=B+1
        ENDDO
c        WRITE(6,*)'I,J,A,B,X(A),X(B),X2(I) = ',
c     1   I,J,A,B,X(A),X(B),X2(I)
        IF(X2(I).EQ.X(A).AND.A.LE.IMAX)THEN
c         WRITE(6,*)'A'
         DO K=1,KMAX
          IF(U(A,J,K).GT.-900.)U2(I,J,K)=U(A,J,K)
         ENDDO
        ELSEIF(X2(I).EQ.X(B).AND.B.LE.IMAX)THEN
c         WRITE(6,*)'B'
         DO K=1,KMAX
          IF(U(B,J,K).GT.-900.)U2(I,J,K)=U(B,J,K)
         ENDDO
        ELSE
c         WRITE(6,*)'C'
         DO K=1,KMAX
          IF(U(A,J,K).GT.-900..AND.U(B,J,K).GT.-900.)
     1    U2(I,J,K)=((X2(I)-X(A))*U(B,J,K)+(X(B)-X2(I))*U(A,J,K))
     1          /(X(B)-X(A))
         ENDDO
        ENDIF
       ENDDO 
      ENDDO
      DO K=1,KMAX
       DO J=1,JMAX
        DO I=1,IMAX
         U(I,J,K)=U2(I,J,K)
        ENDDO
       ENDDO
      ENDDO
      RETURN
      END
