Skip to content
Snippets Groups Projects
Commit 910764ae authored by Dethinne Thomas's avatar Dethinne Thomas
Browse files

Upload New File

parent cc2afc62
No related branches found
No related tags found
No related merge requests found
# -*- 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)
"""##############################################################################################################"""
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(lon)[1]):
for j in np.arange(0,np.shape(lon)[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()
"""##############################################################################################################"""
# Main
# \**********/
#
#
# Here you can define the instructions that are called when you execute this file
#
if __name__ == '__main__':
t0=ti()
variable_to_clim = ["MBrr","RH","UV","SWD"] #"TTmin","TTmax","TT",
path_to_MAR= "F:/MAR_data_raw_BE/MERRA/*.nc"
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)
# sys.exit()
t1=ti()
print('Programme compiled in ',t1-t0, 'sec')
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment