I am trying to scrape data from the IGRF Magnetic Model Calculator (https://www.ngdc.noaa.gov/geomag/calculators/magcalc.shtml#igrfgrid) into a data frame that I already have that contains dates and lat/long. I only need two values from the calculator: total intensity and inclination.
My data frame looks like this (2012-2022; 24,000 observations):
| UID | Animal ID | YEAR | Latitude | Longitude | Intensity | Inclination |
|---|---|---|---|---|---|---|
| 1 | C678 | 2012 | 30.98763 | -80.98457 | [add] | [add] |
| 2 | C679 | 2013 | 29.89765 | -82.38304 | [add] | [add] |
Right now, I have my code running in a For loop to append these values to a list, but I am stuck on two main problems:
I would like this data to add to a new column to my data frame called “Intensity and “Inclination” so that is paired with my other observational data rather than in an empty list.
I am running into time out issues. The data frame I have has 20 columns and 24,000 rows, but my for loop stops running around entry 2,500. Is there a more efficient way to run this computation? (w/o a for-loop)
My code currently looks like this:
DF <- DF[which(!is.na(DF$Latitude) & !is.na(DF$Longitude)),]
Inclination<-list()
Intensity<-list()
base_url = "https://www.ngdc.noaa.gov/geomag-web/calculators/calculateIgrfwmm?"
for (i in 1:length(DF$UID)) {
full_url= paste0(base_url, "lat1=", DF$Latitude[i],
"&lon1=",DF$Longitude[i], "&model=IGRF", "&startYear=", DF$Year[i], "&endYear=", DF$Year[i], "&key=EAU2y&resultFormat=xml")
dat <- read_xml(full_url)
xml_structure(dat) #how the xml is structured
dates <- as.numeric(xml_text(xml_find_all(dat, xpath = "//date")))
longs <- as.numeric(xml_text(xml_find_all(dat, xpath = "//longitude")))
lats <- as.numeric(xml_text(xml_find_all(dat, xpath = "//latitude")))
intensity_val <- as.numeric(xml_text(xml_find_all(dat, xpath = "//totalintensity")))
inclination_val <- as.numeric(xml_text(xml_find_all(dat, xpath = "//inclination")))
Intensity<-append(Intensity,intensity_val)
Inclination<-append(Inclination,inclination_val)
}