import xarray as xr
import glob
import numpy as np
def AddMMM(file="DHW_ssp245_BCC-CSM2-MR_DHW.nc",directoryData="in",directorySal="Sal"):

	nc1 = xr.open_dataset("ALL_mmm.nc",decode_times=False,decode_timedelta=False)
	nc2 = xr.open_dataset(directoryData+"/"+file,decode_times=False,decode_timedelta=False)
	MapType=0
	if file.count("BCC-CSM2-MR")>0:
		MapType=0
	if file.count("CanESM5")>0:
		MapType=1
	if file.count("CESM2")>0:
		MapType=2
	if file.count("EC-Earth3")>0:
		MapType=3
	if file.count("IPSL-CM6A-LR")>0:
		MapType=4
	if file.count("MIROC6")>0:
		MapType=5
	if file.count("MRI-ESM2-0")>0:
		MapType=6
	if file.count("NorESM2-MM")>0:
		MapType=7
	if file.count("Ensemble5")>0:
		MapType=8
	if file.count("Ensemble8")>0:
		MapType=9
	print(MapType)
	ALL_mmm = 1 * np.ones((nc2.dims['lat'], nc2.dims['lon'])) * nc1.ALL_mmm.isel(time=MapType)
	nc2["MMM"]=ALL_mmm
	nc2.MMM.attrs={"long_name" : "monthly mean sea surface temperature climatology",
	"standard_name" : "sea_surface_temperature",
	"units":"degrees_Celsius"}
	nc2.to_netcdf(path=directorySal+"/"+file)


for i in glob.glob("in/*"):
	A=i.split("/")[-1]
	SalidaDir="Sal"
	print(A,i)
	AddMMM(file=A,directoryData="in",directorySal="Sal")
