line survey后续一:找到对辐射谱有贡献的跃迁

该博客主要涉及利用Python处理CDMS数据库和Molfit文件,进行分子线的搜索和证认。通过编写查询字符串,筛选频率和能量范围内的跃迁,并对已证认的分子进行操作。使用XCLASS包进行拟合,提取超过3σ的拟合结果。此外,还介绍了如何将Molfit文件拆分为单个文件并分析识别的过渡。代码中包含了数据库读取、数据处理和文件操作的详细步骤。
摘要由CSDN通过智能技术生成

有两个功能没写好。
XCLASS的分子线证认已经写得很好,但是后续还要自己做一些比较繁杂的工作,这里放了部分的操作步骤。
根据CDMS的database文件和已经证认好分子的molfit文件,这里可以搜寻一下,得出拟合结果大于3sigma的跃迁。

下面的代码是写好的package。

#!/usr/bin/python
# -*- coding: utf-8 -*-
##********************************************************************************************************************************************************

##********************************************************************* load packages ********************************************************************
from __future__ import print_function                                                       ## import print_function package
import numpy                                                                                ## import numpy package
import os                                                                                   ## import os package
import sys                                                                                  ## import sys package
import string                                                                               ## import string package
import random                                                                               ## import random package
import datetime                                                                             ## import datetime package
import sqlite3                                                                              ## import sqlite3 package
from pylab import *                                                                         ## import pylab package
import task_MAGIX                                                                           ## import MAGIX function from XCLASS package
import task_myXCLASS                                                                        ## import package myXCLASS
import task_myXCLASSFit                                                                     ## import package myXCLASSFit
from collections import Counter
##--------------------------------------------------------------------------------------------------------------------------------------------------------


##--------------------------------------------------------------------------------------------------------------------------------------------------------
##
## 
def QueryString(FreqMin,FreqMax,ElowMin, ElowMax,ListOfSelectedMolecules):

        ## define frequency interval form mouse position
        FreqIntMin = FreqMin
        FreqIntMax = FreqMax


        ## initialize some parameter
        NameOfRadTransTable = "transitions"
        ColumnNameForNameTransitions = "T_Name"
        ColumnNameForFreqTransitions = "T_Frequency"
        ColumnNameForIntTransitions = "T_Intensity"
        ColumnNameForEinsteinATransitions = "T_EinsteinA"
        ColumnNameForFreqErrTransitions = "T_Uncertainty"
        ColumnNameForELowTransitions = "T_EnergyLower"
        ColumnNameForgUpTransitions = "T_UpperStateDegeneracy"
        ColumnNameForQNUpLabelTransitions = "T_UpperStateQuantumNumbers"
        ColumnNameForQNLowLabelTransitions = "T_LowerStateQuantumNumbers"
        ColumnNameForURLTransitions = "T_URL"


        ## define query string
        ##
        ## get names of columns with
        ##      import sqlite3
        ##      connection = sqlite3.connect('cdms_sqlite.db')
        ##      cursor = connection.execute('select * from partitionfunctions')
        ##      names = list(map(lambda x: x[0], cursor.description))
        ##
        query_string = "SELECT "
        query_string += ColumnNameForNameTransitions + ", "                                 ## position 1: molecule name
        query_string += ColumnNameForFreqTransitions + ", "                                 ## position 2: frequency
        query_string += ColumnNameForIntTransitions + ", "                                  ## position 3: intensity
        query_string += ColumnNameForEinsteinATransitions + ", "                            ## position 4: Einstein A
        query_string += ColumnNameForFreqErrTransitions + ", "                              ## position 5: Error frequency
        query_string += ColumnNameForELowTransitions + ", "                                 ## position 6: E_low
        query_string += ColumnNameForgUpTransitions + ", "                                  ## position 7: upper state degeneracy
        query_string += ColumnNameForQNUpLabelTransitions + ", "                            ## position 8: quantum number label for upper state
        query_string += ColumnNameForQNLowLabelTransitions                                  ## position 9: quantum number label for lower state
        # query_string += ColumnNameForURLTransitions                                       ## position 10: URL
        query_string += " FROM " + NameOfRadTransTable
        query_string += " WHERE (" + ColumnNameForFreqTransitions + " >= " + str(FreqIntMin)
        query_string += " and " + ColumnNameForFreqTransitions + " <= " + str(FreqIntMax)
        if (ElowMin < ElowMax):
            query_string += " and " + ColumnNameForELowTransitions + " >= " + str(ElowMin)
            query_string += " and " + ColumnNameForELowTransitions + " <= " + str(ElowMax)
        if (len(ListOfSelectedMolecules) > 0):
            if (ListOfSelectedMolecules[0] != ["None"]):
                query_string += " and ("
                counter = 0
                for molecule in ListOfSelectedMolecules:
                    counter += 1
                    if (counter > 1):
                        query_string += " or "
                    query_string += ColumnNameForNameTransitions + " = " + chr(34) + molecule.strip() + chr(34)
                query_string += ")"
        query_string += ")"
        query_string += " ORDER by " + ColumnNameForFreqTransitions

        # Debug:
        #    print " "
        #    print ("\n\nquery_string = ", query_string)
        #    print ("len(query_string) = ", len(query_string))

        return (query_string)



