 library(akima)
 library(maps)
 library(mapproj)
 library('ncdf4')
 source('./function.R')


#prendre en compte les arguments
args<-commandArgs(TRUE)

#attribution des argument aux variables
inputfile<-args[1]
spec<-args[2]
level<-as.integer(args[3])
date<-args[4]


#Tests
if (length(args) != 4){
print("Il manque des arguments : ")
print(paste('inputfile ',args[1],sep=""))
print(paste('specie ',args[2],sep=""))
print(paste('level ',args[3],sep=""))
print(paste('date ',args[4],sep=""))
}

#Donnees pour le graphe
orient_cen=c(45,0)
angle=0

#Ouverture netcdf
in.file <- nc_open(inputfile)

#Variable
lat <- ncvar_get(in.file,"lat")
lon <- ncvar_get(in.file,"lon")
var <- ncvar_get(in.file,spec)
time <- ncvar_get(in.file,"Times")
#fermeture
nc_close(in.file)

#chercher la date
for(i in seq(from=1,to=dim(time),by=1)){
if(date==substr(time[i],1,13)){
itime<-i
break
}}

#pour le plot
legendlab<-paste(spec,'concentration',sep=" ")
outfile <- paste("map_chim_",date,".png", sep="")
titlelab<-paste(substr(time[itime],1,13),sep=" ")

#la function ne prend pas en compte les tableaux
xs<-numeric(dim(lon)[1]*dim(lon)[2])
ys<-numeric(dim(lon)[1]*dim(lon)[2])
zs<-numeric(dim(lon)[1]*dim(lon)[2])
indic<-0
for(izo in seq(from=1,to=dim(lon)[1],by=1)) {
for(ime in seq(from=1,to=dim(lon)[2],by=1)) {
indic<-indic+1
xs[indic]<-lon[izo,ime]
ys[indic]<-lat[izo,ime]
zs[indic]<-var[izo,ime,level,itime]
}}
data<-cbind(xs,ys,zs)

map.xyz.scale(data, projection="perspective", par=3, nside=100, res=100, orientation=c(orient_cen, angle), xlim=range(data[,1]), ylim=range(data[,2]),legend=legendlab,file_name=outfile,title=titlelab)
