#########################################################################################
#                                                                   Sub-seasonal Skill Scores                                                                                   #
#########################################################################################

library(s2dverification)
outdir <- "/gpfs/projects/bsc32/bsc32842/RESILIENCE"

gpfs.path <- "/gpfs/projects/bsc32/bsc32842/RESILIENCE/2014010200/prlr_200705.nc"
moore.path <- "/scratch/Earth/ncortesi/prlr_$YEAR$$MONTH$.nc"
esnas.path <- "/esnas/exp/ecmwf/system4_m1/daily_mean/prlr_f6h/prlr_$YEAR$$MONTH$.nc"
chunk <- as.integer(commandArgs(TRUE)[1])
#chunk=1

# netcdf-3 (10 MB):
#lat <- read.table("/gpfs/projects/bsc32/bsc32842/RESILIENCE/latitudes.txt")[,1]
#lat=rev(lat) # to go in increasing order of latitudes instead of decreasing!
#time <- system.time(data.hindcast <- Load(var='sfcWind', exp = list(list(path="/gpfs/projects/bsc32/bsc32842/RESILIENCE/2014010200/sfcWind_2013010200.nc")), obs=NULL,sdates='20130102', nleadtime=1, output='lonlat', latmin=lat[chunk], latmax=lat[chunk+2],nprocs=1))

### netcdf-3 (1GB):
# [or netcdf-4 if change name in prlr_200706.nc]
lat=seq(-90,90,0.75)
# load 3 chunks (the minimum number possible) from gpfs::
#time <- system.time(data.hindcast <- Load(var='tp', exp = list(list(path="/gpfs/projects/bsc32/bsc32842/RESILIENCE/2014010200/prlr_200705.nc")), obs=NULL,sdates='20070501', nleadtime=157, output='lonlat', latmin=lat[chunk], latmax=lat[chunk+2],nprocs=1))
# load all chunks in gpfs:
#time <- system.time(data.hindcast <- Load(var='tp', exp = list(list(path=gpfs.path)), obs=NULL,sdates='20070501', nleadtime=157, output='lonlat',nprocs=1))
# load all chunks from moore's scratch to moore:
#time <- system.time(data.hindcast <- Load(var='tp', exp = list(list(path=moore.path)), obs=NULL, sdates='20070501', nleadtime=157, output='lonlat',nprocs=1))
# load all chunks from esnas to moore:
time <- system.time(data.hindcast <- Load(var='tp', exp = list(list(path=esnas.path)), obs=NULL, sdates='20070501', nleadtime=157, output='lonlat',nprocs=1))


# load 3 chunks from esnas:
#time <- system.time(data.hindcast <- Load(var='tp', exp = list(list(path="/esnas/exp/ecmwf/system4_m1/daily_mean/prlr_f6h/prlr_200706.nc")), obs=NULL,sdates='20070601', nleadtime=157, output='lonlat', latmin=lat[chunk], latmax=lat[chunk+2], nprocs=1))
# load all chunks from esnas:
#time <- system.time(data.hindcast <- Load(var='tp', exp = list(list(path="/esnas/exp/ecmwf/system4_m1/daily_mean/prlr_f6h/prlr_200705.nc")), obs=NULL,sdates='20070501', nleadtime=157, output='lonlat', nprocs=1))

# netcdf-3 (10 GB):
#time <- system.time(data.hindcast <- Load(var='psl', exp = list(list(path="/gpfs/projects/bsc32/bsc32842/RESILIENCE/2014010200/psl_19900501.nc")), obs=NULL,sdates='19900501', nleadtime=1, output='lonlat', latmin=lat[chunk], latmax=lat[chunk+2],nprocs=1))
#lat=seq(-90,90,0.75)

#.RData (1 GB): (must add +20% loading time because it is a file of 820 MB)
#time <- system.time(load(file="/gpfs/projects/bsc32/bsc32842/RESILIENCE/2014010200/test.RData"))
# in esnas:
#time <- system.time(load(file="/esnas/exp/ecmwf/system4_m1/daily_mean/psl_f6h/test.RData"))

# save chunks:
#save(time, file=paste0(outdir,'/test_chunk_',chunk,'_time_',round(time[3],2),'_',data.hindcast$nleadtime,'.RData'))
#save(time, file=paste0(outdir,'/test_chunk_',chunk,'_time_',round(time[3],2),'.RData'))

#a <- list.files("/esnas/exp/ecmwf/system4_m1/daily_mean/prlr_f6h")
#save(a, file=paste0(outdir,'/test_chunk_',chunk,'_file_',a[1],'.RData'))
#write.table(a, file=paste0(outdir,'/test_chunk_',chunk,'_file_',a[1],'.txt'))





