def plot(i, pol):
     ii12 = analyse.loadfits(qdir+fits12[i][:-5]+'.qlook.ii.fits')
     ii13 = analyse.loadfits(qdir+fits13[i][:-5]+'.qlook.ii.fits')
     ii18 = analyse.loadfits(qdir+fits18[i][:-5]+'.qlook.ii.fits')
     rms12 = analyse.loadfits(qdir+fits12[i][:-5]+'.qlook.rms.fits')
     rms13 = analyse.loadfits(qdir+fits13[i][:-5]+'.qlook.rms.fits')
     rms18 = analyse.loadfits(qdir+fits18[i][:-5]+'.qlook.rms.fits')
     sp12 = analyse.loadfits(qdir+fits12[i][:-5]+'.qlook.data.fits')
     sp13 = analyse.loadfits(qdir+fits13[i][:-5]+'.qlook.data.fits')
     sp18 = analyse.loadfits(qdir+fits18[i][:-5]+'.qlook.data.fits')
     fig = pylab.figure(figsize=(25,25))
     fig.suptitle(name, fontsize=22)
     plot = analyse.custom_draw_map(figure=fig, xspacing=0.2, yspacing=0.2,
                                    tick_labels_size=12, colorber_font_size=11, show=False)
     splt = functools.partial(analyse.draw_otf_spectrum, figure=fig, tick_labels_size=12,
                              xlim=(-150,150), show=False)
     plot(ii12, subplot=331, title='12CO(2-1): mom0')
     plot(ii13, subplot=331+3, title='13CO(2-1): mom0')
     plot(ii18, subplot=331+6, title='C18O(2-1): mom0')
     plot(rms12, subplot=332, title='12CO(2-1): rms')
     plot(rms13, subplot=332+3, title='13CO(2-1): rms')
     plot(rms18, subplot=332+6, title='C18O(2-1): rms')
     splt(sp12, subplot=333, title='12CO(2-1): spectrum')
     splt(sp13, subplot=333+3, title='13CO(2-1): spectrum')
     splt(sp18, subplot=333+6, title='C18O(2-1): spectrum')
     fig.savefig(dirpath+name+'_%s_test.png'%(pol), dpi=70)
     fig.savefig(savepath+'qlook_obs_otf_'+timestamp+'_%s.png'%(pol.upper()))
     pylab.close(fig)
