      SUBROUTINE WRITENET(CNETFILE,IDNC,CNETFLAG,CSTAMP,CDESCR,CMDFFT,
     +                    CALPHA,CSHAPE,CMDTRQ,MXWAV,MXRAD,MXTHET,
     +                    MXBETA,MXPHI,MXN3,MXNAT,MXSCA,MXTIMERS,IORTH,
     +                    NX,NY,NZ,NAT0,NAT,NAT3,NSCAT,NCOMP,NORI,NWAV,
     +                    NRAD,NTHETA,NPHI,NBETA,NTIMERS,MXNX,MXNY,MXNZ,
     +                    MXCOMP,BETMID,BETMXD,THTMID,THTMXD,
     +                    PHIMID,PHIMXD,ICTHM,IPHIM,TOL,WAVEA,AEFFA,
     +                    THETA,BETA,PHI,A1,A2,ICOMP,IXYZ0,IWAV,IRAD,
     +                    ITHETA,IBETA,IPHI,IORI,AEFF,WAVE,XX,AK1,BETAD,
     +                    THETAD,PHID,AKR,CXE01R,CXE02R,CXRFR,CXEPS,
     +                    QEXT,QABS,QSCAT,G,QBKSCA,QPHA,QAV,QTRQAB,
     +                    QTRQSC,SM,SMORI,PHIN,THETAN,TIMERS)
