Ejemplo n.º 1
0
def main():
    """
    NAME
        dmag_magic.py

    DESCRIPTION
       plots intensity decay curves for demagnetization experiments

    SYNTAX
        dmag_magic -h [command line options]

    INPUT 
       takes magic formatted magic_measurements.txt files

    OPTIONS
        -h prints help message and quits
        -f FILE: specify input file, default is: magic_measurements.txt
        -obj OBJ: specify  object  [loc, sit, sam, spc] for plot, default is by location
        -LT [AF,T,M]: specify lab treatment type, default AF
        -XLP [PI]: exclude specific  lab protocols (for example, method codes like LP-PI)
        -N do not normalize by NRM magnetization
        -sav save plots silently and quit
        -fmt [svg,jpg,png,pdf] set figure format [default is svg]
    NOTE
        loc: location (study); sit: site; sam: sample; spc: specimen
    """
    FIG={} # plot dictionary
    FIG['demag']=1 # demag is figure 1
    in_file,plot_key,LT='magic_measurements.txt','er_location_name',"LT-AF-Z"
    XLP=""
    norm=1
    LT='LT-AF-Z'
    units,dmag_key='T','treatment_ac_field'
    plot=0
    fmt='svg'
    if len(sys.argv)>1:
        if '-h' in sys.argv:
            print main.__doc__
            sys.exit()
        if '-N' in sys.argv: norm=0
        if '-sav' in sys.argv: 
            plot=1
        if '-f' in sys.argv:
            ind=sys.argv.index("-f")
            in_file=sys.argv[ind+1]
        if '-fmt' in sys.argv:
            ind=sys.argv.index("-fmt")
            fmt=sys.argv[ind+1]
        if '-obj' in sys.argv:
            ind=sys.argv.index('-obj')
            plot_by=sys.argv[ind+1]
            if plot_by=='sit':plot_key='er_site_name'
            if plot_by=='sam':plot_key='er_sample_name'
            if plot_by=='spc':plot_key='er_specimen_name'
        if '-XLP' in sys.argv:
            ind=sys.argv.index("-XLP")
            XLP=sys.argv[ind+1] # get lab protocol for excluding
        if '-LT' in sys.argv:
            ind=sys.argv.index("-LT")
            LT='LT-'+sys.argv[ind+1]+'-Z' # get lab treatment for plotting
            if  LT=='LT-T-Z':
                units,dmag_key='K','treatment_temp'
            elif  LT=='LT-AF-Z':
                units,dmag_key='T','treatment_ac_field'
            elif  LT=='LT-M-Z':
                units,dmag_key='J','treatment_mw_energy'
            else:
                units='U'
    data,file_type=pmag.magic_read(in_file)
    sids=pmag.get_specs(data)
    pmagplotlib.plot_init(FIG['demag'],5,5)
    print len(data),' records read from ',in_file
    #
    #
    # find desired intensity data
    #
    #
    plotlist,intlist=[],['measurement_magnitude','measurement_magn_moment','measurement_magn_volume','measurement_magn_mass']
    IntMeths=[]
    FixData=[]
    for  rec in data:
        meths=[]
        methcodes=rec['magic_method_codes'].split(':')
        for meth in methcodes:meths.append(meth.strip())
        for key in rec.keys():
            if key in intlist and rec[key]!="":
                if key not in IntMeths:IntMeths.append(key)
                if rec[plot_key] not in plotlist and LT in meths: plotlist.append(rec[plot_key])
                if 'measurement_flag' not in rec.keys():rec['measurement_flag']='g'
                FixData.append(rec)
        plotlist.sort()
    if len(IntMeths)==0:
        print 'No intensity information found'
        sys.exit()
    data=FixData
    int_key=IntMeths[0] # plot first intensity method found - normalized to initial value anyway - doesn't matter which used
    for plt in plotlist:
        if plot==0: print plt,'plotting by: ',plot_key
        PLTblock=pmag.get_dictitem(data,plot_key,plt,'T') # fish out all the data for this type of plot
        PLTblock=pmag.get_dictitem(PLTblock,'magic_method_codes',LT,'has') # fish out all the dmag for this experiment type
        PLTblock=pmag.get_dictitem(PLTblock,int_key,'','F') # get all with this intensity key non-blank
        if XLP!="":PLTblock=pmag.get_dictitem(PLTblock,'magic_method_codes',XLP,'not') # reject data with XLP in method_code
        if len(PLTblock)>2:
            title=PLTblock[0][plot_key]
            spcs=[]
            for rec in PLTblock:
                if rec['er_specimen_name'] not in spcs:spcs.append(rec['er_specimen_name'])
            for spc in spcs:
                SPCblock=pmag.get_dictitem(PLTblock,'er_specimen_name',spc,'T') # plot specimen by specimen
                INTblock=[]
                for rec in SPCblock:
                    INTblock.append([float(rec[dmag_key]),0,0,float(rec[int_key]),1,rec['measurement_flag']])
                if len(INTblock)>2:
                    pmagplotlib.plotMT(FIG['demag'],INTblock,title,0,units,norm)
            if plot==1:
                files={}
                for key in FIG.keys():
                    files[key]=title+'_'+LT+'.'+fmt
                pmagplotlib.saveP(FIG,files) 
                sys.exit()
            else:
                pmagplotlib.drawFIGS(FIG)
                ans=raw_input(" S[a]ve to save plot, [q]uit,  Return to continue:  ")
                if ans=='q':sys.exit()
                if ans=="a": 
                    files={}
                    for key in FIG.keys():
                        files[key]=title+'_'+LT+'.svg' 
                    pmagplotlib.saveP(FIG,files) 
            pmagplotlib.clearFIG(FIG['demag'])
Ejemplo n.º 2
0
def main():
    """
    NAME
        dmag_magic.py

    DESCRIPTION
       plots intensity decay curves for demagnetization experiments

    SYNTAX
        dmag_magic -h [command line options]

    INPUT
       takes magic formatted magic_measurements.txt files

    OPTIONS
        -h prints help message and quits
        -f FILE: specify input file, default is: magic_measurements.txt
        -obj OBJ: specify  object  [loc, sit, sam, spc] for plot,
               default is by location
        -LT [AF,T,M]: specify lab treatment type, default AF
        -XLP [PI]: exclude specific  lab protocols,
               (for example, method codes like LP-PI)
        -N do not normalize by NRM magnetization
        -sav save plots silently and quit
        -fmt [svg,jpg,png,pdf] set figure format [default is svg]
    NOTE
        loc: location (study); sit: site; sam: sample; spc: specimen
    """
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    # initialize variables from command line + defaults
    FIG = {}  # plot dictionary
    FIG['demag'] = 1  # demag is figure 1
    in_file = pmag.get_named_arg_from_sys("-f", default_val="measurements.txt")
    plot_by = pmag.get_named_arg_from_sys("-obj", default_val="loc")
    name_dict = {
        'loc': 'location',
        'sit': 'site',
        'sam': 'sample',
        'spc': 'specimen'
    }
    plot_key = name_dict[plot_by]
    LT = "LT-" + pmag.get_named_arg_from_sys("-LT", "AF") + "-Z"
    if LT == "LT-T-Z":
        units, dmag_key = 'K', 'treat_temp'
    elif LT == "LT-AF-Z":
        units, dmag_key = 'T', 'treat_ac_field'
    elif LT == 'LT-M-Z':
        units, dmag_key = 'J', 'treat_mw_energy'
    else:
        units = 'U'
    no_norm = pmag.get_flag_arg_from_sys("-N")
    norm = 0 if no_norm else 1
    no_plot = pmag.get_flag_arg_from_sys("-sav")
    plot = 0 if no_plot else 1
    fmt = pmag.get_named_arg_from_sys("-fmt", "svg")
    XLP = pmag.get_named_arg_from_sys("-XLP", "")
    dir_path = pmag.get_named_arg_from_sys("-WD", os.getcwd())
    spec_file = pmag.get_named_arg_from_sys("-fsp",
                                            default_val="specimens.txt")
    samp_file = pmag.get_named_arg_from_sys("-fsa", default_val="samples.txt")
    site_file = pmag.get_named_arg_from_sys("-fsi", default_val="sites.txt")

    # create contribution and add required headers
    fnames = {"specimens": spec_file, "samples": samp_file, 'sites': site_file}
    contribution = nb.Contribution(dir_path,
                                   single_file=in_file,
                                   custom_filenames=fnames)
    file_type = list(contribution.tables.keys())[0]
    print(len(contribution.tables['measurements'].df), ' records read from ',
          in_file)
    # add plot_key into measurements table
    if plot_key not in contribution.tables['measurements'].df.columns:
        #contribution.propagate_name_down(plot_key, 'measurements')
        contribution.propagate_location_to_measurements()
    data_container = contribution.tables[file_type]
    # pare down to only records with useful data
    # grab records that have the requested code
    data_slice = data_container.get_records_for_code(LT)
    # and don't have the offending code
    data = data_container.get_records_for_code(XLP,
                                               incl=False,
                                               use_slice=True,
                                               sli=data_slice,
                                               strict_match=False)

    # make sure quality is in the dataframe
    if 'quality' not in data.columns:
        data['quality'] = 'g'
    # get intensity key and make sure intensity data is not blank
    intlist = ['magn_moment', 'magn_volume', 'magn_mass']
    IntMeths = [col_name for col_name in data.columns if col_name in intlist]
    # get rid of any entirely blank intensity columns
    for col_name in IntMeths:
        if not data[col_name].any():
            data.drop(col_name, axis=1, inplace=True)
    IntMeths = [col_name for col_name in data.columns if col_name in intlist]
    if len(IntMeths) == 0:
        print('No intensity headers found')
        sys.exit()

    int_key = IntMeths[
        0]  # plot first intensity method found - normalized to initial value anyway - doesn't matter which used
    data = data[data[int_key].notnull()]
    # make list of individual plots
    # by default, will be by location_name
    plotlist = data[plot_key].unique()
    plotlist.sort()
    pmagplotlib.plot_init(FIG['demag'], 5, 5)
    # iterate through and plot the data
    for plt in plotlist:
        plot_data = data[data[plot_key] == plt].copy()
        if plot:
            print(plt, 'plotting by: ', plot_key)
        if len(plot_data) > 2:
            title = plt
            spcs = []
            spcs = plot_data['specimen'].unique()
            for spc in spcs:
                INTblock = []
                spec_data = plot_data[plot_data['specimen'] == spc]
                for ind, rec in spec_data.iterrows():
                    INTblock.append([
                        float(rec[dmag_key]), 0, 0,
                        float(rec[int_key]), 1, rec['quality']
                    ])
                if len(INTblock) > 2:
                    pmagplotlib.plotMT(FIG['demag'], INTblock, title, 0, units,
                                       norm)

            if not plot:
                files = {}
                for key in list(FIG.keys()):
                    files[key] = title + '_' + LT + '.' + fmt
                pmagplotlib.saveP(FIG, files)
                #sys.exit()
            else:
                pmagplotlib.drawFIGS(FIG)
                prompt = " S[a]ve to save plot, [q]uit,  Return to continue:  "
                ans = input(prompt)
                if ans == 'q':
                    sys.exit()
                if ans == "a":
                    files = {}
                    for key in list(FIG.keys()):
                        files[key] = title + '_' + LT + '.' + fmt
                    pmagplotlib.saveP(FIG, files)
            pmagplotlib.clearFIG(FIG['demag'])
