Beispiel #1
0
def main():
    """
    NAME
        core_depthplot.py

    DESCRIPTION
        plots various measurements versus core_depth or age.  plots data flagged as 'FS-SS-C' as discrete samples.

    SYNTAX
        core_depthplot.py [command line optins]

    OPTIONS
        -h prints help message and quits
        -f FILE: specify input magic_measurments format file from magi
        -fsum FILE: specify input LIMS database (IODP) core summary csv file
        -fwig FILE: specify input depth,wiggle to plot, in magic format with sample_core_depth key for depth
        -fsa FILE: specify input er_samples format file from magic for depth
        -fa FILE: specify input er_ages format file from magic for age
              NB: must have either -fsa OR -fa (not both)
        -fsp FILE sym size: specify input zeq_specimen format file from magic, sym and size
              NB: PCAs will have specified color, while fisher means will be white with specified color as the edgecolor
        -fres FILE specify input pmag_results file from magic, sym and size
        -LP [AF,T,ARM,IRM, X] step [in mT,C,mT,mT, mass/vol] to plot
        -S do not plot blanket treatment data (if this is set, you don't need the -LP)
        -sym SYM SIZE, symbol, size for continuous points (e.g., ro 5, bs 10, g^ 10 for red dot, blue square, green triangle), default is blue dot at 5 pt
        -D do not plot declination
        -M do not plot magnetization
        -log  plot magnetization  on a log scale
        -L do not connect dots with a line
        -I do not plot inclination
        -d min max [in m] depth range to plot
        -n normalize by weight in er_specimen table
        -Iex: plot the expected inc at lat - only available for results with lat info in file
        -ts TS amin amax: plot the GPTS for the time interval between amin and amax (numbers in Ma)
           TS: [ck95, gts04, gts12]
        -ds [mbsf,mcd] specify depth scale, mbsf default
        -fmt [svg, eps, pdf, png] specify output format for plot (default: svg)
        -sav save plot silently

     DEFAULTS:
         Measurements file: magic_measurements.txt
         Samples file: er_samples.txt
         NRM step
         Summary file: none
    """
    meas_file='magic_measurements.txt'
    intlist=['measurement_magnitude','measurement_magn_moment','measurement_magn_volume','measurement_magn_mass']
    samp_file='er_samples.txt'
    depth_scale='sample_core_depth'
    wt_file=''
    verbose=pmagplotlib.verbose
    width=10
    sym,size='bo',5
    Ssym,Ssize='cs',5
    method,fmt="LT-NO",'.svg'
    step=0
    pcol=3
    pel=3
    pltD,pltI,pltM,pltL,pltS=1,1,1,1,1
    logit=0
    maxInt=-1000
    minInt=1e10
    maxSuc=-1000
    minSuc=10000
    plotexp,pTS=0,0
    dir_path="."
    sum_file=""
    suc_file=""
    age_file=""
    spc_file=""
    res_file=""
    ngr_file=""
    wig_file=""
    title,location="",""
    if '-WD' in sys.argv:
        ind=sys.argv.index('-WD')
        dir_path=sys.argv[ind+1]
    norm=0
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-L' in sys.argv:
        pltL=0
    if '-S' in sys.argv: pltS=0 # don't plot the bulk measurements at all
    if '-D' in sys.argv:
        pltD=0
        pcol-=1
        pel-=1
        width-=2
    if '-I' in sys.argv:
        pltI=0
        pcol-=1
        pel-=1
        width-=2
    if '-M' in sys.argv:
        pltM=0
        pcol-=1
        pel-=1
        width-=2
    if '-log' in sys.argv:logit=1
    if '-ds' in sys.argv and 'mcd' in sys.argv:depth_scale='sample_composite_depth'
    if '-sym' in sys.argv:
        ind=sys.argv.index('-sym')
        sym=sys.argv[ind+1]
        size=float(sys.argv[ind+2])
    if '-f' in sys.argv:
        ind=sys.argv.index('-f')
        meas_file=sys.argv[ind+1]
    if '-fsa' in sys.argv:
        ind=sys.argv.index('-fsa')
        samp_file=sys.argv[ind+1]
        if '-fa' in sys.argv:
            print(main.__doc__)
            print('only -fsa OR -fa - not both')
            sys.exit()
    elif '-fa' in sys.argv:
        ind=sys.argv.index('-fa')
        age_file=sys.argv[ind+1]
    if '-fsp' in sys.argv:
        ind=sys.argv.index('-fsp')
        spc_file=dir_path+'/'+sys.argv[ind+1]
        spc_sym=sys.argv[ind+2]
        spc_size=float(sys.argv[ind+3])
    if '-fres' in sys.argv:
        ind=sys.argv.index('-fres')
        res_file=dir_path+'/'+sys.argv[ind+1]
        res_sym=sys.argv[ind+2]
        res_size=float(sys.argv[ind+3])
    if '-fwig' in sys.argv:
        ind=sys.argv.index('-fwig')
        wig_file=dir_path+'/'+sys.argv[ind+1]
        pcol+=1
        width+=2
    if '-fsum' in sys.argv:
        ind=sys.argv.index('-fsum')
        sum_file=dir_path+'/'+sys.argv[ind+1]
    if '-fmt' in sys.argv:
        ind=sys.argv.index('-fmt')
        fmt='.'+sys.argv[ind+1]
    if '-sav' in sys.argv:
        plots=1
        verbose=0
    if '-LP' in sys.argv:
        ind=sys.argv.index('-LP')
        meth=sys.argv[ind+1]
        if meth=="AF":
            step=round(float(sys.argv[ind+2])*1e-3,6)
            method='LT-AF-Z'
        elif meth== 'T':
            step=round(float(sys.argv[ind+2])+273,6)
            method='LT-T-Z'
        elif meth== 'ARM':
            method='LT-AF-I'
            step=round(float(sys.argv[ind+2])*1e-3,6)
        elif meth== 'IRM':
            method='LT-IRM'
            step=round(float(sys.argv[ind+2])*1e-3,6)
        elif meth== 'X':
            method='LP-X'
            pcol+=1
            if sys.argv[ind+2]=='mass':
                suc_key='measurement_chi_mass'
            elif sys.argv[ind+2]=='vol':
                suc_key='measurement_chi_volume'
            else:
                print('error in susceptibility units')
                sys.exit()
        else:
           print('method not supported')
           sys.exit()
    if '-n' in sys.argv:
        ind=sys.argv.index('-n')
        wt_file=dir_path+'/'+sys.argv[ind+1]
        norm=1
    dmin,dmax=-1,-1
    if '-d' in sys.argv:
        ind=sys.argv.index('-d')
        dmin=float(sys.argv[ind+1])
        dmax=float(sys.argv[ind+2])
    if '-ts' in sys.argv:
        ind=sys.argv.index('-ts')
        ts=sys.argv[ind+1]
        amin=float(sys.argv[ind+2])
        amax=float(sys.argv[ind+3])
        pTS=1
        pcol+=1
        width+=2
    #
    #
    # get data read in
    meas_file=dir_path+'/'+meas_file
    if age_file=="":
        samp_file=dir_path+'/'+samp_file
        Samps,file_type=pmag.magic_read(samp_file)
    else:
        depth_scale='age'
        age_file=dir_path+'/'+age_file
        Samps,file_type=pmag.magic_read(age_file)
        age_unit=""
    if spc_file!="":Specs,file_type=pmag.magic_read(spc_file)
    if res_file!="":Results,file_type=pmag.magic_read(res_file)
    if norm==1:
        ErSpecs,file_type=pmag.magic_read(wt_file)
        print(len(ErSpecs), ' specimens read in from ',wt_file)
    Cores=[]
    core_depth_key="Top depth cored CSF (m)"
    if sum_file!="":
        input=open(sum_file,'r').readlines()
        if "Core Summary" in input[0]:
            headline=1
        else:
            headline=0
        keys=input[headline].replace('\n','').split(',')
        if "Core Top (m)" in keys:core_depth_key="Core Top (m)"
        if "Core Label" in keys:core_label_key="Core Label"
        if "Core label" in keys:core_label_key="Core label"
        for line in input[2:]:
            if 'TOTALS' not in line:
                CoreRec={}
                for k in range(len(keys)):CoreRec[keys[k]]=line.split(',')[k]
                Cores.append(CoreRec)
        if len(Cores)==0:
            print('no Core depth information available: import core summary file')
            sum_file=""
    Data=[]
    if depth_scale=='sample_core_depth':
        ylab="Depth (mbsf)"
    elif depth_scale=='age':
        ylab="Age"
    else:
        ylab="Depth (mcd)"
    # collect the data for plotting declination
    Depths,Decs,Incs,Ints=[],[],[],[]
    SDepths,SDecs,SIncs,SInts=[],[],[],[]
    SSucs=[]
    samples=[]
    methods,steps,m2=[],[],[]
    if pltS: # plot the bulk measurement data
        Meas,file_type=pmag.magic_read(meas_file)
        meas_key='measurement_magn_moment'
        print(len(Meas), ' measurements read in from ',meas_file)
        for m in intlist: # find the intensity key with data
            meas_data=pmag.get_dictitem(Meas,m,'','F') # get all non-blank data for this specimen
            if len(meas_data)>0:
                meas_key=m
                break
        m1=pmag.get_dictitem(Meas,'magic_method_codes',method,'has') # fish out the desired method code
        if method=='LT-T-Z':
            m2=pmag.get_dictitem(m1,'treatment_temp',str(step),'eval') # fish out the desired step
        elif 'LT-AF' in method:
            m2=pmag.get_dictitem(m1,'treatment_ac_field',str(step),'eval')
        elif 'LT-IRM' in method:
            m2=pmag.get_dictitem(m1,'treatment_dc_field',str(step),'eval')
        elif 'LT-X' in method:
            m2=pmag.get_dictitem(m1,suc_key,'','F')
        if len(m2)>0:
          for rec in m2: # fish out depths and weights
            D=pmag.get_dictitem(Samps,'er_sample_name',rec['er_sample_name'],'T')
            if not D:  # if using an age_file, you may need to sort by site
                D=pmag.get_dictitem(Samps,'er_site_name',rec['er_site_name'],'T')
            depth=pmag.get_dictitem(D,depth_scale,'','F')
            if len(depth)>0:
                if ylab=='Age': ylab=ylab+' ('+depth[0]['age_unit']+')' # get units of ages - assume they are all the same!

                rec['core_depth'] = float(depth[0][depth_scale])
                rec['magic_method_codes'] = rec['magic_method_codes']+':'+depth[0]['magic_method_codes']
                if norm==1:
                    specrecs=pmag.get_dictitem(ErSpecs,'er_specimen_name',rec['er_specimen_name'],'T')
                    specwts=pmag.get_dictitem(specrecs,'specimen_weight',"",'F')
                    if len(specwts)>0:
                        rec['specimen_weight'] = specwts[0]['specimen_weight']
                        Data.append(rec) # fish out data with core_depth and (if needed) weights
                else:
                    Data.append(rec) # fish out data with core_depth and (if needed) weights
                if title=="":
                   pieces=rec['er_sample_name'].split('-')
                   location=rec['er_location_name']
                   title=location

        SData=pmag.sort_diclist(Data,'core_depth')
        for rec in SData: # fish out bulk measurement data from desired depths
            if dmax==-1 or float(rec['core_depth'])<dmax and float(rec['core_depth'])>dmin:
                Depths.append((rec['core_depth']))
                if method=="LP-X":
                    SSucs.append(float(rec[suc_key]))
                else:
                   if pltD==1:Decs.append(float(rec['measurement_dec']))
                   if pltI==1:Incs.append(float(rec['measurement_inc']))
                   if norm==0 and pltM==1:Ints.append(float(rec[meas_key]))
                   if norm==1 and pltM==1:Ints.append(old_div(float(rec[meas_key]),float(rec['specimen_weight'])))
            if len(SSucs)>0:
                maxSuc=max(SSucs)
                minSuc=min(SSucs)
            if len(Ints)>1:
                maxInt=max(Ints)
                minInt=min(Ints)
        if len(Depths)==0:
            print('no bulk measurement data matched your request')
    SpecDepths,SpecDecs,SpecIncs=[],[],[]
    FDepths,FDecs,FIncs=[],[],[]
    if spc_file!="": # add depths to spec data
        print('spec file found')
        BFLs=pmag.get_dictitem(Specs,'magic_method_codes','DE-BFL','has')  # get all the discrete data with best fit lines
        for spec in BFLs:
            if location=="":
               location=spec['er_location_name']
            samp=pmag.get_dictitem(Samps,'er_sample_name',spec['er_sample_name'],'T')
            if len(samp)>0 and depth_scale in list(samp[0].keys()) and samp[0][depth_scale]!="":
              if ylab=='Age': ylab=ylab+' ('+samp[0]['age_unit']+')' # get units of ages - assume they are all the same!
              if dmax==-1 or float(samp[0][depth_scale])<dmax and float(samp[0][depth_scale])>dmin: # filter for depth
                SpecDepths.append(float(samp[0][depth_scale])) # fish out data with core_depth
                SpecDecs.append(float(spec['specimen_dec'])) # fish out data with core_depth
                SpecIncs.append(float(spec['specimen_inc'])) # fish out data with core_depth
            else:
                print('no core_depth found for: ',spec['er_specimen_name'])
        FMs=pmag.get_dictitem(Specs,'magic_method_codes','DE-FM','has')  # get all the discrete data with best fit lines
        for spec in FMs:
            if location=="":
               location=spec['er_location_name']
            samp=pmag.get_dictitem(Samps,'er_sample_name',spec['er_sample_name'],'T')
        if len(samp)>0 and depth_scale in list(samp[0].keys()) and samp[0][depth_scale]!="":
              if ylab=='Age': ylab=ylab+' ('+samp[0]['age_unit']+')' # get units of ages - assume they are all the same!
              if dmax==-1 or float(samp[0][depth_scale])<dmax and float(samp[0][depth_scale])>dmin: # filter for depth
                FDepths.append(float(samp[0][depth_scale]))# fish out data with core_depth
                FDecs.append(float(spec['specimen_dec'])) # fish out data with core_depth
                FIncs.append(float(spec['specimen_inc'])) # fish out data with core_depth
            else:
                print('no core_depth found for: ',spec['er_specimen_name'])
    ResDepths,ResDecs,ResIncs=[],[],[]
    if 'age' in depth_scale: # set y-key
        res_scale='average_age'
    else:
        res_scale='average_height'
    if res_file!="": #creates lists of Result Data
        for res in Results:
            meths=res['magic_method_codes'].split(":")
            if 'DE-FM' in meths:
              if dmax==-1 or float(res[res_scale])<dmax and float(res[res_scale])>dmin: # filter for depth
                ResDepths.append(float(res[res_scale])) # fish out data with core_depth
                ResDecs.append(float(res['average_dec'])) # fish out data with core_depth
                ResIncs.append(float(res['average_inc'])) # fish out data with core_depth
                Susc,Sus_depths=[],[]
    if dmin==-1:
        if len(Depths)>0: dmin,dmax=Depths[0],Depths[-1]
        if len(FDepths)>0: dmin,dmax=Depths[0],Depths[-1]
        if pltS==1 and len(SDepths)>0:
            if SDepths[0]<dmin:dmin=SDepths[0]
            if SDepths[-1]>dmax:dmax=SDepths[-1]
        if len(SpecDepths)>0:
            if min(SpecDepths)<dmin:dmin=min(SpecDepths)
            if max(SpecDepths)>dmax:dmax=max(SpecDepths)
        if len(ResDepths)>0:
            if min(ResDepths)<dmin:dmin=min(ResDepths)
            if max(ResDepths)>dmax:dmax=max(ResDepths)
    if suc_file!="":
        sucdat=open(suc_file,'r').readlines()
        keys=sucdat[0].replace('\n','').split(',') # splits on underscores
        for line in sucdat[1:]:
            SucRec={}
            for k in range(len(keys)):SucRec[keys[k]]=line.split(',')[k]
            if float(SucRec['Top Depth (m)'])<dmax and float(SucRec['Top Depth (m)'])>dmin and SucRec['Magnetic Susceptibility (80 mm)']!="":
                Susc.append(float(SucRec['Magnetic Susceptibility (80 mm)']))
                if Susc[-1]>maxSuc:maxSuc=Susc[-1]
                if Susc[-1]<minSuc:minSuc=Susc[-1]
                Sus_depths.append(float(SucRec['Top Depth (m)']))
    WIG,WIG_depths=[],[]
    if wig_file!="":
        wigdat,file_type=pmag.magic_read(wig_file)
        swigdat=pmag.sort_diclist(wigdat,depth_scale)
        keys=list(wigdat[0].keys())
        for key in keys:
            if key!=depth_scale:
                plt_key=key
                break
        for wig in swigdat:
            if float(wig[depth_scale])<dmax and float(wig[depth_scale])>dmin:
                WIG.append(float(wig[plt_key]))
                WIG_depths.append(float(wig[depth_scale]))
    tint=4.5
    plt=1
    if len(Decs)>0 and len(Depths)>0 or (len(SpecDecs)>0 and len(SpecDepths)>0) or (len(ResDecs)>0 and len(ResDepths)>0) or (len(SDecs)>0 and len(SDepths)>0) or (len(SInts)>0 and len(SDepths)>0) or (len(SIncs)>0 and len(SDepths)>0):
        pylab.figure(1,figsize=(width,8))
        version_num=pmag.get_version()
        pylab.figtext(.02,.01,version_num)
        if pltD==1:
            ax=pylab.subplot(1,pcol,plt)
            if pltL==1:
                pylab.plot(Decs,Depths,'k')
            if len(Decs)>0:
                pylab.plot(Decs,Depths,sym,markersize=size)
            if len(Decs)==0 and pltL==1 and len(SDecs)>0:
                pylab.plot(SDecs,SDepths,'k')
            if len(SDecs)>0:
                pylab.plot(SDecs,SDepths,Ssym,markersize=Ssize)
            if spc_file!="":
                pylab.plot(SpecDecs,SpecDepths,spc_sym,markersize=spc_size)
            if spc_file!="" and len(FDepths)>0:
                pylab.scatter(FDecs,FDepths,marker=spc_sym[-1],edgecolor=spc_sym[0],facecolor='white',s=spc_size**2)
            if res_file!="":
                pylab.plot(ResDecs,ResDepths,res_sym,markersize=res_size)
            if sum_file!="":
                for core in Cores:
                     depth=float(core[core_depth_key])
                     if depth>dmin and depth<dmax:
                        pylab.plot([0,360.],[depth,depth],'b--')
                        if pel==plt:pylab.text(360,depth+tint,core[core_label_key])
            if pel==plt:
                pylab.axis([0,400,dmax,dmin])
            else:
                pylab.axis([0,360.,dmax,dmin])
            pylab.xlabel('Declination')
            pylab.ylabel(ylab)
            plt+=1
            pmagplotlib.delticks(ax) # dec xticks are too crowded otherwise
    if pltI==1:
            pylab.subplot(1,pcol,plt)
            if pltL==1:pylab.plot(Incs,Depths,'k')
            if len(Incs)>0:pylab.plot(Incs,Depths,sym,markersize=size)
            if len(Incs)==0 and pltL==1 and len(SIncs)>0:pylab.plot(SIncs,SDepths,'k')
            if len(SIncs)>0:pylab.plot(SIncs,SDepths,Ssym,markersize=Ssize)
            if spc_file!="" and len(SpecDepths)>0:pylab.plot(SpecIncs,SpecDepths,spc_sym,markersize=spc_size)
            if spc_file!="" and len(FDepths)>0:
                pylab.scatter(FIncs,FDepths,marker=spc_sym[-1],edgecolor=spc_sym[0],facecolor='white',s=spc_size**2)
            if res_file!="":pylab.plot(ResIncs,ResDepths,res_sym,markersize=res_size)
            if sum_file!="":
                for core in Cores:
                     depth=float(core[core_depth_key])
                     if depth>dmin and depth<dmax:
                         if pel==plt:pylab.text(90,depth+tint,core[core_label_key])
                         pylab.plot([-90,90],[depth,depth],'b--')
            pylab.plot([0,0],[dmax,dmin],'k-')
            if pel==plt:
                pylab.axis([-90,110,dmax,dmin])
            else:
                pylab.axis([-90,90,dmax,dmin])
            pylab.xlabel('Inclination')
            pylab.ylabel('')
            plt+=1
    if pltM==1 and len(Ints)>0 or len(SInts)>0:
            pylab.subplot(1,pcol,plt)
            for pow in range(-10,10):
                if maxInt*10**pow>1:break
            if logit==0:
                for k in range(len(Ints)):
                    Ints[k]=Ints[k]*10**pow
                for k in range(len(SInts)):
                    SInts[k]=SInts[k]*10**pow
                if pltL==1 and len(Ints)>0: pylab.plot(Ints,Depths,'k')
                if len(Ints)>0:pylab.plot(Ints,Depths,sym,markersize=size)
                if len(Ints)==0 and pltL==1 and len(SInts)>0:pylab.plot(SInts,SDepths,'k-')
                if len(SInts)>0:pylab.plot(SInts,SDepths,Ssym,markersize=Ssize)
                if sum_file!="":
                    for core in Cores:
                         depth=float(core[core_depth_key])
                         pylab.plot([0,maxInt*10**pow+.1],[depth,depth],'b--')
                         if depth>dmin and depth<dmax:pylab.text(maxInt*10**pow-.2*maxInt*10**pow,depth+tint,core[core_label_key])
                pylab.axis([0,maxInt*10**pow+.1,dmax,dmin])
                if norm==0:
                    pylab.xlabel('%s %i %s'%('Intensity (10^-',pow,' Am^2)'))
                else:
                    pylab.xlabel('%s %i %s'%('Intensity (10^-',pow,' Am^2/kg)'))
            else:
                if pltL==1: pylab.semilogx(Ints,Depths,'k')
                if len(Ints)>0:pylab.semilogx(Ints,Depths,sym,markersize=size)
                if len(Ints)==0 and pltL==1 and len(SInts)>0:pylab.semilogx(SInts,SDepths,'k')
                if len(Ints)==0 and pltL==1 and len(SInts)>0:pylab.semilogx(SInts,SDepths,'k')
                if len(SInts)>0:pylab.semilogx(SInts,SDepths,Ssym,markersize=Ssize)
                if sum_file!="":
                    for core in Cores:
                         depth=float(core[core_depth_key])
                         pylab.semilogx([minInt,maxInt],[depth,depth],'b--')
                         if depth>dmin and depth<dmax:pylab.text(maxInt-.2*maxInt,depth+tint,core[core_label_key])
                pylab.axis([0,maxInt,dmax,dmin])
                if norm==0:
                    pylab.xlabel('Intensity (Am^2)')
                else:
                    pylab.xlabel('Intensity (Am^2/kg)')
            plt+=1
    if suc_file!="" or len(SSucs)>0:
            pylab.subplot(1,pcol,plt)
            if len(Susc)>0:
                if pltL==1:pylab.plot(Susc,Sus_depths,'k')
                if logit==0:pylab.plot(Susc,Sus_depths,sym,markersize=size)
                if logit==1:pylab.semilogx(Susc,Sus_depths,sym,markersize=size)
            if len(SSucs)>0:
                if logit==0:pylab.plot(SSucs,SDepths,sym,markersize=size)
                if logit==1:pylab.semilogx(SSucs,SDepths,sym,markersize=size)
            if sum_file!="":
                for core in Cores:
                     depth=float(core[core_depth_key])
                     if logit==0:pylab.plot([minSuc,maxSuc],[depth,depth],'b--')
                     if logit==1:pylab.semilogx([minSuc,maxSuc],[depth,depth],'b--')
            pylab.axis([minSuc,maxSuc,dmax,dmin])
            pylab.xlabel('Susceptibility')
            plt+=1
    if wig_file!="":
            pylab.subplot(1,pcol,plt)
            pylab.plot(WIG,WIG_depths,'k')
            if sum_file!="":
                for core in Cores:
                     depth=float(core[core_depth_key])
                     pylab.plot([WIG[0],WIG[-1]],[depth,depth],'b--')
            pylab.axis([min(WIG),max(WIG),dmax,dmin])
            pylab.xlabel(plt_key)
            plt+=1
    if pTS==1:
            ax1=pylab.subplot(1,pcol,plt)
            ax1.axis([-.25,1.5,amax,amin])
            plt+=1
            TS,Chrons=pmag.get_ts(ts)
            X,Y,Y2=[0,1],[],[]
            cnt=0
            if amin<TS[1]: # in the Brunhes
                Y=[amin,amin] # minimum age
                Y1=[TS[1],TS[1]] # age of the B/M boundary
                ax1.fill_between(X,Y,Y1,facecolor='black') # color in Brunhes, black
            for d in TS[1:]:
                pol=cnt%2
                cnt+=1
                if d<=amax and d>=amin:
                   ind=TS.index(d)
                   Y=[TS[ind],TS[ind]]
                   Y1=[TS[ind+1],TS[ind+1]]
                   if pol: ax1.fill_between(X,Y,Y1,facecolor='black') # fill in every other time
            ax1.plot([0,1,1,0,0],[amin,amin,amax,amax,amin],'k-')
            ax2=ax1.twinx()
            pylab.ylabel("Age (Ma): "+ts)
            for k in range(len(Chrons)-1):
                c=Chrons[k]
                cnext=Chrons[k+1]
                d=cnext[1]-old_div((cnext[1]-c[1]),3.)
                if d>=amin and d<amax:
                    ax2.plot([1,1.5],[c[1],c[1]],'k-') # make the Chron boundary tick
                    ax2.text(1.05,d,c[0]) #
            ax2.axis([-.25,1.5,amax,amin])
    figname=location+'_m:_'+method+'_core-depthplot'+fmt
    pylab.title(location)
    if verbose:
        pylab.draw()
        ans=input("S[a]ve plot? ")
        if ans=='a':
            pylab.savefig(figname)
            print('Plot saved as ',figname)
    elif plots:
        pylab.savefig(figname)
        print('Plot saved as ',figname)
    sys.exit()
Beispiel #2
0
def main():
    """
    NAME
        ODP_dcs_magic.py
 
    DESCRIPTION
        converts ODP discrete sample format files to magic_measurements format files

    SYNTAX
        ODP_dsc_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -F FILE: specify output  measurements file, default is magic_measurements.txt
        -Fsp FILE: specify output er_specimens.txt file, default is er_specimens.txt
        -Fsa FILE: specify output er_samples.txt file for appending, default is er_samples.txt
        -Fsi FILE: specify output er_sites.txt file, default is er_sites.txt
        -dc B PHI THETA: dc lab field (in micro tesla) and phi,theta, default is none
              NB: use PHI, THETA = -1 -1 to signal that it changes, i.e. in anisotropy experiment
        -ac B : peak AF field (in mT) for ARM acquisition, default is none
        -A : don't average replicate measurements
    INPUT
        Put data from separate experiments (all AF, thermal, thellier, trm aquisition, Shaw, etc.)  in separate directory

    """
#        
#
    version_num=pmag.get_version()
    meas_file='magic_measurements.txt'
    spec_file='er_specimens.txt'
    samp_file='er_samples.txt'
    site_file='er_sites.txt'
    ErSpecs,ErSamps,ErSites,ErLocs,ErCits=[],[],[],[],[]
    MagRecs=[]
    citation="This study"
    dir_path,demag='.','NRM'
    args=sys.argv
    noave=0
    if '-WD' in args:
        ind=args.index("-WD")
        dir_path=args[ind+1]
    if "-h" in args:
        print main.__doc__
        sys.exit()
    if "-A" in args: noave=1
    if '-F' in args:
        ind=args.index("-F")
        meas_file=args[ind+1]
    if '-Fsp' in args:
        ind=args.index("-Fsp")
        spec_file=args[ind+1]
    if '-Fsa' in args:
        ind=args.index("-Fsa")
        samp_file=dir_path+'/'+args[ind+1]
        ErSamps,file_type=pmag.magic_read(samp_file)
    else:
        samp_file=dir_path+'/'+samp_file
    if '-LP' in args:
        ind=args.index("-LP")
        codelist=args[ind+1]
        codes=codelist.split(':')
        if "AF" in codes:
            demag='AF' 
            if'-dc' not in args: methcode="LT-AF-Z"
            if'-dc' in args: methcode="LT-AF-I"
        if "T" in codes:
            demag="T"
            if '-dc' not in args: methcode="LT-T-Z"
            if '-dc' in args: methcode="LT-T-I"
        if "I" in codes:
            methcode="LP-IRM"
        if "S" in codes: 
            demag="S"
            methcode="LP-PI-TRM:LP-PI-ALT-AFARM"
            trm_labfield=labfield
            ans=raw_input("DC lab field for ARM step: [50uT] ")
            if ans=="":
                arm_labfield=50e-6
            else: 
                arm_labfield=float(ans)*1e-6
            ans=raw_input("temperature for total trm step: [600 C] ")
            if ans=="":
                trm_peakT=600+273 # convert to kelvin
            else: 
                trm_peakT=float(ans)+273 # convert to kelvin
        if "G" in codes: methcode="LT-AF-G"
	if "D" in codes: methcode="LT-AF-D"
        if "TRM" in codes: 
            demag="T"
            trm=1
    if demag=="T" and "ANI" in codes:
        methcode="LP-AN-TRM"
    if demag=="AF" and "ANI" in codes:
        methcode="LP-AN-ARM"
        if labfield==0: labfield=50e-6
        if peakfield==0: peakfield=.180
    spec_file=dir_path+'/'+spec_file
    site_file=dir_path+'/'+site_file
    meas_file=dir_path+'/'+meas_file
    filelist=os.listdir(dir_path) # read in list of files to import
    specimens,samples,sites=[],[],[]
    MagRecs,SpecRecs,SampRecs=[],[],[]
    for samp in ErSamps:
        if samp['er_sample_name'] not in samples:
            samples.append(samp['er_sample_name'])
            SampRecs.append(samp)
    for file in filelist: # parse each file
        if file[-3:].lower()=='dsc':
            print 'processing: ',file
            MagRec,SpecRec,SampRec={},{},{}
            treatment_type,treatment_value,user="","",""
            inst="ODP-SRM"
            input=open(dir_path+'/'+file,'rU').readlines()
            IDs=file.split('_') # splits on underscores
            pieces=IDs[0].split('-')
            expedition=pieces[0]
            location=pieces[1]
            if file[0]!='_':
                while len(pieces[2])<4:pieces[2]='0'+pieces[2] # pad core to be 3 characters
                specimen=""
            else:
                specimen="test"
            for piece in pieces:
                specimen=specimen+piece+'-'
            specimen=specimen[:-1]
            alt_spec=IDs[1] # alternate specimen is second field in field name
