Instruction:
Can you generate the documentation for the following Python 3 function
def huji(magfile="", dir_path=".", input_dir_path="", datafile="", codelist="",
         meas_file="measurements.txt", spec_file="specimens.txt",
         samp_file="samples.txt", site_file="sites.txt", loc_file="locations.txt",
         user="", specnum=0, samp_con="1", labfield=0, phi=0, theta=0,
         location="", CR_cooling_times=None, noave=False):
    """
    Convert HUJI format file to MagIC file(s)

    Parameters
    ----------
    magfile : str
       input file name
    dir_path : str
        working directory, default "."
    input_dir_path : str
        input file directory IF different from dir_path, default ""
    datafile : str
       HUJI datafile with sample orientations, default ""
    codelist : str
        colon-delimited protocols, include all that apply
        see info below
    meas_file : str
        output measurement file name, default "measurements.txt"
    spec_file : str
        output specimen file name, default "specimens.txt"
    samp_file: str
        output sample file name, default "samples.txt"
    site_file : str
        output site file name, default "sites.txt"
    loc_file : str
        output location file name, default "locations.txt"
    user : str
        user name, default ""
    specnum : int
        number of characters to designate a specimen, default 0
    samp_con : str
        sample/site naming convention, default '1', see info below
    labfield : float
        dc lab field (in micro tesla)
    labfield_phi : float
        declination 0-360
    labfield_theta : float
        inclination -90 - 90
    location : str
        location name, default "unknown"
    CR_cooling_times : list
        default None
        cooling times in [K/minutes] seperated by comma,
        ordered at the same order as XXX.10,XXX.20 ...XX.70
    noave : bool
       do not average duplicate measurements, default False (so by default, DO average)

    Info
    --------
    Code list:
        AF:  af demag
        T: thermal including thellier but not trm acquisition
        N: NRM only
        TRM: trm acquisition
        ANI: anisotropy experiment
        CR: cooling rate experiment.
            The treatment coding of the measurement file should be: XXX.00,XXX.10, XXX.20 ...XX.70 etc. (XXX.00 is optional)
            where XXX in the temperature and .10,.20... are running numbers of the cooling rates steps.
            XXX.00 is optional zerofield baseline. XXX.70 is alteration check.
            syntax in sio_magic is: -LP CR xxx,yyy,zzz,.....xx
            where xx, yyy,zzz...xxx  are cooling time in [K/minutes], seperated by comma, ordered at the same order as XXX.10,XXX.20 ...XX.70
            if you use a zerofield step then no need to specify the cooling rate for the zerofield

    Sample naming convention:
        [1] XXXXY: where XXXX is an arbitrary length site designation and Y
            is the single character sample designation.  e.g., TG001a is the
            first sample from site TG001.    [default]
        [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
        [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
        [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
        [5] site name = sample name
        [6] site name entered in site_name column in the orient.txt format input file  -- NOT CURRENTLY SUPPORTED
        [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY

    """

    # format and validate variables
    specnum = int(specnum)
    labfield = float(labfield) * 1e-6
    phi = int(theta)
    theta = int(theta)
    input_dir_path, dir_path = pmag.fix_directories(input_dir_path, dir_path)
    if magfile:
        try:
            fname = pmag.resolve_file_name(magfile, input_dir_path)
            infile = open(fname, 'r')
        except IOError as ex:
            print(ex)
            print("bad mag file name")
            return False, "bad mag file name"
    else:
        print("mag_file field is a required option")
        return False, "mag_file field is a required option"

    if specnum != 0:
        specnum = -specnum
    if "4" in samp_con:
        if "-" not in samp_con:
            print("option [4] must be in form 4-Z where Z is an integer")
            return False, "option [4] must be in form 4-Z where Z is an integer"
        else:
            Z = int(samp_con.split("-")[1])
            samp_con = "4"
    if "7" in samp_con:
        if "-" not in samp_con:
            print("option [7] must be in form 7-Z where Z is an integer")
            return False, "option [7] must be in form 7-Z where Z is an integer"
        else:
            Z = int(samp_con.split("-")[1])
            samp_con = "7"
    else:
        Z = 1

    if codelist:
        codes = codelist.split(':')
    else:
        print(
            "-E- Must select experiment type (codelist/-LP, options are: [AF, T, ANI, TRM, CR])")
        return False, "Must select experiment type (codelist/-LP, options are: [AF, T, ANI, TRM, CR])"
    if "AF" in codes:
        demag = 'AF'
        LPcode = "LP-DIR-AF"
    if "T" in codes:
        demag = "T"
        if not labfield:
            LPcode = "LP-DIR-T"
        if labfield:
            LPcode = "LP-PI-TRM"
        if "ANI" in codes:
            if not labfield:
                print("missing lab field option")
                return False, "missing lab field option"
            LPcode = "LP-AN-TRM"

    if "TRM" in codes:
        demag = "T"
        LPcode = "LP-TRM"
        # trm=1

    if "CR" in codes:
        demag = "T"
        # dc should be in the code
        if not labfield:
            print("missing lab field option")
            return False, "missing lab field option"

        LPcode = "LP-CR-TRM"  # TRM in different cooling rates
        if command_line:
            ind = sys.argv.index("-LP")
            CR_cooling_times = sys.argv[ind+2].split(",")

    version_num = pmag.get_version()

    # --------------------------------------
    # Read the file
    # Assumption:
    # 1. different lab protocolsa are in different files
    # 2. measurements are in the correct order
    # --------------------------------------

    Data = {}
    line_no = 0
    for line in infile.readlines():
        line_no += 1
        this_line_data = {}
        line_no += 1
        instcode = ""
        if len(line) < 2:
            continue
        if line[0] == "#":  # HUJI way of marking bad data points
            continue

        rec = line.strip('\n').split()
        specimen = rec[0]
        date = rec[2].split("/")
        hour = rec[3].split(":")
        treatment_type = rec[4]
        treatment = rec[5].split(".")
        dec_core = rec[6]
        inc_core = rec[7]
        moment_emu = float(rec[-1])

        if specimen not in list(Data.keys()):
            Data[specimen] = []

        # check duplicate treatments:
        # if yes, delete the first and use the second

        if len(Data[specimen]) > 0:
            if treatment == Data[specimen][-1]['treatment']:
                del(Data[specimen][-1])
                print("-W- Identical treatments in file %s magfile line %i: specimen %s, treatment %s ignoring the first. " %
                      (magfile, line_no, specimen, ".".join(treatment)))

        this_line_data = {}
        this_line_data['specimen'] = specimen
        this_line_data['date'] = date
        this_line_data['hour'] = hour
        this_line_data['treatment_type'] = treatment_type
        this_line_data['treatment'] = treatment
        this_line_data['dec_core'] = dec_core
        this_line_data['inc_core'] = inc_core
        this_line_data['moment_emu'] = moment_emu
        this_line_data['azimuth'] = ''
        this_line_data['dip'] = ''
        this_line_data['bed_dip_direction'] = ''
        this_line_data['bed_dip'] = ''
        this_line_data['lat'] = ''
        this_line_data['lon'] = ''
        this_line_data['volume'] = ''
        Data[specimen].append(this_line_data)
    infile.close()
    print("-I- done reading file %s" % magfile)

    if datafile:
        dinfile = open(datafile)
        for line in dinfile.readlines():
            data = line.split()
            if len(data) < 8 or data[0] == '':
                continue
            elif data[0] in list(Data.keys()):
                for i in range(len(Data[data[0]])):
                    Data[data[0]][i]['azimuth'] = data[1]
                    Data[data[0]][i]['dip'] = data[2]
                    try:
                        Data[data[0]][i]['bed_dip_direction'] = float(
                            data[3])+90
                    except ValueError:
                        Data[data[0]][i]['bed_dip_direction'] = ''
                    Data[data[0]][i]['bed_dip'] = data[4]
                    Data[data[0]][i]['lat'] = data[5]
                    Data[data[0]][i]['lon'] = data[6]
                    Data[data[0]][i]['volume'] = data[7]
            else:
                print(
                    "no specimen %s found in magnetometer data file when reading specimen orientation data file, or data file record for specimen too short" % data[0])
        dinfile.close()

    # --------------------------------------
    # Convert to MagIC
    # --------------------------------------

    specimens_list = list(Data.keys())
    specimens_list.sort()

    MeasRecs, SpecRecs, SampRecs, SiteRecs, LocRecs = [], [], [], [], []
    for specimen in specimens_list:
        for i in range(len(Data[specimen])):
            this_line_data = Data[specimen][i]
            methcode = ""
            MeasRec, SpecRec, SampRec, SiteRec, LocRec = {}, {}, {}, {}, {}
            specimen = this_line_data['specimen']
            if specnum != 0:
                sample = this_line_data['specimen'][:specnum]
            else:
                sample = this_line_data['specimen']
            site = pmag.parse_site(sample, samp_con, Z)
            if not location:
                location = site
            if specimen != "" and specimen not in [x['specimen'] if 'specimen' in list(x.keys()) else "" for x in SpecRecs]:
                SpecRec['specimen'] = specimen
                SpecRec['sample'] = sample
                SpecRecs.append(SpecRec)
            if sample != "" and sample not in [x['sample'] if 'sample' in list(x.keys()) else "" for x in SampRecs]:
                SampRec['sample'] = sample
                SampRec['site'] = site
                SampRec['azimuth'] = this_line_data['azimuth']
                SampRec['dip'] = this_line_data['dip']
                SampRec['bed_dip_direction'] = this_line_data['bed_dip_direction']
                SampRec['bed_dip'] = this_line_data['bed_dip']
                SampRecs.append(SampRec)
            if site != "" and site not in [x['site'] if 'site' in list(x.keys()) else "" for x in SiteRecs]:
                SiteRec['site'] = site
                SiteRec['location'] = location
                SiteRec['lat'] = this_line_data['lat']
                SiteRec['lon'] = this_line_data['lon']
                SiteRecs.append(SiteRec)
            if location != "" and location not in [x['location'] if 'location' in list(x.keys()) else "" for x in LocRecs]:
                LocRec['location'] = location
                LocRec['lat_n'] = this_line_data['lat']
                LocRec['lon_e'] = this_line_data['lon']
                LocRec['lat_s'] = this_line_data['lat']
                LocRec['lon_w'] = this_line_data['lon']
                LocRecs.append(LocRec)

            MeasRec['specimen'] = specimen
            MeasRec["meas_temp"] = '%8.3e' % (273)  # room temp in kelvin
            MeasRec["magn_moment"] = '%10.3e' % (
                float(this_line_data['moment_emu'])*1e-3)  # moment in Am^2 (from emu)
            MeasRec["dir_dec"] = this_line_data['dec_core']
            MeasRec["dir_inc"] = this_line_data['inc_core']

            date = this_line_data['date']
            hour = this_line_data['hour']
            if len(date[2]) < 4 and float(date[2]) >= 70:
                yyyy = "19"+date[2]
            elif len(date[2]) < 4 and float(date[2]) < 70:
                yyyy = "20"+date[2]
            else:
                yyyy = date[2]
            if len(date[0]) == 1:
                date[0] = "0"+date[0]
            if len(date[1]) == 1:
                date[1] = "0"+date[1]
            dt = ":".join([date[0], date[1], yyyy, hour[0], hour[1], "0"])
            local = pytz.timezone("America/New_York")
            naive = datetime.datetime.strptime(dt, "%m:%d:%Y:%H:%M:%S")
            local_dt = local.localize(naive, is_dst=None)
            utc_dt = local_dt.astimezone(pytz.utc)
            timestamp = utc_dt.strftime("%Y-%m-%dT%H:%M:%S")+"Z"

            MeasRec['analysts'] = user
            MeasRec["citations"] = "This study"
            MeasRec["instrument_codes"] = "HUJI-2G"
            MeasRec["quality"] = "g"
            MeasRec["meas_n_orient"] = "1"
            MeasRec["standard"] = "u"
            MeasRec["description"] = ""

            # ----------------------------------------
            # AF demag
            # do not support AARM yet
            # ----------------------------------------

            if demag == "AF":
                treatment_type = this_line_data['treatment_type']
                # demag in zero field
                if LPcode != "LP-AN-ARM":
                    MeasRec["treat_ac_field"] = '%8.3e' % (
                        float(this_line_data['treatment'][0])*1e-3)  # peak field in tesla
                    MeasRec["treat_dc_field"] = '0'
                    MeasRec["treat_dc_field_phi"] = '0'
                    MeasRec["treat_dc_field_theta"] = '0'
                    if treatment_type == "N":
                        methcode = "LP-DIR-AF:LT-NO"
                    elif treatment_type == "A":
                        methcode = "LP-DIR-AF:LT-AF-Z"
                    else:
                        print(
                            "ERROR in treatment field line %i... exiting until you fix the problem" % line_no)
                        print(this_line_data)
                        return False, "ERROR in treatment field line %i... exiting until you fix the problem" % line_no
                # AARM experiment
                else:
                    print("Don't support AARM in HUJI format yet. sorry... ")
                MeasRec["method_codes"] = methcode
                MeasRec["experiment"] = specimen + ":" + LPcode
                MeasRec["treat_step_num"] = "%i" % i
                MeasRec["description"] = ""

                MeasRecs.append(MeasRec)

            # ----------------------------------------
            # Thermal:
            # Thellier experiment: "IZ", "ZI", "IZZI", pTRM checks
            # Thermal demag
            # Thermal cooling rate experiment
            # Thermal NLT
            # ----------------------------------------

            if demag == "T":

                treatment = this_line_data['treatment']
                treatment_type = this_line_data['treatment_type']

                # ----------------------------------------
                # Thellier experimet
                # ----------------------------------------

                if LPcode == "LP-PI-TRM":  # Thelllier experiment
                    MeasRec["experiment"] = specimen + ":" + LPcode
                    methcode = LPcode
                    if treatment_type == "N" or ((treatment[1] == '0' or treatment[1] == '00') and float(treatment[0]) == 0):
                        LT_code = "LT-NO"
                        MeasRec["treat_dc_field_phi"] = '0'
                        MeasRec["treat_dc_field_theta"] = '0'
                        MeasRec["treat_dc_field"] = '0'
                        MeasRec["treat_temp"] = '273.'

                    elif treatment[1] == '0' or treatment[1] == '00':
                        LT_code = "LT-T-Z"
                        MeasRec["treat_dc_field_phi"] = '0'
                        MeasRec["treat_dc_field_theta"] = '0'
                        MeasRec["treat_dc_field"] = '%8.3e' % (0)
                        MeasRec["treat_temp"] = '%8.3e' % (
                            float(treatment[0])+273.)  # temp in kelvin

                        # check if this is ZI or IZ:
                        #  check if the same temperature already measured:
                        methcode = "LP-PI-TRM:LP-PI-TRM-ZI"
                        for j in range(0, i):
                            if Data[specimen][j]['treatment'][0] == treatment[0]:
                                if Data[specimen][j]['treatment'][1] == '1' or Data[specimen][j]['treatment'][1] == '10':
                                    methcode = "LP-PI-TRM:LP-PI-TRM-IZ"
                                else:
                                    methcode = "LP-PI-TRM:LP-PI-TRM-ZI"

                    elif treatment[1] == '1' or treatment[1] == '10':
                        LT_code = "LT-T-I"
                        # labfield in tesla (convert from microT)
                        MeasRec["treat_dc_field"] = '%8.3e' % (labfield)
                        MeasRec["treat_dc_field_phi"] = '%7.1f' % (
                            phi)  # labfield phi
                        MeasRec["treat_dc_field_theta"] = '%7.1f' % (
                            theta)  # labfield theta
                        MeasRec["treat_temp"] = '%8.3e' % (
                            float(treatment[0])+273.)  # temp in kelvin

                        # check if this is ZI or IZ:
                        #  check if the same temperature already measured:
                        methcode = "LP-PI-TRM:LP-PI-TRM-IZ"
                        for j in range(0, i):
                            if Data[specimen][j]['treatment'][0] == treatment[0]:
                                if Data[specimen][j]['treatment'][1] == '0' or Data[specimen][j]['treatment'][1] == '00':
                                    methcode = "LP-PI-TRM:LP-PI-TRM-ZI"
                                else:
                                    methcode = "LP-PI-TRM:LP-PI-TRM-IZ"
                    elif treatment[1] == '2' or treatment[1] == '20':
                        LT_code = "LT-PTRM-I"
                        # labfield in tesla (convert from microT)
                        MeasRec["treat_dc_field"] = '%8.3e' % (labfield)
                        MeasRec["treat_dc_field_phi"] = '%7.1f' % (
                            phi)  # labfield phi
                        MeasRec["treat_dc_field_theta"] = '%7.1f' % (
                            theta)  # labfield theta
                        MeasRec["treat_temp"] = '%8.3e' % (
                            float(treatment[0])+273.)  # temp in kelvin
                        methcode = "LP-PI-TRM:LP-PI-TRM-IZ"

                    else:
                        print(
                            "ERROR in treatment field line %i... exiting until you fix the problem" % line_no)
                        return False, "ERROR in treatment field line %i... exiting until you fix the problem" % line_no
                    MeasRec["method_codes"] = LT_code+":"+methcode
                    MeasRec["treat_step_num"] = "%i" % i
                    MeasRec["description"] = ""
                    MeasRecs.append(MeasRec)

                # ----------------------------------------
                # demag experimet
                # ----------------------------------------

                if LPcode == "LP-DIR-T":
                    MeasRec["experiment"] = specimen + ":" + LPcode
                    methcode = LPcode
                    if treatment_type == "N":
                        LT_code = "LT-NO"
                    else:
                        LT_code = "LT-T-Z"
                        methcode = LPcode+":"+"LT-T-Z"
                    MeasRec["treat_dc_field_phi"] = '0'
                    MeasRec["treat_dc_field_theta"] = '0'
                    MeasRec["treat_dc_field"] = '%8.3e' % (0)
                    MeasRec["treat_temp"] = '%8.3e' % (
                        float(treatment[0])+273.)  # temp in kelvin
                    MeasRec["method_codes"] = LT_code+":"+methcode
                    MeasRec["treat_step_num"] = "%i" % i
                    MeasRec["description"] = ""
                    MeasRecs.append(MeasRec)
                    # continue

                # ----------------------------------------
                # ATRM measurements
                # The direction of the magnetization is used to determine the
                # direction of the lab field.
                # ----------------------------------------

                if LPcode == "LP-AN-TRM":
                    MeasRec["experiment"] = specimen + ":" + LPcode
                    methcode = LPcode

                    if float(treatment[1]) == 0:
                        MeasRec["method_codes"] = "LP-AN-TRM:LT-T-Z"
                        MeasRec["treat_dc_field_phi"] = '0'
                        MeasRec["treat_dc_field_theta"] = '0'
                        MeasRec["treat_temp"] = '%8.3e' % (
                            float(treatment[0])+273.)  # temp in kelvin
                        MeasRec["treat_dc_field"] = '0'
                    else:
                        if float(treatment[1]) == 7:
                            # alteration check
                            methcode = "LP-AN-TRM:LT-PTRM-I"
                            MeasRec["treat_step_num"] = '7'  # -z
                        else:
                            MeasRec["method_codes"] = "LP-AN-TRM:LT-T-I"
                            inc = float(MeasRec["dir_inc"])
                            dec = float(MeasRec["dir_dec"])
                            if abs(inc) < 45 and (dec < 45 or dec > 315):  # +x
                                tdec, tinc = 0, 0
                                MeasRec["treat_step_num"] = '1'
                            if abs(inc) < 45 and (dec < 135 and dec > 45):
                                tdec, tinc = 90, 0
                                MeasRec["treat_step_num"] = '2'  # +y
                            if inc > 45:
                                tdec, tinc = 0, 90
                                MeasRec["treat_step_num"] = '3'  # +z
                            if abs(inc) < 45 and (dec < 225 and dec > 135):
                                tdec, tinc = 180, 0
                                MeasRec["treat_step_num"] = '4'  # -x
                            if abs(inc) < 45 and (dec < 315 and dec > 225):
                                tdec, tinc = 270, 0
                                MeasRec["treat_step_num"] = '5'  # -y
                            if inc < -45:
                                tdec, tinc = 0, -90
                                MeasRec["treat_step_num"] = '6'  # -z

                        MeasRec["treat_dc_field_phi"] = '%7.1f' % (tdec)
                        MeasRec["treat_dc_field_theta"] = '%7.1f' % (tinc)
                        MeasRec["treat_temp"] = '%8.3e' % (
                            float(treatment[0])+273.)  # temp in kelvin
                        MeasRec["treat_dc_field"] = '%8.3e' % (labfield)
                    MeasRec["description"] = ""
                    MeasRecs.append(MeasRec)
                    # continue

                # ----------------------------------------
                # NLT measurements
                # or TRM acquisistion experiment
                # ----------------------------------------

                if LPcode == "LP-TRM":
                    MeasRec["experiment"] = specimen + ":" + LPcode
                    MeasRec["method_codes"] = "LP-TRM:LT-T-I"
                    if float(treatment[1]) == 0:
                        labfield = 0
                    else:
                        labfield = float(float(treatment[1]))*1e-6
                    MeasRec["treat_temp"] = '%8.3e' % (
                        float(treatment[0])+273.)  # temp in kelvin
                    # labfield in tesla (convert from microT)
                    MeasRec["treat_dc_field"] = '%8.3e' % (labfield)
                    MeasRec["treat_dc_field_phi"] = '%7.1f' % (
                        phi)  # labfield phi
                    MeasRec["treat_dc_field_theta"] = '%7.1f' % (
                        theta)  # labfield theta
                    MeasRec["treat_step_num"] = "%i" % i
                    MeasRec["description"] = ""
                    MeasRecs.append(MeasRec)
                    # continue

                # ----------------------------------------
                # Cooling rate experiments
                # ----------------------------------------

                if LPcode == "LP-CR-TRM":
                    index = int(treatment[1][0])
                    # print index,"index"
                    # print CR_cooling_times,"CR_cooling_times"
                    # print CR_cooling_times[index-1]
                    # print CR_cooling_times[0:index-1]
                    if index == 7 or index == 70:  # alteration check as final measurement
                        meas_type = "LT-PTRM-I:LP-CR-TRM"
                        CR_cooling_time = CR_cooling_times[-1]
                    else:
                        meas_type = "LT-T-I:LP-CR-TRM"
                        CR_cooling_time = CR_cooling_times[index-1]
                    MeasRec["method_codes"] = meas_type
                    MeasRec["experiment"] = specimen + ":" + LPcode
                    MeasRec["treat_temp"] = '%8.3e' % (
                        float(treatment[0])+273.)  # temp in kelvin
                    # labfield in tesla (convert from microT)
                    MeasRec["treat_dc_field"] = '%8.3e' % (labfield)
                    MeasRec["treat_dc_field_phi"] = '%7.1f' % (
                        phi)  # labfield phi
                    MeasRec["treat_dc_field_theta"] = '%7.1f' % (
                        theta)  # labfield theta
                    MeasRec["treat_step_num"] = "%i" % index
                    MeasRec["description"] = "cooling_rate" + \
                        ":"+CR_cooling_time+":"+"K/min"
                    #MeasRec["description"]="%.1f minutes per cooling time"%int(CR_cooling_time)
                    MeasRecs.append(MeasRec)
                    # continue

    con = cb.Contribution(dir_path, read_tables=[])

    con.add_magic_table_from_data(dtype='specimens', data=SpecRecs)
    con.add_magic_table_from_data(dtype='samples', data=SampRecs)
    con.add_magic_table_from_data(dtype='sites', data=SiteRecs)
    con.add_magic_table_from_data(dtype='locations', data=LocRecs)
    MeasOuts = pmag.measurements_methods3(MeasRecs, noave)
    con.add_magic_table_from_data(dtype='measurements', data=MeasOuts)

    con.tables['specimens'].write_magic_file(custom_name=spec_file,dir_path=dir_path)
    con.tables['samples'].write_magic_file(custom_name=samp_file,dir_path=dir_path)
    con.tables['sites'].write_magic_file(custom_name=site_file,dir_path=dir_path)
    con.tables['locations'].write_magic_file(custom_name=loc_file,dir_path=dir_path)
    con.tables['measurements'].write_magic_file(custom_name=meas_file,dir_path=dir_path)

    return True, meas_file

Response: Convert HUJI format file to MagIC file


Instruction:
Given the following Python 3 function, write the documentation
def huji_sample(orient_file, meths='FS-FD:SO-POM:SO-SUN', location_name='unknown',
                samp_con="1", ignore_dip=True, data_model_num=3,
                samp_file="samples.txt", site_file="sites.txt",
                dir_path=".", input_dir_path=""):
    """
    Convert HUJI sample file to MagIC file(s)

    Parameters
    ----------
    orient_file : str
        input file name
    meths : str
       colon-delimited sampling methods, default FS-FD:SO-POM:SO-SUN
       for more options, see info below
    location : str
        location name, default "unknown"
    samp_con : str
        sample/site naming convention, default '1', see info below
    ignore_dip : bool
        set sample az/dip to 0, default True
    data_model_num : int
        MagIC data model 2 or 3, default 3
    samp_file : str
        sample file name to output (default : samples.txt)
    site_file : str
        site file name to output (default : site.txt)
    dir_path : str
        output directory, default "."
    input_dir_path : str
        input file directory IF different from dir_path, default ""

    Returns
    --------
    type - Tuple : (True or False indicating if conversion was sucessful, file name written)

    Info
    --------
    Sampling method codes:
        FS-FD field sampling done with a drill
        FS-H field sampling done with hand samples
        FS-LOC-GPS  field location done with GPS
        FS-LOC-MAP  field location done with map
        SO-POM   a Pomeroy orientation device was used
        SO-ASC   an ASC orientation device was used
        SO-MAG   orientation with magnetic compass

     Sample naming convention:
        [1] XXXXY: where XXXX is an arbitrary length site designation and Y
            is the single character sample designation.  e.g., TG001a is the
            first sample from site TG001.    [default]
        [2] XXXX-YY: YY sample from site XXXX (XXX, YY of arbitary length)
        [3] XXXX.YY: YY sample from site XXXX (XXX, YY of arbitary length)
        [4-Z] XXXX[YYY]:  YYY is sample designation with Z characters from site XXX
        [5] site name = sample name
        [6] site name entered in site_name column in the orient.txt format input file  -- NOT CURRENTLY SUPPORTED
        [7-Z] [XXX]YYY:  XXX is site designation with Z characters from samples  XXXYYY

    """

    try:
        samp_con, Z = samp_con.split("-")
    except ValueError:
        samp_con = samp_con
        Z = 1

    version_num = pmag.get_version()
    if data_model_num == 2:
        loc_col = "er_location_name"
        site_col = "er_site_name"
        samp_col = "er_sample_name"
        citation_col = "er_citation_names"
        class_col = "site_class"
        lithology_col = "site_lithology"
        definition_col = "site_definition"
        type_col = "site_type"
        sample_bed_dip_direction_col = "sample_bed_dip_direction"
        sample_bed_dip_col = "sample_bed_dip"
        site_bed_dip_direction_col = "site_bed_dip_direction"
        site_bed_dip_col = "site_bed_dip"
        sample_dip_col = "sample_dip"
        sample_az_col = "sample_azimuth"
        sample_lat_col = "sample_lat"
        sample_lon_col = "sample_lon"
        site_lat_col = "site_lat"
        site_lon_col = "site_lon"
        meth_col = "magic_method_codes"
        software_col = "magic_software_packages"
    else:
        loc_col = "location"
        site_col = "site"
        samp_col = "sample"
        citation_col = "citations"
        class_col = "class"
        lithology_col = "lithology"
        definition_col = "definition"
        type_col = "type"
        sample_bed_dip_direction_col = 'bed_dip_direction'
        sample_bed_dip_col = 'bed_dip'
        site_bed_dip_direction_col = 'bed_dip_direction'
        site_bed_dip_col = "bed_dip"
        sample_dip_col = "dip"
        sample_az_col = "azimuth"
        sample_lat_col = "lat"
        sample_lon_col = "lon"
        site_lat_col = "lat"
        site_lon_col = "lon"
        meth_col = "method_codes"
        software_col = "software_packages"

    input_dir_path, dir_path = pmag.fix_directories(input_dir_path, dir_path)
    samp_file = pmag.resolve_file_name(samp_file, dir_path)
    site_file = pmag.resolve_file_name(site_file, dir_path)
    orient_file = pmag.resolve_file_name(orient_file, input_dir_path)
    print("-I- reading in: {}".format(orient_file))
    #
    # read in file to convert
    #
    with open(orient_file, 'r') as azfile:
        AzDipDat = azfile.readlines()
    SampOut = []
    SiteOut = []
    for line in AzDipDat[1:]:
        orec = line.split()
        if len(orec) > 1:
            labaz, labdip = pmag.orient(float(orec[1]), float(orec[2]), '3')
            bed_dip_dir = (orec[3])
            bed_dip = (orec[4])
            SampRec = {}
            SiteRec = {}
            SampRec[loc_col] = location_name
            SampRec[citation_col] = "This study"
            SiteRec[loc_col] = location_name
            SiteRec[citation_col] = "This study"
            SiteRec[class_col] = ""
            SiteRec[lithology_col] = ""
            SiteRec[type_col] = ""
            SiteRec[definition_col] = "s"
    #
    # parse information common to all orientation methods
    #
            SampRec[samp_col] = orec[0]
            SampRec[sample_bed_dip_direction_col] = orec[3]
            SampRec[sample_bed_dip_col] = orec[4]
            SiteRec[site_bed_dip_direction_col] = orec[3]
            SiteRec[site_bed_dip_col] = orec[4]
            if not ignore_dip:
                SampRec[sample_dip_col] = '%7.1f' % (labdip)
                SampRec[sample_az_col] = '%7.1f' % (labaz)
            else:
                SampRec[sample_dip_col] = '0'
                SampRec[sample_az_col] = '0'
            SampRec[sample_lat_col] = orec[5]
            SampRec[sample_lon_col] = orec[6]
            SiteRec[site_lat_col] = orec[5]
            SiteRec[site_lon_col] = orec[6]
            SampRec[meth_col] = meths
            # parse out the site name
            site = pmag.parse_site(orec[0], samp_con, Z)
            SampRec[site_col] = site
            SampRec[software_col] = version_num
            SiteRec[site_col] = site
            SiteRec[software_col] = version_num
            SampOut.append(SampRec)
            SiteOut.append(SiteRec)
    if data_model_num == 2:
        pmag.magic_write(samp_file, SampOut, "er_samples")
        pmag.magic_write(site_file, SiteOut, "er_sites")
    else:
        pmag.magic_write(samp_file, SampOut, "samples")
        pmag.magic_write(site_file, SiteOut, "sites")

    print("Sample info saved in ", samp_file)
    print("Site info saved in ", site_file)
    return True, samp_file

Response: Convert HUJI sample file to MagIC file.


Instruction:
Can you tell what is the following Python 3 function doing
def iodp_dscr_lore(dscr_file,dscr_ex_file="", dir_path=".", input_dir_path="",volume=7,noave=False,\
              meas_file="measurements.txt", offline_meas_file="",spec_file="specimens.txt"):
    """
    Convert IODP discrete measurement files in MagIC file(s). This program
    assumes that you have created the specimens, samples, sites and location
    files using convert_2_magic.iodp_samples_csv from files downloaded from the LIMS online
    repository and that all samples are in that file.

    If there are offline treatments, you will also need the extended version of the SRM discrete
    download file from LORE.

    Parameters
    ----------
    dscr_file : str
        input csv file downloaded from LIMS online repository
    dscr_ex_file : str
        input extended csv file downloaded from LIMS online repository
    dir_path : str
        output directory, default "."
    input_dir_path : str
        input file directory IF different from dir_path, default ""
    meas_file : str
        output measurement file name, default "measurements.txt"
    offline_meas_file : str
        output measurement file for offline measurements , default "". must be specified if dscr_ex_file supplied
    spec_file : str
        specimens file name created by, for example, convert_2_magic.iodp_samples_csv, default "specimens.txt"
        file should already be in dir_path
    volume : float
        volume in cm^3 assumed during measurement on SRM.  The so-called "Japanese" cubes have a volume of 7cc
    noave : Boolean
         if False, average replicate measurements

    Returns
    --------
    type - Tuple : (True or False indicating if conversion was sucessful, meas_file name written)
    """
    # initialize defaults
    version_num = pmag.get_version()
    # format variables
    input_dir_path, output_dir_path = pmag.fix_directories(input_dir_path, dir_path)
    # convert cc to m^3
    volume = volume * 1e-6
    meas_reqd_columns=['specimen','measurement','experiment','sequence','quality','method_codes',\
                       'instrument_codes','citations',\
                       'treat_temp','treat_ac_field','treat_dc_field',\
                      'treat_dc_field_phi','treat_dc_field_theta','meas_temp',\
                      'dir_dec','dir_inc','magn_moment','magn_volume',\
                       'description','timestamp','software_packages',\
                       'external_database_ids','treat_step_num','meas_n_orient']
    dscr_file = pmag.resolve_file_name(dscr_file, input_dir_path)
    if dscr_ex_file:dscr_ex_file = pmag.resolve_file_name(dscr_ex_file, input_dir_path)
    spec_file = pmag.resolve_file_name(spec_file, dir_path)
    specimens_df=pd.read_csv(spec_file,sep='\t',header=1)
    if len(specimens_df)==0:
        print ('you must download and process the samples table from LORE prior to using this')
        print ('see convert_2_magic.iodp_samples_csv for help')
        return False
    LORE_specimens=list(specimens_df.specimen.unique())
    in_df=pd.read_csv(dscr_file)
    in_df['offline_treatment']=""
    if dscr_ex_file:
        ex_df=pd.read_csv(dscr_ex_file)
        ex_df['Test No.']=ex_df['test test_number']
        ex_df['offline_treatment']=ex_df['comments']
        ex_df=ex_df[['Test No.','offline_treatment']]
        in_df=in_df.merge(ex_df,on='Test No.')
        in_df['offline_treatment']=in_df['offline_treatment_y']
        in_df['offline_treatment'].fillna("",inplace=True)
    in_df.drop_duplicates(inplace=True)
    if len(in_df)==0:
        print ('you must download a csv file from the LIMS database and place it in your input_dir_path')
        return False
    in_df.sort_values(by='Test No.',inplace=True)
    in_df.reset_index(inplace=True)
    measurements_df=pd.DataFrame(columns=meas_reqd_columns)
    meas_out = os.path.join(output_dir_path, meas_file)
    if offline_meas_file:
        offline_meas_out = os.path.join(output_dir_path, offline_meas_file)
    if dscr_ex_file and not offline_meas_file:
        print ("You must specify an output file for the offline measurements with dscr_ex_file")
        return
    hole,srm_specimens=iodp_sample_names(in_df)
    for spec in list(srm_specimens.unique()):
        if spec not in LORE_specimens:
            print (' -W- ',spec, ' not found in specimens table ')
            print ( 'check your sample name or add to specimens table by hand\n')
    # set up defaults
    measurements_df['specimen']=srm_specimens
    measurements_df['offline_treatment']=in_df['offline_treatment']
    measurements_df['sequence']=in_df['Test No.']
    measurements_df['offline_list']=""
    measurements_df['quality']='g'
    measurements_df['citations']='This study'
    measurements_df['meas_temp']=273
    measurements_df['software_packages']=version_num
    measurements_df["treat_temp"] = '%8.3e' % (273)  # room temp in kelvin
    measurements_df["meas_temp"] = '%8.3e' % (273)  # room temp in kelvin
    measurements_df['meas_n_orient']=1 # at least one orientation
    measurements_df["treat_ac_field"] = '0'
    measurements_df["treat_dc_field"] = '0'
    measurements_df["treat_dc_field_phi"] = '0'
    measurements_df["treat_dc_field_theta"] = '0'
    measurements_df["treat_step_num"] = '1'
    measurements_df["standard"] = 'u'  # assume all data are "good"
    measurements_df["dir_csd"] = '0'  # assume all data are "good"
    measurements_df["method_codes"] = 'LT-NO' # assume all are NRMs
    measurements_df['instrument_codes']="IODP-SRM" # assume all measurements on shipboard 2G
    measurements_df['timestamp']=pd.to_datetime(\
                               in_df['Timestamp (UTC)']).dt.strftime("%Y-%m-%dT%H:%M:%S")+'Z'
    measurements_df['dir_dec']=in_df['Declination background & drift corrected (deg)'] # declination
    measurements_df['dir_inc']=in_df['Inclination background & drift corrected (deg)'] # inclination
    measurements_df['magn_volume']=in_df['Intensity background & drift corrected (A/m)'] # magnetization
    measurements_df['magn_moment']=measurements_df['magn_volume']*volume # moment in Am^2
    measurements_df['description']=in_df['Treatment Type'] # temporary column
    measurements_df['treat_ac_field']=in_df['Treatment Value']*1e-3 # assume all treatments are AF
    measurements_df.loc[measurements_df['description']=='IN-LINE AF DEMAG',\
                        'method_codes']='LT-AF-Z'
    measurements_df.loc[measurements_df['description']=='IN-LINE AF DEMAG',\
                        'instrument_codes']='IODP-SRM:IODP-SRM-AF'
    measurements_df['external_database_ids']='LORE['+in_df['Test No.'].astype('str')+']'
    measurements_df.fillna("",inplace=True)
    measurements_df.sort_values(by='sequence',inplace=True)
    if dscr_ex_file:
        meas_df=measurements_df[measurements_df.offline_treatment==""] # all the records with no offline treatments
        offline_df=pd.DataFrame(columns=meas_df.columns) # make a container for offline measurements
        arm_df=measurements_df[measurements_df['offline_treatment'].str.contains('ARM')]
        if len(arm_df)>0: # there are ARM treatment steps
           arm_df['offline_list']=arm_df['offline_treatment'].str.split(":")
           arm_list=arm_df.specimen.unique()
           for spc in arm_list: # get all the ARM treated specimens
               spc_df=arm_df[arm_df.specimen.str.match(spc)] # get all the measurements for this specimen
               seq_no=spc_df[spc_df.specimen.str.match(spc)].sequence.values[0] # get the sequence number of the ARM step
               end_seq_no=spc_df[spc_df.specimen.str.match(spc)].sequence.values[-1] # get the sequence number of the last ARM demag step
               arm_df.loc[arm_df.sequence==seq_no,'method_codes']='LT-AF-I:LP-ARM-AFD' # label the ARM record
               arm_df.loc[arm_df.sequence==seq_no,'experiment']=spc+'_LT-AF-I_LT-AF-Z_LP-ARM-AFD' # label the ARM record
               arm_df.loc[arm_df.sequence==seq_no,'treat_ac_field']=arm_df['offline_list'].str.get(1).astype('float')*1e-3 # AF peak field in mT converted to tesla
               arm_df.loc[arm_df.sequence==seq_no,'treat_dc_field']=arm_df['offline_list'].str.get(2).astype('float')*1e-3 # AF peak field in mT converted to tesla
               arm_df.loc[arm_df.sequence==seq_no,'instrument_codes']='IODP-SRM:IODP-DTECH'
               arm_df.loc[(arm_df.specimen.str.match(spc)) &
                          (arm_df.sequence>seq_no) &
                          (arm_df.sequence<=end_seq_no),'method_codes']= 'LT-AF-Z:LP-ARM-AFD'
               arm_df.loc[(arm_df.specimen.str.match(spc)) &
                          (arm_df.sequence>seq_no) &
                          (arm_df.sequence<=end_seq_no),'experiment']= spc+'LT-AF-I_LT-AF-Z_LP-ARM-AFD'
               arm_df.loc[(arm_df.specimen.str.match(spc)) &
                          (arm_df.sequence>seq_no) &
                          (arm_df.sequence<=end_seq_no),'instrument_codes']= 'IODP-SRM:IODP-SRM-AF'
           strings=[]
           for i in range(len(arm_df)):strings.append(str(i))
           arm_df['measurement']=arm_df['experiment']+strings
           arm_df['description']=arm_df['offline_treatment']
           offline_df=pd.concat([offline_df,arm_df])  # put the arm data into the offline dataframe
        irm_in_df=measurements_df[measurements_df['offline_treatment'].str.contains('IRM')]
        if len(irm_in_df)>0: # there are IRM treatment steps
           irm_in_df['offline_list']=irm_in_df['offline_treatment'].str.split(":")
           irm_list=irm_in_df.specimen.unique()
           irm_out_df=pd.DataFrame(columns=irm_in_df.columns) # make an output container
           for spc in irm_list: # get all the IRM treated specimens
               # first do IRM acquisition steps
               spc_df=irm_in_df[irm_in_df.specimen.str.match(spc)] # get all the measurements for this specimen
               spc_acq_df=spc_df[spc_df.treat_ac_field==0] # IRM acquisition step
               spc_acq_df['method_codes']='LT-IRM:LP-IRM' # label the IRM records
               spc_acq_df['experiment']=spc+'_LT-IRM_LP-IRM' # label the IRM experiment
               spc_acq_df['treat_dc_field']=spc_acq_df['offline_list'].str.get(1).astype('float')*1e-3 # IRM field in mT converted to tesla
               spc_acq_df['instrument_codes']='IODP-SRM:IODP-IM-10'
               # do the AF demag of the IRM
               sirm_seq_no=spc_acq_df[spc_acq_df.specimen.str.match(spc)].sequence.values[-1] # get the sequence number of the last IRM step
               spc_afd_df=spc_df[(spc_df.treat_ac_field!=0)] #
               spc_afd_df['method_codes']= 'LP-IRM:LP-IRM-AFD'
               spc_afd_df['experiment']= spc+'LP-IRM:LP-IRM-AFD'
               spc_afd_df['instrument_codes']= 'IODP-SRM:IODP-SRM-AFD'
               irm_out_df=pd.concat([irm_out_df,spc_acq_df,spc_afd_df])
           strings=[]
           for i in range(len(irm_out_df)):strings.append(str(i))
           irm_out_df['measurement']=irm_out_df['experiment']+strings
           irm_out_df['description']=irm_out_df['offline_treatment']
           offline_df=pd.concat([offline_df,irm_out_df])  # put the irm data into the offline dataframe
    if dscr_ex_file:
        offline_df.drop(columns=['offline_list'],inplace=True)
        offline_df.drop(columns=['offline_treatment'],inplace=True)
        offline_df.sort_values(by='sequence',inplace=True)
        offline_df.drop_duplicates(subset=['sequence'],inplace=True)
        offline_df.fillna("",inplace=True)
        offline_dicts = offline_df.to_dict('records')
        pmag.magic_write(offline_meas_out, offline_dicts, 'measurements')
        measurements_df=meas_df # put all the non-offline treatments back into measurements_df
    if 'offline_treatment' in measurements_df.columns:
        measurements_df.drop(columns=['offline_treatment'],inplace=True)
    if 'offline_list' in measurements_df.columns:
        measurements_df.drop(columns=['offline_list'],inplace=True)
    measurements_df.sort_values(by='sequence',inplace=True)
    measurements_df.drop_duplicates(subset=['sequence'],inplace=True)
    measurements_df['treat_step_num']=measurements_df['sequence']
    measurements_df.fillna("",inplace=True)
    meas_dicts = measurements_df.to_dict('records')
    meas_dicts=pmag.measurements_methods3(meas_dicts,noave=noave)
    pmag.magic_write(meas_out, meas_dicts, 'measurements')

    return True

Response: Convert IODP discrete measurement files from LORE to MagIC file.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def iodp_jr6_lore(jr6_file, dir_path=".", input_dir_path="",volume=7,noave=False,dc_field=50e-6,\
              meas_file="measurements.txt", spec_file="specimens.txt"):
    """
    Convert IODP JR6 measurement files in MagIC file(s). This program
    assumes that you have created the specimens, samples, sites and location
    files using convert_2_magic.iodp_samples_csv from files downloaded from the LIMS online
    repository and that all samples are in that file.

    Parameters
    ----------
    jr6_file : str
        input csv file downloaded from LIMS online repository
    dir_path : str
        output directory, default "."
    input_dir_path : str
        input file directory IF different from dir_path, default ""
    meas_file : str
        output measurement file name, default "measurements.txt"
    spec_file : str
        specimens file name created by, for example, convert_2_magic.iodp_samples_csv, default "specimens.txt"
        file should already be in dir_path
    volume : float
        volume in cm^3 assumed during measurement on SRM.  The so-called "Japanese" cubes have a volume of 7cc
    noave : Boolean
         if False, average replicate measurements
    dc_field : float
        if ARM measurements are in the file, this was the DC bias field applied

    Returns
    --------
    type - Tuple : (True or False indicating if conversion was sucessful, meas_file name written)
    """
    # initialize defaults
    version_num = pmag.get_version()
    # format variables
    input_dir_path, output_dir_path = pmag.fix_directories(input_dir_path, dir_path)
    # convert cc to m^3
    volume = volume * 1e-6
    meas_reqd_columns=['specimen','measurement','experiment','sequence','quality','method_codes',\
                       'instrument_codes','citations',\
                       'treat_temp','treat_ac_field','treat_dc_field',\
                      'treat_dc_field_phi','treat_dc_field_theta','meas_temp',\
                      'dir_dec','dir_inc','magn_moment','magn_volume',\
                       'description','timestamp','software_packages',\
                       'external_database_ids','treat_step_num']
    jr6_file = pmag.resolve_file_name(jr6_file, input_dir_path)
    spec_file = pmag.resolve_file_name(spec_file, dir_path)
    specimens_df=pd.read_csv(spec_file,sep='\t',header=1)
    if len(specimens_df)==0:
        print ('you must download and process the samples table from LORE prior to using this')
        print ('see convert_2_magic.iodp_samples_csv for help')
        return False
    LORE_specimens=list(specimens_df.specimen.unique())
    measurements_df=pd.DataFrame(columns=meas_reqd_columns)
    meas_out = os.path.join(output_dir_path, meas_file)
    citations = "This study"
    in_df=pd.read_csv(jr6_file)
    if len(in_df)==0:
        print ('you must download a csv file from the LIMS database and place it in your input_dir_path')
        return False
    in_df.sort_values(by='Treatment value (mT or °C or ex. B14)',inplace=True)
    hole,jr6_specimens=iodp_sample_names(in_df)
    for spec in list(jr6_specimens.unique()):
        if spec not in LORE_specimens:
            print (spec, ' not found in sample table')
    # set up defaults
    measurements_df['specimen']=jr6_specimens
    measurements_df['quality']='g'
    measurements_df['citations']='This study'
    measurements_df['meas_temp']=273
    measurements_df['software_packages']=version_num
    measurements_df["treat_temp"] = '%8.3e' % (273)  # room temp in kelvin
    measurements_df["meas_temp"] = '%8.3e' % (273)  # room temp in kelvin
    measurements_df["treat_ac_field"] = '0'
    measurements_df["treat_dc_field"] = '0'
    measurements_df["treat_dc_field_phi"] = '0'
    measurements_df["treat_dc_field_theta"] = '0'
    measurements_df["treat_step_num"] = 1
    measurements_df["standard"] = 'u'  # assume all data are "good"
    measurements_df["dir_csd"] = '0'  # assume all data are "good"
    measurements_df["method_codes"] = 'LT-NO' # assume all are NRMs
    measurements_df['instrument_codes']="IODP-JR6" # assume all measurements on shipboard JR6
    measurements_df['timestamp']=pd.to_datetime(\
                               in_df['Timestamp (UTC)']).dt.strftime("%Y-%m-%dT%H:%M:%S")+'Z'
    # use the sample coordinates, assume was placed correctly in the JR6 and use az=0,pl=-90 to
    # put into 'geographic' coordinates
    incs=in_df['Inclination (°)'].values
    decs=in_df['Declination (°)'].values
    azs=np.ones(len(decs))*0
    pls=np.ones(len(decs))*-90
    geo=np.vstack((decs,incs,azs,pls)).transpose()
    drot,irot=pmag.dogeo_V(geo)

    measurements_df['dir_dec']=drot # declination
    measurements_df['dir_inc']=irot # inclination
    measurements_df['magn_volume']=in_df['Intensity raw (A/m)'] # magnetization
    measurements_df['magn_moment']=measurements_df['magn_volume']*volume # moment in Am^2
    measurements_df['description']=in_df['Treatment type']
    measurements_df['treat_ac_field']=in_df['Treatment value (mT or °C or ex. B14)']*1e-3 # initialize all treatments to AF in mT
    measurements_df.loc[measurements_df['description']=='AD',\
                        'method_codes']='LT-AF-Z'
    measurements_df.loc[measurements_df['description']=='AD',\
                        'instrument_codes']='IODP-JR6:IODP-D2000'
    measurements_df.loc[measurements_df['description']=='ARM',\
                        'method_codes']='LT-AF-I'
    measurements_df.loc[measurements_df['description']=='ARM',\
                        'instrument_codes']='IODP-JR6:IODP-D2000'
    measurements_df.loc[measurements_df['description']=='ARM',"treat_dc_field"] = dc_field #
    measurements_df['external_database_ids']='LORE['+in_df['Test No.'].astype('str')+']'
# add these later when controlled vocabs implemented
    measurements_df.loc[measurements_df['description']=='TD','method_codes']='LT-T-Z'
    measurements_df.loc[measurements_df['description']=='TD',\
                        'instrument_codes']='IODP-SRM:IODP-TDS'
    measurements_df.loc[measurements_df['description']=='TD',"treat_temp"] =\
                        in_df['Treatment value (mT or °C or ex. B14)']+273
    #measurements_df.loc[measurements_df['description']=='IRM','method_codes']='LT-IRM'
    #measurements_df.loc[measurements_df['description']=='IRM',\
       #                 'instrument_codes']='IODP-SRM:IODP-IRM'

    measurements_df.fillna("",inplace=True)
    meas_dicts = measurements_df.to_dict('records')
    meas_dicts=pmag.measurements_methods3(meas_dicts,noave=noave)
    pmag.magic_write(meas_out, meas_dicts, 'measurements')

    return True

Response: Convert IODP JR6 measurement files in MagIC file to Lore - Marquardt format.


Instruction:
Implement a Python 3 function for
converting a kly4s. txt file from the LIMS online LORE downloaded sample file to MagIC files.

Response:
def iodp_kly4s_lore(kly4s_file, meas_out='measurements.txt',
           spec_infile='specimens.txt', spec_out='specimens.txt',  instrument='IODP-KLY4S',
           actual_volume="",dir_path='.', input_dir_path=''):
    """
    Converts ascii files generated by SUFAR ver.4.0 and downloaded from the LIMS online
    repository to MagIC (datamodel 3) files

    Parameters
    ----------
    kly4s_file : str
        input LORE downloaded csv file, required
    meas_output : str
        measurement output filename, default "measurements.txt"
    spec_infile : str
        specimen infile, default specimens.txt
        [file created by iodp_samples_csv from LORE downloaded sample file]
    spec_outfile : str
        specimen outfile, default "kly4s_specimens.txt"
    instrument : str
        instrument name, default ""
    actual_volume : float
        the nominal volume is assumed to be 8cc or even 10cc, depending on the shipboard
        software used, actual_vol is the  actual specimen volume in cc
    dir_path : str
        output directory, default "."
    input_dir_path : str
        input file directory IF different from dir_path, default ""

    Returns
    --------
    type - Tuple : (True or False indicating if conversion was sucessful, file name written)

    """
    # initialize defaults
    version_num = pmag.get_version()
    # format variables
    input_dir_path, output_dir_path = pmag.fix_directories(input_dir_path, dir_path)
    # set up required columns
    meas_reqd_columns=['specimen','measurement','experiment','sequence','quality','method_codes',\
                       'instrument_codes','citations',\
                       'treat_temp','treat_ac_field','treat_dc_field',\
                      'treat_dc_field_phi','treat_dc_field_theta','meas_temp',\
                      'dir_dec','dir_inc','magn_moment','magn_volume',\
                       'description','timestamp','software_packages',\
                       'external_database_ids','experiments','treat_step_num']
    spec_reqd_columns=['specimen','sample','result_quality','method_codes','volume',\
                       'specimen_name_alternatives','citations',\
                      'aniso_type','aniso_s_n_measurements',\
                      'azimuth','dip','aniso_s_sigma',\
                      'aniso_s_unit','aniso_tilt_correction',]
    # sort out file paths
    kly4s_file = pmag.resolve_file_name(kly4s_file, input_dir_path)
    spec_out = pmag.resolve_file_name(spec_out, dir_path)
    spec_file=pmag.resolve_file_name(spec_infile, dir_path)
    meas_out = pmag.resolve_file_name(meas_out, dir_path)
    # read in necessary data:
    specs=pd.read_csv(spec_file,sep='\t',header=1) # read in existing specimen table
    if len(specs)==0:
        print ('you must download and process the samples table from LORE prior to using this')
        print ('see convert_2_magic.iodp_samples_csv for help')
        return False
    LORE_specimens=list(specs.specimen.unique())
    in_df=pd.read_csv(kly4s_file)
    if len(in_df)==0:
        print ('you must download a csv file from the LIMS database and place it in your input_dir_path')
        return False
    measurements_df=pd.DataFrame(columns=meas_reqd_columns)
    specimens_df=pd.DataFrame(columns=spec_reqd_columns)
    hole,kly4s_specimens=iodp_sample_names(in_df)
    for spec in list(kly4s_specimens.unique()):
        if spec not in LORE_specimens:
            print (spec, ' not found in specimen table')

    # set up defaults
        # specimens table defaults
    specimens_df['specimen']=kly4s_specimens
    specimens_df['sample']=kly4s_specimens
    specimens_df['result_quality']='g'
    specimens_df['citations']='This study'
    specimens_df['aniso_type']='AMS'
    specimens_df['azimuth']=0
    specimens_df['dip']=0
    specimens_df['aniso_s_n_measurements']=192
    specimens_df['aniso_tilt_correction']=0
    specimens_df['aniso_s_unit']='SI'
    specimens_df['aniso_s_sigma']=''
    specimens_df['method_codes']= "LP-X:AE-H:LP-AN-MS"
    specimens_df['experiments']=specimens_df['specimen'].astype('str')+'_'+ "LP-AN-MS"
        # measurements table
    measurements_df['specimen']=kly4s_specimens
    measurements_df['quality']='g'
    measurements_df['citations']='This study'
    measurements_df['meas_temp']=273
    measurements_df['software_packages']=version_num
    measurements_df["treat_temp"] = '%8.3e' % (273)  # room temp in kelvin
    measurements_df["meas_temp"] = '%8.3e' % (273)  # room temp in kelvin
    measurements_df["treat_ac_field"] = '0'
    measurements_df["treat_dc_field"] = '0'
    measurements_df["treat_dc_field_phi"] = '0'
    measurements_df["treat_dc_field_theta"] = '0'
    measurements_df["treat_step_num"] = '1'
    measurements_df["standard"] = 'u'  # assume all data are "good"
    measurements_df['instrument_codes']="IODP-KLY4S" # assume all measurements on shipboard KLY4S
    measurements_df['description']='Bulk sucsecptibility measurement'
    measurements_df['method_codes']='LP-X'
    measurements_df['experiment']=measurements_df['specimen'].astype('str')+'_'+\
                                  measurements_df['method_codes'].astype('str')
    meas_num=range(len(kly4s_specimens))
    measurements_df['sequence']=meas_num
    measurements_df['measurement']=measurements_df['experiment'].astype('str')+'-'+\
                                   measurements_df['sequence'].astype('str')
# parse the measurement data into  columns
    nominal_volume=in_df['Sample volume (CC)']*1e-6 # convert cc to m^3
    if actual_volume:
        actual_volume=(1e-6*actual_volume)
        factor=nominal_volume/actual_volume
    else:
        actual_volume=nominal_volume
        factor=1
    measurements_df['susc_chi_volume']=in_df['Bulk susceptibility(SI)']*factor
    measurements_df['external_database_ids']='LORE['+in_df['Test No.'].astype('str')+']'
# parse specimen data into columns
    specimens_df['specimen_name_alternatives']=in_df['Text ID']
    specimens_df['volume']=actual_volume
    s1=in_df['Normalized tensor K11']
    s2=in_df['Normalized tensor K22']
    s3=in_df['Normalized tensor K33']
    s4=in_df['Normalized tensor K12']
    s5=in_df['Normalized tensor K23']
    s6=in_df['Normalized tensor K13']
    if 'Standard deviation(SI)' in in_df.columns:
        specimens_df['aniso_s_sigma']=in_df['Standard deviation(SI)']
    #if (s1+s2+s3)>.35: # AGICO format
    #    s1=s1/3
    #    s2=s2/3
    #    s3=s3/3
    #    s4=s4/3
    #    s5=s5/3
    #    s6=s6/3
    specimens_df['aniso_s'] = s1.astype('str')+':'+ s2.astype('str')+':'+s3.astype('str')+':'+\
                              s4.astype('str')+':'+ s5.astype('str')+':'+ s6.astype('str')
    tau1=in_df['Kmax susceptibility (SI)']/3
    v1_dec=in_df['Kmax dec (deg)']
    v1_inc=in_df['Kmax inc (deg)']
    specimens_df['aniso_v1']=tau1.astype('str')+":"+v1_dec.astype('str')+":"+v1_inc.astype('str')
    tau2=in_df['Kint susceptibility (SI)']/3
    v2_dec=in_df['Kint dec (deg)']
    v2_inc=in_df['Kint inc (deg)']
    specimens_df['aniso_v2']=tau2.astype('str')+":"+v2_dec.astype('str')+":"+v2_inc.astype('str')
    tau3=in_df['Kmin susceptibility (SI)']/3
    v3_dec=in_df['Kmin dec (deg)']
    v3_inc=in_df['Kmin inc (deg)']
    specimens_df['aniso_v3']=tau3.astype('str')+":"+v3_dec.astype('str')+":"+v3_inc.astype('str')

# output data files
    measurements_df.fillna("",inplace=True)
    meas_dicts = measurements_df.to_dict('records')

    pmag.magic_write(meas_out, meas_dicts, 'measurements')
    specimens_df.fillna("",inplace=True)
    spec_dicts = specimens_df.to_dict('records')
    pmag.magic_write(spec_out, spec_dicts, 'specimens')

    return True


Instruction:
Can you write a function in Python 3 where it
converts IODP samples data file into MagIC samples file.

Response:
def iodp_samples(samp_file, output_samp_file=None, output_dir_path='.',
                 input_dir_path='', data_model_num=3):
    """
    Convert IODP samples data file into MagIC samples file.
    Default is to overwrite samples.txt in your working directory.

    Parameters
    ----------
    samp_file : str
        path to IODP sample file to convert
    output_samp_file : str
        MagIC-format samples file to append to, default None
    output_dir_path : str
        output file directory, default "."
    input_dir_path : str
        input file directory IF different from output_dir_path, default ""
    data_model_num : int
        MagIC data model [2, 3], default 3

    Returns
    --------
    type - Tuple : (True or False indicating if conversion was sucessful, samp_file name written)

    """
    samp_file_name = "samples.txt"
    sample_alternatives = "sample_alternatives"
    method_codes = "method_codes"
    sample_name = "sample"
    site_name = "site"
    expedition_name = "expedition_name"
    location_name = "location"
    citation_name = "citation"
    dip = "dip"
    azimuth = "azimuth"
    core_depth = "core_depth"
    composite_depth = "composite_depth"
    timestamp = "timestamp"
    file_type = "samples"
    data_model_num = int(float(data_model_num))
    if data_model_num == 2:
        samp_file_name = "er_samples.txt"
        sample_alternatives = "er_sample_alternatives"
        method_codes = "magic_method_codes"
        sample_name = "er_sample_name"
        site_name = "er_site_name"
        expedition_name = "er_expedition_name"
        location_name = "er_location_name"
        citation_name = "er_citation_names"
        dip = "sample_dip"
        azimuth = "sample_azimuth"
        core_depth = "sample_core_depth"
        composite_depth = "sample_composite_depth"
        timestamp = "sample_date"
        file_type = "er_samples"

    text_key = None
    comp_depth_key = ""
    input_dir_path, output_dir_path = pmag.fix_directories(input_dir_path, output_dir_path)
    samp_file = pmag.resolve_file_name(samp_file, input_dir_path)
    Samps = []
    samp_out = os.path.join(output_dir_path, samp_file_name)
    if output_samp_file:
        if os.path.exists(output_samp_file):
            samp_out = os.path.join(output_dir_path, output_samp_file)
            Samps, file_type = pmag.magic_read(samp_out)
            print(len(Samps), ' read in from: ', samp_out)
    fin = open(samp_file, "r")
    file_input = fin.readlines()
    fin.close()
    keys = file_input[0].replace('\n', '').split(',')
    if "CSF-B Top (m)" in keys:
        comp_depth_key = "CSF-B Top (m)"
    elif "Top depth CSF-B (m)" in keys:
        comp_depth_key = "Top depth CSF-B (m)"
    # incorporate changes to LIMS data model, while maintaining backward
    # compatibility
    keys = [key.strip('"').strip("'") for key in keys]
    if "Top Depth (m)" in keys:
        depth_key = "Top Depth (m)"
    elif "CSF-A Top (m)" in keys:
        depth_key = "CSF-A Top (m)"
    elif "Top depth CSF-A (m)" in keys:
        depth_key = "Top depth CSF-A (m)"
    if "Text Id" in keys:
        text_key = "Text Id"
    elif "Text identifier" in keys:
        text_key = "Text identifier"
    elif "Text ID" in keys:
        text_key = "Text ID"
    if "Sample Date Logged" in keys:
        date_key = "Sample Date Logged"
    elif "Sample date logged" in keys:
        date_key = "Sample date logged"
    elif "Date sample logged" in keys:
        date_key = "Date sample logged"
    elif "Timestamp (UTC)" in keys:
        date_key = "Timestamp (UTC)"
    if 'Volume (cc)' in keys:
        volume_key = 'Volume (cc)'
    if 'Volume (cm^3)' in keys:
        volume_key = 'Volume (cm^3)'
    if 'Volume (cm3)' in keys:
        volume_key = 'Volume (cm3)'
    if not text_key:
        return False, "Could not extract the necessary data from your input file.\nPlease make sure you are providing a correctly formated IODP samples csv file."
    ErSamples, samples, file_format = [], [], 'old'
    for line in file_input[1:]:
        if line[0] != '0':
            ODPRec, SampRec = {}, {}
            interval, core = "", ""
            rec = line.replace('\n', '').split(',')
            if len(rec) < 2:
                print("Error in csv file, blank columns")
                break
            for k in range(len(keys)):
                ODPRec[keys[k]] = rec[k].strip('"')
            SampRec[sample_alternatives] = ODPRec[text_key]
            if "Label Id" in keys:  # old format
                label = ODPRec['Label Id'].split()
                if len(label) > 1:
                    interval = label[1].split('/')[0]
                    pieces = label[0].split('-')
                    core = pieces[2]
                while len(core) < 4:
                    core = '0' + core  # my way
            else:  # new format
                file_format = 'new'
                pieces = [ODPRec['Exp'], ODPRec['Site'] + ODPRec['Hole'],
                          ODPRec['Core'] + ODPRec['Type'], ODPRec['Sect'], ODPRec['A/W']]
                interval = ODPRec['Top offset (cm)'].split(
                    '.')[0].strip()  # only integers allowed!
                core = ODPRec['Core'] + ODPRec['Type']
            if core != "" and interval != "":
                SampRec[method_codes] = 'FS-C-DRILL-IODP:SP-SS-C:SO-V'
                if file_format == 'old':
                    SampRec[sample_name] = pieces[0] + '-' + pieces[1] + \
                        '-' + core + '-' + pieces[3] + \
                        '-' + pieces[4] + '-' + interval
                else:
                    SampRec[sample_name] = pieces[0] + '-' + pieces[1] + '-' + core + '-' + \
                        pieces[3] + '_' + pieces[4] + '_' + \
                        interval  # change in sample name convention
                SampRec[site_name] = SampRec[sample_name]
                # pieces=SampRec['er_sample_name'].split('-')
                SampRec[expedition_name] = pieces[0]
                SampRec[location_name] = pieces[1]
                SampRec[citation_name] = "This study"
                SampRec[dip] = "0"
                SampRec[azimuth] = "0"
                SampRec[core_depth] = ODPRec[depth_key]
                if ODPRec[volume_key] != "":
                    SampRec['sample_volume'] = str(
                        float(ODPRec[volume_key]) * 1e-6)
                else:
                    SampRec['sample_volume'] = '1'
                if comp_depth_key != "":
                    SampRec[composite_depth] = ODPRec[comp_depth_key]
                dates = ODPRec[date_key].split()
                if '/' in dates[0]:  # have a date
                    mmddyy = dates[0].split('/')
                    yyyy = '20' + mmddyy[2]
                    mm = mmddyy[0]
                    if len(mm) == 1:
                        mm = '0' + mm
                    dd = mmddyy[1]
                    if len(dd) == 1:
                        dd = '0' + dd
                    date = yyyy + ':' + mm + ':' + \
                        dd + ':' + dates[1] + ":00.00"
                else:
                    date = ""
                SampRec[timestamp] = date
                ErSamples.append(SampRec)
                samples.append(SampRec[sample_name])
    if len(Samps) > 0:
        for samp in Samps:
            if samp[sample_name] not in samples:
                ErSamples.append(samp)
    Recs, keys = pmag.fillkeys(ErSamples)
    pmag.magic_write(samp_out, Recs, file_type)
    print('sample information written to: ', samp_out)
    return True, samp_out


