def main():
    """
    NAME
        make_magic_plots.py

    DESCRIPTION
    inspects magic directory for available plots.

    SYNTAX
        make_magic_plots.py [command line options]

    INPUT
        magic files

    OPTIONS
        -h prints help message and quits
        -f FILE specifies input file name
        -fmt [png,eps,svg,jpg,pdf] specify format, default is png
    """
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    # reset log files
    for fname in ['log.txt', 'errors.txt']:
        f = os.path.join(os.getcwd(), fname)
        if os.path.exists(f):
            os.remove(f)
    dirlist = ['./']
    dir_path = os.getcwd()
    #
    if '-fmt' in sys.argv:
        ind = sys.argv.index("-fmt")
        fmt = sys.argv[ind + 1]
    else:
        fmt = 'png'
    if '-f' in sys.argv:
        ind = sys.argv.index("-f")
        filelist = [sys.argv[ind + 1]]
    else:
        filelist = os.listdir(dir_path)
    ## initialize some variables
    samp_file = 'samples.txt'
    azimuth_key = 'azimuth'
    meas_file = 'measurements.txt'
    loc_key = 'location'
    loc_file = 'locations.txt'
    method_key = 'method_codes'
    dec_key = 'dir_dec'
    inc_key = 'dir_inc'
    tilt_corr_key = "dir_tilt_correction"
    aniso_tilt_corr_key = "aniso_tilt_correction"
    hyst_bcr_key = "hyst_bcr"
    hyst_mr_key = "hyst_mr_moment"
    hyst_ms_key = "hyst_ms_moment"
    hyst_bc_key = "hyst_bc"
    Mkeys = ['magnitude', 'magn_moment', 'magn_volume', 'magn_mass']
    results_file = 'sites.txt'
    hyst_file = 'specimens.txt'
    aniso_file = 'specimens.txt'
    # create contribution and propagate data throughout
    con = cb.Contribution()
    con.propagate_location_to_measurements()
    con.propagate_location_to_specimens()
    con.propagate_location_to_samples()
    if not con.tables:
        print('-E- No MagIC tables could be found in this directory')
        error_log("No MagIC tables found")
        return
    # check to see if propagation worked, otherwise you can't plot by location
    lowest_table = None
    for table in con.ancestry:
        if table in con.tables:
            lowest_table = table
            break

    do_full_directory = False
    # check that locations propagated down to the lowest table in the contribution
    if 'location' in con.tables[lowest_table].df.columns:
        # are there any locations in the lowest table?
        if not all(con.tables[lowest_table].df['location'].isnull()):
            locs = con.tables['locations'].df.index.unique()
            lowest_locs = con.tables[lowest_table].df['location'].unique()
            incorrect_locs = set(lowest_locs).difference(set(locs))
            # are they actual locations?
            if not incorrect_locs:
                info_log(
                    'location names propagated to {}'.format(lowest_table))
            else:
                do_full_directory = True
                error_log('location names did not propagate fully to {} table'.
                          format(lowest_table))
        else:
            do_full_directory = True
            error_log(
                'could not propagate location names down to {} table'.format(
                    lowest_table))
    else:
        do_full_directory = True
        error_log('could not propagate location names down to {} table'.format(
            lowest_table))

    all_data = {}
    all_data['measurements'] = con.tables.get('measurements', None)
    all_data['specimens'] = con.tables.get('specimens', None)
    all_data['samples'] = con.tables.get('samples', None)
    all_data['sites'] = con.tables.get('sites', None)
    all_data['locations'] = con.tables.get('locations', None)
    locations = con.tables['locations'].df.index.unique()
    dirlist = [loc for loc in locations if cb.not_null(loc) and loc != 'nan']
    if not dirlist:
        dirlist = ["./"]
    if do_full_directory:
        dirlist = ["./"]

    # plot the whole contribution as one location
    if dirlist == ["./"]:
        error_log('plotting the entire contribution as one location')
        for fname in os.listdir("."):
            if fname.endswith(".txt"):
                shutil.copy(fname, "tmp_" + fname)

    # if possible, go through all data by location
    # use tmp_*.txt files to separate out by location

    for loc in dirlist:
        print('\nworking on: ', loc)

        def get_data(dtype, loc_name):
            """
            Extract data of type dtype for location loc_name.
            Write tmp_dtype.txt files if possible.
            """
            if cb.not_null(all_data[dtype]):
                data_container = all_data[dtype]
                data_df = data_container.df[data_container.df['location'] ==
                                            loc_name]
                data = data_container.convert_to_pmag_data_list(df=data_df)
                res = data_container.write_magic_file(
                    'tmp_{}.txt'.format(dtype), df=data_df)
                if not res:
                    return []
                return data

        meas_data = get_data('measurements', loc)
        spec_data = get_data('specimens', loc)
        samp_data = get_data('samples', loc)
        site_data = get_data('sites', loc)
        location_data = get_data('locations', loc)

        if loc == "./":  # if you can't sort by location, do everything together
            try:
                meas_data = con.tables[
                    'measurements'].convert_to_pmag_data_list()
            except KeyError:
                meas_data = None
            try:
                spec_data = con.tables['specimens'].convert_to_pmag_data_list()
            except KeyError:
                spec_data = None
            try:
                samp_data = con.tables['samples'].convert_to_pmag_data_list()
            except KeyError:
                samp_data = None
            try:
                site_data = con.tables['sites'].convert_to_pmag_data_list()
            except KeyError:
                site_data = None

        crd = 's'
        if samp_file in filelist:  # find coordinate systems
            samps = samp_data
            file_type = "samples"
            # get all non blank sample orientations
            Srecs = pmag.get_dictitem(samps, azimuth_key, '', 'F')
            if len(Srecs) > 0:
                crd = 'g'
                print('using geographic coordinates')
            else:
                print('using specimen coordinates')
        else:
            if VERBOSE:
                print('-I- No sample data found')
        if meas_file in filelist:  # start with measurement data
            print('working on measurements data')
            data = meas_data
            file_type = 'measurements'
            # looking for  zeq_magic possibilities
            # get all non blank method codes
            AFZrecs = pmag.get_dictitem(data, method_key, 'LT-AF-Z', 'has')
            # get all non blank method codes
            TZrecs = pmag.get_dictitem(data, method_key, 'LT-T-Z', 'has')
            # get all non blank method codes
            MZrecs = pmag.get_dictitem(data, method_key, 'LT-M-Z', 'has')
            # get all dec measurements
            Drecs = pmag.get_dictitem(data, dec_key, '', 'F')
            # get all inc measurements
            Irecs = pmag.get_dictitem(data, inc_key, '', 'F')
            for key in Mkeys:
                Mrecs = pmag.get_dictitem(data, key, '',
                                          'F')  # get intensity data
                if len(Mrecs) > 0:
                    break
            # potential for stepwise demag curves
            if len(AFZrecs) > 0 or len(TZrecs) > 0 or len(MZrecs) > 0 and len(
                    Drecs) > 0 and len(Irecs) > 0 and len(Mrecs) > 0:
                CMD = 'zeq_magic.py -f tmp_measurements.txt -fsp tmp_specimens.txt -fsa tmp_samples.txt -fsi tmp_sites.txt -sav -fmt ' + fmt + ' -crd ' + crd
                print(CMD)
                info_log(CMD, loc)
                os.system(CMD)
            # looking for  thellier_magic possibilities
            if len(pmag.get_dictitem(data, method_key, 'LP-PI-TRM',
                                     'has')) > 0:
                CMD = 'thellier_magic.py -f tmp_measurements.txt -fsp tmp_specimens.txt -sav -fmt ' + fmt
                print(CMD)
                info_log(CMD, loc)
                os.system(CMD)
            # looking for hysteresis possibilities
            if len(pmag.get_dictitem(data, method_key, 'LP-HYS',
                                     'has')) > 0:  # find hyst experiments
                # check for reqd columns
                missing = check_for_reqd_cols(data, ['treat_temp'])
                if missing:
                    error_log(
                        'LP-HYS method code present, but required column(s) [{}] missing'
                        .format(", ".join(missing)), loc, "quick_hyst.py")
                else:
                    CMD = 'quick_hyst.py -f tmp_measurements.txt -sav -fmt ' + fmt
                    print(CMD)
                    info_log(CMD, loc)
                    os.system(CMD)
            # equal area plots of directional data
            # at measurment level (by specimen)
            if data:
                missing = check_for_reqd_cols(data, ['dir_dec', 'dir_inc'])
                if not missing:
                    CMD = "eqarea_magic.py -f tmp_measurements.txt -obj spc -sav -no-tilt -fmt " + fmt
                    print(CMD)
                    os.system(CMD)
                    info_log(CMD, loc, "eqarea_magic.py")

        else:
            if VERBOSE:
                print('-I- No measurement data found')

        # site data
        if results_file in filelist:
            print('-I- result file found', results_file)
            data = site_data
            file_type = 'sites'
            print('-I- working on site directions')
            print('number of datapoints: ', len(data), loc)
            dec_key = 'dir_dec'
            inc_key = 'dir_inc'
            int_key = 'int_abs'
            SiteDIs = pmag.get_dictitem(data, dec_key, "", 'F')  # find decs
            SiteDIs = pmag.get_dictitem(SiteDIs, inc_key, "",
                                        'F')  # find decs and incs
            dir_data_found = len(SiteDIs)
            print('{} Dec/inc pairs found'.format(dir_data_found))
            # only individual results - not poles
            # get only individual results (if result_type col is available)
            if SiteDIs:
                if 'result_type' in SiteDIs[0]:
                    SiteDIs = pmag.get_dictitem(SiteDIs, 'result_type', 'i',
                                                'has')
                # then convert tilt_corr_key to correct format
                old_SiteDIs = SiteDIs
                SiteDIs = []
                for rec in old_SiteDIs:
                    if tilt_corr_key not in rec:
                        error_log(
                            "Directional data found, but missing {}, can't plot directions"
                            .format(tilt_corr_key), loc, "eqarea_magic.py")
                        break
                    if cb.is_null(
                            rec[tilt_corr_key]) and rec[tilt_corr_key] != 0:
                        rec[tilt_corr_key] = ""
                    else:
                        try:
                            rec[tilt_corr_key] = str(
                                int(float(rec[tilt_corr_key])))
                        except ValueError:
                            rec[tilt_corr_key] = ""
                    SiteDIs.append(rec)

                print('number of individual directions: ', len(SiteDIs))
                # tilt corrected coordinates
                SiteDIs_t = pmag.get_dictitem(SiteDIs,
                                              tilt_corr_key,
                                              '100',
                                              'T',
                                              float_to_int=True)
                print('number of tilt corrected directions: ', len(SiteDIs_t))
                SiteDIs_g = pmag.get_dictitem(
                    SiteDIs, tilt_corr_key, '0', 'T',
                    float_to_int=True)  # geographic coordinates
                print('number of geographic  directions: ', len(SiteDIs_g))
                SiteDIs_s = pmag.get_dictitem(
                    SiteDIs, tilt_corr_key, '-1', 'T',
                    float_to_int=True)  # sample coordinates
                print('number of sample  directions: ', len(SiteDIs_s))
                SiteDIs_x = pmag.get_dictitem(SiteDIs, tilt_corr_key, '',
                                              'T')  # no coordinates
                print('number of no coordinates  directions: ', len(SiteDIs_x))
                if len(SiteDIs_t) > 0 or len(SiteDIs_g) > 0 or len(
                        SiteDIs_s) > 0 or len(SiteDIs_x) > 0:
                    CRD = ""
                    if len(SiteDIs_t) > 0:
                        CRD = ' -crd t'
                    elif len(SiteDIs_g) > 0:
                        CRD = ' -crd g'
                    elif len(SiteDIs_s) > 0:
                        CRD = ' -crd s'
                    CMD = 'eqarea_magic.py -f tmp_sites.txt -fsp tmp_specimens.txt -fsa tmp_samples.txt -flo tmp_locations.txt -sav -fmt ' + fmt + CRD
                    print(CMD)
                    info_log(CMD, loc)
                    os.system(CMD)
                else:
                    if dir_data_found:
                        error_log(
                            '{} dec/inc pairs found, but no equal area plots were made'
                            .format(dir_data_found), loc, "equarea_magic.py")
            #
            print('-I- working on VGP map')
            VGPs = pmag.get_dictitem(SiteDIs, 'vgp_lat', "",
                                     'F')  # are there any VGPs?
            if len(VGPs) > 0:  # YES!
                CMD = 'vgpmap_magic.py -f tmp_sites.txt -prj moll -res c -sym ro 5 -sav -fmt png'
                print(CMD)
                info_log(CMD, loc, 'vgpmap_magic.py')
                os.system(CMD)
            else:
                print('-I- No vgps found')

            print('-I- Look for intensities')
            # is there any intensity data?
            if site_data:
                if int_key in site_data[0].keys():
                    # old way, wasn't working right:
                    #CMD = 'magic_select.py  -key ' + int_key + ' 0. has -F tmp1.txt -f tmp_sites.txt'
                    Selection = pmag.get_dictkey(site_data, int_key, dtype="f")
                    with open('intensities.txt', 'w') as out:
                        for rec in Selection:
                            if rec != 0:
                                out.write(str(rec * 1e6) + "\n")

                    histfile = 'LO:_' + loc + \
                        '_TY:_intensities_histogram:_.' + fmt
                    # maybe run histplot.main here instead, so you can return an error message
                    CMD = "histplot.py -b 1 -xlab 'Intensity (uT)' -sav -f intensities.txt -F " + histfile
                    os.system(CMD)
                    info_log(CMD, loc)
                    print(CMD)
                else:
                    print('-I- No intensities found')
            else:
                print('-I- No intensities found')

        ##
        if hyst_file in filelist:
            print('working on hysteresis', hyst_file)
            data = spec_data
            file_type = 'specimens'
            hdata = pmag.get_dictitem(data, hyst_bcr_key, '', 'F')
            hdata = pmag.get_dictitem(hdata, hyst_mr_key, '', 'F')
            hdata = pmag.get_dictitem(hdata, hyst_ms_key, '', 'F')
            # there are data for a dayplot
            hdata = pmag.get_dictitem(hdata, hyst_bc_key, '', 'F')
            if len(hdata) > 0:
                CMD = 'dayplot_magic.py -f tmp_specimens.txt -sav -fmt ' + fmt
                info_log(CMD, loc)
                print(CMD)
            else:
                print('no hysteresis data found')
        if aniso_file in filelist:  # do anisotropy plots if possible
            print('working on anisotropy', aniso_file)
            data = spec_data
            file_type = 'specimens'

            # make sure there is some anisotropy data
            if not data:
                print('No anisotropy data found')
            elif 'aniso_s' not in data[0]:
                print('No anisotropy data found')
            else:
                # get specimen coordinates
                if aniso_tilt_corr_key not in data[0]:
                    sdata = data
                else:
                    sdata = pmag.get_dictitem(data,
                                              aniso_tilt_corr_key,
                                              '-1',
                                              'T',
                                              float_to_int=True)
                # get specimen coordinates
                gdata = pmag.get_dictitem(data,
                                          aniso_tilt_corr_key,
                                          '0',
                                          'T',
                                          float_to_int=True)
                # get specimen coordinates
                tdata = pmag.get_dictitem(data,
                                          aniso_tilt_corr_key,
                                          '100',
                                          'T',
                                          float_to_int=True)
                CRD = ""
                CMD = 'aniso_magic.py -x -B -sav -fmt ' + fmt
                if len(sdata) > 3:
                    CMD = CMD + ' -crd s'
                    print(CMD)
                    info_log(CMD, loc)
                    os.system(CMD)
                if len(gdata) > 3:
                    CMD = CMD + ' -crd g'
                    print(CMD)
                    info_log(CMD, loc)
                    os.system(CMD)
                if len(tdata) > 3:
                    CMD = CMD + ' -crd t'
                    print(CMD)
                    info_log(CMD, loc)
                    os.system(CMD)
        # remove temporary files
        for fname in glob.glob('tmp*.txt'):
            os.remove(fname)
        try:
            os.remove('intensities.txt')
        except FileNotFoundError:
            pass
    if loc_file in filelist:
        data, file_type = pmag.magic_read(loc_file)  # read in location data
        print('-I- working on pole map')
        poles = pmag.get_dictitem(data, 'pole_lat', "",
                                  'F')  # are there any poles?
        poles = pmag.get_dictitem(poles, 'pole_lon', "",
                                  'F')  # are there any poles?
        if len(poles) > 0:  # YES!
            CMD = 'polemap_magic.py -sav -fmt png'
            print(CMD)
            info_log(CMD, "all locations", "polemap_magic.py")
            os.system(CMD)
        else:
            print('-I- No poles found')
def main():
    """
    NAME
        make_magic_plots.py

    DESCRIPTION
 	inspects magic directory for available plots.

    SYNTAX
        make_magic_plots.py [command line options]

    INPUT
        magic files

    OPTIONS
        -h prints help message and quits
        -f FILE specifies input file name
        -fmt [png,eps,svg,jpg,pdf] specify format, default is png
    """
    dirlist=['./']
    dir_path=os.getcwd()
    names=os.listdir(dir_path)
    for n in names:
        if 'Location' in n:
            dirlist.append(n)
    if '-fmt' in sys.argv:
        ind=sys.argv.index("-fmt")
        fmt=sys.argv[ind+1]
    else: fmt='png'
    if '-f' in sys.argv:
        ind=sys.argv.index("-f")
        filelist=[sys.argv[ind+1]]
    else:
        filelist=os.listdir(dir_path)
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    for loc in dirlist:
        print 'working on: ',loc
        os.chdir(loc) # change working directories to each location
        crd='s'
        if 'er_samples.txt' in filelist: # find coordinate systems
            samps,file_type=pmag.magic_read('er_samples.txt') # read in data
            Srecs=pmag.get_dictitem(samps,'sample_azimuth','','F')# get all none blank sample orientations
            if len(Srecs)>0: 
                crd='g'
        if 'magic_measurements.txt' in filelist: # start with measurement data
            print 'working on measurements data'
            data,file_type=pmag.magic_read('magic_measurements.txt') # read in data
            if loc == './': data=pmag.get_dictitem(data,'er_location_name','','T') # get all the blank location names from data file
            # looking for  zeq_magic possibilities
            AFZrecs=pmag.get_dictitem(data,'magic_method_codes','LT-AF-Z','has')# get all none blank method codes
            TZrecs=pmag.get_dictitem(data,'magic_method_codes','LT-T-Z','has')# get all none blank method codes
            MZrecs=pmag.get_dictitem(data,'magic_method_codes','LT-M-Z','has')# get all none blank method codes
            Drecs=pmag.get_dictitem(data,'measurement_dec','','F') # get all dec measurements
            Irecs=pmag.get_dictitem(data,'measurement_inc','','F') # get all dec measurements
            Mkeys=['measurement_magnitude','measurement_magn_moment','measurement_magn_volume','measurement_magn_mass']
            for key in Mkeys:
                Mrecs=pmag.get_dictitem(data,key,'','F') # get intensity data
                if len(Mrecs)>0:break
            if len(AFZrecs)>0 or len(TZrecs)>0 or len(MZrecs)>0 and len(Drecs)>0 and len(Irecs)>0 and len(Mrecs)>0: # potential for stepwise demag curves 
                print 'zeq_magic.py -fsp pmag_specimens.txt -sav -fmt '+fmt+' -crd '+crd
                os.system('zeq_magic.py -sav -fmt '+fmt+' -crd '+crd )
            # looking for  thellier_magic possibilities
            if len(pmag.get_dictitem(data,'magic_method_codes','LP-PI-TRM','has'))>0:
                print 'thellier_magic.py -fsp pmag_specimens.txt -sav -fmt '+fmt
                os.system('thellier_magic.py -sav -fmt '+fmt)
            # looking for hysteresis possibilities
            if len(pmag.get_dictitem(data,'magic_method_codes','LP-HYS','has'))>0: # find hyst experiments
                print 'quick_hyst.py -sav -fmt '+fmt
                os.system('quick_hyst.py -sav -fmt '+fmt)
        if 'pmag_results.txt' in filelist: # start with measurement data
            data,file_type=pmag.magic_read('pmag_results.txt') # read in data
            print 'number of datapoints: ',len(data) 
            if loc == './': data=pmag.get_dictitem(data,'er_location_names',':','has') # get all the concatenated location names from data file
            print 'number of datapoints: ',len(data) ,loc
            print 'working on pmag_results directions'
            SiteDIs=pmag.get_dictitem(data,'average_dec',"",'F') # find decs
            print 'number of directions: ',len(SiteDIs) 
            SiteDIs=pmag.get_dictitem(SiteDIs,'average_inc',"",'F') # find decs and incs
            print 'number of directions: ',len(SiteDIs) 
            SiteDIs=pmag.get_dictitem(SiteDIs,'data_type','i','has') # only individual results - not poles
            print 'number of directions: ',len(SiteDIs) 
            SiteDIs_t=pmag.get_dictitem(SiteDIs,'tilt_correction','100','T')# tilt corrected coordinates
            print 'number of directions: ',len(SiteDIs) 
            if len(SiteDIs_t)>0:
                print 'eqarea_magic.py -sav -crd t -fmt '+fmt
                os.system('eqarea_magic.py -sav -crd t -fmt '+fmt)
            elif len(SiteDIs)>0 and 'tilt_correction' not in SiteDIs[0].keys():
                print 'eqarea_magic.py -sav -fmt '+fmt
                os.system('eqarea_magic.py -sav -fmt '+fmt)
            else:
                SiteDIs_g=pmag.get_dictitem(SiteDIs,'tilt_correction','0','T')# geographic coordinates
                if len(SiteDIs_g)>0:
                    print 'eqarea_magic.py -sav -crd g -fmt '+fmt
                    os.system('eqarea_magic.py -sav -crd g -fmt '+fmt)
                else:
                    SiteDIs_s=pmag.get_dictitem(SiteDIs,'tilt_correction','-1','T')# sample coordinates
                    if len(SiteDIs_s)>0:
                        print 'eqarea_magic.py -sav -crd s -fmt '+fmt
                        os.system('eqarea_magic.py -sav -crd s -fmt '+fmt)
                    else:
                        SiteDIs_x=pmag.get_dictitem(SiteDIs,'tilt_correction','','T')# no coordinates
                        if len(SiteDIs_x)>0:
                            print 'eqarea_magic.py -sav -fmt '+fmt
                            os.system('eqarea_magic.py -sav -fmt '+fmt)
            print 'working on pmag_results VGP map'
            VGPs=pmag.get_dictitem(SiteDIs,'vgp_lat',"",'F') # are there any VGPs?   
            if len(VGPs)>0:  # YES!  
                os.system('vgpmap_magic.py -prj moll -res c -sym ro 5 -sav -fmt png')
            print 'working on pmag_results intensities'
            os.system('magic_select.py -f pmag_results.txt -key data_type i T -F tmp.txt')
            os.system('magic_select.py -f tmp.txt -key average_int 0. has -F tmp1.txt')
            os.system("grab_magic_key.py -f tmp1.txt -key average_int | awk '{print $1*1e6}' >tmp2.txt")
            data,file_type=pmag.magic_read('tmp1.txt') # read in data
            locations=pmag.get_dictkey(data,'er_location_names',"")
            histfile='LO:_'+locations[0]+'_intensities_histogram:_.'+fmt
            os.system("histplot.py -b 1 -xlab 'Intensity (uT)' -sav -f tmp2.txt -F " +histfile)
            print "histplot.py -b 1 -xlab 'Intensity (uT)' -sav -f tmp2.txt -F " +histfile
            os.system('rm tmp*.txt')
        if 'rmag_hysteresis.txt' in filelist: # start with measurement data
            print 'working on rmag_hysteresis'
            data,file_type=pmag.magic_read('rmag_hysteresis.txt') # read in data
            if loc == './': data=pmag.get_dictitem(data,'er_location_name','','T') # get all the blank location names from data file
            hdata=pmag.get_dictitem(data,'hysteresis_bcr','','F')
            hdata=pmag.get_dictitem(hdata,'hysteresis_mr_moment','','F')
            hdata=pmag.get_dictitem(hdata,'hysteresis_ms_moment','','F')
            hdata=pmag.get_dictitem(hdata,'hysteresis_bc','','F') # there are data for a dayplot
            if len(hdata)>0:
                print 'dayplot_magic.py -sav -fmt '+fmt
                os.system('dayplot_magic.py -sav -fmt '+fmt) 
    #if 'er_sites.txt' in filelist: # start with measurement data
        #    print 'working on er_sites'
            #os.system('basemap_magic.py -sav -fmt '+fmt)
        if 'rmag_anisotropy.txt' in filelist: # do anisotropy plots if possible
            print 'working on rmag_anisotropy'
            data,file_type=pmag.magic_read('rmag_anisotropy.txt') # read in data
            if loc == './': data=pmag.get_dictitem(data,'er_location_name','','T') # get all the blank location names from data file
            sdata=pmag.get_dictitem(data,'anisotropy_tilt_correction','-1','T') # get specimen coordinates
            gdata=pmag.get_dictitem(data,'anisotropy_tilt_correction','0','T') # get specimen coordinates
            tdata=pmag.get_dictitem(data,'anisotropy_tilt_correction','100','T') # get specimen coordinates
            if len(sdata)>3:
                print 'aniso_magic.py -x -B -crd s -sav -fmt '+fmt
                os.system('aniso_magic.py -x -B -crd s -sav -fmt '+fmt)
            if len(gdata)>3:
                os.system('aniso_magic.py -x -B -crd g -sav -fmt '+fmt)
            if len(tdata)>3:
                os.system('aniso_magic.py -x -B -crd t -sav -fmt '+fmt)
        if loc!='./':os.chdir('..') # change working directories to each location
def main():
    """
    NAME
        make_magic_plots.py

    DESCRIPTION
 	inspects magic directory for available plots.

    SYNTAX
        make_magic_plots.py [command line options]

    INPUT
        magic files

    OPTIONS
        -h prints help message and quits
        -f FILE specifies input file name
        -fmt [png,eps,svg,jpg,pdf] specify format, default is png
    """
    dirlist = ['./']
    dir_path = os.getcwd()
    names = os.listdir(dir_path)
    for n in names:
        if 'Location' in n:
            dirlist.append(n)
    if '-fmt' in sys.argv:
        ind = sys.argv.index("-fmt")
        fmt = sys.argv[ind + 1]
    else:
        fmt = 'png'
    if '-f' in sys.argv:
        ind = sys.argv.index("-f")
        filelist = [sys.argv[ind + 1]]
    else:
        filelist = os.listdir(dir_path)
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    for loc in dirlist:
        print('working on: ', loc)
        os.chdir(loc)  # change working directories to each location
        crd = 's'
        if 'er_samples.txt' in filelist:  # find coordinate systems
            samps, file_type = pmag.magic_read(
                'er_samples.txt')  # read in data
            Srecs = pmag.get_dictitem(
                samps, 'sample_azimuth', '',
                'F')  # get all none blank sample orientations
            if len(Srecs) > 0:
                crd = 'g'
        if 'magic_measurements.txt' in filelist:  # start with measurement data
            print('working on measurements data')
            data, file_type = pmag.magic_read(
                'magic_measurements.txt')  # read in data
            if loc == './':
                data = pmag.get_dictitem(
                    data, 'er_location_name', '',
                    'T')  # get all the blank location names from data file
            # looking for  zeq_magic possibilities
            AFZrecs = pmag.get_dictitem(
                data, 'magic_method_codes', 'LT-AF-Z',
                'has')  # get all none blank method codes
            TZrecs = pmag.get_dictitem(
                data, 'magic_method_codes', 'LT-T-Z',
                'has')  # get all none blank method codes
            MZrecs = pmag.get_dictitem(
                data, 'magic_method_codes', 'LT-M-Z',
                'has')  # get all none blank method codes
            Drecs = pmag.get_dictitem(data, 'measurement_dec', '',
                                      'F')  # get all dec measurements
            Irecs = pmag.get_dictitem(data, 'measurement_inc', '',
                                      'F')  # get all dec measurements
            Mkeys = [
                'measurement_magnitude', 'measurement_magn_moment',
                'measurement_magn_volume', 'measurement_magn_mass'
            ]
            for key in Mkeys:
                Mrecs = pmag.get_dictitem(data, key, '',
                                          'F')  # get intensity data
                if len(Mrecs) > 0: break
            if len(AFZrecs) > 0 or len(TZrecs) > 0 or len(MZrecs) > 0 and len(
                    Drecs) > 0 and len(Irecs) > 0 and len(
                        Mrecs) > 0:  # potential for stepwise demag curves
                print('zeq_magic.py -fsp pmag_specimens.txt -sav -fmt ' + fmt +
                      ' -crd ' + crd)
                os.system('zeq_magic.py -sav -fmt ' + fmt + ' -crd ' + crd)
            # looking for  thellier_magic possibilities
            if len(
                    pmag.get_dictitem(data, 'magic_method_codes', 'LP-PI-TRM',
                                      'has')) > 0:
                print('thellier_magic.py -fsp pmag_specimens.txt -sav -fmt ' +
                      fmt)
                os.system('thellier_magic.py -sav -fmt ' + fmt)
            # looking for hysteresis possibilities
            if len(
                    pmag.get_dictitem(data, 'magic_method_codes', 'LP-HYS',
                                      'has')) > 0:  # find hyst experiments
                print('quick_hyst.py -sav -fmt ' + fmt)
                os.system('quick_hyst.py -sav -fmt ' + fmt)
        if 'pmag_results.txt' in filelist:  # start with measurement data
            data, file_type = pmag.magic_read(
                'pmag_results.txt')  # read in data
            print('number of datapoints: ', len(data))
            if loc == './':
                data = pmag.get_dictitem(
                    data, 'er_location_names', ':', 'has'
                )  # get all the concatenated location names from data file
            print('number of datapoints: ', len(data), loc)
            print('working on pmag_results directions')
            SiteDIs = pmag.get_dictitem(data, 'average_dec', "",
                                        'F')  # find decs
            print('number of directions: ', len(SiteDIs))
            SiteDIs = pmag.get_dictitem(SiteDIs, 'average_inc', "",
                                        'F')  # find decs and incs
            print('number of directions: ', len(SiteDIs))
            SiteDIs = pmag.get_dictitem(
                SiteDIs, 'data_type', 'i',
                'has')  # only individual results - not poles
            print('number of directions: ', len(SiteDIs))
            SiteDIs_t = pmag.get_dictitem(SiteDIs, 'tilt_correction', '100',
                                          'T')  # tilt corrected coordinates
            print('number of directions: ', len(SiteDIs))
            if len(SiteDIs_t) > 0:
                print('eqarea_magic.py -sav -crd t -fmt ' + fmt)
                os.system('eqarea_magic.py -sav -crd t -fmt ' + fmt)
            elif len(SiteDIs) > 0 and 'tilt_correction' not in SiteDIs[0].keys(
            ):
                print('eqarea_magic.py -sav -fmt ' + fmt)
                os.system('eqarea_magic.py -sav -fmt ' + fmt)
            else:
                SiteDIs_g = pmag.get_dictitem(SiteDIs, 'tilt_correction', '0',
                                              'T')  # geographic coordinates
                if len(SiteDIs_g) > 0:
                    print('eqarea_magic.py -sav -crd g -fmt ' + fmt)
                    os.system('eqarea_magic.py -sav -crd g -fmt ' + fmt)
                else:
                    SiteDIs_s = pmag.get_dictitem(SiteDIs, 'tilt_correction',
                                                  '-1',
                                                  'T')  # sample coordinates
                    if len(SiteDIs_s) > 0:
                        print('eqarea_magic.py -sav -crd s -fmt ' + fmt)
                        os.system('eqarea_magic.py -sav -crd s -fmt ' + fmt)
                    else:
                        SiteDIs_x = pmag.get_dictitem(SiteDIs,
                                                      'tilt_correction', '',
                                                      'T')  # no coordinates
                        if len(SiteDIs_x) > 0:
                            print('eqarea_magic.py -sav -fmt ' + fmt)
                            os.system('eqarea_magic.py -sav -fmt ' + fmt)
            print('working on pmag_results VGP map')
            VGPs = pmag.get_dictitem(SiteDIs, 'vgp_lat', "",
                                     'F')  # are there any VGPs?
            if len(VGPs) > 0:  # YES!
                os.system(
                    'vgpmap_magic.py -prj moll -res c -sym ro 5 -sav -fmt png')
            print('working on pmag_results intensities')
            os.system(
                'magic_select.py -f pmag_results.txt -key data_type i T -F tmp.txt'
            )
            os.system(
                'magic_select.py -f tmp.txt -key average_int 0. has -F tmp1.txt'
            )
            os.system(
                "grab_magic_key.py -f tmp1.txt -key average_int | awk '{print $1*1e6}' >tmp2.txt"
            )
            data, file_type = pmag.magic_read('tmp1.txt')  # read in data
            locations = pmag.get_dictkey(data, 'er_location_names', "")
            histfile = 'LO:_' + locations[0] + '_intensities_histogram:_.' + fmt
            os.system(
                "histplot.py -b 1 -xlab 'Intensity (uT)' -sav -f tmp2.txt -F "
                + histfile)
            print(
                "histplot.py -b 1 -xlab 'Intensity (uT)' -sav -f tmp2.txt -F "
                + histfile)
            os.system('rm tmp*.txt')
        if 'rmag_hysteresis.txt' in filelist:  # start with measurement data
            print('working on rmag_hysteresis')
            data, file_type = pmag.magic_read(
                'rmag_hysteresis.txt')  # read in data
            if loc == './':
                data = pmag.get_dictitem(
                    data, 'er_location_name', '',
                    'T')  # get all the blank location names from data file
            hdata = pmag.get_dictitem(data, 'hysteresis_bcr', '', 'F')
            hdata = pmag.get_dictitem(hdata, 'hysteresis_mr_moment', '', 'F')
            hdata = pmag.get_dictitem(hdata, 'hysteresis_ms_moment', '', 'F')
            hdata = pmag.get_dictitem(hdata, 'hysteresis_bc', '',
                                      'F')  # there are data for a dayplot
            if len(hdata) > 0:
                print('dayplot_magic.py -sav -fmt ' + fmt)
                os.system('dayplot_magic.py -sav -fmt ' + fmt)
    #if 'er_sites.txt' in filelist: # start with measurement data
    #    print 'working on er_sites'
    #os.system('basemap_magic.py -sav -fmt '+fmt)
        if 'rmag_anisotropy.txt' in filelist:  # do anisotropy plots if possible
            print('working on rmag_anisotropy')
            data, file_type = pmag.magic_read(
                'rmag_anisotropy.txt')  # read in data
            if loc == './':
                data = pmag.get_dictitem(
                    data, 'er_location_name', '',
                    'T')  # get all the blank location names from data file
            sdata = pmag.get_dictitem(data, 'anisotropy_tilt_correction', '-1',
                                      'T')  # get specimen coordinates
            gdata = pmag.get_dictitem(data, 'anisotropy_tilt_correction', '0',
                                      'T')  # get specimen coordinates
            tdata = pmag.get_dictitem(data, 'anisotropy_tilt_correction',
                                      '100', 'T')  # get specimen coordinates
            if len(sdata) > 3:
                print('aniso_magic.py -x -B -crd s -sav -fmt ' + fmt)
                os.system('aniso_magic.py -x -B -crd s -sav -fmt ' + fmt)
            if len(gdata) > 3:
                os.system('aniso_magic.py -x -B -crd g -sav -fmt ' + fmt)
            if len(tdata) > 3:
                os.system('aniso_magic.py -x -B -crd t -sav -fmt ' + fmt)
        if loc != './':
            os.chdir('..')  # change working directories to each location
