Skip to content

Instantly share code, notes, and snippets.

@joefutrelle
Last active February 5, 2024 11:55
Show Gist options
  • Save joefutrelle/7f6e23ac7ac480a2c2b9712c12355dde to your computer and use it in GitHub Desktop.
Save joefutrelle/7f6e23ac7ac480a2c2b9712c12355dde to your computer and use it in GitHub Desktop.
Parse Seabird CTD .btl file
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Bottle', 'Date', 'Sal00', 'Sal11', 'Sigma-t00', 'Sigma-t11', 'OxsatMm/Kg', 'Sbox0Mm/Kg', 'Sbox1Mm/Kg', 'Potemp090C', 'Potemp190C', 'SvCM', 'SvCM1', 'Scan', 'TimeJ', 'TimeS', 'PrDM', 'T090C', 'T190C', 'T2-T190C', 'C0S/m', 'C1S/m', 'C2-C1S/m', 'V0', 'CStarAt0', 'CStarTr0', 'V1', 'FlECO-AFL', 'V2', 'AltM', 'V3', 'V4', 'Sbeox0V', 'V5', 'Sbeox1V', 'V6', 'Par', 'Cpar', 'V7', 'Spar', 'Latitude', 'Longitude']\n"
]
}
],
"source": [
"import pandas as pd\n",
"import re\n",
"\n",
"BTL_FILE = r'EN608_L2_Cast04.btl'\n",
"#BTL_FILE = 'ar28b003.btl'\n",
"\n",
"lines = []\n",
"\n",
"with open(BTL_FILE) as fin:\n",
" for l in fin.readlines():\n",
" # skip header lines\n",
" if l.startswith('#') or l.startswith('*'):\n",
" continue\n",
" lines.append(l.rstrip())\n",
"\n",
"# the first line is the first line of column headers\n",
"col_headers = re.split(r' +',lines[0])[1:]\n",
"# third line and on are the data lines\n",
"lines = lines[2:]\n",
"\n",
"print(col_headers)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# average values are every four lines\n",
"avg_lines = lines[::4]\n",
"# the lines with the time (and sddev values) are the ones immediately following the average value lines\n",
"time_lines = lines[1::4]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# columns are 11 characters wide, except the first two\n",
"col_widths = [7,15] + [11] * (len(col_headers) - 2)\n",
"\n",
"# read fixed-width column values\n",
"def col_values_iter(line, col_widths):\n",
" i = 0\n",
" for w in col_widths:\n",
" start = i\n",
" end = i + w\n",
" yield line[start:end].lstrip()\n",
" i += w\n",
"\n",
"def col_values(line, col_widths):\n",
" return list(col_values_iter(line, col_widths))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Bottle</th>\n",
" <th>Date</th>\n",
" <th>Sal00</th>\n",
" <th>Sal11</th>\n",
" <th>Sigma-t00</th>\n",
" <th>Sigma-t11</th>\n",
" <th>OxsatMm/Kg</th>\n",
" <th>Sbox0Mm/Kg</th>\n",
" <th>Sbox1Mm/Kg</th>\n",
" <th>Potemp090C</th>\n",
" <th>...</th>\n",
" <th>Sbeox0V</th>\n",
" <th>V5</th>\n",
" <th>Sbeox1V</th>\n",
" <th>V6</th>\n",
" <th>Par</th>\n",
" <th>Cpar</th>\n",
" <th>V7</th>\n",
" <th>Spar</th>\n",
" <th>Latitude</th>\n",
" <th>Longitude</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>Feb 01 2018 05:44:26</td>\n",
" <td>32.7592</td>\n",
" <td>32.7570</td>\n",
" <td>25.9766</td>\n",
" <td>25.9748</td>\n",
" <td>318.78100</td>\n",
" <td>-85.158</td>\n",
" <td>-84.973</td>\n",
" <td>4.2685</td>\n",
" <td>...</td>\n",
" <td>0.0013</td>\n",
" <td>0.0024</td>\n",
" <td>0.0024</td>\n",
" <td>0.1722</td>\n",
" <td>1.0000e-12</td>\n",
" <td>1.0556e-11</td>\n",
" <td>0.0000</td>\n",
" <td>9.4731e+00</td>\n",
" <td>41.02064</td>\n",
" <td>-70.87162</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2</td>\n",
" <td>Feb 01 2018 05:44:40</td>\n",
" <td>32.7591</td>\n",
" <td>32.7568</td>\n",
" <td>25.9765</td>\n",
" <td>25.9746</td>\n",
" <td>318.77156</td>\n",
" <td>-85.172</td>\n",
" <td>-84.972</td>\n",
" <td>4.2697</td>\n",
" <td>...</td>\n",
" <td>0.0013</td>\n",
" <td>0.0024</td>\n",
" <td>0.0024</td>\n",
" <td>0.1722</td>\n",
" <td>1.0000e-12</td>\n",
" <td>1.0556e-11</td>\n",
" <td>0.0000</td>\n",
" <td>9.4731e+00</td>\n",
" <td>41.02056</td>\n",
" <td>-70.87158</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>Feb 01 2018 05:47:02</td>\n",
" <td>32.7612</td>\n",
" <td>32.7585</td>\n",
" <td>25.9775</td>\n",
" <td>25.9753</td>\n",
" <td>318.72313</td>\n",
" <td>-84.854</td>\n",
" <td>-84.738</td>\n",
" <td>4.2763</td>\n",
" <td>...</td>\n",
" <td>0.0018</td>\n",
" <td>0.0027</td>\n",
" <td>0.0027</td>\n",
" <td>0.1721</td>\n",
" <td>1.0000e-12</td>\n",
" <td>1.0556e-11</td>\n",
" <td>0.0000</td>\n",
" <td>9.4731e+00</td>\n",
" <td>41.02010</td>\n",
" <td>-70.87136</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4</td>\n",
" <td>Feb 01 2018 05:47:15</td>\n",
" <td>32.7599</td>\n",
" <td>32.7574</td>\n",
" <td>25.9769</td>\n",
" <td>25.9748</td>\n",
" <td>318.75613</td>\n",
" <td>-84.893</td>\n",
" <td>-84.805</td>\n",
" <td>4.2724</td>\n",
" <td>...</td>\n",
" <td>0.0019</td>\n",
" <td>0.0026</td>\n",
" <td>0.0026</td>\n",
" <td>0.1721</td>\n",
" <td>1.0000e-12</td>\n",
" <td>1.0556e-11</td>\n",
" <td>0.0000</td>\n",
" <td>9.4731e+00</td>\n",
" <td>41.02006</td>\n",
" <td>-70.87133</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5</td>\n",
" <td>Feb 01 2018 05:47:25</td>\n",
" <td>32.7596</td>\n",
" <td>32.7571</td>\n",
" <td>25.9766</td>\n",
" <td>25.9746</td>\n",
" <td>318.74959</td>\n",
" <td>-84.908</td>\n",
" <td>-84.701</td>\n",
" <td>4.2734</td>\n",
" <td>...</td>\n",
" <td>0.0018</td>\n",
" <td>0.0030</td>\n",
" <td>0.0030</td>\n",
" <td>0.1722</td>\n",
" <td>1.0000e-12</td>\n",
" <td>1.0556e-11</td>\n",
" <td>0.0000</td>\n",
" <td>9.4731e+00</td>\n",
" <td>41.02002</td>\n",
" <td>-70.87130</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 42 columns</p>\n",
"</div>"
],
"text/plain": [
" Bottle Date Sal00 Sal11 Sigma-t00 Sigma-t11 \\\n",
"0 1 Feb 01 2018 05:44:26 32.7592 32.7570 25.9766 25.9748 \n",
"1 2 Feb 01 2018 05:44:40 32.7591 32.7568 25.9765 25.9746 \n",
"2 3 Feb 01 2018 05:47:02 32.7612 32.7585 25.9775 25.9753 \n",
"3 4 Feb 01 2018 05:47:15 32.7599 32.7574 25.9769 25.9748 \n",
"4 5 Feb 01 2018 05:47:25 32.7596 32.7571 25.9766 25.9746 \n",
"\n",
" OxsatMm/Kg Sbox0Mm/Kg Sbox1Mm/Kg Potemp090C ... Sbeox0V V5 \\\n",
"0 318.78100 -85.158 -84.973 4.2685 ... 0.0013 0.0024 \n",
"1 318.77156 -85.172 -84.972 4.2697 ... 0.0013 0.0024 \n",
"2 318.72313 -84.854 -84.738 4.2763 ... 0.0018 0.0027 \n",
"3 318.75613 -84.893 -84.805 4.2724 ... 0.0019 0.0026 \n",
"4 318.74959 -84.908 -84.701 4.2734 ... 0.0018 0.0030 \n",
"\n",
" Sbeox1V V6 Par Cpar V7 Spar Latitude \\\n",
"0 0.0024 0.1722 1.0000e-12 1.0556e-11 0.0000 9.4731e+00 41.02064 \n",
"1 0.0024 0.1722 1.0000e-12 1.0556e-11 0.0000 9.4731e+00 41.02056 \n",
"2 0.0027 0.1721 1.0000e-12 1.0556e-11 0.0000 9.4731e+00 41.02010 \n",
"3 0.0026 0.1721 1.0000e-12 1.0556e-11 0.0000 9.4731e+00 41.02006 \n",
"4 0.0030 0.1722 1.0000e-12 1.0556e-11 0.0000 9.4731e+00 41.02002 \n",
"\n",
" Longitude \n",
"0 -70.87162 \n",
"1 -70.87158 \n",
"2 -70.87136 \n",
"3 -70.87133 \n",
"4 -70.87130 \n",
"\n",
"[5 rows x 42 columns]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# now assemble the rows of the dataframe\n",
"rows = []\n",
"\n",
"for al, tl in zip(avg_lines, time_lines):\n",
" cvs = col_values(al, col_widths)\n",
" time = col_values(tl, col_widths)[1] # just use the first value\n",
" cvs[1] = '{} {}'.format(cvs[1], time)\n",
" rows.append(cvs)\n",
"\n",
"df = pd.DataFrame(rows, columns=col_headers)\n",
"df.head()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:pyifcb]",
"language": "python",
"name": "conda-env-pyifcb-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment