# Script used to produce MERRAclim
# The steps described follow the Methods Section from the Data Descriptor: "MERRAclim, A high-resolution global dataset of remotely sensed bioclimatic variables for ecological modelling"
# by Greta C. Vega, Luis R. Pertierra, Miguel Ángel Olalla-Tárraga




################################################################################
################################################################################
#Step 1: DATA DOWNLOAD
## We downloaded the variables T2M (Temperature at 2m in Kelvin degrees) and QV2M (Specific Humidity at 2m in kg of water/kg of air) from the dataset MAT1NXSLV from the Global Modeling and Assimilation Office available at: https://disc.sci.gsfc.nasa.gov/mdisc/
## Download files have the following name: 
## MERRA300.prod.assim.tavg1_2d_slv_Nx.20100101.hdf.nc 
## This file correspond to the 1st of January of 2010 and contains 24 values, one for each hour of the day


################################################################################
################################################################################
#Step 2: OPENING AND EXTRACTION OF THE MAXIMUM AND MINIMUM VALUES (in R)

#Set working directory where the download files are stored
setwd("C:\\MERRA\\download")

#Load packages
library(RNetCDF)
library(raster)
library(rgdal)

years <- c("1981", "1982", "1983","1984","1985", "1986", "1987", "1988","1989","1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010")

months <- c("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12")
hours <- seq(1:24)
print (paste(Sys.time(),"start"))
#one year
for (y in 1:length(years))
{
	for (m in 1:length(months))
	{
		s<-stack()
		#one month
		pat<-paste("assim.tavg1_2d_slv_Nx.",years[y],months[m], sep="")
		#print(pat) #debug
		listfiles <- list.files(pattern = pat)
		#listfiles is the list of the days in the month m
		

		for( f in 1:length(listfiles))
		{
		#Open data file 

			fname<-listfiles[f] 
			# f is one file that represents one day
			fid<-open.nc(fname)

			# #Read data

			dat<-read.nc(fid)

			#Close file
			
			close.nc(fid)
			
			#this loop extracts all the hours of the day
			 for (h in 1: length(hours))
			 {
				 
				 mat<-dat$T2M[,,h]  #here we chose an hour of the day
				 r<-flip(raster(t(mat)), direction = 2)
				 s<-addLayer(s,r)
				 
			 }
			#at the end of this loop s is a raster stack of the 24 hours of day f
		}
		#at the end of this loop s is a raster stack of the 24 hours of all the days of month m
		#the min and max can be calculated.
		 minM <-min(s)
		 maxM <-max(s)
		 
		 extent(minM)<-c(-180, 180, -90, 90)
		 extent(maxM)<-c(-180, 180, -90, 90)
		 
		 projection(minM)<-"+proj=longlat +datum=WGS84"
		 projection(maxM)<-"+proj=longlat +datum=WGS84"
		 
		 minfname<-paste("min",years[y],months[m], ".tif", sep="")
		 maxfname<-paste("max",years[y],months[m], ".tif", sep="")
		 
		 
		 writeRaster(minM, minfname,format="GTiff")
		 writeRaster(maxM, maxfname,format="GTiff")
		 
		# For each month of each year two rasters are written one with the minimum and another one with the maximum
		
		 removeTmpFiles(h=0)
		 
	}
}
print (paste(Sys.time(),"Step 2 done"))

################################################################################
################################################################################
#Step 3: COMPUTING THE BIOCLIMATIC VARIABLES FOR EACH VERSION OF HUMIDITY (in R)

#Load packages
library(dismo)
library(raster)

## (a) The monthly mean of the humidity (QV2M) has to be computed. It is computed as (max+min)/2, i.e. the mean of max and min. 
#Set working directory where the humidity max and mean of each month computed in Step 2 are stored
setwd("C:\\MERRA\\QV2M_max_min") 

years <- c("1981", "1982", "1983","1984","1985", "1986", "1987", "1988","1989","1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010")
months <- c("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12")


for (y in 1:length(years))
{
	for (m in 1:length(months))
	{
		s<-stack()
		
		pat<-paste(years[y],months[m], sep="")
		
		listfiles <- list.files(pattern = pat)
		s<-addLayer(s,raster(listfiles[1]),raster(listfiles[2]))
		
		res<-mean(s)
		QV2M_mean<-paste("QV2M_mean",years[y],months[m], ".tif", sep="")
		writeRaster(res, QV2M_mean,format="GTiff")
		
	}
}

## (b) Once the three versions of Humidity are computed the bioclimatic variables are computed for the three versions
#Set working directory to where the min and max of temperature and the min, mean and max of humidity are stored
setwd("C:\\MERRA\\QV2M_T2M_max_mean_min") 

###This is using the Humidity (QV2M) max

for (y in 1:length(years))
{
	listyear<-list.files(pattern = years[y])
	
	
	#max Temperature
	 janmax<-raster(listyear[37])
	 febmax<-raster(listyear[38])
	 marmax<-raster(listyear[39])
	 aprmax<-raster(listyear[40])
	 maymax<-raster(listyear[41])
	 junmax<-raster(listyear[42])
	 julmax<-raster(listyear[43])
	 augmax<-raster(listyear[44])
	 sepmax<-raster(listyear[45])
	 octmax<-raster(listyear[46])
	 novmax<-raster(listyear[47])
	 decmax<-raster(listyear[48])


	maxTemp<-stack(janmax,febmax,marmax,aprmax,maymax,junmax,julmax,augmax,sepmax,octmax,novmax,decmax)

	#min Temperature
	 janmin<-raster(listyear[49])
	 febmin<-raster(listyear[50])
	 marmin<-raster(listyear[51])
	 aprmin<-raster(listyear[52])
	 maymin<-raster(listyear[53])
	 junmin<-raster(listyear[54])
	 julmin<-raster(listyear[55])
	 augmin<-raster(listyear[56])
	 sepmin<-raster(listyear[57])
	 octmin<-raster(listyear[58])
	 novmin<-raster(listyear[59])
	 decmin<-raster(listyear[60])


	minTemp<-stack(janmin,febmin,marmin,aprmin,maymin,junmin,julmin,augmin,sepmin,octmin,novmin,decmin)
	
	#Humidity
	 janprec<-raster(listyear[1])
	 febprec<-raster(listyear[2])
	 marprec<-raster(listyear[3])
	 aprprec<-raster(listyear[4])
	 mayprec<-raster(listyear[5])
	 junprec<-raster(listyear[6])
	 julprec<-raster(listyear[7])
	 augprec<-raster(listyear[8])
	 sepprec<-raster(listyear[9])
	 octprec<-raster(listyear[10])
	 novprec<-raster(listyear[11])
	 decprec<-raster(listyear[12])
	
	 prec<-stack(janprec,febprec,marprec,aprprec,mayprec,junprec,julprec,augprec,sepprec,octprec,novprec,decprec)


	 bios<-biovars(prec,minTemp,maxTemp)
	 outname<-paste("C:\\MERRA\\BIOvariables\\dismoMERRA_T2M_QV2M\\withMaxQV2M\\",years[y],"maxQV2M_bios.tif",sep="")
	 writeRaster(bios,filename=outname,bylayer=TRUE,suffix="names")
}


###This is using the QV2M mean

for (y in 1:length(years))
{
	listyear<-list.files(pattern = years[y])
	
	
	#max Temperature
	 janmax<-raster(listyear[37])
	 febmax<-raster(listyear[38])
	 marmax<-raster(listyear[39])
	 aprmax<-raster(listyear[40])
	 maymax<-raster(listyear[41])
	 junmax<-raster(listyear[42])
	 julmax<-raster(listyear[43])
	 augmax<-raster(listyear[44])
	 sepmax<-raster(listyear[45])
	 octmax<-raster(listyear[46])
	 novmax<-raster(listyear[47])
	 decmax<-raster(listyear[48])


	maxTemp<-stack(janmax,febmax,marmax,aprmax,maymax,junmax,julmax,augmax,sepmax,octmax,novmax,decmax)

	#min temperature
	 janmin<-raster(listyear[49])
	 febmin<-raster(listyear[50])
	 marmin<-raster(listyear[51])
	 aprmin<-raster(listyear[52])
	 maymin<-raster(listyear[53])
	 junmin<-raster(listyear[54])
	 julmin<-raster(listyear[55])
	 augmin<-raster(listyear[56])
	 sepmin<-raster(listyear[57])
	 octmin<-raster(listyear[58])
	 novmin<-raster(listyear[59])
	 decmin<-raster(listyear[60])


	minTemp<-stack(janmin,febmin,marmin,aprmin,maymin,junmin,julmin,augmin,sepmin,octmin,novmin,decmin)
	
	#Humidity
	 janprec<-raster(listyear[13])
	 febprec<-raster(listyear[14])
	 marprec<-raster(listyear[15])
	 aprprec<-raster(listyear[16])
	 mayprec<-raster(listyear[17])
	 junprec<-raster(listyear[18])
	 julprec<-raster(listyear[19])
	 augprec<-raster(listyear[20])
	 sepprec<-raster(listyear[21])
	 octprec<-raster(listyear[22])
	 novprec<-raster(listyear[23])
	 decprec<-raster(listyear[24])
	
	 prec<-stack(janprec,febprec,marprec,aprprec,mayprec,junprec,julprec,augprec,sepprec,octprec,novprec,decprec)


	 bios<-biovars(prec,minTemp,maxTemp)

	 outname<-paste("C:\\MERRA\\BIOvariables\\dismoMERRA_T2M_QV2M\\withMeanQV2M\\",years[y],"meanQV2M_bios.tif",sep="")
	 writeRaster(bios,filename=outname,bylayer=TRUE,suffix="names")
	 
}

