#==============================================================
#
#  Abs:  Initialize Area Detector Camera and load databases
#
#  Name: init_cam.cmd.ad
#
#  Side:  This script expectes the following environment
#         variables tobe defined upon entry:
#           PREFIX      - <device_type>:<area>
#           EVENT       - Camera trigger event
#           QSIZE       - Queue size
#           XSIZE       - X pixel count of camera sensor
#           YSIZE       - Y pixel count of camera sensor
#           X_OFF       - X Offset
#           Y_OFF       - Y Offset
#           NELEMENTS   - Number of elements
#           NCHANS      - Number of channels
#           RESOLUTION  - bit resolution
#           EGU         - Engineering Units
#           BLOCKING    -
#           IP          - Camera IP address (ie. 172.27.121.4)
#           ID          - Camera port name id number
#
#         Upon entering, this script expectes to be in the
#         iocBoot/<facility>/<ioc> directory, so that it can
#         load the CAM_INITS script to setup the camera env
#         variables.  The INIT_CAM env variable may also include
#         a relatative path as a prefix to the filename.
#
#         Upon exit, this script will leave the ioc sitting
#         at the application TOP directory.
#
#  Facility: EED Laser Controls
#
#  Auth: 08-Feb-2018, James Morad     (JMORAD)
#  Rev:  dd-mmm-yyyy, Reviewer's Name (USERNAME)
#--------------------------------------------------------------
#  Mod:
#        27-Sep-2022, S. Alverson     (ALVERSON):
#         add NDPvaConfigure
#         add NDPva.template
#        26-May-20201, S. Alverson    (ALVERSON):
#         remove profile2.template
#         remove NDStats2.template
#        24-May-20201, K . Luchini    (LUCHINI):
#          remove env var CAM_INITS
#        21-May-2021, K. Luchini      (LUCHINI):
#         add standard header
#         use env variables to setup directory
#         to TOP/db before loading dbs and to
#         TOP upon existig this script.
#
#==============================================================

cd("${TOP}/db")

#dbLoadRecords("ADBase.template", "P=$(PREFIX):$(ID),R=:,PORT=PS$(PORT),ADDR=0,TIMEOUT=1")
callbackSetQueueSize(10000)

# ========================================================
# Configure Arrays for the Acquisition
# ========================================================
NDStdArraysConfigure("Image$(PORT)", $(QSIZE), 0, "PS$(PORT)", 0)
dbLoadRecords("NDStdArrays.template", "P=$(PREFIX):$(ID),R=:RAW:, PORT=Image$(PORT), ADDR=0, TIMEOUT=1, TYPE=Int16, FTVL=SHORT, NELEMENTS=$(NELEMENTS), NDARRAY_PORT=PS$(PORT),NDARRAY_ADDR=0")

NDPvaConfigure("PVA$(PORT)", $(QSIZE), 0, "PS$(PORT)", 0, $(PREFIX):$(ID):RAW, 0, 0, 0)
dbLoadRecords("NDPva.template",  "P=$(PREFIX):$(ID),R=:RAWPVA:, PORT=PVA$(PORT),ADDR=0,TIMEOUT=1,NDARRAY_PORT=PS$(PORT)")

# ========================================================
# Configure NDROI
# ========================================================
NDROIConfigure("ROI$(PORT)", $(QSIZE), $(BLOCKING), "PS$(PORT)", 0, 0, 0)
dbLoadRecords("NDROI.template", "P=$(PREFIX):$(ID),R=:ROI:,  PORT=ROI$(PORT),ADDR=0,TIMEOUT=1,NDARRAY_PORT=PS$(PORT)")

NDStdArraysConfigure("ImageROI$(PORT)", 3, 0, "ROI$(PORT)", 0)
dbLoadRecords("NDStdArrays.template", "P=$(PREFIX):$(ID),R=:Image:, PORT=ImageROI$(PORT), ADDR=0, TIMEOUT=1, TYPE=Int16, FTVL=SHORT, NELEMENTS=$(NELEMENTS), NDARRAY_PORT=ROI$(PORT),NDARRAY_ADDR=0")

NDPvaConfigure("PVAROI$(PORT)", $(QSIZE), 0, "ROI$(PORT)", 0, $(PREFIX):$(ID):Image, 0, 0, 0)
dbLoadRecords("NDPva.template",  "P=$(PREFIX):$(ID),R=:ImagePVA:, PORT=PVAROI$(PORT),ADDR=0,TIMEOUT=1,NDARRAY_PORT=ROI$(PORT)")

