Skip to content

Commit

Permalink
add meteo
Browse files Browse the repository at this point in the history
  • Loading branch information
cfontana00 committed Apr 9, 2024
1 parent 2b9e4de commit dc48ed6
Show file tree
Hide file tree
Showing 7 changed files with 441 additions and 5 deletions.
7 changes: 7 additions & 0 deletions config/config_TEMPLATE.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@


"_____":"METEO PARAMETERS",
"meteo_ini":"2015-03-10",
"meteo_end":"2015-03-11",
"meteodir":"/path/to/meteo/files",
"imin":"500",
"imax":"600",
"jmin":"350",
"jmax":"450",



"_____":"DYFAMED PARAMETERS",
Expand Down
Binary file added meteo/ECMWF/.process_data.py.swp
Binary file not shown.
134 changes: 134 additions & 0 deletions meteo/ECMWF/get_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# --------------------------------#
# Download ECMWF atmospheric data #
# --------------------------------#
import warnings
warnings.filterwarnings("ignore")

import sys,os
import numpy as np
import cdsapi
from fun_gen import *
from fun_io import *


# Get args
# --------
config = sys.argv[1] # Configuration name

# Load config file
# ----------------
load_config(config)
from fun_gen import *


# Get args
# --------
config=sys.argv[1] # Configuration name


# Load lon/lat
# ------------
lon,lat,levels = load_coords()

yini,mini,dini = meteo_ini.split('-')
yend,mend,dend = meteo_end.split('-')

yini,mini = int(yini),int(mini)
yend,mend = int(yend),int(mend)


name = 'reanalysis-cerra-single-levels'

# Create output directory
# -----------------------
os.system('mkdir -p '+diagdir+'/'+config)
savedir= diagdir+'/'+config+'/METEO'
os.system('mkdir -p '+savedir)


latmax,latmin = str(np.amax(lat)+1),str(np.amin(lat)-1)
lonmax,lonmin = str(np.amax(lon)+1),str(np.amin(lon)-1)

area = "'"+latmax+"/"+lonmin+"/"+latmin+"/"+lonmax+"'"


c = cdsapi.Client()


# Loop on years
# -------------
for year in range(int(yini),int(yend)+1):

# Define months array
if yini == yend :
months = range(mini,mend+1)
else :
if year == yini :
months = range(mini,13)
elif year == yend :
months = range(1,mend+1)


for mon in range(np.amin(months),np.amax(months)+1):

mon = str(mon).zfill(2)


'''
'10m_wind_direction', '10m_wind_speed', '2m_relative_humidity',
'surface_latent_heat_flux', 'surface_net_solar_radiation', 'surface_net_thermal_radiation',
'surface_pressure', 'surface_sensible_heat_flux', 'surface_solar_radiation_downwards',
'surface_thermal_radiation_downwards', 'time_integrated_surface_direct_short_wave_radiation_flux', 'total_cloud_cover',
'2m_temperature', 'evaporation', 'mean_sea_level_pressure',
'total_precipitation',
'''

for var in [ '10m_wind_direction', '10m_wind_speed', '2m_relative_humidity','surface_latent_heat_flux', 'surface_net_solar_radiation', 'surface_net_thermal_radiation','surface_pressure', 'surface_sensible_heat_flux', 'surface_solar_radiation_downwards','surface_thermal_radiation_downwards', 'time_integrated_surface_direct_short_wave_radiation_flux', 'total_cloud_cover','2m_temperature', 'evaporation', 'mean_sea_level_pressure','total_precipitation', '2m_temperature']:



oname = savedir+'/'+name+'_'+str(year)+mon+'_'+var+'.nc'


c.retrieve(
name,
{
'variable': var ,
'level_type': 'surface_or_atmosphere',
'data_type': 'reanalysis',
'product_type': 'forecast',
'year': str(year),
'month': mon,
'day': [
'01', '02', '03',
'04', '05', '06',
'07', '08', '09',
'10', '11', '12',
'13', '14', '15',
'16', '17', '18',
'19', '20', '21',
'22', '23', '24',
'25', '26', '27',
'28', '29', '30',
'31',
],
'time': [
'00:00', '03:00', '06:00',
'09:00', '12:00', '15:00',
'18:00', '21:00',
],
'leadtime_hour': '1',
'format': 'netcdf',
},
oname)


# Crop file
# ---------
os.system('ncks -O -d x,'+imin+','+imax+' -d y,'+jmin+','+jmax+' '+oname+' '+oname)

print('\n[DOWNLOADED]',oname,'\n')


196 changes: 196 additions & 0 deletions meteo/ECMWF/process_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
# --------------------------------#
# Download ECMWF atmospheric data #
# --------------------------------#
import warnings
warnings.filterwarnings("ignore")

import sys,os
from glob import glob
import numpy as np
import datetime as dt
import cdsapi
from fun_gen import *
from fun_io import *
import xarray as xr
from scipy.interpolate import griddata
from netCDF4 import Dataset



# Get args
# --------
config = sys.argv[1] # Configuration name

# Load config file
# ----------------
load_config(config)
from fun_gen import *


# Get args
# --------
config=sys.argv[1] # Configuration name

indir = diagdir+'/'+config+'/METEO'

ncdir = diagdir+'/'+config+'/METEO/ITP_NC'
os.system('mkdir -p '+ncdir)


# Load lon/lat
# ------------
lon,lat,levels = load_coords()


# Define parameters
# -----------------
mdini = dt.datetime.strptime(meteo_ini,'%Y-%m-%d').toordinal()
mdend = dt.datetime.strptime(meteo_end,'%Y-%m-%d').toordinal()

name = 'reanalysis-cerra-single-levels'

tags = [] # File tag Variable name 'Units' Output tag
tags.append(['2m_temperature', 't2m' , 'degK' ,'tag' ])
#tags.append(['2m_relative_humidity', 'r2' , ,'tag' ])
#tags.append(['surface_net_solar_radiation', 'ssr' , 'W.m-2' , 'tag' ])

# Load meteo lon/lat
# ------------------
fname = glob(indir+'/*2m_temperature*')[0]
ds = xr.open_dataset(fname)
met_lon = np.array(ds['longitude']).flatten()
met_lat = np.array(ds['latitude']).flatten()
ds.close()

# Mesh model lon/lat
LAT,LON = np.meshgrid(lat,lon)



print('Processing data :\n')

switch = 0


# Initialize NetCDF file for writing
# -----------------------------------
meteo_ini = meteo_ini.replace('-','')
meteo_end = meteo_end.replace('-','')

fname = name + '_'+ meteo_ini +'_'\
+ meteo_end +'.nc'

# Open file
dataset = Dataset(ncdir+'/'+fname,'w',format='NETCDF3_CLASSIC')

# Create dimension
dlon = dataset.createDimension('longitude',lon.shape[0])
dlat = dataset.createDimension('latitude',lat.shape[0])
dtime = dataset.createDimension('time',(mdend-mdini+1)*8)

# Create variables & set attributes
vlon = dataset.createVariable('longitude',np.float32,('longitude'))
vlon.units = "degrees east"
vlon.long_name = "longitude"
vlon[:] = lon

vlat = dataset.createVariable('latitude',np.float32,('latitude'))
vlat.units = "degrees north"
vlat.long_name = "latitude"
vlat[:] = lat

vtime = dataset.createVariable('time',np.float32,('time'))
vtime.units = "hours since 1900-01-01 00:00:00"
vtime.calendar = "gregorian"
vtime[:] = range(0,(mdend-mdini+1)*8)



# Process standard variables
# --------------------------
for tag in tags:

ftag = tag[0]
var = tag[1]
otag = tag[2]
units = tag[3]

print('=>',ftag)

full_data = []


# Loop on days
# ------------
for jd in range(mdini,mdend+1):

date = dt.datetime.fromordinal(jd)
print(date.strftime('%Y-%m-%d'))

year,mon,day = date.strftime('%Y'),date.strftime('%m'),date.strftime('%d')

# Load variable
fname = indir+'/'+name+'_'+str(year)+mon+'_'+ftag+'.nc'
ds = xr.open_dataset(fname)

# Loop on records
for rec in range(0,8):

data = np.array(ds[var])[8*int(day)+rec,:,:]

# Interpolate on model grid
data = data.flatten()
idata = griddata((met_lat,met_lon),data,(LAT,LON),method='linear')
full_data.append(idata.T)

full_data = np.array(full_data)
ds.close()


# Specific tuning
#if tag[0] == 'surface_net_solar_radiation':
# full_data = full_data/3600.
# print('HERE')


# Write variable to NetCDF
# ------------------------
vdata = dataset.createVariable(var,np.float32,('time','latitude','longitude'),fill_value=-9999)
vdata.units = units
print(var)
vlat.long_name = ds[var].GRIB_name

vdata[:] = full_data



dataset.close()





















'''
for var in [ '10m_wind_direction', '10m_wind_speed', '2m_relative_humidity','surface_latent_heat_flux', 'surface_net_solar_radiation', 'surface_net_thermal_radiation','surface_pressure', 'surface_sensible_heat_flux', 'surface_solar_radiation_downwards','surface_thermal_radiation_downwards', 'time_integrated_surface_direct_short_wave_radiation_flux', 'total_cloud_cover','2m_temperature', 'evaporation', 'mean_sea_level_pressure','total_precipitation', '2m_temperature']:
'''


Loading

0 comments on commit dc48ed6

Please sign in to comment.