Ejemplo n.º 3
0
def main():
    """
    NAME
        dmag_magic.py

    DESCRIPTION
       plots intensity decay curves for demagnetization experiments

    SYNTAX
        dmag_magic -h [command line options]

    INPUT
       takes magic formatted magic_measurements.txt files

    OPTIONS
        -h prints help message and quits
        -f FILE: specify input file, default is: magic_measurements.txt
        -obj OBJ: specify  object  [loc, sit, sam, spc] for plot, default is by location
        -LT [AF,T,M]: specify lab treatment type, default AF
        -XLP [PI]: exclude specific  lab protocols (for example, method codes like LP-PI)
        -N do not normalize by NRM magnetization
        -sav save plots silently and quit
        -fmt [svg,jpg,png,pdf] set figure format [default is svg]
    NOTE
        loc: location (study); sit: site; sam: sample; spc: specimen
    """
    FIG = {}  # plot dictionary
    FIG['demag'] = 1  # demag is figure 1
    in_file, plot_key, LT = 'magic_measurements.txt', 'er_location_name', "LT-AF-Z"
    XLP = ""
    norm = 1
    LT = 'LT-AF-Z'
    units, dmag_key = 'T', 'treatment_ac_field'
    plot = 0
    fmt = 'svg'
    if len(sys.argv) > 1:
        if '-h' in sys.argv:
            print(main.__doc__)
            sys.exit()
        if '-N' in sys.argv: norm = 0
        if '-sav' in sys.argv:
            plot = 1
        if '-f' in sys.argv:
            ind = sys.argv.index("-f")
            in_file = sys.argv[ind + 1]
        if '-fmt' in sys.argv:
            ind = sys.argv.index("-fmt")
            fmt = sys.argv[ind + 1]
        if '-obj' in sys.argv:
            ind = sys.argv.index('-obj')
            plot_by = sys.argv[ind + 1]
            if plot_by == 'sit': plot_key = 'er_site_name'
            if plot_by == 'sam': plot_key = 'er_sample_name'
            if plot_by == 'spc': plot_key = 'er_specimen_name'
        if '-XLP' in sys.argv:
            ind = sys.argv.index("-XLP")
            XLP = sys.argv[ind + 1]  # get lab protocol for excluding
        if '-LT' in sys.argv:
            ind = sys.argv.index("-LT")
            LT = 'LT-' + sys.argv[ind +
                                  1] + '-Z'  # get lab treatment for plotting
            if LT == 'LT-T-Z':
                units, dmag_key = 'K', 'treatment_temp'
            elif LT == 'LT-AF-Z':
                units, dmag_key = 'T', 'treatment_ac_field'
            elif LT == 'LT-M-Z':
                units, dmag_key = 'J', 'treatment_mw_energy'
            else:
                units = 'U'
    data, file_type = pmag.magic_read(in_file)
    sids = pmag.get_specs(data)
    pmagplotlib.plot_init(FIG['demag'], 5, 5)
    print(len(data), ' records read from ', in_file)
    #
    #
    # find desired intensity data
    #
    #
    plotlist, intlist = [], [
        'measurement_magnitude', 'measurement_magn_moment',
        'measurement_magn_volume', 'measurement_magn_mass'
    ]
    IntMeths = []
    FixData = []
    for rec in data:
        meths = []
        methcodes = rec['magic_method_codes'].split(':')
        for meth in methcodes:
            meths.append(meth.strip())
        for key in rec.keys():
            if key in intlist and rec[key] != "":
                if key not in IntMeths: IntMeths.append(key)
                if rec[plot_key] not in plotlist and LT in meths:
                    plotlist.append(rec[plot_key])
                if 'measurement_flag' not in rec.keys():
                    rec['measurement_flag'] = 'g'
                FixData.append(rec)
        plotlist.sort()
    if len(IntMeths) == 0:
        print('No intensity information found')
        sys.exit()
    data = FixData
    int_key = IntMeths[
        0]  # plot first intensity method found - normalized to initial value anyway - doesn't matter which used
    for plt in plotlist:
        if plot == 0: print(plt, 'plotting by: ', plot_key)
        PLTblock = pmag.get_dictitem(
            data, plot_key, plt,
            'T')  # fish out all the data for this type of plot
        PLTblock = pmag.get_dictitem(
            PLTblock, 'magic_method_codes', LT,
            'has')  # fish out all the dmag for this experiment type
        PLTblock = pmag.get_dictitem(
            PLTblock, int_key, '',
            'F')  # get all with this intensity key non-blank
        if XLP != "":
            PLTblock = pmag.get_dictitem(
                PLTblock, 'magic_method_codes', XLP,
                'not')  # reject data with XLP in method_code
        if len(PLTblock) > 2:
            title = PLTblock[0][plot_key]
            spcs = []
            for rec in PLTblock:
                if rec['er_specimen_name'] not in spcs:
                    spcs.append(rec['er_specimen_name'])
            for spc in spcs:
                SPCblock = pmag.get_dictitem(PLTblock, 'er_specimen_name', spc,
                                             'T')  # plot specimen by specimen
                INTblock = []
                for rec in SPCblock:
                    INTblock.append([
                        float(rec[dmag_key]), 0, 0,
                        float(rec[int_key]), 1, rec['measurement_flag']
                    ])
                if len(INTblock) > 2:
                    pmagplotlib.plotMT(FIG['demag'], INTblock, title, 0, units,
                                       norm)
            if plot == 1:
                files = {}
                for key in FIG.keys():
                    files[key] = title + '_' + LT + '.' + fmt
                pmagplotlib.saveP(FIG, files)
                sys.exit()
            else:
                pmagplotlib.drawFIGS(FIG)
                ans = raw_input(
                    " S[a]ve to save plot, [q]uit,  Return to continue:  ")
                if ans == 'q': sys.exit()
                if ans == "a":
                    files = {}
                    for key in FIG.keys():
                        files[key] = title + '_' + LT + '.' + fmt
                    pmagplotlib.saveP(FIG, files)
            pmagplotlib.clearFIG(FIG['demag'])