Instruction:
Write a Python 3 function for
converting DataFrame df to list of sample names in format U999A - 1H - 1 - W - 1

Response:
def iodp_sample_names(df):
    """
    Convert expedition, hole, section, type, interval to sample name in format:
    exp-hole-type-sect-interval

    Parameters
    ___________
    df : Pandas DataFrame
         dataframe read in from .csv file downloaded from LIMS online database

    Returns
    --------------

    holes : list
           IODP Holes name in format U999A

    specimens : pandas Series
            series with sample names, e.g. 999-U999A-1H-1-W-1

    """
    if 'Top offset (cm)' in df.columns:
        offset_key='Top offset (cm)'
    elif 'Offset (cm)' in df.columns:
        offset_key='Offset (cm)'
    else:
        print ('No offset key found')
        return False,[]
    interval=df[offset_key].astype('float').astype('str')
    holes=df['Site'].astype('str')+df['Hole']
    specimens=df['Exp'].astype('str')+'-'+holes +'-'+df['Core'].astype('str')+df['Type'].astype('str')+\
              '-'+df['Sect'].astype('str')+ '-' + df['A/W']+'-'+ interval
    return holes, specimens


Instruction:
Can you generate the documentation for the following Python 3 function
def iodp_samples_csv(lims_sample_file, spec_file='specimens.txt',samp_file="samples.txt",
                     site_file="sites.txt",loc_file="locations.txt",dir_path='.',
                     input_dir_path='',comp_depth_key="",lat="",lon="",
                     exp_name="",exp_desc="",age_low=0,age_high=200e6):
    """
    Convert IODP samples data file downloaded from LIMS as .csv file into datamodel 3.0 MagIC samples file.
    Default is to overwrite samples.txt in your working directory.

    Parameters
    ----------
    lims_sample_file : str
        path to IODP sample file to convert
    dir_path : str
        working directory, default "."
    input_dir_path : str
        input file directory IF different from dir_path, default ""

    spec_file : str
        output specimens.txt file name
    samp_file : str
        output samples.txt file name
    site_file : str
        output sites.txt file name
    loc_file : str
        output locations.txt name
    comp_depth_key : str
        if not "", there is a composite depth model, for example 'Top depth CSF-B (m)'
    lat : float
        latitude of hole location
    lon : float
        longitude of hole location
    exp_name : str
        expedition number (e.g., 382)
    exp_description :str
        expedition nick name (e.g., Iceberg Alley)
    age_high : float
        maximum age of hole in Ma
    age_low : float
        minimum age of hole in Ma

    NOTE:  all output files will overwrite existing files.

    Returns
    --------
    type - Tuple : (True or False indicating if conversion was sucessful,file names written)

    """
    # define required columns for samples, sites, locations (holes).
    spec_reqd_columns=['specimen','sample','result_quality','method_codes','volume',\
                       'specimen_name_alternatives','citations']
    samp_reqd_columns=['sample','site','result_type','result_quality',\
                       'azimuth','dip','azimuth_correction','method_codes','citations']
    site_reqd_columns=['site','location','lat','lon','result_type','result_quality','method_codes',\
                       'core_depth','composite_depth',\
                       'geologic_types','geologic_classes','geologic_lithologies',\
                      'age','age_low','age_high','age_unit','citations']
    loc_reqd_columns=['expedition_name','expedition_ship','expedition_description','lat_s','lat_n',\
                      'geologic_classes','geologic_lithologies','location_type',\
                      'lon_w','lon_e','age_low','age_high','age_unit','citations']
    # fix the path names for input and output directories (if different)
    input_dir_path, output_dir_path = pmag.fix_directories(input_dir_path, dir_path)
    # find the input sample file name downloaded from LORE
    samp_file_name = pmag.resolve_file_name(lims_sample_file, input_dir_path)
    # set the output file names
    spec_out = os.path.join(output_dir_path, spec_file)
    samp_out = os.path.join(output_dir_path, samp_file)
    site_out = os.path.join(output_dir_path, site_file)
    loc_out = os.path.join(output_dir_path, loc_file)
    # read in the data from the downloaded .csv file
    iodp_sample_data=pd.read_csv(samp_file_name)
    # get rid of incomplete sample rows
    iodp_sample_data.dropna(subset=['Hole','Type','Sect','Top offset (cm)','Top depth CSF-A (m)','A/W'],
                            inplace=True)

    # replace the 'nan' values with blanks
    iodp_sample_data.fillna('',inplace=True)
    # check if there are data
    if len(iodp_sample_data)==0:
        return False, "Could not extract the necessary data from your input file.\nPlease make sure you are providing a correctly formated IODP samples csv file."
    # create the MagIC data model 3.0 DataFrames with the required column
    specimens_df=pd.DataFrame(columns = spec_reqd_columns)
    samples_df=pd.DataFrame(columns = samp_reqd_columns)
    sites_df=pd.DataFrame(columns = site_reqd_columns)
    # set some column headers for the sample master .csv file
    depth_key = "Top depth CSF-A (m)"
    text_key = "Text ID"
    date_key = "Date sample logged"
    volume_key = 'Volume (cm3)'
    if volume_key not in iodp_sample_data.columns:
        volume_key = 'Sample volume (CC)'
    # convert the meta data in the sample master to MagIC datamodel 3
    # construct the sample name
    holes,specimens=iodp_sample_names(iodp_sample_data)
    # put the sample name in the specimen, sample, site
    specimens_df['specimen']=specimens
    specimens_df['sample']=specimens
    samples_df['sample']=specimens
    samples_df['site']=specimens

    # add required meta-data for each hole to the locations table
    unique_holes=list(holes.unique())
    loc_dicts=[]
    for hole in unique_holes:
        loc_dict={}
        print (hole)
        loc_dict['location_name']=hole
        loc_dict['expedition_name']=exp_name
        loc_dict['expedition_ship']='JOIDES Resolution'
        loc_dict['expedition_description']=exp_desc
        loc_dict['lat_s']=lat
        loc_dict['lat_n']=lat
        loc_dict['lon_w']=lon
        loc_dict['lon_e']=lon
        loc_dict['location_type']="Drill Site"
        loc_dict['citations']='This study'
        loc_dict['age_high']=age_high
        loc_dict['age_low']=age_low
        loc_dict['age_unit']='Ma'

        loc_dicts.append(loc_dict)
    # add in the rest to the sites table
    sites_df['site']=specimens
    sites_df['core_depth']=iodp_sample_data['Top depth CSF-A (m)']
    sites_df['lat']=lat
    sites_df['lon']=lon
    sites_df['result_type']='i'
    sites_df['result_quality']='g'
    sites_df['method_codes']='FS-C-DRILL-IODP'
    sites_df['location']=hole
    sites_df['citations']="This study"
    if comp_depth_key: sites_df['composite_depth']=iodp_sample_data[comp_depth_key]
    # now do the samples

    samples_df['method_codes']='FS-C-DRILL-IODP:SP-SS-C:SO-V' # could put in sampling tool...
    samples_df['site']=specimens # same as sample and specimen name
    samples_df['result_type']='i'
    samples_df['result_type']='g'
    samples_df['azimuth']='0'
    samples_df['dip']='0'
    samples_df['citations']='This study'

    # NEED TO ADD TIMESTAMP FROM TIMESTAMP KEY SOMEDAY
    # and the specimens
    specimens_df['specimen_name_alternatives']=iodp_sample_data[text_key]
    specimens_df['result_quality']='g'
    specimens_df['method_codes']='FS-C-DRILL-IODP:SP-SS-C:SO-V'
    specimens_df['volume']=iodp_sample_data[volume_key]*1e-6 # convert from cm^3 to m^3
    specimens_df['citations']='This study'
    # fill in the np.nan with blanks
    specimens_df.fillna("",inplace=True)
    # make specimen_df format compatible with MagicDataFrame
    specimens_df.index = specimens_df['specimen']
    specimens_df.index.name = 'specimen name'
    # combine with old specimen records if available
    if os.path.exists(spec_file):
        old_specimens = cb.MagicDataFrame(spec_file)
        old_specimens.df = old_specimens.merge_dfs(specimens_df)
        old_specimens.write_magic_file(spec_file)
    else:
        spec_dicts = specimens_df.to_dict('records')
        pmag.magic_write(spec_out, spec_dicts, 'specimens')

    # fill in the np.nan with blanks
    samples_df.fillna("",inplace=True)
    sites_df.fillna("",inplace=True)
    # save the files in the designated spots samp_out, site_out and loc_out
    samp_dicts = samples_df.to_dict('records')
    pmag.magic_write(samp_out, samp_dicts, 'samples')
    site_dicts = sites_df.to_dict('records')
    pmag.magic_write(site_out, site_dicts, 'sites')
    pmag.magic_write(loc_out, loc_dicts, 'locations')
    return True