def qlook_radiop(dirpath, savepath='./'):
    import numpy
    import os
    import pylab
    import analyse
    import analyse.basefunc.analyse_radiop
    import analyse.plotter.plot_radiop
    import pylab
    
    if dirpath[-1]!='/': dirpath += '/'
    files = sorted(os.listdir(dirpath))
    
    fits_files = []
    # file check
    for f in files:
        if f.split('.')[-1]!='fits':
            continue
        fits_files.append(f)
        continue
    fits_files.sort()
    # load analysed data
    ana_data = []
    for f in fits_files:
        hdu = analyse.loadfits(dirpath+f)
        ana_data.append(analyse.basefunc.analyse_radiop.analyse_radiop(hdu))
    
    d = ana_data
    
    #import plot_radiop
    fig = pylab.figure(figsize=(12,5))
    name = f.split('_')[2]
    target = d[0][0]['target']
    pylab.rcParams['legend.fontsize'] = 5
    pylab.rcParams['figure.subplot.hspace'] = 0.5
    pylab.rcParams['figure.subplot.wspace'] = 0.5
    pylab.rcParams['font.size'] = 6
    ax1,ax2,fig = analyse.plotter.plot_radiop.draw_radiop(d[0][0], iso = '12CO_H', color='b',fig=fig,ax1_plot=221, ax2_plot=223,show=False)
    ax1,ax2,fig = analyse.plotter.plot_radiop.draw_radiop(d[2][0], iso = '13CO_H', color='g',fig=fig,ax1=ax1, ax2=ax2,show=False)
    ax1,ax2,fig = analyse.plotter.plot_radiop.draw_radiop(d[4][0], iso = 'C18O_H', color='c',fig=fig,ax1=ax1, ax2=ax2,show=False)
    ax1,ax2,fig = analyse.plotter.plot_radiop.draw_radiop(d[1][0], iso = '12CO_V', color='r',fig=fig,ax1=ax1, ax2=ax2,show=False)
    ax1,ax2,fig = analyse.plotter.plot_radiop.draw_radiop(d[3][0], iso = '13CO_V', color='m',fig=fig,ax1=ax1, ax2=ax2,show=False)
    ax1,ax2,fig = analyse.plotter.plot_radiop.draw_radiop(d[5][0], iso = 'C18O_V', color='y',fig=fig,ax1=ax1, ax2=ax2,show=False)
    ax3 = None
    ax4 = None
    ax3,ax4,fig = analyse.plotter.plot_radiop.draw_radiop(d[0][1], iso = '12CO_H', color='b',fig=fig, ax1=ax3, ax2=ax4, ax1_plot=144, ax2_plot=143,show=False, vertical=True)
    ax3,ax4,fig = analyse.plotter.plot_radiop.draw_radiop(d[2][1], iso = '13CO_H', color='g',fig=fig,ax1=ax3, ax2=ax4,show=False, vertical=True)
    ax3,ax4,fig = analyse.plotter.plot_radiop.draw_radiop(d[4][1], iso = 'C18O_H', color='c',fig=fig,ax1=ax3, ax2=ax4,show=False, vertical=True)
    ax3,ax4,fig = analyse.plotter.plot_radiop.draw_radiop(d[1][1], iso = '12CO_V', color='r',fig=fig,ax1=ax3, ax2=ax4,show=False, vertical=True)
    ax3,ax4,fig = analyse.plotter.plot_radiop.draw_radiop(d[3][1], iso = '13CO_V', color='m',fig=fig,ax1=ax3, ax2=ax4,show=False, vertical=True)
    ax3,ax4,fig = analyse.plotter.plot_radiop.draw_radiop(d[5][1], iso = 'C18O_V', color='y',fig=fig,ax1=ax3, ax2=ax4,show=False, vertical=True)
    ax1.legend()
    ax2.legend()
    ax3.legend()
    ax4.legend()
    fig.text(0.5,0.975, target + ' cross scan results ' + name, horizontalalignment='center',verticalalignment='top')
    pylab.savefig(savepath+'qlook_radiop_' + name + '.png')
    #pylab.savefig('qlook_radiop' + name + '.pdf')
    pylab.close(fig)
    return
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
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 qlook_ss(dirpath, filesave=False, savepath='./'):
    import os
    import pylab
    import analyse

    if dirpath[-1]!='/': dirpath += '/'
    files = sorted(os.listdir(dirpath))

    if len(files)<10:
        print('6 FITS files are required.')
        print(files)
        return

    fits_files = []
    # file check
    for f in files:
        if f.split('_')[0]!='ps':
            continue
        if f.split('.')[-1]!='fits':
            continue
        if f.split('.')[-3]=='qlook':
            continue
        fits_files.append(f)
        continue

    name = files[4].split('_12CO_H.fits')[0]
    timestamp = dirpath.split('/')[-2]

    for f in fits_files:
        os.system('python %s %s'%(__file__, dirpath+f))
        continue

    isotope = ['12CO', '13CO', 'C18O']
    fig = pylab.figure(figsize=(20,12))
    fig.suptitle(name+'_H')
    for i, f in enumerate(fits_files[::2]):
        _name = dirpath+f.split('.fits')[0]
        raw = analyse.loadfits(_name+'.fits')
        d = analyse.makespec(raw)
        cube, flag = analyse.basefit(d[0])
        #cube = analyse.loadfits(_name+'.qlook.data.fits')
        #flag = analyse.loadfits(_name+'.qlook.flag.fits')
        analyse.draw_ps_raw(raw, figure=fig, subplot=231+i, title='raw: '+isotope[i], show=False)
        analyse.draw_ss_spectrum(cube, flag, figure=fig, subplot=234+i, title='spectrum: '+isotope[i], show=False)
        continue
    fig.savefig(savepath+'qlook_standardsource_'+timestamp+'_H.png')
    pylab.close(fig)
    #pylab.show()

    fig = pylab.figure(figsize=(20,12))
    fig.suptitle(name+'_V')
    for i, f in enumerate(fits_files[1::2]):
        _name = dirpath+f.split('.fits')[0]
        raw = analyse.loadfits(_name+'.fits')
        d = analyse.makespec(raw)
        cube, flag = analyse.basefit(d[0])
        #cube = analyse.loadfits(_name+'.qlook.data.fits')
        #flag = analyse.loadfits(_name+'.qlook.flag.fits')
        analyse.draw_ps_raw(raw, figure=fig, subplot=231+i, title='raw: '+isotope[i], show=False)
        analyse.draw_ss_spectrum(cube, flag, figure=fig, subplot=234+i, title='spectrum: '+isotope[i], show=False)
        continue
    fig.savefig(savepath+'qlook_standardsource_'+timestamp+'_V.png')
    pylab.close(fig)
    #pylab.show()
    return
def qlook_skydip(dirpath, savepath='./'):
    import os
    import pylab
    import analyse
    import analyse.basefunc.analyse_skydip
    import analyse.plotter.plot_skydip
    import pylab
    
    if dirpath[-1]!='/': dirpath += '/'
    files = sorted(os.listdir(dirpath))
    
    fits_files = []
    # file check
    for f in files:
        if f.split('.')[-1]!='fits':
            continue
        fits_files.append(f)
        continue
    fits_files.sort()
    
    # load analysed data
    ana_data = []
    for f in fits_files:
        hdu = analyse.loadfits(dirpath+f)
        ana_data.append(analyse.basefunc.analyse_skydip.analyse_skydip(hdu))
    # plot 
    
    name = f.split('_')[2]
    pylab.rcParams['legend.fontsize']=5.5
    pylab.rcParams['figure.subplot.hspace']=0.5
    fig = pylab.figure(figsize=(8,5))
    ax = analyse.plotter.plot_skydip.draw_skydip(ana_data[0],fig=fig,show=False,iso='12CO H',color='b')
    ax = analyse.plotter.plot_skydip.draw_skydip(ana_data[2],fig=fig,ax=ax,show=False,iso='13CO H',color='g')
    ax = analyse.plotter.plot_skydip.draw_skydip(ana_data[4],fig=fig,ax=ax,show=False,iso='C18O H',color='c')
    ax = analyse.plotter.plot_skydip.draw_skydip(ana_data[1],fig=fig,ax=ax,show=False,iso='12CO V',color='r')
    ax = analyse.plotter.plot_skydip.draw_skydip(ana_data[3],fig=fig,ax=ax,show=False,iso='13CO V',color='m')
    ax = analyse.plotter.plot_skydip.draw_skydip(ana_data[5],fig=fig,ax=ax,show=False,iso='C18O V',color='y')
    ax.set_title('skydip results ' + name)
    ax.legend()

    #----------- plot raw data ---------------
    data12H = analyse.loadfits(dirpath+fits_files[0]).data
    data12V = analyse.loadfits(dirpath+fits_files[1]).data
    data13H = analyse.loadfits(dirpath+fits_files[2]).data
    data13V = analyse.loadfits(dirpath+fits_files[3]).data
    data18H = analyse.loadfits(dirpath+fits_files[4]).data
    data18V = analyse.loadfits(dirpath+fits_files[5]).data    
    print('-------sum 13 12 18 spectra------')
    
    import numpy
    x = numpy.linspace(0., 1.0, 16383)
    ax1 = fig.add_subplot(212)
    for  i in range(len(data12H)):
        d_H = numpy.concatenate((data18H[i]['data'], data12H[i]['data'], data13H[i]['data']))
        d_V = numpy.concatenate((data18V[i]['data'], data12V[i]['data'], data13V[i]['data']))
        ax1.plot(x, d_H,'b--')
        ax1.plot(x, d_V,'r--')
    ax1.set_ylabel('IF Power')
    ax1.set_xlabel('Frequency [GHz]')
    ax1.set_title('IFpower (blue:Hpol red:Vpol)')
    ax1.grid()
    
    pylab.savefig(savepath+'qlook_skydip_' + name + '.png')
    pylab.close(ax.figure)
    #pylab.show()
    return