###This is using the QV2M min

for (y in 1:length(years))
{
	listyear<-list.files(pattern = years[y])
	
	
	#max Temperature
	 janmax<-raster(listyear[37])
	 febmax<-raster(listyear[38])
	 marmax<-raster(listyear[39])
	 aprmax<-raster(listyear[40])
	 maymax<-raster(listyear[41])
	 junmax<-raster(listyear[42])
	 julmax<-raster(listyear[43])
	 augmax<-raster(listyear[44])
	 sepmax<-raster(listyear[45])
	 octmax<-raster(listyear[46])
	 novmax<-raster(listyear[47])
	 decmax<-raster(listyear[48])


	maxTemp<-stack(janmax,febmax,marmax,aprmax,maymax,junmax,julmax,augmax,sepmax,octmax,novmax,decmax)

	#min temperature
	 janmin<-raster(listyear[49])
	 febmin<-raster(listyear[50])
	 marmin<-raster(listyear[51])
	 aprmin<-raster(listyear[52])
	 maymin<-raster(listyear[53])
	 junmin<-raster(listyear[54])
	 julmin<-raster(listyear[55])
	 augmin<-raster(listyear[56])
	 sepmin<-raster(listyear[57])
	 octmin<-raster(listyear[58])
	 novmin<-raster(listyear[59])
	 decmin<-raster(listyear[60])


	minTemp<-stack(janmin,febmin,marmin,aprmin,maymin,junmin,julmin,augmin,sepmin,octmin,novmin,decmin)

	#Humidity
	 janprec<-raster(listyear[25])
	 febprec<-raster(listyear[26])
	 marprec<-raster(listyear[27])
	 aprprec<-raster(listyear[28])
	 mayprec<-raster(listyear[29])
	 junprec<-raster(listyear[30])
	 julprec<-raster(listyear[31])
	 augprec<-raster(listyear[32])
	 sepprec<-raster(listyear[33])
	 octprec<-raster(listyear[34])
	 novprec<-raster(listyear[35])
	 decprec<-raster(listyear[36])
	
	 prec<-stack(janprec,febprec,marprec,aprprec,mayprec,junprec,julprec,augprec,sepprec,octprec,novprec,decprec)


	 bios<-biovars(prec,minTemp,maxTemp)

	 outname<-paste("C:\\MERRA\\BIOvariables\\dismoMERRA_T2M_QV2M\\withMinQV2M\\",years[y],"minQV2M_bios.tif",sep="")
	 writeRaster(bios,filename=outname,bylayer=TRUE,suffix="names")
	 
	 
}

################################################################################
################################################################################
#STEP 4: MERGING BIOS BY DECADES AND VERSION 
##For each version (each in one folder) the mean of the bios is calculated by decade

#Load packages
library(rgdal)
library(raster)

#set the working directory in the max folder
setwd("C:\\MERRA\\BIOvariables\\dismoMERRA_T2M_QV2M\\withMaxQV2M")

bioclim<-c("bio1.tif","bio2.tif","bio3.tif","bio4.tif","bio5.tif","bio6.tif","bio7.tif","bio8.tif","bio9.tif","bio10.tif","bio11.tif","bio12.tif","bio13.tif","bio14.tif","bio15.tif","bio16.tif","bio17.tif","bio18.tif","bio19.tif")

for (bio in 1:length(bioclim)) #loop is run per bioclimatic variable
{
        
		listfiles <- list.files(pattern = bioclim[bio])
		
		s<-stack(listfiles [1:10]) 
		res<-mean(s)
		outname<-paste("max_80s_bio",bio, ".tif", sep="")
		writeRaster(res, outname,format="GTiff")
		
		s<-stack(listfiles [11:20]) 
		res<-mean(s)
		outname<-paste("max_90s_bio",bio, ".tif", sep="")
		writeRaster(res, outname,format="GTiff")
		
		s<-stack(listfiles [21:30]) 
		res<-mean(s)
		outname<-paste("max_00s_bio",bio, ".tif", sep="")
		writeRaster(res, outname,format="GTiff")
}


#set the working directory in the mean folder
setwd("C:\\MERRA\\BIOvariables\\dismoMERRA_T2M_QV2M\\withMeanQV2M")

bioclim<-c("bio1.tif","bio2.tif","bio3.tif","bio4.tif","bio5.tif","bio6.tif","bio7.tif","bio8.tif","bio9.tif","bio10.tif","bio11.tif","bio12.tif","bio13.tif","bio14.tif","bio15.tif","bio16.tif","bio17.tif","bio18.tif","bio19.tif")
for (bio in 1:length(bioclim)) #loop is run per bioclimatic variable
{
        
		listfiles <- list.files(pattern = bioclim[bio])
		
		s<-stack(listfiles[1:10]) 
		res<-mean(s)
		outname<-paste("mean_80s_bio",bio, ".tif", sep="")
		writeRaster(res, outname,format="GTiff")
		
		s<-stack(listfiles[11:20]) 
		res<-mean(s)
		outname<-paste("mean_90s_bio",bio, ".tif", sep="")
		writeRaster(res, outname,format="GTiff")
		
		s<-stack(listfiles[21:30]) 
		res<-mean(s)
		outname<-paste("mean_00s_bio",bio, ".tif", sep="")
		writeRaster(res, outname,format="GTiff")
}

#set the working directory in the mean folder
setwd("C:\\MERRA\\BIOvariables\\dismoMERRA_T2M_QV2M\\withMinQV2M")

bioclim<-c("bio1.tif","bio2.tif","bio3.tif","bio4.tif","bio5.tif","bio6.tif","bio7.tif","bio8.tif","bio9.tif","bio10.tif","bio11.tif","bio12.tif","bio13.tif","bio14.tif","bio15.tif","bio16.tif","bio17.tif","bio18.tif","bio19.tif")
for (bio in 1:length(bioclim)) #loop is run per bioclimatic variable
{
        
		listfiles <- list.files(pattern = bioclim[bio])
		
		s<-stack(listfiles[1:10]) 
		res<-mean(s)
		outname<-paste("min_80s_bio",bio, ".tif", sep="")
		writeRaster(res, outname,format="GTiff")
		
		s<-stack(listfiles[11:20]) 
		res<-mean(s)
		outname<-paste("min_90s_bio",bio, ".tif", sep="")
		writeRaster(res, outname,format="GTiff")
		
		s<-stack(listfiles[21:30]) 
		res<-mean(s)
		outname<-paste("min_00s_bio",bio, ".tif", sep="")
		writeRaster(res, outname,format="GTiff")
}


## Convert the bioclim raster into points

# Set the working directory for the max folder 80s
setwd("C:\\MERRA\\BIOvariables\\dismoMERRA_T2M_QV2M\\withMaxQV2M\\80s")
listfiles <- list.files(pattern="max_80s_bio")
listfiles #hay que comprobar que el nombre de las bio aparece en orden
for (l in 1:length(listfiles))
{
	r<-raster(listfiles[l])
	#nameRaster<-names(r)[1]
	#outLay<-paste("points_",nameRaster, seq="")
	p<-rasterToPoints(r,spatial=T)
	writeOGR(p, dsn = '.', layer = names(r), driver = "ESRI Shapefile")

}

#for the max folder 90s
setwd("C:\\MERRA\\BIOvariables\\dismoMERRA_T2M_QV2M\\withMaxQV2M\\90s")
listfiles <- list.files(pattern="max_90s_bio")
listfiles #hay que comprobar que el nombre de las bio aparece en orden
for (l in 1:length(listfiles))
{
	r<-raster(listfiles[l])
	#nameRaster<-names(r)[1]
	#outLay<-paste("points_",nameRaster, seq="")
	p<-rasterToPoints(r,spatial=T)
	writeOGR(p, dsn = '.', layer = names(r), driver = "ESRI Shapefile")

}

