;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Lance Simms, Stanford University 2009
;pro create_fe_db_xrg
;
;PURPOSE:
;  To create tables in the Fe55 Ray MySQL databases for the HxRG detectors.
;  
;  The script uses Mark Buie's MySQL library to do this.
;
;INPUTS:
;  DETSTR: string
;     The name of the detector used ('H1RG-022', 'H2RG-32-147', 'H4RG-007')
;  ELECSTR: string
;     The name of the electronics used ('ASIC', 'LEACH')
;  MODE: str
;     'Window' - Create a window mode table for DETSTR+ELECSTR
;
;     'FullFrame' - Create a full frame table for DETSTR+ELECSTR
;  GAIN: int
;     The gain of the preamps
;KEYWORDS:
;  OVERWRITE: int
;     0 - Don't overwrite the old tables;
;     1 - Overwrite the old tables
; 
;EFFECTS:
;  The MySQL database will be updated with the new table.
;
;CALLING SEQUENCE:
;  Create_Fe_DB_xRG, DetStr, ElecStr, Mode
;
;EXAMPLES
;To create the single table
;   Create_Fe_DB_xRG, 'H2RG-32-147', 'ASIC', 0
;To create the fe55 table
;   H1RG-022=============================================
;   Create_fe55_db_xrg,'H1RG-022','ASIC','Window', Gain=4 
;   H2RG-011=============================================
;   Create_fe55_db_xrg,'H2RG-001','ASIC','Window', Gain=4
;   Create_fe55_db_xrg,'H2RG-001','ASIC','FullFrame', Gain=4
;   H2RG-32-147==========================================
;   Create_fe55_db_xrg,'H2RG-32-147', 'ASIC', 'Window', Gain=4, /overwrite
;   Create_fe55_db_xrg,'H2RG-32-147', 'ASIC', 'FullFrame', Gain=4, /overwrite
;   Create_fe55_db_xrg,'H2RG-32-147', 'ASIC', 'Window', Gain=1, /overwrite
;
pro create_fe55_db_xRG, DetStr, ElecStr, Mode, OverWrite=OverWrite, Gain=Gain

If N_Elements(OverWrite) eq 0 then OverWrite = 1
If N_Elements(Gain)      eq 0 then Gain      = 1

Date = ''
Common KeyParams, KeyStr
;Include all of the keywords from the KeywordStruct file

@KeywordStruct_xRG.pro
@PlotSettings_xRG.pro

;Single Hit Cosmic Ray Database
openmysql, MySQLLun, 'FE55_HitSize', MyQLErr
    
;Format the strings in the table name
TableName = KeyStr.DetStr+'_'+KeyStr.ElecStr+'_'+Mode+'_Gain'+$
            strtrim(Gain,2)+'_Fe55'
strreplace, TableName, '-','_'
strreplace, TableName, '-','_'
if OverWrite eq 1 then $
   mysqlcmd, MySQLLun, 'DROP TABLE ' +TableName+';'

Command = 'CREATE TABLE '+TableName+'('+$
   'READNUM SMALLINT, X SMALLINT    , Y SMALLINT, '+$
   'CHAVG  TINYINT  , FILENAME VARCHAR(120), '+$
   'TEMP SMALLINT   , VSUB SMALLINT,'+ $
   'NUMPIX TINYINT  , TOTQ SMALLINT , MAXQ SMALLINT,'+$
   'ADU1 SMALLINT   , ADU2 SMALLINT , ADU3 SMALLINT,'+$
   'ADU4 SMALLINT   , ADU5 SMALLINT , ADU6 SMALLINT,'+$
   'ADU7 SMALLINT   , ADU8 SMALLINT , ADU9 SMALLINT,'+$
   'NEGPIX TINYINT  , NEGBOR TINYINT);'
mysqlcmd, MySQLLun, Command, Answer, NumLines

end  