# set up specimen record for Er_specimens table
            SpecRec['er_expedition_name']=expedition
            SpecRec['er_location_name']=location
            SpecRec['er_site_name']=specimen
            SpecRec['er_sample_name']=specimen
            SpecRec['er_citation_names']=citation
            for key in SpecRec.keys():SampRec[key]=SpecRec[key]
            SampRec['sample_azimuth']='0'
            SampRec['sample_dip']='0'
            SampRec['magic_method_codes']='FS-C-DRILL-IODP:FS-SS-C:SO-V'
            SpecRec['er_specimen_name']=specimen
            for key in SpecRec.keys():MagRec[key]=SpecRec[key]
# set up measurement record - default is NRM 
            MagRec['er_analyst_mail_names']=user
            MagRec['magic_method_codes']='LT-NO'
            MagRec['magic_software_packages']=version_num
            MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
            MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
            MagRec["treatment_ac_field"]=0.
            MagRec["treatment_dc_field"]='0'
            MagRec["treatment_dc_field_phi"]='0'
            MagRec["treatment_dc_field_theta"]='0'
            MagRec["measurement_flag"]='g' # assume all data are "good"
            MagRec["measurement_standard"]='u' # assume all data are "good"
            MagRec["measurement_csd"]='' # set csd to blank
            SpecRec['er_specimen_alternatives']=alt_spec
            vol=7e-6 # assume 7 cc samples
            datestamp=input[1].split() # date time is second line of file
            mmddyy=datestamp[0].split('/') # break into month day year
            date=mmddyy[2]+':'+mmddyy[0]+":"+mmddyy[1] +':' +datestamp[1]
            MagRec["measurement_date"]=date
            for k in range(len(input)):
                fields= input[k].split("=")
                if 'treatment_type' in fields[0]:
                    if "Alternating Frequency Demagnetization" in fields[1]:
                        MagRec['magic_method_codes'] = 'LT-AF-Z'
                        inst=inst+':ODP-DTECH' # measured on shipboard AF DTECH D2000
                        treatment_type="AF"
                    if "Anhysteretic Remanent Magnetization" in fields[1]:
                        MagRec['magic_method_codes'] = 'LT-AF-I'
                        inst=inst+':ODP-DTECH' # measured on shipboard AF DTECH D2000
                        treatment_type="ARM"
                    if "Isothermal Remanent Magnetization" in fields[1]:
                        MagRec['magic_method_codes'] = 'LT-IRM'
                        inst=inst+':ODP-IMP' # measured on shipboard ASC IMPULSE magnetizer
                        treatment_type="IRM"
                if "treatment_value" in fields[0]:
                    values=fields[1].split(',')
                    value=values[0]
                    if value!=" \n":
                        if treatment_type=="AF":
                            treatment_value=float(value)*1e-3
                            MagRec["treatment_ac_field"]=treatment_value # AF demag in treat mT => T
                        elif treatment_type=="IRM":
                            treatment_value=float(value)*1e-3
                            MagRec["treatment_dc_field"]='%8.3e'%(treatment_value) # IRM treat mT => T
                        if treatment_type=="ARM":
                            treatment_value=float(value)*1e-3
                            dc_value=float(values[1])*1e-3
                            MagRec["treatment_ac_field"]=treatment_value # AF demag in treat mT => T
                            MagRec["treatment_dc_field"]='%8.3e'%(dc_value) # DC mT => T
                if 'user' in fields[0]: 
                    user=fields[-1]
                    MagRec["er_analyst_mail_names"]=user
                if 'sample_orientation' in fields[0]: 
                    MagRec["measurement_description"]=fields[-1]
                MagRec["measurement_standard"]='u' # assume all data are "good"
                if 'sample_area' in fields[0]:  vol=float(fields[1])*1e-6 # takes volume (cc) and converts to m^3
                if 'run_number' in fields[0]:  
                    MagRec['external_database_ids']=fields[1] # run number is the LIMS measurement number
                    MagRec['external_database_names']='LIMS'
                if input[k][0:7]=='<MULTI>':
                    rec=input[k+1].split(',') # list of data
                    for item in rec:
                        items=item.split('=')
                        if items[0].strip()=='demag_level' and  treatment_value=="" :
                            treat= float(items[1])
                            if treat!=0:
                                MagRec['magic_method_codes']='LT-AF-Z'
                                inst=inst+':ODP-SRM-AF'
                                MagRec["treatment_ac_field"]=treat*1e-3 # AF demag in treat mT => T
                        if items[0].strip()=='inclination_w_tray_w_bkgrd': MagRec['measurement_inc']=items[1]
                        if items[0].strip()=='declination_w_tray_w_bkgrd': MagRec['measurement_dec']=items[1]
                        if items[0].strip()=='intensity_w_tray_w_bkgrd': MagRec['measurement_magn_moment']='%8.3e'%(float(items[1])*vol) # convert intensity from A/m to Am^2 using vol
                        if items[0].strip()=='x_stdev':MagRec['measurement_x_sd']=items[1]
                        if items[0].strip()=='y_stdev':MagRec['measurement_y_sd']=items[1]
                        if items[0].strip()=='z_stdev':MagRec['measurement_z_sd']=items[1]
                        MagRec['magic_instrument_codes']=inst
                        MagRec['measurement_number']='1'
            MagRecs.append(MagRec)
            if specimen not in specimens:
                specimens.append(specimen)
                SpecRecs.append(SpecRec)
            if MagRec['er_sample_name'] not in samples:
                samples.append(MagRec['er_sample_name'])
                SampRecs.append(SampRec)
    MagOuts=pmag.sort_diclist(MagRecs,'treatment_ac_field')
    for MagRec in MagOuts:
        MagRec["treatment_ac_field"]='%8.3e'%(MagRec["treatment_ac_field"]) # convert to string
    pmag.magic_write(spec_file,SpecRecs,'er_specimens')
    if len(SampRecs)>0:
        SampOut,keys=pmag.fillkeys(SampRecs)
        pmag.magic_write(samp_file,SampOut,'er_samples')
        print 'samples stored in ',samp_file
    pmag.magic_write(samp_file,SampRecs,'er_samples')
    print 'specimens stored in ',spec_file
    Fixed=pmag.measurements_methods(MagOuts,noave)
    pmag.magic_write(meas_file,Fixed,'magic_measurements')
    print 'data stored in ',meas_file
Beispiel #3
0
def main(command_line=True, **kwargs):
    """
    NAME
        IODP_dscr_magic.py
 
    DESCRIPTION
        converts ODP LIMS discrete sample format files to magic_measurements format files


    SYNTAX
        IODP_descr_magic.py [command line options]

    OPTIONS
        -h: prints the help message and quits.
        -f FILE: specify input .csv file, default is all in directory
        -F FILE: specify output  measurements file, default is magic_measurements.txt
        -A : don't average replicate measurements
    INPUTS
 	 IODP discrete sample .csv file format exported from LIMS database
    """
    #
    # initialize defaults
    version_num = pmag.get_version()
    meas_file = "magic_measurements.txt"
    csv_file = ""
    MagRecs, Specs = [], []
    citation = "This study"
    dir_path, demag = ".", "NRM"
    args = sys.argv
    noave = 0
    # get command line args
    if command_line:
        if "-WD" in args:
            ind = args.index("-WD")
            dir_path = args[ind + 1]
        if "-ID" in args:
            ind = args.index("-ID")
            input_dir_path = args[ind + 1]
        else:
            input_dir_path = dir_path
        output_dir_path = dir_path
        if "-h" in args:
            print main.__doc__
            return False
        if "-A" in args:
            noave = 1
        if "-f" in args:
            ind = args.index("-f")
            csv_file = args[ind + 1]
        if "-F" in args:
            ind = args.index("-F")
            meas_file = args[ind + 1]

    if not command_line:
        dir_path = kwargs.get("dir_path", ".")
        input_dir_path = kwargs.get("input_dir_path", dir_path)
        output_dir_path = dir_path  # rename dir_path after input_dir_path is set
        noave = kwargs.get("noave", 0)  # default (0) is DO average
        csv_file = kwargs.get("csv_file", "")
        meas_file = kwargs.get("meas_file", "magic_measurements.txt")

    # format variables

    meas_file = os.path.join(output_dir_path, meas_file)
    if csv_file == "":
        filelist = os.listdir(input_dir_path)  # read in list of files to import
    else:
        csv_file = os.path.join(input_dir_path, csv_file)
        filelist = [csv_file]
    # parsing the data
    file_found = False
    for file in filelist:  # parse each file
        if file[-3:].lower() == "csv":
            file_found = True
            print "processing: ", file
            input = open(file, "rU").readlines()
            keys = input[0].replace("\n", "").split(",")  # splits on underscores
            interval_key = "Offset (cm)"
            demag_key = "Demag level (mT)"
            offline_demag_key = "Treatment Value (mT or &deg;C)"
            offline_treatment_type = "Treatment type"
            run_key = "Test No."
            if "Inclination background + tray corrected  (deg)" in keys:
                inc_key = "Inclination background + tray corrected  (deg)"
            if "Inclination background &amp; tray corrected (deg)" in keys:
                inc_key = "Inclination background &amp; tray corrected (deg)"
            if "Declination background + tray corrected (deg)" in keys:
                dec_key = "Declination background + tray corrected (deg)"
            if "Declination background &amp; tray corrected (deg)" in keys:
                dec_key = "Declination background &amp; tray corrected (deg)"
            if "Intensity background + tray corrected  (A/m)" in keys:
                int_key = "Intensity background + tray corrected  (A/m)"
            if "Intensity background &amp; tray corrected (A/m)" in keys:
                int_key = "Intensity background &amp; tray corrected (A/m)"
            type = "Type"
            sect_key = "Sect"
            half_key = "A/W"
            # need to add volume_key to LORE format!
            if "Sample volume (cm^3)" in keys:
                volume_key = "Sample volume (cm^3)"
            if "Sample volume (cc)" in keys:
                volume_key = "Sample volume (cc)"
            if "Sample volume (cm&sup3;)" in keys:
                volume_key = "Sample volume (cm&sup3;)"
            for line in input[1:]:
                InRec = {}
                for k in range(len(keys)):
                    InRec[keys[k]] = line.split(",")[k]
                inst = "IODP-SRM"
                MagRec = {}
                expedition = InRec["Exp"]
                location = InRec["Site"] + InRec["Hole"]
                offsets = InRec[interval_key].split(
                    "."
                )  # maintain consistency with er_samples convention of using top interval
                if len(offsets) == 1:
                    offset = int(offsets[0])
                else:
                    offset = int(offsets[0]) - 1
                # interval=str(offset+1)# maintain consistency with er_samples convention of using top interval
                interval = str(offset)  # maintain consistency with er_samples convention of using top interval
                specimen = (
                    expedition
                    + "-"
                    + location
                    + "-"
                    + InRec["Core"]
                    + InRec[type]
                    + "-"
                    + InRec[sect_key]
                    + "_"
                    + InRec[half_key]
                    + "_"
                    + interval
                )
                if specimen not in Specs:
                    Specs.append(specimen)
                MagRec["er_expedition_name"] = expedition
                MagRec["er_location_name"] = location
                MagRec["er_site_name"] = specimen
                MagRec["er_citation_names"] = citation
                MagRec["er_specimen_name"] = specimen
                MagRec["er_sample_name"] = specimen
                MagRec["er_site_name"] = specimen
                # set up measurement record - default is NRM
                MagRec["magic_software_packages"] = version_num
                MagRec["treatment_temp"] = "%8.3e" % (273)  # room temp in kelvin
                MagRec["measurement_temp"] = "%8.3e" % (273)  # room temp in kelvin
                MagRec["treatment_ac_field"] = "0"
                MagRec["treatment_dc_field"] = "0"
                MagRec["treatment_dc_field_phi"] = "0"
                MagRec["treatment_dc_field_theta"] = "0"
                MagRec["measurement_flag"] = "g"  # assume all data are "good"
                MagRec["measurement_standard"] = "u"  # assume all data are "good"
                MagRec["measurement_csd"] = "0"  # assume all data are "good"
                volume = InRec[volume_key]
                MagRec["magic_method_codes"] = "LT-NO"
                sort_by = "treatment_ac_field"  # set default to AF demag
                if InRec[demag_key] != "0":
                    MagRec["magic_method_codes"] = "LT-AF-Z"
                    inst = inst + ":IODP-SRM-AF"  # measured on shipboard in-line 2G AF
                    treatment_value = float(InRec[demag_key].strip('"')) * 1e-3  # convert mT => T
                    if sort_by == "treatment_ac_field":
                        MagRec["treatment_ac_field"] = treatment_value  # AF demag in treat mT => T
                    else:
                        MagRec["treatment_ac_field"] = str(treatment_value)  # AF demag in treat mT => T
                elif offline_treatment_type in InRec.keys() and InRec[offline_treatment_type] != "":
                    if "Lowrie" in InRec["Comments"]:
                        MagRec["magic_method_codes"] = "LP-IRM-3D"
                        treatment_value = float(InRec[offline_demag_key].strip('"')) + 273.0  # convert C => K
                        MagRec["treatment_temp"] = treatment_value
                        MagRec["treatment_ac_field"] = "0"
                        sort_by = "treatment_temp"
                    elif "Isothermal" in InRec[offline_treatment_type]:
                        MagRec["magic_method_codes"] = "LT-IRM"
                        treatment_value = float(InRec[offline_demag_key].strip('"')) * 1e-3  # convert mT => T
                        MagRec["treatment_dc_field"] = treatment_value
                        MagRec["treatment_ac_field"] = "0"
                        sort_by = "treatment_dc_field"
                MagRec["measurement_standard"] = "u"  # assume all data are "good"
                vol = float(volume) * 1e-6  # convert from cc to m^3
                if run_key in InRec.keys():
                    run_number = InRec[run_key]
                    MagRec["external_database_ids"] = run_number
                    MagRec["external_database_names"] = "LIMS"
                else:
                    MagRec["external_database_ids"] = ""
                    MagRec["external_database_names"] = ""
                MagRec["measurement_description"] = "sample orientation: " + InRec["Sample orientation"]
                MagRec["measurement_inc"] = InRec[inc_key].strip('"')
                MagRec["measurement_dec"] = InRec[dec_key].strip('"')
                intens = InRec[int_key].strip('"')
                MagRec["measurement_magn_moment"] = "%8.3e" % (
                    float(intens) * vol
                )  # convert intensity from A/m to Am^2 using vol
                MagRec["magic_instrument_codes"] = inst
                MagRec["measurement_number"] = "1"
                MagRec["measurement_positions"] = ""
                MagRecs.append(MagRec)
    if not file_found:
        print "No .csv files were found"
        return False, "No .csv files were found"
    MagOuts = []
    for spec in Specs:
        Speclist = pmag.get_dictitem(MagRecs, "er_specimen_name", spec, "T")
        sorted = pmag.sort_diclist(Speclist, sort_by)
        for rec in sorted:
            for key in rec.keys():
                rec[key] = str(rec[key])
            MagOuts.append(rec)
    Fixed = pmag.measurements_methods(MagOuts, noave)
    Out, keys = pmag.fillkeys(Fixed)
    if pmag.magic_write(meas_file, Out, "magic_measurements"):
        print "data stored in ", meas_file
        return True, meas_file
    else:
        print "no data found.  bad magfile?"
        return False, "no data found.  bad magfile?"