def GetTrans(rows):
    names = []
    freqs = []
    for row in rows:                                                                    ## loop over all entries of the database
        element_counter = 0
        for elements in row:
            element_counter += 1
            if (element_counter == 1):                                                  ## get name of molecule
                name = elements.strip()
                name = '%25s' % name[:25]                                               ## name of molecule has max. 25 characters
                names.append(name)
            elif (element_counter == 2):                                                ## get frequency (in MHz)
                freqVal = '%17s' % elements
                if (freqVal.strip() != "None"):
                    freqVal = float(freqVal)
                    freq = '%17.5f' % freqVal
                    freqs.append(freq)
                else:
                    freq = freqVal
                    freqs.append(freq)
    return (names,freqs)

def MKMolFileOfMolecule(MolfitsFile,path,spw,molecule):
    f = open(MolfitsFile)
    lines = f.readlines()
    f.close()
    for i in range(len(lines)):
        CurrentLine = lines[i].strip()
        w = CurrentLine.find("%") 
        if (w == 0):
            CurrentLine = "" 
        elif (w > 0):
            CurrentLine = CurrentLine[:w]
        if (CurrentLine != ""):
            SplitLines = CurrentLine.split()
            helpstring = SplitLines[-1].strip()
        else:
            helpstring = ''

        if (helpstring.isdigit()):
            if int(helpstring)>3:
                print ("helpstring is %s" %(helpstring))
                print ("If helpstring larger than 3, lease modify the code!")
            if (SplitLines[0]==molecule):
                if '#' in molecule:
                    molfitfilename = string.replace(molecule,';','_')
                else:
                    molfitfilename = string.replace(molecule[:-1],';','_')

                print(molfitfilename)
                if int(helpstring)==1:
                    numpy.savetxt(path+"/molfitfilein%s/%s.molfit" %(spw,molfitfilename),[CurrentLine,lines[i+1].strip()],fmt='%s')
                elif int(helpstring)==2:
                    numpy.savetxt(path+"/molfitfilein%s/%s.molfit" %(spw,molfitfilename),[CurrentLine,lines[i+1].strip(),lines[i+2].strip()],fmt='%s')
                elif int(helpstring)==3:
                    numpy.savetxt(path+"/molfitfilein%s/%s.molfit" %(spw,molfitfilename),[CurrentLine,lines[i+1].strip(),lines[i+2].strip(),lines[i+3].strip()],fmt='%s')

def GetIdentifiedTransitions(names,freqs,FreqStep,modeldata,expdata,molecule,sigma):
    name = []
    freq = []
    value = []
    tag_molecule = []
    newmolname = tagmoleculenames(molecule)
    for i in range(len(names)):
        if (names[i].encode('raw_unicode_escape').strip()==molecule):
            model_position = numpy.argwhere(numpy.array(modeldata)[:,0]-FreqStep/2>float(freqs[i].strip()))[0]
            exp_position = numpy.argwhere(numpy.array(expdata)[:,0]-FreqStep/2>float(freqs[i].strip()))[0]
            model_x,model_y = modeldata[:,0],modeldata[:,1]
            exp_x,exp_y = expdata[:,0],expdata[:,1]
            if model_y[model_position]>3*sigma:
                tag_molecule.append(newmolname)
                __name = names[i].encode('raw_unicode_escape').strip()
                w = __name.find('#')
                if (w<0):
                    __name = __name[:-1]

                name.append(__name)
                freq.append(float(freqs[i].strip()))
                value.append(float(exp_y[exp_position]))
    return (tag_molecule,name,freq,value)


