ghdc/GO.R.backup
2024-10-23 16:30:58 +08:00

124 lines
4.6 KiB
Plaintext

#' ===============================================================
#' Author: Lele Shu <shulele@lzb.ac.cn>
#' Date: 2023.05.05
#' Function: The core function of the GHDC
#' ===============================================================
#'
rm(list=ls())
source('getReady.R')
debug=FALSE
debug=TRUE
CV=list()
# system('cp -r bak/* data/')
CV$serv = load.conf(file = file.path('script', paste0('service_', Sys.info()['nodename'], '.cfg.txt') ) )
CV$para = list(projname = '', Area = -999, distBuff = -999, NumCellMin = -999,
dataonly = FALSE, # prepare data only.
gcs = sp::CRS('+init=epsg:4326'), pcs='')
CV$figtype='cairo'
caller = 'GO.R'
dir.out <- file.path(CV$serv$DIR.WORKING, 'rProcessing')
dir.log <- file.path(CV$serv$DIR.WORKING, 'rlog')
tmp = lapply(list(dir.out, dir.log), dir.create, showWarnings = FALSE, recursive = TRUE)
# LOGFILE = file.path(dir.log, paste0(format(Sys.time(), '%Y%m%d%H%M%S' ), '.log'))
LOGFILE = file.path(dir.log, paste0(format(Sys.time(), '%Y%m%d' ), '.log'))
assign('LOGFILE',LOGFILE, envir = .GlobalEnv)
if(!file.exists(LOGFILE)){
write('', file=LOGFILE)
}
writelog(LOGFILE, caller = caller)
writelog("======== RUNING ==========", caller = caller)
args <- commandArgs(trailingOnly = TRUE)
if (length(args) < 1) {
writelog('Empty args. Nothing to do. ', caller = caller)
stop()
}
task.fn <- args[1]
writelog(paste('starting task: ', task.fn), caller = caller)
task.backup(task.fn, CV)
LOGFILE = file.path(dir.log, paste0(format(Sys.time(), '%Y%m%d' ), '.log'))
assign('LOGFILE',LOGFILE, envir = .GlobalEnv)
writelog(paste('===================================='), caller = caller)
theTask = gsub('/home/wwwroot/r', '.', task.fn)
writelog(paste('\t++++++START++++++:', theTask), caller = caller)
theTask = normalizePath(theTask)
CV=configure(CV, theTask)
# BACKUP the input.zip, input.json to UserData.
fn.input = list.files(path=theTask, pattern = glob2rx('input*'), full.names = TRUE, recursive = TRUE)
file.copy(from = fn.input, to = CV$dirs$userdata, overwrite = TRUE, recursive = TRUE)
##### 1. Task.Ready.....
writemessage(paste0('Trying to run task.ready(CV)'), caller = caller, CV$task.log)
iflag=try(task.ready(CV),TRUE)
if( class(iflag)=="try-error" ) {
writemessage(paste0('ERROR: something wrong in task.ready(CV). '), caller = caller, CV$task.log)
warning('Task stop at task.ready(CV).')
print(iflag)
} else {
CV = iflag
writemessage(paste0('Finish the task.ready(CV). '), caller = caller, CV$task.log)
message('Step of task.ready finished. \n\n')
}
###### 2. Extract ETV
writemessage(paste0('Trying to run ETV.Delineation(CV)'), caller = caller, CV$task.log)
iflag=try(ETV.Delineation(CV),TRUE)
if( class(iflag)=="try-error" ) {
writemessage(paste0('ERROR: something wrong in ETV.Delineation(CV). '), caller = caller, CV$task.log)
warning('Task stop at ETV.Delineation(CV).')
print(iflag)
} else {
writemessage(paste0('Finish the ETV.Delineation(CV). '), caller = caller, CV$task.log)
message('Step of ETV.Delineation finished. \n\n')}
writemessage(paste0('Trying to run ExtractETV(CV)'), caller = caller, CV$task.log)
iflag=try(ExtractETV(CV),TRUE)
if( class(iflag)=="try-error" ) {
writemessage(paste0('ERROR: something wrong in ExtractETV(CV). '), caller = caller, CV$task.log)
warning('Task stop at ExtractETV(CV).')
print(iflag)
} else {
writemessage(paste0('Finish the ExtractETV(CV). '), caller = caller, CV$task.log)
message('Step of ExtractETV finished. \n\n')}
###### 3. Model Deployment
iflag=try(model.Deploy(CV),TRUE)
writemessage(paste0('Trying to run model.Deploy(CV)'), caller = caller, CV$task.log)
if( class(iflag)=="try-error" ) {
writemessage(paste0('ERROR: something wrong in model.Deploy(CV). '), caller = caller, CV$task.log)
warning('Task stop at model.Deploy(CV).')
print(iflag)
} else {
writemessage(paste0('Finish the model.Deploy(CV). '), caller = caller, CV$task.log)
message('Step of model.Deploy finished. \n\n') }
###### 4. Task done. Data packaging. update info.json/citys.js =====
iflag=try(task.done(CV),TRUE)
writemessage(paste0('Trying to run task.done(CV)'), caller = caller, CV$task.log)
if( class(iflag)=="try-error" ) {
writemessage(paste0('ERROR: something wrong in task.done(CV). '), caller = caller, CV$task.log)
warning('Task stop at task.done(CV)')
# next
} else {
writemessage(paste0('Finish the task.done(CV). '), caller = caller, CV$task.log)
message('Step of task.done finished. \n\n')
}
writemessage(paste0('======== END ========'), caller = caller, CV$task.log)
writelog(paste('\n'), caller = caller)