C******************** START FILE TDB_PRESYM.FOR ; GROUP TDB_PRESYM ***********
C------------------------------------------------------------
C  TDB_PRESYM  -- modularized vsn of old PRESYM -- no trcom dependency.
C
C  INTERFACE MODIFIED DMC JULY 1991 -- PART OF GENERAL REWRITE OF THE
C  METHODS FOR ACQUIRING/INTERPOLATING UFILES INPUT DATA PROFILES IN
C  TRANSP (TRDAT UPGRADE)
C
C  TDB_PRESYM IS NOW CALLED FROM EXECSYM WHICH IS CALLED FROM MAKSYM;
C  PRESYMMETRIZATION IS CARRIED OUT IN TRDAT -- during setup of a
C  TRANSP run.
C
C  PRESYMMETRIZE TWO-SIDED PROFILE DATA-- MASTER
C  ROUTINE
C
C  THE INTERFACE HAS BEEN GENERALIZED -- NOT JUST ELECTRON TEMPERATURE
C  OR DENSITY DATA, BUT ANY DATA MAY BE USED AS INPUT.  **HOWEVER** THE
C  DATA MUST STILL BE POSITIVE AND TEND TOWARDS ZERO AT THE EDGES *****
C
C  VERSION FOR GENERALIZED GEOMETRY-- INPUT COORDINATE IS
C  DEFINED RELATIVE TO GENERALIZED SPATIAL LOCATION
C    X = (R-R0)/A  WHERE R0 AND A ARE THE PLASMA MAJOR AND
C  MINOR RADII.
C
C  THE PLASMA INNER BDY IS AT X = -1; THE OUTER BDY AT X = +1
C
C  presymmetrized output is stored on a relative minor radius, or
C  for ECE data, relative aspect ratio (will be (1/mod(B)) aspect ratio
C  at final lookup time).
C    xa=0 --> mag. axis
C    xa=1 --> region of f(xa) > f(1) has aspect ratio (R2-R1)/(R2+R1)
C
C  THIS ROUTINE CONVERTS A 2-SIDED PROFILE COVERING
C  X = (-1,+1) PLUS SOME SLOP OVER THE EDGES, TO A ONE-SIDED
C  PROFILE CONVERING X = (0,1) PLUS some margin.
C
C  APPROACH:  "2X ONION SKIN ALGORITHM"-- F0(X) IS TURNED INTO
C   THE INVERTED MONOTONIC FCN X(F) BY THE RULE
C   X(F) = THE LENGTH OF X (POSSIBLY MULTIPLE SEGMENTS) FOR
C          WHICH F0(X) .LE. F
C
C  THE ADDITIONAL QUANITY <X>(F) = THE AVERAGE X FOR WHICH
C          F0(X) .GE. F   IS ALSO CALCULATED.
C
C  NOTE THAT X(FMAX)=0.0 AND <X>(FMAX)= LOCATION OF PEAK VALUE
C   OF UNSYMMETRIZED INPUT PROFILE.
C
C  THE ALGORITHM EXPECTS THE INPUT PROFILE F0 TO BE POSITIVE
C   AND TO TEND TOWARDS ZERO AT THE EDGES
C
C  -----------------
C
C  PASSED ARGUMENTS:
C   ILX0 - LOCATION OF X VECTOR DEFINING F0
C   INX0 - NUMBER OF PTS IN X VECTOR FOR F0
C   ILX  - TARGET X VECTOR FOR SYMMETRIZED DATA (SHOULD COVER
C          RANGE APPROX (0,1) -- SEE EXECSYM
C   INX  - NUMBER OF PTS IN TARGET
C
C  THE PROFILE TO BE SYMMETRIZED IS IN COMMON:
C    WORKBUF(LBX(1)) THRU WORKBUF(LBX(1)+INX0-1)
C  WHICH IS DEFINED AT NORMALIZE SPATIAL COORDINATES
C    WORKBUF(ILX0) THRU WORKBUF(ILX0+INX0-1)
C
C  THE SYMMETRIZED DATA IS TO BE MAPPED TO NORMALIZED SPATIAL COORDS
C    DATBUF(ILX) THRU DATBUF(ILX+INX-1)
C
C  DATA IS IN WORKBUF ARRAY IN [TRANSP] COMMON
C
C  WORKBUF WORK AREAS (WORKBUF(LBX(IJ)...)) ARE USED BY THE
C  SYMMETRIZATION ALGORITHM
C
C  OUTPUT:
C   WORKBUF(LBX(7) TO LBX(7)+INX-1) CONTAINS SYMMETRIZED PROFILE
C  INTERPOLATED TO TARGET X VECTOR
C   WORKBUF(LBX(8) TO LBX(8)+INX-1) CONTAINS PROFILE <X>*RMINOR
C  INTERPOLATED TO TARGET X VECTOR.  THIS DEFINES A "PHENOMEN-
C  OLOGICAL" SHAFRANOV SHIFT CORRESPONDING TO THE INPUT PROFILE
C  DATA
C
C--------------------------------------------------------
C
      SUBROUTINE TDB_PRESYM(d,zr1,zr2,IMAP,ILX0,INX0,ILX,INX)
C
      use trdatbuf_obj
      IMPLICIT NONE

      type (trdatbuf) :: d
      real*8 zr1,zr2            ! midplane bdy intercepts

      INTEGER, PARAMETER :: R8=SELECTED_REAL_KIND(12,100)
C
C  CALCULATE X(F), <X>(F)
C
!============
! idecl:  explicitize implicit INTEGER declarations:
      INTEGER inx0,ilx,inx,ilx0,inumr,IMAP
!============
! idecl:  explicitize implicit REAL declarations:
      REAL*8 zxmax
!============
!
      ZXMAX=max(d%DATBUF(ILX+INX-1),
     >   0.5001_R8*(d%WORKBUF(ILX0+INX0-1)-d%WORKBUF(ILX0)))
      CALL TDB_PRFINV(d,d%WORKBUF(ILX0),INX0,INUMR,ZXMAX)
C
C  INUMR IS THE LENGTH OF THESE FCNS:
C    INVERTED F IN WORKBUF(LBX(2)..)-- *MONOTONIC INCREASING SEQUENCE
C    AT X LOCATIONS IN WORKBUF(LBX(3)..)-- *MONOTONIC DECREASING SEQUENCE
C    WITH <X> IN WORKBUF(LBX(5)..)
C
C  INVERT TO OUTPUT PROFILE F(X) AND CONVERT <X> TO A SHIFT IN CM, AND
C  INTERPOLATE TO TARGET X LOCATIONS
C
C  IMAP=1 -> normal;  IMAP=2 -> ECE
C
      CALL TDB_PRFINS(d,zr1,zr2,IMAP,INUMR,d%DATBUF(ILX),INX)
C
C  RESULTS IN WORKBUF(LBX(7)..) = F; WORKBUF(LBX(8)..) = SHIFT.
C
      RETURN
      END
C******************** END FILE TDB_PRESYM.FOR ; GROUP TDB_PRESYM **************