#for the max folder 00s
setwd("C:\\MERRA\\BIOvariables\\dismoMERRA_T2M_QV2M\\withMaxQV2M\\00s")
listfiles <- list.files(pattern="max_00s_bio")
listfiles #hay que comprobar que el nombre de las bio aparece en orden
for (l in 1:length(listfiles))
{
	r<-raster(listfiles[l])
	#nameRaster<-names(r)[1]
	#outLay<-paste("points_",nameRaster, seq="")
	p<-rasterToPoints(r,spatial=T)
	writeOGR(p, dsn = '.', layer = names(r), driver = "ESRI Shapefile")

}

#for the mean folder 80s
setwd("C:\\MERRA\\BIOvariables\\dismoMERRA_T2M_QV2M\\withMeanQV2M\\80s")
listfiles <- list.files(pattern="mean_80s_bio")
listfiles #It is important to check that the Bio names are in order
for (l in 1:length(listfiles))
{
	r<-raster(listfiles[l])
	#nameRaster<-names(r)[1]
	#outLay<-paste("points_",nameRaster, seq="")
	p<-rasterToPoints(r,spatial=T)
	writeOGR(p, dsn = '.', layer = names(r), driver = "ESRI Shapefile")

}

#for the mean folder 90s
setwd("C:\\MERRA\\BIOvariables\\dismoMERRA_T2M_QV2M\\withMeanQV2M\\90s")
listfiles <- list.files(pattern="mean_90s_bio")
listfiles #It is important to check that the Bio names are in order
for (l in 1:length(listfiles))
{
	r<-raster(listfiles[l])
	#nameRaster<-names(r)[1]
	#outLay<-paste("points_",nameRaster, seq="")
	p<-rasterToPoints(r,spatial=T)
	writeOGR(p, dsn = '.', layer = names(r), driver = "ESRI Shapefile")

}

#for the mean folder 00s
setwd("C:\\MERRA\\BIOvariables\\dismoMERRA_T2M_QV2M\\withMeanQV2M\\00s")
listfiles <- list.files(pattern="mean_00s_bio")
listfiles #It is important to check that the Bio names are in order
for (l in 1:length(listfiles))
{
	r<-raster(listfiles[l])
	#nameRaster<-names(r)[1]
	#outLay<-paste("points_",nameRaster, seq="")
	p<-rasterToPoints(r,spatial=T)
	writeOGR(p, dsn = '.', layer = names(r), driver = "ESRI Shapefile")

}

#for the min folder  80s
setwd("C:\\MERRA\\BIOvariables\\dismoMERRA_T2M_QV2M\\withMinQV2M\\80s")
listfiles <- list.files(pattern="min_80s_bio")
listfiles #It is important to check that the Bio names are in order
for (l in 1:length(listfiles))
{
	r<-raster(listfiles[l])
	#nameRaster<-names(r)[1]
	#outLay<-paste("points_",nameRaster, seq="")
	p<-rasterToPoints(r,spatial=T)
	writeOGR(p, dsn = '.', layer = names(r), driver = "ESRI Shapefile")

}

#for the min folder  90s
setwd("C:\\MERRA\\BIOvariables\\dismoMERRA_T2M_QV2M\\withMinQV2M\\90s")
listfiles <- list.files(pattern="min_90s_bio")
listfiles #It is important to check that the Bio names are in order
for (l in 1:length(listfiles))
{
	r<-raster(listfiles[l])
	#nameRaster<-names(r)[1]
	#outLay<-paste("points_",nameRaster, seq="")
	p<-rasterToPoints(r,spatial=T)
	writeOGR(p, dsn = '.', layer = names(r), driver = "ESRI Shapefile")

}

#for the min folder  00s
setwd("C:\\MERRA\\BIOvariables\\dismoMERRA_T2M_QV2M\\withMinQV2M\\00s")
listfiles <- list.files(pattern="min_00s_bio")
listfiles #It is important to check that the Bio names are in order
for (l in 1:length(listfiles))
{
	r<-raster(listfiles[l])
	#nameRaster<-names(r)[1]
	#outLay<-paste("points_",nameRaster, seq="")
	p<-rasterToPoints(r,spatial=T)
	writeOGR(p, dsn = '.', layer = names(r), driver = "ESRI Shapefile")

}

################################################################################
################################################################################
#Step 5: INTERPOLATION (in Python with arcpy; code only shown for Mean version)

# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *

## Set Workspace to Mean version 80s decade
env.workspace = "C:/MERRA/BIOvariables/dismoMERRA_T2M_QV2M/withMeanQV2M/80s"
pointFiles = arcpy.ListFiles("*.shp")

zFields = ["min_bio1","min_bio10", "min_bio11", "min_bio12", "min_bio13", "min_bio14", "min_bio15", "min_bio16", "min_bio17", "min_bio18", "min_bio19", "min_bio2", "min_bio3", "min_bio4", "min_bio5", "min_bio6", "min_bio7", "min_bio8", "min_bio9"]

resolution = "10m_"

zipped = zip (pointFiles,zFields)



for file in pointFiles:	
	field_list = arcpy.ListFields(file)
	nfield = field_list[2].name
	# Set local variables
	inPntFeat = file
	zField = nfield #zf
	cellSize = 0.16662
	splineType = "REGULARIZED"
	weight = 0.1

	# Check out the ArcGIS Spatial Analyst extension license
	arcpy.CheckOutExtension("Spatial")

	# Execute Spline
	outSpline = Spline(inPntFeat, zField, cellSize, splineType, weight)
	# Save the output 
	outname = "C:/MERRA/BIOvariables/dismoMERRA_T2M_QV2M/withMeanQV2M/80s/" +resolution + file + ".tif"
	outSpline.save(outname)
	
	
print "completed 10m"	


resolution = "5m_"

for file in pointFiles:
	field_list = arcpy.ListFields(file)
	nfield = field_list[2].name
	
	# Set local variables
	inPntFeat = file
	zField = nfield #zf
	cellSize = 0.08331
	splineType = "REGULARIZED"
	weight = 0.1

	# Check out the ArcGIS Spatial Analyst extension license
	arcpy.CheckOutExtension("Spatial")

	# Execute Spline
	outSpline = Spline(inPntFeat, zField, cellSize, splineType, weight)
	
	# Save the output 
	outname = "C:/MERRA/BIOvariables/dismoMERRA_T2M_QV2M/withMeanQV2M/80s/" +resolution + file + ".tif"
	outSpline.save(outname)
	
	
print "completed 5m MEAN 80S"	

resolution = "2_5m_"

for file in pointFiles:
	
	field_list = arcpy.ListFields(file)
	nfield = field_list[2].name
	
	# Set local variables
	inPntFeat = file
	zField = nfield #zf
	cellSize = 0.041655
	splineType = "REGULARIZED"
	weight = 0.1

	# Check out the ArcGIS Spatial Analyst extension license
	arcpy.CheckOutExtension("Spatial")

	# Execute Spline
	outSpline = Spline(inPntFeat, zField, cellSize, splineType, weight)
	
	# Save the output 
	outname = "C:/MERRA/BIOvariables/dismoMERRA_T2M_QV2M/withMeanQV2M/80s/" +resolution + file + ".tif"
	outSpline.save(outname)
		
print "completed 2.5m MEAN 80S"



## Set Workspace to Mean version 90s decade

env.workspace = "C:/MERRA/BIOvariables/dismoMERRA_T2M_QV2M/withMeanQV2M/90s"

pointFiles = arcpy.ListFiles("*.shp")

zFields = ["min_bio1","min_bio10", "min_bio11", "min_bio12", "min_bio13", "min_bio14", "min_bio15", "min_bio16", "min_bio17", "min_bio18", "min_bio19", "min_bio2", "min_bio3", "min_bio4", "min_bio5", "min_bio6", "min_bio7", "min_bio8", "min_bio9"]

resolution = "10m_"

for file in pointFiles:	
	field_list = arcpy.ListFields(file)
	nfield = field_list[2].name
	# Set local variables
	inPntFeat = file
	zField = nfield #zf
	cellSize = 0.16662
	splineType = "REGULARIZED"
	weight = 0.1

	# Check out the ArcGIS Spatial Analyst extension license
	arcpy.CheckOutExtension("Spatial")

	# Execute Spline
	outSpline = Spline(inPntFeat, zField, cellSize, splineType, weight)
	# Save the output 
	outname = "C:/MERRA/BIOvariables/dismoMERRA_T2M_QV2M/withMeanQV2M/90s/" +resolution + file + ".tif"
	outSpline.save(outname)
	
	
print "completed 10m"	


resolution = "5m_"

for file in pointFiles:
	field_list = arcpy.ListFields(file)
	nfield = field_list[2].name
	
	# Set local variables
	inPntFeat = file
	zField = nfield #zf
	cellSize = 0.08331
	splineType = "REGULARIZED"
	weight = 0.1

	# Check out the ArcGIS Spatial Analyst extension license
	arcpy.CheckOutExtension("Spatial")

	# Execute Spline
	outSpline = Spline(inPntFeat, zField, cellSize, splineType, weight)
	
	# Save the output 
	outname = "C:/MERRA/BIOvariables/dismoMERRA_T2M_QV2M/withMeanQV2M/90s/" +resolution + file + ".tif"
	outSpline.save(outname)
	
	
print "completed 5m MEAN 90S"	

resolution = "2_5m_"


for file in pointFiles:
	
	field_list = arcpy.ListFields(file)
	nfield = field_list[2].name
	
	# Set local variables
	inPntFeat = file
	zField = nfield #zf
	cellSize = 0.041655
	splineType = "REGULARIZED"
	weight = 0.1

	# Check out the ArcGIS Spatial Analyst extension license
	arcpy.CheckOutExtension("Spatial")

	# Execute Spline
	outSpline = Spline(inPntFeat, zField, cellSize, splineType, weight)
	
	# Save the output 
	outname = "C:/MERRA/BIOvariables/dismoMERRA_T2M_QV2M/withMeanQV2M/90s/" +resolution + file + ".tif"
	outSpline.save(outname)
	
	
print "completed 2.5m MEAN 90S"



## Set Workspace to Mean version 00s decade

env.workspace = "C:/MERRA/BIOvariables/dismoMERRA_T2M_QV2M/withMeanQV2M/00s"

pointFiles = arcpy.ListFiles("*.shp")

zFields = ["min_bio1","min_bio10", "min_bio11", "min_bio12", "min_bio13", "min_bio14", "min_bio15", "min_bio16", "min_bio17", "min_bio18", "min_bio19", "min_bio2", "min_bio3", "min_bio4", "min_bio5", "min_bio6", "min_bio7", "min_bio8", "min_bio9"]

resolution = "10m_"

#zipped = zip (pointFiles,zFields)



for file in pointFiles:	
	field_list = arcpy.ListFields(file)
	nfield = field_list[2].name
	# Set local variables
	inPntFeat = file
	zField = nfield #zf
	cellSize = 0.16662
	splineType = "REGULARIZED"
	weight = 0.1

	# Check out the ArcGIS Spatial Analyst extension license
	arcpy.CheckOutExtension("Spatial")

	# Execute Spline
	outSpline = Spline(inPntFeat, zField, cellSize, splineType, weight)
	
	# Save the output 
	outname = "C:/MERRA/BIOvariables/dismoMERRA_T2M_QV2M/withMeanQV2M/00s/" +resolution + file + ".tif"
	outSpline.save(outname)
	
	
print "completed 10m"	


resolution = "5m_"


for file in pointFiles:
	field_list = arcpy.ListFields(file)
	nfield = field_list[2].name
	
	# Set local variables
	inPntFeat = file
	zField = nfield #zf
	cellSize = 0.08331
	splineType = "REGULARIZED"
	weight = 0.1

	# Check out the ArcGIS Spatial Analyst extension license
	arcpy.CheckOutExtension("Spatial")

	# Execute Spline
	outSpline = Spline(inPntFeat, zField, cellSize, splineType, weight)
	
	# Save the output 
	outname = "C:/MERRA/BIOvariables/dismoMERRA_T2M_QV2M/withMeanQV2M/00s/" +resolution + file + ".tif"
	outSpline.save(outname)
	
	
print "completed 5m  MEAN 00S"	

resolution = "2_5m_"

#zipped = zip (pointFiles,zFields)

for file in pointFiles:
	
	field_list = arcpy.ListFields(file)
	nfield = field_list[2].name
	
	# Set local variables
	inPntFeat = file
	zField = nfield #zf
	cellSize = 0.041655
	splineType = "REGULARIZED"
	weight = 0.1

	# Check out the ArcGIS Spatial Analyst extension license
	arcpy.CheckOutExtension("Spatial")

	# Execute Spline
	outSpline = Spline(inPntFeat, zField, cellSize, splineType, weight)
	
	# Save the output 
	outname = "C:/MERRA/BIOvariables/dismoMERRA_T2M_QV2M/withMeanQV2M/00s/" +resolution + file + ".tif"
	outSpline.save(outname)
	
	
