C-----------------------------------------------------------------------
C  TDB_MAKSYM -- PRESYMMETRIZE PROFILE DATA FOR NEXT GEOMETRY FRAME
C
c  dmc -- modularization -- May 2005
c    dependence on trcom removed!
c
C  NEW DMC JULY 1991 -- PORTIONS GENERATED BY TRDATGEN!
C
C  The purpose of this routine is to perform onion skin symmetrization
C  on any input profile data for which this symmetrization algorithm is
C  requested.
C
C  Symmetrization is carried out over the entire time range of the
C  data (on the LTIME2 - NTIME2 time grid).  For normal data, the
C  symmetrized data is stored vs. minor radius; for ECE it is stored
C  vs. a normalized aspect ratio (related to (1/mod(B)).  This allows
C  the symmetrization to be done without prior knowledge
C  of the flux surface equilibrium geometry.
C
      SUBROUTINE TDB_MAKSYM(d,IER)
C
C--------------------------------------
C
      use trdatbuf_obj
 
      IMPLICIT NONE
 
      type (trdatbuf) :: d
 
      INTEGER, PARAMETER :: R8=SELECTED_REAL_KIND(12,100)
 
!============
! idecl:  explicitize implicit INTEGER declarations:
      INTEGER ier,imap
      INTEGER jsav,i,j,idum_lxsy,idum_nxsy,isizb,ineed
      integer :: nonlin,lunmsg_tdb
!============
! idecl:  explicitize implicit REAL declarations:
      REAL*8 ztl
!============
      CHARACTER*25 ZZLABEL        ! label for 3d profile ID
C
C--------------------------------------
C
      IER=0
C
      nonlin = lunmsg_tdb(0)
C
C  check that initialization was done...
C
      if(d%lbx(1).eq.0) then
         ier=100
         write(nonlin,*)
     >        ' ? tdb_maksym: error exit -- "d%lbx" unitialized'
         write(nonlin,*)
     >        ' ? call to "tdb_symini" is needed.'
         return
      endif
C
C  A GAP is left IN workBUF STARTING AT workBUF(LFREE_W)
C  AND LEAVING ROOM FOR NBX(1) = MAX NO. X PTS IN PROFILE DATA
C  DATA POINTS -- USED AS A WORKSPACE BY EXECSYM
C
      call trdatbuf_expand(d%WORKBUF,d%lfree_w+d%nbx(1),isizb)
C
C  NBX(1) WAS COMPUTED IN SUBROUTINE TR2INI
C
C=>TRDATGEN+
!
!    ******************************************
!    * TRDATGEN GENERATED CODE -- DO NOT EDIT *
!    ******************************************
!
!    code generation ends at C=>TRDATGEN- line
!
!
C
      IF((IABS(D%NRIAXR).EQ.3).AND.(D%NSYAXR.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'AXR',IMAP,
     >       D%LXAXR,D%NXAXR,D%LFAXR,
     >       D%LXSYAXR,D%NXSYAXR,D%LFSYAXR,D%LSSYAXR, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIBOL).EQ.3).AND.(D%NSYBOL.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'BOL',IMAP,
     >       D%LXBOL,D%NXBOL,D%LFBOL,
     >       D%LXSYBOL,D%NXSYBOL,D%LFSYBOL,D%LSSYBOL, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIBPA).EQ.3).AND.(D%NSYBPA.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'BPA',IMAP,
     >       D%LXBPA,D%NXBPA,D%LFBPA,
     >       D%LXSYBPA,D%NXSYBPA,D%LFSYBPA,D%LSSYBPA, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIBPB).EQ.3).AND.(D%NSYBPB.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'BPB',IMAP,
     >       D%LXBPB,D%NXBPB,D%LFBPB,
     >       D%LXSYBPB,D%NXSYBPB,D%LFSYBPB,D%LSSYBPB, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRID2F).EQ.3).AND.(D%NSYD2F.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'D2F',IMAP,
     >       D%LXD2F,D%NXD2F,D%LFD2F,
     >       D%LXSYD2F,D%NXSYD2F,D%LFSYD2F,D%LSSYD2F, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIDE2).EQ.3).AND.(D%NSYDE2.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'DE2',IMAP,
     >       D%LXDE2,D%NXDE2,D%LFDE2,
     >       D%LXSYDE2,D%NXSYDE2,D%LFSYDE2,D%LSSYDE2, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIDF3).EQ.3).AND.(D%NSYDF3.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'DF3',IMAP,
     >       D%LXDF3,D%NXDF3,D%LFDF3,
     >       D%LXSYDF3,D%NXSYDF3,D%LFSYDF3,D%LSSYDF3, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIDF4).EQ.3).AND.(D%NSYDF4.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'DF4',IMAP,
     >       D%LXDF4,D%NXDF4,D%LFDF4,
     >       D%LXSYDF4,D%NXSYDF4,D%LFSYDF4,D%LSSYDF4, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIDF6).EQ.3).AND.(D%NSYDF6.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'DF6',IMAP,
     >       D%LXDF6,D%NXDF6,D%LFDF6,
     >       D%LXSYDF6,D%NXSYDF6,D%LFSYDF6,D%LSSYDF6, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIDFD).EQ.3).AND.(D%NSYDFD.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'DFD',IMAP,
     >       D%LXDFD,D%NXDFD,D%LFDFD,
     >       D%LXSYDFD,D%NXSYDFD,D%LFSYDFD,D%LSSYDFD, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIDFH).EQ.3).AND.(D%NSYDFH.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'DFH',IMAP,
     >       D%LXDFH,D%NXDFH,D%LFDFH,
     >       D%LXSYDFH,D%NXSYDFH,D%LFSYDFH,D%LSSYDFH, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIDFT).EQ.3).AND.(D%NSYDFT.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'DFT',IMAP,
     >       D%LXDFT,D%NXDFT,D%LFDFT,
     >       D%LXSYDFT,D%NXSYDFT,D%LFSYDFT,D%LSSYDFT, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIECF).EQ.3).AND.(D%NSYECF.EQ.1)) THEN
        IMAP = 2
        CALL TDB_EXECSYM(d,'ECF',IMAP,
     >       D%LXECF,D%NXECF,D%LFECF,
     >       D%LXSYECF,D%NXSYECF,D%LFSYECF,D%LSSYECF, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIFEH).EQ.3).AND.(D%NSYFEH.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'FEH',IMAP,
     >       D%LXFEH,D%NXFEH,D%LFFEH,
     >       D%LXSYFEH,D%NXSYFEH,D%LFSYFEH,D%LSSYFEH, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIFEL).EQ.3).AND.(D%NSYFEL.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'FEL',IMAP,
     >       D%LXFEL,D%NXFEL,D%LFFEL,
     >       D%LXSYFEL,D%NXSYFEL,D%LFSYFEL,D%LSSYFEL, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIGRB).EQ.3).AND.(D%NSYGRB.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'GRB',IMAP,
     >       D%LXGRB,D%NXGRB,D%LFGRB,
     >       D%LXSYGRB,D%NXSYGRB,D%LFSYGRB,D%LSSYGRB, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIIBE).EQ.3).AND.(D%NSYIBE.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'IBE',IMAP,
     >       D%LXIBE,D%NXIBE,D%LFIBE,
     >       D%LXSYIBE,D%NXSYIBE,D%LFSYIBE,D%LSSYIBE, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIIBI).EQ.3).AND.(D%NSYIBI.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'IBI',IMAP,
     >       D%LXIBI,D%NXIBI,D%LFIBI,
     >       D%LXSYIBI,D%NXSYIBI,D%LFSYIBI,D%LSSYIBI, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIJBM).EQ.3).AND.(D%NSYJBM.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'JBM',IMAP,
     >       D%LXJBM,D%NXJBM,D%LFJBM,
     >       D%LXSYJBM,D%NXSYJBM,D%LFSYJBM,D%LSSYJBM, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIJEC).EQ.3).AND.(D%NSYJEC.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'JEC',IMAP,
     >       D%LXJEC,D%NXJEC,D%LFJEC,
     >       D%LXSYJEC,D%NXSYJEC,D%LFSYJEC,D%LSSYJEC, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIJIC).EQ.3).AND.(D%NSYJIC.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'JIC',IMAP,
     >       D%LXJIC,D%NXJIC,D%LFJIC,
     >       D%LXSYJIC,D%NXSYJIC,D%LFSYJIC,D%LSSYJIC, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRILF3).EQ.3).AND.(D%NSYLF3.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'LF3',IMAP,
     >       D%LXLF3,D%NXLF3,D%LFLF3,
     >       D%LXSYLF3,D%NXSYLF3,D%LFSYLF3,D%LSSYLF3, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRILF4).EQ.3).AND.(D%NSYLF4.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'LF4',IMAP,
     >       D%LXLF4,D%NXLF4,D%LFLF4,
     >       D%LXSYLF4,D%NXSYLF4,D%LFSYLF4,D%LSSYLF4, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRILF6).EQ.3).AND.(D%NSYLF6.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'LF6',IMAP,
     >       D%LXLF6,D%NXLF6,D%LFLF6,
     >       D%LXSYLF6,D%NXSYLF6,D%LFSYLF6,D%LSSYLF6, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRILFD).EQ.3).AND.(D%NSYLFD.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'LFD',IMAP,
     >       D%LXLFD,D%NXLFD,D%LFLFD,
     >       D%LXSYLFD,D%NXSYLFD,D%LFSYLFD,D%LSSYLFD, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRILFH).EQ.3).AND.(D%NSYLFH.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'LFH',IMAP,
     >       D%LXLFH,D%NXLFH,D%LFLFH,
     >       D%LXSYLFH,D%NXSYLFH,D%LFSYLFH,D%LSSYLFH, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRILFT).EQ.3).AND.(D%NSYLFT.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'LFT',IMAP,
     >       D%LXLFT,D%NXLFT,D%LFLFT,
     >       D%LXSYLFT,D%NXSYLFT,D%LFSYLFT,D%LSSYLFT, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRILHE).EQ.3).AND.(D%NSYLHE.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'LHE',IMAP,
     >       D%LXLHE,D%NXLHE,D%LFLHE,
     >       D%LXSYLHE,D%NXSYLHE,D%LFSYLHE,D%LSSYLHE, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRILHJ).EQ.3).AND.(D%NSYLHJ.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'LHJ',IMAP,
     >       D%LXLHJ,D%NXLHJ,D%LFLHJ,
     >       D%LXSYLHJ,D%NXSYLHJ,D%LFSYLHJ,D%LSSYLHJ, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRINER).EQ.3).AND.(D%NSYNER.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'NER',IMAP,
     >       D%LXNER,D%NXNER,D%LFNER,
     >       D%LXSYNER,D%NXSYNER,D%LFSYNER,D%LSSYNER, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRINI3).EQ.3).AND.(D%NSYNI3.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'NI3',IMAP,
     >       D%LXNI3,D%NXNI3,D%LFNI3,
     >       D%LXSYNI3,D%NXSYNI3,D%LFSYNI3,D%LSSYNI3, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRINI4).EQ.3).AND.(D%NSYNI4.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'NI4',IMAP,
     >       D%LXNI4,D%NXNI4,D%LFNI4,
     >       D%LXSYNI4,D%NXSYNI4,D%LFSYNI4,D%LSSYNI4, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRINI6).EQ.3).AND.(D%NSYNI6.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'NI6',IMAP,
     >       D%LXNI6,D%NXNI6,D%LFNI6,
     >       D%LXSYNI6,D%NXSYNI6,D%LFSYNI6,D%LSSYNI6, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRINID).EQ.3).AND.(D%NSYNID.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'NID',IMAP,
     >       D%LXNID,D%NXNID,D%LFNID,
     >       D%LXSYNID,D%NXSYNID,D%LFSYNID,D%LSSYNID, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRINIH).EQ.3).AND.(D%NSYNIH.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'NIH',IMAP,
     >       D%LXNIH,D%NXNIH,D%LFNIH,
     >       D%LXSYNIH,D%NXSYNIH,D%LFSYNIH,D%LSSYNIH, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRINIM).EQ.3).AND.(D%NSYNIM.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'NIM',IMAP,
     >       D%LXNIM,D%NXNIM,D%LFNIM,
     >       D%LXSYNIM,D%NXSYNIM,D%LFSYNIM,D%LSSYNIM, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRINIT).EQ.3).AND.(D%NSYNIT.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'NIT',IMAP,
     >       D%LXNIT,D%NXNIT,D%LFNIT,
     >       D%LXSYNIT,D%NXSYNIT,D%LFSYNIT,D%LSSYNIT, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRINMR).EQ.3).AND.(D%NSYNMR.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'NMR',IMAP,
     >       D%LXNMR,D%NXNMR,D%LFNMR,
     >       D%LXSYNMR,D%NXSYNMR,D%LFSYNMR,D%LSSYNMR, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIOMG).EQ.3).AND.(D%NSYOMG.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'OMG',IMAP,
     >       D%LXOMG,D%NXOMG,D%LFOMG,
     >       D%LXSYOMG,D%NXSYOMG,D%LFSYOMG,D%LSSYOMG, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIPRS).EQ.3).AND.(D%NSYPRS.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'PRS',IMAP,
     >       D%LXPRS,D%NXPRS,D%LFPRS,
     >       D%LXSYPRS,D%NXSYPRS,D%LFSYPRS,D%LSSYPRS, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIQBE).EQ.3).AND.(D%NSYQBE.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'QBE',IMAP,
     >       D%LXQBE,D%NXQBE,D%LFQBE,
     >       D%LXSYQBE,D%NXSYQBE,D%LFSYQBE,D%LSSYQBE, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIQBI).EQ.3).AND.(D%NSYQBI.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'QBI',IMAP,
     >       D%LXQBI,D%NXQBI,D%LFQBI,
     >       D%LXSYQBI,D%NXSYQBI,D%LFSYQBI,D%LSSYQBI, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIQEC).EQ.3).AND.(D%NSYQEC.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'QEC',IMAP,
     >       D%LXQEC,D%NXQEC,D%LFQEC,
     >       D%LXSYQEC,D%NXSYQEC,D%LFSYQEC,D%LSSYQEC, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIQIC).EQ.3).AND.(D%NSYQIC.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'QIC',IMAP,
     >       D%LXQIC,D%NXQIC,D%LFQIC,
     >       D%LXSYQIC,D%NXSYQIC,D%LFSYQIC,D%LSSYQIC, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIQPR).EQ.3).AND.(D%NSYQPR.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'QPR',IMAP,
     >       D%LXQPR,D%NXQPR,D%LFQPR,
     >       D%LXSYQPR,D%NXSYQPR,D%LFSYQPR,D%LSSYQPR, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIRES).EQ.3).AND.(D%NSYRES.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'RES',IMAP,
     >       D%LXRES,D%NXRES,D%LFRES,
     >       D%LXSYRES,D%NXSYRES,D%LFSYRES,D%LSSYRES, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRISBI).EQ.3).AND.(D%NSYSBI.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'SBI',IMAP,
     >       D%LXSBI,D%NXSBI,D%LFSBI,
     >       D%LXSYSBI,D%NXSYSBI,D%LFSYSBI,D%LSSYSBI, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRITER).EQ.3).AND.(D%NSYTER.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'TER',IMAP,
     >       D%LXTER,D%NXTER,D%LFTER,
     >       D%LXSYTER,D%NXSYTER,D%LFSYTER,D%LSSYTER, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRITI2).EQ.3).AND.(D%NSYTI2.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'TI2',IMAP,
     >       D%LXTI2,D%NXTI2,D%LFTI2,
     >       D%LXSYTI2,D%NXSYTI2,D%LFSYTI2,D%LSSYTI2, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRITQI).EQ.3).AND.(D%NSYTQI.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'TQI',IMAP,
     >       D%LXTQI,D%NXTQI,D%LFTQI,
     >       D%LXSYTQI,D%NXSYTQI,D%LFSYTQI,D%LSSYTQI, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIV2F).EQ.3).AND.(D%NSYV2F.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'V2F',IMAP,
     >       D%LXV2F,D%NXV2F,D%LFV2F,
     >       D%LXSYV2F,D%NXSYV2F,D%LFSYV2F,D%LSSYV2F, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIVB2).EQ.3).AND.(D%NSYVB2.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'VB2',IMAP,
     >       D%LXVB2,D%NXVB2,D%LFVB2,
     >       D%LXSYVB2,D%NXSYVB2,D%LFSYVB2,D%LSSYVB2, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIVC3).EQ.3).AND.(D%NSYVC3.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'VC3',IMAP,
     >       D%LXVC3,D%NXVC3,D%LFVC3,
     >       D%LXSYVC3,D%NXSYVC3,D%LFSYVC3,D%LSSYVC3, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIVC4).EQ.3).AND.(D%NSYVC4.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'VC4',IMAP,
     >       D%LXVC4,D%NXVC4,D%LFVC4,
     >       D%LXSYVC4,D%NXSYVC4,D%LFSYVC4,D%LSSYVC4, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIVC6).EQ.3).AND.(D%NSYVC6.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'VC6',IMAP,
     >       D%LXVC6,D%NXVC6,D%LFVC6,
     >       D%LXSYVC6,D%NXSYVC6,D%LFSYVC6,D%LSSYVC6, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIVCD).EQ.3).AND.(D%NSYVCD.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'VCD',IMAP,
     >       D%LXVCD,D%NXVCD,D%LFVCD,
     >       D%LXSYVCD,D%NXSYVCD,D%LFSYVCD,D%LSSYVCD, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIVCH).EQ.3).AND.(D%NSYVCH.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'VCH',IMAP,
     >       D%LXVCH,D%NXVCH,D%LFVCH,
     >       D%LXSYVCH,D%NXSYVCH,D%LFSYVCH,D%LSSYVCH, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIVCT).EQ.3).AND.(D%NSYVCT.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'VCT',IMAP,
     >       D%LXVCT,D%NXVCT,D%LFVCT,
     >       D%LXSYVCT,D%NXSYVCT,D%LFSYVCT,D%LSSYVCT, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIVEE).EQ.3).AND.(D%NSYVEE.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'VEE',IMAP,
     >       D%LXVEE,D%NXVEE,D%LFVEE,
     >       D%LXSYVEE,D%NXSYVEE,D%LFSYVEE,D%LSSYVEE, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIVIE).EQ.3).AND.(D%NSYVIE.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'VIE',IMAP,
     >       D%LXVIE,D%NXVIE,D%LFVIE,
     >       D%LXSYVIE,D%NXSYVIE,D%LFSYVIE,D%LSSYVIE, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIVMO).EQ.3).AND.(D%NSYVMO.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'VMO',IMAP,
     >       D%LXVMO,D%NXVMO,D%LFVMO,
     >       D%LXSYVMO,D%NXSYVMO,D%LFSYVMO,D%LSSYVMO, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIVP2).EQ.3).AND.(D%NSYVP2.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'VP2',IMAP,
     >       D%LXVP2,D%NXVP2,D%LFVP2,
     >       D%LXSYVP2,D%NXSYVP2,D%LFSYVP2,D%LSSYVP2, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIVPO).EQ.3).AND.(D%NSYVPO.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'VPO',IMAP,
     >       D%LXVPO,D%NXVPO,D%LFVPO,
     >       D%LXSYVPO,D%NXSYVPO,D%LFSYVPO,D%LSSYVPO, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIVPR).EQ.3).AND.(D%NSYVPR.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'VPR',IMAP,
     >       D%LXVPR,D%NXVPR,D%LFVPR,
     >       D%LXSYVPR,D%NXSYVPR,D%LFSYVPR,D%LSSYVPR, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIVTR).EQ.3).AND.(D%NSYVTR.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'VTR',IMAP,
     >       D%LXVTR,D%NXVTR,D%LFVTR,
     >       D%LXSYVTR,D%NXSYVTR,D%LFSYVTR,D%LSSYVTR, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIZF2).EQ.3).AND.(D%NSYZF2.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'ZF2',IMAP,
     >       D%LXZF2,D%NXZF2,D%LFZF2,
     >       D%LXSYZF2,D%NXSYZF2,D%LFSYZF2,D%LSSYZF2, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      IF((IABS(D%NRIZXR).EQ.3).AND.(D%NSYZXR.EQ.1)) THEN
        IMAP = 1
        CALL TDB_EXECSYM(d,'ZXR',IMAP,
     >       D%LXZXR,D%NXZXR,D%LFZXR,
     >       D%LXSYZXR,D%NXSYZXR,D%LFSYZXR,D%LSSYZXR, IER)
        IF(IER.NE.0) GO TO 999
      ENDIF
C
      JSAV=-99
      DO I = 1, 300
        J = D%NISSIM(I)
        IF (J==0) EXIT
        IF((D%LFSIM(I).NE.0).AND.(IABS(D%NRISIM(J)).EQ.3)
     >     .AND.(D%NSYSIM(J).EQ.1)) THEN
          IMAP = 1
          WRITE(ZZLABEL,'(A,I3,A,I5)') 'SIM SET:',J,', TOT:',I
          CALL TDB_EXECSYM(d,ZZLABEL,IMAP,
     >           D%LXSIM(J),D%NXSIM(J),D%LFSIM(I),
     >           idum_LXSY,idum_NXSY,D%LFSYSIM(I),D%LSSYSIM(I),IER)
          IF(IER.NE.0) GO TO 999
          IF (J .NE. JSAV) THEN
            D%LXSYSIM(J) = idum_LXSY
            D%NXSYSIM(J) = idum_NXSY
            JSAV = J
          ELSE
            IF (idum_NXSY .NE. D%NXSYSIM(J)) IER=1
            CALL MKDBCOMP(d, D%LXSYSIM(J), idum_LXSY,
     >           MIN(D%NXSYSIM(J),idum_NXSY),IER)
            IF(IER.NE.0) THEN
              write(nonlin,*)
     > ' ?TDB_MAKSYM: SYMMETRIZED X-AXIS DO NOT MATCH IN 3D PROFILE'
              write(nonlin,*)' ?TDB_MAKSYM: DATA SET SIM'
              GOTO 999
            END IF
          END IF
        END IF
      END DO
C
C
C=>TRDATGEN-
C
      GO TO 1000
C
C  ERROR
C
 999  CONTINUE
      IER=1
C
C  EXIT
C
 1000 CONTINUE
      RETURN
      END
 
C
C ------------------------ MKDBCOMP --------------------------------
C
C COMPARES THE DATA IN THE datbuf AT TWO ADDRESSES FOR (near)EQUALITY
C
      SUBROUTINE MKDBCOMP(d, ILX1, ILX2, INX, IER)
 
      use trdatbuf_obj
      IMPLICIT NONE
 
      type (trdatbuf) :: d
 
      INTEGER, PARAMETER :: R8=SELECTED_REAL_KIND(12,100)
 
      INTEGER ILX1              ! address of data number 1 in datbuf
      INTEGER ILX2              ! address of data number 2 in datbuf
      INTEGER INX               ! length of data
      INTEGER IER               ! return true on error -- do not change IER if no error
      INTEGER J                 ! loop variable
 
      REAL*8 ZSCALE             ! scale factor for data
      REAL*8 Z1,Z2              ! data at one point
 
      zscale = 1.0d-35          ! approximate scale of x-axis
      do j = 1, inx
         z1 = d%DATBUF(ilx1+j-1)
         z2 = d%DATBUF(ilx2+j-1)
         zscale = max(abs(z1),abs(z2),zscale)
      end do
 
      do j = 1, inx
         z1 = d%DATBUF(ilx1+j-1)
         z2 = d%DATBUF(ilx2+j-1)
         if (abs(z2-z1)/zscale .GT. 1.E-5_R8) IER=1
      end do
 
      END
! 20jan2003 fgtok -s r8_precision.sub all.sub "r8con.csh conversion"