Ejemplo n.º 4
0
def main():
    """
    NAME
        irmaq_magic.py

    DESCRIPTION
       plots IRM acquisition curves from magic_measurements file

    SYNTAX 
        irmaq_magic [command line options]
    
    INPUT 
       takes magic formatted magic_measurements.txt files
    
    OPTIONS
        -h prints help message and quits
        -f FILE: specify input file, default is: magic_measurements.txt
        -obj OBJ: specify  object  [loc, sit, sam, spc] for plot, default is by location
        -N ; do not normalize by last point - use original units
        -fmt [png,jpg,eps,pdf] set plot file format [default is svg]
        -sav save plot[s] and quit
    NOTE
        loc: location (study); sit: site; sam: sample; spc: specimen
    """
    FIG = {}  # plot dictionary
    FIG['exp'] = 1  # exp is figure 1
    dir_path = './'
    plot, fmt = 0, 'svg'
    units, dmag_key = 'T', 'treatment_dc_field'
    XLP = []
    norm = 1
    in_file, plot_key, LP = 'magic_measurements.txt', 'er_location_name', "LP-IRM"
    if len(sys.argv) > 1:
        if '-h' in sys.argv:
            print main.__doc__
            sys.exit()
        if '-N' in sys.argv: norm = 0
        if '-sav' in sys.argv: plot = 1
        if '-fmt' in sys.argv:
            ind = sys.argv.index("-fmt")
            fmt = sys.argv[ind + 1]
        if '-f' in sys.argv:
            ind = sys.argv.index("-f")
            in_file = sys.argv[ind + 1]
        if '-WD' in sys.argv:
            ind = sys.argv.index('-WD')
            dir_path = sys.argv[ind + 1]
            in_file = dir_path + '/' + in_file
        if '-obj' in sys.argv:
            ind = sys.argv.index('-obj')
            plot_by = sys.argv[ind + 1]
            if plot_by == 'sit': plot_key = 'er_site_name'
            if plot_by == 'sam': plot_key = 'er_sample_name'
            if plot_by == 'spc': plot_key = 'er_specimen_name'
    data, file_type = pmag.magic_read(in_file)
    sids = pmag.get_specs(data)
    pmagplotlib.plot_init(FIG['exp'], 6, 6)
    #
    #
    # find desired intensity data
    #
    # get plotlist
    #
    plotlist, intlist = [], [
        'measurement_magnitude', 'measurement_magn_moment',
        'measurement_magn_volume', 'measurement_magn_mass'
    ]
    IntMeths = []
    data = pmag.get_dictitem(
        data, 'magic_method_codes', LP,
        'has')  # get all the records with this lab protocol
    Ints = {}
    NoInts, int_key = 1, ""
    for key in intlist:
        Ints[key] = pmag.get_dictitem(
            data, key, '', 'F')  # get all non-blank data for intensity type
        if len(Ints[key]) > 0:
            NoInts = 0
            if int_key == "": int_key = key
    if NoInts == 1:
        print 'No intensity information found'
        sys.exit()
    for rec in Ints[int_key]:
        if rec[plot_key] not in plotlist: plotlist.append(rec[plot_key])
    plotlist.sort()
    for plt in plotlist:
        print plt
        INTblock = []
        data = pmag.get_dictitem(
            Ints[int_key], plot_key, plt, 'T'
        )  # get data with right intensity info whose plot_key matches plot
        sids = pmag.get_specs(
            data)  # get a list of specimens with appropriate data
        if len(sids) > 0:
            title = data[0][plot_key]
        for s in sids:
            INTblock = []
            sdata = pmag.get_dictitem(data, 'er_specimen_name', s,
                                      'T')  # get data for each specimen
            for rec in sdata:
                INTblock.append(
                    [float(rec[dmag_key]), 0, 0,
                     float(rec[int_key]), 1, 'g'])
            pmagplotlib.plotMT(FIG['exp'], INTblock, title, 0, units, norm)
        files = {}
        for key in FIG.keys():
            files[key] = title + '_' + LP + '.' + fmt
        if plot == 0:
            pmagplotlib.drawFIGS(FIG)
            ans = raw_input(
                " S[a]ve to save plot, [q]uit,  Return to continue:  ")
            if ans == 'q': sys.exit()
            if ans == "a":
                pmagplotlib.saveP(FIG, files)
        else:
            pmagplotlib.saveP(FIG, files)
        pmagplotlib.clearFIG(FIG['exp'])