print "completed 2.5m  MEAN 00S"





################################################################################
################################################################################
#STEP 6: REDUCING DEPTH OF PIXEL OF RASTERS (in R)

library(raster)


setwd("C:\\MERRA\\BIOvariables\\dismoMERRA_T2M_QV2M\\")


folders<-c( "10m_max_00s", "10m_max_80s", "10m_max_90s", "10m_mean_00s", "10m_mean_80s", "10m_mean_90s", "10m_min_00s", "10m_min_80s", "10m_min_90s", "5m_max_00s", "5m_max_80s", "5m_max_90s", "5m_mean_00s", "5m_mean_80s",  "5m_mean_90s", "5m_min_00s", "5m_min_80s", "5m_min_90s", "2_5m_max_00s", "2_5m_max_80s", "2_5m_max_90s", "2_5m_mean_00s", "2_5m_mean_80s", "2_5m_mean_90s", "2_5m_min_00s", "2_5m_min_80s", "2_5m_min_90s", "30s_max_00s",   "30s_max_80s", "30s_max_90s", "30s_mean_00s", "30s_mean_80s", "30s_mean_90s", "30s_min_00s", "30s_min_80s", "30s_min_90s")

bios<-c("bio1","bio2","bio3","bio4", "bio5", "bio6", "bio7", "bio8", "bio9", "bio10", "bio11", "bio12", "bio13", "bio14", "bio15","bio16", "bio17", "bio18", "bio19")


rbios<-rep(NA,19)

class(rbios)<-"character"

print (paste(Sys.time()," start"))

for (folder in 1: length(folders))
{
	rbios<-paste(folders[folder],"_",bios,".tif", sep="")
	
	
	for (rastername in 1:11) # para las variables de temperatura
	{
	rasterfile<-rbios[rastername]
	r<-raster(paste("E:\\MERRAClim\\DataDryads\\", folders[folder],"\\",rasterfile, sep="" ))
	writeRaster(round(r,0), filename=rasterfile, datatype='INT2S',format="GTiff")
	
	}
	
	for (rastername in 12:19) # para las variables de humedad
	{
	rasterfile<-rbios[rastername]
	r<-raster(paste("E:\\MERRAClim\\DataDryads\\", folders[folder],"\\",rasterfile, sep="" ))
	outR<-round(r*100000,0)
	writeRaster(outR,filename=rasterfile, datatype='INT4U',format="GTiff")
	
	}

}
paste(Sys.time()," end")

################################################################################
################################################################################
#STEP 7: ADAPTATION FROM PRECIPITATION TO HUMIDITY (in R, code only shown for Mean version at 2.5 minutes resolution from the 80s decade)
##BIO12 has to be divided by 12
##BIO18 has to be divided by 3
##BIO19 has to be divided by 3
##BIO16 has to be divided by 3
##BIO17 has to be divided by 3

#load packages
library(raster)

# Set the working directory where all the ras
setwd("C:\\MERRA\\BIOvariables\\dismoMERRA_T2M_QV2M\\2_5m_mean_80s\\")


bio12<-raster("2_5m_mean_80s_bio12.tif")
newbio12<-bio12/12
writeRaster(newbio12, "2_5m_mean_80s_bio12_tbg.tif",format="GTiff")

bio16<-raster("2_5m_mean_80s_bio16.tif")
newbio16<-bio16/3
writeRaster(newbio16, "2_5m_mean_80s_bio16_tbg.tif",format="GTiff")

bio17<-raster("2_5m_mean_80s_bio17.tif")
newbio17<-bio17/3
writeRaster(newbio17, "2_5m_mean_80s_bio17_tbg.tif",format="GTiff")

bio18<-raster("2_5m_mean_80s_bio18.tif")
newbio18<-bio18/3
writeRaster(newbio18, "2_5m_mean_80s_bio18_tbg.tif",format="GTiff")

bio19<-raster("2_5m_mean_80s_bio19.tif")
newbio19<-bio19/3
writeRaster(newbio19, "2_5m_mean_80s_bio19_tbg.tif",format="GTiff")

