;+
; NAME:
;     MSEC2STR
; PURPOSE:
;     Converts a time (or RA) in msec to a string of the form HH:MM:SS.SSS
;     (Can also convert Declination in masec, maintaining negative sign)
; CATEGORY:
;     OVRO SPAN CONVERSION
; CALLING SEQUENCE:
;     tstring = msec2str(msec)
; INPUTS:
;     msec	the time (or RA) in msec (should not exceed 99 hours)
;                         or DEC in masec (should not exceed 99 degrees)
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
; ROUTINES CALLED:
;     sec2hms
; OUTPUTS:
;     tstring	the time of day as a string of the form HH:MM:SS.SSS
; COMMENTS:
; SIDE EFFECTS:
; RESTRICTIONS:
;     msec cannot be an array--must be a scalar
; MODIFICATION HISTORY:
;     Written 31-Jan-1997 by Dale Gary
;     24-Aug-1998  DG
;       Division by 1000. changed to 1000.D to keep needed precision.
;-
function msec2str,msec


   b = size(msec)

   if (b(0) gt 0) then begin
      message,'Argument must be a scalar',/continue
      return,'0'
   endif

   ; Detect if a negative number

   neg = 0
   if (msec lt 0) then neg = 1

   ; Convert output of sec2hms to a time string, using abs of time

   tstr = string(sec2hms(abs(msec)/1000.D),format='(f10.3)')

   ; Restore sign if negative, or add a blank if positive

   if (neg) then tstr = '-'+tstr else tstr = ' '+tstr

   ; Fill with leading zeroes if necessary

   i = 1
   while (strmid(tstr,i,1) eq ' ') do begin
      strput,tstr,'0',i
      i = i + 1
   endwhile

   ; Insert colons and return

return,strmid(tstr,0,3)+':'+strmid(tstr,3,2)+':'+strmid(tstr,5,6)
end
