Do You Use R? Here's a Tool for Importing Campbell Scientific Data

by Dirk Baker | Updated: 02/15/2017 | Comments: 2

Search the Blog


Subscribe to the Blog

Set up your preferences for receiving email notifications when new blog articles are posted that match your areas of interest.


Area / Application

Product Category

Activity

Corporate / News

Enter your email address:



Suggest an Article

Is there a topic you would like to learn more about? Let us know. Please be as specific as possible.

Leave this field empty

datalogger data to R

You may be familiar with R for data processing and analysis. But do you know how to easily import data that is in Campbell Scientific’s TOA5 format into R? In this article, I’ll briefly discuss R and TOA5, and then share a function to create an R dataframe from your TOA5.

R – Statistical Computing and Graphics

R is a powerful, open-source software environment for statistical computing and graphics. It is also popular for its ability to produce publication-quality plots with the necessary mathematical symbols and formulas using UNIX, Windows, and MacOS platforms.

Recommended for You: To learn more about R or download R, visit The R Project for Statistical Computing website.

TOA5 File Format

The TOA5 file format is the default format used for data collected from any contemporary Campbell Scientific dataloggers. This format is a simple comma-delimited text file that has a .dat extension.

The TOA5 file format includes a four-line ASCII header with information about the datalogger you’re using to collect your data. In addition, the header describes the data values with variable names and units of measurement—if they are available.

The four lines of the header are the following:

  • The first line contains information about the datalogger, such as the serial number and program name.
  • The second line is what most people would view as the data header as it contains the names of the variables stored in the table.
  • The third line consists of the units for the variables—if they have been defined in the datalogger.
  • In the fourth line, there is an abbreviation for the processing the datalogger performed (for example, sample, average, standard deviation, maximum, minimum, etc.).

Recommended for You: For more information about TOA5, refer to Appendix B of the LoggerNet instruction manual.

How to Import a TOA5 File into R

You can find the complete code for the import function at the end of this section. Simply copy the code into a file or the R command line.  A call to this R function has one required parameter—the file name, as well as one optional parameter—the Return Option (RetOpt):


importCSdata(“filename.dat”, “RetOpt”)

If you just want the data with a simple header, all you need is one line of code like this:


myData ‹- importCSdata(“CR1000_HourlyWeather.dat”)

The optional RetOpt parameter defaults to “data,” as in the line below:


myData ‹- importCSdata(“CR1000_HourlyWeather.dat”, “data”)

When the RetOpt (Return Option) parameter is omitted or is “data,” an R data frame is created with the second line of the raw data file used for the names. The TIMESTAMP is converted to what R recognizes as a date and time stamp so that this information can be used in graphing, time-series analysis, or the aggregation of data based on date or time.

Because there is a lot of good information in the header of the TOA5 file, you may also want to simply import the header as documentation for your process by using the syntax below:


myData.Info ‹- importCSdata(“CR1000_HourlyWeather.dat”, “info”)

The complete code for the import function:


importCSdata ‹- function(filename,RetOpt="data"){
	if(RetOpt=="info"){
		# bring in entire header of CSI TOA5 data file for metadata
		stn.info ‹- scan(file=filename,nlines=4,what=character(),sep="\r")
		return(stn.info)
	} else {
		# second line of header contains variable names
		header ‹- scan(file=filename,skip=1,nlines=1,what=character(),sep=",")
		# bring in data
		stn.data ‹- read.table(file=filename,skip=4,header=FALSE, na.strings=c("NAN"),sep=",")
		names(stn.data) ‹- header
		# add column of R-formatted date/timestamps
		stn.data$TIMESTAMP ‹- as.POSIXlt(strptime(stn.data$TIMESTAMP,"%Y-%m-%d %H:%M:%S"))
		return(stn.data)}
}

You can download this code.

Conclusion

I hope you find this function useful in your data processing and analyses. Feel free to post your comments about your experience, or your suggestions, below.

Happy analyzing!



Credits: The R logo is © 2016 The R Foundation. The logo was used without modification and with permission in accordance with the terms of the Creative Commons Attribution-ShareAlike 4.0 International license (CC-BY-SA 4.0).


Share This Article



About the Author

dirk baker Dr. Dirk V. Baker is an Application Research Scientist in the Environmental Group at Campbell Scientific, Inc. Areas of interest include ecology, agriculture, and meteorology—among others. He has a bachelor's degree in wildlife biology and a doctorate in weed science, both from Colorado State University. Dirk’s graduate and postdoctoral research centered around measuring and modeling wind-driven plant dispersal.

View all articles by this author.


Comments

J. Magnin | 02/15/2017 at 03:20 AM

Hello,

This article is quite useful, although your code has some mistakes: you use 2 variables names for the same object (stn.data / station.data & stn.info / station.info), so the function stops & returns an error.

By the way, the way you use scan & read.table gave me an idea for enhanced functions to read TOA5 data (and why not, later, other data formats ?). I would like to publish that work, as soon as it will be done, on the CRAN (maybe under GPL V3) to make it available for the R users community. For this, I would like to have your authorization for using your code as a basis, if you agree with that idea.

Thanks,

J.M.

Dirk | 02/15/2017 at 10:33 AM

Thank you for pointing that out, JM!  Looks like I introduced that typo in condensing the code to fit the page.  It's fixed now and a downloadable version added.

I like the idea of expanding and enhancing the function and, as far as I'm concerned, anyone is free to use and modify my code as needed.  I'm interested to learn more about your ideas and possibly collaborating.  You're welcome to contact me at dbaker@campbellsci.com.

Thanks!

Dirk

Please log in or register to comment.



We're now on Twitter!
Stay informed with our latest updates by following @CampbellSci