Exemple #4
0
def main():
    """
    NAME
        lowrie.py

    DESCRIPTION
       plots intensity decay curves for Lowrie experiments

    SYNTAX 
        lowrie -h [command line options]
    
    INPUT 
       takes SIO formatted input files
    
    OPTIONS
        -h prints help message and quits
        -f FILE: specify input file
        -N do not normalize by maximum magnetization
        -fmt [svg, pdf, eps, png] specify fmt, default is svg
        -sav save plots and quit
    """
    fmt, plot = "svg", 0
    FIG = {}  # plot dictionary
    FIG["lowrie"] = 1  # demag is figure 1
    pmagplotlib.plot_init(FIG["lowrie"], 6, 6)
    norm = 1  # default is to normalize by maximum axis
    if len(sys.argv) > 1:
        if "-h" in sys.argv:
            print main.__doc__
            sys.exit()
        if "-N" in sys.argv:
            norm = 0  # don't normalize
        if "-sav" in sys.argv:
            plot = 1  # don't normalize
        if "-fmt" in sys.argv:  # sets input filename
            ind = sys.argv.index("-fmt")
            fmt = sys.argv[ind + 1]
        if "-f" in sys.argv:  # sets input filename
            ind = sys.argv.index("-f")
            in_file = sys.argv[ind + 1]
        else:
            print main.__doc__
            print "you must supply a file name"
            sys.exit()
    else:
        print main.__doc__
        print "you must supply a file name"
        sys.exit()
    data = open(in_file).readlines()  # open the SIO format file
    PmagRecs = []  # set up a list for the results
    keys = ["specimen", "treatment", "csd", "M", "dec", "inc"]
    for line in data:
        PmagRec = {}
        rec = line.replace("\n", "").split()
        for k in range(len(keys)):
            PmagRec[keys[k]] = rec[k]
        PmagRecs.append(PmagRec)
    specs = pmag.get_dictkey(PmagRecs, "specimen", "")
    sids = []
    for spec in specs:
        if spec not in sids:
            sids.append(spec)  # get list of unique specimen names
    for spc in sids:  # step through the specimen names
        print spc
        specdata = pmag.get_dictitem(PmagRecs, "specimen", spc, "T")  # get all this one's data
        DIMs, Temps = [], []
        for dat in specdata:  # step through the data
            DIMs.append([float(dat["dec"]), float(dat["inc"]), float(dat["M"]) * 1e-3])
            Temps.append(float(dat["treatment"]))
        carts = pmag.dir2cart(DIMs).transpose()
        # if norm==1: # want to normalize
        #    nrm=max(max(abs(carts[0])),max(abs(carts[1])),max(abs(carts[2]))) # by maximum of x,y,z values
        #    ylab="M/M_max"
        if norm == 1:  # want to normalize
            nrm = DIMs[0][2]  # normalize by NRM
            ylab = "M/M_o"
        else:
            nrm = 1.0  # don't normalize
            ylab = "Magnetic moment (Am^2)"
        xlab = "Temperature (C)"
        pmagplotlib.plotXY(FIG["lowrie"], Temps, abs(carts[0]) / nrm, sym="r-")
        pmagplotlib.plotXY(FIG["lowrie"], Temps, abs(carts[0]) / nrm, sym="ro")  # X direction
        pmagplotlib.plotXY(FIG["lowrie"], Temps, abs(carts[1]) / nrm, sym="c-")
        pmagplotlib.plotXY(FIG["lowrie"], Temps, abs(carts[1]) / nrm, sym="cs")  # Y direction
        pmagplotlib.plotXY(FIG["lowrie"], Temps, abs(carts[2]) / nrm, sym="k-")
        pmagplotlib.plotXY(
            FIG["lowrie"], Temps, abs(carts[2]) / nrm, sym="k^", title=spc, xlab=xlab, ylab=ylab
        )  # Z direction
        files = {"lowrie": "lowrie:_" + spc + "_." + fmt}
        if plot == 0:
            pmagplotlib.drawFIGS(FIG)
            ans = raw_input("S[a]ve figure? [q]uit, <return> to continue   ")
            if ans == "a":
                pmagplotlib.saveP(FIG, files)
            elif ans == "q":
                sys.exit()
        else:
            pmagplotlib.saveP(FIG, files)
        pmagplotlib.clearFIG(FIG["lowrie"])
Exemple #5
0
def main():
    """
    NAME
        sites_locations.py

    DESCRIPTION
        reads in er_sites.txt file and finds all locations and bounds of locations
        outputs er_locations.txt file

    SYNTAX
        sites_locations.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f: specimen input er_sites format file, default is "er_sites.txt"
        -F: locations table: default is "er_locations.txt"
    """
    # set defaults
    site_file = "er_sites.txt"
    loc_file = "er_locations.txt"
    Names, user = [], "unknown"
    Done = []
    version_num = pmag.get_version()
    args = sys.argv
    dir_path = '.'
    # get command line stuff
    if '-WD' in args:
        ind = args.index("-WD")
        dir_path = args[ind + 1]
    if "-h" in args:
        print(main.__doc__)
        sys.exit()
    if '-f' in args:
        ind = args.index("-f")
        site_file = args[ind + 1]
    if '-F' in args:
        ind = args.index("-F")
        loc_file = args[ind + 1]
    #
    site_file = dir_path + '/' + site_file
    loc_file = dir_path + '/' + loc_file
    Sites, file_type = pmag.magic_read(site_file)
    if file_type != 'er_sites':
        print(file_type)
        print(file_type, "This is not a valid er_sites file ")
        sys.exit()
    # read in site data
    #
    LocNames, Locations = [], []
    for site in Sites:
        if site['er_location_name'] not in LocNames:  # new location name
            LocNames.append(site['er_location_name'])
            sites_locs = pmag.get_dictitem(Sites, 'er_location_name',
                                           site['er_location_name'],
                                           'T')  # get all sites for this loc
            lats = pmag.get_dictkey(sites_locs, 'site_lat',
                                    'f')  # get all the latitudes as floats
            lons = pmag.get_dictkey(sites_locs, 'site_lon',
                                    'f')  # get all the longitudes as floats
            LocRec = {
                'er_citation_names': 'This study',
                'er_location_name': site['er_location_name'],
                'location_type': ''
            }
            LocRec['location_begin_lat'] = str(min(lats))
            LocRec['location_end_lat'] = str(max(lats))
            LocRec['location_begin_lon'] = str(min(lons))
            LocRec['location_end_lon'] = str(max(lons))
            Locations.append(LocRec)
    if len(Locations) > 0:
        pmag.magic_write(loc_file, Locations, "er_locations")
        print("Locations written to: ", loc_file)
Exemple #6
0
def main():
    """
    NAME
        lowrie.py

    DESCRIPTION
       plots intensity decay curves for Lowrie experiments

    SYNTAX
        lowrie -h [command line options]

    INPUT
       takes SIO formatted input files

    OPTIONS
        -h prints help message and quits
        -f FILE: specify input file
        -N do not normalize by maximum magnetization
        -fmt [svg, pdf, eps, png] specify fmt, default is svg
        -sav save plots and quit
    """
    fmt, plot = 'svg', 0
    FIG = {}  # plot dictionary
    FIG['lowrie'] = 1  # demag is figure 1
    pmagplotlib.plot_init(FIG['lowrie'], 6, 6)
    norm = 1  # default is to normalize by maximum axis
    if len(sys.argv) > 1:
        if '-h' in sys.argv:
            print(main.__doc__)
            sys.exit()
        if '-N' in sys.argv:
            norm = 0  # don't normalize
        if '-sav' in sys.argv:
            plot = 1  # don't normalize
        if '-fmt' in sys.argv:  # sets input filename
            ind = sys.argv.index("-fmt")
            fmt = sys.argv[ind + 1]
        if '-f' in sys.argv:  # sets input filename
            ind = sys.argv.index("-f")
            in_file = sys.argv[ind + 1]
        else:
            print(main.__doc__)
            print('you must supply a file name')
            sys.exit()
    else:
        print(main.__doc__)
        print('you must supply a file name')
        sys.exit()
    data = pmag.open_file(in_file)
    PmagRecs = []  # set up a list for the results
    keys = ['specimen', 'treatment', 'csd', 'M', 'dec', 'inc']
    for line in data:
        PmagRec = {}
        rec = line.replace('\n', '').split()
        for k in range(len(keys)):
            PmagRec[keys[k]] = rec[k]
        PmagRecs.append(PmagRec)
    specs = pmag.get_dictkey(PmagRecs, 'specimen', '')
    sids = []
    for spec in specs:
        if spec not in sids:
            sids.append(spec)  # get list of unique specimen names
    for spc in sids:  # step through the specimen names
        print(spc)
        specdata = pmag.get_dictitem(PmagRecs, 'specimen', spc,
                                     'T')  # get all this one's data
        DIMs, Temps = [], []
        for dat in specdata:  # step through the data
            DIMs.append(
                [float(dat['dec']),
                 float(dat['inc']),
                 float(dat['M']) * 1e-3])
            Temps.append(float(dat['treatment']))
        carts = pmag.dir2cart(DIMs).transpose()
        # if norm==1: # want to normalize
        #    nrm=max(max(abs(carts[0])),max(abs(carts[1])),max(abs(carts[2]))) # by maximum of x,y,z values
        #    ylab="M/M_max"
        if norm == 1:  # want to normalize
            nrm = (DIMs[0][2])  # normalize by NRM
            ylab = "M/M_o"
        else:
            nrm = 1.  # don't normalize
            ylab = "Magnetic moment (Am^2)"
        xlab = "Temperature (C)"
        pmagplotlib.plotXY(FIG['lowrie'],
                           Temps,
                           old_div(abs(carts[0]), nrm),
                           sym='r-')
        pmagplotlib.plotXY(FIG['lowrie'],
                           Temps,
                           old_div(abs(carts[0]), nrm),
                           sym='ro')  # X direction
        pmagplotlib.plotXY(FIG['lowrie'],
                           Temps,
                           old_div(abs(carts[1]), nrm),
                           sym='c-')
        pmagplotlib.plotXY(FIG['lowrie'],
                           Temps,
                           old_div(abs(carts[1]), nrm),
                           sym='cs')  # Y direction
        pmagplotlib.plotXY(FIG['lowrie'],
                           Temps,
                           old_div(abs(carts[2]), nrm),
                           sym='k-')
        pmagplotlib.plotXY(FIG['lowrie'],
                           Temps,
                           old_div(abs(carts[2]), nrm),
                           sym='k^',
                           title=spc,
                           xlab=xlab,
                           ylab=ylab)  # Z direction
        files = {'lowrie': 'lowrie:_' + spc + '_.' + fmt}
        if plot == 0:
            pmagplotlib.drawFIGS(FIG)
            ans = input('S[a]ve figure? [q]uit, <return> to continue   ')
            if ans == 'a':
                pmagplotlib.saveP(FIG, files)
            elif ans == 'q':
                sys.exit()
        else:
            pmagplotlib.saveP(FIG, files)
        pmagplotlib.clearFIG(FIG['lowrie'])
