UAH temp data retrieval functions in R

# get the url for lower trop, mid, or strat file
uah_file_get = function (type= 1) {
uahurl = "http://vortex.nsstc.uah.edu/data/msu/t2lt/uahncdc_lt_5.6"
if(type == 1){
uahurl = "http://vortex.nsstc.uah.edu/data/msu/t2lt/uahncdc_lt_5.6"
}
else if(type ==2){
uahurl ="http://vortex.nsstc.uah.edu/data/msu/t2/uahncdc_mt_5.6"
}
else if(type == 3){
uahurl ="http://vortex.nsstc.uah.edu/data/msu/t4/uahncdc_ls_5.6"
}

return(uahurl)
}

# get the column names and file size
uah_get_header = function(uahurl){
uahtxt = readLines(con = uahurl)
fsize = length(uahtxt)
print(fsize)
uahheader = uahtxt[1]
print(uahheader)
uahcolnames = unlist(strsplit(uahheader," +" , perl = T)[[1]])
uahcolnames = uahcolnames[-1]

return(c(fsize, uahcolnames))
}

#retrieve the file reading it in fixed format
uah_temp_get = function (uahurl, field = "Globe",uahcolnames, fsize) {
cwidths = c(5,3,rep(6,27))
all.temps = read.fwf(file = uahurl, widths = cwidths, header = F, skip =1, strip.white = T, n = fsize-12, stringsAsFactors = F)
names(all.temps)= uahcolnames
# print(names(all.temps))
# print(field)
head(all.temps)
temps = all.temps[field]
fyear = as.numeric(all.temps[1,1])
fmon = as.numeric(all.temps[1,2])
temps = ts((temps), start = c(fyear,fmon), frequency = 12)
return (temps)
}

# short program to execute the functions
uahurl.lt = uah_file_get(1)
filedata = uah_get_header(uahurl.lt)
temp = uah_temp_get(uahurl.lt, "NoPol",filedata[2:length(filedata)], as.numeric(filedata[1]) )

Advertisements

Leave a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s