<jupyter_start><jupyter_text>Introduction on Using the language R to Access Station Data In this notebook we will look at the station active in a set time frame in a set area. By the end of this tutorial we have a station map like the one below    &nbsp; Table of contents  1. Introduction 2. Set Up 3. Get Station data 4. Visualising Station data  &nbsp; 1. Introduction In this tutorial we will be learning how to use the language R to be able to access station data. R is a language made for statisticians by statisticians. It is an open source language which anyone can make a package for. This means that R has a lot of useful functions for all kinds of research. This tutorial assumes that you have basic knowledge of the language R, but basic knowledge of any programming language should be sufficient to follow this tutorial.We will be using the packages IRISSeismic (https://CRAN.R-project.org/package=IRISSeismic) and maps (https://CRAN.R-project.org/package=maps ). This tutorial will be using GeoNet's FDSN webservices. FDSN stands for Federation of Digital Seismograph Networks. Their specifications make sure there is a consistent way for people to access seismic data. IRIS(Incorporated Research Institutions for Seismology) also have a FDSN webservice which IRISSeismic was written for. As such some of the functions in IRISSeismic may not work on GeoNet's data. All data will be save into R as a data frame which is much like, what would be called, a dictionary in other languages, but with a few more restrictions to the data structure. To understand more about data frames go to https://www.rdocumentation.org/packages/base/versions/3.5.1/topics/data.frame  &nbsp; 2. Set Up  Install the IRISSeismic and maps packages<jupyter_code>install.packages("IRISSeismic")
install.packages("maps")<jupyter_output><empty_output><jupyter_text>If not using binder to run this code you will need run this ONCE in R to install `IRISSeismic` and `maps`<jupyter_code>#library(help = "IRISSeismic")<jupyter_output><empty_output><jupyter_text>Note this code might take some time to run but you only need to run this ONCE. IRISSemismic has many functions that may be useful. To see a list of them all with description run `library(help = "IRISSeismic")`  Import the library and set your URLs to the same as below<jupyter_code>library(IRISSeismic)
library(maps)
#Near real time
nrt <- new('IrisClient', site = 'https://service-nrt.geonet.org.nz')
#Archive
acr <- new('IrisClient', site = 'https://service.geonet.org.nz')<jupyter_output><empty_output><jupyter_text>GeoNet has two FDSN services For waveform dataan archive service holds verified data starting 7 days after collection, and a near real-time service holds data that could be incomplete from the last 8 daysFor the event and station data there is no distinction between these services   Set the start and end time of the data you want to select<jupyter_code>starttime <- as.POSIXct("2010-10-01",tz="GMT")
endtime <- as.POSIXct("2011-02-23",tz="GMT")<jupyter_output><empty_output><jupyter_text>Note that time must be in UTC (Universal Time Coordinated) which is 13 or 12 hours behind nzt (New Zealand Time) (depending on daylight savings time).Also Note that GMT or Greenwich mean time is the same time as UTC.    There are online converters if needed.    You can add to the time in number of seconds eg. `stattime + 60` would be the time 2016-11-13 00:01:00.000  Set longitude and latitude you want to look at<jupyter_code>lat <- -43.5321
lon <- 172.6362<jupyter_output><empty_output><jupyter_text>In this case we are looking at the Christchurch  &nbsp; 3. Get Station data Station data is an inventory of metadata from the stations The `getStation` function with return a R `data.frame` with columns `network` `station` `latitude` `longitude` `elevation` `sitename` `starttime` `endtime`. Where `startime` is the date of the stations installation and `endtime` is the date of that the station stopped running. Note if `endtime` is NA then the station is still running.<jupyter_code>station<- getStation(acr,"NZ", "*","*", "?H?",starttime ,endtime, latitude= lat, longitude= lon ,maxradius = 1)<jupyter_output><empty_output><jupyter_text>The parameters needed are service, network, station, location, channel, starttime, and endtime.     The `*` is a wildcard character that says that value can be anything    Optional parameters are `latitude` `longitude` `maxradius` and `minradius`.    Here `latitude` and `longitude` are the latitude and longitude of the event.  The channel code is in the fomat `"?H?"` This means we are looking for only weak motion sensor. See https://www.geonet.org.nz/data/supplementary/channels for more information.  &nbsp; 4. Visualising Station data<jupyter_code>xhigh <- max(station$longitude)+ .3
xlow <- min(station$longitude)- .3
yhigh <- max(station$latitude)+ .3
ylow <- min(station$latitude)- .3<jupyter_output><empty_output><jupyter_text>We can take the maximum longitude shown on the map to the maximum longitude of all the station and the same for minimum and latitude. So that the data is not on the edge of the map we can make sure all points are at least 0.3 from the edge of the map by modifying the maximum and minimum,longitude and latitude by 0.3.<jupyter_code>map("nz",xlim=c(xlow,xhigh),ylim=c(ylow,yhigh), fill=TRUE, col="lightgreen")
points(station$longitude, station$latitude, pch=17, cex=2, col='red')
labels <- paste(station$station)
text(station$longitude, station$latitude, labels=labels, cex=1, pos=4)
map.axes()<jupyter_output><empty_output><jupyter_text>`map` makes the base map using the longitude and latitude as found above.`pch` say what shape will be plotted` pch=17` say the shape will be triangle.If you right click on this plot in the notebook you can save it.For more about what you can do with maps go to http://www.geog.uoregon.edu/GeogR/ or run `library(help ="maps")`  Print station data<jupyter_code>station<jupyter_output><empty_output><jupyter_text>&nbsp;  4.3 Channel Data Channel data is a inventory of metadata from the stations with some extra data to the station data set. The `getChannel` function with return a R `data.frame` with columns `network` `station` `location` `channel` `latitude` `longitude` `elevation` `depth` `azimuth` `dip` `instrument` `scale` `scalefreq` `scaleunits` `samplerate` `starttime` `endtim` and `snclId`. Where `startime` and `endtime` are the same as for the station data.  Get Channel data<jupyter_code>channels<-getChannel(acr,"NZ", "*","*", "?H?",starttime ,endtime, latitude= lat, longitude= lon ,maxradius = 1)<jupyter_output><empty_output><jupyter_text>The parameters needed are service, network, station, location, channel, starttime, and endtime.     The `*` is a wildcard character that says that value can be anything.    Optional parameters are `latitude` `longitude` `maxradius` and `minradius`. To get the same stations as befor give `getChannel` the same parameters as `getStation`.  Print channel data<jupyter_code>channels<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0848372886173085, 'fraction_numerical': 0.011936904931078584, 'mean_word_length': 5.346257889990983, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 45, 'https://': 6, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '481895', 'license': 'Apache-2.0', 'n_tokens_mistral': 2143, 'n_tokens_neox': 1999, 'n_words': 907, 'path': 'Seismic_Data/R/Station_Data_using_FDSN_in_R.ipynb', 'repo_name': 'megankortink/data-tutorials', 'size': '7037'}