;===========================================================================
;+
; NAME:
;   inms_add_aux_axes - adds auxiliary axes to a plot
;
pro inms_add_aux_axes, axData, $
                       anXticks,        $ ;; (i) the x positions of the tick marks
                       nYpos,           $ ;; (i/o) the position of the first axis
                       nYspacing,       $ ;; (i) the spacing between axes
                       time=time,       $ ;; (i) if set, specifies indpendant variable
                       target=target,   $ ;; (i) set to specify target based coordinates
                       catime=catime,   $ ;; (i) if set add time to closest approach scale
                       lst=lst            ;; (i) if set replace wLon with local solar time

;
; PURPOSE:
;   adds additional auxiliary axes below the main x axis
;
; RESTRICTION:
;  routine assumes that the axis transform information created
;  by plot or contour is valid. This routine should be called
;  immediately after the routine that displays the data to be annotated
;
; ARGUMENTS:
;  axData    - a structure containing the data
;  anXticks  - the location in data units of each tick mark on the prime axis
;  nYpos     - the location of the axis, upon return it contains
;              the position at which the next axis would be plotted
;  nYspacing - the spacing between axes
;              nYpos and nYspacing are in normal coordinates
;  /target   - a keyword which specifies whether additional axis contain
;              s/c position wrt Saturn or wrt the current target body. If set
;              the auxilliary axis is with respect to the target body, not
;              Saturn
;  /lst      - a keyword which controls the display of longitude
;              If present, local solar time is displayed as an auxiliary axis
;              If absent, west longitude is displayed a san auxiliary axis
;  /catime   - a keyword which controls the addition of a time to closest 
;              approach axis. If /target is set and /catime is set, a
;              fourth auxilliary axis is displayed showing the time to closest approach
;  time      - A keyword containing a value of time for each element of the
;              axData array of structures. Used when the independent variable
;              is not an field of the axData structure.
;
; METHOD:
;   Determines whether the input data consists of L1A or Spectra Records
;   Extracts or calculates time from closest approach, altitude, latitude, 
;   and local solar time.
;   Interpolates those values to the values of the tick marks. If the time
;   keyword supplies a vector, that vector is taken as the independant vector, 
;   otherwise the time of day in seconds (uttime or nUttime) is used. The
;   axis routine is used to display the resulting scale
;
; CHANGES:
;  13-Jul-2004      DAG initial coding
;  20-August-2004   NAB simply changed 'bp_add_aux_axes' to 'inms_*'
;  26-August-2004   NAB changed to use structure created by inms_get_data
;  13-October-2004  DAG allow plotting of either saturn or target coordinates
;  24-Jun-2004      DAG allow use of alternative independent variable data
;  28-Jun-2004      DAG corrected prolog
;  30-Sep-2006      DAG Adjust size of characters annotating axis
;  16-Nov-2006      DAG Add time to closest approach to axis when /catime and /target are
;                       present
;  10-Apr-2007      DAG Use data from either L1A or Spectra record array for aux axis
;                       Add switch to replace west longitude with solar zenith angle
;-
;===========================================================================


    ;; validate data

    case 1 of
        inms_validate_spectra_data(axData) eq 1 $
             and inms_validate_l1a_data(axData) eq 0: bSpectraData = 1
        inms_validate_spectra_data(axData) eq 0 $
             and inms_validate_l1a_data(axData) eq 1: bSpectraData = 0
		else: message, 'You must supply either a valid L1a or Spectra Record Array'

    endcase

    bPlotCA  = keyword_set(catime)
    bPlotLST = keyword_set(lst)
    nSatRad  = 60286.
    nCharSize= inms_set_charsize(0.66)

    ;; decide what aux axis to plot
    ;; if /spectra present axData is an array of spectra records
    ;; if /spectra absent, axData is an array of L1A records
    ;;     if /target is specified without /spectra values are with respect to
    ;;     target body

    if bSpectraData eq 1 then begin
        ;; get values from spectra record
        ;; get latitude, west longitude, altitude and CA time
        sTargetName = axData[0].sTarget
        strput, sTargetName, strupcase(strmid(sTargetName, 0, 1)), 0

        anTime = axData.nUTtime
        anAlt = axData.nAlt_t
        anLat = axData.nLat_t
        anLon = bPlotLST ? axData.nLST_t : axData.nWlon_t
        anTimeCA = axData.nTimeCA

    endif else begin
        ;; axData contains l1a records
        anTime = axData.uttime

        if keyword_set(target) then begin
            ;; get data w.r.t. the target
            ;; target name
            sTargetName = inms_remove_quotes(axData[0].target)
            strput, sTargetName, strupcase(strmid(sTargetName, 0, 1)), 0

            ;; quantities
            anAlt = axData.alt_t
            anLat = inms_auxiliary_value(axData,/lat)
            anLon = bPlotLST ? axData.lst_t : inms_auxiliary_value(axData,/wlon)
            anTimeCA = axData.time_ca			
        endif else begin
            ;; get data w.r.t saturn
            sTargetName = 'Saturn'
            anAlt = axData.distance_s / nSatRad
            anLat = inms_auxiliary_value(axData,/lat,/saturn)
            anLon = bPlotLST ? axData.lst_s : inms_auxiliary_value(axData,/wlon,/saturn)
            bPlotCA = 0
        endelse
    endelse

    ;; display axes

    if keyword_set(time) then anTime = time

    nXtickCnt  = n_elements(anXticks)
    anXtickIdx = where(anXticks ge  (anTime - 0.5*(anXticks[1]-anXticks[0])))

    if bPlotCA then begin

        asXtickNames = replicate(' ', nXtickCnt)
        asXtickNames[anXtickIdx] =  $
            strtrim(string(interpol(anTimeCA/1000., anTime, $
                    anXticks[anXtickIdx]), format='(F7.1)'), 2)


        axis, 0, nYpos, xaxis=0, /normal, $
               xtitle='Time from '+ sTargetName +' Closest Approach Time (sec)', $
               xcharsize=nCharSize, $
               xticks=nXtickCnt-1, $
               xtickv=anXticks, $
               xtickname=asXtickNames
        nYpos = nYpos - nYspacing
    endif

    asXtickNames = replicate(' ', nXtickCnt)
    asXtickNames[anXtickIdx] = $
        strtrim(string(interpol(anAlt,  anTime, anXticks[anXtickIdx]), $
                format='(F8.2)'), 2)

    axis, 0, nYpos, xaxis=0, /normal, $
           xtitle=sTargetName + ' Altitude (km)', $
           xticks=nXtickCnt-1, $
           xcharsize=nCharSize, $
           xtickv=anXticks, $
           xtickname=asXticknames
    nYpos = nYpos - nYspacing

    asXtickNames = replicate(' ', nXtickCnt)
    asXtickNames[anXtickIdx] =  $
        strtrim(string(interpol(anLat, anTime, $
                anXticks[anXtickIdx]), format='(F6.2)'), 2)

    axis, 0, nYpos, xaxis=0, /normal, $
           xtitle=sTargetName +' Latitude (deg)', $
           xcharsize=nCharSize, $
           xticks=nXtickCnt-1, $
           xtickv=anXticks, $
           xtickname=asXtickNames

    nYpos = nYpos - nYspacing

    asXtickNames = replicate(' ', nXtickCnt)
    asXtickNames[anXtickIdx] =  $
        strtrim(string(interpol(anLon, anTime, $
                anXticks[anXtickIdx]), format='(F7.2)'), 2)

    axis, 0, nYpos, xaxis=0, /normal, $
           xtitle=sTargetName $
           + (bPlotLST ? ' Local Solar Time (hr)' : ' West Longitude (deg)'), $
           xcharsize=nCharSize, $
           xticks=nXtickCnt-1, $
           xtickv=anXticks, $
           xtickname=asXtickNames

    nYpos = nYpos - nYspacing

end