Response: Convert IODP samples data file downloaded from LIMS as. csv file into datamodel 3. 0 MagIC samples file.


Instruction:
How would you implement a function in Python 3 that
converts IODP samples data generated from the SRM measurements file into datamodel 3. 0 MagIC samples file.

Response:
def iodp_samples_srm(df, spec_file='specimens.txt',samp_file="samples.txt",site_file="sites.txt",dir_path='.',
                 input_dir_path='',comp_depth_key="",lat="",lon=""):
    """
    Convert IODP samples data generated from the SRM measurements file into datamodel 3.0 MagIC samples file.
    Default is to overwrite the output files in your working directory.

    Parameters
    ----------
    df : str
        Pandas DataFrame of SRM Archive data
    dir_path : str
        working directory, default "."
    input_dir_path : str
        input file directory IF different from dir_path, default ""

    spec_file : str
        output specimens.txt file name
    samp_file : str
        output samples.txt file name
    site_file : str
        output sites.txt file name
    comp_depth_key : str
        if not "", there is a composite depth model, for example 'Depth CSF-B (m)'
    lat : float
        latitude of hole location
    lon : float
        longitude of hole location

    NOTE:  all output files will overwrite existing files.

    Returns
    --------
    type - Tuple : (True or False indicating if conversion was sucessful,file names written)

    """
    # define required columns for samples, sites, locations (holes).
    spec_reqd_columns=['specimen','sample','result_quality','method_codes','volume',\
                       'specimen_name_alternatives','citations']
    samp_reqd_columns=['sample','site','result_type','result_quality',\
                       'azimuth','dip','azimuth_correction','method_codes','citations','core_depth','composite_depth']
    site_reqd_columns=['site','location','lat','lon','result_type','result_quality','method_codes',\
                       'core_depth','composite_depth',\
                       'geologic_types','geologic_classes','geologic_lithologies',\
                      'age','age_low','age_high','age_unit','citations']
    # fix the path names for input and output directories (if different)
    input_dir_path, output_dir_path = pmag.fix_directories(input_dir_path, dir_path)
    # find the input sample file name downloaded from LORE
    # set the output file names
    spec_out = os.path.join(output_dir_path, spec_file)
    samp_out = os.path.join(output_dir_path, samp_file)
    site_out = os.path.join(output_dir_path, site_file)
    # read in the data from the downloaded .csv file
    # create the MagIC data model 3.0 DataFrames with the required column
    specimens_df=pd.DataFrame(columns = spec_reqd_columns)
    samples_df=pd.DataFrame(columns = samp_reqd_columns)
    sites_df=pd.DataFrame(columns = site_reqd_columns)
    # set some column headers for the sample master .csv file
    depth_key = "Depth CSF-A (m)"
    text_key = "Text ID"
    date_key = "Date sample logged"
    volume_key = 'Volume (cm3)'
    # convert the meta data in the sample master to MagIC datamodel 3
    # construct the sample name

    holes,specimens=iodp_sample_names(df)
    # put the sample name in the specimen, sample, site
    specimens_df['specimen']=specimens
    specimens_df['sample']=specimens
    samples_df['sample']=specimens
    samples_df['site']=specimens
    samples_df['core_depth']=df[depth_key]
    if comp_depth_key: samples_df['composite_depth']=df[comp_depth_key]

    # add in the rest to the sites table
    sites_df['site']=specimens
    sites_df['core_depth']=df[depth_key]
    sites_df['lat']=lat
    sites_df['lon']=lon
    sites_df['result_type']='i'
    sites_df['result_quality']='g'
    sites_df['method_codes']='FS-C-DRILL-IODP'
    sites_df['location']=holes[0]
    sites_df['citations']="This study"
    if comp_depth_key: sites_df['composite_depth']=df[comp_depth_key]
    # now do the samples

    samples_df['method_codes']='FS-C-DRILL-IODP:SP-SS-C:SO-V' # could put in sampling tool...
    samples_df['site']=specimens # same as sample and specimen name
    samples_df['result_type']='i'
    samples_df['result_type']='g'
    samples_df['azimuth']='0'
    samples_df['dip']='0'
    samples_df['citations']='This study'

    # NEED TO ADD TIMESTAMP FROM TIMESTAMP KEY SOMEDAY
    # and the specimens
    specimens_df['result_quality']='g'
    specimens_df['method_codes']='FS-C-DRILL-IODP'
    specimens_df['citations']='This study'
    # fill in the np.nan with blanks
    specimens_df.fillna("",inplace=True)
    samples_df.fillna("",inplace=True)
    sites_df.fillna("",inplace=True)
    # save the files in the designated spots spec_out, samp_out, site_out and loc_out
    spec_dicts = specimens_df.to_dict('records')
    pmag.magic_write(spec_out, spec_dicts, 'specimens')
    samp_dicts = samples_df.to_dict('records')
    pmag.magic_write(samp_out, samp_dicts, 'samples')
    site_dicts = sites_df.to_dict('records')
    pmag.magic_write(site_out, site_dicts, 'sites')

    return holes[0],specimens


Instruction:
Can you implement a function in Python 3 that
converts IODP archive half measurement files into MagIC file

Response:
def iodp_srm_lore(srm_file, dir_path=".", input_dir_path="",noave=False,comp_depth_key='Depth CSF-B (m)',\
              meas_file="srm_arch_measurements.txt", spec_file="srm_arch_specimens.txt",\
                 samp_file='srm_arch_samples.txt',site_file='srm_arch_sites.txt',lat="",lon=""):
    """
    Convert IODP archive half measurement files into MagIC file(s).

    Parameters
    ----------
    srm_file : str
        input csv file downloaded from LIMS online repository
    dir_path : str
        output directory, default "."
    input_dir_path : str
        input file directory IF different from dir_path, default ""
    meas_file : str
        output measurement file name, default "measurements.txt"
    spec_file : str
        specimens file name as output file - these specimens are not already in the specimen table
        created by iodp_samples_csv
    samp_file : str
       samples file name as output file - these  are not already in the specimen table
        created by iodp_samples_csv
    site_file : str
        sites file name as output file - these are not already in the samples table
    noave : Boolean
        if False, average replicate measurements

    Returns
    --------
    type - Tuple : (True or False indicating if conversion was sucessful, meas_file name written)
    """
    # initialize defaults
    version_num = pmag.get_version()
    # format variables
    input_dir_path, output_dir_path = pmag.fix_directories(input_dir_path, dir_path)
    # convert cc to m^3
    meas_reqd_columns=['specimen','measurement','experiment','sequence','quality','method_codes',\
                       'instrument_codes','citations',\
                       'treat_temp','treat_ac_field','treat_dc_field',\
                      'treat_dc_field_phi','treat_dc_field_theta','meas_temp',\
                      'dir_dec','dir_inc','magn_moment','magn_volume',\
                       'description','timestamp','software_packages',\
                       'external_database_ids','treat_step_num','meas_n_orient']
    srm_file = pmag.resolve_file_name(srm_file, input_dir_path)
    spec_file = pmag.resolve_file_name(spec_file, dir_path)
    in_df=pd.read_csv(srm_file,header=0)
    in_df.drop_duplicates(inplace=True)
    in_df.sort_values(by='Treatment Value',inplace=True)
    if len(in_df)==0:
        print ('you must download a csv file from the LIMS database and place it in your input_dir_path')
        return False
    measurements_df=pd.DataFrame(columns=meas_reqd_columns)
    meas_out = os.path.join(output_dir_path, meas_file)
    hole,srm_specimens=iodp_samples_srm(in_df, spec_file=spec_file,samp_file=samp_file,site_file=site_file,\
                                        dir_path=dir_path,input_dir_path=input_dir_path,\
                                        comp_depth_key=comp_depth_key,lat=lat,lon=lon)
    # assume only one hole
    # set up defaults
    measurements_df['specimen']=srm_specimens
    measurements_df['quality']='g'
    measurements_df['citations']='This study'
    measurements_df['meas_temp']=273
    measurements_df['software_packages']=version_num
    measurements_df["treat_temp"] = '%8.3e' % (273)  # room temp in kelvin
    measurements_df["meas_temp"] = '%8.3e' % (273)  # room temp in kelvin
    measurements_df["treat_ac_field"] = '0'
    measurements_df["treat_dc_field"] = '0'
    measurements_df["treat_dc_field_phi"] = '0'
    measurements_df["treat_dc_field_theta"] = '0'
    measurements_df["standard"] = 'u'  # assume all data are "good"
    measurements_df["dir_csd"] = '0'  # assume all data are "good"
    measurements_df["method_codes"] = 'LT-NO' # assume all are NRMs
    measurements_df['instrument_codes']="IODP-SRM" # assume all measurements on shipboard 2G
    measurements_df['treat_step_num']='0' # assign a number
    measurements_df['meas_n_orient']=1 # at least one orientation
    measurements_df['timestamp']=pd.to_datetime(\
                               in_df['Timestamp (UTC)']).dt.strftime("%Y-%m-%dT%H:%M:%S")+'Z'
    measurements_df['dir_dec']=in_df['Declination background & drift corrected (deg)'] # declination
    measurements_df['dir_inc']=in_df['Inclination background & drift corrected (deg)'] # inclination
    measurements_df['magn_volume']=in_df['Intensity background & drift corrected (A/m)'] # magnetization
    Xs=in_df['Magnetic moment x (Am²)']
    Ys=in_df['Magnetic moment y (Am²)']
    Zs=in_df['Magnetic moment z (Am²)']
    magn_moment=np.sqrt(Xs**2+Ys**2+Zs**2)
    measurements_df['magn_moment']=magn_moment # moment in Am^2
    measurements_df['description']=in_df['Treatment Type'] # temporary column
    measurements_df['treat_ac_field']=in_df['Treatment Value']*1e-3 # assume all treatments are AF
    measurements_df.loc[measurements_df['description']=='IN-LINE AF DEMAG',\
                        'method_codes']='LT-AF-Z'
    measurements_df.loc[measurements_df['description']=='IN-LINE AF DEMAG',\
                        'instrument_codes']='IODP-SRM:IODP-SRM-AF'
    measurements_df['external_database_ids']='LORE['+in_df['Test No.'].astype('str')+']'

    measurements_df.fillna("",inplace=True)
    meas_dicts = measurements_df.to_dict('records')
    meas_dicts=pmag.measurements_methods3(meas_dicts,noave=noave)
    pmag.magic_write(meas_out, meas_dicts, 'measurements')
    return True