# ========================================================
# Configure Statistics Plugin
# ========================================================
NDStatsConfigure("STATS$(PORT)", $(QSIZE), $(BLOCKING), "ROI$(PORT)", 0, 0, 0)
dbLoadRecords("NDStats.template", "P=$(PREFIX):$(ID),R=:Stats:,  PORT=STATS$(PORT),ADDR=0,TIMEOUT=1,HIST_SIZE=256,XSIZE=$(XSIZE),YSIZE=$(YSIZE),NCHANS=$(NCHANS),NDARRAY_PORT=ROI$(PORT),NDARRAY_ADDR=0")
NDTimeSeriesConfigure("STATS$(PORT)_TS", $(QSIZE), 0, "STATS$(PORT)", 1, 23)
dbLoadRecords("NDTimeSeries.template", "P=$(PREFIX):$(ID),R=:StatsTS:, PORT=STATS$(PORT)_TS,ADDR=0,TIMEOUT=1,NDARRAY_PORT=STATS$(PORT),NDARRAY_ADDR=1,NCHANS=$(NCHANS),ENABLED=1")

#NDStats2Configure("STATS$(PORT)", $(QSIZE), $(BLOCKING), "ROI$(PORT)", 0, 0, 0)
#dbLoadRecords("NDPluginBase.template","P=$(PREFIX):$(ID),R=:Stats:,  PORT=STATS$(PORT),ADDR=0,TIMEOUT=1,NDARRAY_PORT=ROI$(PORT),NDARRAY_ADDR=0")
#dbLoadRecords("NDStats2.template", "P=$(PREFIX):$(ID),R=:Stats:,  PORT=STATS$(PORT),ADDR=0,TIMEOUT=1,HIST_SIZE=256,XSIZE=$(XSIZE),YSIZE=$(YSIZE),NCHANS=$(NCHANS),NDARRAY_PORT=ROI$(PORT),NDARRAY_ADDR=0")

# ========================================================
# Configure Processing Plugin
# ========================================================
NDProcessConfigure("PROC$(PORT)", $(QSIZE), $(BLOCKING), "ROI$(PORT)", 0, 0, 0)
dbLoadRecords("NDProcess.template",   "P=$(PREFIX):$(ID),R=:Proc:,  PORT=PROC$(PORT),ADDR=0,TIMEOUT=1,NDARRAY_PORT=ROI$(PORT)")

# ========================================================
# File Plugins
# ========================================================
# NDFileTIFFConfigure(const char *portName, int queueSize, int blockingCallbacks, const char *NDArrayPort, int NDArrayAddr, int priority, int stackSize)
# Use the Raw port for input
NDFileTIFFConfigure("TIFF$(PORT)", $(QSIZE), $(BLOCKING), "Image$(PORT)", 0)
dbLoadRecords("NDFileTIFF.template",   "P=$(PREFIX):$(ID),R=:TIFF:,PORT=TIFF$(PORT),NDARRAY_PORT=Image$(PORT),NDARRAY_ADDR=0,TIMEOUT=1")

# NDFileJPEGConfigure(const char *portName, int queueSize, int blockingCallbacks, const char *NDArrayPort, int NDArrayAddr, int priority, int stackSize)
NDFileJPEGConfigure("JPEG$(PORT)", $(QSIZE), $(BLOCKING), "Image$(PORT)", 0)
dbLoadRecords("NDFileJPEG.template", "P=$(PREFIX):$(ID),R=:JPEG:,PORT=JPEG$(PORT),NDARRAY_PORT=Image$(PORT),NDARRAY_ADDR=0,TIMEOUT=1")

# Extras

dbLoadRecords("NDFile.template"  ,"P=$(PREFIX):$(ID),R=:,PORT=PS$(PORT),ADDR=0,TIMEOUT=1")
#dbLoadRecords("profile.template" ,"P=$(PREFIX):, ID=$(ID), XSIZE=$(XSIZE), X_OFF=$(X_OFF), YSIZE=$(YSIZE), Y_OFF=$(Y_OFF), RESOLUTION=$(RESOLUTION), PREC=1, EGU=$(EGU)")
dbLoadRecords("profile2.template","P=$(PREFIX):, ID=$(ID), XSIZE=$(XSIZE), X_OFF=$(X_OFF), YSIZE=$(YSIZE), Y_OFF=$(Y_OFF), RESOLUTION=$(RESOLUTION), PREC=1, EGU=$(EGU)")
dbLoadRecords("camera_extras.db" ,"P=$(PREFIX):, ID=$(ID), XSIZE=$(XSIZE), X_OFF=$(X_OFF), YSIZE=$(YSIZE), Y_OFF=$(Y_OFF), RESOLUTION=$(RESOLUTION), PREC=1, EGU=$(EGU)")

cd("${TOP}")

# End of file

