def basefit_auto(hdu, v, fitting_range=[-150, 150], degree=1,
                 smooth=11, mincount=21, nsig=1.3, convolve=5):
    import numpy
    import analyse
    cube = hdu.data
    nz, ny, nx = cube.shape
    if hdu.header['_OBS-MOD']=='otf':
        ccube = analyse.convolve(hdu, convolve)
    else:
        ccube = hdu
        pass

    spectra = ccube.data.T.reshape(nx*ny, nz)
    fit_results = [basefit_auto_line(i, spec, v, fitting_range, degree, smooth, mincount, nsig) \
                   for i,spec in enumerate(spectra)]
    fit_results = numpy.array(fit_results)
    emission_flag = numpy.array(fit_results[:,1].reshape(nx, ny, nz).T, dtype=numpy.int16)
    fitted = _basefit_flag(cube, emission_flag)

    """
    import pyfits
    cfitted = _basefit_flag(ccube.data, emission_flag)
    cc = pyfits.PrimaryHDU(cfitted, hdu.header)
    flag = pyfits.PrimaryHDU(emission_flag, hdu.header)
    analyse.basefit_interactive(cc, flag)
    """
    return fitted, emission_flag
def easy_analyse(fitspath, save=False, savepath='./'):
    import analyse
    print('make_cube: %s'%(fitspath.split('/')[-1]))
    raw_data = analyse.loadfits(fitspath)
    cw_data = analyse.makespec(raw_data)
    fitted_data, flag = analyse.basefit(cw_data)
    convolved_data = analyse.convolve(fitted_data, 2)
    fitted_data, flag = analyse.basefit(convolved_data, 'auto')

    savepath = savepath + fitspath.split('.fits')[0]
    if save:
        analyse.savefits(fitted_data, savepath+'.qlook.data.fits', clobber=True)
        analyse.savefits(flag, savepath+'.qlook.flag.fits', clobber=True)
        pass

    raw_data.data = None
    cw_data.data = None
    del(raw_data)
    del(cw_data)
    return fitted_data
def easy_analyse(fitspath, output_dir, flag=None, plot=False, save=False):
    import analyse
    print('make_cube: %s'%(fitspath.split('/')[-1]))
    raw_data = analyse.loadfits(fitspath)
    cw_data = analyse.makespec(raw_data)

    if flag is None:
        fitted_data, flag = analyse.basefit(cw_data)
    else:
        flag = analyse.loadfits(flag)
        fitted_data = analyse.basefit_flag(cw_data, None, flag)
        pass
    convolved_data = analyse.convolve(fitted_data, 2)
    fitted_data = analyse.basefit_flag(convolved_data, None, flag)

    ii = analyse.make_2d_map(fitted_data, flag)
    rms = analyse.make_2d_map(fitted_data, flag, 'rms')

    if save:
        savepath = output_dir + fitspath.split('/')[-1].split('.fits')[0]
        analyse.savefits(fitted_data, savepath+'.qlook.data.fits', clobber=True)
        analyse.savefits(flag, savepath+'.qlook.flag.fits', clobber=True)
        analyse.savefits(ii, savepath+'.qlook.ii.fits', clobber=True)
        analyse.savefits(rms, savepath+'.qlook.rms.fits', clobber=True)
        pass

    if plot:
        savepath = output_dir + fitspath.split('/')[-1].split('.fits')[0]
        isotope = fitspath.split('_')[-2]
        plot = analyse.custom_draw_map(figure=(8,8), xspacing=0.2, yspacing=0.2,
                                       tick_labels_size=12, colorber_font_size=11, show=False)
        plot(ii, title='mom0: '+isotope).save(savepath+'.qlook.ii.png')
        plot(rms, title='rms: '+isotope).save(savepath+'.qlook.rms.png')
        analyse.draw_otf_spectrum(fitted_data, figure=(12,5), title='spectra: '+isotope,
                                  show=False).savefig(savepath+'.qlook.spectrum.png')
        pass

    raw_data.data = None
    cw_data.data = None
    return fitted_data, flag