# $Header: https://oberon.oapd.inaf.it/shins/branches/gigan-virgin_system/templates/util/variables.py 2132 2019-10-07 12:58:21Z depa $
import os

#########
# PATHS #
#########

pkgDataPath = os.environ["INSROOT"] + "/share/shins/templates"

refDir = pkgDataPath + "/ref"
defDir = pkgDataPath + "/ob"
schemaDir = pkgDataPath + "/schema"
combDir = pkgDataPath + "/comb"
fitsDir = "/data/fits/"

filenameSHINS = "FILENAME_SHINS"

#################
# EXIT STATUSES #
#################

EXIT_SUCCESS = 0
EXIT_FAILURE = 1

#############
# Resources #
#############

# Name        IP                #port  #mapped #description
resources = {
    "cisco":      ("192.168.61.40",  80,   801,  "switch"),
    "mocon1":     ("192.168.61.50",  None, None, "Motor board"),
    "mocon2":     ("192.168.61.51",  None, None, "Motor board"),
    "mocon3":     ("192.168.61.52",  None, None, "Motor board"),
    "cred":       ("192.168.61.64",  8888, 8064, "Technical camera"),
    "dm":         ("192.168.61.65",  80,   8065, "Deformable Mirror"),
    "rtc":        ("192.168.61.66",  None, None, "Real Time computer"),
    "lakeshore":  ("192.168.61.67",  80,   8067, "Temperature controller"),
    "ipcam":      ("192.168.61.68",  80,   8068, "IP Camera of the lab"),
    "pdu1":       ("192.168.61.221", 80,   8221, "DM, RTC Mocon, lamps"),
    "pdu2":       ("192.168.61.222", 80,   8222, "MACIE; MediaA, MediaB"),
    "sashaws":    ("192.168.61.115", 8888, 8125, "scicam"),
    "monit":      ("192.168.61.113", 2812, 2812, "Monit (already on ws)"),
}

###########
# RTC DSP #
###########

TTControlLoopEna = 4399
CentroidOrigin = 4432

rtc_s_base = {
    
    "TTM2CFILE"             : "20220603-Zernike-FullPupil-Arcetri-1u-rms-4rtc.dat",
    "TTACTUATORMAPFILE"     : "BAX479-DM97-map.dat",
    "TTACTUATORSIGNMAPFILE" : "BAX479-DM97-sign.dat",
    "TTBIASFILE"            : "2024-03-06T15-18-55_PixelBias_-15C_1000Hz-064x064.dat",
    "TTCAMTINT"             : "0.000994919",
    "TTCENTROIDGAINX"       : "-4.249796",
    "TTCENTROIDGAINY"       : "3.7651188",
    "TTCENTROIDORIGX"       : "-0.0000",
    "TTCENTROIDORIGY"       : "0.0000",
    "TTDIAGDECIMATION"      : "0",
    "TTDIAGENABLED"         : "true",
    "TTDIAGRECORDIPV4"      : "192.168.61.113",
    "TTDIAGRECORDMAC"       : "4c:d9:8f:a0:22:44",
    "TTDIAGRECORDPORT"      : "8001",
    "TTDMFLATFILE"          : "dm_shape_2023-07-11-14-25-12_int_140b_90el_T15C_0as.txt",
    "TTDMMAXPOWER"          : "10",
    "TTDMOUTPUTENABLED"     : "1",
    "TTFRAMERATE"           : "1000",
    "TTHISTORYENABLED"      : "0",
    "TTLOOPENABLED"         : "0",
    "TTMASTERDECENABLED"    : "true",
    "TTMAXCENTROIDVAL"      : "+1",
    "TTMAXTTCOMMAND"        : "3",
    "TTMINCENTROIDVAL"      : "-1",
    "TTMINTHRESHOLD"        : "100",
    "TTMINTTCOMMAND"        : "-3",
    "TTNSLOPEPIXELS"        : "4096",
    "TTPIXELDECIMATION"     : "49",
    "TTPIXELENABLED"        : "1",
    "TTPIXELGAINFILE"       : "flat_gain_64x64.dat",
    "TTPIXELGAINMODE"       : "FLATGAIN",
    "TTRTCMDENABLED"        : "0",
    "TTTIMEHISTLEN"         : "50000",
    "TTTIMEHISTORYFILE"     : "TimeHistory.dat",
    "TTWINCOLS"             : "64",
    "TTWINROWS"             : "64",
    "TTWINCOORDX"           : "224",
    "TTWINCOORDY"           : "212"
}

###########
# Devices #
###########

comment_char = "#"

base_path = "/opt/tan/etc/"

motion_ctrl = "motion_ctrl"
calunit_ctrl = "calunit_ctrl"
track_ctrl = "track_ctrl"
    