def tagmoleculenames(molname):
    new_molname = ''
    value = molname.find('#')
    if (value>0):
        molname = molname[:value]

    molname = molname[:-1]

    if ('-' in molname):
        start = molname.find('-')
        if ('-' in molname[start+1:]):
            end = start+1+molname[start+1:].find('-')
            superscr = molname[start+1:end]
            molname_extracted = string.replace(molname,molname[start:end+1],'')
            subscr = []
            count = 0
            a = 0
            for i in molname_extracted[:molname_extracted.find(';')]:
                count+=1
                if (i.isdigit()):
                    subscr.append(i)
                    new_molname = molname_extracted[:molname_extracted.find(';')]
                    if (count<start):
                        a += 1
                    b = dict(Counter(subscr))
                    c = b.keys()
                    for num in range(len(c)):
                        new_molname = string.replace(new_molname,c[num],'$_'+c[num]+'$')
                        if num ==0:
                            new_molname = new_molname+molname_extracted[molname_extracted.find(';'):]
                if (subscr==[]):
                    new_molname = molname_extracted
            print(superscr)
            new_molname = string.replace(new_molname,new_molname[start+a*3-1:],'$^{'+superscr+'}$'+new_molname[start+a*3-1:])

    else:
        subscr = []
        for i in molname[:molname.find(';')]:
            if (i.isdigit()):
                subscr.append(i)
                new_molname = molname[:molname.find(';')]
                b = dict(Counter(subscr))
                a = b.keys()
                for num in range(len(a)):
                    new_molname = string.replace(new_molname,a[num],'$_'+a[num]+'$')
                    if num ==0:
                        new_molname = new_molname+molname[molname.find(';'):]
            
        if (len(subscr)==0):
                new_molname = molname

    return (new_molname)


def ListSelectMolecule(SelectMolecule):
    ##====================================================================================================================================================
    ## test, if SelectMolecule is a list, otherwise try to use SelectMolecule as filename
    print (" ")
    print (" ")
    print ("Analyze selected molecules ..")
    sys.stdout.flush()
    ListOfSelectedMolecules = []
    if (len(SelectMolecule) > 0 and SelectMolecule != ["None"]):
        for entry in SelectMolecule:                                                        ## loop over all entries of SelectMolecule list
            entryStrip = entry.strip()                                                      ## remove leading and trailing blanks


            ## check, if current entry is a file
            if (os.path.isfile(entryStrip)):


                ##----------------------------------------------------------------------------------------------------------------------------------------
                ## if file is a molfit file, get names of molecules from molfit file
                if (entryStrip.endswith("molfit")):

                    # Debug:
                    # print "Selection string is a molfit file!"


                    ## read in contents of molfit file
                    f = open(entryStrip)
                    MolfitContents = f.readlines()                                          ## read in contents of molfit file
                    f.close()


                    ## get names of molecules
                    for lines in MolfitContents:                                            ## loop over all lines of molfit file
                        CurrentLine = lines.strip()                                         ## remove leading and trailing blanks

                        # Debug:
                        # print "CurrentLine = sys.stdout.flush() Python的作用", CurrentLine


                        ## check for comments
                        w = CurrentLine.find("%")                                           ## are there comments in the current line ?
                        if (w == 0):                                                        ## ignore lines which contains only comments
                            CurrentLine = ""                                                ## if line is only a comment clear line
                        elif (w > 0):                                                       ## is there a comment in the current line ?
                            CurrentLine = CurrentLine[:w]                                   ## remove comments

                        # Debug:
                        # print "CurrentLine = ", CurrentLine


                        ## analyze, if the current line contains the name of a molecule
                        if (CurrentLine != ""):                                             ## ignore empty lines
                            SplitLines = CurrentLine.split()                                ## split current line into columns

                            # Debug:
                            # print "SplitLines = ", SplitLines


                            ## contains the last column an integer number?
                            helpstring = SplitLines[-1].strip()
                            is_int = "false"
                            if (helpstring.isdigit()):                                      ## continue, if string is an integer number
                                is_int = "true"


                            ## If yes, then the current line contains the name of a molecule
                            if (is_int == "true"):                                          ## if last entry in a line is an integer number, then
                                MolName = ""                                                ## the current line contains the name of a molecule
                                for col in SplitLines[:-1]:                                 ## get whole line without integer number at the end
                                    MolName += col                                          ## construct entry for SelectMolecule

                                # Debug:
                                # print "MolName = ", MolName

                                ListOfSelectedMolecules.append(MolName)                     ## construct final array containing all molecule names


                ##----------------------------------------------------------------------------------------------------------------------------------------
                ## read names of molecules from ASCII file
                else:

                    # Debug:
                    # print "Selection string is a ASCII file!"


                    ## read in whole ASCII file
                    f = open(entryStrip)
                    AllLines = f.readlines()
                    f.close()


                    ## append lines in ASCII file to selection list
                    for line in AllLines:
                        ListOfSelectedMolecules.append(line)


            ##--------------------------------------------------------------------------------------------------------------------------------------------
            ## continue here, if current entry is not a filename
            else:
                ListOfSelectedMolecules.append(entryStrip)

    # Debug:
    # print ("ListOfSelectedMolecules = ", ListOfSelectedMolecules)
    return (ListOfSelectedMolecules)