Beispiel #4
0
def main():
    """
    NAME 
        ODP_depthplot.py

    DESCRIPTION
        plots various measurements versus core_depth

    SYNTAX
        ODP_depthplot.py [command line optins]

    OPTIONS
        -h prints help message and quits
        -f FILE: specify input magic_measurments format file from magic
        -fsum FILE: specify input ODP core summary csv file
        -fwig FILE: specify input depth,wiggle to plot, in magic format with sample_core_depth key for depth
        -fsa FILE: specify input er_samples format file from magic
        -fsp FILE sym size: specify input zeq_specimen format file from magic, sym and size
        -LP [AF,T,ARM,IRM, X] step [in mT,C,mT,mT, mass/vol] to plot 
        -sym SYM SIZE, symbol, size for data points (e.g., ro 5, bs 10, g^ 10 for red dot, blue square, green triangle), default is blue dot at 5 pt
        -D do not plot declination
        -M do not plot magnetization
        -log  plot magnetization  on a log scale
        -L do not connect dots with a line
        -I do not plot inclination
        -S do not plot subsample blanket treatment data
        -d min max [in m] depth range to plot
        -n normalize by weight in er_specimen table
        -Iex: plot the expected inc at lat - only available for results with lat info in file
        -ts TS amin amax: plot the GPTS for the time interval between amin and amax (numbers in Ma)
           TS: [ck95, gts04] 

     DEFAULTS:
         Measurements file: magic_measurements.txt
         Samples file: er_samples.txt
         NRM step
         Summary file: none
    """
    meas_file='magic_measurements.txt'
    samp_file='er_samples.txt'
    wt_file=''
    width=10
    sym,size='bo',5
    Ssym,Ssize='ws',5
    method,fmt="LT-NO",'.pdf'
    step=0
    pcol=3
    pel=3
    pltD,pltI,pltM,pltL,pltS=1,1,1,1,1
    logit=0
    maxInt=-1000
    minInt=1e10
    maxSuc=-1000
    minSuc=10000
    plotexp,pTS=0,0
    dir_path="."
    sum_file=""
    suc_file=""
    spc_file=""
    ngr_file=""
    wig_file=""
    title=""
    if '-WD' in sys.argv: 
        ind=sys.argv.index('-WD')
        dir_path=sys.argv[ind+1]
    norm=0
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    if '-L' in sys.argv:
        pltL=0
    if '-S' in sys.argv:
        pltS=0
    if '-D' in sys.argv:
        pltD=0
        pcol-=1
        pel-=1
        width-=2
    if '-I' in sys.argv:
        pltI=0
        pcol-=1
        pel-=1
        width-=2
    if '-M' in sys.argv:
        pltM=0
        pcol-=1
        pel-=1
        width-=2
    if '-log' in sys.argv:logit=1
    if '-sym' in sys.argv:
        ind=sys.argv.index('-sym')
        sym=sys.argv[ind+1]
        size=float(sys.argv[ind+2])
    if '-f' in sys.argv:
        ind=sys.argv.index('-f')
        meas_file=sys.argv[ind+1]
    if '-fsa' in sys.argv:
        ind=sys.argv.index('-fsa')
        samp_file=sys.argv[ind+1]
    if '-fsp' in sys.argv:
        ind=sys.argv.index('-fsp')
        spc_file=dir_path+'/'+sys.argv[ind+1]
        spc_sym=sys.argv[ind+2]
        spc_size=float(sys.argv[ind+3])
    if '-fwig' in sys.argv:
        ind=sys.argv.index('-fwig')
        wig_file=dir_path+'/'+sys.argv[ind+1]
        pcol+=1
        width+=2
    if '-fsum' in sys.argv:
        ind=sys.argv.index('-fsum')
        sum_file=sys.argv[ind+1]
        sum_file=dir_path+'/'+sys.argv[ind+1]
    if '-fmt' in sys.argv:
        ind=sys.argv.index('-fmt')
        fmt='.'+sys.argv[ind+1]
    if '-LP' in sys.argv:
        ind=sys.argv.index('-LP')
        meth=sys.argv[ind+1]
        if meth=="AF":
            step=float(sys.argv[ind+2])*1e-3
            method='LT-AF-Z'
        elif meth== 'T':
            step=float(sys.argv[ind+2])+273
            method='LT-T-Z'
        elif meth== 'ARM':
            method='LT-AF-I'
            step=float(sys.argv[ind+2])*1e-3
        elif meth== 'IRM':
            method='LT-IRM'
            step=float(sys.argv[ind+2])*1e-3
        elif meth== 'X':
            method='LP-X'
            pcol+=1
            if sys.argv[ind+2]=='mass':
                suc_key='measurement_chi_mass'
            elif sys.argv[ind+2]=='vol':
                suc_key='measurement_chi_volume'
            else:
                print 'error in susceptibility units'
                sys.exit()
        else:
           print 'method not supported'
           sys.exit()
    if '-n' in sys.argv: 
        ind=sys.argv.index('-n')
        wt_file=dir_path+'/'+sys.argv[ind+1]
        norm=1
    dmin,dmax=-1,-1
    if '-d' in sys.argv:
        ind=sys.argv.index('-d')
        dmin=float(sys.argv[ind+1])
        dmax=float(sys.argv[ind+2])
    if '-ts' in sys.argv:
        ind=sys.argv.index('-ts')
        ts=sys.argv[ind+1]
        amin=float(sys.argv[ind+2])
        amax=float(sys.argv[ind+3])
        pTS=1
        pcol+=1
        width+=2
    #
    #
    # get data read in
    meas_file=dir_path+'/'+meas_file
    samp_file=dir_path+'/'+samp_file
    Meas,file_type=pmag.magic_read(meas_file) 
    print len(Meas), ' measurements read in from ',meas_file
    Samps,file_type=pmag.magic_read(samp_file) 
    if spc_file!="":Specs,file_type=pmag.magic_read(spc_file) 
    if norm==1:
        ErSpecs,file_type=pmag.magic_read(wt_file) 
        print len(ErSpecs), ' specimens read in from ',wt_file
    Cores=[] 
    if sum_file!="":
        input=open(sum_file,'rU').readlines()
        keys=input[1].replace('\n','').split(',') # splits on underscores
        for line in input[2:]:
            if 'TOTALS' not in line:
                CoreRec={}
                for k in range(len(keys)):CoreRec[keys[k]]=line.split(',')[k]
                Cores.append(CoreRec)
        if len(Cores)==0:
            print 'no Core depth information available: import core summary file'
            sum_file=""
    Data=[]
    for rec in Meas:
        for samp in Samps:
            if samp['er_sample_name']== rec['er_sample_name'] and 'sample_core_depth' in samp.keys() and samp['sample_core_depth']!="":
                rec['core_depth'] = float(samp['sample_core_depth'])
                rec['magic_method_codes'] = rec['magic_method_codes']+':'+samp['magic_method_codes']
                if norm==1:
                    for spec in ErSpecs:
                        if spec['er_sample_name']== rec['er_sample_name'] and 'specimen_weight' in spec.keys() and spec['specimen_weight']!="":
                            rec['specimen_weight'] = spec['specimen_weight']
                            Data.append(rec) # fish out data with core_depth and (if needed) weights
                else:
                    Data.append(rec) # fish out data with core_depth and (if needed) weights
                if title=="":
                   pieces=samp['er_sample_name'].split('-')
                   title=pieces[0]+'-'+pieces[1]
    SData=pmag.sort_diclist(Data,'core_depth')
    xlab="Depth (mbsf)"
    # collect the data for plotting declination
    Depths,Decs,Incs,Ints=[],[],[],[]
    SDepths,SDecs,SIncs,SInts=[],[],[],[]
    SSucs=[]
    samples=[]
    methods,steps=[],[]
    for rec in SData:
        if "magic_method_codes" in rec.keys():
            meths=rec["magic_method_codes"].split(":")
            if method in meths:
                if step==0 or ('LT-AF-Z' in method and float(rec['treatment_ac_field'])==step) or ('LT-T-Z' in method and float(rec['treatment_temp'])==step) or ('LT-AF-I' in method and float(rec['treatment_ac_field'])==step) or ('LT-IRM' in method and float(rec['treatment_dc_field'])==step) or ('LP-X' in method and rec[suc_key] in rec.keys() and rec[suc_key]!=""):
                    if dmax==-1 or float(rec['core_depth'])<dmax and float(rec['core_depth'])>dmin:
                        if  'FS-SS-C' not in meths: # make sure it is desired lab treatment step 
                            Depths.append((rec['core_depth']))
                            if pltD==1:Decs.append(float(rec['measurement_dec']))
                            if pltI==1:Incs.append(float(rec['measurement_inc']))
                            if norm==0 and pltM==1:Ints.append(float(rec['measurement_magn_moment']))
                            if norm==1 and pltM==1:Ints.append(float(rec['measurement_magn_moment'])/float(rec['specimen_weight']))
                            if len(Ints)>1 and Ints[-1]>maxInt:maxInt=Ints[-1]
                            if len(Ints)>1 and Ints[-1]<minInt:minInt=Ints[-1]
                        elif  pltS==1: # make sure it is desired lab treatment step 
                            SDepths.append((rec['core_depth']))
                            if pltD==1:SDecs.append(float(rec['measurement_dec']))
                            if pltI==1:SIncs.append(float(rec['measurement_inc']))
                            if norm==0 and pltM==1:SInts.append(float(rec['measurement_magn_moment']))
                            if norm==1 and pltM==1:SInts.append(float(rec['measurement_magn_moment'])/float(rec['specimen_weight']))
                            if len(SInts)>1 and SInts[-1]>maxInt:maxInt=SInts[-1]
                            if len(SInts)>1 and SInts[-1]<minInt:minInt=SInts[-1]
                            if method=="LP-X": 
                                SSucs.append(float(rec[suc_key]))
                                if SSucs[-1]>maxSuc:maxSuc=SSucs[-1]
                                if SSucs[-1]<minSuc:minSuc=SSucs[-1]
                    elif len(Depths) and len(SDepths)==0:
                        for meth in meths:
                            if 'LT-AF-Z' in meth:
                                thisstep='%7.1f'%(float(rec['treatment_ac_field'])*1e3)
                                if thisstep not in steps:steps.append(thisstep)
                                if 'AF' not in methods:methods.append('AF')
                            if 'LT-T-Z' in meth:
                                thisstep='%7.1f'%(float(rec['treatment_ac_field'])-273.)
                                if thisstep not in steps:steps.append(thisstep)
                                if 'T' not in methods:methods.append('T')
                            if 'LT-T-I' in meth:
                                thisstep='%7.1f'%(float(rec['treatment_ac_field'])*1e3)
                                if thisstep not in steps:steps.append(thisstep)
                                if 'ARM' not in methods:methods.append('ARM')
                            if 'LT-IRM' in meth:
                                thisstep='%7.1f'%(float(rec['treatment_dc_field'])*1e3)
                                if thisstep not in steps:steps.append(thisstep)
                                if 'IRM' not in methods:methods.append('IRM')
    if len(Depths)==0 and len(SDepths)==0:
        print 'no data matched your request, try a different lab treatment '
        print 'available treatment types: ',methods
        print 'available treatment steps: ',steps
#        sys.exit()
    if dmin==-1:
        if len(Depths)>0: dmin,dmax=Depths[0],Depths[-1]
        if pltS==1 and len(SDepths)>0:
            if SDepths[0]<dmin:dmin=SDepths[0]
            if SDepths[-1]>dmax:dmax=SDepths[-1]
    SpecDepths,SpecDecs,SpecIncs=[],[],[]
    if spc_file!="" and pltS==1: # add depths to spec data
        for spec in Specs:
            for samp in Samps:
                if samp['er_sample_name']== spec['er_sample_name'] and 'sample_core_depth' in samp.keys() and samp['sample_core_depth']!="":
                    meths=samp['magic_method_codes'].split(":")
                    if 'DE-BFP' not in meths:
                        SpecDepths.append(float(samp['sample_core_depth'])) # fish out data with core_depth
                        SpecDecs.append(float(spec['specimen_dec'])) # fish out data with core_depth
                        SpecIncs.append(float(spec['specimen_inc'])) # fish out data with core_depth
    Susc,Sus_depths=[],[]
    if suc_file!="":
        sucdat=open(suc_file,'rU').readlines()
        keys=sucdat[0].replace('\n','').split(',') # splits on underscores
        for line in sucdat[1:]:
            SucRec={}
            for k in range(len(keys)):SucRec[keys[k]]=line.split(',')[k]
            if float(SucRec['Top Depth (m)'])<dmax and float(SucRec['Top Depth (m)'])>dmin and SucRec['Magnetic Susceptibility (80 mm)']!="":
                Susc.append(float(SucRec['Magnetic Susceptibility (80 mm)']))
                if Susc[-1]>maxSuc:maxSuc=Susc[-1]
                if Susc[-1]<minSuc:minSuc=Susc[-1]
                Sus_depths.append(float(SucRec['Top Depth (m)']))
    WIG,WIG_depths=[],[]
    if wig_file!="":
        wigdat,file_type=pmag.magic_read(wig_file)
        swigdat=pmag.sort_diclist(wigdat,'sample_core_depth')
        keys=wigdat[0].keys()
        for key in keys:
            if key!="sample_core_depth":
                plt_key=key
                break
        for wig in swigdat:
            if float(wig['sample_core_depth'])<dmax and float(wig['sample_core_depth'])>dmin:
                WIG.append(float(wig[plt_key]))
                WIG_depths.append(float(wig['sample_core_depth']))
    tint=4.5
    plt=1
    if len(Decs)>0 and len(Depths)>0 or (len(SpecDecs)>0 and len(SpecDepths)>0) or (len(SDecs)>0 and len(SDepths)>0) or (len(SInts)>0 and len(SDepths)>0) or (len(SIncs)>0 and len(SDepths)>0):
        for pow in range(-10,10):
            if maxInt*10**pow>1:break
        if logit==0:
            for k in range(len(Ints)):
                Ints[k]=Ints[k]*10**pow
            for k in range(len(SInts)):
                SInts[k]=SInts[k]*10**pow
        pylab.figure(1,figsize=(width,8))
        version_num=pmag.get_version()
        pylab.figtext(.02,.01,version_num)
        if pltD==1:
            pylab.subplot(1,pcol,plt)
            if pltL==1:pylab.plot(Decs,Depths,'k') 
            if len(Decs)>0:pylab.plot(Decs,Depths,sym,markersize=size) 
            if len(Decs)==0 and pltL==1 and len(SDecs)>0:pylab.plot(SDecs,SDepths,'k')
            if len(SDecs)>0:pylab.plot(SDecs,SDepths,Ssym,markersize=Ssize) 
            if spc_file!="":pylab.plot(SpecDecs,SpecDepths,spc_sym,markersize=spc_size) 
            if sum_file!="":
                for core in Cores:
                     depth=float(core['Core Top (m)']) 
                     if depth>dmin and depth<dmax:
                        pylab.plot([0,360.],[depth,depth],'b--')
                        if pel==plt:pylab.text(360,depth+tint,core['Core #']+core['Core Type'])
            if pel==plt:
                pylab.axis([0,400,dmax,dmin])
            else:
                pylab.axis([0,360.,dmax,dmin])
            pylab.xlabel('Declination')
            pylab.ylabel('Depth (mbsf)')
            if title!="":pylab.title(title)
            title=""
            plt+=1 
    if pltI==1:
            pylab.subplot(1,pcol,plt)
            if pltL==1:pylab.plot(Incs,Depths,'k') 
            if len(Incs)>0:pylab.plot(Incs,Depths,sym,markersize=size) 
            if len(Incs)==0 and pltL==1 and len(SIncs)>0:pylab.plot(SIncs,SDepths,'k')
            if len(SIncs)>0:pylab.plot(SIncs,SDepths,Ssym,markersize=Ssize) 
            if spc_file!="":pylab.plot(SpecIncs,SpecDepths,spc_sym,markersize=spc_size) 
            if sum_file!="":
                for core in Cores:
                     depth=float(core['Core Top (m)']) 
                     if depth>dmin and depth<dmax:
                         if pel==plt:pylab.text(90,depth+tint,core['Core #']+core['Core Type'])
                         pylab.plot([-90,90],[depth,depth],'b--')
            pylab.plot([0,0],[dmax,dmin],'k-') 
            if pel==plt:
                pylab.axis([-90,110,dmax,dmin])
            else:
                pylab.axis([-90,90,dmax,dmin])
            pylab.xlabel('Inclination')
            pylab.ylabel('')
            if title!="":pylab.title(title)
            title=""
            plt+=1
    if pltM==1 and len(Ints)>0 or len(SInts)>0:
            pylab.subplot(1,pcol,plt)
            if logit==0:
                if pltL==1 and len(Ints)>0: pylab.plot(Ints,Depths,'k') 
                if len(Ints)>0:pylab.plot(Ints,Depths,sym,markersize=size) 
                if len(Ints)==0 and pltL==1 and len(SInts)>0:pylab.plot(SInts,SDepths,'k-')
                if len(SInts)>0:pylab.plot(SInts,SDepths,Ssym,markersize=Ssize) 
                if sum_file!="":
                    for core in Cores:
                         depth=float(core['Core Top (m)']) 
                         pylab.plot([0,maxInt*10**pow+.1],[depth,depth],'b--')
                         if depth>dmin and depth<dmax:pylab.text(maxInt*10**pow-.2*maxInt*10**pow,depth+tint,core['Core #']+core['Core Type'])
                pylab.axis([0,maxInt*10**pow+.1,dmax,dmin])
                if norm==0:
                    pylab.xlabel('%s %i %s'%('Intensity (10^-',pow,' Am^2)'))
                else:
                    pylab.xlabel('%s %i %s'%('Intensity (10^-',pow,' Am^2/kg)'))
            else:
                if pltL==1: pylab.semilogx(Ints,Depths,'k') 
                if len(Ints)>0:pylab.semilogx(Ints,Depths,sym,markersize=size) 
                if len(Ints)==0 and pltL==1 and len(SInts)>0:pylab.semilogx(SInts,SDepths,'k')
                if len(Ints)==0 and pltL==1 and len(SInts)>0:pylab.semilogx(SInts,SDepths,'k')
                if len(SInts)>0:pylab.semilogx(SInts,SDepths,Ssym,markersize=Ssize) 
                if sum_file!="":
                    for core in Cores:
                         depth=float(core['Core Top (m)']) 
                         pylab.semilogx([minInt,maxInt],[depth,depth],'b--')
                         if depth>dmin and depth<dmax:pylab.text(maxInt-.2*maxInt,depth+tint,core['Core #']+core['Core Type'])
                pylab.axis([0,maxInt,dmax,dmin])
                if norm==0:
                    pylab.xlabel('Intensity (Am^2)')
                else:
                    pylab.xlabel('Intensity (Am^2/kg)')
            if title!="":pylab.title(title)
            title=""
            plt+=1
    if suc_file!="" or len(SSucs)>0:
            pylab.subplot(1,pcol,plt)
            if len(Susc)>0:
                if pltL==1:pylab.plot(Susc,Sus_depths,'k') 
                if logit==0:pylab.plot(Susc,Sus_depths,sym,markersize=size) 
                if logit==1:pylab.semilogx(Susc,Sus_depths,sym,markersize=size) 
            if len(SSucs)>0:
                if logit==0:pylab.plot(SSucs,SDepths,sym,markersize=size) 
                if logit==1:pylab.semilogx(SSucs,SDepths,sym,markersize=size) 
            if sum_file!="":
                for core in Cores:
                     depth=float(core['Core Top (m)']) 
                     if logit==0:pylab.plot([minSuc,maxSuc],[depth,depth],'b--')
                     if logit==1:pylab.semilogx([minSuc,maxSuc],[depth,depth],'b--')
            pylab.axis([minSuc,maxSuc,dmax,dmin])
            pylab.xlabel('Susceptibility')
            if title!="":pylab.title(title)
            title=""
            plt+=1
    if wig_file!="":
            pylab.subplot(1,pcol,plt)
            pylab.plot(WIG,WIG_depths,'k') 
            if sum_file!="":
                for core in Cores:
                     depth=float(core['Core Top (m)']) 
                     pylab.plot([WIG[0],WIG[-1]],[depth,depth],'b--')
            pylab.axis([min(WIG),max(WIG),dmax,dmin])
            pylab.xlabel(plt_key)
            if title!="":pylab.title(title)
            title=""
            plt+=1
    if pTS==1:
            ax1=pylab.subplot(1,pcol,plt)
            plt+=1
            TS,Chrons=pmag.get_TS(ts)
            p=1
            X,Y=[],[]
            for d in TS:
                if d<=amax and d>=amin:
                    if len(X)==0:
                        ind=TS.index(d)
                        X.append(TS[ind-1])
                        Y.append(p%2)
                    X.append(d)
                    Y.append(p%2)
                    p+=1
                    X.append(d)
                    Y.append(p%2)
                else: 
                    X.append(amax)
                    Y.append(p%2)
                #    isign=-1
            ax1.plot(Y,X,'k')
            ax1.plot([0,1,1,0,0],[amin,amin,amax,amax,amin],'k-')
            ax1.axis([-.25,1.5,amax,amin])
            ax2=ax1.twinx()
            pylab.ylabel("Age (Ma): "+ts) 
            for c in Chrons:
                #     isign=-1*isign 
                #     if isign>0: off=1.05
                     if c[1]>=amin and c[1]<amax:
                         ax2.text(1.05,c[1],c[0])
            ax2.axis([-.25,1.5,amax,amin])
    pylab.draw()
    ans=raw_input("Press return to quit  ")
    sys.exit()