""" 
The main purpose of this script is to show how you can save a spectrum in a fits
 file from Jython scripting interface in CASSIS.

Author: J-M. Glorian
Author: M. Boiziot
Date: 01/20/16
Licence: CeCILL-C
Tested on: CASSIS 6.5
"""

# The imports
from eu.omp.irap.cassis.file import FileManagerFits
from java.io import File
from eu.omp.irap.cassis.common import  CommentedSpectrum
from eu.omp.irap.cassis.properties import Software
import os


# First, create a simple spectrum with x (frequency, in MHz) and y (intensity) points.
freq = range(1, 11)
inte = [ 0.0, 0.1, 0.5, 2.0, 1.2, 0.8, 0.3, 0.1, 0.0, 0.0 ]

# Create a CommentedSpectrum object needed to save the spectrum.
cs = CommentedSpectrum(None, freq, inte, "SpectrumName")

# Create a FitsManagerFits needed for the save
fileManagerFits = FileManagerFits(None)

# Save it, by default, in cassisPath/delivery/script/outputs (creates it if it doesn't exist)
outputDir = Software.getCassisPath() + "/delivery/script/outputs/"
if not os.path.exists(outputDir):
    os.makedirs(outputDir)

file_name = "spectrum.fits"
path =  outputDir + file_name
fileManagerFits.save(File(path),  cs)