devices = {

    "APODIZER_W" : motion_ctrl,
    "ND_FILT_W" : motion_ctrl,
    "CORO_SLIT_W" : motion_ctrl,
    "LYOT_GRISM_W" : motion_ctrl,
    "SCI_FILT_W1" : motion_ctrl,
    "SCI_FILT_W2" : motion_ctrl,
    "DB_FILT_W" : motion_ctrl,
    "PUPIL_LENS_DEP" : motion_ctrl,
    "INBEAM_TT" : motion_ctrl,
    "INBEAM_TT_X" : motion_ctrl,
    "INBEAM_TT_Y" : motion_ctrl,
    "INBEAM_DEP" : motion_ctrl,
    "SHUTTER" : motion_ctrl,

    "CAL_MIRROR_DEP" : calunit_ctrl,
    "CAL_FIBER_DEP" : calunit_ctrl,
    "CAL_FILTER_DEP" : calunit_ctrl,

    "PRISM1" : track_ctrl,
    "PRISM2" : track_ctrl,
    "ADC_Mode" : track_ctrl,
    "ADC1_PosAng" : track_ctrl,
    "ADC2_PosAng" : track_ctrl,
    
    "DEROT" : track_ctrl,
    "DROT_PosAng" : track_ctrl,

}

########
# PDUs #
########

# PDU and lamps
ipPDU1 = resources["pdu1"][0]
ipPDU2 = resources["pdu2"][0]
pduUID = "shins"
snmpVer = 3

outletCmdDict = {
    "ON": 1,
    "OFF": 2,
    "REBOOT": 3,
    "DELAYED_ON": 4,
    "DELAYED_OFF": 5,
    "DELAYED_REBOOT": 6
}

# dictionary associating to device alias name its outlet on PDU #2
 
aliasDict = {
    "MOCON": 1,
    "CAL_FIBER_FOCUS_LAMP": 2,
    "CAL_FF_LAMP": 3,
    "CAL_FIBER_DEFOCUS_LAMP": 4,
    "MACIE": 5,
    "DM": 6,
    "RTC": 7,
    "CRED2": 8,
}

aliasDict2 = {
    "FREE": 1,
    "FREE": 2,
    "ARDUINO": 3,
    "MEDIA1": 4,
    "MEDIA2": 5,
    "LAKESHORE": 6,    
    "ELECTROVALVE": 7,
    "FREE": 8,
}

lampStateDict = {1: "ON",
                 2: "OFF"}

COMMON_OID_PATH = ".1.3.6.1.4.1.3808.1.1.3.3"
COMMON_CONTROL_PATH = ".3.1.1"
COMMON_STATUS_PATH = ".5.1.1"

dictOutletControl = {
    "Index"          : COMMON_OID_PATH + COMMON_CONTROL_PATH + ".1.{}",
    "OutletName"     : COMMON_OID_PATH + COMMON_CONTROL_PATH + ".2.{}",
    "OutletPhase"    : COMMON_OID_PATH + COMMON_CONTROL_PATH + ".3.{}",
    "OutletCommand"  : COMMON_OID_PATH + COMMON_CONTROL_PATH + ".4.{}",
    "OutletBank"     : COMMON_OID_PATH + COMMON_CONTROL_PATH + ".5.{}",
    "OutletState"    : COMMON_OID_PATH + COMMON_STATUS_PATH + ".4.{}",  # (R) INTEGER, # (R) INTEGER,
}

dictOutletStatus = {
    "Index"          : COMMON_OID_PATH + COMMON_STATUS_PATH + ".1.{}",  # (R) INTEGER, # (R) DisplayString,
    "OutletName"     : COMMON_OID_PATH + COMMON_STATUS_PATH + ".2.{}",
    "OutletPhase"    : COMMON_OID_PATH + COMMON_STATUS_PATH + ".3.{}",  # (R) INTEGER,
    "OutletState"    : COMMON_OID_PATH + COMMON_STATUS_PATH + ".4.{}",  # (R) INTEGER, # (R) INTEGER,
    "CommandPending" : COMMON_OID_PATH + COMMON_STATUS_PATH + ".5.{}",
    "OutletBank"     : COMMON_OID_PATH + COMMON_STATUS_PATH + ".6.{}",  # (R) INTEGER,
    "Load"           : COMMON_OID_PATH + COMMON_STATUS_PATH + ".7.{}",  # (R) Gauge,
    "ActivePower"    : COMMON_OID_PATH + COMMON_STATUS_PATH + ".8.{}",  # (R) Gauge,
    "Alarm"          : COMMON_OID_PATH + COMMON_STATUS_PATH + ".9.{}",  # (R) INTEGER,
    "PeakPower"      : COMMON_OID_PATH + COMMON_STATUS_PATH + ".10.{}", # (R) Gauge, # (R) DisplayString,
    "PeakPowerTime"  : COMMON_OID_PATH + COMMON_STATUS_PATH + ".11.{}", # (R) DisplayString,
    "PeakPowerStart" : COMMON_OID_PATH + COMMON_STATUS_PATH + ".12.{}",
    "Energy"         : COMMON_OID_PATH + COMMON_STATUS_PATH + ".13.{}", # (R) Gauge, # (R) DisplayString
    "EnergyStartTime": COMMON_OID_PATH + COMMON_STATUS_PATH + ".14.{}"
}

