PRO GET_FLAG, DATE, STATUS, ERRMSG=ERRMSG
;
    ON_ERROR, 2
;
;  Initialize the output structure, in case any errors occur.
;
    STATUS = {STUDY_ID: -1}
;
;  Check the number of parameters.
;
        IF N_PARAMS() NE 2 THEN BEGIN
           MESSAGE = 'Syntax:  GET_FLAG, DATE, STATUS'
       GOTO, HANDLE_ERROR
        ENDIF
;
;  Check the input parameter.
;
    IF N_ELEMENTS(DATE) NE 1 THEN BEGIN
           MESSAGE = 'DATE must be a scalar'
           GOTO, HANDLE_ERROR
    ENDIF
;
;  Convert the date to TAI format.
;
    IF DATATYPE(DATE,1) EQ 'Double' THEN TAI = DATE ELSE    $
        TAI = UTC2TAI(DATE)
;
;  Open the database.
;
    DBOPEN, 'sci_flag'
;
;  Search for entries with dates spanning that given by the user.
;
    ENTRIES = DBFIND('RCVR_STOP>' + TRIM(TAI,'(F15.3)') + ',RCVR_START<' +$
        TRIM(TAI,'(F15.3)') + ',DELETED=N', /SILENT)
;
;  If no entries were found, then return immediately.
;
    IF !ERR EQ 0 THEN BEGIN
        MESSAGE = 'Flag receiver entry not found'
        GOTO, HANDLE_ERROR
    ENDIF
;
;  Extract the entry.
;
    DBEXT, ENTRIES, 'prog_id,study_id,studyvar,sci_obj,sci_spec,cmp_no', $
        PROG_ID, STUDY_ID, STUDYVAR, SCI_OBJ, SCI_SPEC, CMP_NO
    DBEXT, ENTRIES, 'object,obj_id,rcvr_start,rcvr_stop,n_rasters1', $
        OBJECT, OBJ_ID, RCVR_START, RCVR_STOP, N_RASTERS1
    DBEXT, ENTRIES, 'tracking,n_pointings,orig_dur,gset_id,repoint', $
        TRACKING, N_POINTINGS, ORIG_DUR, GSET_ID, REPOINT
;
;  If more than one entry has been found, then use the one with the closest
;  start time to the requested time.  This avoids problems with times which are
;  right on the boundaries between entries.
;
    DIFF = ABS(RCVR_START - TAI)
    I = (WHERE(DIFF EQ MIN(DIFF)))(0)
;
;  Define the structure to be used with the pointings.
;
    POINTINGS = {PLAN_PNT_STRUC, INS_X: 0.0, INS_Y: 0.0, ZONE_ID: 0}
;
;  Extract the pointings associated with this entry.
;
    N_POINTINGS = N_POINTINGS(I)
    IF N_POINTINGS GT 0 THEN BEGIN
        DBOPEN, 'point_flag'
        ENTRIES = DBFIND('RCVR_START=' + TRIM(RCVR_START(I),    $
            '(F15.3)') + ',DELETED=N', /SILENT)
        DBEXT, ENTRIES, 'pt_ind,ins_x,ins_y,zone_id',   $
            PT_IND, INS_X, INS_Y, ZONE_ID
;
;  Put the data into the structure.
;
        IF N_POINTINGS EQ 1 THEN BEGIN
            POINTINGS.INS_X   = INS_X(0)
            POINTINGS.INS_Y   = INS_Y(0)
            POINTINGS.ZONE_ID = ZONE_ID(0)
        END ELSE BEGIN
            POINTINGS = REPLICATE(POINTINGS, N_POINTINGS)
            S = SORT(PT_IND)
            POINTINGS.INS_X   = INS_X(S)
            POINTINGS.INS_Y   = INS_Y(S)
            POINTINGS.ZONE_ID = ZONE_ID(S)
        ENDELSE
    ENDIF
;
;  Define the output structure as a whole.
;
    STATUS = {STRUCT_TYPE: 'CDS-FLAG',  $
        PROG_ID: PROG_ID(I),        $
        STUDY_ID: STUDY_ID(I),      $
        STUDYVAR: STUDYVAR(I),      $
        SCI_OBJ: SCI_OBJ(I),        $
        SCI_SPEC: SCI_SPEC(I),      $
        CMP_NO: CMP_NO(I),      $
        OBJECT: OBJECT(I),      $
        OBJ_ID: OBJ_ID(I),      $
        RCVR_START: RCVR_START(I),  $
        RCVR_STOP: RCVR_STOP(I),    $
        ORIG_DUR: ORIG_DUR(I),      $
        N_RASTERS1: N_RASTERS1(I),  $
        TRACKING: TRACKING(I),      $
        N_POINTINGS: N_POINTINGS,   $
        REPOINT: REPOINT(I),        $
        GSET_ID: GSET_ID(I),        $
        POINTINGS: POINTINGS}
    GOTO, FINISH
;
;  Error handling point.
;
HANDLE_ERROR:
    IF N_ELEMENTS(ERRMSG) NE 0 THEN ERRMSG = 'GET_FLAG: ' + MESSAGE $
        ELSE MESSAGE, MESSAGE, /CONTINUE
;
;  Close the database, and return.
;
FINISH:
    DBCLOSE
;
    RETURN
    END