C***********************************************************************
C Subroutine  "writenet" writes NetCDF portable binary  file
C to file cnetfile ('dd.cdf')
C
C To use netCDF option (a) set "include" statement in this routine to
C proper location of "netcdf.inc"  (b) set "ddscat.par"
C variable "cnetflag" to 'ALLCDF' or 'ORICDF', (c) modify Makefile to link
C with NetCDF library, e.g. (-L/netcdf/location -lnetcdf)
C
C Original version created by P.J.Flatau, University of California, SD
C History:
C 96.11.15 (PJF): First version of NetCDF  (Wolff's request)
C                 5a6 ---> 5a7 Converted to subroutine.
C 96.11.21 (PJF): Add IF(CNEFLAG.EQ.'ALLCDF')THEN and ENDIF
C 96.11.21 (BTD): Removed second occurrence of MXSCA in argument list
C                 here and call from WRITESCA
C 96.11.21 (BTD): added call to NCCLOS if called with CNETFLAG='NCCLOS'
C                 in order to get call to NCCLOS out of DDSCAT.f
C end history
C Copyright (C) 1996 B.T. Draine and P.J. Flatau
C This code is covered by the GNU General Public License.
C***********************************************************************
C
C IMPORTANT warning:
C NetCDF "include" statement. "include" is NOT standard FORTRAN 77.
C For location of "netcdf.inc" ask your system administrator.
C Flatau's system:
C      include '/usr/local/netcdf-2.4.2/include/netcdf.inc'
C Draine's system:
      include '/u/flatau/netcdf-2.4.3/include/netcdf.inc'
C***********************************************************************
C     .. Scalar Arguments ..
      REAL AEFF,AK1,BETAD,BETMID,BETMXD,PHID,PHIMID,PHIMXD,THETAD,
     +     THTMID,THTMXD,TOL,WAVE,XX
      INTEGER IBETA,ICTHM,IDNC,IORI,IORTH,IPHI,IPHIM,IRAD,ITHETA,IWAV,
     +        MXBETA,MXCOMP,MXN3,MXNAT,MXNX,MXNY,MXNZ,MXPHI,MXRAD,MXSCA,
     +        MXTHET,MXTIMERS,MXWAV,NAT,NAT0,NAT3,NBETA,NCOMP,NORI,NPHI,
     +        NRAD,NSCAT,NTHETA,NTIMERS,NWAV,NX,NY,NZ
      CHARACTER CALPHA*6,CMDFFT*6,CMDTRQ*6,CNETFLAG*6,CSHAPE*6,
     +          CNETFILE*14,CSTAMP*22,CDESCR*67
C     .. Array Arguments ..
      COMPLEX CXE01R(3),CXE02R(3),CXEPS(MXCOMP),CXRFR(MXCOMP)
      REAL A1(3),A2(3),AEFFA(MXRAD),AKR(3),BETA(MXBETA),G(2),PHI(MXPHI),
     +     PHIN(MXSCA),QABS(2),QAV(17),QBKSCA(2),QEXT(2),QPHA(2),
     +     QSCAT(2),QTRQAB(3,2),QTRQSC(3,2),SM(MXSCA,4,4),
     +     SMORI(MXSCA,4,4),THETA(MXTHET),THETAN(MXSCA),
     +     TIMERS(MXTIMERS),WAVEA(MXWAV)
      INTEGER*2 ICOMP(MXN3),IXYZ0(MXNAT,3)
C     .. Local Scalars ..
      INTEGER ID17,ID2,ID3,IDA1,IDA2,IDAEFF,IDAEFFA,IDAK1,IDBETA,
     +        IDBETMID,IDBETMXD,IDC22,IDC6,IDC67,IDCALPHA,IDCDESCR,
     +        IDCMDFFT,IDCMDTRQ,IDCSHAPE,IDCSTAMP,IDCXRFR,IDG,IDICOMP,
     +        IDICTHM,IDIORTH,IDIPHIM,IDIX0,IDIY0,IDIZ0,IDNAT,IDNAT0,
     +        IDNAT3,IDNBETA,IDNCOMP,IDNORI,IDNPHI,IDNRAD,IDNSCAT,
     +        IDNTHETA,IDNTIMERS,IDNWAV,IDNX,IDNY,IDNZ,IDONE,IDPHI,
     +        IDPHIMID,IDPHIMXD,IDPHIN,IDQABS,IDQAV,IDQBKSCA,IDQEXT,
     +        IDQPHA,IDQSCAT,IDQTRQAB,IDQTRQSC,IDSM11,IDSM12,IDSM13,
     +        IDSM14,IDSM21,IDSM22,IDSM23,IDSM24,IDSM31,IDSM32,IDSM33,
     +        IDSM34,IDSM41,IDSM42,IDSM43,IDSM44,IDSMORI11,IDSMORI12,
     +        IDSMORI13,IDSMORI14,IDSMORI21,IDSMORI22,IDSMORI23,
     +        IDSMORI24,IDSMORI31,IDSMORI32,IDSMORI33,IDSMORI34,
     +        IDSMORI41,IDSMORI42,IDSMORI43,IDSMORI44,IDTHETA,IDTHETAN,
     +        IDTHTMID,IDTHTMXD,IDTOL,IDWAVE,IDWAVEA,IDXX,IENTRY,IERR
C (defined in netcdf.inc)      +        NCCHAR,NCCLOB,NCFLOAT,NCLONG,NCSHORT
C     .. Local Arrays ..
      INTEGER ICOUNT(7),IDDIM(7),ISTART(7)
C     .. External Functions ..
C (defined in netcdf.inc)       INTEGER NCCRE,NCDDEF,NCVDEF
C (defined in netcdf.inc)      EXTERNAL NCCRE,NCDDEF,NCVDEF
C     .. External Subroutines ..
      EXTERNAL NCAPTC,NCENDF,NCVPT,NCVPT1,NCVPTC,WRIMSG
C     .. Save statement ..
      SAVE IENTRY,IDQEXT,IDQABS,IDQSCAT,IDG,IDQBKSCA,IDQPHA,IDQAV,
     +     IDQTRQAB,IDQTRQSC,IDSM11,IDSM12,IDSM13,IDSM14,IDSM21,IDSM22,
     +     IDSM23,IDSM24,IDSM31,IDSM32,IDSM33,IDSM34,IDSM41,IDSM42,
     +     IDSM43,IDSM44,IDSMORI11,IDSMORI12,IDSMORI13,IDSMORI14,
     +     IDSMORI21,IDSMORI22,IDSMORI23,IDSMORI24,IDSMORI31,IDSMORI32,
     +     IDSMORI33,IDSMORI34,IDSMORI41,IDSMORI42,IDSMORI43,IDSMORI44,
     +     IDWAVE,IDAEFF,IDXX,IDAK1,IDCXRFR
C     .. Data statements ..

      DATA IENTRY/0/

C 1) IENTRY=0 defines first entry to this routine. All NetCDF
C    dimensions will be  defined in this step. This part of the
C     code is never executed again.NetCDF "id" variables are saved.
C 2) iori=0. Routine is called for iwav, irad, itheta, ibeta, iphi
C    These 5 variables can be split to two groups:
C    ("iwav", "irad") and ("itheta", "iphi", "ibeta").
C    The second group defines orientations of target and is also
C    defined by variable "iori". If "iori=0"  the code
C    finished all specified ntheta, nbeta, nphi orientations
C    and it is time to write orientational averages.
C 3) If iori .ne. 0 we cycle over orientations and iwav, irad.
C    Many-dimensional arrays are stored on disk.

      IF(CNETFLAG.EQ.'NCCLOS')THEN
         CALL NCCLOS(IDNC,IERR)
         RETURN
      ENDIF
      IF (IENTRY.EQ.0) THEN
          CALL WRIMSG('WRITENET',' First entry to NetCDF write')
          IENTRY = 1
C this dd.cdf should be "cnetfile"
          IDNC = NCCRE('dd.cdf',NCCLOB,IERR)
C NetCDF dimensions
C character variables
          IDC6 = NCDDEF(IDNC,'c6',6,IERR)
          IDC67 = NCDDEF(IDNC,'c67',67,IERR)
          IDC22 = NCDDEF(IDNC,'c22',22,IERR)
          IDCALPHA = NCVDEF(IDNC,'calpha',NCCHAR,1,IDC6,IERR)
          IDCDESCR = NCVDEF(IDNC,'cdescr',NCCHAR,1,IDC67,IERR)
          IDCMDFFT = NCVDEF(IDNC,'cmdfft',NCCHAR,1,IDC6,IERR)
          IDCMDTRQ = NCVDEF(IDNC,'cmdtrq',NCCHAR,1,IDC6,IERR)
          IDCSHAPE = NCVDEF(IDNC,'cshape',NCCHAR,1,IDC6,IERR)
          IDCSTAMP = NCVDEF(IDNC,'cstamp',NCCHAR,1,IDC22,IERR)
C single variables
          IDONE = NCDDEF(IDNC,'one',1,IERR)
          IDIORTH = NCVDEF(IDNC,'iorth',NCLONG,1,IDONE,IERR)
          IDBETMID = NCVDEF(IDNC,'betmid',NCFLOAT,1,IDONE,IERR)
          IDBETMXD = NCVDEF(IDNC,'betmxd',NCFLOAT,1,IDONE,IERR)
          IDTHTMID = NCVDEF(IDNC,'thtmid',NCFLOAT,1,IDONE,IERR)
          IDTHTMXD = NCVDEF(IDNC,'thtmxd',NCFLOAT,1,IDONE,IERR)
          IDPHIMID = NCVDEF(IDNC,'phimid',NCFLOAT,1,IDONE,IERR)
          IDPHIMXD = NCVDEF(IDNC,'phimxd',NCFLOAT,1,IDONE,IERR)
          IDICTHM = NCVDEF(IDNC,'icthm',NCLONG,1,IDONE,IERR)
          IDIPHIM = NCVDEF(IDNC,'iphim',NCLONG,1,IDONE,IERR)
          IDTOL = NCVDEF(IDNC,'tol',NCFLOAT,1,IDONE,IERR)
C dimensions arrays
          IDNX = NCDDEF(IDNC,'nx',NX,IERR)
          IDNY = NCDDEF(IDNC,'ny',NY,IERR)
          IDNZ = NCDDEF(IDNC,'nz',NZ,IERR)
          IDNAT0 = NCDDEF(IDNC,'nat0',NAT0,IERR)
          IDNAT = NCDDEF(IDNC,'nat',NAT,IERR)
          IDNAT3 = NCDDEF(IDNC,'nat3',NAT3,IERR)
          IDNSCAT = NCDDEF(IDNC,'nscat',NSCAT,IERR)
          IDNCOMP = NCDDEF(IDNC,'ncomp',NCOMP,IERR)
          IDNORI = NCDDEF(IDNC,'nori',NORI,IERR)
          IDNWAV = NCDDEF(IDNC,'nwav',NWAV,IERR)
          IDNRAD = NCDDEF(IDNC,'nrad',NRAD,IERR)
          IDNTHETA = NCDDEF(IDNC,'ntheta',NTHETA,IERR)
          IDNPHI = NCDDEF(IDNC,'nphi',NPHI,IERR)
          IDNBETA = NCDDEF(IDNC,'nbeta',NBETA,IERR)
          IDNTIMERS = NCDDEF(IDNC,'ntimers',NTIMERS,IERR)
          ID2 = NCDDEF(IDNC,'two',2,IERR)
          ID3 = NCDDEF(IDNC,'three',3,IERR)
          ID17 = NCDDEF(IDNC,'seventeen',17,IERR)