Ejemplo n.º 5
0
def main():
    """
    NAME
        irmaq_magic.py

    DESCRIPTION
       plots IRM acquisition curves from magic_measurements file

    SYNTAX 
        irmaq_magic [command line options]
    
    INPUT 
       takes magic formatted magic_measurements.txt files
    
    OPTIONS
        -h prints help message and quits
        -f FILE: specify input file, default is: magic_measurements.txt
        -obj OBJ: specify  object  [loc, sit, sam, spc] for plot, default is by location
        -N ; do not normalize by last point - use original units
        -fmt [png,jpg,eps,pdf] set plot file format [default is svg]
        -sav save plot[s] and quit
    NOTE
        loc: location (study); sit: site; sam: sample; spc: specimen
    """
    FIG={} # plot dictionary
    FIG['exp']=1 # exp is figure 1
    dir_path='./'
    plot,fmt=0,'svg'
    units,dmag_key='T','treatment_dc_field'
    XLP=[]
    norm=1
    in_file,plot_key,LP='magic_measurements.txt','er_location_name',"LP-IRM"
    if len(sys.argv)>1:
        if '-h' in sys.argv:
            print main.__doc__
            sys.exit()
        if '-N' in sys.argv:norm=0
        if '-sav' in sys.argv:plot=1
        if '-fmt' in sys.argv:
            ind=sys.argv.index("-fmt")
            fmt=sys.argv[ind+1]
        if '-f' in sys.argv:
            ind=sys.argv.index("-f")
            in_file=sys.argv[ind+1]
        if '-WD' in sys.argv:
            ind=sys.argv.index('-WD')
            dir_path=sys.argv[ind+1]
            in_file=dir_path+'/'+in_file
        if '-obj' in sys.argv:
            ind=sys.argv.index('-obj')
            plot_by=sys.argv[ind+1]
            if plot_by=='sit':plot_key='er_site_name'
            if plot_by=='sam':plot_key='er_sample_name'
            if plot_by=='spc':plot_key='er_specimen_name'
    data,file_type=pmag.magic_read(in_file)
    sids=pmag.get_specs(data)
    pmagplotlib.plot_init(FIG['exp'],6,6)
    #
    #
    # find desired intensity data
    #
    # get plotlist
    #
    plotlist,intlist=[],['measurement_magnitude','measurement_magn_moment','measurement_magn_volume','measurement_magn_mass']
    IntMeths=[]
    data=pmag.get_dictitem(data,'magic_method_codes',LP,'has') # get all the records with this lab protocol
    Ints={}
    NoInts,int_key=1,""
    for key in intlist:
       Ints[key]=pmag.get_dictitem(data,key,'','F') # get all non-blank data for intensity type
       if len(Ints[key])>0:
           NoInts=0 
           if int_key=="":int_key=key
    if NoInts==1:
        print 'No intensity information found'
        sys.exit()
    for  rec in Ints[int_key]:
        if rec[plot_key] not in plotlist: plotlist.append(rec[plot_key])
    plotlist.sort()
    for plt in plotlist:
        print plt
        INTblock=[]
        data=pmag.get_dictitem(Ints[int_key],plot_key,plt,'T') # get data with right intensity info whose plot_key matches plot
        sids=pmag.get_specs(data) # get a list of specimens with appropriate data
        if len(sids)>0: 
            title=data[0][plot_key]
        for s in sids:
            INTblock=[]
            sdata=pmag.get_dictitem(data,'er_specimen_name',s,'T') # get data for each specimen
            for rec in sdata:
                INTblock.append([float(rec[dmag_key]),0,0,float(rec[int_key]),1,'g'])
            pmagplotlib.plotMT(FIG['exp'],INTblock,title,0,units,norm)
        files={}
        for key in FIG.keys():
            files[key]=title+'_'+LP+'.'+fmt 
        if plot==0:
            pmagplotlib.drawFIGS(FIG)
            ans=raw_input(" S[a]ve to save plot, [q]uit,  Return to continue:  ")
            if ans=='q':sys.exit()
            if ans=="a": 
                pmagplotlib.saveP(FIG,files) 
        else:
            pmagplotlib.saveP(FIG,files) 
        pmagplotlib.clearFIG(FIG['exp'])
