;+
; NAME:
;     FREESCAN
; PURPOSE:
;     Frees the memory (structures) allocated during a call to NEWSCAN.
; CATEGORY:
;     OVRO APC DATA ANALYSIS
; CALLING SEQUENCE:
;     freescan,obseq,traj,refcal,gparm[,geometry,pRecent,pOld,cycle])
; INPUTS:
;     rec      The record number of the next data record, or -1 if an error.
;     header
;     cfg
;     obseq
;     gparm
;     geometry
;     pRecent
;     pOld
;     cycle
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
; ROUTINES CALLED:
;     free_pointer
; OUTPUTS:
; COMMENTS:
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 04-Dec-1999 by Dale E. Gary
;     02-Jun-2006  DG
;        Somehow forgot to put in code to free GEOMETRY structure.  Now added.
;     11-Apr-2014  DG
;        Some change in the bowels of SSW made freeing the GEOMETRY structure
;        no longer work, so added code to release only PTRs in it, since
;        ordinary variables in the structure will be freed by the system.
;     20-Apr-2014  DG
;        The above problem surfaced for the TRAJ structure, too, so I went
;        ahead and made the change for all structures.
;-

pro freescan,obseq,traj,refcal,gparm,geometry,pRecent,pOld,cyc_str

  if (n_elements(cyc_str) ne 0) then begin
     n = n_tags(cyc_str)
     for i = 0, n-1 do begin
        if datatype(cyc_str.(i)) eq 'PTR' then free_pointer,cyc_str.(i)
     endfor
  endif
  if (n_elements(pOld) ne 0) then free_pointer,pOld
  if (n_elements(pRecent) ne 0) then free_pointer,pRecent
  if (n_elements(gparm) ne 0) then begin
     n = n_tags(gparm)
     for i = 0, n-1 do begin
        if datatype(gparm.(i)) eq 'PTR' then free_pointer,gparm.(i)
     endfor
  endif
  if (n_elements(refcal) ne 0) then begin
     n = n_tags(refcal)
     for i = 0, n-1 do begin
        if datatype(refcal.(i)) eq 'PTR' then free_pointer,refcal.(i)
     endfor
  endif
  if (n_elements(traj) ne 0) then begin
     n = n_tags(traj)
     for i = 0, n-1 do begin
        if datatype(traj.(i)) eq 'PTR' then free_pointer,traj.(i)
     endfor
  endif
  if (n_elements(obseq) ne 0) then begin
     n = n_tags(obseq)
     for i = 0, n-1 do begin
        if datatype(obseq.(i)) eq 'PTR' then free_pointer,obseq.(i)
     endfor
  endif
  if (n_elements(geometry) ne 0) then begin
     n = n_tags(geometry)
     for i = 0, n-1 do begin
        if datatype(geometry.(i)) eq 'PTR' then free_pointer,geometry.(i)
     endfor
  endif

return
end