C arrays
          IDWAVEA = NCVDEF(IDNC,'wavea',NCFLOAT,1,IDNWAV,IERR)
          CALL NCAPTC(IDNC,IDWAVEA,'long_name',NCCHAR,16,
     +                'wavelength array',IERR)
          IDAEFFA = NCVDEF(IDNC,'aeffa',NCFLOAT,1,IDNRAD,IERR)
          CALL NCAPTC(IDNC,IDAEFFA,'long_name',NCCHAR,22,
     +                'effective radius array',IERR)
          IDTHETA = NCVDEF(IDNC,'theta',NCFLOAT,1,IDNTHETA,IERR)
          CALL NCAPTC(IDNC,IDTHETA,'long_name',NCCHAR,38,
     +                'theta (target orientation angle array)',IERR)
          IDBETA = NCVDEF(IDNC,'beta',NCFLOAT,1,IDNBETA,IERR)
          CALL NCAPTC(IDNC,IDBETA,'long_name',NCCHAR,37,
     +                'beta (target orientation angle array)',IERR)
          IDPHI = NCVDEF(IDNC,'phi',NCFLOAT,1,IDNPHI,IERR)
          CALL NCAPTC(IDNC,IDPHI,'long_name',NCCHAR,36,
     +                'phi (target orientation angle array)',IERR)

          IDA1 = NCVDEF(IDNC,'a1',NCFLOAT,1,ID3,IERR)
          CALL NCAPTC(IDNC,IDA1,'long_name',NCCHAR,24,
     +                'target definition vector',IERR)
          IDA2 = NCVDEF(IDNC,'a2',NCFLOAT,1,ID3,IERR)
          CALL NCAPTC(IDNC,IDA2,'long_name',NCCHAR,24,
     +                'target definition vector',IERR)
          IDICOMP = NCVDEF(IDNC,'icomp',NCSHORT,1,IDNAT3,IERR)
          CALL NCAPTC(IDNC,IDICOMP,'long_name',NCCHAR,39,
     +                'dipole composition definition (0-empty)',IERR)
C let as split ixyz0
          IDIX0 = NCVDEF(IDNC,'ix0',NCSHORT,1,IDNAT,IERR)
          IDIY0 = NCVDEF(IDNC,'iy0',NCSHORT,1,IDNAT,IERR)
          IDIZ0 = NCVDEF(IDNC,'iz0',NCSHORT,1,IDNAT,IERR)
C declare "running" variables dimensions
cc         WRITE(IOBIN) IWAV, IRAD, IORI, (timers(i), i=1,ntimers)
cc         WRITE(iobin) AEFF,WAVE,XX, AK1, BETAD,THETAD,PHID,
cc     $         AKR, CXE01R, CXE02R,
cc     $         (CXRFR(i), i=1, ncomp), (cxeps(i),i=1, ncomp)
cc         WRITE(iobin) QEXT, QABS, QSCAT, G, QBKSCA, QPHA, QAV,
cc     $               QTRQAB, QTRQSC
c          idone=ncddef(idnc,'one',1,ierr)
c          idiorth=ncvdef(idnc,'iorth',nclong,1, idone,ierr)
C
          IDDIM(1) = IDNWAV
          IDWAVE = NCVDEF(IDNC,'wave',NCFLOAT,1,IDDIM,IERR)
          IDDIM(1) = IDNRAD
          IDAEFF = NCVDEF(IDNC,'aeff',NCFLOAT,1,IDDIM,IERR)
          IDDIM(1) = IDNWAV
          IDDIM(2) = IDNRAD
          IDXX = NCVDEF(IDNC,'xx',NCFLOAT,2,IDDIM,IERR)
          IDAK1 = NCVDEF(IDNC,'ak1',NCFLOAT,2,IDDIM,IERR)