def main():
    """
    NAME
        make_magic_plots.py

    DESCRIPTION
        inspects magic directory for available data and makes plots

    SYNTAX
        make_magic_plots.py [command line options]

    INPUT
        magic files

    OPTIONS
        -h prints help message and quits
        -f FILE specifies input file name
        -fmt [png,eps,svg,jpg,pdf] specify format, default is png
    """
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    # reset log files
    for fname in ['log.txt', 'errors.txt']:
        f = os.path.join(os.getcwd(), fname)
        if os.path.exists(f):
            os.remove(f)
    image_recs = []
    dirlist = ['./']
    dir_path = os.getcwd()
    #
    if '-fmt' in sys.argv:
        ind = sys.argv.index("-fmt")
        fmt = sys.argv[ind + 1]
    else:
        fmt = 'png'
    if '-f' in sys.argv:
        ind = sys.argv.index("-f")
        filelist = [sys.argv[ind + 1]]
    else:
        filelist = os.listdir(dir_path)
    ## initialize some variables
    samp_file = 'samples.txt'
    meas_file = 'measurements.txt'
    #loc_key = 'location'
    loc_file = 'locations.txt'
    method_key = 'method_codes'
    dec_key = 'dir_dec'
    inc_key = 'dir_inc'
    tilt_corr_key = "dir_tilt_correction"
    aniso_tilt_corr_key = "aniso_tilt_correction"
    hyst_bcr_key = "hyst_bcr"
    hyst_mr_key = "hyst_mr_moment"
    hyst_ms_key = "hyst_ms_moment"
    hyst_bc_key = "hyst_bc"
    Mkeys = ['magnitude', 'magn_moment', 'magn_volume', 'magn_mass']
    results_file = 'sites.txt'
    hyst_file = 'specimens.txt'
    aniso_file = 'specimens.txt'
    # create contribution and propagate data throughout
    full_con = cb.Contribution()
    full_con.propagate_location_to_measurements()
    full_con.propagate_location_to_specimens()
    full_con.propagate_location_to_samples()
    if not full_con.tables:
        print('-E- No MagIC tables could be found in this directory')
        error_log("No MagIC tables found")
        return
    # try to get the contribution id for error logging
    con_id = ""
    if 'contribution' in full_con.tables:
        if 'id' in full_con.tables['contribution'].df.columns:
            con_id = full_con.tables['contribution'].df.iloc[0]['id']
    # check to see if propagation worked, otherwise you can't plot by location
    lowest_table = None
    for table in full_con.ancestry:
        if table in full_con.tables:
            lowest_table = table
            break

    do_full_directory = False
    # check that locations propagated down to the lowest table in the contribution
    if 'location' in full_con.tables[lowest_table].df.columns:
        if 'locations' not in full_con.tables:
            info_log(
                'location names propagated to {}, but could not be validated'.
                format(lowest_table))
        # are there any locations in the lowest table?
        elif not all(full_con.tables[lowest_table].df['location'].isnull()):
            locs = full_con.tables['locations'].df.index.unique()
            lowest_locs = full_con.tables[lowest_table].df['location'].unique()
            incorrect_locs = set(lowest_locs).difference(set(locs))
            # are they actual locations?
            if not incorrect_locs:
                info_log(
                    'location names propagated to {}'.format(lowest_table))
            else:
                do_full_directory = True
                error_log(
                    'location names did not propagate fully to {} table (looks like there are some naming inconsistencies between tables)'
                    .format(lowest_table),
                    con_id=con_id)
        else:
            do_full_directory = True
            error_log(
                'could not propagate location names down to {} table'.format(
                    lowest_table),
                con_id=con_id)
    else:
        do_full_directory = True
        error_log('could not propagate location names down to {} table'.format(
            lowest_table),
                  con_id=con_id)

    all_data = {}
    all_data['measurements'] = full_con.tables.get('measurements', None)
    all_data['specimens'] = full_con.tables.get('specimens', None)
    all_data['samples'] = full_con.tables.get('samples', None)
    all_data['sites'] = full_con.tables.get('sites', None)
    all_data['locations'] = full_con.tables.get('locations', None)
    if 'locations' in full_con.tables:
        locations = full_con.tables['locations'].df.index.unique()
    else:
        locations = ['']
    dirlist = [
        loc for loc in locations if cb.not_null(loc, False) and loc != 'nan'
    ]
    if not dirlist:
        dirlist = ["./"]
    if do_full_directory:
        dirlist = ["./"]

    # plot the whole contribution as one location
    if dirlist == ["./"]:
        error_log('plotting the entire contribution as one location',
                  con_id=con_id)
        for fname in os.listdir("."):
            if fname.endswith(".txt"):
                shutil.copy(fname, "tmp_" + fname)

    # if possible, go through all data by location
    # use tmp_*.txt files to separate out by location

    for loc in dirlist:
        print('\nworking on: ', loc)

        def get_data(dtype, loc_name):
            """
            Extract data of type dtype for location loc_name.
            Write tmp_dtype.txt files if possible.
            """
            if cb.not_null(all_data[dtype], False):
                data_container = all_data[dtype]
                if loc_name == "./":
                    data_df = data_container.df
                else:
                    # awkward workaround for chars like "(" and "?" that break in regex
                    try:
                        data_df = data_container.df[data_container.df[
                            'location'].astype(str).str.contains(loc_name,
                                                                 na=False)]
                    except:  #sre_constants.error:
                        data_df = data_container.df[
                            data_container.df['location'] == loc_name]

                data = data_container.convert_to_pmag_data_list(df=data_df)
                res = data_container.write_magic_file(
                    'tmp_{}.txt'.format(dtype), df=data_df)
                if not res:
                    return [], []
                return data, data_df
            return [], []

        meas_data, meas_df = get_data('measurements', loc)
        spec_data, spec_df = get_data('specimens', loc)
        samp_data, samp_df = get_data('samples', loc)
        site_data, site_df = get_data('sites', loc)
        loc_data, loc_df = get_data('locations', loc)

        con = cb.Contribution(read_tables=[])
        con.tables['measurements'] = cb.MagicDataFrame(df=meas_df,
                                                       dtype="measurements")
        con.tables['specimens'] = cb.MagicDataFrame(df=spec_df,
                                                    dtype="specimens")
        con.tables['samples'] = cb.MagicDataFrame(df=samp_df, dtype="samples")
        con.tables['sites'] = cb.MagicDataFrame(df=site_df, dtype="sites")
        con.tables['locations'] = cb.MagicDataFrame(df=loc_df,
                                                    dtype="locations")

        if loc == "./":  # if you can't sort by location, do everything together
            con = full_con
            try:
                meas_data = con.tables[
                    'measurements'].convert_to_pmag_data_list()
            except KeyError:
                meas_data = None
            try:
                spec_data = con.tables['specimens'].convert_to_pmag_data_list()
            except KeyError:
                spec_data = None
            try:
                samp_data = con.tables['samples'].convert_to_pmag_data_list()
            except KeyError:
                samp_data = None
            try:
                site_data = con.tables['sites'].convert_to_pmag_data_list()
            except KeyError:
                site_data = None

        crd = 's'
        if 'samples' in con.tables:
            if 'azimuth' in con.tables['samples'].df.columns:
                if any(con.tables['samples'].df['azimuth'].dropna()):
                    crd = 'g'
        if crd == 's':
            print('using specimen coordinates')
        else:
            print('using geographic coordinates')
        if meas_file in filelist and meas_data:  # start with measurement data
            print('working on plotting measurements data')
            data = meas_data
            file_type = 'measurements'
            # looking for  zeq_magic possibilities
            # get all non blank method codes
            AFZrecs = pmag.get_dictitem(data, method_key, 'LT-AF-Z', 'has')
            # get all non blank method codes
            TZrecs = pmag.get_dictitem(data, method_key, 'LT-T-Z', 'has')
            # get all non blank method codes
            MZrecs = pmag.get_dictitem(data, method_key, 'LT-M-Z', 'has')
            # get all dec measurements
            Drecs = pmag.get_dictitem(data, dec_key, '', 'F')
            # get all inc measurements
            Irecs = pmag.get_dictitem(data, inc_key, '', 'F')
            for key in Mkeys:
                Mrecs = pmag.get_dictitem(data, key, '',
                                          'F')  # get intensity data
                if len(Mrecs) > 0:
                    break
            # potential for stepwise demag curves
            if len(AFZrecs) > 0 or len(TZrecs) > 0 or len(MZrecs) > 0 and len(
                    Drecs) > 0 and len(Irecs) > 0 and len(Mrecs) > 0:
                #CMD = 'zeq_magic.py -f tmp_measurements.txt -fsp tmp_specimens.txt -fsa tmp_samples.txt -fsi tmp_sites.txt -sav -fmt ' + fmt + ' -crd ' + crd + " -new"
                CMD = "ipmag.zeq_magic(crd={}, n_plots='all', contribution={}, image_records=True)".format(
                    crd, con)
                print(CMD)
                info_log(CMD, loc)
                res, outfiles, zeq_images = ipmag.zeq_magic(crd=crd,
                                                            n_plots='all',
                                                            contribution=con,
                                                            image_records=True)
                image_recs.extend(zeq_images)
            # looking for  thellier_magic possibilities
            if len(pmag.get_dictitem(data, method_key, 'LP-PI-TRM',
                                     'has')) > 0:
                #CMD = 'thellier_magic.py -f tmp_measurements.txt -fsp tmp_specimens.txt -sav -fmt ' + fmt
                CMD = "ipmag.thellier_magic(n_specs='all', fmt='png', contribution={}, image_records=True)".format(
                    con)
                print(CMD)
                info_log(CMD, loc)
                res, outfiles, thellier_images = ipmag.thellier_magic(
                    n_specs='all',
                    fmt="png",
                    contribution=con,
                    image_records=True)
                image_recs.extend(thellier_images)
            # looking for hysteresis possibilities
            if len(pmag.get_dictitem(data, method_key, 'LP-HYS',
                                     'has')) > 0:  # find hyst experiments
                # check for reqd columns
                missing = check_for_reqd_cols(data, ['treat_temp'])
                if missing:
                    error_log(
                        'LP-HYS method code present, but required column(s) [{}] missing'
                        .format(", ".join(missing)),
                        loc,
                        "quick_hyst.py",
                        con_id=con_id)
                else:
                    #CMD = 'quick_hyst.py -f tmp_measurements.txt -sav -fmt ' + fmt
                    CMD = "ipmag.quick_hyst(fmt='png', n_plots='all', contribution={}, image_records=True)".format(
                        con)
                    print(CMD)
                    info_log(CMD, loc)
                    res, outfiles, quick_hyst_recs = ipmag.quick_hyst(
                        fmt="png",
                        n_plots='all',
                        contribution=con,
                        image_records=True)
                    image_recs.extend(quick_hyst_recs)
            # equal area plots of directional data
            # at measurement level (by specimen)
            if data:
                missing = check_for_reqd_cols(data, ['dir_dec', 'dir_inc'])
                if not missing:
                    #CMD = "eqarea_magic.py -f tmp_measurements.txt -obj spc -sav -no-tilt -fmt " + fmt
                    CMD = "ipmag.eqarea_magic(fmt='png', n_plots='all', ignore_tilt=True, plot_by='spc', contribution={}, source_table='measurements', image_records=True)".format(
                        con)
                    print(CMD)
                    info_log(CMD, loc, "eqarea_magic.py")
                    res, outfiles, eqarea_spc_images = ipmag.eqarea_magic(
                        fmt="png",
                        n_plots='all',
                        ignore_tilt=True,
                        plot_by="spc",
                        contribution=con,
                        source_table="measurements",
                        image_records=True)
                    image_recs.extend(eqarea_spc_images)

        else:
            if VERBOSE:
                print('-I- No measurement data found')

        # site data
        if results_file in filelist and site_data:
            print('-I- result file found', results_file)
            data = site_data
            file_type = 'sites'
            print('-I- working on site directions')
            print('number of datapoints: ', len(data), loc)
            dec_key = 'dir_dec'
            inc_key = 'dir_inc'
            int_key = 'int_abs'
            SiteDIs = pmag.get_dictitem(data, dec_key, "", 'F')  # find decs
            SiteDIs = pmag.get_dictitem(SiteDIs, inc_key, "",
                                        'F')  # find decs and incs
            dir_data_found = len(SiteDIs)
            print('{} Dec/inc pairs found'.format(dir_data_found))
            if SiteDIs:
                # then convert tilt_corr_key to correct format
                old_SiteDIs = SiteDIs
                SiteDIs = []
                for rec in old_SiteDIs:
                    if tilt_corr_key not in rec:
                        rec[tilt_corr_key] = "0"
                    # make sure tilt_corr_key is a correct format
                    try:
                        rec[tilt_corr_key] = str(int(float(
                            rec[tilt_corr_key])))
                    except ValueError:
                        rec[tilt_corr_key] = "0"
                    SiteDIs.append(rec)

                print('number of individual directions: ', len(SiteDIs))
                # tilt corrected coordinates
                SiteDIs_t = pmag.get_dictitem(SiteDIs,
                                              tilt_corr_key,
                                              '100',
                                              'T',
                                              float_to_int=True)
                print('number of tilt corrected directions: ', len(SiteDIs_t))
                SiteDIs_g = pmag.get_dictitem(
                    SiteDIs, tilt_corr_key, '0', 'T',
                    float_to_int=True)  # geographic coordinates
                print('number of geographic  directions: ', len(SiteDIs_g))
                SiteDIs_s = pmag.get_dictitem(
                    SiteDIs, tilt_corr_key, '-1', 'T',
                    float_to_int=True)  # sample coordinates
                print('number of sample  directions: ', len(SiteDIs_s))
                SiteDIs_x = pmag.get_dictitem(SiteDIs, tilt_corr_key, '',
                                              'T')  # no coordinates
                print('number of no coordinates  directions: ', len(SiteDIs_x))
                if len(SiteDIs_t) > 0 or len(SiteDIs_g) > 0 or len(
                        SiteDIs_s) > 0 or len(SiteDIs_x) > 0:
                    CRD = ""
                    if len(SiteDIs_t) > 0:
                        CRD = ' -crd t'
                        crd = "t"
                    elif len(SiteDIs_g) > 0:
                        CRD = ' -crd g'
                        crd = "g"
                    elif len(SiteDIs_s) > 0:
                        CRD = ' -crd s'
                        crd = "s"
                    #CMD = 'eqarea_magic.py -f tmp_sites.txt -fsp tmp_specimens.txt -fsa tmp_samples.txt -flo tmp_locations.txt -sav -fmt ' + fmt + CRD
                    CMD = "ipmag.eqarea_magic(crd={}, fmt='png', n_plots='all', contribution={}, source_table='sites')".format(
                        crd, con)
                    print(CMD)
                    info_log(CMD, loc)
                    res, outfiles, eqarea_site_recs = ipmag.eqarea_magic(
                        crd=crd,
                        fmt="png",
                        n_plots='all',
                        contribution=con,
                        source_table="sites",
                        image_records=True)
                    image_recs.extend(eqarea_site_recs)
                else:
                    if dir_data_found:
                        error_log(
                            '{} dec/inc pairs found, but no equal area plots were made'
                            .format(dir_data_found),
                            loc,
                            "equarea_magic.py",
                            con_id=con_id)
            #
            print('-I- working on VGP map')
            VGPs = pmag.get_dictitem(SiteDIs, 'vgp_lat', "",
                                     'F')  # are there any VGPs?
            if len(VGPs) > 0:  # YES!
                #CMD = 'vgpmap_magic.py -f tmp_sites.txt -prj moll -res c -sym ro 5 -sav -fmt png'
                CMD = "ipmag.vgpmap_magic(proj='moll', sym='ro', size=5, fmt='png', contribution={})".format(
                    con)
                print(CMD)
                info_log(CMD, loc, 'vgpmap_magic.py')
                res, outfiles, vgpmap_recs = ipmag.vgpmap_magic(
                    proj='moll',
                    sym='ro',
                    size=5,
                    fmt="png",
                    contribution=con,
                    image_records=True)
                image_recs.extend(vgpmap_recs)
            else:
                print('-I- No vgps found')

            print('-I- Look for intensities')
            # is there any intensity data?
            if site_data:
                if int_key in site_data[0].keys():
                    # old way, wasn't working right:
                    #CMD = 'magic_select.py  -key ' + int_key + ' 0. has -F tmp1.txt -f tmp_sites.txt'
                    Selection = pmag.get_dictkey(site_data, int_key, dtype="f")
                    selection = [i * 1e6 for i in Selection if i != 0]
                    loc = loc.replace(" ", "_")
                    if loc == "./":
                        loc_name = ""
                    else:
                        loc_name = loc
                    histfile = 'LO:_' + loc_name + \
                        '_TY:_intensities_histogram:_.' + fmt
                    CMD = "histplot.py -twin -b 1 -xlab 'Intensity (uT)' -sav -f intensities.txt -F " + histfile
                    CMD = "ipmag.histplot(data=selection, outfile=histfile, xlab='Intensity (uT)', binsize=1, norm=-1, save_plots=True)".format(
                        histfile)
                    info_log(CMD, loc)
                    print(CMD)
                    ipmag.histplot(data=selection,
                                   outfile=histfile,
                                   xlab="Intensity (uT)",
                                   binsize=1,
                                   norm=-1,
                                   save_plots=True)
                    histplot_rec = {
                        'file': histfile,
                        'type': 'Other',
                        'title': 'Intensity histogram',
                        'software_packages': version.version,
                        'keywords': "",
                        'timestamp': datetime.date.today().isoformat()
                    }
                    image_recs.append(histplot_rec)
                else:
                    print('-I- No intensities found')
            else:
                print('-I- No intensities found')

        ##
        if hyst_file in filelist and spec_data:
            print('working on hysteresis', hyst_file)
            data = spec_data
            file_type = 'specimens'
            hdata = pmag.get_dictitem(data, hyst_bcr_key, '', 'F')
            hdata = pmag.get_dictitem(hdata, hyst_mr_key, '', 'F')
            hdata = pmag.get_dictitem(hdata, hyst_ms_key, '', 'F')
            # there are data for a dayplot
            hdata = pmag.get_dictitem(hdata, hyst_bc_key, '', 'F')
            if len(hdata) > 0:
                CMD = "ipmag.dayplot_magic(save=True, fmt='png', contribution={}, image_records=True)".format(
                    con)
                info_log(CMD, loc)
                print(CMD)
                res, outfiles, dayplot_recs = ipmag.dayplot_magic(
                    save=True, fmt='png', contribution=con, image_records=True)
                image_recs.extend(dayplot_recs)
            else:
                print('no hysteresis data found')
        if aniso_file in filelist and spec_data:  # do anisotropy plots if possible
            print('working on anisotropy', aniso_file)
            data = spec_data
            file_type = 'specimens'

            # make sure there is some anisotropy data
            if not data:
                print('No anisotropy data found')
            elif 'aniso_s' not in data[0]:
                print('No anisotropy data found')
            else:
                # get specimen coordinates
                if aniso_tilt_corr_key not in data[0]:
                    sdata = data
                else:
                    sdata = pmag.get_dictitem(data,
                                              aniso_tilt_corr_key,
                                              '-1',
                                              'T',
                                              float_to_int=True)
                # get specimen coordinates
                gdata = pmag.get_dictitem(data,
                                          aniso_tilt_corr_key,
                                          '0',
                                          'T',
                                          float_to_int=True)
                # get specimen coordinates
                tdata = pmag.get_dictitem(data,
                                          aniso_tilt_corr_key,
                                          '100',
                                          'T',
                                          float_to_int=True)
                if len(sdata) > 3:
                    CMD = "ipmag.aniso_magic(iboot=0, ihext=1, crd='s', fmt='png', contribution={})".format(
                        con)
                    print(CMD)
                    info_log(CMD, loc)
                    res, files, aniso_recs = ipmag.aniso_magic(
                        iboot=0,
                        ihext=1,
                        crd="s",
                        fmt="png",
                        contribution=con,
                        image_records=True)
                    image_recs.extend(aniso_recs)
                if len(gdata) > 3:
                    CMD = "ipmag.aniso_magic(iboot=0, ihext=1, crd='g', fmt='png', contribution={})".format(
                        con)
                    print(CMD)
                    info_log(CMD, loc)
                    res, files, aniso_recs = ipmag.aniso_magic(
                        iboot=0,
                        ihext=1,
                        crd="g",
                        fmt="png",
                        contribution=con,
                        image_records=True)
                    image_recs.extend(aniso_recs)
                if len(tdata) > 3:
                    CMD = "ipmag.aniso_magic(iboot=0, ihext=1, crd='g', fmt='png', contribution={})".format(
                        con)
                    print(CMD)
                    info_log(CMD, loc)
                    res, files, aniso_recs = ipmag.aniso_magic(
                        iboot=0,
                        ihext=1,
                        crd="t",
                        fmt="png",
                        contribution=con,
                        image_records=True)
                    image_recs.extend(aniso_recs)

        # remove temporary files
        for fname in glob.glob('tmp*.txt'):
            os.remove(fname)

    # now we need full contribution data
    if loc_file in filelist and loc_data:
        #data, file_type = pmag.magic_read(loc_file)  # read in location data
        data = loc_data
        print('-I- working on pole map')
        poles = pmag.get_dictitem(data, 'pole_lat', "",
                                  'F')  # are there any poles?
        poles = pmag.get_dictitem(poles, 'pole_lon', "",
                                  'F')  # are there any poles?
        if len(poles) > 0:  # YES!
            CMD = 'polemap_magic.py -sav -fmt png -rev gv 40'
            CMD = 'ipmag.polemap_magic(flip=True, rsym="gv", rsymsize=40, fmt="png", contribution={})'.format(
                full_con)
            print(CMD)
            info_log(CMD, "all locations", "polemap_magic.py")
            res, outfiles, polemap_recs = ipmag.polemap_magic(
                flip=True,
                rsym="gv",
                rsymsize=40,
                fmt="png",
                contribution=full_con,
                image_records=True)
            image_recs.extend(polemap_recs)
        else:
            print('-I- No poles found')

    if image_recs:
        new_image_file = os.path.join(dir_path, 'new_images.txt')
        old_image_file = os.path.join(dir_path, 'images.txt')
        pmag.magic_write(new_image_file, image_recs, 'images')
        if os.path.exists(old_image_file):
            ipmag.combine_magic([old_image_file, new_image_file],
                                outfile=old_image_file,
                                magic_table="images",
                                dir_path=dir_path)
        else:
            os.rename(new_image_file, old_image_file)
    if set_env.isServer:
        thumbnails.make_thumbnails(dir_path)
Exemple #8
0
def main():
    """
    NAME
        sites_locations.py

    DESCRIPTION
        reads in er_sites.txt file and finds all locations and bounds of locations
        outputs er_locations.txt file

    SYNTAX
        sites_locations.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f: specimen input er_sites format file, default is "er_sites.txt"
        -F: locations table: default is "er_locations.txt"
    """
# set defaults
    site_file="er_sites.txt"
    loc_file="er_locations.txt"
    Names,user=[],"unknown"
    Done=[]
    version_num=pmag.get_version()
    args=sys.argv
    dir_path='.'