## read data from database
def ReadDatabase(query_string):
    ##====================================================================================================================================================
    ## get path of default database file
    dbFilename = task_myXCLASS.GetDefaultDBFile()

    ##====================================================================================================================================================
    ## connect to the sqlite3 database

    ## connect to database
    try:
        conn = sqlite3.connect(dbFilename)
        print (" ")
        print ("Connection to sqlite3 database %s established." % dbFilename)
        print (" ")
        print (" ")
    except sqlite3.Error, e:
        print (" ")
        print ("Can not connect to sqlite3 databse %s." % dbFilename)
        print ("Error: %d: %s" % (e.args[0], e.args[1]))
        sys.exit(1)

    cursor = conn.cursor()
    cursor.execute(query_string)
    rows = cursor.fetchall()
    return (rows)

这里是在casa里运行的调用上面package的代码。

#data
from task_SelectTransitions import *
import os
#from pandas import DataFrame
import numpy as np
##remove the old runing data
import shutil
path = '/home/zhang/software/XCLASS-Interface/run/myXCLASS/'
if os.path.exists(path)==False:
    os.makedirs(path)
files = os.listdir(path)
if len(files)>0:
    shutil.rmtree(path)
    os.makedirs(path)

spw = "spw35"
sigma = 0.3


if spw=='spw35':
    FreqMin = 9.753638179980000132e+04
    FreqMax = 9.941000529416945938e+04
else:
    FreqMin = 9.947395539869999629e+04
    FreqMax = 1.013475792277295404e+05
#data off
#FreqMin = 219345
#FreqMax = 221323

FreqStep = 0.4883
#######################
TelescopeSize = 1.000
######################
Inter_Flag = True

t_back_flag = False
#tBack = 0
#tslope = 0.0000000000E+00

nH_flag = False
#N_H = 3.0000000000E+24
#beta_dust = 2.0
#kappa_1300 = 0.02


iso_flag = False
#IsoTableFileName = " "

RestFreq = 0.0
vLSR = 0.0
MolfitsFile = "/home/zhang/data/ATOMS/spec/I17233-3606/3/%s.molfit" %(spw)


FileName = "/home/zhang/data/ATOMS/spec/I17233-3606/3/I17233-3606_%s_17h26m42.4915s_-36d9m18.1152s.txt" %(spw)
NumHeaderLines = 0
expdata = LoadASCIIFile()




MinTransEnergy = 0.0
#data
#xLowerLimit = 9.753638179980000132e+04
#xUpperLimit = 9.941000529416945938e+04
#data off
#xLowerLimit = 219345
#xUpperLimit = 221323

SelectMolecule = ["/home/zhang/data/ATOMS/spec/I17233-3606/3/%s.molfit" %(spw)]
FrequencyWidth = 5.0


yLowerLimit = -8
yUpperLimit = 109
PlotTitle = "I17233_%s" %(spw)
LegendFlag = True
SaveFigureFile = " "
#OutputDevice = " "
ElowMin = 0.0
ElowMax = 1200.0

ListOfSelectedMolecules = ListSelectMolecule(SelectMolecule)

query_string = QueryString(FreqMin,FreqMax,ElowMin, ElowMax,ListOfSelectedMolecules)
rows = ReadDatabase(query_string)
names,freqs = GetTrans(rows)
path = os.getcwd()
direxists = os.path.exists(path+'/molfitfilein%s/' %(spw))
if direxists==False:
    os.makedirs('molfitfilein%s' %(spw))

if (os.path.exists('tag_transitions_%s.txt' %(spw))==False):
    tag_molecules = []
    note_molecules = []
    tag_x_positions = []
    tag_y_positions = []

    for molecule in ListOfSelectedMolecules:
        MKMolFileOfMolecule(MolfitsFile,path,spw,molecule)
        if '#' in molecule:
            molfitfilename = string.replace(molecule,';','_')
        else:
            molfitfilename = string.replace(molecule[:-1],';','_')
    
        MolfitsFileName = path+"/molfitfilein%s/%s.molfit" %(spw,molfitfilename)
        modeldata,log,TransEnergies,IntOptical,jobDir = myXCLASS ()
        tag_molecule,name,freq,value = GetIdentifiedTransitions(names,freqs,FreqStep,modeldata,expdata,molecule,sigma)
        tag_molecules.append(tag_molecule)
        note_molecules.append(name)
        tag_x_positions.append(freq)
        tag_y_positions.append(value)

    tag_molecules.remove([])
    note_molecules.remove([])
    tag_x_positions.remove([])
    tag_y_positions.remove([])
    tag = []
    for a in tag_molecules:
        for b in a:
            tag.append(b)
    
    x = []
    for a in tag_x_positions:
        for b in a:
            x.append(b)
    
    y = []
    for a in tag_y_positions:
        for b in a:
            y.append(b)

    note = []
    for a in note_molecules:
        for b in a:
            note.append(b)
    
    ########
    # saved txt file
    
    ind = np.lexsort((y,x))
    data = [[tag[i],x[i],y[i],note[i]] for i in ind]

    np.savetxt('tag_transitions_%s.txt' %(spw), data, delimiter='   ',fmt='%s')
else:
    pass
以下是对提供的参考资料的总结,按照要求结构化多个要点分条输出: 4G/5G无线网络优化与网规案例分析: NSA站点下终端掉4G问题:部分用户反馈NSA终端频繁掉4G,主要因终端主动发起SCGfail导致。分析显示,在信号较好的环境下,终端可能因节能、过热保护等原因主动释放连接。解决方案建议终端侧进行分析处理,尝试关闭节电开关等。 RSSI算法识别天馈遮挡:通过计算RSSI平均值及差值识别天馈遮挡,差值大于3dB则认定有遮挡。不同设备分组规则不同,如64T和32T。此方法可有效帮助现场人员识别因环境变化引起的网络问题。 5G 160M组网小区CA不生效:某5G站点开启100M+60M CA功能后,测试发现UE无法正常使用CA功能。问题原因在于CA频点集标识配置错误,修正后测试正常。 5G网络优化与策略: CCE映射方式优化:针对诺基亚站点覆盖农村区域,通过优化CCE资源映射方式(交织、非交织),提升RRC连接建立成功率和无线接通率。非交织方式相比交织方式有显著提升。 5G AAU两扇区组网:与三扇区组网相比,AAU两扇区组网在RSRP、SINR、下载速率和上传速率上表现不同,需根据具体场景选择适合的组网方式。 5G语音解决方案:包括沿用4G语音解决方案、EPS Fallback方案和VoNR方案。不同方案适用于不同的5G组网策略,如NSA和SA,并影响语音连续性和网络覆盖。 4G网络优化与资源利用: 4G室分设备利旧:面对4G网络投资压减与资源需求矛盾,提出利旧多维度调优策略,包括资源整合、统筹调配既有资源,以满足新增需求和提质增效。 宏站RRU设备1托N射灯:针对5G深度覆盖需求,研究使用宏站AAU结合1托N射灯方案,快速便捷地开通5G站点,提升深度覆盖能力。 基站与流程管理: 爱立信LTE基站邻区添加流程:未提供具体内容,但通常涉及邻区规划、参数配置、测试验证等步骤,以确保基站间顺畅切换和覆盖连续性。 网络规划与策略: 新高铁跨海大桥覆盖方案试点:虽未提供详细内容,但可推测涉及高铁跨海大桥区域的4G/5G网络覆盖规划,需考虑信号穿透、移动性管理、网络容量等因素。 总结: 提供的参考资料涵盖了4G/5G无线网络优化、网规案例分析、网络优化策略、资源利用、基站管理等多个方面。 通过具体案例分析,展示了无线网络优化中的常见问题及解决方案,如NSA终端掉4G、RSSI识别天馈遮挡、CA不生效等。 强调了5G网络优化与策略的重要性,包括CCE映射方式优化、5G语音解决方案、AAU扇区组网选择等。 提出了4G网络优化与资源利用的策略,如室分设备利旧、宏站RRU设备1托N射灯等。 基站与流程管理方面,提到了爱立信LTE基站邻区添加流程,但未给出具体细节。 新高铁跨海大桥覆盖方案试点展示了特殊场景下的网络规划需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值