;+
;NAME:
;	hxt_pattern_maker
;PURPOSE:
;	To input the image synthesis control parameters, and generate the 
;	modulation pattern, and control the iteration. 
;CATEGORY:
;       Yohkoh HXT
;CALLING SEQUENCE:
;	hxt_pattern_maker,hxi_index,p,sc_paras,trn,xy=xy,expfac=expfac,$
;                     n_pix=n_pix, patt_file = patt_file, $
;                     grause_file = grause_file, ntrn = ntrn
;INPUT:
;       trn= 1-d mod patterns of each SC, if passed in, you need not generate it
;	hxi_index= a structure containing everything we need to know
;                  about the hxt image. 
;	Tags for the structure hxi_index:
;	hxi_index.alpha= amplitude of phase correction factor
;	hxi_index.iway=0 for triangular patterns
;	hxi_index.area= total area in cm^2 of scintillators
;	hxi_index.syserr= systematic error term to be added to Poisson Uncertainty
;	hxi_index.gamma0= initial value of iteration gain
;	hxi_index.laminc= initial value for lambda increment
;	hxi_index.chilim= minimum value of chi^2 needed
;OUTPUT:
;	hxi_index.x0, hxi_index.y0= X and Y corrdinates in HXT Coordinate
;               system, these are not included in Ref. data base, and must
;               be input by hand, or obtained from other sources, try
;               xy = conv_p2hxt(gt_center(sxt_index)) given SXT data
;       hxi_index.grause(8)=Each element is either 0b to 255b, 
;		If hxi_index.grause(j)=0b, then the Subcollimators with 
;		spatial wave number j+1 aren't used in the calculation
;	p= 2-d modulation pattern, for the given coordinates
;	sc_paras - The subcollimator parameters in a QS structure
;       trn= 1-d mod patterns of each SC, if passed in, you need not generate it
;KEYWORDS:
;       xy= an array containing the 2 HXA aspect coordinates, if not
;           set, the program will prompt you. This now allows you
;           to pass in an sxt_index as the corrdinates, or a string
;           representation of the position, e.g. 'n16w13'
;       expfac= expansion factor, 1.0 gives 2.47 arcsec pixels
;       n_pix= number of pixels in images, default is 64
;       interactive= if set, you choose the files for the SC parameters
;       patt_file = a filename for the mod. pattern parameters, the default
;                      is $DIR_HXT_CAL/para3.dat
;       grause_file = a filename for the grid use file, the default is
;                     $DIR_HXT_CAL/grause, or an array of 64 1's or 0's
;                     if set to 0, that subcollimator is not used
;       ntrn = the number of grid points for each 1-d modulation pattern
;              the default is 512
;DESCRIPTION:
;	hxt_pattern_maker, creates the 2-d modulation
;       pattern used for the synthesis.
;MODIFICATION HISTORY:
;	Written July '92 by J. McTiernan
;	24-Aug-92 (MDM) - Changed default GRAUSE file from "grause_k7"
;			  to "grause"
;	22-Jun-93 (MDM) - Changed to use "para3.dat" instead of "para_3"
;			  data files.
;       29-oct-93 (JMM) - Added expfac keyword, for larger fields of view
;       18-jul-94 (JMM) - Added n_pix keyword, for any number of pixels in
;                         The image
;       Pulled out the img_default routine, which is now called directly,
;       and changed the name from hxt_img_prep, to pattern_maker.pro
;       20-jul-94, jmm
;       Added trn as a parameter, and absorbed MAKPAT.pro, 21-jul-94
;       Got rid of prompt for expansion factor, now it defaults to 1.0
;       Added option to pass in sxt_index as the xy keyword, and
;       also the string heliocentric coordinates, and the date keyword
;       18-oct-96, jmm
;       Added patt_file and grause_file keywords, jmm, 26-nov-96
;       New mod. patterns, 13-May-1998, jmm
;       New name, Hxt_pattern_maker, 24-jun-1998, jmm
;       Added ntrn keyword, 24-jun-1998, jmm
;-
PRO Hxt_pattern_maker, hxi_index, p, sc_paras, trn, xy=xy, expfac=expfac, $
                       n_pix=n_pix, Date=date, Patt_file=patt_file, $
                       Grause_file=grause_file, Xxx=xxx, Ntrn=ntrn

   grdnum = 64
   IF(KEYWORD_SET(n_pix)) THEN pxlnum = n_pix ELSE pxlnum = 64
   chnum = 4

; Prompt for the coordinates,
   IF(KEYWORD_SET(xy)) THEN BEGIN
      typ = size(xy) & typ = typ(typ(0)+1) ;jmm, 18-oct-1996
      IF(typ EQ 8) THEN BEGIN
         xy = gt_center(xy(0), /hxt)
      ENDIF ELSE IF(typ EQ 7) THEN BEGIN
         xy = conv_h2hxt(conv_hs2h(xy(0)), date)
      ENDIF 
      fxpos = xy(0)
      fypos = xy(1)

   ENDIF ELSE BEGIN 
      print, ' INPUT THE FLARE POSITION IN HXT X AND Y COORDINATES.>>'
      read, fxpos, fypos
   ENDELSE
   hxi_index.x0 = fxpos
   hxi_index.y0 = fypos
; Make modulation patterns
   IF(KEYWORD_SET(n_pix)) THEN pxlnum = n_pix ELSE pxlnum = 64
   hxi_index.shape_sav(0) = pxlnum
   hxi_index.shape_sav(1) = pxlnum
   IF(KEYWORD_SET(expfac)) THEN expf = expfac ELSE expf = 1.0 ;jmm, 22-jun-95
   
   IF(N_ELEMENTS(trn) EQ 0) THEN BEGIN
      IF(KEYWORD_SET(patt_file)) THEN parafile = patt_file $
        ELSE parafile = concat_dir('$DIR_HXT_CAL', 'npara.dat')
      IF(KEYWORD_SET(grause_file)) THEN grausfile = grause_file $
        ELSE grausfile = concat_dir('$DIR_HXT_CAL', 'grause')
; hxt_rdpara inputs the sc info
      hxt_rdpara, parafile, sc_paras
; usegrdnew inputs the grid use info
      hxt_usegrd, grausfile, grause
      hxi_index.grause = grause
; gentrn creates the 1d modulation pattern
      IF(KEYWORD_SET(xxx)) THEN BEGIN
         hxt_gentrn, hxi_index, sc_paras, trn, npts = 128
         filex = concat_dir('$DIR_HXT_CAL',['hxt_pat.L', 'hxt_pat.M1'])
         temp = fltarr(128)
         FOR ich = 0, 1 DO BEGIN
            openr, unit, filex(ich), /get_lun
            FOR grd = 0, 63 DO BEGIN
               readf, unit, temp
;            pmm, temp
               trn(grd, *, ich) = temp*hxi_index(0).area
            ENDFOR
            free_lun, unit
         ENDFOR
      ENDIF ELSE hxt_gentrn, hxi_index, sc_paras, trn, npts = ntrn
      print, '*** 1-d MODULATION PATTERNS LOADED.'
   ENDIF

;modpat gets the 2-d modulation pattern
   hxt_modpat, hxi_index, sc_paras, trn, p, expfac = expf, n_pix = pxlnum
   print, '*** 2-d MODULATION PATTERNS LOADED.'

   RETURN
END