# get command line stuff
    if '-WD' in args:
        ind=args.index("-WD")
        dir_path=args[ind+1]
    if "-h" in args:
        print(main.__doc__)
        sys.exit()
    if '-f' in args:
        ind=args.index("-f")
        site_file=args[ind+1]
    if '-F' in args:
        ind=args.index("-F")
        loc_file=args[ind+1]
    #
    site_file=dir_path+'/'+site_file
    loc_file=dir_path+'/'+loc_file
    Sites,file_type=pmag.magic_read(site_file)
    if file_type != 'er_sites':
        print(file_type)
        print(file_type,"This is not a valid er_sites file ")
        sys.exit()
    # read in site data
    #
    LocNames,Locations=[],[]
    for site in Sites:
        if site['er_location_name'] not in LocNames: # new location name
            LocNames.append(site['er_location_name'])
            sites_locs=pmag.get_dictitem(Sites,'er_location_name',site['er_location_name'],'T') # get all sites for this loc
            lats=pmag.get_dictkey(sites_locs,'site_lat','f') # get all the latitudes as floats
            lons=pmag.get_dictkey(sites_locs,'site_lon','f') # get all the longitudes as floats
            LocRec={'er_citation_names':'This study','er_location_name':site['er_location_name'],'location_type':''}
            LocRec['location_begin_lat']=str(min(lats))
            LocRec['location_end_lat']=str(max(lats))
            LocRec['location_begin_lon']=str(min(lons))
            LocRec['location_end_lon']=str(max(lons))
            Locations.append(LocRec)
    if len(Locations)>0:
        pmag.magic_write(loc_file,Locations,"er_locations")
        print("Locations written to: ",loc_file)
Exemple #9
0
def main():
    """
    NAME
        lowrie_magic.py

    DESCRIPTION
       plots intensity decay curves for Lowrie experiments

    SYNTAX 
        lowrie_magic.py -h [command line options]
    
    INPUT 
       takes magic_measurements formatted input files
    
    OPTIONS
        -h prints help message and quits
        -f FILE: specify input file, default is magic_measurements.txt
        -N do not normalize by maximum magnetization
        -fmt [svg, pdf, eps, png] specify fmt, default is svg
        -sav saves plots and quits
    """
    fmt,plot='svg',0
    FIG={} # plot dictionary
    FIG['lowrie']=1 # demag is figure 1
    pmagplotlib.plot_init(FIG['lowrie'],6,6)
    norm=1 # default is to normalize by maximum axis
    in_file,dir_path='magic_measurements.txt','.'
    if len(sys.argv)>1:
        if '-WD' in sys.argv:
            ind=sys.argv.index('-WD')
            dir_path=sys.argv[ind+1]
        if '-h' in sys.argv:
            print main.__doc__
            sys.exit()
        if '-N' in sys.argv: norm=0 # don't normalize
        if '-sav' in sys.argv: plot=1 # don't normalize
        if '-fmt' in sys.argv: # sets input filename
            ind=sys.argv.index("-fmt")
            fmt=sys.argv[ind+1]
        if '-f' in sys.argv: # sets input filename
            ind=sys.argv.index("-f")
            in_file=sys.argv[ind+1]
    else:
        print main.__doc__
        print 'you must supply a file name'
        sys.exit() 
    in_file=dir_path+'/'+in_file
    print in_file
    PmagRecs,file_type=pmag.magic_read(in_file)
    if file_type!="magic_measurements":
         print 'bad input file'
         sys.exit()
    PmagRecs=pmag.get_dictitem(PmagRecs,'magic_method_codes','LP-IRM-3D','has') # get all 3D IRM records
    if len(PmagRecs)==0:
        print 'no records found'
        sys.exit()
    specs=pmag.get_dictkey(PmagRecs,'er_specimen_name','')
    sids=[]
    for spec in specs:
        if spec not in sids:sids.append(spec) # get list of unique specimen names
    for spc in sids:  # step through the specimen names
        print spc
        specdata=pmag.get_dictitem(PmagRecs,'er_specimen_name',spc,'T') # get all this one's data
        DIMs,Temps=[],[]
        for dat in specdata: # step through the data
            DIMs.append([float(dat['measurement_dec']),float(dat['measurement_inc']),float(dat['measurement_magn_moment'])])
            Temps.append(float(dat['treatment_temp'])-273.)
        carts=pmag.dir2cart(DIMs).transpose()
        if norm==1: # want to normalize
            nrm=(DIMs[0][2]) # normalize by NRM
            ylab="M/M_o"
        else: 
            nrm=1. # don't normalize
            ylab="Magnetic moment (Am^2)"
        xlab="Temperature (C)"
        pmagplotlib.plotXY(FIG['lowrie'],Temps,abs(carts[0])/nrm,sym='r-')
        pmagplotlib.plotXY(FIG['lowrie'],Temps,abs(carts[0])/nrm,sym='ro') # X direction
        pmagplotlib.plotXY(FIG['lowrie'],Temps,abs(carts[1])/nrm,sym='c-')
        pmagplotlib.plotXY(FIG['lowrie'],Temps,abs(carts[1])/nrm,sym='cs') # Y direction
        pmagplotlib.plotXY(FIG['lowrie'],Temps,abs(carts[2])/nrm,sym='k-')
        pmagplotlib.plotXY(FIG['lowrie'],Temps,abs(carts[2])/nrm,sym='k^',title=spc,xlab=xlab,ylab=ylab) # Z direction
        files={'lowrie':'lowrie:_'+spc+'_.'+fmt}
        if plot==0:
            pmagplotlib.drawFIGS(FIG)
            ans=raw_input('S[a]ve figure? [q]uit, <return> to continue   ')
            if ans=='a':
                pmagplotlib.saveP(FIG,files)
            elif ans=='q':
                sys.exit()
        else:
            pmagplotlib.saveP(FIG,files)
        pmagplotlib.clearFIG(FIG['lowrie'])
Exemple #10
0
def main():
    """
    NAME
        make_magic_plots.py

    DESCRIPTION
 	inspects magic directory for available plots.

    SYNTAX
        make_magic_plots.py [command line options]

    INPUT
        magic files

    OPTIONS
        -h prints help message and quits
        -f FILE specifies input file name
        -fmt [png,eps,svg,jpg,pdf] specify format, default is png
        -DM [2,3] define data model
    """
    dirlist = ['./']
    dir_path = os.getcwd()
    names = os.listdir(dir_path)
    for n in names:
        if 'Location' in n:
            dirlist.append(n)
    if '-fmt' in sys.argv:
        ind = sys.argv.index("-fmt")
        fmt = sys.argv[ind + 1]
    else:
        fmt = 'png'
    if '-f' in sys.argv:
        ind = sys.argv.index("-f")
        filelist = [sys.argv[ind + 1]]
    else:
        filelist = os.listdir(dir_path)
    new_model = 0
    if '-DM' in sys.argv:
        ind = sys.argv.index("-DM")
        data_model = sys.argv[ind + 1]
        if data_model == '3': new_model = 1
    if new_model:
        samp_file = 'samples.txt'
        azimuth_key = 'azimuth'
        meas_file = 'measurements.txt'
        loc_key = 'location'
        method_key = 'method_codes'
        dec_key = 'dir_dec'
        inc_key = 'dir_inc'
        Mkeys = ['magnitude', 'magn_moment', 'magn_volume', 'magn_mass']
        results_file = 'sites.txt'
        tilt_key = 'direction_tilt_correction'
        hyst_file = 'specimens.txt'
        aniso_file = 'specimens.txt'
    else:
        new_model = 0
        samp_file = 'er_samples.txt'
        azimuth_key = 'sample_azimuth'
        meas_file = 'magic_measurements.txt'
        loc_key = 'er_location_name'
        method_key = 'magic_method_codes'
        dec_key = 'measurement_dec'
        inc_key = 'measurement_inc'
        Mkeys = [
            'measurement_magnitude', 'measurement_magn_moment',
            'measurement_magn_volume', 'measurement_magn_mass'
        ]
        results_file = 'pmag_results.txt'
        tilt_key = 'tilt_correction'
        hyst_file = 'rmag_hysteresis'
        aniso_file = 'rmag_anisotropy'
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    for loc in dirlist:
        print 'working on: ', loc
        os.chdir(loc)  # change working directories to each location
        crd = 's'
        print samp_file
        if samp_file in filelist:  # find coordinate systems
            print 'found sample file'
            samps, file_type = pmag.magic_read(samp_file)  # read in data
            Srecs = pmag.get_dictitem(
                samps, azimuth_key, '',
                'F')  # get all none blank sample orientations
            if len(Srecs) > 0:
                crd = 'g'
        if meas_file in filelist:  # start with measurement data
            print 'working on measurements data'
            data, file_type = pmag.magic_read(meas_file)  # read in data
            if loc == './':
                data = pmag.get_dictitem(
                    data, loc_key, '',
                    'T')  # get all the blank location names from data file
            # looking for  zeq_magic possibilities
            AFZrecs = pmag.get_dictitem(
                data, method_key, 'LT-AF-Z',
                'has')  # get all none blank method codes
            TZrecs = pmag.get_dictitem(
                data, method_key, 'LT-T-Z',
                'has')  # get all none blank method codes
            MZrecs = pmag.get_dictitem(
                data, method_key, 'LT-M-Z',
                'has')  # get all none blank method codes
            Drecs = pmag.get_dictitem(data, dec_key, '',
                                      'F')  # get all dec measurements
            Irecs = pmag.get_dictitem(data, inc_key, '',
                                      'F')  # get all inc measurements
            for key in Mkeys:
                Mrecs = pmag.get_dictitem(data, key, '',
                                          'F')  # get intensity data
                if len(Mrecs) > 0: break
            if len(AFZrecs) > 0 or len(TZrecs) > 0 or len(MZrecs) > 0 and len(
                    Drecs) > 0 and len(Irecs) > 0 and len(
                        Mrecs) > 0:  # potential for stepwise demag curves
                if new_model:
                    CMD = 'zeq_magic3.0.py -fsp specimens.txt -sav -fmt ' + fmt + ' -crd ' + crd
                else:
                    CMD = 'zeq_magic.py -fsp pmag_specimens.txt -sav -fmt ' + fmt + ' -crd ' + crd
                print CMD
                os.system(CMD)
            # looking for  thellier_magic possibilities
            if len(pmag.get_dictitem(data, method_key, 'LP-PI-TRM',
                                     'has')) > 0:
                if new_model:
                    CMD = 'thellier_magic3.0.py -fsp specimens.txt -sav -fmt ' + fmt
                else:
                    CMD = 'thellier_magic.py -fsp pmag_specimens.txt -sav -fmt ' + fmt
                print CMD
                os.system(CMD)
            # looking for hysteresis possibilities
            if len(pmag.get_dictitem(data, method_key, 'LP-HYS',
                                     'has')) > 0:  # find hyst experiments
                if new_model:
                    CMD = 'quick_hyst3.0.py -sav -fmt ' + fmt
                else:
                    CMD = 'quick_hyst.py -sav -fmt ' + fmt
                print CMD
                os.system(CMD)
        if results_file in filelist:  # start with measurement data
            data, file_type = pmag.magic_read(results_file)  # read in data
            print 'number of datapoints: ', len(data)
            if loc == './':
                data = pmag.get_dictitem(
                    data, loc_key, ':', 'has'
                )  # get all the concatenated location names from data file
            print 'number of datapoints: ', len(data), loc
            if new_model:
                print 'working on site directions'
                dec_key = 'dir_dec'
                inc_key = 'dir_inc'
                int_key = 'int_abs'
            else:
                print 'working on results directions'
                dec_key = 'average_dec'
                inc_key = 'average_inc'
                int_key = 'average_int'
            SiteDIs = pmag.get_dictitem(data, dec_key, "", 'F')  # find decs
            SiteDIs = pmag.get_dictitem(SiteDIs, inc_key, "",
                                        'F')  # find decs and incs
            SiteDIs = pmag.get_dictitem(
                SiteDIs, 'data_type', 'i',
                'has')  # only individual results - not poles
            print 'number of directions: ', len(SiteDIs)
            SiteDIs_t = pmag.get_dictitem(SiteDIs, tilt_key, '100',
                                          'T')  # tilt corrected coordinates
            print 'number of tilt corrected directions: ', len(SiteDIs)
            SiteDIs_g = pmag.get_dictitem(SiteDIs, tilt_key, '0',
                                          'T')  # geographic coordinates
            SiteDIs_s = pmag.get_dictitem(SiteDIs, 'tilt_correction', '-1',
                                          'T')  # sample coordinates
            SiteDIs_x = pmag.get_dictitem(SiteDIs, 'tilt_correction', '',
                                          'T')  # no coordinates
            if len(SiteDIs_t) > 0 or len(SiteDIs_g) > 0 or len(
                    SiteDIs_s) > 0 or len(SiteDIs_x) > 0:
                CRD = ""
                if len(SiteDIs_t) > 0:
                    CRD = ' -crd t'
                elif len(SiteDIs_g) > 0:
                    CRD = ' -crd g'
                elif len(SiteDIs_s) > 0:
                    CRD = ' -crd s'
                if new_model:
                    CMD = 'eqarea_magic3.0.py -sav -crd t -fmt ' + fmt + CRD
                else:
                    CMD = 'eqarea_magic.py -sav -crd t -fmt ' + fmt + CRD
                print CMD
                os.system(CMD)
            print 'working on VGP map'
            VGPs = pmag.get_dictitem(SiteDIs, 'vgp_lat', "",
                                     'F')  # are there any VGPs?
            if len(VGPs) > 0:  # YES!
                os.system(
                    'vgpmap_magic.py -prj moll -res c -sym ro 5 -sav -fmt png')
            print 'working on intensities'
            if not new_model:
                CMD = 'magic_select.py -f ' + results_file + ' -key data_type i T -F tmp.txt'
                os.system(CMD)
                infile = ' tmp.txt'
            else:
                infile = results_file
            print int_key
            CMD = 'magic_select.py  -key ' + int_key + ' 0. has -F tmp1.txt -f ' + infile
            os.system(CMD)
            CMD = "grab_magic_key.py -f tmp1.txt -key " + int_key + " | awk '{print $1*1e6}' >tmp2.txt"
            os.system(CMD)
            data, file_type = pmag.magic_read('tmp1.txt')  # read in data
            if new_model:
                locations = pmag.get_dictkey(data, loc_key, "")
            else:
                locations = pmag.get_dictkey(data, loc_key + 's', "")
            histfile = 'LO:_' + locations[0] + '_intensities_histogram:_.' + fmt
            os.system(
                "histplot.py -b 1 -xlab 'Intensity (uT)' -sav -f tmp2.txt -F "
                + histfile)
            print "histplot.py -b 1 -xlab 'Intensity (uT)' -sav -f tmp2.txt -F " + histfile
            os.system('rm tmp*.txt')
        if hyst_file in filelist:  # start with measurement data
            print 'working on hysteresis'
            data, file_type = pmag.magic_read(hyst_file)  # read in data
            if loc == './':
                data = pmag.get_dictitem(
                    data, loc_key, '',
                    'T')  # get all the blank location names from data file
            hdata = pmag.get_dictitem(data, 'hysteresis_bcr', '', 'F')
            hdata = pmag.get_dictitem(hdata, 'hysteresis_mr_moment', '', 'F')
            hdata = pmag.get_dictitem(hdata, 'hysteresis_ms_moment', '', 'F')
            hdata = pmag.get_dictitem(hdata, 'hysteresis_bc', '',
                                      'F')  # there are data for a dayplot
            if len(hdata) > 0:
                print 'dayplot_magic.py -sav -fmt ' + fmt
                os.system('dayplot_magic.py -sav -fmt ' + fmt)
        if aniso_file in filelist:  # do anisotropy plots if possible
            print 'working on anisotropy'
            data, file_type = pmag.magic_read(aniso_file)  # read in data
            if loc == './':
                data = pmag.get_dictitem(
                    data, loc_key, '',
                    'T')  # get all the blank location names from data file
            sdata = pmag.get_dictitem(data, 'anisotropy_tilt_correction', '-1',
                                      'T')  # get specimen coordinates
            gdata = pmag.get_dictitem(data, 'anisotropy_tilt_correction', '0',
                                      'T')  # get specimen coordinates
            tdata = pmag.get_dictitem(data, 'anisotropy_tilt_correction',
                                      '100', 'T')  # get specimen coordinates
            CRD = ""
            if new_model:
                CMD = 'aniso_magic3.0.py -x -B -sav -fmt ' + fmt
            else:
                CMD = 'aniso_magic.py -x -B -sav -fmt ' + fmt
            if len(sdata) > 3:
                CMD = CMD + ' -crd s'
                print CMD
                os.system(CMD)
            if len(gdata) > 3:
                CMD = CMD + ' -crd g'
                print CMD
                os.system(CMD)
            if len(tdata) > 3:
                CMD = CMD + ' -crd t'
                print CMD
                os.system(CMD)
        if loc != './':
            os.chdir('..')  # change working directories to each location
