<file_sep># Buoydata #

A simple package for reading NOAA [global drifter program](http://www.aoml.noaa.gov/phod/dac/index.php) data.

## Download Data

Navigate to ftp://ftp.aoml.noaa.gov/pub/phod/buoydata/

Download the buoydata_*.dat files (naming conventions make it impossible to automate)

## Requirements ##

- Pandas


<file_sep>import pandas as pd
from datetime import datetime

col_names = ['id', 'mm', 'dd', 'yy',
            'lat', 'lon', 'temp',
            've', 'vn', 'spd', 
            'var_lat', 'var_lon', 'var_tmp']

def _parse_date(mm, dd, yy):
    dd = float(dd)
    mm = int(mm)
    yy = int(yy)
    day = int(dd)
    hour = int(24 * (dd - day))
    return datetime(yy, mm, day, hour)

def read_buoy_data(fname):
    """Read NOAA Global Drifter Program text data into pandas format.

    Parameters
    ----------
    fname : str

    Returns
    -------
    df : datafram
    """
    return pd.read_csv(fname, names=col_names, sep='\s+',
                 header=None, na_values=999.999,
                 parse_dates={'time': [1,2,3]},
                 date_parser=_parse_date)
<file_sep>from buoydata import buoydata
import pandas as pd
import datetime as datetime
import pytest

def test_parse_date():
    mm, dd, yy = '2', '2.5', '2000'
    date = buoydata._parse_date(mm,dd,yy)
    assert date == datetime.datetime(2000, 2, 2, 12, 0)

@pytest.fixture(scope='session')
def datafile(tmpdir_factory):
    data = """
 7702986    3  8.000 1988    -1.320   274.848    25.473   999.999   999.999   999.999  0.25315E-04  0.33929E-04  0.45179E-02"""
    fn = tmpdir_factory.mktemp('data').join('buoydata.dat')
    fn.write(data)
    return str(fn)

def test_read_buoy_data(datafile):
    df = buoydata.read_buoy_data(datafile)
    assert isinstance(df, pd.DataFrame)
    assert len(df)==1
    row = df.loc[0]
    for colname in ['time', 'lat', 'lon', 'temp', 've', 'vn', 'spd',
            'var_lat', 'var_lon', 'var_tmp']:
        assert colname in row
    assert row.id == 7702986
