Beispiel #1
0
def update_single_md(fname, guano_md={}, project_md={}, site_md={}):
    g = GuanoFile(fname)

    parts = parse_nabat_fname(fname)
    # content from the NABat filename
    g['NABat', 'Grid Cell GRTS ID'] = parts['GrtsId']
    g['NABat', 'Site Name'] = parts['SiteName']

    #content from the site_md
    if guano_md is None:
        g['GUANO', 'time_to_time'] = str(parts['datetime'])
    else:
        for k, v in guano_md.items():
            g['GUANO', k] = v

    #content from the project_md
    if project_md is not None:
        for k, v in project_md.items():
            g['NABat', k] = v

    #content from the site_md
    if site_md is not None:
        for k, v in site_md.items():
            g['NABat', k] = v

    g.write(make_backup=False)
Beispiel #2
0
def sonobat2guano(fname):
    """Convert a file with Sonobat metadata to GUANO metadata"""
    print '\n', fname
    sb_md = extract_sonobat_metadata(fname)
    if not sb_md:
        print >> sys.stderr, 'Skipping non-Sonobat file: ' + fname
        return False
    pprint(sb_md)

    gfile = GuanoFile(fname)
    gfile['GUANO|Version'] = 1.0
    if 'timestamp' in sb_md:
        gfile['Timestamp'] = sb_md['timestamp']
    if sb_md.get('te', 1) != 1:
        gfile['TE'] = sb_md['te']
    gfile['Length'] = sb_md['length']
    gfile['Note'] = sb_md['note'].strip().replace('\r\n', '\\n').replace('\n', '\\n')
    if sb_md.get('species', None):
        gfile['Species Auto ID'] = sb_md['species']

    if 'd500x' in sb_md:
        for k, v in sb_md['d500x'].items():
            gfile['PET', k] = v

    if 'ar125' in sb_md:
        for k, v in sb_md['ar125'].items():
            gfile['BAT', k] = v

    print gfile._as_string()

    gfile.write()
Beispiel #3
0
def d500x2guano(fname):
    """Convert a file with raw D500X metadata to use GUANO metadata instead"""
    print "\n", fname
    md = extract_d500x_metadata(fname)
    if not md:
        print >> sys.stderr, "Skipping non-D500X file: " + fname
        return False
    pprint(md)

    gfile = GuanoFile(fname)
    gfile["GUANO|Version"] = 1.0

    gfile["Make"] = "Pettersson"
    gfile["Model"] = "D500X"
    gfile["Timestamp"] = md.pop("File Time")
    gfile["Samplerate"] = md.pop("Samplerate")
    gfile["Length"] = md.pop("Length")

    if md.get("Profile HP", None) == "Y":
        gfile["Filter HP"] = 20

    lat, lon = md.pop("LAT", None), md.pop("LON", None)
    if lat and lon:
        gfile["Loc Position"] = dms2decimal(lat), dms2decimal(lon)

    for k, v in md.items():
        gfile["PET", k] = v

    print gfile._as_string()

    gfile.wav_data = gfile.wav_data[D500X_DATA_SKIP_BYTES:]  # throw out the metadata bytes from 'data' chunk

    unlock(fname)  # D500X "locks" files as unwriteable, we must unlock before we can modify
    gfile.write()
Beispiel #4
0
def wamd2guano(fname):
    """Convert a Wildlife Acoustics WAMD metadata file to GUANO metadata format"""
    wamd_md = wamd(fname)
    pprint(wamd_md)

    gfile = GuanoFile(fname)
    gfile['GUANO|Version'] = 1.0

    gfile['Timestamp'] = wamd_md.pop('timestamp')
    gfile['Note'] = wamd_md.pop('notes', '')

    gfile['Make'] = 'Wildlife Acoustics'
    gfile['Model'] = wamd_md.pop('model', '')
    gfile['Firmware Version'] = wamd_md.pop('firmware', '')

    gfile['Species Auto ID'] = wamd_md.pop('auto_id', '')
    gfile['Species Manual ID'] = wamd_md.pop('manual_id', '')

    gfile['TE'] = wamd_md.pop('time_expansion', 1)
    gfile['Samplerate'] = gfile.wav_params.framerate * gfile['TE']
    gfile['Length'] = gfile.wav_params.nframes / float(gfile.wav_params.framerate) * gfile['TE']

    if 'gpsfirst' in wamd_md:
        lat, lon, alt = wamd_md.pop('gpsfirst')
        gfile['Loc Position'] = lat, lon
        gfile['Loc Elevation'] = alt

    for k, v in wamd_md.items():
        gfile['WA', k] = v

    print(gfile.to_string())

    gfile.write()