Exemple #11
0
def main():
    """
    NAME
        lowrie.py

    DESCRIPTION
       plots intensity decay curves for Lowrie experiments

    SYNTAX
        lowrie -h [command line options]

    INPUT
       takes SIO formatted input files

    OPTIONS
        -h prints help message and quits
        -f FILE: specify input file
        -N do not normalize by maximum magnetization
        -fmt [svg, pdf, eps, png] specify fmt, default is svg
        -sav save plots and quit
    """
    fmt, plot = 'svg', 0
    FIG = {}  # plot dictionary
    FIG['lowrie'] = 1  # demag is figure 1
    pmagplotlib.plot_init(FIG['lowrie'], 6, 6)
    norm = 1  # default is to normalize by maximum axis
    if len(sys.argv) > 1:
        if '-h' in sys.argv:
            print(main.__doc__)
            sys.exit()
        if '-N' in sys.argv:
            norm = 0  # don't normalize
        if '-sav' in sys.argv:
            plot = 1  # don't normalize
        if '-fmt' in sys.argv:  # sets input filename
            ind = sys.argv.index("-fmt")
            fmt = sys.argv[ind + 1]
        if '-f' in sys.argv:  # sets input filename
            ind = sys.argv.index("-f")
            in_file = sys.argv[ind + 1]
        else:
            print(main.__doc__)
            print('you must supply a file name')
            sys.exit()
    else:
        print(main.__doc__)
        print('you must supply a file name')
        sys.exit()
    data = pmag.open_file(in_file)
    PmagRecs = []  # set up a list for the results
    keys = ['specimen', 'treatment', 'csd', 'M', 'dec', 'inc']
    for line in data:
        PmagRec = {}
        rec = line.replace('\n', '').split()
        for k in range(len(keys)):
            PmagRec[keys[k]] = rec[k]
        PmagRecs.append(PmagRec)
    specs = pmag.get_dictkey(PmagRecs, 'specimen', '')
    sids = []
    for spec in specs:
        if spec not in sids:
            sids.append(spec)  # get list of unique specimen names
    for spc in sids:  # step through the specimen names
        print(spc)
        specdata = pmag.get_dictitem(
            PmagRecs, 'specimen', spc, 'T')  # get all this one's data
        DIMs, Temps = [], []
        for dat in specdata:  # step through the data
            DIMs.append([float(dat['dec']), float(
                dat['inc']), float(dat['M']) * 1e-3])
            Temps.append(float(dat['treatment']))
        carts = pmag.dir2cart(DIMs).transpose()
        # if norm==1: # want to normalize
        #    nrm=max(max(abs(carts[0])),max(abs(carts[1])),max(abs(carts[2]))) # by maximum of x,y,z values
        #    ylab="M/M_max"
        if norm == 1:  # want to normalize
            nrm = (DIMs[0][2])  # normalize by NRM
            ylab = "M/M_o"
        else:
            nrm = 1.  # don't normalize
            ylab = "Magnetic moment (Am^2)"
        xlab = "Temperature (C)"
        pmagplotlib.plotXY(FIG['lowrie'], Temps, old_div(
            abs(carts[0]), nrm), sym='r-')
        pmagplotlib.plotXY(FIG['lowrie'], Temps, old_div(
            abs(carts[0]), nrm), sym='ro')  # X direction
        pmagplotlib.plotXY(FIG['lowrie'], Temps, old_div(
            abs(carts[1]), nrm), sym='c-')
        pmagplotlib.plotXY(FIG['lowrie'], Temps, old_div(
            abs(carts[1]), nrm), sym='cs')  # Y direction
        pmagplotlib.plotXY(FIG['lowrie'], Temps, old_div(
            abs(carts[2]), nrm), sym='k-')
        pmagplotlib.plotXY(FIG['lowrie'], Temps, old_div(
            abs(carts[2]), nrm), sym='k^', title=spc, xlab=xlab, ylab=ylab)  # Z direction
        files = {'lowrie': 'lowrie:_' + spc + '_.' + fmt}
        if plot == 0:
            pmagplotlib.drawFIGS(FIG)
            ans = input('S[a]ve figure? [q]uit, <return> to continue   ')
            if ans == 'a':
                pmagplotlib.saveP(FIG, files)
            elif ans == 'q':
                sys.exit()
        else:
            pmagplotlib.saveP(FIG, files)
        pmagplotlib.clearFIG(FIG['lowrie'])
Exemple #12
0
def main():
    """
    NAME
        make_magic_plots.py

    DESCRIPTION
        inspects magic directory for available data and makes plots

    SYNTAX
        make_magic_plots.py [command line options]

    INPUT
        magic files

    OPTIONS
        -h prints help message and quits
        -f FILE specifies input file name
        -fmt [png,eps,svg,jpg,pdf] specify format, default is png
    """
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    # reset log files
    for fname in ['log.txt', 'errors.txt']:
        f = os.path.join(os.getcwd(), fname)
        if os.path.exists(f):
            os.remove(f)
    dirlist = ['./']
    dir_path = os.getcwd()
    #
    if '-fmt' in sys.argv:
        ind = sys.argv.index("-fmt")
        fmt = sys.argv[ind + 1]
    else:
        fmt = 'png'
    if '-f' in sys.argv:
        ind = sys.argv.index("-f")
        filelist = [sys.argv[ind + 1]]
    else:
        filelist = os.listdir(dir_path)
    ## initialize some variables
    samp_file = 'samples.txt'
    azimuth_key = 'azimuth'
    meas_file = 'measurements.txt'
    loc_key = 'location'
    loc_file = 'locations.txt'
    method_key = 'method_codes'
    dec_key = 'dir_dec'
    inc_key = 'dir_inc'
    tilt_corr_key = "dir_tilt_correction"
    aniso_tilt_corr_key = "aniso_tilt_correction"
    hyst_bcr_key = "hyst_bcr"
    hyst_mr_key = "hyst_mr_moment"
    hyst_ms_key = "hyst_ms_moment"
    hyst_bc_key = "hyst_bc"
    Mkeys = ['magnitude', 'magn_moment', 'magn_volume', 'magn_mass']
    results_file = 'sites.txt'
    hyst_file = 'specimens.txt'
    aniso_file = 'specimens.txt'
    # create contribution and propagate data throughout
    con = cb.Contribution()
    con.propagate_location_to_measurements()
    con.propagate_location_to_specimens()
    con.propagate_location_to_samples()
    if not con.tables:
        print('-E- No MagIC tables could be found in this directory')
        error_log("No MagIC tables found")
        return
    # try to get the contribution id for error logging
    con_id = ""
    if 'contribution' in con.tables:
        if 'id' in con.tables['contribution'].df.columns:
            con_id = con.tables['contribution'].df.iloc[0]['id']
    # check to see if propagation worked, otherwise you can't plot by location
    lowest_table = None
    for table in con.ancestry:
        if table in con.tables:
            lowest_table = table
            break

    do_full_directory = False
    # check that locations propagated down to the lowest table in the contribution
    if 'location' in con.tables[lowest_table].df.columns:
        if 'locations' not in con.tables:
            info_log('location names propagated to {}, but could not be validated'.format(lowest_table))
        # are there any locations in the lowest table?
        elif not all(con.tables[lowest_table].df['location'].isnull()):
            locs = con.tables['locations'].df.index.unique()
            lowest_locs = con.tables[lowest_table].df['location'].unique()
            incorrect_locs = set(lowest_locs).difference(set(locs))
            # are they actual locations?
            if not incorrect_locs:
                info_log('location names propagated to {}'.format(lowest_table))
            else:
                do_full_directory = True
                error_log('location names did not propagate fully to {} table (looks like there are some naming inconsistencies between tables)'.format(lowest_table), con_id=con_id)
        else:
            do_full_directory = True
            error_log('could not propagate location names down to {} table'.format(lowest_table), con_id=con_id)
    else:
        do_full_directory = True
        error_log('could not propagate location names down to {} table'.format(lowest_table), con_id=con_id)

    all_data = {}
    all_data['measurements'] = con.tables.get('measurements', None)
    all_data['specimens'] = con.tables.get('specimens', None)
    all_data['samples'] = con.tables.get('samples', None)
    all_data['sites'] = con.tables.get('sites', None)
    all_data['locations'] = con.tables.get('locations', None)
    if 'locations' in con.tables:
        locations = con.tables['locations'].df.index.unique()
    else:
        locations = ['']
    dirlist = [loc for loc in locations if cb.not_null(loc, False) and loc != 'nan']
    if not dirlist:
        dirlist = ["./"]
    if do_full_directory:
        dirlist = ["./"]

    # plot the whole contribution as one location
    if dirlist == ["./"]:
        error_log('plotting the entire contribution as one location', con_id=con_id)
        for fname in os.listdir("."):
            if fname.endswith(".txt"):
                shutil.copy(fname, "tmp_" + fname)

    # if possible, go through all data by location
    # use tmp_*.txt files to separate out by location

    for loc in dirlist:
        print('\nworking on: ', loc)

        def get_data(dtype, loc_name):
            """
            Extract data of type dtype for location loc_name.
            Write tmp_dtype.txt files if possible.
            """
            if cb.not_null(all_data[dtype], False):
                data_container = all_data[dtype]
                if loc_name == "./":
                    data_df = data_container.df
                else:
                    # awkward workaround for chars like "(" and "?" that break in regex
                    try:
                        data_df = data_container.df[data_container.df['location'].astype(str).str.contains(loc_name, na=False)]
                    except: #sre_constants.error:
                        data_df = data_container.df[data_container.df['location'] == loc_name]

                data = data_container.convert_to_pmag_data_list(df=data_df)
                res = data_container.write_magic_file('tmp_{}.txt'.format(dtype), df=data_df)
                if not res:
                    return []
                return data

        meas_data = get_data('measurements', loc)
        spec_data = get_data('specimens', loc)
        samp_data = get_data('samples', loc)
        site_data = get_data('sites', loc)
        loc_data = get_data('locations', loc)

        if loc == "./":  # if you can't sort by location, do everything together
            try:
                meas_data = con.tables['measurements'].convert_to_pmag_data_list()
            except KeyError:
                meas_data = None
            try:
                spec_data = con.tables['specimens'].convert_to_pmag_data_list()
            except KeyError:
                spec_data = None
            try:
                samp_data = con.tables['samples'].convert_to_pmag_data_list()
            except KeyError:
                samp_data = None
            try:
                site_data = con.tables['sites'].convert_to_pmag_data_list()
            except KeyError:
                site_data = None

        crd = 's'
        if samp_file in filelist and samp_data:  # find coordinate systems
            samps = samp_data
            file_type = "samples"
            # get all non blank sample orientations
            Srecs = pmag.get_dictitem(samps, azimuth_key, '', 'F')
            if len(Srecs) > 0:
                crd = 'g'
                print('using geographic coordinates')
            else:
                print('using specimen coordinates')
        else:
            if VERBOSE:
                print('-I- No sample data found')
        if meas_file in filelist and meas_data:  # start with measurement data
            print('working on measurements data')
            data = meas_data
            file_type = 'measurements'
            # looking for  zeq_magic possibilities
            # get all non blank method codes
            AFZrecs = pmag.get_dictitem(data, method_key, 'LT-AF-Z', 'has')
            # get all non blank method codes
            TZrecs = pmag.get_dictitem(data, method_key, 'LT-T-Z', 'has')
            # get all non blank method codes
            MZrecs = pmag.get_dictitem(data, method_key, 'LT-M-Z', 'has')
            # get all dec measurements
            Drecs = pmag.get_dictitem(data, dec_key, '', 'F')
            # get all inc measurements
            Irecs = pmag.get_dictitem(data, inc_key, '', 'F')
            for key in Mkeys:
                Mrecs = pmag.get_dictitem(
                    data, key, '', 'F')  # get intensity data
                if len(Mrecs) > 0:
                    break
            # potential for stepwise demag curves
            if len(AFZrecs) > 0 or len(TZrecs) > 0 or len(MZrecs) > 0 and len(Drecs) > 0 and len(Irecs) > 0 and len(Mrecs) > 0:
                CMD = 'zeq_magic.py -f tmp_measurements.txt -fsp tmp_specimens.txt -fsa tmp_samples.txt -fsi tmp_sites.txt -sav -fmt ' + fmt + ' -crd ' + crd + " -new"
                print(CMD)
                info_log(CMD, loc)
                os.system(CMD)
            # looking for  thellier_magic possibilities
            if len(pmag.get_dictitem(data, method_key, 'LP-PI-TRM', 'has')) > 0:
                CMD = 'thellier_magic.py -f tmp_measurements.txt -fsp tmp_specimens.txt -sav -fmt ' + fmt
                print(CMD)
                info_log(CMD, loc)
                os.system(CMD)
            # looking for hysteresis possibilities
            if len(pmag.get_dictitem(data, method_key, 'LP-HYS', 'has')) > 0:  # find hyst experiments
                # check for reqd columns
                missing = check_for_reqd_cols(data, ['treat_temp'])
                if missing:
                    error_log('LP-HYS method code present, but required column(s) [{}] missing'.format(", ".join(missing)), loc, "quick_hyst.py", con_id=con_id)
                else:
                    CMD = 'quick_hyst.py -f tmp_measurements.txt -sav -fmt ' + fmt
                    print(CMD)
                    info_log(CMD, loc)
                    os.system(CMD)
            # equal area plots of directional data
            # at measurment level (by specimen)

            if data:
                missing = check_for_reqd_cols(data, ['dir_dec', 'dir_inc'])
                if not missing:
                    CMD = "eqarea_magic.py -f tmp_measurements.txt -obj spc -sav -no-tilt -fmt " + fmt
                    print(CMD)
                    os.system(CMD)
                    info_log(CMD, loc, "eqarea_magic.py")

        else:
            if VERBOSE:
                print('-I- No measurement data found')

        # site data
        if results_file in filelist and site_data:
            print('-I- result file found', results_file)
            data = site_data
            file_type = 'sites'
            print('-I- working on site directions')
            print('number of datapoints: ', len(data), loc)
            dec_key = 'dir_dec'
            inc_key = 'dir_inc'
            int_key = 'int_abs'
            SiteDIs = pmag.get_dictitem(data, dec_key, "", 'F')  # find decs
            SiteDIs = pmag.get_dictitem(
                SiteDIs, inc_key, "", 'F')  # find decs and incs
            dir_data_found = len(SiteDIs)
            print('{} Dec/inc pairs found'.format(dir_data_found))
            if SiteDIs:
                # then convert tilt_corr_key to correct format
                old_SiteDIs = SiteDIs
                SiteDIs = []
                for rec in old_SiteDIs:
                    if tilt_corr_key not in rec:
                        rec[tilt_corr_key] = "0"
                    # make sure tilt_corr_key is a correct format
                    try:
                        rec[tilt_corr_key] = str(int(float(rec[tilt_corr_key])))
                    except ValueError:
                        rec[tilt_corr_key] = "0"
                    SiteDIs.append(rec)

                print('number of individual directions: ', len(SiteDIs))
                # tilt corrected coordinates
                SiteDIs_t = pmag.get_dictitem(SiteDIs, tilt_corr_key, '100',
                                              'T', float_to_int=True)
                print('number of tilt corrected directions: ', len(SiteDIs_t))
                SiteDIs_g = pmag.get_dictitem(
                    SiteDIs, tilt_corr_key, '0', 'T', float_to_int=True)  # geographic coordinates
                print('number of geographic  directions: ', len(SiteDIs_g))
                SiteDIs_s = pmag.get_dictitem(
                    SiteDIs, tilt_corr_key, '-1', 'T', float_to_int=True)  # sample coordinates
                print('number of sample  directions: ', len(SiteDIs_s))
                SiteDIs_x = pmag.get_dictitem(
                    SiteDIs, tilt_corr_key, '', 'T')  # no coordinates
                print('number of no coordinates  directions: ', len(SiteDIs_x))
                if len(SiteDIs_t) > 0 or len(SiteDIs_g) > 0 or len(SiteDIs_s) > 0 or len(SiteDIs_x) > 0:
                    CRD = ""
                    if len(SiteDIs_t) > 0:
                        CRD = ' -crd t'
                    elif len(SiteDIs_g) > 0:
                        CRD = ' -crd g'
                    elif len(SiteDIs_s) > 0:
                        CRD = ' -crd s'
                    CMD = 'eqarea_magic.py -f tmp_sites.txt -fsp tmp_specimens.txt -fsa tmp_samples.txt -flo tmp_locations.txt -sav -fmt ' + fmt + CRD
                    print(CMD)
                    info_log(CMD, loc)
                    os.system(CMD)
                else:
                    if dir_data_found:
                        error_log('{} dec/inc pairs found, but no equal area plots were made'.format(dir_data_found), loc, "equarea_magic.py", con_id=con_id)
            #
            print('-I- working on VGP map')
            VGPs = pmag.get_dictitem(
                SiteDIs, 'vgp_lat', "", 'F')  # are there any VGPs?
            if len(VGPs) > 0:  # YES!
                CMD = 'vgpmap_magic.py -f tmp_sites.txt -prj moll -res c -sym ro 5 -sav -fmt png'
                print(CMD)
                info_log(CMD, loc, 'vgpmap_magic.py')
                os.system(CMD)
            else:
                print('-I- No vgps found')

            print('-I- Look for intensities')
            # is there any intensity data?
            if site_data:
                if int_key in site_data[0].keys():
                    # old way, wasn't working right:
                    #CMD = 'magic_select.py  -key ' + int_key + ' 0. has -F tmp1.txt -f tmp_sites.txt'
                    Selection = pmag.get_dictkey(site_data, int_key, dtype="f")
                    with open('intensities.txt', 'w') as out:
                        for rec in Selection:
                            if rec != 0:
                                out.write(str(rec * 1e6) + "\n")

                    loc = loc.replace(" ", "_")
                    if loc == "./":
                        loc_name = ""
                    else:
                        loc_name = loc
                    histfile = 'LO:_' + loc_name + \
                        '_TY:_intensities_histogram:_.' + fmt
                    # maybe run histplot.main here instead, so you can return an error message
                    CMD = "histplot.py -twin -b 1 -xlab 'Intensity (uT)' -sav -f intensities.txt -F " + histfile
                    os.system(CMD)
                    info_log(CMD, loc)
                    print(CMD)
                else:
                    print('-I- No intensities found')
            else:
                print('-I- No intensities found')

        ##
        if hyst_file in filelist and spec_data:
            print('working on hysteresis', hyst_file)
            data = spec_data
            file_type = 'specimens'
            hdata = pmag.get_dictitem(data, hyst_bcr_key, '', 'F')
            hdata = pmag.get_dictitem(hdata, hyst_mr_key, '', 'F')
            hdata = pmag.get_dictitem(hdata, hyst_ms_key, '', 'F')
            # there are data for a dayplot
            hdata = pmag.get_dictitem(hdata, hyst_bc_key, '', 'F')
            if len(hdata) > 0:
                CMD = 'dayplot_magic.py -f tmp_specimens.txt -sav -fmt ' + fmt
                info_log(CMD, loc)
                print(CMD)
            else:
                print('no hysteresis data found')
        if aniso_file in filelist and spec_data:  # do anisotropy plots if possible
            print('working on anisotropy', aniso_file)
            data = spec_data
            file_type = 'specimens'

            # make sure there is some anisotropy data
            if not data:
                print('No anisotropy data found')
            elif 'aniso_s' not in data[0]:
                print('No anisotropy data found')
            else:
                # get specimen coordinates
                if aniso_tilt_corr_key not in data[0]:
                    sdata = data
                else:
                    sdata = pmag.get_dictitem(
                        data, aniso_tilt_corr_key, '-1', 'T', float_to_int=True)
                # get specimen coordinates
                gdata = pmag.get_dictitem(
                    data, aniso_tilt_corr_key, '0', 'T', float_to_int=True)
                # get specimen coordinates
                tdata = pmag.get_dictitem(
                    data, aniso_tilt_corr_key, '100', 'T', float_to_int=True)
                CRD = ""
                CMD = 'aniso_magic.py -x -B -sav -fmt ' + fmt + " -new"
                if len(sdata) > 3:
                    CMD = CMD + ' -crd s'
                    print(CMD)
                    info_log(CMD, loc)
                    os.system(CMD)
                if len(gdata) > 3:
                    CMD = CMD + ' -crd g'
                    print(CMD)
                    info_log(CMD, loc)
                    os.system(CMD)
                if len(tdata) > 3:
                    CMD = CMD + ' -crd t'
                    print(CMD)
                    info_log(CMD, loc)
                    os.system(CMD)
        # remove temporary files
        for fname in glob.glob('tmp*.txt'):
            os.remove(fname)
        try:
            os.remove('intensities.txt')
        except FileNotFoundError:
            pass
    if loc_file in filelist and loc_data:
        #data, file_type = pmag.magic_read(loc_file)  # read in location data
        data = loc_data
        print('-I- working on pole map')
        poles = pmag.get_dictitem(
            data, 'pole_lat', "", 'F')  # are there any poles?
        poles = pmag.get_dictitem(
            poles, 'pole_lon', "", 'F')  # are there any poles?
        if len(poles) > 0:  # YES!
            CMD = 'polemap_magic.py -sav -fmt png -rev gv 40'
            print(CMD)
            info_log(CMD, "all locations", "polemap_magic.py")
            os.system(CMD)
        else:
            print('-I- No poles found')
    thumbnails.make_thumbnails(dir_path)
Exemple #13
0
def main():
    """
    NAME
        lowrie_magic.py

    DESCRIPTION
       plots intensity decay curves for Lowrie experiments

    SYNTAX
        lowrie_magic.py -h [command line options]

    INPUT
       takes measurements formatted input files

    OPTIONS
        -h prints help message and quits
        -f FILE: specify input file, default is magic_measurements.txt
        -N do not normalize by maximum magnetization
        -fmt [svg, pdf, eps, png] specify fmt, default is svg
        -sav saves plots and quits
        -DM [2, 3] MagIC data model number
    """
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if len(sys.argv) <= 1:
        print(main.__doc__)
        print('you must supply a file name')
        sys.exit()
    FIG = {}  # plot dictionary
    FIG['lowrie'] = 1  # demag is figure 1
    pmagplotlib.plot_init(FIG['lowrie'], 6, 6)
    norm = 1  # default is to normalize by maximum axis
    in_file = pmag.get_named_arg("-f", "measurements.txt")
    dir_path = pmag.get_named_arg("-WD", ".")
    in_file = pmag.resolve_file_name(in_file, dir_path)
    data_model = pmag.get_named_arg("-DM", 3)
    data_model = int(float(data_model))
    fmt = pmag.get_named_arg("-fmt", "svg")
    if '-N' in sys.argv:
        norm = 0  # don't normalize
    if '-sav' in sys.argv:
        plot = 1  # silently save and quit
    else:
        plot = 0 # generate plots
    print(in_file)
    # read in data
    PmagRecs, file_type = pmag.magic_read(in_file)
    if data_model == 2 and file_type != "magic_measurements":
        print('bad input file', file_type)
        sys.exit()
    if data_model == 3 and file_type != "measurements":
        print('bad input file', file_type)
        sys.exit()

    if data_model == 2:
        meth_code_col = 'magic_method_codes'
        spec_col = 'er_specimen_name'
        dec_col = "measurement_dec"
        inc_col = 'measurement_inc'
        moment_col = 'measurement_magn_moment'
        temp_col = 'treatment_temp'
    else:
        meth_code_col = 'method_codes'
        spec_col = 'specimen'
        dec_col = 'dir_dec'
        inc_col = 'dir_inc'
        moment_col = 'magn_moment'
        temp_col = "treat_temp"

    PmagRecs = pmag.get_dictitem(
        PmagRecs, meth_code_col, 'LP-IRM-3D', 'has')  # get all 3D IRM records

    if len(PmagRecs) == 0:
        print('no records found with the method code LP-IRM-3D')
        sys.exit()

    specs = pmag.get_dictkey(PmagRecs, spec_col, '')
    sids = []
    for spec in specs:
        if spec not in sids:
            sids.append(spec)  # get list of unique specimen names
    for spc in sids:  # step through the specimen names
        print(spc)
        specdata = pmag.get_dictitem(
            PmagRecs, spec_col, spc, 'T')  # get all this one's data

        DIMs, Temps = [], []
        for dat in specdata:  # step through the data
            DIMs.append([float(dat[dec_col]), float(
                dat[inc_col]), float(dat[moment_col])])
            Temps.append(float(dat[temp_col])-273.)
        carts = pmag.dir2cart(DIMs).transpose()
        if norm == 1:  # want to normalize
            nrm = (DIMs[0][2])  # normalize by NRM
            ylab = "M/M_o"
        else:
            nrm = 1.  # don't normalize
            ylab = "Magnetic moment (Am^2)"
        xlab = "Temperature (C)"
        pmagplotlib.plot_xy(FIG['lowrie'], Temps, abs(carts[0]) / nrm, sym='r-')
        pmagplotlib.plot_xy(FIG['lowrie'], Temps, abs(carts[0]) / nrm, sym='ro')  # X direction
        pmagplotlib.plot_xy(FIG['lowrie'], Temps, abs(carts[1]) / nrm, sym='c-')
        pmagplotlib.plot_xy(FIG['lowrie'], Temps, abs(carts[1]) / nrm, sym='cs')  # Y direction
        pmagplotlib.plot_xy(FIG['lowrie'], Temps, abs(carts[2]) / nrm, sym='k-')
        pmagplotlib.plot_xy(FIG['lowrie'], Temps, abs(carts[2]) / nrm, sym='k^', title=spc, xlab=xlab, ylab=ylab)  # Z direction
        files = {'lowrie': 'lowrie:_'+spc+'_.'+fmt}
        if plot == 0:
            pmagplotlib.draw_figs(FIG)
            ans = input('S[a]ve figure? [q]uit, <return> to continue   ')
            if ans == 'a':
                pmagplotlib.save_plots(FIG, files)
            elif ans == 'q':
                sys.exit()
        else:
            pmagplotlib.save_plots(FIG, files)
        pmagplotlib.clearFIG(FIG['lowrie'])
Exemple #14
0
def main():
    """
    NAME
        lowrie_magic.py

    DESCRIPTION
       plots intensity decay curves for Lowrie experiments

    SYNTAX 
        lowrie_magic.py -h [command line options]
    
    INPUT 
       takes magic_measurements formatted input files
    
    OPTIONS
        -h prints help message and quits
        -f FILE: specify input file, default is magic_measurements.txt
        -N do not normalize by maximum magnetization
        -fmt [svg, pdf, eps, png] specify fmt, default is svg
        -sav saves plots and quits
    """
    fmt, plot = 'svg', 0
    FIG = {}  # plot dictionary
    FIG['lowrie'] = 1  # demag is figure 1
    pmagplotlib.plot_init(FIG['lowrie'], 6, 6)
    norm = 1  # default is to normalize by maximum axis
    in_file, dir_path = 'magic_measurements.txt', '.'
    if len(sys.argv) > 1:
        if '-WD' in sys.argv:
            ind = sys.argv.index('-WD')
            dir_path = sys.argv[ind + 1]
        if '-h' in sys.argv:
            print(main.__doc__)
            sys.exit()
        if '-N' in sys.argv: norm = 0  # don't normalize
        if '-sav' in sys.argv: plot = 1  # don't normalize
        if '-fmt' in sys.argv:  # sets input filename
            ind = sys.argv.index("-fmt")
            fmt = sys.argv[ind + 1]
        if '-f' in sys.argv:  # sets input filename
            ind = sys.argv.index("-f")
            in_file = sys.argv[ind + 1]
    else:
        print(main.__doc__)
        print('you must supply a file name')
        sys.exit()
    in_file = dir_path + '/' + in_file
    print(in_file)
    PmagRecs, file_type = pmag.magic_read(in_file)
    if file_type != "magic_measurements":
        print('bad input file')
        sys.exit()
    PmagRecs = pmag.get_dictitem(PmagRecs, 'magic_method_codes', 'LP-IRM-3D',
                                 'has')  # get all 3D IRM records
    if len(PmagRecs) == 0:
        print('no records found')
        sys.exit()
    specs = pmag.get_dictkey(PmagRecs, 'er_specimen_name', '')
    sids = []
    for spec in specs:
        if spec not in sids:
            sids.append(spec)  # get list of unique specimen names
    for spc in sids:  # step through the specimen names
        print(spc)
        specdata = pmag.get_dictitem(PmagRecs, 'er_specimen_name', spc,
                                     'T')  # get all this one's data
        DIMs, Temps = [], []
        for dat in specdata:  # step through the data
            DIMs.append([
                float(dat['measurement_dec']),
                float(dat['measurement_inc']),
                float(dat['measurement_magn_moment'])
            ])
            Temps.append(float(dat['treatment_temp']) - 273.)
        carts = pmag.dir2cart(DIMs).transpose()
        if norm == 1:  # want to normalize
            nrm = (DIMs[0][2])  # normalize by NRM
            ylab = "M/M_o"
        else:
            nrm = 1.  # don't normalize
            ylab = "Magnetic moment (Am^2)"
        xlab = "Temperature (C)"
        pmagplotlib.plotXY(FIG['lowrie'],
                           Temps,
                           old_div(abs(carts[0]), nrm),
                           sym='r-')
        pmagplotlib.plotXY(FIG['lowrie'],
                           Temps,
                           old_div(abs(carts[0]), nrm),
                           sym='ro')  # X direction
        pmagplotlib.plotXY(FIG['lowrie'],
                           Temps,
                           old_div(abs(carts[1]), nrm),
                           sym='c-')
        pmagplotlib.plotXY(FIG['lowrie'],
                           Temps,
                           old_div(abs(carts[1]), nrm),
                           sym='cs')  # Y direction
        pmagplotlib.plotXY(FIG['lowrie'],
                           Temps,
                           old_div(abs(carts[2]), nrm),
                           sym='k-')
        pmagplotlib.plotXY(FIG['lowrie'],
                           Temps,
                           old_div(abs(carts[2]), nrm),
                           sym='k^',
                           title=spc,
                           xlab=xlab,
                           ylab=ylab)  # Z direction
        files = {'lowrie': 'lowrie:_' + spc + '_.' + fmt}
        if plot == 0:
            pmagplotlib.drawFIGS(FIG)
            ans = input('S[a]ve figure? [q]uit, <return> to continue   ')
            if ans == 'a':
                pmagplotlib.saveP(FIG, files)
            elif ans == 'q':
                sys.exit()
        else:
            pmagplotlib.saveP(FIG, files)
        pmagplotlib.clearFIG(FIG['lowrie'])
Exemple #15
0
def main():
    """
    NAME 
        plot_map_pts.py 

    DESCRIPTION
        plots points on map
 
    SYNTAX
        plot_map_pts.py [command line options]

    OPTIONS
        -h prints help and quits
        -sym [ro, bs, g^, r., b-, etc.] [1,5,10] symbol and size for points
           colors are r=red,b=blue,g=green, etc.
           symbols are '.' for points, ^, for triangle, s for square, etc.
            -, for lines, -- for dotted lines, see matplotlib online documentation for plot()
        -eye  ELAT ELON [specify eyeball location]
        -etp  put on topography
        -f FILE, specify input file
        -o color ocean blue/land green (default is not)
        -res [c,l,i,h] specify resolution (crude, low, intermediate, high]
        -fmt [pdf,eps, png] specify output format (default is pdf)
        -R don't plot details of rivers
        -B don't plot national/state boundaries, etc.
        -pad [LAT LON] pad bounding box by LAT/LON (default is not)
        -grd SPACE specify grid spacing
        -sav  save plot and quit
        -prj PROJ,  specify one of the supported projections: (see basemap.py online documentation)
            aeqd = Azimuthal Equidistant
            poly = Polyconic
            gnom = Gnomonic
            moll = Mollweide
            tmerc = Transverse Mercator
            nplaea = North-Polar Lambert Azimuthal
            mill = Miller Cylindrical
            merc = Mercator
            stere = Stereographic
            npstere = North-Polar Stereographic
            geos = Geostationary
            laea = Lambert Azimuthal Equal Area
            sinu = Sinusoidal
            spstere = South-Polar Stereographic
            lcc = Lambert Conformal
            npaeqd = North-Polar Azimuthal Equidistant
            eqdc = Equidistant Conic
            cyl = Cylindrical Equidistant
            omerc = Oblique Mercator
            aea = Albers Equal Area
            spaeqd = South-Polar Azimuthal Equidistant
            ortho = Orthographic
            cass= Cassini-Soldner
            splaea = South-Polar Lambert Azimuthal
            robin = Robinson
        Special codes for MagIC formatted input files:
            -n
            -l
    
    INPUTS
        space or tab delimited LON LAT data
        OR: 
           standard MagIC formatted er_sites or pmag_results table
    DEFAULTS
        res:  c
        prj: mollweide;  lcc for MagIC format files 
        ELAT,ELON = 0,0
        pad LAT,LON=0,0
        NB: high resolution or lines can be very slow
    
    """
    dir_path='.'
    plot=0
    ocean=0
    res='c'
    proj='moll'
    Lats,Lons=[],[]
    fmt='pdf'
    sym='ro'
    symsize=5
    fancy=0
    rivers,boundaries,ocean=1,1,0
    latmin,latmax,lonmin,lonmax,lat_0,lon_0=-90,90,0.,360.,0.,0.
    padlat,padlon,gridspace=0,0,30
    lat_0,lon_0="",""
    prn_name,prn_loc,names,locs=0,0,[],[]
    if '-WD' in sys.argv:
        ind = sys.argv.index('-WD')
        dir_path=sys.argv[ind+1]
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-fmt' in sys.argv:
        ind = sys.argv.index('-fmt')
        fmt=sys.argv[ind+1]
    if '-res' in sys.argv:
        ind = sys.argv.index('-res')
        res=sys.argv[ind+1]
        if res!= 'c' and res!='l':
            print('this resolution will take a while - be patient')
    if '-etp' in sys.argv: fancy=1
    if '-sav' in sys.argv: plot=1
    if '-R' in sys.argv:rivers=0
    if '-B' in sys.argv:boundaries=0
    if '-o' in sys.argv:ocean=1
    if '-grd' in sys.argv:
        ind = sys.argv.index('-grd')
        gridspace=float(sys.argv[ind+1])
    if '-eye' in sys.argv:
        ind = sys.argv.index('-eye')
        lat_0=float(sys.argv[ind+1])
        lon_0=float(sys.argv[ind+2])
    if '-sym' in sys.argv:
        ind = sys.argv.index('-sym')
        sym=sys.argv[ind+1]
        symsize=int(sys.argv[ind+2])
    if '-pad' in sys.argv:
        ind = sys.argv.index('-pad')
        padlat=float(sys.argv[ind+1])
        padlon=float(sys.argv[ind+2])
    if '-f' in sys.argv:
        ind = sys.argv.index('-f')
        file=dir_path+'/'+sys.argv[ind+1]
        header=open(file,'r').readlines()[0].split('\t')
        if 'tab' in header[0]:
            if '-n' in sys.argv:prn_name=1
            if '-l' in sys.argv:prn_loc=1
            proj='lcc'
            if 'results' in header[1]:
                latkey='average_lat'
                lonkey='average_lon'
                namekey='pmag_result_name'
                lockey='er_location_names'
            elif 'sites' in header[1]:
                latkey='site_lat'
                lonkey='site_lon'
                namekey='er_site_name'
                lockey='er_location_name'
            else:  
                print('file type not supported')
                print(main.__doc__)
                sys.exit()
            Sites,file_type=pmag.magic_read(file)
            Lats=pmag.get_dictkey(Sites,latkey,'f')
            Lons=pmag.get_dictkey(Sites,lonkey,'f')
            if prn_name==1:names=pmag.get_dictkey(Sites,namekey,'')
            if prn_loc==1:names=pmag.get_dictkey(Sites,lockey,'')
        else:
            ptdata=numpy.loadtxt(file)
            Lons=ptdata.transpose()[0]
            Lats=ptdata.transpose()[1]
        latmin=numpy.min(Lats)-padlat
        lonmin=numpy.min(Lons)-padlon
        latmax=numpy.max(Lats)+padlat
        lonmax=numpy.max(Lons)+padlon
        if lon_0=="":
            lon_0=0.5*(lonmin+lonmax)
            lat_0=0.5*(latmin+latmax)
    else:
        print("input file must be specified")
        sys.exit()
    if '-prj' in sys.argv:
        ind = sys.argv.index('-prj')
        proj=sys.argv[ind+1]
    FIG={'map':1}
    pmagplotlib.plot_init(FIG['map'],6,6)
    if res=='c':skip=8
    if res=='l':skip=5
    if res=='i':skip=2
    if res=='h':skip=1
    cnt=0
    Opts={'latmin':latmin,'latmax':latmax,'lonmin':lonmin,'lonmax':lonmax,'lat_0':lat_0,'lon_0':lon_0,'proj':proj,'sym':sym,'symsize':3,'pltgrid':1,'res':res,'boundinglat':0.,'padlon':padlon,'padlat':padlat,'gridspace':gridspace}
    Opts['details']={}
    Opts['details']['coasts']=1
    Opts['details']['rivers']=rivers
    Opts['details']['states']=boundaries
    Opts['details']['countries']=boundaries
    Opts['details']['ocean']=ocean
    Opts['details']['fancy']=fancy
    if len(names)>0:Opts['names']=names
    if len(locs)>0:Opts['loc_name']=locs
    if proj=='merc':
        Opts['latmin']=-70
        Opts['latmax']=70
        Opts['lonmin']=-180
        Opts['lonmax']=180
    print('please wait to draw points')
    Opts['sym']=sym
    Opts['symsize']=symsize
    pmagplotlib.plotMAP(FIG['map'],Lats,Lons,Opts)
    files={}
    titles={}
    titles['map']='PT Map'
    for key in list(FIG.keys()):
        files[key]='Map_PTS'+'.'+fmt
    if pmagplotlib.isServer:
        black     = '#000000'
        purple    = '#800080'
        FIG = pmagplotlib.addBorders(FIG,titles,black,purple)
        pmagplotlib.saveP(FIG,files)
    if plot==1:
        pmagplotlib.saveP(FIG,files)
    else:
        pmagplotlib.drawFIGS(FIG)
        ans=input(" S[a]ve to save plot, Return to quit:  ")
        if ans=="a": pmagplotlib.saveP(FIG,files)
