用来处理gwdac网站爬取的实验数据的python代码

写了几个用来整理gwdac爬取的散射实验数据的python代码,在这里保存一下,免得以后找不到

代码一
把按组分类的实验数据转成一行一个数据点

import os

obslist = (["DSG","P","D","DT","AYY","AXX","AZZ","AZX","CKP","CKK",
 "CPP","R","RP","A","AP","RT","RPT","AT","APT","MSSN",
 "MKSN","MSKN","MKKN","SGTE","ALFA","REF3","REF2","SGT","SGTL","SGTT",
 "DTRT","AMPL","SGEL","SGTR","DELT","MTXR","D0SS","D0KS","D0SK","D0KK",
 "NKNK","NSNK","NKNS","NSNS","NNKK","MSNS","MKNS","MSNK","MKNK","NSSN",
 "NKSN","NSKN","NKKN","NNSK","NNKS","BAMP","S00S","K00S","S00K","K00K",
 "A0ST","A0KT","KS0T","KK0T","MSNT","MNST","MNKT","MKNT","DSGL"])

txt = ".txt"
dat = ".dat"

retype = "NP_"
errtype = "S"

for obslist in obslist:

    file = retype + obslist + txt
    print (file)

    if os.path.isfile(retype+obslist+txt):

        print (retype+obslist+txt," exist")

        fr = open(retype+obslist+txt,"r",encoding="utf-8")
        

        fread = fr.readline()
        fread = fr.readline()
        fread = fr.readline()
        fread = fr.readline()
        string = fr.read(5)

        if string != " Summ": # "Summ" means this obs is empty

            print(file,"created")
                

            fw = open("out/"+retype+obslist+dat,"w",encoding="utf-8")

            while True:

                dele= " T "
                num = int(string)

                #print (num)
                

                energy = fr.read(14)
                energy = fr.read(8)


                fread = fr.readline()
                ref = fr.read(8)
                ref = ref[0:4]+ref[5:7]
                fread = fr.read(1)
                ref2 = fr.read(41)

                fread = fr.readline()
                fread = fr.read(8)
                if fread == " DELETED":
                    dele= " F "
                    fread = fr.readline()
                    fread = fr.read(8)
                if fread == " N(theo)":
                    fread = fr.read(16)
                    syserr = fr.read(7)
                    if float(syserr)>0.99:
                        errtype = "F"
                    else:
                        errtype = "S"
                    fread = fr.readline()
                    fread = fr.readline()
                elif fread =="     A  ":
                    errtype = "N"
                    fread = fr.readline()
                    syserr = "  0.000"
                else:
                    print("wrong")
               
                        

                #while fread!= "     A":
                    #fread = fr.readline()
                    #fread = fr.read(6)
                #fread = fr.readline()

            
                #print(num)
                index = 1
                while index < num+1:
                    fw.write(energy)   
                    acm = fr.read(10)
                    fw.write(acm)
                    fread = fr.read(13)
                    obs = fr.read(13)                
                    fw.write(obs)
                    err = fr.read(13)
                    fw.write(err)
                    fw.write(ref)
                    fw.write(" "+errtype)
                    fw.write(syserr)
                    fw.write(dele)
                    fw.write(string)
                    fw.write(ref2)
                    fw.write("\n")
                    
                    fread = fr.readline()
                    index = index+1
                    #print("acm"+acm)
                fread = fr.readline()
                string = fr.read(5)
                #print(string)
                if string == " Summ":
                    fr.close()
                    fw.close()
                    break

代码二
把所有数据合成单个文件

import os

obslists = (["DSG","P","D","DT","AYY","AXX","AZZ","AZX","CKP","CKK",
 "CPP","R","RP","A","AP","RT","RPT","AT","APT","MSSN",
 "MKSN","MSKN","MKKN","SGTE",#"ALFA","REF3","REF2",
 "SGT","SGTL","SGTT",
 #"DTRT","AMPL","SGEL","SGTR","DELT","MTXR",
 "D0SS","D0KS","D0SK","D0KK", 
 "NKNK","NSNK","NKNS","NSNS","NNKK","MSNS","MKNS","MSNK","MKNK","NSSN",
 "NKSN","NSKN","NKKN","NNSK","NNKS","BAMP","S00S","K00S","S00K","K00K",
 "A0ST","A0KT","KS0T","KK0T","MSNT","MNST","MNKT","MKNT","DSGL"])



txt = ".txt"
dat = ".dat"

retype = "NP_"

#fw = open("out/alldata.dat","w",encoding="utf-8")
fw = open("out/300mev.dat","w",encoding="utf-8")

for obslist in obslists:



    file = retype + obslist + dat
    print (file)

    if os.path.isfile(retype+obslist+dat):

                

        print (retype+obslist+dat," exist")

        fr = open(retype+obslist+dat,"r",encoding="utf-8")

        for line in fr.readlines():
            if float(line[0:7])<300.001:
                fw.write(obslist.ljust(5)+line)
            #fw.write(obslist.ljust(5)+line)
        fr.close()


fw.close()

你可能感兴趣的:(Python)