"""
Copied from apex_cmz_h2co then cropped to just get the grids
"""
from astropy import log
from astropy.io import fits
from pyspeckit.spectrum import models
from pyspeckit.spectrum.models.model import SpectralModel
from paths import gpath

try:
    # create the Formaldehyde Radex fitter
    # This step cannot be easily generalized: the user needs to read in their own grids
    texgrid303 = fits.getdata(gpath('fjdu_pH2CO_303_tex_1kms.fits'))
    taugrid303 = fits.getdata(gpath('fjdu_pH2CO_303_tau_1kms.fits'))
    texgrid321 = fits.getdata(gpath('fjdu_pH2CO_321_tex_1kms.fits'))
    taugrid321 = fits.getdata(gpath('fjdu_pH2CO_321_tau_1kms.fits'))
    texgrid322 = fits.getdata(gpath('fjdu_pH2CO_322_tex_1kms.fits'))
    taugrid322 = fits.getdata(gpath('fjdu_pH2CO_322_tau_1kms.fits'))
    hdr = hdrb = fits.getheader(gpath('fjdu_pH2CO_303_tex_1kms.fits'))

    # # this deserves a lot of explanation:
    # # models.formaldehyde.formaldehyde_radex is the MODEL that we are going to fit
    # # models.model.SpectralModel is a wrapper to deal with parinfo, multiple peaks,
    # # and annotations
    # # all of the parameters after the first are passed to the model function

    h2co_radex_fitter = SpectralModel(
        models.formaldehyde_mm.formaldehyde_mm_radex,
        5,
        parnames=['temperature', 'column', 'density', 'center', 'width'],
        parvalues=[50, 12, 4.5, 0, 1],
    bt = re.compile("tex|tau|flux")

    (fTI, fpars, fbad_pars) = compute_grid(Radex=pyradex.fjdu.Fjdu,
                                           run_kwargs={})

    for pn in fpars:
        btype = bt.search(pn).group()
        ff = makefits(fpars[pn],
                      btype,
                      densities=densities,
                      temperatures=temperatures,
                      columns=columns)
        outfile = 'fjdu_pH2CO_{line}_{type}_{dv}.fits'.format(line=pn[-3:],
                                                              type=btype,
                                                              dv='1kms')
        ff.writeto(gpath(outfile), clobber=True)
        print(outfile)

    ff = makefits(fpars['fluxgrid_321'] / fpars['fluxgrid_303'],
                  'ratio',
                  densities=densities,
                  temperatures=temperatures,
                  columns=columns)
    outfile = 'fjdu_pH2CO_{line}_{type}_{dv}.fits'.format(line='321to303',
                                                          type='ratio',
                                                          dv='1kms')
    ff.writeto(gpath(outfile), clobber=True)

    (TI, pars, bad_pars) = compute_grid()

    for pn in pars:
import pyspeckit
import numpy as np
from astropy import log
from astropy.io import fits
from pyspeckit.spectrum import models
from pyspeckit.spectrum.models.model import SpectralModel
import FITS_tools
from paths import h2copath, mergepath, figurepath, gpath
from make_ratiotem_cubesims import tm
import os

try:
    # create the Formaldehyde Radex fitter
    # This step cannot be easily generalized: the user needs to read in their own grids
    texgrid303 = fits.getdata(gpath('fjdu_pH2CO_303_tex_5kms.fits'))
    taugrid303 = fits.getdata(gpath('fjdu_pH2CO_303_tau_5kms.fits'))
    texgrid321 = fits.getdata(gpath('fjdu_pH2CO_321_tex_5kms.fits'))
    taugrid321 = fits.getdata(gpath('fjdu_pH2CO_321_tau_5kms.fits'))
    texgrid322 = fits.getdata(gpath('fjdu_pH2CO_322_tex_5kms.fits'))
    taugrid322 = fits.getdata(gpath('fjdu_pH2CO_322_tau_5kms.fits'))
    hdr = hdrb = fits.getheader(gpath('fjdu_pH2CO_303_tex_5kms.fits'))

    # # this deserves a lot of explanation:
    # # models.formaldehyde.formaldehyde_radex is the MODEL that we are going to fit
    # # models.model.SpectralModel is a wrapper to deal with parinfo, multiple peaks,
    # # and annotations
    # # all of the parameters after the first are passed to the model function 

    h2co_radex_fitter = SpectralModel(models.formaldehyde_mm.formaldehyde_mm_radex,
                                      5,
                                      parnames=['temperature','column','density','center','width'],
        from paths import gpath
    except ImportError:
        gpath = lambda x: x
    bt = re.compile("tex|tau|flux")

    (fTI, fpars, fbad_pars) = compute_grid(Radex=pyradex.fjdu.Fjdu,
                                           run_kwargs={})

    for pn in fpars:
        btype = bt.search(pn).group()
        ff = makefits(fpars[pn], btype, densities=densities,
                      temperatures=temperatures, columns=columns)
        outfile = 'fjdu_pH2CO_{line}_{type}_{dv}.fits'.format(line=pn[-3:],
                                                              type=btype,
                                                              dv='5kms')
        ff.writeto(gpath(outfile),
                   clobber=True)
        print(outfile)

    ff = makefits(fpars['fluxgrid_321']/fpars['fluxgrid_303'], 'ratio',
                  densities=densities, temperatures=temperatures,
                  columns=columns)
    outfile = 'fjdu_pH2CO_{line}_{type}_{dv}.fits'.format(line='321to303',
                                                          type='ratio',
                                                          dv='5kms')
    ff.writeto(gpath(outfile), clobber=True)

    (TI, pars, bad_pars) = compute_grid()

    for pn in pars:
        btype = bt.search(pn).group()
Example #5
0
    newfile.header.update('CRVAL3' ,  (min(temperatures)) )
    newfile.header.update('CRPIX3' ,  1 )
    if len(np.unique(temperatures)) == 1:
        newfile.header.update('CTYPE3' ,  'ONE-TEMP' )
        newfile.header.update('CDELT3' , temperatures[0])
    else:
        newfile.header.update('CTYPE3' ,  'LIN-TEMP' )
        newfile.header.update('CDELT3' , (np.unique(temperatures)[1]) - (np.unique(temperatures)[0]) )
    return newfile

if __name__ == "__main__":
    import re
    from paths import gpath
    import os
    if not os.path.isdir(gpath('')):
        os.mkdir(gpath(''))
    bt = re.compile("tex|tau|flux")

    (fTI, fpars, fbad_pars) = compute_grid(Radex=pyradex.fjdu.Fjdu,
                                           run_kwargs={})
    
    for pn in fpars:
        btype = bt.search(pn).group()
        ff = makefits(fpars[pn], btype, densities=densities,
                      temperatures=temperatures, columns=columns)
        outfile = 'fjdu_hc3n_{line}_{type}_{dv}.fits'.format(line=pn.split("_")[-1],
                                                              type=btype,
                                                              dv='5kms')
        ff.writeto(gpath(outfile),
                   clobber=True)