{"id":1190,"date":"2012-07-11T20:42:49","date_gmt":"2012-07-12T00:42:49","guid":{"rendered":"http:\/\/lukemiller.org\/?p=1190"},"modified":"2012-07-11T20:42:49","modified_gmt":"2012-07-12T00:42:49","slug":"r-scripts-for-downloading-ibutton-thermochron-dataloggers","status":"publish","type":"post","link":"https:\/\/lukemiller.org\/index.php\/2012\/07\/r-scripts-for-downloading-ibutton-thermochron-dataloggers\/","title":{"rendered":"R scripts for downloading iButton Thermochron dataloggers"},"content":{"rendered":"<p><a href=\"https:\/\/lukemiller.org\/index.php\/2012\/03\/launching-ibutton-thermochrons-with-the-help-of-r\/\" target=\"_blank\">Last time,<\/a> I posted some R code to help quickly launch many iButton Thermochron temperature dataloggers with the same mission parameters. The R code makes use of a publicly-available command line utility released by the iButton&#8217;s manufacturer, Maxim.\u00a0 Of course, Maxim also has a command line utility for downloading the data from those iButtons that you launched already. The code below will make use of that program to download an iButton, give the file a unique name of your choosing, parse the data out into a simple comma-separated-value file (for easy opening in R or Excel), and then immediately be ready to download another iButton (repeat ad infinitum).<\/p>\n<p>A word of warning, I&#8217;ve only tested this on Windows 7, but Maxim does make command line utilities for Linux\/OSX that could probably be adapted into my R script. I downloaded the 1-Wire Public Domain software development kit, in this case the pre-compiled binary version for Win64 USB Preliminary Version 3.11 Beta 2 that came as a zip file called\u00a0winusb64vc311Beta2_r2.zip (downloaded 2012-03-15) from this site:<\/p>\n<p><a href=\"http:\/\/www.maxim-ic.com\/products\/ibutton\/software\/1wire\/wirekit.cfm\">http:\/\/www.maxim-ic.com\/products\/ibutton\/software\/1wire\/wirekit.cfm<\/a><\/p>\n<p>Inside the \\builds\\winusb64vc\\release folder you&#8217;ll find thermoms.exe and thermodl.exe, which are the command line utilities required for launching and downloading iButtons in Windows. They should be copied into the R working directory where these R scripts will be run.<\/p>\n<p>My iButton scripts are available in a Github.com repository:<\/p>\n<p><a href=\"http:\/\/github.com\/millerlp\/ibuttons\" target=\"_blank\">http:\/\/github.com\/millerlp\/ibuttons<\/a><\/p>\n<p>The R script for launching iButtons (mission_launch_multi.R) covered last time is available there, as are two scripts for downloading iButtons (ibutton_download_custom_names.R and ibutton_download_increment_names.R). If you have any helpful changes to these scripts, feel free to send a pull request via Github. <\/p>\n<p>The concept here is fairly straightforward. I&#8217;m using R (because it&#8217;s what I know), to ask the user to provide a name for their downloaded data file, then calling the thermodl.exe utility to download an iButton that is attached to a DS9490R USB adapter, and parsing the raw data file produced by the utility to extract the useful time\/date and temperature data. The script also includes the iButton&#8217;s serial number (in case you use that) and your unique ID for the filename in the comma-separated-value file that is output at the end.<\/p>\n<p>The script creates two directories, one to hold the raw downloaded files from the iButton created by the thermodl.exe (&#8220;_raw_downloads&#8221;) in case you want to hold on to those. The second directory (&#8220;_iButton_data&#8221;), holds csv files that can be easily opened in Excel. <\/p>\n<p><figure id=\"attachment_1200\" aria-describedby=\"caption-attachment-1200\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2012\/07\/pic11.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2012\/07\/pic11-300x62.png\" alt=\"\" title=\"pic1\" width=\"300\" height=\"62\" class=\"size-medium wp-image-1200\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2012\/07\/pic11-300x62.png 300w, https:\/\/lukemiller.org\/wp-content\/uploads\/2012\/07\/pic11.png 653w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-1200\" class=\"wp-caption-text\">Two directories will be created in your R working directory to hold the output files.<\/figcaption><\/figure><br \/>\n<\/br><br \/>\nThe &#8220;raw&#8221; data files have lots of information about the mission parameters and include the temperature histogram along with the time-stamped temperature data. These files can&#8217;t be easily imported into a spreadsheet or data frame. <\/p>\n<p><figure id=\"attachment_1192\" aria-describedby=\"caption-attachment-1192\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2012\/07\/pic2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2012\/07\/pic2-300x231.png\" alt=\"\" title=\"pic2\" width=\"300\" height=\"231\" class=\"size-medium wp-image-1192\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2012\/07\/pic2-300x231.png 300w, https:\/\/lukemiller.org\/wp-content\/uploads\/2012\/07\/pic2.png 670w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-1192\" class=\"wp-caption-text\">The &#8220;raw&#8221; datafiles downloaded from the iButton. An example of some of the information stored in the raw data file is shown.<\/figcaption><\/figure><br \/>\n<\/br><br \/>\nThe parsed csv files throw away most of that extra information and just retain the time stamps and temperature in Celsius in the first two columns. The iButton&#8217;s serial number is included in the 3rd column, as is the name I gave to this file when the script downloaded the iButton. Either of those two columns might be useful for sorting data when you start collating data from multiple iButtons in the same file.<br \/>\n<\/br><br \/>\n<figure id=\"attachment_1193\" aria-describedby=\"caption-attachment-1193\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2012\/07\/pic3.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2012\/07\/pic3-300x208.png\" alt=\"\" title=\"pic3\" width=\"300\" height=\"208\" class=\"size-medium wp-image-1193\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2012\/07\/pic3-300x208.png 300w, https:\/\/lukemiller.org\/wp-content\/uploads\/2012\/07\/pic3.png 681w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-1193\" class=\"wp-caption-text\">The csv files created in the 2nd directory are formatted for easy access to the important time and temperature information.<\/figcaption><\/figure><br \/>\n<\/br><br \/>\nYou&#8217;ll notice the weird jumbles of numbers peppered throughout the filenames and directory names. Hopefully you&#8217;ve been able to discern that these are simply the date (20120711) and time (1705) expressed in a compact format. The aim here is to help organize files if you&#8217;re accumulating data over many months, and will hopefully prevent you from overwriting previous data from one site with newly downloaded data from that same site. Each time you run the script, it will update with the current date and time and insert those into your newly created filenames. <\/p>\n<p>When you run the script, it will initially ask you for an ID to insert into the downloaded file name. This could be anything, such as &#8220;PointSur_High_Zone_A&#8221;, but avoid blank spaces in the name. Once you enter this ID, the script will immediately look for the iButton attached to your USB adapter, and download its data. After it&#8217;s finished, the temperature data will be flashed on the R console, and the script will sit and wait for you to enter the ID for the next iButton you want to download (go ahead and swap in the new iButton before you enter the next ID). Repeat this as much as necessary, and when you&#8217;re ready to quit, just type &#8220;q&#8221; instead of an ID. Hopefully you don&#8217;t have a site simply named &#8220;q&#8221;, because that will make your life difficult. <\/p>\n<div style=\"overflow:auto;\">\n<div class=\"geshifilter\">\n<pre class=\"r geshifilter-R\" style=\"font-family:monospace;\"><span style=\"color: #666666; font-style: italic;\"># Filename: ibutton_download_custom_names.R<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Author: Luke Miller Jul 11, 2012<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">###############################################################################<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># This script will allow the user to download iButtons, giving a unique name<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># to each file. It will parse the raw data file and extract the useful bits<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># (to me at least): date\/time, temperature (Celsius), user's ID for this<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># iButton, and the serial number for this iButton, and place all of these in a<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># comma-separated-value file that can be easily opened in Excel or R. Each<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># downloaded iButton data file has the current date and time included in the<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># filename to avoid overwriting older data files. Raw data files are stored in a<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># directory with today's date on it, while the parsed csv files go in a 2nd<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># directory.<\/span>\r\n&nbsp;\r\n<span style=\"color: #666666; font-style: italic;\"># The thermodl.exe file was originally downloaded as part of the Maxim iButton<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># 1-Wire Public Domain Kit.<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># There are several versions of the Kit available, including<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># versions with pre-compiled binaries (executables) for Windows\/Linux\/OSX.<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># http:\/\/www.maxim-ic.com\/products\/ibutton\/software\/1wire\/wirekit.cfm<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># On my Windows 7 x64 computer using the DS9490B USB ibutton adapter, I used the<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># precompiled binary build for Win64 USB (DS9490 + WinUSB) Preliminary Version<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># 3.11 Beta 2,<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># filename: winusb64vc311Beta2_r2.zip, downloaded 2012-03-15<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Unzip this file and find the .exe files thermoms.exe and thermodl.exe in the<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># builds\\winusb64vc\\release folder. Copy these to your R working directory.<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># The drivers for the DS9490 USB iButton adapter must also be downloaded and<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># installed:<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># http:\/\/www.maxim-ic.com\/products\/ibutton\/software\/tmex\/<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># I downloaded and installed the file &quot;install_1_wire_drivers_x64_v403.msi&quot;<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># The Java OneWireViewer app can also be downloaded and used to verify that your<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># drivers work and that you can communicate with iButtons successfully through<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># the USB adapter. You can download this app here:<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># http:\/\/www.maxim-ic.com\/products\/ibutton\/software\/1wire\/OneWireViewer.cfm<\/span>\r\n&nbsp;\r\n&nbsp;\r\n<span style=\"color: #666666; font-style: italic;\"># NOTE: the Maxim program &quot;thermodl.exe&quot; must be present in the current R<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># working directory. Uncomment the setwd() line below to change the R working<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># directory. Enter your working directory location inside the quotes.<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># setwd('D:\/R\/ibuttons')<\/span>\r\n&nbsp;\r\ncur.date = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/Sys.Date\"><span style=\"color: #003399; font-weight: bold;\">Sys.Date<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #666666; font-style: italic;\"># Get current date<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Assemble a directory name to store raw downloaded data into<\/span>\r\ndir.name = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">'.<span style=\"color: #000099; font-weight: bold;\">\\\\<\/span>'<\/span><span style=\"color: #339933;\">,<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.character\"><span style=\"color: #003399; font-weight: bold;\">as.character<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>cur.date<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">'_raw_downloads'<\/span><span style=\"color: #339933;\">,<\/span>sep = <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Assemble a directory name to store the parsed csv output files in<\/span>\r\ndir.name2 = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">'.<span style=\"color: #000099; font-weight: bold;\">\\\\<\/span>'<\/span><span style=\"color: #339933;\">,<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.character\"><span style=\"color: #003399; font-weight: bold;\">as.character<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>cur.date<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'_iButton_data'<\/span><span style=\"color: #339933;\">,<\/span> sep = <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Check to see if that folder already exists, if not then create it.<\/span>\r\n<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/is.na\"><span style=\"color: #003399; font-weight: bold;\">is.na<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/file.info\"><span style=\"color: #003399; font-weight: bold;\">file.info<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>dir.name<span style=\"color: #009900;\">&#41;<\/span><span style=\"\">$<\/span>isdir<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/dir.create\"><span style=\"color: #003399; font-weight: bold;\">dir.create<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>dir.name<span style=\"color: #009900;\">&#41;<\/span>\t\r\n<span style=\"color: #009900;\">&#125;<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Check to see if the other folder already exists, if not then create it.<\/span>\r\n<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/is.na\"><span style=\"color: #003399; font-weight: bold;\">is.na<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/file.info\"><span style=\"color: #003399; font-weight: bold;\">file.info<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>dir.name2<span style=\"color: #009900;\">&#41;<\/span><span style=\"\">$<\/span>isdir<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/dir.create\"><span style=\"color: #003399; font-weight: bold;\">dir.create<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>dir.name2<span style=\"color: #009900;\">&#41;<\/span>\t\r\n<span style=\"color: #009900;\">&#125;<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#######################################<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Enter main while loop to repeatedly download buttons until user decides to<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># quit.<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Have user enter their own ibutton ID to be included in the filename<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">'Enter starting number for file name: <span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\nfnameID = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/scan\"><span style=\"color: #003399; font-weight: bold;\">scan<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/file\"><span style=\"color: #003399; font-weight: bold;\">file<\/span><\/a> = <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #339933;\">,<\/span> what = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/character\"><span style=\"color: #003399; font-weight: bold;\">character<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">,<\/span> n = <span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\nloop = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span>\r\n<span style=\"color: #000000; font-weight: bold;\">while<\/span><span style=\"color: #009900;\">&#40;<\/span>loop<span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Get current time to insert in filename so we don't overwrite old data<\/span>\r\ncurrTime = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/strftime\"><span style=\"color: #003399; font-weight: bold;\">strftime<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/Sys.time\"><span style=\"color: #003399; font-weight: bold;\">Sys.time<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">,<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/format\"><span style=\"color: #003399; font-weight: bold;\">format<\/span><\/a> = <span style=\"color: #0000ff;\">&quot;%Y%m%d_%H%M&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Assemble filename<\/span>\r\nfname = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>dir.name<span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\\\<\/span>'<\/span><span style=\"color: #339933;\">,<\/span>fnameID<span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'_'<\/span><span style=\"color: #339933;\">,<\/span>currTime<span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'.dat'<\/span><span style=\"color: #339933;\">,<\/span> sep = <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Call the thermodl.exe program provided by Maxim. This must be in the<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># current R working directory. Start by assembling a command to be issued<\/span>\r\nsys.com = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">'thermodl.exe ds2490-0 '<\/span><span style=\"color: #339933;\">,<\/span> fname<span style=\"color: #339933;\">,<\/span> sep = <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Now issue the command to the system using the system() function<\/span>\r\ntemp = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/system\"><span style=\"color: #003399; font-weight: bold;\">system<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>sys.com<span style=\"color: #339933;\">,<\/span> wait = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span><span style=\"color: #339933;\">,<\/span> intern = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># The raw downloaded data should now be in the &quot;raw_downloads&quot; directory<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># created at the start of the script.<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Output the status updates from thermodl.exe<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>temp<span style=\"color: #009900;\">&#091;<\/span><span style=\"color: #cc66cc;\">7<\/span><span style=\"color: #009900;\">&#093;<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>temp<span style=\"color: #009900;\">&#091;<\/span><span style=\"color: #cc66cc;\">9<\/span><span style=\"color: #009900;\">&#093;<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>temp<span style=\"color: #009900;\">&#091;<\/span><span style=\"color: #cc66cc;\">18<\/span><span style=\"color: #009900;\">&#093;<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>temp<span style=\"color: #009900;\">&#091;<\/span><span style=\"color: #cc66cc;\">20<\/span><span style=\"color: #009900;\">&#093;<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Open the data file created by thermodl.exe<\/span>\r\nx = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/readLines\"><span style=\"color: #003399; font-weight: bold;\">readLines<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>fname<span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #666666; font-style: italic;\"># read data into a character vector<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Extract serial number from line 4 of file<\/span>\r\nserialnum = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/substr\"><span style=\"color: #003399; font-weight: bold;\">substr<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>x<span style=\"color: #009900;\">&#091;<\/span><span style=\"color: #cc66cc;\">4<\/span><span style=\"color: #009900;\">&#093;<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #cc66cc;\">26<\/span><span style=\"color: #339933;\">,<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/nchar\"><span style=\"color: #003399; font-weight: bold;\">nchar<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>x<span style=\"color: #009900;\">&#091;<\/span><span style=\"color: #cc66cc;\">4<\/span><span style=\"color: #009900;\">&#093;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n&nbsp;\r\n<span style=\"color: #666666; font-style: italic;\"># Parse the iButton data file to extract the relevant temp\/time data<\/span>\r\nlog.start = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/grep\"><span style=\"color: #003399; font-weight: bold;\">grep<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">'Log Data'<\/span><span style=\"color: #339933;\">,<\/span> x<span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #666666; font-style: italic;\"># Find what line the log data starts below<\/span>\r\nlog.start = log.start <span style=\"\">+<\/span> <span style=\"color: #cc66cc;\">3<\/span> <span style=\"color: #666666; font-style: italic;\"># change value to first line of log data<\/span>\r\nlog.end = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/grep\"><span style=\"color: #003399; font-weight: bold;\">grep<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">'Debug Dump'<\/span><span style=\"color: #339933;\">,<\/span> x<span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #666666; font-style: italic;\"># Find where the log data ends (roughly)<\/span>\r\nlog.end = log.end <span style=\"\">-<\/span> <span style=\"color: #cc66cc;\">2<\/span> <span style=\"color: #666666; font-style: italic;\"># change value to last line of log data<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Check if there were any logged temperature data<\/span>\r\n<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span><span style=\"\">!<\/span><span style=\"color: #009900;\">&#40;<\/span>log.end <span style=\"\">-<\/span> log.start <span style=\"\">&lt;<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\r\ntemps = x<span style=\"color: #009900;\">&#091;<\/span>log.start<span style=\"\">:<\/span>log.end<span style=\"color: #009900;\">&#093;<\/span> <span style=\"color: #666666; font-style: italic;\"># extract log data, still as characters<\/span>\r\n&nbsp;\r\n<span style=\"color: #666666; font-style: italic;\"># Convert time stamps to POSIX object<\/span>\r\ntimes = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.POSIXct\"><span style=\"color: #003399; font-weight: bold;\">as.POSIXct<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>strptime<span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/substr\"><span style=\"color: #003399; font-weight: bold;\">substr<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>temps<span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">17<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">,<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/format\"><span style=\"color: #003399; font-weight: bold;\">format<\/span><\/a> = <span style=\"color: #0000ff;\">'%m\/%d\/%Y %H:%M'<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\ntemp.data = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/data.frame\"><span style=\"color: #003399; font-weight: bold;\">data.frame<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/inside-r.org\/packages\/cran\/time\"><span style=\"\">Time<\/span><\/a> = times<span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #666666; font-style: italic;\"># stick into a new data frame<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># convert temps to numeric and insert into temp.data<\/span>\r\ntemp.data<span style=\"\">$<\/span>TempC = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.numeric\"><span style=\"color: #003399; font-weight: bold;\">as.numeric<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/substr\"><span style=\"color: #003399; font-weight: bold;\">substr<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>temps<span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">20<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">26<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Insert column with iButton's unique serial number<\/span>\r\ntemp.data<span style=\"\">$<\/span>Serial.number = serialnum\r\n<span style=\"color: #666666; font-style: italic;\"># Insert new column with ibutton ID from file name<\/span>\r\ntemp.data<span style=\"\">$<\/span>ID = fnameID\r\n<span style=\"color: #666666; font-style: italic;\"># Output temperature data to console<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">'Temperature summary data: <span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/sprintf\"><span style=\"color: #003399; font-weight: bold;\">sprintf<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">'%s'<\/span><span style=\"color: #339933;\">,<\/span> temp.data<span style=\"color: #009900;\">&#41;<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/print\"><span style=\"color: #003399; font-weight: bold;\">print<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>temp.data<span style=\"color: #009900;\">&#41;<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/utils\/flush.console\"><span style=\"color: #003399; font-weight: bold;\">flush.console<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Output temperature data to a comma-separated-value file for easy<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># reading in Excel or R.<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Start by assembling new filename, sticking output file in the 2nd<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># directory created at the start of the script.<\/span>\r\noutputfile = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>dir.name2<span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\\\<\/span>'<\/span><span style=\"color: #339933;\">,<\/span>fnameID<span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'_'<\/span><span style=\"color: #339933;\">,<\/span>currTime<span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'.csv'<\/span><span style=\"color: #339933;\">,<\/span> sep = <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Write temp.data to a comma-separated-value file<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/utils\/write.csv\"><span style=\"color: #003399; font-weight: bold;\">write.csv<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>temp.data<span style=\"color: #339933;\">,<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/file\"><span style=\"color: #003399; font-weight: bold;\">file<\/span><\/a> = outputfile<span style=\"color: #339933;\">,<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/quote\"><span style=\"color: #003399; font-weight: bold;\">quote<\/span><\/a> = <span style=\"color: #000000; font-weight: bold;\">FALSE<\/span><span style=\"color: #339933;\">,<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/row.names\"><span style=\"color: #003399; font-weight: bold;\">row.names<\/span><\/a> = <span style=\"color: #000000; font-weight: bold;\">FALSE<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<span style=\"color: #009900;\">&#125;<\/span> <span style=\"color: #000000; font-weight: bold;\">else<\/span> <span style=\"color: #009900;\">&#123;<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\n<\/span><span style=\"color: #000099; font-weight: bold;\">\\n<\/span>*****No temperature data*****<span style=\"color: #000099; font-weight: bold;\">\\a<\/span><span style=\"color: #000099; font-weight: bold;\">\\n<\/span><span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<span style=\"color: #009900;\">&#125;<\/span>\r\n&nbsp;\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>temp<span style=\"color: #009900;\">&#091;<\/span><span style=\"color: #cc66cc;\">18<\/span><span style=\"color: #009900;\">&#093;<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>temp<span style=\"color: #009900;\">&#091;<\/span><span style=\"color: #cc66cc;\">20<\/span><span style=\"color: #009900;\">&#093;<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\a<\/span><span style=\"color: #000099; font-weight: bold;\">\\n<\/span>---------------------<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">'Swap in next iButton and hit enter. Enter q to quit.<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Read next iButton ID from user's input<\/span>\r\nfnameID = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/scan\"><span style=\"color: #003399; font-weight: bold;\">scan<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/file\"><span style=\"color: #003399; font-weight: bold;\">file<\/span><\/a> = <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #339933;\">,<\/span> what =<a href=\"http:\/\/inside-r.org\/r-doc\/base\/character\"><span style=\"color: #003399; font-weight: bold;\">character<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">,<\/span> n = <span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/length\"><span style=\"color: #003399; font-weight: bold;\">length<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>fnameID<span style=\"color: #009900;\">&#41;<\/span> <span style=\"\">&gt;<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\r\n<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span>fnameID <span style=\"\">==<\/span> <span style=\"color: #0000ff;\">'q'<\/span><span style=\"color: #009900;\">&#41;<\/span> loop = <span style=\"color: #000000; font-weight: bold;\">FALSE<\/span> <span style=\"color: #666666; font-style: italic;\"># quit out of while loop<\/span>\r\n<span style=\"color: #009900;\">&#125;<\/span> <span style=\"color: #000000; font-weight: bold;\">else<\/span> loop = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span> <span style=\"color: #666666; font-style: italic;\"># return to start of while loop to download next iButton<\/span>\r\n&nbsp;\r\n<span style=\"color: #009900;\">&#125;<\/span>\r\n&nbsp;\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">'Finished<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">&#41;<\/span><\/pre>\n<\/div>\n<\/div>\n<p><a href=\"http:\/\/www.inside-r.org\/pretty-r\" title=\"Created by Pretty R at inside-R.org\">Created by Pretty R at inside-R.org<\/a><\/p>\n<p>The other version of this script, called ibutton_download_increment_names.R, also available in the github repository (<a href=\"http:\/\/github.com\/millerlp\/ibuttons\" target=\"_blank\">http:\/\/github.com\/millerlp\/ibuttons<\/a>) is for people that have assigned simple sequential numeric values to their iButtons. Maybe you have sites or iButtons that you simply refer to as 1,2,3,4,5 etc. You can use this alternate script to enter the number for the 1st ibutton (23 for example), and it will automatically increment the filename for each subsequent iButton (24, then 25, then 26 and so on) that you download. <\/p>\n<p>By the way, iButtons still aren&#8217;t waterproof, so you&#8217;d better do your own waterproofing if you&#8217;re going to be putting them anywhere with lots of moisture (in the ocean, buried in soil, in an animal, etc.). <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last time, I posted some R code to help quickly launch many iButton Thermochron temperature dataloggers with the same mission parameters. The R code makes use of a publicly-available command line utility released by the iButton&#8217;s manufacturer, Maxim.\u00a0 Of course, Maxim also has a command line utility for downloading the data from those iButtons that [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[218],"tags":[125,131,7,45,130],"class_list":["post-1190","post","type-post","status-publish","format-standard","hentry","category-r-project","tag-ibutton","tag-maxim","tag-r","tag-script","tag-thermochron"],"_links":{"self":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/1190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/comments?post=1190"}],"version-history":[{"count":12,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/1190\/revisions"}],"predecessor-version":[{"id":1206,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/1190\/revisions\/1206"}],"wp:attachment":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/media?parent=1190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/categories?post=1190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/tags?post=1190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}