#' Conversion between Japanese Era and AD
#'
#' @param yearJ a vector of Japanese years
#'  with Japanese era name,  e.g.  M01, T15, S27, H1, R04
#' @param yearAD a vector of years in AD \cr
#' This has priority over yearJ.
#' @param EraName a Japanese Era Name to be used for the conversion from AD
#'
#' @details
#'  \code{ yearJAD( "R1" ) } returns 2019. \cr
#'  \code{ yearJAD( yearAD=2019 ) } returns "R01". \cr
#'  \code{ yearJAD( yearAD=2019, EraName="S" ) } returns "S94".
#'
#' @examples
#' # conversion table
#' yJ <- c("M1","M2","M44","M45","T1","T2","T14","T15","S1","S2","S63","S64"
#'      , "H01","H02","H30","H31","R1","R2","R4")
#' yAD <- yearJAD( yJ )
#' yJfromAD <- yearJAD( yearAD=yAD )
#' Print(yJ,yAD,yJfromAD)
#'
#' # using out-of-range Era Name
#' yearJAD(yearAD=2017, EraName="R")  # negative number
#' yearJAD(yearAD=2018, EraName="R")  # year 0 means a year before year 1.
#' yearJAD(yearAD=2019, EraName="R")
#'
#' yearJAD(yearAD=1952, EraName="T")
#' yearJAD(yearAD=1952, EraName="S")
#' yearJAD(yearAD=1952, EraName="H")  # negative number
#'
#' @export
#'

yearJAD <- function( yearJ=1952, yearAD=NULL, EraName="" ){
 # convert yearJ to yearAD
 # Shin-ichi Mayekawa
 # 20221220dnc
 #

 if( is.null( yearAD) ){

  nyear=length(yearJ)
  yearAD=numeric(nyear)

  for( i in 1:nyear){
   YearJ=yearJ[i]
   EraName=toupper(substr(YearJ,1,1))
   if( !( EraName %in% c("M","T","S","H","R") ) ){
    cat("\nerror1:(yearJAD) yearJ must start with a Roman character.\n")
    return(NULL)
   }
   YearJ=as.numeric(substring(YearJ,2))
   if( EraName == "M" ) base=1867
   else if( EraName == "T" ) base=1911
   else if( EraName == "S" ) base=1925
   else if( EraName == "H" ) base=1988
   else if( EraName == "R" ) base=2018
   yearAD[i]=base+YearJ
  }
  return(yearAD)

 }
 else if( EraName == "" ){

  nyear=length(yearAD)
  yearJ=numeric(nyear)
  for( i in 1:nyear ){
   YearAD=yearAD[i]
   if( YearAD > 2018 ){
    YearJ=paste("R", formatC(YearAD-2018,format="d",flag="0",width=2),sep="")
   }
   else if( YearAD > 1988 ){
    YearJ=paste("H", formatC(YearAD-1988,format="d",flag="0",width=2),sep="")
   }
   else if( YearAD > 1925 ){
    YearJ=paste("S", formatC(YearAD-1925,format="d",flag="0",width=2),sep="")
   }
   else if( YearAD > 1911 ){
    YearJ=paste("T", formatC(YearAD-1911,format="d",flag="0",width=2),sep="")
   }
   else if( YearAD > 1867 ){
    YearJ=paste("M", formatC(YearAD-1867,format="d",flag="0",width=2),sep="")
   }
   else{
    YearJ=""
   }
   yearJ[i]=YearJ
  }
  return( yearJ )

 }
 else{

  nyear=length(yearAD)
  yearJ=numeric(nyear)
  for( i in 1:nyear ){
   YearAD=yearAD[i]
   if( EraName =="R" ){
    YearJ=paste("R", formatC(YearAD-2018,format="d",flag="0",width=2),sep="")
   }
   else if( EraName =="H" ){
    YearJ=paste("H", formatC(YearAD-1988,format="d",flag="0",width=2),sep="")
   }
   else if( EraName =="S" ){
    YearJ=paste("S", formatC(YearAD-1925,format="d",flag="0",width=2),sep="")
   }
   else if( EraName =="T" ){
    YearJ=paste("T", formatC(YearAD-1911,format="d",flag="0",width=2),sep="")
   }
   else if( EraName =="M" ){
    YearJ=paste("M", formatC(YearAD-1867,format="d",flag="0",width=2),sep="")
   }
   else{
    YearJ=""
   }
   yearJ[i]=YearJ
  }
  return( yearJ )

 }

} # end of yearJAD


