-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathERA5PrecipitationtoDelft3D
101 lines (80 loc) · 3.85 KB
/
ERA5PrecipitationtoDelft3D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
Created on Wed Jun 19 2024
@author: Faezah Maghsoodifar
This script reads precipitation data from a netCDF file generated by the ERA5 dataset.
It processes the data by flipping the precipitation values along the latitude axis
and adjusting longitude values to fit within the [-180, 180] range. The script saves
the processed data into a new netCDF file in the NETCDF4 format. The time units are
preserved to ensure correct dates.
"""
import os
import numpy as np
from netCDF4 import Dataset
def process_netcdf(input_filepath, output_filepath):
"""
Process the input netCDF file and write the required data to a new netCDF file.
Parameters:
input_filepath (str): Path to the input netCDF file.
output_filepath (str): Path to save the output netCDF file.
"""
try:
# Open the input netCDF file in read mode
with Dataset(input_filepath, 'r') as ncid0:
# Read longitude values from the netCDF file
lon = ncid0.variables['longitude'][:]
lon[lon >= 180] = lon[lon >= 180] - 360 # Adjust longitudes to [-180, 180] range
lon = np.sort(lon) # Sort longitudes
# Read latitude values
lat = ncid0.variables['latitude'][:]
# Read time values and their original units
time = ncid0.variables['valid_time'][:]
time_units = ncid0.variables['valid_time'].units # Fetch original units
# Read total precipitation values and flip along the latitude axis
Tprec = ncid0.variables['tp'][:, :, :]
Tprec_flipped = np.flip(Tprec, axis=1)
# Create the output netCDF file in write mode
with Dataset(output_filepath, 'w', format='NETCDF4') as ncid:
# Define dimensions
ncid.createDimension('valid_time', len(time))
ncid.createDimension('y', len(lat))
ncid.createDimension('x', len(lon))
# Define and write longitude variable
var_lon = ncid.createVariable('X', 'f8', ('x',))
var_lon.units = 'degrees_east'
var_lon.standard_name = 'longitude'
var_lon[:] = lon
# Define and write latitude variable
var_lat = ncid.createVariable('Y', 'f8', ('y',))
var_lat.units = 'degrees_north'
var_lat.standard_name = 'latitude'
var_lat[:] = lat
# Define and write time variable
var_time = ncid.createVariable('time', 'f8', ('valid_time',))
var_time.units = time_units # Preserve original time units
var_time.calendar = 'gregorian'
var_time.standard_name = 'time'
var_time.long_name = 'Time'
var_time[:] = time
# Define and write total precipitation (rainfall)
var_tprec = ncid.createVariable('rainfall', 'f4', ('valid_time', 'y', 'x'))
var_tprec.units = 'm'
var_tprec.long_name = 'rainfall'
var_tprec.standard_name = 'rainfall'
var_tprec[:, :, :] = Tprec_flipped
print("NetCDF file successfully created:", output_filepath)
except Exception as e:
print(f"An error occurred: {e}")
def main():
"""
Main function to set paths and process the netCDF file.
"""
# USER-DEFINED INPUT: Set input/output paths
dinput = input("Enter the input directory path: ").strip() # Input directory
doutput = input("Enter the output directory path: ").strip() # Output directory
filename = 'data_stream-oper.nc' # Input file name
input_filepath = os.path.join(dinput, filename)
output_filepath = os.path.join(doutput, filename.replace('.nc', '_dfm.nc'))
# Process the netCDF file
process_netcdf(input_filepath, output_filepath)
if __name__ == "__main__":
main()