def main():
    """
    NAME
        make_magic_plots.py

    DESCRIPTION
    inspects magic directory for available plots.

    SYNTAX
        make_magic_plots.py [command line options]

    INPUT
        magic files

    OPTIONS
        -h prints help message and quits
        -f FILE specifies input file name
        -fmt [png,eps,svg,jpg,pdf] specify format, default is png
        -DM [2,3] define data model
    """
    dirlist=['./']
    dir_path=os.getcwd()
    names=os.listdir(dir_path)
    for n in names:
        if 'Location' in n:
            dirlist.append(n)
    if '-fmt' in sys.argv:
        ind=sys.argv.index("-fmt")
        fmt=sys.argv[ind+1]
    else: fmt='png'
    if '-f' in sys.argv:
        ind=sys.argv.index("-f")
        filelist=[sys.argv[ind+1]]
    else:
        filelist=os.listdir(dir_path)
    new_model=0
    if '-DM' in sys.argv:
        ind=sys.argv.index("-DM")
        data_model=sys.argv[ind+1]
        if data_model=='3': new_model=1
    if new_model:
            samp_file='samples.txt'
            azimuth_key='azimuth'
            meas_file='measurements.txt'
            loc_key='location'
            method_key='method_codes'
            dec_key='dir_dec'
            inc_key='dir_inc'
            Mkeys=['magnitude','magn_moment','magn_volume','magn_mass']
            results_file='sites.txt'
            tilt_key='direction_tilt_correction'
            hyst_file='specimens.txt'
            aniso_file='specimens.txt'
    else:
            new_model=0
            samp_file='er_samples.txt'
            azimuth_key='sample_azimuth'
            meas_file='magic_measurements.txt'
            loc_key='er_location_name'
            method_key='magic_method_codes'
            dec_key='measurement_dec'
            inc_key='measurement_inc'
            Mkeys=['measurement_magnitude','measurement_magn_moment','measurement_magn_volume','measurement_magn_mass']
            results_file='pmag_results.txt'
            tilt_key='tilt_correction'
            hyst_file='rmag_hysteresis'
            aniso_file='rmag_anisotropy'
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    for loc in dirlist:
        print('working on: ',loc)
        os.chdir(loc) # change working directories to each location
        crd='s'
        print(samp_file)
        if samp_file in filelist: # find coordinate systems
            print('found sample file')
            samps,file_type=pmag.magic_read(samp_file) # read in data
            Srecs=pmag.get_dictitem(samps,azimuth_key,'','F')# get all none blank sample orientations
            if len(Srecs)>0:
                crd='g'
        if meas_file in filelist: # start with measurement data
            print('working on measurements data')
            data,file_type=pmag.magic_read(meas_file) # read in data
            if loc == './': data=pmag.get_dictitem(data,loc_key,'','T') # get all the blank location names from data file
            # looking for  zeq_magic possibilities
            AFZrecs=pmag.get_dictitem(data,method_key,'LT-AF-Z','has')# get all none blank method codes
            TZrecs=pmag.get_dictitem(data,method_key,'LT-T-Z','has')# get all none blank method codes
            MZrecs=pmag.get_dictitem(data,method_key,'LT-M-Z','has')# get all none blank method codes
            Drecs=pmag.get_dictitem(data,dec_key,'','F') # get all dec measurements
            Irecs=pmag.get_dictitem(data,inc_key,'','F') # get all inc measurements
            for key in Mkeys:
                Mrecs=pmag.get_dictitem(data,key,'','F') # get intensity data
                if len(Mrecs)>0:break
            if len(AFZrecs)>0 or len(TZrecs)>0 or len(MZrecs)>0 and len(Drecs)>0 and len(Irecs)>0 and len(Mrecs)>0: # potential for stepwise demag curves
                if new_model:
                    CMD = 'zeq_magic3.py -fsp specimens.txt -sav -fmt '+fmt+' -crd '+crd
                else:
                    CMD='zeq_magic.py -fsp pmag_specimens.txt -sav -fmt '+fmt+' -crd '+crd
                print(CMD)
                os.system(CMD)
            # looking for  thellier_magic possibilities
            if len(pmag.get_dictitem(data,method_key,'LP-PI-TRM','has'))>0:
                if new_model:
                    CMD= 'thellier_magic3.py -fsp specimens.txt -sav -fmt '+fmt
                else:
                    CMD= 'thellier_magic.py -fsp pmag_specimens.txt -sav -fmt '+fmt
                print(CMD)
                os.system(CMD)
            # looking for hysteresis possibilities
            if len(pmag.get_dictitem(data,method_key,'LP-HYS','has'))>0: # find hyst experiments
                if new_model:
                    CMD= 'quick_hyst3.py -sav -fmt '+fmt
                else:
                    CMD= 'quick_hyst.py -sav -fmt '+fmt
                print(CMD)
                os.system(CMD)
        if results_file in filelist: # start with measurement data
            data,file_type=pmag.magic_read(results_file) # read in data
            print('number of datapoints: ',len(data))
            if loc == './': data=pmag.get_dictitem(data,loc_key,':','has') # get all the concatenated location names from data file
            print('number of datapoints: ',len(data) ,loc)
            if new_model:
                print('working on site directions')
                dec_key='dir_dec'
                inc_key='dir_inc'
                int_key='int_abs'
            else:
                print('working on results directions')
                dec_key='average_dec'
                inc_key='average_inc'
                int_key='average_int'
            SiteDIs=pmag.get_dictitem(data,dec_key,"",'F') # find decs
            SiteDIs=pmag.get_dictitem(SiteDIs,inc_key,"",'F') # find decs and incs
            SiteDIs=pmag.get_dictitem(SiteDIs,'data_type','i','has') # only individual results - not poles
            print('number of directions: ',len(SiteDIs))
            SiteDIs_t=pmag.get_dictitem(SiteDIs,tilt_key,'100','T')# tilt corrected coordinates
            print('number of tilt corrected directions: ',len(SiteDIs))
            SiteDIs_g=pmag.get_dictitem(SiteDIs,tilt_key,'0','T')# geographic coordinates
            SiteDIs_s=pmag.get_dictitem(SiteDIs,'tilt_correction','-1','T')# sample coordinates
            SiteDIs_x=pmag.get_dictitem(SiteDIs,'tilt_correction','','T')# no coordinates
            if len(SiteDIs_t)>0 or len(SiteDIs_g) >0 or len(SiteDIs_s)>0 or len(SiteDIs_x)>0:
                CRD=""
                if len(SiteDIs_t)>0:
                    CRD=' -crd t'
                elif len(SiteDIs_g )>0:
                    CRD=' -crd g'
                elif len(SiteDIs_s )>0:
                    CRD=' -crd s'
                if new_model:
                    CMD= 'eqarea_magic3.py -sav -crd t -fmt '+fmt +CRD
                else:
                    CMD= 'eqarea_magic.py -sav -crd t -fmt '+fmt +CRD
                print(CMD)
                os.system(CMD)
            print('working on VGP map')
            VGPs=pmag.get_dictitem(SiteDIs,'vgp_lat',"",'F') # are there any VGPs?
            if len(VGPs)>0:  # YES!
                os.system('vgpmap_magic.py -prj moll -res c -sym ro 5 -sav -fmt png')
            print('working on intensities')
            if not new_model:
                CMD='magic_select.py -f '+results_file+' -key data_type i T -F tmp.txt'
                os.system(CMD)
                infile=' tmp.txt'
            else: infile=results_file
            print(int_key)
            CMD='magic_select.py  -key '+int_key +' 0. has -F tmp1.txt -f '+infile
            os.system(CMD)
            CMD="grab_magic_key.py -f tmp1.txt -key "+int_key+ " | awk '{print $1*1e6}' >tmp2.txt"
            os.system(CMD)
            data,file_type=pmag.magic_read('tmp1.txt') # read in data
            if new_model:
                locations=pmag.get_dictkey(data,loc_key,"")
            else:
                locations=pmag.get_dictkey(data,loc_key+'s',"")
            histfile='LO:_'+locations[0]+'_intensities_histogram:_.'+fmt
            os.system("histplot.py -b 1 -xlab 'Intensity (uT)' -sav -f tmp2.txt -F " +histfile)
            print("histplot.py -b 1 -xlab 'Intensity (uT)' -sav -f tmp2.txt -F " +histfile)
            os.system('rm tmp*.txt')
        if hyst_file in filelist: # start with measurement data
            print('working on hysteresis')
            data,file_type=pmag.magic_read(hyst_file) # read in data
            if loc == './': data=pmag.get_dictitem(data,loc_key,'','T') # get all the blank location names from data file
            hdata=pmag.get_dictitem(data,'hysteresis_bcr','','F')
            hdata=pmag.get_dictitem(hdata,'hysteresis_mr_moment','','F')
            hdata=pmag.get_dictitem(hdata,'hysteresis_ms_moment','','F')
            hdata=pmag.get_dictitem(hdata,'hysteresis_bc','','F') # there are data for a dayplot
            if len(hdata)>0:
                print('dayplot_magic.py -sav -fmt '+fmt)
                os.system('dayplot_magic.py -sav -fmt '+fmt)
        if aniso_file in filelist: # do anisotropy plots if possible
            print('working on anisotropy')
            data,file_type=pmag.magic_read(aniso_file) # read in data
            if loc == './': data=pmag.get_dictitem(data,loc_key,'','T') # get all the blank location names from data file
            sdata=pmag.get_dictitem(data,'anisotropy_tilt_correction','-1','T') # get specimen coordinates
            gdata=pmag.get_dictitem(data,'anisotropy_tilt_correction','0','T') # get specimen coordinates
            tdata=pmag.get_dictitem(data,'anisotropy_tilt_correction','100','T') # get specimen coordinates
            CRD=""
            if new_model:
                CMD= 'aniso_magic3.py -x -B -sav -fmt '+fmt
            else:
                CMD= 'aniso_magic.py -x -B -sav -fmt '+fmt
            if len(sdata)>3:
                CMD=CMD+' -crd s'
                print(CMD)
                os.system(CMD)
            if len(gdata)>3:
                CMD=CMD+' -crd g'
                print(CMD)
                os.system(CMD)
            if len(tdata)>3:
                CMD=CMD+' -crd t'
                print(CMD)
                os.system(CMD)
        if loc!='./':os.chdir('..') # change working directories to each location
Exemple #17
0
def main():
    """
    NAME
        lowrie_magic.py

    DESCRIPTION
       plots intensity decay curves for Lowrie experiments

    SYNTAX
        lowrie_magic.py -h [command line options]

    INPUT
       takes measurements formatted input files

    OPTIONS
        -h prints help message and quits
        -f FILE: specify input file, default is magic_measurements.txt
        -N do not normalize by maximum magnetization
        -fmt [svg, pdf, eps, png] specify fmt, default is svg
        -sav saves plots and quits
        -DM [2, 3] MagIC data model number
    """
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if len(sys.argv) <= 1:
        print(main.__doc__)
        print('you must supply a file name')
        sys.exit()
    FIG = {}  # plot dictionary
    FIG['lowrie'] = 1  # demag is figure 1
    pmagplotlib.plot_init(FIG['lowrie'], 6, 6)
    norm = 1  # default is to normalize by maximum axis
    in_file = pmag.get_named_arg("-f", "measurements.txt")
    dir_path = pmag.get_named_arg("-WD", ".")
    in_file = pmag.resolve_file_name(in_file, dir_path)
    data_model = pmag.get_named_arg("-DM", 3)
    data_model = int(float(data_model))
    fmt = pmag.get_named_arg("-fmt", "svg")
    if '-N' in sys.argv:
        norm = 0  # don't normalize
    if '-sav' in sys.argv:
        plot = 1  # silently save and quit
    else:
        plot = 0  # generate plots
    print(in_file)
    # read in data
    PmagRecs, file_type = pmag.magic_read(in_file)
    if data_model == 2 and file_type != "magic_measurements":
        print('bad input file', file_type)
        sys.exit()
    if data_model == 3 and file_type != "measurements":
        print('bad input file', file_type)
        sys.exit()

    if data_model == 2:
        meth_code_col = 'magic_method_codes'
        spec_col = 'er_specimen_name'
        dec_col = "measurement_dec"
        inc_col = 'measurement_inc'
        moment_col = 'measurement_magn_moment'
        temp_col = 'treatment_temp'
    else:
        meth_code_col = 'method_codes'
        spec_col = 'specimen'
        dec_col = 'dir_dec'
        inc_col = 'dir_inc'
        moment_col = 'magn_moment'
        temp_col = "treat_temp"

    PmagRecs = pmag.get_dictitem(PmagRecs, meth_code_col, 'LP-IRM-3D',
                                 'has')  # get all 3D IRM records

    if len(PmagRecs) == 0:
        print('no records found with the method code LP-IRM-3D')
        sys.exit()

    specs = pmag.get_dictkey(PmagRecs, spec_col, '')
    sids = []
    for spec in specs:
        if spec not in sids:
            sids.append(spec)  # get list of unique specimen names
    for spc in sids:  # step through the specimen names
        print(spc)
        specdata = pmag.get_dictitem(PmagRecs, spec_col, spc,
                                     'T')  # get all this one's data

        DIMs, Temps = [], []
        for dat in specdata:  # step through the data
            DIMs.append([
                float(dat[dec_col]),
                float(dat[inc_col]),
                float(dat[moment_col])
            ])
            Temps.append(float(dat[temp_col]) - 273.)
        carts = pmag.dir2cart(DIMs).transpose()
        if norm == 1:  # want to normalize
            nrm = (DIMs[0][2])  # normalize by NRM
            ylab = "M/M_o"
        else:
            nrm = 1.  # don't normalize
            ylab = "Magnetic moment (Am^2)"
        xlab = "Temperature (C)"
        pmagplotlib.plot_xy(FIG['lowrie'],
                            Temps,
                            abs(carts[0]) / nrm,
                            sym='r-')
        pmagplotlib.plot_xy(FIG['lowrie'],
                            Temps,
                            abs(carts[0]) / nrm,
                            sym='ro')  # X direction
        pmagplotlib.plot_xy(FIG['lowrie'],
                            Temps,
                            abs(carts[1]) / nrm,
                            sym='c-')
        pmagplotlib.plot_xy(FIG['lowrie'],
                            Temps,
                            abs(carts[1]) / nrm,
                            sym='cs')  # Y direction
        pmagplotlib.plot_xy(FIG['lowrie'],
                            Temps,
                            abs(carts[2]) / nrm,
                            sym='k-')
        pmagplotlib.plot_xy(FIG['lowrie'],
                            Temps,
                            abs(carts[2]) / nrm,
                            sym='k^',
                            title=spc,
                            xlab=xlab,
                            ylab=ylab)  # Z direction
        files = {'lowrie': 'lowrie:_' + spc + '_.' + fmt}
        if plot == 0:
            pmagplotlib.draw_figs(FIG)
            ans = input('S[a]ve figure? [q]uit, <return> to continue   ')
            if ans == 'a':
                pmagplotlib.save_plots(FIG, files)
            elif ans == 'q':
                sys.exit()
        else:
            pmagplotlib.save_plots(FIG, files)
        pmagplotlib.clearFIG(FIG['lowrie'])