Ejemplo n.º 6
0
def main():
    """
    NAME
        dmag_magic.py

    DESCRIPTION
       plots intensity decay curves for demagnetization experiments

    SYNTAX
        dmag_magic -h [command line options]

    INPUT
       takes magic formatted magic_measurements.txt files

    OPTIONS
        -h prints help message and quits
        -f FILE: specify input file, default is: magic_measurements.txt
        -obj OBJ: specify  object  [loc, sit, sam, spc] for plot,
               default is by location
        -LT [AF,T,M]: specify lab treatment type, default AF
        -XLP [PI]: exclude specific  lab protocols,
               (for example, method codes like LP-PI)
        -N do not normalize by NRM magnetization
        -sav save plots silently and quit
        -fmt [svg,jpg,png,pdf] set figure format [default is svg]
    NOTE
        loc: location (study); sit: site; sam: sample; spc: specimen
    """
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    # initialize variables from command line + defaults
    FIG = {}  # plot dictionary
    FIG['demag'] = 1  # demag is figure 1
    in_file = pmag.get_named_arg_from_sys("-f", default_val="measurements.txt")
    plot_by = pmag.get_named_arg_from_sys("-obj", default_val="loc")
    name_dict = {'loc': 'location', 'sit': 'site',
                 'sam': 'sample', 'spc': 'specimen'}
    plot_key = name_dict[plot_by]
    LT = "LT-" + pmag.get_named_arg_from_sys("-LT", "AF") + "-Z"
    if LT == "LT-T-Z":
        units, dmag_key = 'K', 'treat_temp'
    elif LT == "LT-AF-Z":
        units, dmag_key = 'T', 'treat_ac_field'
    elif LT == 'LT-M-Z':
        units, dmag_key = 'J', 'treat_mw_energy'
    else:
        units = 'U'
    no_norm = pmag.get_flag_arg_from_sys("-N")
    norm = 0 if no_norm else 1
    no_plot = pmag.get_flag_arg_from_sys("-sav")
    plot = 0 if no_plot else 1
    fmt = pmag.get_named_arg_from_sys("-fmt", "svg")
    XLP = pmag.get_named_arg_from_sys("-XLP", "")
    dir_path = pmag.get_named_arg_from_sys("-WD", os.getcwd())
    spec_file = pmag.get_named_arg_from_sys("-fsp", default_val="specimens.txt")
    samp_file = pmag.get_named_arg_from_sys("-fsa", default_val="samples.txt")
    site_file = pmag.get_named_arg_from_sys("-fsi", default_val="sites.txt")

    # create contribution and add required headers
    fnames = {"specimens": spec_file, "samples": samp_file, 'sites': site_file}
    contribution = nb.Contribution(dir_path, single_file=in_file,
                                   custom_filenames=fnames)
    file_type = contribution.tables.keys()[0]
    print len(contribution.tables['measurements'].df), ' records read from ', in_file
    # add plot_key into measurements table
    if plot_key not in contribution.tables['measurements'].df.columns:
        contribution.propagate_name_down(plot_key, 'measurements')
    data_container = contribution.tables[file_type]
    # pare down to only records with useful data
    # grab records that have the requested code
    data_slice = data_container.get_records_for_code(LT)
    # and don't have the offending code
    data = data_container.get_records_for_code(XLP, incl=False, use_slice=True,
                                               sli=data_slice, strict_match=False)

    # make sure quality is in the dataframe
    if 'quality' not in data.columns:
        data['quality'] = 'g'
    # get intensity key and make sure intensity data is not blank
    intlist = ['magn_moment', 'magn_volume', 'magn_mass']
    IntMeths = [col_name for col_name in data.columns if col_name in intlist]
    # get rid of any entirely blank intensity columns
    for col_name in IntMeths:
        if not data[col_name].any():
            data.drop(col_name, axis=1, inplace=True)
    IntMeths = [col_name for col_name in data.columns if col_name in intlist]
    if len(IntMeths) == 0:
        print 'No intensity headers found'
        sys.exit()

    int_key = IntMeths[0] # plot first intensity method found - normalized to initial value anyway - doesn't matter which used
    data = data[data[int_key].notnull()]
    # make list of individual plots
    # by default, will be by location_name
    plotlist = data[plot_key].unique()
    plotlist.sort()
    pmagplotlib.plot_init(FIG['demag'], 5, 5)
    # iterate through and plot the data
    for plt in plotlist:
        plot_data = data[data[plot_key] == plt].copy()
        if plot:
            print plt, 'plotting by: ', plot_key
        if len(plot_data) > 2:
            title = plt
            spcs = []
            spcs = plot_data['specimen'].unique()
            for spc in spcs:
                INTblock = []
                spec_data = plot_data[plot_data['specimen'] == spc]
                for ind, rec in spec_data.iterrows():
                    INTblock.append([float(rec[dmag_key]), 0, 0, float(rec[int_key]), 1, rec['quality']])
                if len(INTblock) > 2:
                    pmagplotlib.plotMT(FIG['demag'], INTblock,
                                       title, 0, units, norm)

            if not plot:
                files = {}
                for key in FIG.keys():
                    files[key] = title + '_' + LT + '.' + fmt
                pmagplotlib.saveP(FIG, files)
                #sys.exit()
            else:
                pmagplotlib.drawFIGS(FIG)
                prompt = " S[a]ve to save plot, [q]uit,  Return to continue:  "
                ans = raw_input(prompt)
                if ans == 'q':
                    sys.exit()
                if ans == "a":
                    files = {}
                    for key in FIG.keys():
                        files[key] = title + '_' + LT + '.' + fmt
                    pmagplotlib.saveP(FIG, files)
            pmagplotlib.clearFIG(FIG['demag'])
Ejemplo n.º 7
0
def main():
    """
    NAME
        irmaq_magic.py

    DESCRIPTION
       plots IRM acquisition curves from measurements file

    SYNTAX
        irmaq_magic [command line options]

    INPUT
       takes magic formatted magic_measurements.txt files

    OPTIONS
        -h prints help message and quits
        -f FILE: specify input file, default is: magic_measurements.txt/measurements.txt
        -obj OBJ: specify  object  [loc, sit, sam, spc] for plot, default is by location
        -N ; do not normalize by last point - use original units
        -fmt [png,jpg,eps,pdf] set plot file format [default is svg]
        -sav save plot[s] and quit
        -DM MagIC data model number, default is 3
    NOTE
        loc: location (study); sit: site; sam: sample; spc: specimen
    """
    FIG = {}  # plot dictionary
    FIG['exp'] = 1  # exp is figure 1
    dir_path = './'
    plot, fmt = 0, 'svg'
    units = 'T',
    XLP = []
    norm = 1
    LP = "LP-IRM"
    if len(sys.argv) > 1:
        if '-h' in sys.argv:
            print(main.__doc__)
            sys.exit()
        data_model = int(pmag.get_named_arg_from_sys("-DM", 3))
        if '-N' in sys.argv:
            norm = 0
        if '-sav' in sys.argv:
            plot = 1
        if '-fmt' in sys.argv:
            ind = sys.argv.index("-fmt")
            fmt = sys.argv[ind + 1]
        if data_model == 3:
            in_file = pmag.get_named_arg_from_sys("-f", 'measurements.txt')
        else:
            in_file = pmag.get_named_arg_from_sys("-f", 'magic_measurements.txt')
        if '-WD' in sys.argv:
            ind = sys.argv.index('-WD')
            dir_path = sys.argv[ind + 1]
        dir_path = os.path.realpath(dir_path)
        in_file = pmag.resolve_file_name(in_file, dir_path)
        if '-WD' not in sys.argv:
            dir_path = os.path.split(in_file)[0]
        plot_by = pmag.get_named_arg_from_sys("-obj", "loc")
        if data_model == 3:
            plot_key = 'location'
            if plot_by == 'sit':
                plot_key = 'site'
            if plot_by == 'sam':
                plot_key = 'sample'
            if plot_by == 'spc':
                plot_key = 'specimen'
        else:
            plot_key = 'er_location_name'
            if plot_by == 'sit':
                plot_key = 'er_site_name'
            if plot_by == 'sam':
                plot_key = 'er_sample_name'
            if plot_by == 'spc':
                plot_key = 'er_specimen_name'

    # set defaults and get more information if needed
    if data_model == 3:
        dmag_key = 'treat_dc_field'
    else:
        dmag_key = 'treatment_dc_field'
    #
    if data_model == 3 and plot_key != 'specimen':
        # gonna need to read in more files
        print('-W- You are trying to plot measurements by {}'.format(plot_key))
        print('    By default, this information is not available in your measurement file.')
        print('    Trying to acquire this information from {}'.format(dir_path))
        con = nb.Contribution(dir_path)
        meas_df = con.propagate_location_to_measurements()
        if plot_key not in meas_df.columns:
            print('-W- Could not find required data.')
            print('    Try a different plot key.')
            return
        else:
            print('-I- Found {} information, continuing with plotting'.format(plot_key))
        con.write_table_to_file('measurements')

    # read in data
    data, file_type = pmag.magic_read(in_file)
    sids = pmag.get_specs(data)
    pmagplotlib.plot_init(FIG['exp'], 6, 6)
    #
    #
    # find desired intensity data
    #
    # get plotlist
    #
    plotlist = []
    if data_model == 3:
        intlist = ['magn_moment', 'magn_volume', 'magn_mass', 'magnitude']
    else:
        intlist = ['measurement_magnitude', 'measurement_magn_moment',
                    'measurement_magn_volume', 'measurement_magn_mass']
    IntMeths = []
    # get all the records with this lab protocol
    #print('data', len(data))
    #print('data[0]', data[0])
    if data_model == 3:
        data = pmag.get_dictitem(data, 'method_codes', LP, 'has')
    else:
        data = pmag.get_dictitem(data, 'magic_method_codes', LP, 'has')
    Ints = {}
    NoInts, int_key = 1, ""
    for key in intlist:
        # get all non-blank data for intensity type
        Ints[key] = pmag.get_dictitem(data, key, '', 'F')
        if len(Ints[key]) > 0:
            NoInts = 0
            if int_key == "":
                int_key = key
    if NoInts == 1:
        print('No intensity information found')
        sys.exit()
    for rec in Ints[int_key]:
        if rec[plot_key] not in plotlist:
            plotlist.append(rec[plot_key])
    plotlist.sort()
    for plt in plotlist:
        print(plt)
        INTblock = []
        # get data with right intensity info whose plot_key matches plot
        data = pmag.get_dictitem(Ints[int_key], plot_key, plt, 'T')
        # get a list of specimens with appropriate data
        sids = pmag.get_specs(data)
        if len(sids) > 0:
            title = data[0][plot_key]
        for s in sids:
            INTblock = []
            # get data for each specimen
            if data_model == 3:
                sdata = pmag.get_dictitem(data, 'specimen', s, 'T')
            else:
                sdata = pmag.get_dictitem(data, 'er_specimen_name', s, 'T')
            for rec in sdata:
                INTblock.append([float(rec[dmag_key]), 0, 0,
                                 float(rec[int_key]), 1, 'g'])
            pmagplotlib.plotMT(FIG['exp'], INTblock, title, 0, units, norm)
        files = {}
        for key in list(FIG.keys()):
            files[key] = title + '_' + LP + '.' + fmt
        if plot == 0:
            pmagplotlib.drawFIGS(FIG)
            ans = input(" S[a]ve to save plot, [q]uit,  Return to continue:  ")
            if ans == 'q':
                sys.exit()
            if ans == "a":
                pmagplotlib.saveP(FIG, files)
        else:
            pmagplotlib.saveP(FIG, files)
        pmagplotlib.clearFIG(FIG['exp'])
Ejemplo n.º 8
0
def main():
    """
    NAME
        irmaq_magic.py

    DESCRIPTION
       plots IRM acquisition curves from measurements file

    SYNTAX
        irmaq_magic [command line options]

    INPUT
       takes magic formatted magic_measurements.txt files

    OPTIONS
        -h prints help message and quits
        -f FILE: specify input file, default is: magic_measurements.txt/measurements.txt
        -obj OBJ: specify  object  [loc, sit, sam, spc] for plot, default is by location
        -N ; do not normalize by last point - use original units
        -fmt [png,jpg,eps,pdf] set plot file format [default is svg]
        -sav save plot[s] and quit
        -DM MagIC data model number, default is 3
    NOTE
        loc: location (study); sit: site; sam: sample; spc: specimen
    """
    FIG = {}  # plot dictionary
    FIG['exp'] = 1  # exp is figure 1
    dir_path = './'
    plot, fmt = 0, 'svg'
    units = 'T',
    XLP = []
    norm = 1
    LP = "LP-IRM"
    if len(sys.argv) > 1:
        if '-h' in sys.argv:
            print(main.__doc__)
            sys.exit()
        data_model = int(pmag.get_named_arg_from_sys("-DM", 3))
        if '-N' in sys.argv:
            norm = 0
        if '-sav' in sys.argv:
            plot = 1
        if '-fmt' in sys.argv:
            ind = sys.argv.index("-fmt")
            fmt = sys.argv[ind + 1]
        if data_model == 3:
            in_file = pmag.get_named_arg_from_sys("-f", 'measurements.txt')
        else:
            in_file = pmag.get_named_arg_from_sys("-f",
                                                  'magic_measurements.txt')
        if '-WD' in sys.argv:
            ind = sys.argv.index('-WD')
            dir_path = sys.argv[ind + 1]
        dir_path = os.path.realpath(dir_path)
        in_file = pmag.resolve_file_name(in_file, dir_path)
        if '-WD' not in sys.argv:
            dir_path = os.path.split(in_file)[0]
        plot_by = pmag.get_named_arg_from_sys("-obj", "loc")
        if data_model == 3:
            plot_key = 'location'
            if plot_by == 'sit':
                plot_key = 'site'
            if plot_by == 'sam':
                plot_key = 'sample'
            if plot_by == 'spc':
                plot_key = 'specimen'
        else:
            plot_key = 'er_location_name'
            if plot_by == 'sit':
                plot_key = 'er_site_name'
            if plot_by == 'sam':
                plot_key = 'er_sample_name'
            if plot_by == 'spc':
                plot_key = 'er_specimen_name'

    # set defaults and get more information if needed
    if data_model == 3:
        dmag_key = 'treat_dc_field'
    else:
        dmag_key = 'treatment_dc_field'
    #
    if data_model == 3 and plot_key != 'specimen':
        # gonna need to read in more files
        print('-W- You are trying to plot measurements by {}'.format(plot_key))
        print(
            '    By default, this information is not available in your measurement file.'
        )
        print(
            '    Trying to acquire this information from {}'.format(dir_path))
        con = nb.Contribution(dir_path)
        meas_df = con.propagate_location_to_measurements()
        if plot_key not in meas_df.columns:
            print('-W- Could not find required data.')
            print('    Try a different plot key.')
            return
        else:
            print('-I- Found {} information, continuing with plotting'.format(
                plot_key))
        con.write_table_to_file('measurements')

    # read in data
    data, file_type = pmag.magic_read(in_file)
    sids = pmag.get_specs(data)
    pmagplotlib.plot_init(FIG['exp'], 6, 6)
    #
    #
    # find desired intensity data
    #
    # get plotlist
    #
    plotlist = []
    if data_model == 3:
        intlist = ['magn_moment', 'magn_volume', 'magn_mass', 'magnitude']
    else:
        intlist = [
            'measurement_magnitude', 'measurement_magn_moment',
            'measurement_magn_volume', 'measurement_magn_mass'
        ]
    IntMeths = []
    # get all the records with this lab protocol
    #print('data', len(data))
    #print('data[0]', data[0])
    if data_model == 3:
        data = pmag.get_dictitem(data, 'method_codes', LP, 'has')
    else:
        data = pmag.get_dictitem(data, 'magic_method_codes', LP, 'has')
    Ints = {}
    NoInts, int_key = 1, ""
    for key in intlist:
        # get all non-blank data for intensity type
        Ints[key] = pmag.get_dictitem(data, key, '', 'F')
        if len(Ints[key]) > 0:
            NoInts = 0
            if int_key == "":
                int_key = key
    if NoInts == 1:
        print('No intensity information found')
        sys.exit()
    for rec in Ints[int_key]:
        if rec[plot_key] not in plotlist:
            plotlist.append(rec[plot_key])
    plotlist.sort()
    for plt in plotlist:
        print(plt)
        INTblock = []
        # get data with right intensity info whose plot_key matches plot
        data = pmag.get_dictitem(Ints[int_key], plot_key, plt, 'T')
        # get a list of specimens with appropriate data
        sids = pmag.get_specs(data)
        if len(sids) > 0:
            title = data[0][plot_key]
        for s in sids:
            INTblock = []
            # get data for each specimen
            if data_model == 3:
                sdata = pmag.get_dictitem(data, 'specimen', s, 'T')
            else:
                sdata = pmag.get_dictitem(data, 'er_specimen_name', s, 'T')
            for rec in sdata:
                INTblock.append(
                    [float(rec[dmag_key]), 0, 0,
                     float(rec[int_key]), 1, 'g'])
            pmagplotlib.plotMT(FIG['exp'], INTblock, title, 0, units, norm)
        files = {}
        for key in list(FIG.keys()):
            files[key] = title + '_' + LP + '.' + fmt
        if plot == 0:
            pmagplotlib.drawFIGS(FIG)
            ans = input(" S[a]ve to save plot, [q]uit,  Return to continue:  ")
            if ans == 'q':
                sys.exit()
            if ans == "a":
                pmagplotlib.saveP(FIG, files)
        else:
            pmagplotlib.saveP(FIG, files)
        pmagplotlib.clearFIG(FIG['exp'])