diff --git a/CAR2MAR_fire.py b/CAR2MAR_fire.py new file mode 100644 index 0000000000000000000000000000000000000000..435e6f52cf2a24efa7b330e459d2b698b7ec5d8b --- /dev/null +++ b/CAR2MAR_fire.py @@ -0,0 +1,160 @@ +# -*- coding: utf-8 -*- +""" +Created on Fri Apr 26 09:50:43 2024 + +@author: Thomas +""" + +# +# ---------------------------------------------------------------------------------------------------------------------- +# +# Title of script +# +# ---------------------------------------------------------------------------------------------------------------------- + + +# ---------------------------------------------------------------------------------------------------------------------- +# +# Imports and global variables +# \**********************************/ +# +import numpy as np + +import time + +import xarray as xr + +import os + +import sys + +import matplotlib.pyplot as plt + +from scipy.interpolate import griddata + +# ---------------------------------------------------------------------------------------------------------------------- +# +# Functions +# \***************/ +# +# +# Here you can define usefull functions to be used in the main +# +"""##############################################################################################################""" +def ti(): + return time.time() +"""##############################################################################################################""" + +def open_mar(path): + + return xr.open_mfdataset(path+"*.nc") + +"""##############################################################################################################""" + +def main_fire(path_to_fire, path_to_MAR): + + print("open data") + MAR = open_mar(path_to_MAR) + + lon_mar = MAR.LON.isel(time=0).values + lat_mar = MAR.LAT.isel(time=0).values + + lon_e, lat_e, data_e = read_data(path_to_fire) + + flash = interpol(lon_mar, lat_mar, lon_e, lat_e, data_e) + + write_file(lon_mar, lat_mar, flash) + +"""##############################################################################################################""" + +def read_data(path_to_fire): + + with open(path_to_fire, "r") as f: + full = f.read() + + cleaned = full.replace(" "," ").replace(" "," ").replace("\n"," ").split(" ") + cleaned = np.array(cleaned) + + cleaned_array = cleaned[cleaned != ''].astype(float) + + reshaped = cleaned_array.reshape(-1,12)*365/12 + + lat = np.arange(-90,90,0.5) + lon = np.arange(-180,180,0.5) + + # Use meshgrid to create the grid of coordinates + lon_grid, lat_grid = np.meshgrid(lon, lat) + + + monthly_values = reshaped + + # Reshape monthly values to (360, 720, 12) + reshaped_data = monthly_values.reshape((360, 720, 12)) + + reshaped_lon = lon_grid.reshape((360, 720)) + + reshaped_lat = lat_grid.reshape((360, 720)) + + return reshaped_lon, reshaped_lat, reshaped_data + + +"""##############################################################################################################""" + +def interpol(lon_m, lat_m, lon_e, lat_e, data_e): + + shape_to_get = np.shape(lon_m) + + flash = np.zeros((shape_to_get+ (12,))) + + for month in range(data_e.shape[2]): + + month_data = data_e[:, :, month] + + interpolated_att1 = griddata( + (lon_e.flatten(), lat_e.flatten()), + month_data.flatten(), + (lon_m.flatten(), lat_m.flatten()), + method='linear' + ) + + interpolated_att1 = interpolated_att1.reshape(shape_to_get) + + flash[:,:,month] = interpolated_att1 + + return flash + +def write_file(lon_m, lat_m, data): + var_values = data + + print("Start writing the file") + for i in np.arange(0,np.shape(lon_m)[1]): + for j in np.arange(0,np.shape(lat_m)[0]): + + + lo = lon_m[j,i] + la = lat_m[j,i] + + va = var_values[j,i,:] + + f = open("lightning.dat", "a") + text =f"{lo:8.3f} {la:8.3f}" +"\t" + "\t".join(f"{x:8.3E}" for x in va) + "\n" + f.write(text) + f.close() + +# Main +# \**********/ +# +# +# Here you can define the instructions that are called when you execute this file +# +if __name__ == '__main__': + + t0=ti() + + path_to_fire = "E:/Car_files/data.ascii" + path_to_MAR = "E:/MAR_data_raw_BE/MERRA/" + + main_fire(path_to_fire,path_to_MAR) + + t1=ti() + print('Programme compiled in ',t1-t0, 'sec') \ No newline at end of file diff --git a/CAR2MAR_manage.py b/CAR2MAR_manage.py new file mode 100644 index 0000000000000000000000000000000000000000..a479ec4461e59531efb382fe53d85eb9410a1e7c --- /dev/null +++ b/CAR2MAR_manage.py @@ -0,0 +1,120 @@ +# -*- coding: utf-8 -*- +""" +Created on Fri Apr 26 09:50:43 2024 + +@author: Thomas +""" + +# +# ---------------------------------------------------------------------------------------------------------------------- +# +# Create management file for CARAIB +# +# ---------------------------------------------------------------------------------------------------------------------- + + +# ---------------------------------------------------------------------------------------------------------------------- +# +# Imports and global variables +# \**********************************/ +# +import numpy as np + +import time + +import xarray as xr + +import os + +import sys + +import matplotlib.pyplot as plt + +from scipy.interpolate import griddata +import calendar + +# ---------------------------------------------------------------------------------------------------------------------- +# +# Functions +# \***************/ +# +# +# Here you can define usefull functions to be used in the main +# +"""##############################################################################################################""" +def ti(): + return time.time() +"""##############################################################################################################""" + +def open_mar(path): + + return xr.open_mfdataset(path+"*.nc") + +"""##############################################################################################################""" + +def main_manag(path_to_manag, path_to_MAR, manag_type,ys,ye): + + print("open data") + MAR = open_mar(path_to_MAR) + + lon_mar = MAR.LON.isel(time=0).values + lat_mar = MAR.LAT.isel(time=0).values + + + write_file(lon_mar, lat_mar, path_to_manag, manag_type,ys,ye) + +"""##############################################################################################################""" + + +def write_file(lon_m, lat_m, path_to_manag, manag_type,ys,ye): + + if not os.path.isdir(path_to_manag): + os.mkdir(path_to_manag) + + for yy in np.arange(ys,ye+1,1): + + va = manag_type + + if calendar.isleap(yy): + if manag_type[2]==365: + va[2]=366 + + + print("Start writing the file") + for i in np.arange(0,np.shape(lon_m)[1]): + for j in np.arange(0,np.shape(lat_m)[0]): + + + lo = lon_m[j,i] + la = lat_m[j,i] + + f = open(path_to_manag+f"manag{yy}.dat", "a") + text =f"{lo:8.3f} {la:8.3f}" + for x in np.arange(0,10,1): + text = text + "\t"+ "\t".join(str(y) for y in va[0:3]) + "\t"+ "\t".join(str(round(float(y),2)) for y in va[3:]) + + text = text + "\n" + f.write(text) + f.close() + +# Main +# \**********/ +# +# +# Here you can define the instructions that are called when you execute this file +# +if __name__ == '__main__': + + t0=ti() + + path_to_manag = "./man/" + path_to_MAR = "E:/MAR_data_raw_BE/MERRA/" + manage_type = [2,1,365,0,0] #full cut + # manage_type = [1,1,150,50,50] #user defined + ys = 2009 #starting year -1 + ye = 2020 #end year + + main_manag(path_to_manag, path_to_MAR, manage_type,ys,ye) + + t1=ti() + print('Programme compiled in ',t1-t0, 'sec') \ No newline at end of file diff --git a/MAR2CAR_clim.py b/MAR2CAR_clim.py new file mode 100644 index 0000000000000000000000000000000000000000..f05736da387e1f5d8e5bf37d87566f175396c14c --- /dev/null +++ b/MAR2CAR_clim.py @@ -0,0 +1,153 @@ +# -*- coding: utf-8 -*- +""" +Created on Fri Apr 26 09:50:43 2024 + +@author: Thomas +""" + +# +# ---------------------------------------------------------------------------------------------------------------------- +# +# Title of script +# +# ---------------------------------------------------------------------------------------------------------------------- + + +# ---------------------------------------------------------------------------------------------------------------------- +# +# Imports and global variables +# \**********************************/ +# +import numpy as np + +import time + +import xarray as xr + +import os + +import sys + +import matplotlib.pyplot as plt + + +# ---------------------------------------------------------------------------------------------------------------------- +# +# Functions +# \***************/ +# +# +# Here you can define usefull functions to be used in the main +# +"""##############################################################################################################""" +def ti(): + return time.time() +"""##############################################################################################################""" + +def open_mar(path): + + return xr.open_mfdataset(path+"*.nc") + +"""##############################################################################################################""" + +def group_per_day(sa): + + return sa.groupby("time.dayofyear").mean() + +"""##############################################################################################################""" + +def resample(sa,var): + + dico = {"TTmin":sa.resample(time='1D').min(), + "TTmax":sa.resample(time='1D').max(), + "TT":sa.resample(time='1D').mean(), + "MBrr":sa.resample(time='1D').sum(), + "RH":sa.resample(time='1D').mean(), + "UV":sa.resample(time='1D').mean(), + "SWD":sa.resample(time='1D').mean() + } + + return dico[var]#.get(var,print("no good key")) + +"""##############################################################################################################""" + +def get_name(var): + + dico = {"TTmin":"tminyyyy", + "TTmax":"tmaxyyyy", + "TT":"temyyyy", + "MBrr":"prcyyyy", + "RH":"ruhyyyy", + "UV":"wndyyyy", + "SWD":"fsolyyyy" + } + + return dico[var]#.get(var,print("no good key")) + +"""##############################################################################################################""" + +def write_clim(sa,longitude,latitude,var): + + var_values = sa.values + name_of_file = get_name(var) + + print("Start writing the file") + for i in np.arange(0,np.shape(longitude)[1]): + for j in np.arange(0,np.shape(latitude)[0]): + + + lo = longitude[j,i] + la = latitude[j,i] + + try: + va = var_values[:,0,j,i] + except: + va = var_values[:,j,i] + + f = open(name_of_file+".dat", "a") + text = str(round(lo,3))+'\t' + str(round(la,3)) +'\t'+ '\t'.join(str(round(x,3)) for x in va) + "\n" + f.write(text) + f.close() + +"""##############################################################################################################""" + +def main_climatology(path_to_MAR): + + variable_to_clim = ["MBrr","RH","UV","SWD","TTmin","TTmax","TT"] + + print("open data") + MAR = open_mar(path_to_MAR) + + lon = MAR.LON.isel(time=0).values + lat = MAR.LAT.isel(time=0).values + + for var in variable_to_clim: + + print("Extract", var) + MAR_var = MAR[var] + + print("Resample to daily") + MAR_daily = resample(MAR_var,var) + + print("Group by doy") + MAR_clim = group_per_day(MAR_var) + + print("Write climatology") + write_clim(MAR_clim,lon,lat,var) + + +# Main +# \**********/ +# +# +# Here you can define the instructions that are called when you execute this file +# +if __name__ == '__main__': + t0=ti() + + path_to_MAR = "F:/MAR_data_raw_BE/MERRA/" + + main_climatology(path_to_MAR) + + t1=ti() + print('Programme compiled in ',t1-t0, 'sec') \ No newline at end of file diff --git a/MAR2CAR_landuse.py b/MAR2CAR_landuse.py new file mode 100644 index 0000000000000000000000000000000000000000..f1998e2cbb5b74aced84577c686fc1bdea7187d6 --- /dev/null +++ b/MAR2CAR_landuse.py @@ -0,0 +1,133 @@ +# -*- coding: utf-8 -*- +""" +Created on Mon Jul 10 14:44:05 2023 + +@author: Thomas +""" + +# +# ---------------------------------------------------------------------------------------------------------------------- +# +# Title of script +# +# ---------------------------------------------------------------------------------------------------------------------- + + + +# ---------------------------------------------------------------------------------------------------------------------- +# +# Imports and global variables +# \**********************************/ +# +import numpy as np + +import time + +import xarray as xr + +import os + +import sys +import matplotlib.pyplot as plt +# ---------------------------------------------------------------------------------------------------------------------- +# +# Functions +# \***************/ +# +# +# Here you can define usefull functions to be used in the main +# +"""##############################################################################################################""" +def convert(x): + converteur = {0:5, # Barren soil + 1:1, # Crops low + 2:1, # Crops medium + 3:1, # Crops high + 4:2, # Grass low + 5:2, # Grass medium + 6:2, # Grass high + 7:0, # Broadleaf low + 8:0, # Broadleaf medium + 9:0, # Broadleaf high + 10:0, # Needleleaf low + 11:0, # Needleleaf medium + 12:0, # Needleleaf high + 13:3, # City + } + + + return converteur[x] + + +def main_landuse(path_to_mar): + + mar = xr.open_mfdataset(path_to_mar+"*.nc") + + mar = mar.isel(time=1) + + longitude = mar.LON.values + latitude = mar.LAT.values + + veg = mar.VEG.values + + frac = mar.FRV.values + + sect = [0,1,2] + + f = open('./land_use_caraib.dat', "w") + + print(range(len(mar.x.values))) + for i in range(len(mar.x.values)): + for j in range(len(mar.y.values)): + + veg_car = np.zeros(6) + + lo = longitude[j,i] + la = latitude[j,i] + + new_lu = [convert(veg[x,j,i]) for x in sect] + + new_frv = [frac[x,j,i]/100 for x in sect] + + if not np.isclose(np.sum(new_frv),1,atol=0.0001): + print('error') + print(np.sum(new_frv)) + new_frv = [(frac[x,j,i]/100)/np.sum(frac[x,j,i]/100) for x in sect] + print(new_frv) + sys.exit() + + for k,values in enumerate(new_lu): + + veg_car[values]= veg_car[values] + new_frv[k] + + + + veg_car = np.round(veg_car,2) + + if not np.isclose(np.sum(veg_car),1,atol=0.0001): + difference = 1 - np.sum(veg_car) + max_index = np.argmax(veg_car) + veg_car[max_index] += difference + + # print(lo,la) + # sys.exit() + text ='\t' + '{:.4f}'.format(round(lo,4))+'\t' + '{:.4f}'.format(round(la,4)) +'\t'+ '\t'.join('{:.2f}'.format(round(x,2)) for x in veg_car) + "\n" + f.write(text) + + f.close() + +# Main +# \**********/ +# +# +# Here you can define the instructions that are called when you execute this file +# +if __name__ == '__main__': + t0=time.time() + + path_to_mar = "E:/MAR_data_raw_BE/MERRA/" + + main_landuse(path_to_mar) + + t1=time.time() + print('Programme compiled in ',t1-t0, 'sec') \ No newline at end of file diff --git a/create_necessary_files.py b/create_necessary_files.py new file mode 100644 index 0000000000000000000000000000000000000000..55fb733096ac9b1728d5add79e0f8a894953ed09 --- /dev/null +++ b/create_necessary_files.py @@ -0,0 +1,109 @@ +# -*- coding: utf-8 -*- +""" +Created on Wed Oct 18 11:15:29 2023 + +@author: Thomas +""" + +# +# ---------------------------------------------------------------------------------------------------------------------- +# +# Call all the necessary routine to interpolate the MAR file to CAR or the opposite +# +# ---------------------------------------------------------------------------------------------------------------------- + + +# ---------------------------------------------------------------------------------------------------------------------- +# +# Imports and global variables +# \**********************************/ +# + +import time + +from MAR2CAR_clim import main_climatology + +from MAR2CAR_landuse import main_landuse + +from CAR2MAR_sowing import main_sowing + +from CAR2MAR_fire import main_fire + +from CAR2MAR_manage import main_manage + +from CAR2MAR_co2_tprev import main_co2, main_tprev + +# ---------------------------------------------------------------------------------------------------------------------- +# +# Functions +# \***************/ +# +# +# Here you can define usefull functions to be used in the main +# +"""##############################################################################################################""" +def ti(): + return time.time() +"""##############################################################################################################""" + +# Main +# \**********/ +# +# +# Here you can define the instructions that are called when you execute this file +# +if __name__ == '__main__': + t0=ti() + + path_to_MAR_ICE = "E:/MAR_data_raw_BE/MERRA/" + + path_to_sowing = "E:/MAR_data_raw_BE/MERRA/" + path_to_fire = "E:/Car_files/data.ascii" #data.ascii input file + path_to_manage = "./man/" #manageYYYY.dat file output + + do_sowing = 1 + do_fire = 1 + do_manage = 1 + + + # manage_type = [0,1,1,100,100] #no cut + manage_type = [2,1,365,0,0] #full cut + # manage_type = [1,1,150,50,50] #user defined + ys = 2009 #starting year -1 + ye = 2020 #end year + + print("Creating the climatology files") + main_climatology(path_to_MAR_ICE) + + print("Creating the Landuse files") + main_landuse(path_to_MAR_ICE) + + print("Creating the C02 files") + main_co2(ys,ye) + + print("Creating the Temprev") + main_tprev(ys) + + print("Creating the sowing files") + if do_sowing : main_sowing(path_to_sowing, path_to_MAR_ICE) + + print("Creating the fire files") + if do_fire : main_fire(path_to_fire, path_to_MAR_ICE) + + print("Creating the manage files") + if do_manage : main_manage(path_to_manage, path_to_MAR_ICE, manage_type,ys,ye) + + + + + + + + + + + + print("All scripts have completed.") + + t1=ti() + print('Programme compiled in ',t1-t0, 'sec') \ No newline at end of file