;+
; NAME:
;	WR_PLAN_AREA
; PURPOSE:
;	writes a modified plan area and history
; CATEGORY:
; CALLING SEQUENCE:
;	wr_plan_area,plan_area,history
; INPUTS:	
;	plan_area	512 byte plan area
;	history		history records of changes (must be passed!)
; OPTIONAL INPUT PARAMETERS:
; KEYWORD PARAMETERS:
; OUTPUTS:
; OPTIONAL OUTPUT PARAMETERS:
; COMMON BLOCKS:
; SIDE EFFECTS:
; RESTRICTIONS:
;	hsitory records must be passed
; PROCEDURE:
; MODIFICATION HISTORY:
;	RDB  14-May-94	Extracted into own routine, some mods to file format
;	RDB  17-May-94	Made file /xdr
;-

pro	wr_plan_area,plan_area,history

;	  Write an image of the BCS Plan Area

;	check that history records were passed
if n_elements(history) eq 0 then begin
  print,'** History records must be passed **'

;	write file after checking size of array
endif else begin
  sz = size(plan_area)
  if sz(0) ne 1 or sz(1) ne 512 or sz(2) ne 1 then begin
    print,'** Plan_area is not of correct size/type **'
    return
  endif

;	final verification
  yesnox,'* Do you really want to update the plane area',ans
  if not ans then return

  jhist_max = fix(n_elements(history))		;derive these values
  jhist = fix(max(where(history.type ge 0))+1)
  print,'** History records:',jhist,jhist_max
  ctime = !stime
  tarr = byte(timstr2ex(ctime))			;get current time
  print,'** Time is: ',ctime

  openw,lunw,concat_dir('$BCS_COMMAND','plan_area_image.dat'),/get_lun,/xdr
  writeu,lunw,plan_area				;write area
  writeu,lunw,tarr				;write date and history
  writeu,lunw,jhist,jhist_max
  writeu,lunw,history
  free_lun,lunw

  print,'** Plan area image updated **'

endelse

return
end