Beispiel #5
0
def d500x2guano(fname):
    """Convert a file with raw D500X metadata to use GUANO metadata instead"""
    print('\n', fname)
    md = extract_d500x_metadata(fname)
    if not md:
        print('Skipping non-D500X file: ' + fname, file=sys.stderr)
        return False
    pprint(md)

    gfile = GuanoFile(fname)
    gfile['GUANO|Version'] = 1.0

    gfile['Make'] = 'Pettersson'
    gfile['Model'] = 'D500X'
    gfile['Timestamp'] = md.pop('File Time')
    gfile['Original Filename'] = md.pop('File Name')
    gfile['Samplerate'] = md.pop('Samplerate')
    gfile['Length'] = md.pop('Length')

    if md.get('Profile HP', None) == 'Y':
        gfile['Filter HP'] = 20

    lat, lon = md.pop('LAT', None), md.pop('LON', None)
    if lat and lon:
        gfile['Loc Position'] = dms2decimal(lat), dms2decimal(lon)

    for k, v in md.items():
        gfile['PET', k] = v

    print(gfile.to_string())

    gfile.wav_data = gfile.wav_data[D500X_DATA_SKIP_BYTES:]  # throw out the metadata bytes from 'data' chunk

    unlock(fname)  # D500X "locks" files as unwriteable, we must unlock before we can modify
    gfile.write()
def batlogger2guano(fname):
    """Convert an Elekon BatLogger .WAV with sidecar .XML to GUANO metadata"""
    xmlfname = os.path.splitext(fname)[0] + '.xml'
    if not os.path.exists(xmlfname):
        raise ValueError('Unable to find XML metadata file for %s' % fname)
    g = GuanoFile(fname)
    with open(xmlfname, 'rt') as f:
        xml = ElementTree.parse(f)

    g['Timestamp'] = get(xml, 'DateTime', lambda x: datetime.strptime(x, '%d.%m.%Y %H:%M:%S'))
    g['Firmware Version'] = get(xml, 'Firmware')
    g['Make'] = 'Elekon'
    g['Model'] = 'BatLogger'
    g['Serial'] = get(xml, 'SN')
    g['Samplerate'] = get(xml, 'Samplerate', lambda x: int(x.split()[0]))
    g['Length'] = get(xml, 'Duration', lambda x: float(x.split()[0]))
    g['Original Filename'] = get(xml, 'Filename')
    g['Temperature Ext'] = get(xml, 'Temperature', lambda x: float(x.split()[0]))
    g['Loc Position'] = get(xml, 'GPS/Position', lambda x: tuple(map(float, x.split())))
    g['Loc Elevation'] = get(xml, 'GPS/Altitude', lambda x: float(x.split()[0]))

    g['Elekon|BattVoltage'] = get(xml, 'BattVoltage')
    for node in xml.find('Trigger'):
        g['Elekon|Trigger|%s' % node.tag] = node.text
    for node in xml.find('GPS'):
        g['Elekon|GPS|%s' % node.tag] = node.text

    # for k, v in g.items():
    #     print('%s:\t%s' % (k, v))

    print(g.to_string())
    g.write()
    os.remove(xmlfname)

    return g
Beispiel #7
0
def wamd2guano(fname):
    """Convert a Wildlife Acoustics WAMD metadata file to GUANO metadata format"""
    wamd_md = wamd(fname)
    pprint(wamd_md)

    gfile = GuanoFile(fname)
    gfile['GUANO|Version'] = 1.0

    gfile['Timestamp'] = wamd_md.pop('timestamp')
    gfile['Note'] = wamd_md.pop('notes', '')

    gfile['Make'] = 'Wildlife Acoustics'
    gfile['Model'] = wamd_md.pop('model', '')
    gfile['Firmware Version'] = wamd_md.pop('firmware', '')

    gfile['Species Auto ID'] = wamd_md.pop('auto_id', '')
    gfile['Species Manual ID'] = wamd_md.pop('manual_id', '')

    gfile['TE'] = wamd_md.pop('time_expansion', 1)
    gfile['Samplerate'] = gfile.wav_params.framerate * gfile['TE']
    gfile['Length'] = gfile.wav_params.nframes / float(gfile.wav_params.framerate) * gfile['TE']

    if 'gpsfirst' in wamd_md:
        lat, lon, alt = wamd_md.pop('gpsfirst')
        gfile['Loc Position'] = lat, lon
        gfile['Loc Elevation'] = alt

    for k, v in wamd_md.items():
        gfile['WAC', k] = v

    print gfile._as_string()

    gfile.write()
Beispiel #8
0
def sonobat2guano(fname):
    """Convert a file with Sonobat metadata to GUANO metadata"""
    print('\n', fname)
    sb_md = extract_sonobat_metadata(fname)
    if not sb_md:
        print('Skipping non-Sonobat file: ' + fname, file=sys.stderr)
        return False
    pprint(sb_md)

    gfile = GuanoFile(fname)
    gfile['GUANO|Version'] = 1.0
    if 'timestamp' in sb_md:
        gfile['Timestamp'] = sb_md['timestamp']
    if sb_md.get('te', 1) != 1:
        gfile['TE'] = sb_md['te']
    gfile['Length'] = sb_md['length']
    gfile['Note'] = sb_md['note'].strip().replace('\r\n',
                                                  '\\n').replace('\n', '\\n')
    if sb_md.get('species', None):
        gfile['Species Auto ID'] = sb_md['species']

    if 'd500x' in sb_md:
        for k, v in sb_md['d500x'].items():
            gfile['PET', k] = v

    if 'ar125' in sb_md:
        for k, v in sb_md['ar125'].items():
            gfile['BAT', k] = v

    print(gfile.to_string())

    gfile.write()
    def save(self):
        fname = self.ui.file_name.text()
        try:
            g = GuanoFile(fname)
        except:
            msg = f"There was a problem loading the Guano MD from:\n{fname}\n\nPlease verify that it is a valid wav file"
            QMessageBox.warning(self, "File Error", msg)
            return None

        for namespace_name, namespace_group in self.namespaces.items():
            print(namespace_name)
            namespace_data = namespace_group.get_data()

            if namespace_name == 'guano_base':
                namespace_name = ''

            for k, v in namespace_data.items():
                if v == '':
                    try:
                        del g[f"{namespace_name}|{k}"]
                    except KeyError:
                        pass
                else:
                    g[f"{namespace_name}|{k}"] = v

        g.write(make_backup=False)
    def save(self):

        d = Path(self.ui.directory_name.text())
        wavs = list(d.glob('**\*.wav'))
        wavs += list(d.glob('**\*.zc'))

        self.ui.progressBar.setVisible(True)
        self.ui.progressBar.setMinimum(1)
        self.ui.progressBar.setMaximum(len(wavs))
        self.ui.progressBar.setVisible(1)

        if self.ui.directory_name_output.text():
            if not Path(self.ui.directory_name_output.text()).exists():
                msq = r"The output directory specified does not exist!  Please point to an existing directory."
                QMessageBox.warning(self, "Output directory does not exist",
                                    msg)
                return None
            out_dir = Path(self.ui.directory_name_output.text())
            make_copy = True
        else:
            out_dir = Path(self.ui.directory_name.text())
            make_copy = False

        for f in wavs:
            original_fname = f.name

            new_fname = self.change_fname_function(original_fname, f=f)

            full_name = f.parent.joinpath(new_fname)
            full_name = str(full_name).replace(str(Path(self.ui.directory_name.text())), \
                                                   str(out_dir))
            if not Path(full_name).exists():
                Path(full_name).parent.mkdir(parents=True, exist_ok=True)
                if make_copy:
                    shutil.copy(str(f), full_name)
                else:
                    f.rename(full_name)

            if original_fname != new_fname:
                try:
                    g = GuanoFile(full_name)
                    g['Original Filename'] = original_fname
                    g.write(make_backup=False)
                except:
                    pass

            self.ui.progressBar.setValue(self.ui.progressBar.value() + 1)

        msg = f"Finished renaming files in directory:\n\n{out_dir}"
        QMessageBox.information(self, "Renaming Process Complete", msg)

        self.ui.progressBar.setVisible(False)