C cxrfr is complex but NetCDF doesn't support complex:
          IDDIM(1) = IDNWAV
          IDDIM(2) = ID2
          IDDIM(3) = IDNCOMP
          IDCXRFR = NCVDEF(IDNC,'cxrfr',NCFLOAT,3,IDDIM,IERR)
          IDDIM(1) = IDNWAV
          IDDIM(2) = IDNRAD
          IDDIM(3) = IDNSCAT
          IDSMORI11 = NCVDEF(IDNC,'smori11',NCFLOAT,3,IDDIM,IERR)
          IDSMORI12 = NCVDEF(IDNC,'smori12',NCFLOAT,3,IDDIM,IERR)
          IDSMORI13 = NCVDEF(IDNC,'smori13',NCFLOAT,3,IDDIM,IERR)
          IDSMORI14 = NCVDEF(IDNC,'smori14',NCFLOAT,3,IDDIM,IERR)
          IDSMORI21 = NCVDEF(IDNC,'smori21',NCFLOAT,3,IDDIM,IERR)
          IDSMORI22 = NCVDEF(IDNC,'smori22',NCFLOAT,3,IDDIM,IERR)
          IDSMORI23 = NCVDEF(IDNC,'smori23',NCFLOAT,3,IDDIM,IERR)
          IDSMORI24 = NCVDEF(IDNC,'smori24',NCFLOAT,3,IDDIM,IERR)
          IDSMORI31 = NCVDEF(IDNC,'smori31',NCFLOAT,3,IDDIM,IERR)
          IDSMORI32 = NCVDEF(IDNC,'smori32',NCFLOAT,3,IDDIM,IERR)
          IDSMORI33 = NCVDEF(IDNC,'smori33',NCFLOAT,3,IDDIM,IERR)
          IDSMORI34 = NCVDEF(IDNC,'smori34',NCFLOAT,3,IDDIM,IERR)
          IDSMORI41 = NCVDEF(IDNC,'smori41',NCFLOAT,3,IDDIM,IERR)
          IDSMORI42 = NCVDEF(IDNC,'smori42',NCFLOAT,3,IDDIM,IERR)
          IDSMORI43 = NCVDEF(IDNC,'smori43',NCFLOAT,3,IDDIM,IERR)
          IDSMORI44 = NCVDEF(IDNC,'smori44',NCFLOAT,3,IDDIM,IERR)