################################
# SCICAM and TECCAM Parameters #
################################

SOUL_TARGET_POS         =  [948  ,  1070]   # Target position of SOUL Alignment on SCICAM in Full_Image 2048x2048
center_of_rotation      =  [943 , 1063.5]   # Center of rotation of the bearing on SCICAM in Full_Image 2048x2048
fixed_teccam_ref_coords =  [0.07, 0.04]     #Fixed position in normalized coordinates where to close loop

dictScicamRegionBounds = {
    "Full_Image"                 :  {"x1" :   1, "x2" : 2048, "y1" :    1, "y2" : 2048} ,
    "Center"                     :  {"x1" :   1, "x2" : 2048, "y1" : 400,  "y2" : 1679} ,
    "Bottom"                     :  {"x1" :   1, "x2" : 2048, "y1" :    1, "y2" : 512}  ,
    "Top"                        :  {"x1" :   1, "x2" : 2048, "y1" : 1537, "y2" : 2048} ,
    "256x256"                    :  {"x1" : 815, "x2" : 1070, "y1" : 1050, "y2" : 1305} ,
    "128x128"                    :  {"x1" : 885, "x2" : 1012, "y1" : 1020, "y2" : 1147} ,
    "64x64"                      :  {"x1" : 916, "x2" :  979, "y1" : 1038, "y2" : 1101} ,
    "32x32"                      :  {"x1" : 932, "x2" :  963, "y1" : 1054, "y2" : 1085} ,
    "1000x1000"                  :  {"x1" : 449, "x2" : 1448, "y1" :  584, "y2" : 1583} ,
    "coro_stipe"                 :  {"x1" :   1, "x2" : 2048, "y1" : 1044, "y2" : 1103} ,
    "PD_wollaston_stripe_small"  :  {"x1" :   1, "x2" : 2048, "y1" : 1014, "y2" : 1093} ,
    "PD_wollaston_stripe_large"  :  {"x1" :   1, "x2" : 2048, "y1" :  984, "y2" : 1123} ,
    "run2"                       :  {"x1" : 899, "x2" : 1027, "y1" : 1020, "y2" : 1148} ,
    "obs_stripe_200px"       :  {"x1" :   1, "x2" : 2048, "y1" :  940, "y2" : 1139} ,
    "obs_stripe_512px"           :  {"x1" :   1, "x2" : 2048, "y1" :  784, "y2" : 1295}
    #"neptune_stripe_350px"       :  {"x1" :   1, "x2" : 2048, "y1" :  865, "y2" : 1214} 
}

Tech_Image_NGMAX = 2
Sci_Image_NGMIN  = 1
Default_NGMIN    = 2

########################################
# Scientific Filters Wavelength for PD #
########################################

filters_wavelength = {
    "NB_FeII"   :  1.644,
    "NB_HeI"    :  1.091,
    "NB_PaB"    :  1.282,
    "NB_ContJ"  :  1.216,
    "NB_ContH"  :  1.558,
    "NB_H2"     :  1.593,
    "NB_H3"     :  1.667,
    "BB_Y"      :  None,
    "BB_J"      :  None,
    "BB_H"      :  None,
    "ND2"       :  None,
    "ND3"       :  None,
    "ND4"       :  None,
    "HOLE"      :  None,
    "HOLE2"     :  None
}

####################
# LBT DD CONSTANTS #
####################

lbt_L_ins = "LBT_L_AUTH_INS"
lbt_R_ins = "LBT_R_AUTH_INS"

####################
# TEMPLATE OUTPUTs #
####################

tpl_output_names = {
    "sasha_flux"   : "IMAGE FLUX",
    "sasha_bkg"    : "IMAGE BACKGROUND",
    "sasha_min"    : "IMAGE MIN VAL",
    "sasha_max"    : "IMAGE MAX VAL",
    "converged"    : "CONVERGED",
    "threshold"    : "THRESHOLD",
    "soul_dx"      : "SOUL DX",
    "soul_dy"      : "SOUL DY",
    "psf_dx"       : "PSF_DX",
    "psf_dy"       : "PSF_DY",
    "pupil_counts" : "PUPIL TOT COUNTS",
    "pupil_max"    : "PUPIL_MAX VAL",
    "pupil_dx"     : "PUPIL_DX",
    "pupil_dy"     : "PUPIL_DY",
    "final_ee"     : "ENCIRCLED_ENERGY",
    "trial_ee_evol"  : "ENCICLED ENERGY EVOLUTION",
    "trial_dbi_ee_l" : "ENCIRCLED ENERGY L",
    "trial_dbi_ee_r" : "ENCIRCLED ENERGY R"
    
}
