#======================================================================================#
# This script is an example showing how to use the PCygniModel
#
# Data file in myDirInput : I16293_H2CCO.lis
# Files created in myDirOutput : myName_bestModel.lis, myName.lam, myName.dat
# (see below for the definitions of myDirInput, myDirOutput and myName)
# 
# To run this script as it is, the only thing you need to specify is the path to your python executable (myPython variable).
#
# Last update: 12-jan-2026
# Tested on: CASSIS 6.5
#
# CASSIS scripts and scripting documentation :
#   - http://cassis.irap.omp.eu/docs/CASSIS_scripts_summary.pdf
#   - http://cassis.irap.omp.eu/docs/script/README.html
#======================================================================================#

import os
from eu.omp.irap.cassis.properties import Software
import ScriptEnvironment
import subprocess
from PCygni import PCygniModel
from Range import Range
from PCygniComponent import PCygniComponent
import time


# USER INPUTS

Range.unit = "km/s"
velocityRange = Range(3.0, 4.35)

myDirInput = Software.getCassisPath()+"/delivery/script/data/"
myDirOutput = Software.getCassisPath()+"/delivery/script/outputs/"
if not os.path.exists(myDirOutput):
    os.makedirs(myDirOutput)

inputFile = myDirInput+"I16293_H2CCO.lis"
sourceName = "I16293"
speciesName = "H2CCO"
myName = sourceName + "_" + speciesName
outputFile = myDirOutput + myName

# MODEL INPUTS

pCygniModel = PCygniModel(
    tuningRange = [0.0, 0.0],
    tuningBand = 15.0,
    moltag = 42501,
    template = "Full CDMS",
    eup = [0.0, 150.0],
    telescope = "alma_400m",
    tmb2ta = False,
    inputFile = inputFile,
    outputFile = outputFile,
    selectedLines = {"1": velocityRange},
    rmsLines = {"1": 0.045},
    calLines = {"1": 0.1}
)

# MODEL PARAMETERS

comp_1 = PCygniComponent(
    Tr = {"min": 35.0, "max": 65.0, "nstep": 11},
    tau = {"min": 0.15, "max": 0.55, "nstep": 11},
    Vin = {"min": 0.25, "max": 0.25, "nstep": 11},
    fwhm = {"min": 0.30, "max": 0.40, "nstep": 11},
    vlsr = {"min": 3.45, "max": 3.45, "nstep": 11},
    Tf = {"min": 2.70, "max": 2.70, "nstep": 11},
    Tc = {"min": 20.0, "max": 20.0, "nstep": 11},
    Fi = {"min": 0.40, "max": 0.40, "nstep": 11}
)

# RUNNING

# Execution time beginning
timeStart = time.time()
# Computation of the model
pCygniModel.computeBestPhysicalModel(comp_1)
# Plot of the best model overlayed with the input data
pCygniModel.plotBestPhysicalModel()

# Execution time ending
timeEnd = time.time()
print("CASSIS execution time = %.3f seconds." % (timeEnd - timeStart))