C real 6-7 dimensional cases
          IDDIM(1) = IDNWAV
          IDDIM(2) = IDNRAD
          IDDIM(3) = IDNTHETA
          IDDIM(4) = IDNBETA
          IDDIM(5) = IDNPHI
          IDDIM(6) = ID2
          IDQEXT = NCVDEF(IDNC,'qext',NCFLOAT,6,IDDIM,IERR)
          IDQABS = NCVDEF(IDNC,'qabs',NCFLOAT,6,IDDIM,IERR)
          IDQSCAT = NCVDEF(IDNC,'qscat',NCFLOAT,6,IDDIM,IERR)
          IDG = NCVDEF(IDNC,'g',NCFLOAT,6,IDDIM,IERR)
          IDQBKSCA = NCVDEF(IDNC,'qbksca',NCFLOAT,6,IDDIM,IERR)
          IDQPHA = NCVDEF(IDNC,'qpha',NCFLOAT,6,IDDIM,IERR)
          IDDIM(6) = ID17
          IDQAV = NCVDEF(IDNC,'qav',NCFLOAT,6,IDDIM,IERR)
          IDDIM(6) = ID3
          IDDIM(7) = ID2
          IDQTRQAB = NCVDEF(IDNC,'qtrqab',NCFLOAT,7,IDDIM,IERR)
          IDQTRQSC = NCVDEF(IDNC,'qtrqsc',NCFLOAT,7,IDDIM,IERR)
          IDDIM(6) = IDNSCAT
          IF(CNETFLAG.EQ.'ALLCDF')THEN
             IDSM11 = NCVDEF(IDNC,'sm11',NCFLOAT,6,IDDIM,IERR)
             IDSM12 = NCVDEF(IDNC,'sm12',NCFLOAT,6,IDDIM,IERR)
             IDSM13 = NCVDEF(IDNC,'sm13',NCFLOAT,6,IDDIM,IERR)
             IDSM14 = NCVDEF(IDNC,'sm14',NCFLOAT,6,IDDIM,IERR)
             IDSM21 = NCVDEF(IDNC,'sm21',NCFLOAT,6,IDDIM,IERR)
             IDSM22 = NCVDEF(IDNC,'sm22',NCFLOAT,6,IDDIM,IERR)
             IDSM23 = NCVDEF(IDNC,'sm23',NCFLOAT,6,IDDIM,IERR)
             IDSM24 = NCVDEF(IDNC,'sm24',NCFLOAT,6,IDDIM,IERR)
             IDSM31 = NCVDEF(IDNC,'sm31',NCFLOAT,6,IDDIM,IERR)
             IDSM32 = NCVDEF(IDNC,'sm32',NCFLOAT,6,IDDIM,IERR)
             IDSM33 = NCVDEF(IDNC,'sm33',NCFLOAT,6,IDDIM,IERR)
             IDSM34 = NCVDEF(IDNC,'sm34',NCFLOAT,6,IDDIM,IERR)
             IDSM41 = NCVDEF(IDNC,'sm41',NCFLOAT,6,IDDIM,IERR)
             IDSM42 = NCVDEF(IDNC,'sm42',NCFLOAT,6,IDDIM,IERR)
             IDSM43 = NCVDEF(IDNC,'sm43',NCFLOAT,6,IDDIM,IERR)
             IDSM44 = NCVDEF(IDNC,'sm44',NCFLOAT,6,IDDIM,IERR)
          ENDIF
          IDPHIN = NCVDEF(IDNC,'phin',NCFLOAT,1,IDNSCAT,IERR)
          CALL NCAPTC(IDNC,IDPHIN,'long_name',NCCHAR,16,
     +                'scattering angle',IERR)
          IDTHETAN = NCVDEF(IDNC,'thetan',NCFLOAT,1,IDNSCAT,IERR)
          CALL NCAPTC(IDNC,IDTHETAN,'long_name',NCCHAR,16,
     +                'scattering angle',IERR)
          CALL NCENDF(IDNC,IERR)
C write characters
          CALL NCVPTC(IDNC,IDCALPHA,1,6,CALPHA,6,IERR)
          CALL NCVPTC(IDNC,IDCDESCR,1,67,CDESCR,67,IERR)
          CALL NCVPTC(IDNC,IDCMDFFT,1,6,CMDFFT,6,IERR)
          CALL NCVPTC(IDNC,IDCMDTRQ,1,6,CMDTRQ,6,IERR)
          CALL NCVPTC(IDNC,IDCSHAPE,1,6,CSHAPE,6,IERR)
          CALL NCVPTC(IDNC,IDCSTAMP,1,22,CSTAMP,22,IERR)
C write single variables:
          CALL NCVPT1(IDNC,IDIORTH,1,IORTH,IERR)
          CALL NCVPT1(IDNC,IDBETMID,1,BETMID,IERR)
          CALL NCVPT1(IDNC,IDBETMXD,1,BETMXD,IERR)
          CALL NCVPT1(IDNC,IDTHTMID,1,THTMID,IERR)
          CALL NCVPT1(IDNC,IDTHTMXD,1,THTMXD,IERR)
          CALL NCVPT1(IDNC,IDPHIMID,1,PHIMID,IERR)
          CALL NCVPT1(IDNC,IDPHIMXD,1,PHIMXD,IERR)
          CALL NCVPT1(IDNC,IDICTHM,1,ICTHM,IERR)
          CALL NCVPT1(IDNC,IDIPHIM,1,IPHIM,IERR)
          CALL NCVPT1(IDNC,IDTOL,1,TOL,IERR)
C write arrays
          CALL NCVPT(IDNC,IDWAVEA,1,NWAV,WAVEA,IERR)
          CALL NCVPT(IDNC,IDAEFFA,1,NRAD,AEFFA,IERR)
          CALL NCVPT(IDNC,IDTHETA,1,NTHETA,THETA,IERR)
          CALL NCVPT(IDNC,IDBETA,1,NBETA,BETA,IERR)
          CALL NCVPT(IDNC,IDPHI,1,NPHI,PHI,IERR)
          CALL NCVPT(IDNC,IDA1,1,3,A1,IERR)
          CALL NCVPT(IDNC,IDA2,1,3,A2,IERR)
          CALL NCVPT(IDNC,IDICOMP,1,NAT3,ICOMP,IERR)
