diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a7a1f72 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +1Teselas/map/* +2nc2image/img/* +3image2video/ConCoralN/* +3image2video/env/* +3image2video/img/* +3image2video/SinCoralN/* +3image2video/Videos/* +Data/* +DataF/* +DataOriginal/* +DataVieja/* \ No newline at end of file diff --git a/0PreProc/CorrectNameandFormat.py b/0PreProc/CorrectNameandFormat.py new file mode 100644 index 0000000..1266a8d --- /dev/null +++ b/0PreProc/CorrectNameandFormat.py @@ -0,0 +1,139 @@ +import xarray as xr +import glob +import numpy as np + +def ModAttrs(DictOri,site,label,data): + keys=list(DictOri.keys()) + NewDict={} + for key in range(len(keys)): + if site==key: + NewDict[label]=data + NewDict[keys[key]]=DictOri[keys[key]] + return NewDict + +def date2float(df,var): + V1=df[var].astype("float64") + return V1 + +def cleanAttr(var,T): + A=T.attrs + if var in A.keys(): + A.pop(var) + return A + +def Corrector2(file,DataEntry,DataSal): + + T=xr.open_dataset(DataEntry+file) + + T.DoY_DHW4cal.attrs={'long_name': 'first day of the year when DHW exceeds 4 degree-weeks', + 'units': 'day of the year', + 'comment': 'considering January 1st the first day of the year'} + T.DoY_DHW8cal.attrs={'long_name': 'first day of the year when DHW exceeds 8 degree-weeks', + 'units': 'day of the year', + 'comment': 'considering January 1st the first day of the year'} + data =T.DHW_q99[0,:,:].values + T["nCellsTotal"]=np.count_nonzero(np.isnan(data))+np.count_nonzero(~np.isnan(data)) + T["nCellsValid"]=np.count_nonzero(~np.isnan(data)) + T.nCellsTotal.attrs={"long_name": "number of data cells in selected area"} + T.nCellsValid.attrs={ "long_name": "number of valid data cells in selected area"} + T.to_netcdf(path=DataSal+file) + +def CorrectNc(Map1="/home/mario/Documentos/Ocean/NetcdfToPng/NC2023Patron/",SalidaDir="/home/mario/Documentos/Ocean/NetcdfToPng/MapsModificados/",file="DHW_ssp245_BCC-CSM2-MR_DHW.nc"): + nc1 = xr.open_dataset(Map1+file)#,decode_times=False,decode_timedelta=False + #nc1["DoY_DHW4cal"]=nc1["DoY_DHW4"] + #nc1["DoY_DHW8cal"]=nc1["DoY_DHW8"] + #nc1["nDays_DHW4cal"]=nc1["nDays_DHW4"] + #nc1["nDays_DHW8cal"]=nc1["nDays_DHW8"] + #nc1=nc1.drop("DoY_DHW4") + #nc1=nc1.drop("DoY_DHW8") + #nc1=nc1.drop("nDays_DHW4") + #nc1=nc1.drop("nDays_DHW8") + #nc1["DoY_DHW4"]=nc1["DoYrel_DHW4"] + #nc1["DoY_DHW8"]=nc1["DoYrel_DHW8"] + #nc1=nc1.drop("DoYrel_DHW4") + #nc1=nc1.drop("DoYrel_DHW8") + #nc1=nc1.drop_sel({"time":1986}) + df=nc1 + try: + df=df.drop("quantile") + except: + print("noq") + df.attrs.keys() + #df.attrs=ModAttrs(df.attrs,5,"label","data") + df.attrs["time_coverage_start"]=1986 + data=df.DHW_q99[0,:,:].values + df.attrs=ModAttrs(df.attrs,5,"region_name","Global") + #df["nCellsTotal"]=np.count_nonzero(np.isnan(data))+np.count_nonzero(~np.isnan(data)) + #df["nCellsValid"]=np.count_nonzero(~np.isnan(data)) + file=file.replace("_start1986","").replace("_dec22","") + HH=SalidaDir+file + # df.DoY_DHW4.attrs={'long_name': "first day of the year when DHW exceeds 4 degree-weeks, relative to the climatological coldest DOY", + # 'units': 'day of the year', + # 'comment': "considering the coldest climatological DoY as the first day of the year"} + # df.DoY_DHW8.attrs={'long_name': "first day of the year when DHW exceeds 8 degree-weeks, relative to the climatological coldest DOY", + # 'units': 'day of the year', + # 'comment': "considering the coldest climatological DoY as the first day of the year"} + # df.nDays_DHW8.attrs={"long_name" : "number of days above 8 degrees-week", + # "units" : "days"} + # df.nDays_DHW4.attrs={"long_name" : "number of days above 4 degrees-week", + # "units" : "days"} + mask_land = 1 * np.ones((df.dims['lat'], df.dims['lon'])) * np.isnan(df.DHW_q99.isel(time=0)) + df["mask_land"]=mask_land + df["nDays_DHW4"]=df["nDays_DHW4"].astype(np.float64)/1e9/60/60/24 + #if np.nanmax(df["nDays_DHW4"].values)>1000: + #df["nDays_DHW4"]=df["nDays_DHW4"]/1e9/60/60/24 + #print("NDais") + df["nDays_DHW8"]=df["nDays_DHW8"].astype(np.float64)/1e9/60/60/24 + df["nDays_DHW8"]=df["nDays_DHW8"].where(df.mask_land != 1) + df["nDays_DHW4"]=df["nDays_DHW4"].where(df.mask_land != 1) + #if np.nanmax(df["nDays_DHW8"].values)>1000: + #df["nDays_DHW8"]=df["nDays_DHW8"] + #print("NDais") + # df["nDays_DHW4"]=df["nDays_DHW4"].astype("float64") + # if np.nanmax(df["nDaysrel_DHW4"].values)>1000: + # df["nDaysrel_DHW4"]=df["nDaysrel_DHW4"]/1e9/60/60/24 + # print("NDais") + # df["nDaysrel_DHW8"]=df["nDaysrel_DHW8"].astype("float64") + # if np.nanmax(df["nDaysrel_DHW8cal"].values)>1000: + # df["nDaysrel_DHW8"]=df["nDaysrel_DHW8"]/1e9/60/60/24 + # print("NDais") + #df["nDays_DHW8"].attrs={'long_name': 'number of days above 8 degrees-week, relative to the climatological coldest DOY'} + #df["nDays_DHW4"].attrs={'long_name': 'number of days above 4 degrees-week, relative to the climatological coldest DOY'} + #df["nDays_DHW8cal"].attrs={'long_name': 'number of days above 8 degrees-week, considering January 1st the first day of the year'} + #df["nDays_DHW4cal"].attrs={'long_name': 'number of days above 4 degrees-week, considering January 1st the first day of the year'} + + #df["nDays_DHW8cal"]=df["nDays_DHW8cal"].where(df.mask_land != 1) + #df["nDays_DHW4cal"]=df["nDays_DHW4cal"].where(df.mask_land != 1) + df=df.drop("mask_land") + Tempp=list(df.var().keys()) + for i in Tempp: + df[i].attrs=cleanAttr("coordinates",df[i]) + # df.DoY_DHW8cal.attrs={'long_name': 'first day of the year when DHW exceeds 8 degree-weeks', + # 'units': 'day of the year', + # 'comment': 'considering January 1st the first day of the year'} + # df.DoY_DHW4cal.attrs={'long_name': 'first day of the year when DHW exceeds 4 degree-weeks', + # 'units': 'day of the year', + # 'comment': 'considering January 1st the first day of the year'} + comp = dict(zlib=True, complevel=5) + encoding = {var: comp for var in df.data_vars} + data =df.DHW_q99[0,:,:].values + df["nCellsTotal"]=np.count_nonzero(np.isnan(data))+np.count_nonzero(~np.isnan(data)) + df["nCellsValid"]=np.count_nonzero(~np.isnan(data)) + df.nCellsTotal.attrs={"long_name": "number of data cells in selected area"} + df.nCellsValid.attrs={ "long_name": "number of valid data cells in selected area"} + print(df) + df.to_netcdf(path=HH, encoding=encoding) +nn=0 +for i in glob.glob("../DataOriginal/*"): + A=i.split("/")[-1] + SalidaDir="../Data/" + nn+=1 + print(A) + CorrectNc(Map1="../DataOriginal/",SalidaDir=SalidaDir,file=A) + print(1) + #if nn>1: + #break + #SalidaDir2="../DataF/" + #Corrector2(A,SalidaDir,SalidaDir2) + #print(2) + #break diff --git a/0PreProc/CorregirNCPythonpuro.py b/0PreProc/CorregirNCPythonpuro.py new file mode 100644 index 0000000..5cdb4ff --- /dev/null +++ b/0PreProc/CorregirNCPythonpuro.py @@ -0,0 +1,117 @@ +import xarray as xr +import glob +import numpy as np + +def ModAttrs(DictOri,site,label,data): + keys=list(DictOri.keys()) + NewDict={} + for key in range(len(keys)): + if site==key: + NewDict[label]=data + NewDict[keys[key]]=DictOri[keys[key]] + return NewDict + +def date2float(df,var): + V1=df[var].astype("float64") + return V1 + +def cleanAttr(var,T): + A=T.attrs + if var in A.keys(): + A.pop(var) + return A + +def Corrector2(file,DataEntry,DataSal): + T=xr.open_dataset(DataEntry+file) + T.DoY_DHW4cal.attrs={'long_name': 'first day of the year when DHW exceeds 4 degree-weeks', + 'units': 'day of the year', + 'comment': 'considering January 1st the first day of the year'} + T.DoY_DHW8cal.attrs={'long_name': 'first day of the year when DHW exceeds 8 degree-weeks', + 'units': 'day of the year', + 'comment': 'considering January 1st the first day of the year'} + data =T.DHW_q99[0,:,:].values + T["nCellsTotal"]=np.count_nonzero(np.isnan(data))+np.count_nonzero(~np.isnan(data)) + T["nCellsValid"]=np.count_nonzero(~np.isnan(data)) + T.nCellsTotal.attrs={"long_name": "number of data cells in selected area"} + T.nCellsValid.attrs={ "long_name": "number of valid data cells in selected area"} + T.to_netcdf(path=DataSal+file) + +def FusionNc(Map1="/home/mario/Documentos/Ocean/NetcdfToPng/NC2023Patron/",Map2="/home/mario/Documentos/Ocean/NetcdfToPng/MapsAModificar/",SalidaDir="/home/mario/Documentos/Ocean/NetcdfToPng/MapsModificados/",file="DHW_ssp245_BCC-CSM2-MR_DHW.nc"): + nc1 = xr.open_dataset(Map1+file,decode_times=False,decode_timedelta=False) + nc2b = xr.open_dataset(Map2+file,decode_times=False,decode_timedelta=False) + #nc2b=nc2b.drop_sel({"time":1986}) + #nc2b=nc2b.drop_sel({"time":1985}) + nc1["DoY_DHW4cal"]=nc2b["DoY_DHW4"] + nc1["DoY_DHW8cal"]=nc2b["DoY_DHW8"] + nc1["nDays_DHW4cal"]=nc2b["nDays_DHW4"] + nc1["nDays_DHW8cal"]=nc2b["nDays_DHW8"] + df=nc1 + try: + df=df.drop("quantile") + except: + print("noq") + df.attrs.keys() + #df.attrs=ModAttrs(df.attrs,5,"label","data") + df.attrs["time_coverage_start"]=1987 + data=df.DHW_q99[0,:,:].values + df.attrs=ModAttrs(df.attrs,5,"region_name","Global") + #df["nCellsTotal"]=np.count_nonzero(np.isnan(data))+np.count_nonzero(~np.isnan(data)) + #df["nCellsValid"]=np.count_nonzero(~np.isnan(data)) + HH=SalidaDir+file + df.DoY_DHW4.attrs={'long_name': "first day of the year when DHW exceeds 4 degree-weeks, relative to the climatological coldest DOY", + 'units': 'day of the year', + 'comment': "considering the coldest climatological DoY as the first day of the year"} + df.DoY_DHW8.attrs={'long_name': "first day of the year when DHW exceeds 8 degree-weeks, relative to the climatological coldest DOY", + 'units': 'day of the year', + 'comment': "considering the coldest climatological DoY as the first day of the year"} + df.nDays_DHW8.attrs={"long_name" : "number of days above 8 degrees-week", + "units" : "days"} + df.nDays_DHW4.attrs={"long_name" : "number of days above 4 degrees-week", + "units" : "days"} + df["nDays_DHW4"]=df["nDays_DHW4"].astype("float64") + if np.nanmax(df["nDays_DHW4"].values)>1000: + df["nDays_DHW4"]=df["nDays_DHW4"]/1e9/60/60/24 + df["nDays_DHW8"]=df["nDays_DHW8"].astype("float64") + if np.nanmax(df["nDays_DHW8"].values)>1000: + df["nDays_DHW8"]=df["nDays_DHW8"]/1e9/60/60/24 + df["nDays_DHW4cal"]=df["nDays_DHW4cal"].astype("float64") + if np.nanmax(df["nDays_DHW4cal"].values)>1000: + df["nDays_DHW4cal"]=df["nDays_DHW4cal"]/1e9/60/60/24 + df["nDays_DHW8cal"]=df["nDays_DHW8cal"].astype("float64") + if np.nanmax(df["nDays_DHW8cal"].values)>1000: + df["nDays_DHW8cal"]=df["nDays_DHW8cal"]/1e9/60/60/24 + df["nDays_DHW8"].attrs={'long_name': 'number of days above 8 degrees-week, relative to the climatological coldest DOY'} + df["nDays_DHW4"].attrs={'long_name': 'number of days above 4 degrees-week, relative to the climatological coldest DOY'} + df["nDays_DHW8cal"].attrs={'long_name': 'number of days above 8 degrees-week, considering January 1st the first day of the year'} + df["nDays_DHW4cal"].attrs={'long_name': 'number of days above 4 degrees-week, considering January 1st the first day of the year'} + mask_land = 1 * np.ones((df.dims['lat'], df.dims['lon'])) * np.isnan(df.DHW_q99.isel(time=0)) + df["mask_land"]=mask_land + df["nDays_DHW8"]=df["nDays_DHW8"].where(df.mask_land != 1) + df["nDays_DHW4"]=df["nDays_DHW4"].where(df.mask_land != 1) + df["nDays_DHW8cal"]=df["nDays_DHW8cal"].where(df.mask_land != 1) + df["nDays_DHW4cal"]=df["nDays_DHW4cal"].where(df.mask_land != 1) + df=df.drop("mask_land") + Tempp=list(df.var().keys()) + for i in Tempp: + df[i].attrs=cleanAttr("coordinates",df[i]) + df.DoY_DHW8cal.attrs={'long_name': 'first day of the year when DHW exceeds 8 degree-weeks', + 'units': 'day of the year', + 'comment': 'considering January 1st the first day of the year'} + df.DoY_DHW4cal.attrs={'long_name': 'first day of the year when DHW exceeds 4 degree-weeks', + 'units': 'day of the year', + 'comment': 'considering January 1st the first day of the year'} + comp = dict(zlib=True, complevel=5) + encoding = {var: comp for var in df.data_vars} + df.to_netcdf(path=HH, encoding=encoding) + +for i in glob.glob("/home/mario/Documentos/Ocean/NetcdfToPng/NC2023Patron/*"): + A=i.split("/")[-1] + #try: + #SalidaDir="/home/mario/Documentos/Ocean/NetcdfToPng/MapsModificados/" + #file=A + print(A) + #FusionNc(Map1="/home/mario/Documentos/Ocean/NetcdfToPng/NC2023Patron/",Map2="/home/mario/Documentos/Ocean/NetcdfToPng/MapsAModificar/",SalidaDir=SalidaDir,file=file) + #print(1) + #Corrector2(file,SalidaDir,"/home/mario/Documentos/Ocean/NetcdfToPng/MapCorregidoV2/") + #print(2) + #break diff --git a/1Teselas/P1_96P6.jgw b/1Teselas/P1_96P6.jgw new file mode 100644 index 0000000..d2907e8 --- /dev/null +++ b/1Teselas/P1_96P6.jgw @@ -0,0 +1,6 @@ +0.01837109614206981 +0 +0 +-0.01837109614206981 +-180.36742192284140174 +89.99081445192896922 diff --git a/1Teselas/P1_96P6.jpeg b/1Teselas/P1_96P6.jpeg new file mode 100644 index 0000000..f8c8a74 Binary files /dev/null and b/1Teselas/P1_96P6.jpeg differ diff --git a/1Teselas/README.md b/1Teselas/README.md new file mode 100644 index 0000000..e69de29 diff --git a/1Teselas/init.sh b/1Teselas/init.sh new file mode 100644 index 0000000..5fac73e --- /dev/null +++ b/1Teselas/init.sh @@ -0,0 +1,5 @@ +docker run -it --mount type=bind,src=/home/mario/Documentos/Ocean/ScuenciaOrganizadaPre/Teselas,dst=/temporal ghcr.io/osgeo/gdal:ubuntu-full-latest bash +gdal2tiles.py -p geodetic -z 0-7 -r average -s EPSG:4326 --xyz -w mapml /temporal/P1_96P6.jpeg /temporal/map + + + diff --git a/2nc2image/ExtractImage.py b/2nc2image/ExtractImage.py new file mode 100644 index 0000000..8fd728d --- /dev/null +++ b/2nc2image/ExtractImage.py @@ -0,0 +1,213 @@ +import xarray as xr +import rioxarray as rio +from matplotlib import cm +import numpy as np +import time +import matplotlib +import matplotlib.image as imgg +import os +from PIL import Image, ImageDraw, ImageFont + + + + + +def write_png(data,name, origin='upper', colormapD=None): + """ + Transform an array of data into a PNG string. + This can be written to disk using binary I/O, or encoded using base64 + for an inline PNG like this: + + >>> png_str = write_png(array) + >>> "data:image/png;base64,"+png_str.encode('base64') + + Inspired from + https://stackoverflow.com/questions/902761/saving-a-numpy-array-as-an-image + + Parameters + ---------- + data: numpy array or equivalent list-like object. + Must be NxM (mono), NxMx3 (RGB) or NxMx4 (RGBA) + + origin : ['upper' | 'lower'], optional, default 'upper' + Place the [0,0] index of the array in the upper left or lower left + corner of the axes. + + colormap : callable, used only for `mono` image. + Function of the form [x -> (r,g,b)] or [x -> (r,g,b,a)] + for transforming a mono image into RGB. + It must output iterables of length 3 or 4, with values between + 0. and 1. Hint: you can use colormaps from `matplotlib.cm`. + + Returns + ------- + PNG formatted byte string + + """ + + if colormapD is None: + def colormapD(x): + return (x, x, x, 1) + + arr = np.atleast_3d(data) + height, width, nblayers = arr.shape + + if nblayers not in [1, 3, 4]: + raise ValueError('Data must be NxM (mono), ' + 'NxMx3 (RGB), or NxMx4 (RGBA)') + assert arr.shape == (height, width, nblayers) + + + if nblayers == 1: + arr = np.array(list(map(colormapD, arr.ravel()))) + nblayers = arr.shape[1] + if nblayers not in [3, 4]: + raise ValueError('colormap must provide colors of r' + 'length 3 (RGB) or 4 (RGBA)') + arr = arr.reshape((height, width, nblayers)) + assert arr.shape == (height, width, nblayers) + + if nblayers == 3: + arr = np.concatenate((arr, np.ones((height, width, 1))), axis=2) + nblayers = 4 + assert arr.shape == (height, width, nblayers) + assert nblayers == 4 + + # Normalize to uint8 if it isn't already. + if arr.dtype != 'uint8': + with np.errstate(divide='ignore', invalid='ignore'): + arr = arr * 255./np.array([1., 1., 1., 1.]).reshape((1, 1, 4)) + arr[~np.isfinite(arr)] = 0 + arr = arr.astype('uint8') + + # Eventually flip the image. + if origin == 'lower': + arr = arr[::-1, :, :] + r3 = arr.copy(order='C') + matplotlib.image.imsave(name, r3) + + +def image_to_url(image,name, colormapD=None, origin='upper'): + """ + Infers the type of an image argument and transforms it into a URL. + + Parameters + ---------- + image: string, file or array-like object + * If string, it will be written directly in the output file. + * If file, it's content will be converted as embedded in the + output file. + * If array-like, it will be converted to PNG base64 string and + embedded in the output. + origin: ['upper' | 'lower'], optional, default 'upper' + Place the [0, 0] index of the array in the upper left or + lower left corner of the axes. + colormap: callable, used only for `mono` image. + Function of the form [x -> (r,g,b)] or [x -> (r,g,b,a)] + for transforming a mono image into RGB. + It must output iterables of length 3 or 4, with values between + 0. and 1. You can use colormaps from `matplotlib.cm`. + + """ + + if 'ndarray' in image.__class__.__name__: + img = write_png(image, origin=origin, colormapD=colormapD,name=name) + + +def get_color(x,colormap,Min,Max): + decimals = 2 + try: + #print(x,Min,Max) + x=(x*1.-Min)/(Max*1.-Min) + #print(x) + if x < 0: + x=0.0 + if x > 1.0: + x=1.0 + x = np.around(x, decimals=decimals) + if colormap=="Spectral" or colormap=='ocean' or colormap=="RdYlBu": + Tempcm=cm.get_cmap(colormap).reversed() + else: + Tempcm=cm.get_cmap(colormap) + #print(x) + ls = np.around(np.linspace(0,1,10**decimals+1),decimals=decimals) + #print(ls) + if 0 <= x <= 1: + #print(cm.get_cmap('viridis')(ls)[np.argwhere(ls==x)][0][0].shape,cm.get_cmap('viridis')(ls)[np.argwhere(ls==x)]) + #print(x,np.argwhere(ls==x)) + return Tempcm(ls)[np.argwhere(ls==x)][0][0] + else: + #print(np.array(np.zeros(4)).shape,np.array(np.zeros(4))) + #print(x) + return np.zeros(4) + except: + print(x,np.argwhere(ls==x)) + return np.zeros(4) + +def ExtractMapImage(file,colormap,countyear,name,nc,geometry=""): + + nc = nc.rio.write_crs(4326) + if geometry=="": + try: + nc = nc.rio.clip(geometries) + except: + pass + else: + pass + bounds=[[float(nc.lat.min().values),float(nc.lon.min().values)],[float(nc.lat.max().values),float(nc.lon.max().values)]] + i=countyear + + year=int(nc.time[i].values) + print(i,year) + ncVar=nc.DHW_q99 + data = ncVar[i,:,:].values + + + Min=np.around(np.nanquantile(ncVar[int(year)-1986,:,:].values, 0.01),2) + Max=np.around(np.nanquantile(ncVar[int(year)-1986,:,:].values, 0.99),2)#np.around(np.nanmax(ncVar[year-1985,:,:].values),2) + + image_to_url(data,name, colormapD=lambda x: get_color(x,colormap,Min,Max), origin="lower") + + +def ProcessAllImage(ssp,model,Colorpalete,ExportDirectory,DataDirectory): + cc=0 + Var="DHW" + for i in ssp: + for j in model: + ff=DataDirectory+"%s_%s_%s_DHW.nc"%(Var,i,j) + for CM in Colorpalete: + for countyear in range(115): + nc = xr.open_dataset(ff, decode_coords="all") + year=int(nc.time[countyear].values) + Evaluado=ExportDirectory+"%s_%s"%(CM,ff.split("/")[-1].replace(".nc","_%s.png"%(year))) + if not os.path.isfile(Evaluado): + cc=cc+1 + + Total=cc + start=time.time() + cc=0 + for i in ssp: + for j in model: + ff=DataDirectory+"%s_%s_%s_DHW.nc"%(Var,i,j) + for CM in Colorpalete: + for countyear in range(115): + nc = xr.open_dataset(ff, decode_coords="all") + year=int(nc.time[countyear].values) + Evaluado=ExportDirectory+"%s_%s"%(CM,ff.split("/")[-1].replace(".nc","_%s.png"%(year))) + if not os.path.isfile(Evaluado): + ExtractMapImage(ff,CM,countyear,Evaluado,nc) + cc=cc+1 + now=time.time() + print(cc,(start-now)/cc,(start-now)/cc*(Total-cc)) + +ssp=["ssp245","ssp370","ssp585"] +model=("BCC-CSM2-MR","CESM2","CanESM5","EC-Earth3","IPSL-CM6A-LR","MIROC6","MRI-ESM2-0","NorESM2-MM")#,"ensemble5","ensemble8") +Colorpalete=["RdYlBu",'Spectral','ocean',"coolwarm",] +ExportDirectory="img/" +DataDirectory="../Data/" + +ProcessAllImage(ssp,model,Colorpalete,ExportDirectory,DataDirectory) + + + + diff --git a/2nc2image/ExtractImageNOAA.py b/2nc2image/ExtractImageNOAA.py new file mode 100644 index 0000000..1bd4a0f --- /dev/null +++ b/2nc2image/ExtractImageNOAA.py @@ -0,0 +1,220 @@ +import xarray as xr +import rioxarray as rio +from matplotlib import cm +import numpy as np +import time +import matplotlib +import matplotlib.image as imgg +import os + + +def write_png(data,name, origin='upper', colormapD=None): + """ + Transform an array of data into a PNG string. + This can be written to disk using binary I/O, or encoded using base64 + for an inline PNG like this: + + >>> png_str = write_png(array) + >>> "data:image/png;base64,"+png_str.encode('base64') + + Inspired from + https://stackoverflow.com/questions/902761/saving-a-numpy-array-as-an-image + + Parameters + ---------- + data: numpy array or equivalent list-like object. + Must be NxM (mono), NxMx3 (RGB) or NxMx4 (RGBA) + + origin : ['upper' | 'lower'], optional, default 'upper' + Place the [0,0] index of the array in the upper left or lower left + corner of the axes. + + colormap : callable, used only for `mono` image. + Function of the form [x -> (r,g,b)] or [x -> (r,g,b,a)] + for transforming a mono image into RGB. + It must output iterables of length 3 or 4, with values between + 0. and 1. Hint: you can use colormaps from `matplotlib.cm`. + + Returns + ------- + PNG formatted byte string + + """ + + if colormapD is None: + def colormapD(x): + return (x, x, x, 1) + arr = np.atleast_3d(data) + height, width, nblayers = arr.shape + if nblayers not in [1, 3, 4]: + raise ValueError('Data must be NxM (mono), ' + 'NxMx3 (RGB), or NxMx4 (RGBA)') + assert arr.shape == (height, width, nblayers) + if nblayers == 1: + arr = np.array(list(map(colormapD, arr.ravel()))) + nblayers = arr.shape[1] + if nblayers not in [3, 4]: + raise ValueError('colormap must provide colors of r' + 'length 3 (RGB) or 4 (RGBA)') + arr = arr.reshape((height, width, nblayers)) + assert arr.shape == (height, width, nblayers) + if nblayers == 3: + arr = np.concatenate((arr, np.ones((height, width, 1))), axis=2) + nblayers = 4 + assert arr.shape == (height, width, nblayers) + assert nblayers == 4 + # Normalize to uint8 if it isn't already. + if arr.dtype != 'uint8': + with np.errstate(divide='ignore', invalid='ignore'): + arr = arr * 255./np.array([1., 1., 1., 1.]).reshape((1, 1, 4)) + arr[~np.isfinite(arr)] = 0 + arr = arr.astype('uint8') + # Eventually flip the image. + if origin == 'upper': + arr = arr[::-1, :, :] + r3 = arr.copy(order='C') + matplotlib.image.imsave(name, r3) + + +def image_to_url(image,name, colormapD=None, origin='lower'): + """ + Infers the type of an image argument and transforms it into a URL. + + Parameters + ---------- + image: string, file or array-like object + * If string, it will be written directly in the output file. + * If file, it's content will be converted as embedded in the + output file. + * If array-like, it will be converted to PNG base64 string and + embedded in the output. + origin: ['upper' | 'lower'], optional, default 'upper' + Place the [0, 0] index of the array in the upper left or + lower left corner of the axes. + colormap: callable, used only for `mono` image. + Function of the form [x -> (r,g,b)] or [x -> (r,g,b,a)] + for transforming a mono image into RGB. + It must output iterables of length 3 or 4, with values between + 0. and 1. You can use colormaps from `matplotlib.cm`. + + """ + + if 'ndarray' in image.__class__.__name__: + img = write_png(image, origin=origin, colormapD=colormapD,name=name) + +def get_colorD(x,Tempcm,Min,Max,np):#* + """Calculate color of pixel + Args: + x (float): Value of pixel + Tempcm (array): Colormap values + Min (float): min value + Max (float): max value + ls (_type_): linear interpolation function + decimals (_type_): number of decimals + np (_type_): library numpy + + Returns: + array (4x1): array of color rbga + """ + import numpy as np + try: + if x <= 0: + x = 0 + else: + x = x+1 + if x > 21: + x = 21 + x = int(np.fix(x)) + except: + return Tempcm[-1] + + return Tempcm[x] + +def ExtractMapImage(file,colormap,countyear,name,nc,geometry=""): + import matplotlib as mpl + nc = nc.rio.write_crs(4326) + if geometry=="": + try: + nc = nc.rio.clip(geometries) + except: + pass + else: + pass + bounds=[[float(nc.lat.min().values),float(nc.lon.min().values)],[float(nc.lat.max().values),float(nc.lon.max().values)]] + i=countyear + + year=int(nc.time[i].values) + print(i,year) + ncVar=nc.DHW_q99 + data = ncVar[i,:,:].values + + + if colormap=="noaa": + Min=np.around(np.nanmin(data),0) + Max=np.around(np.nanmax(data),0) + cmap=np.array([[200, 250, 250, 255], + [ 69, 49, 120, 255], + [ 99, 79, 149, 255], + [129, 110, 179, 255], + [159, 140, 209, 255], + [255, 252, 0, 255], + [253, 220, 0, 255], + [251, 185, 0, 255], + [251, 149, 1, 255], + [248, 2, 1, 255], + [209, 1, 0, 255], + [159, 1, 0, 255], + [110, 0, 0, 255], + [229, 125, 69, 255], + [179, 90, 39, 255], + [125, 60, 31, 255], + [ 84, 45, 19, 255], + [239, 33, 239, 255], + [200, 25, 200, 255], + [159, 18, 159, 255], + [120, 10, 120, 255], + [ 49, 2, 49, 255], + [200, 250, 250, 255], + [ 49, 2, 49, 255], + [ 0, 0, 0, 0]], dtype=np.uint8) + image_to_url(data, name,colormapD=lambda x: get_colorD(x,cmap,Min,Max,np), origin='upper') + + +def ProcessAllImage(ssp,model,Colorpalete,ExportDirectory,DataDirectory): + cc=0 + Var="DHW" + for i in ssp: + for j in model: + ff=DataDirectory+"%s_%s_%s_DHW.nc"%(Var,i,j) + for CM in Colorpalete: + for countyear in range(115): + nc = xr.open_dataset(ff, decode_coords="all") + year=int(nc.time[countyear].values) + Evaluado=ExportDirectory+"%s_%s"%(CM,ff.split("/")[-1].replace(".nc","_%s.png"%(year))) + if not os.path.isfile(Evaluado): + cc=cc+1 + + Total=cc + start=time.time() + cc=0 + for i in ssp: + for j in model: + ff=DataDirectory+"%s_%s_%s_DHW.nc"%(Var,i,j) + for CM in Colorpalete: + for countyear in range(115): + nc = xr.open_dataset(ff, decode_coords="all") + year=int(nc.time[countyear].values) + Evaluado=ExportDirectory+"%s_%s"%("noaa",ff.split("/")[-1].replace(".nc","_%s.png"%(year))) + if not os.path.isfile(Evaluado): + ExtractMapImage(ff,"noaa",countyear,Evaluado,nc) + cc=cc+1 + now=time.time() + print(cc,(start-now)/cc,(start-now)/cc*(Total-cc)) + +ssp=("ssp245","ssp370","ssp585") +model=("ensemble5","ensemble8","BCC-CSM2-MR","CESM2","CanESM5","EC-Earth3","IPSL-CM6A-LR","MIROC6","MRI-ESM2-0","NorESM2-MM") +Colorpalete=['CRW-NOAA'] +ExportDirectory="img/" +DataDirectory="../Data/" + +ProcessAllImage(ssp,model,Colorpalete,ExportDirectory,DataDirectory) diff --git a/2nc2image/requirimments.txt b/2nc2image/requirimments.txt new file mode 100644 index 0000000..a11eb04 --- /dev/null +++ b/2nc2image/requirimments.txt @@ -0,0 +1,54 @@ +affine==2.3.0 +attrs==21.4.0 +beautifulsoup4==4.10.0 +branca==0.4.2 +certifi==2021.10.8 +cftime==1.5.2 +charset-normalizer==2.0.10 +click==8.0.4 +click-plugins==1.1.1 +cligj==0.7.2 +contourpy==1.0.5 +cycler==0.11.0 +docopt==0.6.2 +folium==0.12.1.post1 +fonttools==4.37.4 +h5netcdf==0.14.0 +h5py==3.6.0 +idna==3.3 +imageio==2.16.1 +Jinja2==3.0.3 +joblib==1.1.0 +kiwisolver==1.3.2 +MarkupSafe==2.0.1 +matplotlib==3.6.0 +netCDF4==1.5.8 +networkx==2.7 +numpy==1.22.0 +packaging==21.3 +pandas==1.4.1 +Pillow==9.0.1 +plotly==5.6.0 +Pydap==3.2.2 +pyparsing==3.0.7 +pyproj==3.3.0 +python-dateutil==2.8.2 +pytz==2021.3 +PyWavelets==1.2.0 +rasterio==1.2.10 +requests==2.27.1 +rioxarray==0.12.2 +scikit-image==0.19.2 +scikit-learn==1.0.2 +scipy==1.8.0 +Shapely==1.8.4 +six==1.16.0 +sklearn==0.0 +snuggs==1.4.7 +soupsieve==2.3.1 +tenacity==8.0.1 +threadpoolctl==3.1.0 +tifffile==2022.2.9 +urllib3==1.26.8 +WebOb==1.8.7 +xarray==0.21.1 diff --git a/3image2video/0ColormapNoaa.py b/3image2video/0ColormapNoaa.py new file mode 100644 index 0000000..2b42b43 --- /dev/null +++ b/3image2video/0ColormapNoaa.py @@ -0,0 +1,134 @@ +import xarray as xr +from PIL import Image, ImageDraw, ImageFont +import os +import os +import moviepy.video.io.ImageSequenceClip + +def calculategetValuesColormap(nc2,year):#* + """ Calculate values of colormap + + Args: + nc (nc data): all layer of nc + year (int): year + + Returns: + Min (float): Minimum value + Max (float): Maximun value + q1 (float): q1 value + q50 (float): q50 value + q25 (float): q25 value + q75 (float): q75 value + q99 (float): q99 value + """ + import numpy as np + try: + q1=np.around(np.nanquantile(nc2[int(year)-1986,:,:].values, 0.01),2) + Min=np.around(np.nanmin(nc2[int(year)-1986,:,:].values),2) + Max=np.around(np.nanmax(nc2[int(year)-1986,:,:].values),2) + q99=np.around(np.nanquantile(nc2[int(year)-1986,:,:].values, 0.99),1) + q50= np.around((q1+q99)/2,2)#np.around(np.nanquantile(nc.DHW[int(year)-1987,:,:].values, 0.50),1) + q25= np.around((q1+q50)/2,2)#np.around(np.nanquantile(nc.DHW[int(year)-1987,:,:].values, 0.25),1) + q75= np.around((q50+q99)/2,2)#np.around(np.nanquantile(nc.DHW[int(year)-1987,:,:].values, 0.75),1) + except Exception: + pass + return Min,Max,q1,q50,q25,q75,q99 + +def textdraw(back_im,text,x,y,color,size=18,colormap=False): + draw = ImageDraw.Draw(back_im) + title_font = ImageFont.truetype('Roboto/Roboto-Regular.ttf', size) + textwidth, textheight = draw.textsize(str(text)) + #print(textwidth, textheight,text) + if colormap: + x=x-textheight + draw.text((x, y), str(text),color,title_font) + return back_im + +def ProcessAllImage(ssp,model,Colormap): + cc=0 + Var="DHW" + for i in ssp: + for j in model: + ff="../Data/%s_%s_%s_DHW.nc"%(Var,i,j) + print(ff) + nc = xr.open_dataset(ff, decode_coords="all") + for countyear in range(len(nc.time)): + + + #try: + year=int(nc.time[countyear].values) + path='./SinCoralN/%s_%s_%s_%s_DHW_%s.png'%(Colormap,Var,i,j,year) + isFile = os.path.isfile(path) + if isFile: + print(" Ya existe "+ path) + continue + #print(ff,year) + im1 = Image.open('./CapasJuntas_%s.png'%(Colormap)) + im2 = Image.open('../2nc2image/img/%s_%s_%s_%s_DHW_%s.png'%(Colormap,Var,i,j,year)) + + back_im = im1.copy() + back_im.paste(im2, (0, 27)) + #final2 = Image.new("RGBA", back_im.size) + #final2 = Image.alpha_composite(final2, back_im) + #im3 = Image.open("/home/mario/Documentos/Ocean/NetcdfToPng/CapasCoralJunta.png") + #final2 = Image.alpha_composite(final2, im3) + #back_im=final2 + im3 = Image.open("./FondoRosa.png") + final2 = Image.alpha_composite(im3,back_im) + back_im=final2 + x=10 + y=0 + back_im=textdraw(back_im,year,x,y+2,(0, 0, 0)) + #back_im.save('rocket_pillow_paste_pos.png', quality=95) + x=500 + y=0 + back_im=textdraw(back_im,i,x,y+2,(0, 0, 0)) + x=570 + y=0 + back_im=textdraw(back_im,j,x,y+2,(0, 0, 0)) + #back_im.save('rocket_pillow_paste_pos.png', quality=95) + + back_im.save('./SinCoralN/%s_%s_%s_%s_DHW_%s.png'%(Colormap,Var,i,j,year), quality=100) + + ListY=[] + path="Videos/Animated_%s_%s_%s_%s_OFF"%(Colormap,Var,i,j)+".webm" + isFile = os.path.isfile(path) + if isFile: + print(" Ya existe "+ path) + continue + for year in range(1986,2101): + ListY.append('./SinCoralN/%s_%s_%s_%s_DHW_%s.png'%(Colormap,Var,i,j,year)) + movie_clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(ListY, 2) + movie_clip.write_videofile("Videos/Animated_%s_%s_%s_%s_OFF"%(Colormap,Var,i,j)+".avi",codec="rawvideo") + + + + #except: + # print(3432) + #break + #break + #break + + +ssp=("ssp245","ssp370","ssp585") +#model=("ensemble5","ensemble8") +model=("ensemble5","ensemble8","BCC-CSM2-MR","CESM2","CanESM5","EC-Earth3","IPSL-CM6A-LR","MIROC6","MRI-ESM2-0","NorESM2-MM") +Colormaps=["noaa"] +for Colormap in Colormaps: + ProcessAllImage(ssp,model,Colormap) + + +import moviepy.video.io.ImageSequenceClip + +Var="DHW" +ssp=("ssp245","ssp370","ssp585") +model=("ensemble5","ensemble8","BCC-CSM2-MR","CESM2","CanESM5","EC-Earth3","IPSL-CM6A-LR","MIROC6","MRI-ESM2-0","NorESM2-MM") +Colormaps=["noaa"] +for Colormap in Colormaps: + for i in ssp: + for j in model: + ListY=[] + for year in range(1986,2101): + ListY.append('ConCoralN/%s_%s_%s_%s_DHW_%s.png'%(Colormap,Var,i,j,year)) + movie_clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(ListY, 2) + movie_clip.write_videofile("Videos/Animated_%s_%s_%s_%s_ON"%(Colormap,Var,i,j)+".webm") + diff --git a/3image2video/0colormap.py b/3image2video/0colormap.py new file mode 100644 index 0000000..4495d59 --- /dev/null +++ b/3image2video/0colormap.py @@ -0,0 +1,206 @@ +import xarray as xr +from PIL import Image, ImageDraw, ImageFont +import os +import moviepy.video.io.ImageSequenceClip + +def calculategetValuesColormap(nc2,year):#* + """ Calculate values of colormap + + Args: + nc (nc data): all layer of nc + year (int): year + + Returns: + Min (float): Minimum value + Max (float): Maximun value + q1 (float): q1 value + q50 (float): q50 value + q25 (float): q25 value + q75 (float): q75 value + q99 (float): q99 value + """ + import numpy as np + try: + q1=np.around(np.nanquantile(nc2[int(year)-1986,:,:].values, 0.01),2) + Min=np.around(np.nanmin(nc2[int(year)-1986,:,:].values),2) + Max=np.around(np.nanmax(nc2[int(year)-1986,:,:].values),2) + q99=np.around(np.nanquantile(nc2[int(year)-1986,:,:].values, 0.99),2) + q50= int(np.around((q1+q99)/2,2))#np.around(np.nanquantile(nc.DHW[int(year)-1987,:,:].values, 0.50),1) + q25= int(np.around((q1+q50)/2,2))#np.around(np.nanquantile(nc.DHW[int(year)-1987,:,:].values, 0.25),1) + q75= int(np.around((q50+q99)/2,2))#np.around(np.nanquantile(nc.DHW[int(year)-1987,:,:].values, 0.75),1) + if q99<10: + q99=8 + q1=0 + q25=2 + q50=4 + q75=6 + except Exception: + pass + return Min,Max,q1,q50,q25,q75,q99 + +def textdraw(back_im,text,x,y,color,size=18,colormap=False): + draw = ImageDraw.Draw(back_im) + title_font = ImageFont.truetype('Roboto/Roboto-Regular.ttf', size) + textwidth, textheight = draw.textsize(str(text)) + #print(textwidth, textheight,text) + if colormap: + x=x-textheight + draw.text((x, y), str(text),color,title_font) + return back_im + +def ProcessAllImage(ssp,model,Colormap): + cc=0 + Var="DHW" + for i in ssp: + for j in model: + + ff="../Data/%s_%s_%s_DHW.nc"%(Var,i,j) + + print(ff) + try: + print(ff) + nc = xr.open_dataset(ff, decode_coords="all") + except: + pass + for countyear in range(len(nc.time)): + + + #try: + nc2=nc.DHW_q99 + year=int(nc.time[countyear].values) + path='SinCoralN/%s_%s_%s_%s_DHW_%s.png'%(Colormap,Var,i,j,year) + isFile = os.path.isfile(path) + if isFile: + print(" Ya existe "+ path) + continue + + #print(ff,year) + Min,Max,q1,q50,q25,q75,q99=calculategetValuesColormap(nc2,year) + #print(Min,Max,q1,q50,q25,q75,q99) + print(year) + if q1<0.01: + q1=0.01 + im1 = Image.open('./CapasJuntas_%s.png'%(Colormap)) + im2 = Image.open('../2nc2image/img/%s_%s_%s_%s_DHW_%s.png'%(Colormap,Var,i,j,year)) + + back_im = im1.copy() + back_im.paste(im2, (0, 27)) + final2 = Image.new("RGBA", back_im.size) + final2 = Image.alpha_composite(final2, back_im) + im3 = Image.open("./FondoRosa.png") + final2 = Image.alpha_composite(im3,final2) + back_im=final2 + x=10 + y=0 + back_im=textdraw(back_im,year,x,y+2,(0, 0, 0)) + #back_im.save('rocket_pillow_paste_pos.png', quality=95) + x=500 + y=0 + back_im=textdraw(back_im,i,x,y+2,(0, 0, 0)) + x=570 + y=0 + back_im=textdraw(back_im,j,x,y+2,(0, 0, 0)) + #back_im.save('rocket_pillow_paste_pos.png', quality=95) + x=230 + y=197 + back_im=textdraw(back_im,"<"+str(q1),x,y,(205, 205, 205),12,True) + #back_im.save('rocket_pillow_paste_pos.png', quality=95) + x1=470#91 + back_im=textdraw(back_im,">"+str(q99),x1,y,(205, 205, 205),12,True) + x2=(x+x1)/2#53 + back_im=textdraw(back_im,str(q50),x2,y,(205, 205, 205),12,True) + x3=(x2+x1)/2 + back_im=textdraw(back_im,str(q75),x3,y,(205, 205, 205),12,True) + x4=(x+x2)/2 + back_im=textdraw(back_im,str(q25),x4,y,(205, 205, 205),12,True) + back_im.save('SinCoralN/%s_%s_%s_%s_DHW_%s.png'%(Colormap,Var,i,j,year), quality=95) + + + ListY=[] + path="Videos/Animated_%s_%s_%s_%s_OFF"%(Colormap,Var,i,j)+".webm" + isFile = os.path.isfile(path) + if isFile: + print(" Ya existe "+ path) + continue + for year in range(1986,2101): + ListY.append('./SinCoralN/%s_%s_%s_%s_DHW_%s.png'%(Colormap,Var,i,j,year)) + movie_clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(ListY, 2) + movie_clip.write_videofile("./Videos/Animated_%s_%s_%s_%s_OFF"%(Colormap,Var,i,j)+".webm") + + #except: + # print(3432) + #break + #break + #break + + +ssp=("ssp245","ssp370","ssp585") +model=("BCC-CSM2-MR","CESM2","CanESM5","EC-Earth3","IPSL-CM6A-LR","MIROC6","MRI-ESM2-0","NorESM2-MM","ensemble5","ensemble8",) +Colormaps=['Spectral','ocean',"coolwarm","RdYlBu"] +for Colormap in Colormaps: + ProcessAllImage(ssp,model,Colormap) + + +import xarray as xr +from PIL import Image, ImageDraw, ImageFont +import os + + +# for e in os.walk('./SinCoralN'): +# pass +# for file in e[2]: +# #print(file) + +# back_im = Image.open("./SinCoralN/"+file) + +# final2 = Image.new("RGBA", back_im.size) +# #print(back_im.size,final2.size) +# im1 = Image.open("./3CoralesOrig.png") +# final2.paste(im1, (0, 27)) +# #print(back_im.size,final2.size) + +# final2 = Image.alpha_composite(back_im,final2,) +# final2.save("./ConcoralN/"+file, quality=95) + +for e in os.walk('./SinCoralN'): + pass +for file in e[2]: + print(file) + isFile = os.path.isfile("./ConCoralN/"+file) + if isFile: + print(" Ya existe "+ "./ConCoralN/"+file) + continue + + back_im = Image.open("./SinCoralN/"+file) + + final2 = Image.new("RGBA", back_im.size) + #print(back_im.size,final2.size) + im1 = Image.open("./3CoralesOrig.png") + final2.paste(im1, (0, 27)) + #print(back_im.size,final2.size) + + final2 = Image.alpha_composite(back_im,final2,) + final2.save("./ConCoralN/"+file, quality=95) + + +import moviepy.video.io.ImageSequenceClip +Var="DHW" +ssp=("ssp245","ssp370","ssp585") +model=("BCC-CSM2-MR","CESM2","CanESM5","IPSL-CM6A-LR","MIROC6","NorESM2-MM","MRI-ESM2-0","EC-Earth3") +Colormaps=['Spectral','ocean',"coolwarm","RdYlBu"] +for Colormap in Colormaps: + for i in ssp: + for j in model: + ListY=[] + path="Videos/Animated_%s_%s_%s_%s_ON"%(Colormap,Var,i,j)+".webm" + isFile = os.path.isfile(path) + if isFile: + print(" Ya existe "+ path) + continue + for year in range(1986,2101): + ListY.append('./ConCoralN/%s_%s_%s_%s_DHW_%s.png'%(Colormap,Var,i,j,year)) + print("%s_%s_%s_%s_ON"%(Colormap,Var,i,j)) + movie_clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(ListY, 2) + movie_clip.write_videofile(path) + + diff --git a/3image2video/3CoralesOrig.png b/3image2video/3CoralesOrig.png new file mode 100644 index 0000000..0fd8468 Binary files /dev/null and b/3image2video/3CoralesOrig.png differ diff --git a/3image2video/CapasJuntas.png b/3image2video/CapasJuntas.png new file mode 100644 index 0000000..f70b625 Binary files /dev/null and b/3image2video/CapasJuntas.png differ diff --git a/3image2video/CapasJuntas_RdYlBu.png b/3image2video/CapasJuntas_RdYlBu.png new file mode 100644 index 0000000..1216ec6 Binary files /dev/null and b/3image2video/CapasJuntas_RdYlBu.png differ diff --git a/3image2video/CapasJuntas_Spectral.png b/3image2video/CapasJuntas_Spectral.png new file mode 100644 index 0000000..7d45d8c Binary files /dev/null and b/3image2video/CapasJuntas_Spectral.png differ diff --git a/3image2video/CapasJuntas_coolwarm.png b/3image2video/CapasJuntas_coolwarm.png new file mode 100644 index 0000000..bbf163b Binary files /dev/null and b/3image2video/CapasJuntas_coolwarm.png differ diff --git a/3image2video/CapasJuntas_noaa.png b/3image2video/CapasJuntas_noaa.png new file mode 100644 index 0000000..2ed2083 Binary files /dev/null and b/3image2video/CapasJuntas_noaa.png differ diff --git a/3image2video/CapasJuntas_ocean.png b/3image2video/CapasJuntas_ocean.png new file mode 100644 index 0000000..b9153e1 Binary files /dev/null and b/3image2video/CapasJuntas_ocean.png differ diff --git a/3image2video/FondoRosa.png b/3image2video/FondoRosa.png new file mode 100644 index 0000000..e24d87e Binary files /dev/null and b/3image2video/FondoRosa.png differ diff --git a/3image2video/Roboto/LICENSE.txt b/3image2video/Roboto/LICENSE.txt new file mode 100644 index 0000000..75b5248 --- /dev/null +++ b/3image2video/Roboto/LICENSE.txt @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/3image2video/Roboto/Roboto-Black.ttf b/3image2video/Roboto/Roboto-Black.ttf new file mode 100644 index 0000000..0112e7d Binary files /dev/null and b/3image2video/Roboto/Roboto-Black.ttf differ diff --git a/3image2video/Roboto/Roboto-BlackItalic.ttf b/3image2video/Roboto/Roboto-BlackItalic.ttf new file mode 100644 index 0000000..b2c6aca Binary files /dev/null and b/3image2video/Roboto/Roboto-BlackItalic.ttf differ diff --git a/3image2video/Roboto/Roboto-Bold.ttf b/3image2video/Roboto/Roboto-Bold.ttf new file mode 100644 index 0000000..43da14d Binary files /dev/null and b/3image2video/Roboto/Roboto-Bold.ttf differ diff --git a/3image2video/Roboto/Roboto-BoldItalic.ttf b/3image2video/Roboto/Roboto-BoldItalic.ttf new file mode 100644 index 0000000..bcfdab4 Binary files /dev/null and b/3image2video/Roboto/Roboto-BoldItalic.ttf differ diff --git a/3image2video/Roboto/Roboto-Italic.ttf b/3image2video/Roboto/Roboto-Italic.ttf new file mode 100644 index 0000000..1b5eaa3 Binary files /dev/null and b/3image2video/Roboto/Roboto-Italic.ttf differ diff --git a/3image2video/Roboto/Roboto-Light.ttf b/3image2video/Roboto/Roboto-Light.ttf new file mode 100644 index 0000000..e7307e7 Binary files /dev/null and b/3image2video/Roboto/Roboto-Light.ttf differ diff --git a/3image2video/Roboto/Roboto-LightItalic.ttf b/3image2video/Roboto/Roboto-LightItalic.ttf new file mode 100644 index 0000000..2d277af Binary files /dev/null and b/3image2video/Roboto/Roboto-LightItalic.ttf differ diff --git a/3image2video/Roboto/Roboto-Medium.ttf b/3image2video/Roboto/Roboto-Medium.ttf new file mode 100644 index 0000000..ac0f908 Binary files /dev/null and b/3image2video/Roboto/Roboto-Medium.ttf differ diff --git a/3image2video/Roboto/Roboto-MediumItalic.ttf b/3image2video/Roboto/Roboto-MediumItalic.ttf new file mode 100644 index 0000000..fc36a47 Binary files /dev/null and b/3image2video/Roboto/Roboto-MediumItalic.ttf differ diff --git a/3image2video/Roboto/Roboto-Regular.ttf b/3image2video/Roboto/Roboto-Regular.ttf new file mode 100644 index 0000000..ddf4bfa Binary files /dev/null and b/3image2video/Roboto/Roboto-Regular.ttf differ diff --git a/3image2video/Roboto/Roboto-Thin.ttf b/3image2video/Roboto/Roboto-Thin.ttf new file mode 100644 index 0000000..2e0dee6 Binary files /dev/null and b/3image2video/Roboto/Roboto-Thin.ttf differ diff --git a/3image2video/Roboto/Roboto-ThinItalic.ttf b/3image2video/Roboto/Roboto-ThinItalic.ttf new file mode 100644 index 0000000..084f9c0 Binary files /dev/null and b/3image2video/Roboto/Roboto-ThinItalic.ttf differ diff --git a/3image2video/colormapVideo.py b/3image2video/colormapVideo.py new file mode 100644 index 0000000..890f7c7 --- /dev/null +++ b/3image2video/colormapVideo.py @@ -0,0 +1,73 @@ +import xarray as xr +from PIL import Image, ImageDraw, ImageFont +import os +import moviepy.video.io.ImageSequenceClip + +def calculategetValuesColormap(nc2,year):#* + """ Calculate values of colormap + + Args: + nc (nc data): all layer of nc + year (int): year + + Returns: + Min (float): Minimum value + Max (float): Maximun value + q1 (float): q1 value + q50 (float): q50 value + q25 (float): q25 value + q75 (float): q75 value + q99 (float): q99 value + """ + import numpy as np + try: + q1=np.around(np.nanquantile(nc2[int(year)-1987,:,:].values, 0.01),2) + Min=np.around(np.nanmin(nc2[int(year)-1987,:,:].values),2) + Max=np.around(np.nanmax(nc2[int(year)-1987,:,:].values),2) + q99=np.around(np.nanquantile(nc2[int(year)-1987,:,:].values, 0.99),2) + q50= np.around((q1+q99)/2,2)#np.around(np.nanquantile(nc.DHW[int(year)-1987,:,:].values, 0.50),1) + q25= np.around((q1+q50)/2,2)#np.around(np.nanquantile(nc.DHW[int(year)-1987,:,:].values, 0.25),1) + q75= np.around((q50+q99)/2,2)#np.around(np.nanquantile(nc.DHW[int(year)-1987,:,:].values, 0.75),1) + if q99<10: + q99=8 + q1=0 + q25=2 + q50=4 + q75=6 + except Exception: + pass + return Min,Max,q1,q50,q25,q75,q99 + +def textdraw(back_im,text,x,y,color,size=18,colormap=False): + draw = ImageDraw.Draw(back_im) + title_font = ImageFont.truetype('Roboto/Roboto-Regular.ttf', size) + textwidth, textheight = draw.textsize(str(text)) + #print(textwidth, textheight,text) + if colormap: + x=x-textheight + draw.text((x, y), str(text),color,title_font) + return back_im + +def ProcessAllImage(ssp,model,Colormap): + cc=0 + Var="DHW" + for i in ssp: + for j in model: + ListY=[] + for year in range(1987,2101): + ListY.append('/home/mario/Documentos/Ocean/NetcdfToPng/SinCoral/%s_%s_%s_%s_DHW_%s.png'%(Colormap,Var,i,j,year)) + movie_clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(ListY, 2) + movie_clip.write_videofile("Videos/Animated_%s_%s_%s_%s_OFF"%(Colormap,Var,i,j)+".webm") + + #except: + # print(3432) + #break + #break + #break + + +ssp=("ssp245","ssp370","ssp585") +model=("BCC-CSM2-MR","CESM2","CanESM5","EC-Earth3","IPSL-CM6A-LR","MIROC6","MRI-ESM2-0","NorESM2-MM") +Colormaps=['Spectral','ocean',"coolwarm","RdYlBu"] +for Colormap in Colormaps: + ProcessAllImage(ssp,model,Colormap)