import numpy as np
import fitsio
def write_prior_for_RR(targetid, z_prior, filename_priors):
"""
Minimal fonction to write prior file for redrock.
targetid : must be the array of targetid list given to redrock in the rrdesi command
z_prior : array of size targetid.size containing the prior value of the redshift for the considered targetid. For instant value from QuasarNet.
filename_priors : name of the prior file which will be given to the rrdesi command
"""
# need to be the same for every target
# only function[0] will be read in the prior class !
function = np.array(['tophat'] * z_prior.size)
# can be different for every target (I set it constant here)
sigma = 0.1*np.ones(z_prior.size)
# save
out = fitsio.FITS(filename_priors, 'rw', clobber=True)
data, names, extname = [targetid, function, z_prior, sigma], ['TARGETID', 'FUNCTION', 'Z', 'SIGMA'], 'PRIORS'
out.write(data, names=names, extname=extname)
out.close()
print(f' Write prior file for RR with {z_prior.size} objetcs: {filename_priors}')