C+
C Last modification date of this file: Tue Nov 25 16:19:19 CST 2014
C Roger J. Dejus (dejus@aps.anl.gov)
C
C PROGRAM DESCRIPTION:
C  Program to calculate wiggler and bending magnet spectra using the
C  Bessel function approximation. The program may be executed from the xop
C  interface.
C
C AUTHORS:
C  Roger J. Dejus
C  The Advanced Photon Source
C  Experimental Facilities Division
C  Argonne National Laboratory
C
C CREATION DATE:
C  17-FEB-1994
C
C INPUT PARAMETERS:
C  The input parameters are divided into sections related to the storage ring,
C  the wiggler device, and the quantity to be calculated.
C Machine Parameters:
C  Storage ring energy 			(GeV)
C  Storage ring current			(mA)
C Wiggler Parameters:
C  Period length			(cm)
C  Number of periods
C Note: For a bending magnet source: set N=0.5, and make Ky large and adjust
C       the period length accordingly. For example, put Ky=9.34 and calculate
C       the period length from, Period (cm) = 10.0/B0(T), where B0 is the known
C       strength of the magnetic field (in Tesla) for the bending magnet.  The
C       calculated power density (pd) is correct, but the total power (ptot)
C       is irrelevant. Typically make the extend of the pinhole small in the
C	horizontal direction (theta << Ky/gamma) as the intensity should
C	not depend on the horizontal offset. Check value of B0 (and critical
C	energy EC0) in the plot file.
C  Deflection parameter (hor.  field) Kx (= 0.0 only; for elliptical wiggler
C  not yet implemented)
C  Deflection parameter (vert. field) Ky
C Scan Parameters:
C  Minimum energy			(eV)
C  Maximum energy			(eV)
C  Number of energy points
C Pinhole Parameters:
C  Distance from the source		(m)
C    (d=0.0 => angular units)
C  X-coordinate for center of pinhole	(mm) or (mrad)
C  Y-coordinate for center of pinhole	(mm) or (mrad)
C  X-size of pinhole (full width)	(mm) or (mrad)
C  Y-size of pinhole (full width)	(mm) or (mrad)
C    (for angular units (d=0.0) values are entered in mrad)
C    (X is for horizontal direction)
C    (Y is for the vertical direction)
C  Number of subdivisions of pinhole in X (max 200)
C  Number of subdivisions of pinhole in Y (max 200)
C
C Mode:
C  Depending on the mode selected, some of the pinhole parameters may be
C  set to different values by the program; see the output file ws.plt.
C  MODE    1    Angular/spatial flux density distribution
C  MODE    2    Angular/spatial flux density spectrum
C  MODE    3    On-axis brilliance spectrum (not yet implemented)
C  MODE    4    Flux spectrum through a pinhole
C  MODE    5    Flux spectrum integrated over all angles
C  MODE    6    Power density and integrated power
C
C  Angular/spatial flux density distribution
C    - Flux distribution at the energy chosen as minimum energy.
C  Angular/spatial flux density spectrum
C    - Spectrum at any given point in space as selected by the X and Y
C      coordinate for the center of the pinhole. X is horizontal and Y is
C      vertical.
C  On-axis brilliance spectrum (not yet implemented)
C  Flux spectrum through a pinhole
C    - Spectrum through a pinhole centered at X-center and Y-center with
C      size X-size and Y-size.  The energy range is from the minimum to the
C      maximum energy.
C  Flux spectrum integrated over all angles (wiggler only).
C    -  The pinhole parameters have no significance here.
C  Power density and integrated power
C    -  Integrated over all energies, thus the energy parameters have no
C       significance here.
C
C Polarization:
C  The normalized Stokes parameters are calculated including the
C  unpolarized component.
C
C DESIGN ISSUES:
C  Program calculates the spectra from the Modified Bessel functions.  See K.J.
C  Kim, in "Physics of Particle Accelerators", vol. 1, AIP Conference Proc. 184
C  Ed. R.G. Lerner, New York (1989), p. 583, Eq. (3.12).
C  The algorithm is based on a series expansion for small arguments Z
C  (Abramowitz & Stegun Eq. 9.6.2 and 9.6.10) and an asymptotic expansion for
C  large arguments (Eq. 9.7.2).
C  Reference: Handbook of Mathematical Functions, Eds. Milton Abramowitz and
C  Irene A. Stegun, Ninth Printing, Dover Publications, New York (1970).
C  NOTE: THE POLARIZATION PARAMETERS ARE PROVIDED ALTHOUGH NOT THOROUGHLY
C  TESTED - USE WITH CAUTION.
C
C COPYRIGHT:
C  This routine must only be used at The Advanced Photon Source and must not
C  be tranferred or used at any other location without the written consent
C  of the author.
C
C FILES USED:
C  Input file - ws.dat (tc.inp for XOP)  File in the user's current directory
C                       containing the input parameters.
C  Output file - ws.plt (tc.out for XOP) File in the user's current directory
C                       containing the results of the calculation. The header contains
C                       all input parameters and the calculated on-axis first
C			harmonic energy (e1), corresponding wavelength (l1),
C			total power (ptot), and the on-axis power density (pd).
C			See note above when using N=0.5 for bending magnet.
C KEYWORDS:
C  Wiggler Spectrum, Modified Bessel Function of Second kind.
C
C LINK/LIBRARY ISSUES:
C  The gamma function is needed.  Currently uses no library routines. The values
C  for gamma(2/3) and gamma(1/3) are stored as constants. May be substituted by
C  calls to the NAG library routine S14AAF.
C
C PORTABILITY ISSUES:
C  Runs on DEC 3000/400 AXP alpha (Tru64Unix v5.0)
C  Release v5.6), and Windows 95/98/NT (Pentium and higher).
C  SUN: f90: Sun Fortran 95 8.3 SunOS_sparc 2007/05/03 (Tested November 24, 2008)
C  Updated October 8, 2013 (Argonne National Laboratory), tested on:
C  *** Linux Red Hat Enterprise Linux Workstation release 6.3 (Santiago) ***
C  Red Hat Enterprise Linux (RHEL) 64-bit with the Intel(R) Fortran
C  Intel(R) 64 Compiler XE for applications running on Intel(R) 64,
C  Version 13.1.1.163 Build 2013031, and with GFORTRAN, gcc version 4.4.6 20120305
C  (Red Hat 4.4.6-4) (GCC).
C  *** Sun Solaris SunOS 5.10 Generic_147440-27 sun4u sparc SUNW,Sun-Blade-2500 ***
C  Sun Fortran 90/95 8.4 SunOS_sparc Patch 128231-02 2009/10/20 with the -f77 switch.
C  and with GFORTRAN, gcc version 4.5.1 (GCC).
C  Windows 7/8 64-bit and MacOS X 10.6 (and newer) are also supported.
C  The GFORTRAN compiler (GCC) v4.8.1 was used for compilations on Windows and (GCC) v4.6.1 on MacOS.
C
C TIMING:
C  Execution time is typically very fast but depends on the quantity being
C  calculated. Typically seconds to at the most minutes.
C
C EXAMPLES:
C Ex. 1 using the input file ~/test/ws.txt (output file becomes ws.plt in the current working directory)
C % ws ~/test/ws.txt
C Ex. 2 using the default input file ws.dat in the current working directory (the output file becomes ws.plt).
C % ws
C Ex. 3 using the input abc in the current working directory (the output file becomes abc.plt).
C % ws abc
C
C VERSION:
C  1.62
C
C MODIFICATION HISTORY:
C
C	 Date     | Name  | Description
C ----------------+-------+-----------------------------------------------------
C 17-JUL-2000     | RJD   | Adopted from v1.4 which was never released to the
C                 |       | public. Turned off circular polarization for the
C                 |       | wiggler (which is only valid for EMW).
C                 |       | Current version is v1.5.
C ----------------+-------+-----------------------------------------------------
C 24-NOV-2008     | RJD   | Modified format statements 263 and 264 in main program
C                 |       | to print additional digits. Modified format statements
C                 |       | 210 and 220 in the print_out routine to print three
C                 |       | digits in the exponent to allow for very small (and
C                 |       | large) values. With the newest Sun Compiler, floating
C                 |       | point underflow is being flagged (but not trapped)
C                 |       | on the standard output. Nothing "extra" is written
C                 |       | to the output file "ws.plt."
C                 |       | The smallest value (double precision): 2.2250739e-308
C                 |       | The largest value (double precision): 1.7976931e+308
C                 |       | The floating underflow comes from the variables
C                 |       | asigma and api which are less than sqrt(smallest value).
C                 |       | Values are simply put to zero and no harm is done.
C                 |       | Changed format statement 254 to print one more decimal.
C                 |       | Increased the number of subdivisions of the pinhole
C                 |       | from 50 to 200 (P_SZ=201).
C                 |       | Current version is v1.6.
C ----------------+-------+-----------------------------------------------------
C 02-NOV-2013     | RJD   | Updated calls to date and time routines.
C                 |       | Current version is v1.61.
C ----------------+-------+-----------------------------------------------------
C 10-JUN-2014     | RJD   | Updated so that an arbitrary input file can be used on the command line.
C                 |       | If no input file is given on the command line then the file 'ws.dat'
C                 |       | is assumed ('ws.inp' for the XOP version). The output filename is created
C                 |       | from the rootname, which is derived from the input filename using the string after
C                 |       | the last directory separator (/) without its trailing file extension (if it exists).
C                 |       | The output filename is the rootname with the extension (.plt) appended (.out for the
C                 |       | XOP version). Search "standalone" for changing defaults.
C                 |       | Current version is v1.62.
C ----------------+-------+-----------------------------------------------------
C [change_entry]
C-