#######################
## LEAF MARGIN CODER ##
##    Brody 6/7/08   ##
#######################

#read in the table with margin description->code information

margin_codes = read.csv("coded_leaf_margins.csv",as.is=TRUE)

#the formatting is a bit off. the following cleans it up
parsed_text = strsplit(margin_codes[,1],split = ",")

m_text= NULL
for (i in 1:length(parsed_text))
	{
##discard the first and last info from each name (row number and frequency)
	begin = 2
	end = length(parsed_text[[i]])-1	
	temp = parsed_text[[i]][begin:end]
	
##paste together the description
	temp2 = NULL
	for (j in 1:length(temp))
		{
		temp2 = paste(temp2,temp[j],sep=",")
		}


#gets rid of the leading comma
	temp2 = substr(temp2,2,nchar(temp2))
	m_text[i] = temp2
	}
m_code = margin_codes[,2]

text_code = data.frame()
text_code = cbind(m_text,m_code)

####################################################
#read in some data to apply margin codes to
#right now, I'm just using my data, because I couldn't 
#find the group data
####################################################
data = read.csv("SANDEL_06APR08.csv",as.is=TRUE)


######################################
#Here's where the matching happens!
######################################
MarginCode=NULL
for (i in 1:nrow(data))
	{
	MarginCode[i] = text_code[which(text_code[,1] == data$LeafMargin[i]),2]
	}
###########################################################################
##This produces some warning messages, because "entire" is in the table
##three times.  MarginCode still comes out right, though, because all of those
##"entire"s are coded as 0.  This could be easily fixed by deleting those
##rows in excel, or by removing duplicate rows in R
############################################################################

data = cbind(data,MarginCode)