C print, idix0
          CALL NCVPT(IDNC,IDIX0,1,NAT,IXYZ0(1,1),IERR)
          CALL NCVPT(IDNC,IDIY0,1,NAT,IXYZ0(1,2),IERR)
          CALL NCVPT(IDNC,IDIZ0,1,NAT,IXYZ0(1,3),IERR)
          CALL NCVPT(IDNC,IDPHIN,1,NSCAT,PHIN,IERR)
          CALL NCVPT(IDNC,IDTHETAN,1,NSCAT,THETAN,IERR)
C endif      "if(ientry .eq. 0) then"
      END IF


      IF (IORI.NE.0 .AND. CNETFLAG.EQ.'ALLCDF') THEN
C NetCDF option --- running variables
C first 5 dimensions are always the same:
C define "lower left"  corner of the (many dimensional) box

C some stuff will be written over and over
          ISTART(1) = IWAV
          ICOUNT(1) = 1
          CALL NCVPT(IDNC,IDWAVE,ISTART,ICOUNT,WAVE,IERR)
          ISTART(1) = IRAD
          ICOUNT(1) = 1
          CALL NCVPT(IDNC,IDAEFF,ISTART,ICOUNT,AEFF,IERR)
          ISTART(1) = IWAV
          ISTART(2) = IRAD
          ICOUNT(1) = 1
          ICOUNT(2) = 1
          CALL NCVPT(IDNC,IDXX,ISTART,ICOUNT,XX,IERR)
          CALL NCVPT(IDNC,IDAK1,ISTART,ICOUNT,AK1,IERR)
          ISTART(1) = IWAV
          ISTART(2) = 1
          ISTART(3) = 1
          ICOUNT(1) = 1
          ICOUNT(2) = 2
          ICOUNT(3) = NCOMP
          CALL NCVPT(IDNC,IDCXRFR,ISTART,ICOUNT,CXRFR,IERR)
C
          ISTART(1) = IWAV
          ISTART(2) = IRAD
          ISTART(3) = ITHETA
          ISTART(4) = IBETA
          ISTART(5) = IPHI
          ISTART(6) = 1
          ISTART(7) = 1
C define how many elements to write
          ICOUNT(1) = 1
          ICOUNT(2) = 1
          ICOUNT(3) = 1
          ICOUNT(4) = 1
          ICOUNT(5) = 1
          ICOUNT(6) = 2
          CALL NCVPT(IDNC,IDQEXT,ISTART,ICOUNT,QEXT,IERR)
          CALL NCVPT(IDNC,IDQABS,ISTART,ICOUNT,QABS,IERR)
          CALL NCVPT(IDNC,IDQSCAT,ISTART,ICOUNT,QSCAT,IERR)
          CALL NCVPT(IDNC,IDG,ISTART,ICOUNT,G,IERR)
          CALL NCVPT(IDNC,IDQBKSCA,ISTART,ICOUNT,QBKSCA,IERR)
          CALL NCVPT(IDNC,IDQPHA,ISTART,ICOUNT,QPHA,IERR)
          ICOUNT(6) = 17
          CALL NCVPT(IDNC,IDQAV,ISTART,ICOUNT,QAV,IERR)
          ICOUNT(6) = 3
          ICOUNT(7) = 2
          CALL NCVPT(IDNC,IDQTRQAB,ISTART,ICOUNT,QTRQAB,IERR)
          CALL NCVPT(IDNC,IDQTRQSC,ISTART,ICOUNT,QTRQSC,IERR)
C Muller matrix (IDL handles only 7 dimensions) - so 16 ncvpt's
          ICOUNT(6) = NSCAT
          CALL NCVPT(IDNC,IDSM11,ISTART,ICOUNT,SM(1,1,1),IERR)
          CALL NCVPT(IDNC,IDSM12,ISTART,ICOUNT,SM(1,1,2),IERR)
          CALL NCVPT(IDNC,IDSM13,ISTART,ICOUNT,SM(1,1,3),IERR)
          CALL NCVPT(IDNC,IDSM14,ISTART,ICOUNT,SM(1,1,4),IERR)
          CALL NCVPT(IDNC,IDSM21,ISTART,ICOUNT,SM(1,2,1),IERR)
          CALL NCVPT(IDNC,IDSM22,ISTART,ICOUNT,SM(1,2,2),IERR)
          CALL NCVPT(IDNC,IDSM23,ISTART,ICOUNT,SM(1,2,3),IERR)
          CALL NCVPT(IDNC,IDSM24,ISTART,ICOUNT,SM(1,2,4),IERR)
          CALL NCVPT(IDNC,IDSM31,ISTART,ICOUNT,SM(1,3,1),IERR)
          CALL NCVPT(IDNC,IDSM32,ISTART,ICOUNT,SM(1,3,2),IERR)
          CALL NCVPT(IDNC,IDSM33,ISTART,ICOUNT,SM(1,3,3),IERR)
          CALL NCVPT(IDNC,IDSM34,ISTART,ICOUNT,SM(1,3,4),IERR)
          CALL NCVPT(IDNC,IDSM41,ISTART,ICOUNT,SM(1,4,1),IERR)
          CALL NCVPT(IDNC,IDSM42,ISTART,ICOUNT,SM(1,4,2),IERR)
          CALL NCVPT(IDNC,IDSM43,ISTART,ICOUNT,SM(1,4,3),IERR)
          CALL NCVPT(IDNC,IDSM44,ISTART,ICOUNT,SM(1,4,4),IERR)
C endif   "if(iori. ne. 0  .and. cnetflag.eq.'ALLCDF') then"
      END IF

C Here only for iori=0. Write smori to netCDF:
      IF (IORI.EQ.0) THEN
          ISTART(1) = IWAV
          ISTART(2) = IRAD
          ISTART(3) = 1
          ICOUNT(1) = 1
          ICOUNT(2) = 1
          ICOUNT(3) = NSCAT
          CALL NCVPT(IDNC,IDSMORI11,ISTART,ICOUNT,SMORI(1,1,1),IERR)
          CALL NCVPT(IDNC,IDSMORI12,ISTART,ICOUNT,SMORI(1,1,2),IERR)
          CALL NCVPT(IDNC,IDSMORI13,ISTART,ICOUNT,SMORI(1,1,3),IERR)
          CALL NCVPT(IDNC,IDSMORI14,ISTART,ICOUNT,SMORI(1,1,4),IERR)
          CALL NCVPT(IDNC,IDSMORI21,ISTART,ICOUNT,SMORI(1,2,1),IERR)
          CALL NCVPT(IDNC,IDSMORI22,ISTART,ICOUNT,SMORI(1,2,2),IERR)
          CALL NCVPT(IDNC,IDSMORI23,ISTART,ICOUNT,SMORI(1,2,3),IERR)
          CALL NCVPT(IDNC,IDSMORI24,ISTART,ICOUNT,SMORI(1,2,4),IERR)
          CALL NCVPT(IDNC,IDSMORI31,ISTART,ICOUNT,SMORI(1,3,1),IERR)
          CALL NCVPT(IDNC,IDSMORI32,ISTART,ICOUNT,SMORI(1,3,2),IERR)
          CALL NCVPT(IDNC,IDSMORI33,ISTART,ICOUNT,SMORI(1,3,3),IERR)
          CALL NCVPT(IDNC,IDSMORI34,ISTART,ICOUNT,SMORI(1,3,4),IERR)
          CALL NCVPT(IDNC,IDSMORI41,ISTART,ICOUNT,SMORI(1,4,1),IERR)
          CALL NCVPT(IDNC,IDSMORI42,ISTART,ICOUNT,SMORI(1,4,2),IERR)
          CALL NCVPT(IDNC,IDSMORI43,ISTART,ICOUNT,SMORI(1,4,3),IERR)
          CALL NCVPT(IDNC,IDSMORI44,ISTART,ICOUNT,SMORI(1,4,4),IERR)
c endif    "if(iori.eq.0) then
      END IF

      RETURN

      END

