; $Id: getformatcode.pro,v 1.4 2005/02/08 17:18:06 azuah Exp $
;###############################################################################
;
; NAME:
;   getFormatCode
;
; PURPOSE:
;   Determine appropriate format code to display data to a specified
;   precision (number of decimal places).
;
; CATEGORY:
;   Data Browser
;
; AUTHOR:
;   Richard Tumanjong Azuah
;   NIST Center for Neutron Research
;   100 Bureau Drive, Gaithersburg, MD 20899
;   United States
;   azuah@nist.gov; (301) 9755604
;   Jul, 2003
;
; LICENSE:
;  The software in this file is written by an employee of 
;  National Institute of Standards and Technology 
;  as part of the DAVE software project.
;
;  The DAVE software package is not subject to copyright protection
;  and is in the public domain. It should be considered as an
;  experimental neutron scattering data reduction, visualization, and
;  analysis system. As such, the authors assume no responsibility
;  whatsoever for its use, and make no guarantees, expressed or
;  implied, about its quality, reliability, or any other
;  characteristic. The use of certain trade names or commercial
;  products does not imply any endorsement of a particular product,
;  nor does it imply that the named product is necessarily the best
;  product for the stated purpose. We would appreciate acknowledgment
;  if the DAVE software is used or if the code in this file is
;  included in another product.
;
;###############################################################################



;===============================================================================
; getFormatCode
;
; PURPOSE:
;   Determine appropriate format code to display a range of numbers to
;   a specified precision (number of decimal places).
;
; PARAMETERS:
;   minimum [in; req] - lower limit of range to be displayed eg -10.0
;
;   maximum [in; req] - upper limit of range to be displayed eg 200.0
;
;   decimalPlaces [in; req] - precision or number of decimal places to
;                             be displayed
;
; RETURN VALUE
;   returns a string containing the appropriate format code
;
function getFormatCode, minimum, maximum, decimalPlaces

fCode = '(G10.4)'               ; generic format string

if (n_params() ne 3) then return, fCode

absmax = abs(minimum) > abs(maximum) ; determine absolute maximum to be displayed

size = floor(alog10(absmax))  ; determine size of maximum data

case 1 of
    ((size ge 4) or (size le -4)): begin
        fieldWidth = strtrim(string(fix(7+decimalPlaces)),2)
        precision = strtrim(string(fix(1+decimalPlaces)),2)
        fCode = '(G'+fieldWidth+'.'+precision+')'
        return, fcode
    end

    ((size lt 1) and (size gt -4)): begin
        fieldWidth = strtrim(string(fix(3+decimalPlaces)),2)
        precision = strtrim(string(fix(decimalPlaces)),2)
        fCode = '(F'+fieldWidth+'.'+precision+')'
        return, fcode
    end

    else: begin
        fieldWidth = strtrim(string(fix(size+3+decimalPlaces)),2)
        precision = strtrim(string(fix(decimalPlaces)),2)
        fCode = '(F'+fieldWidth+'.'+precision+')'
        return, fcode
    end
endcase


end
