-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_a_netcdf_example.py
More file actions
64 lines (39 loc) · 1.61 KB
/
load_a_netcdf_example.py
File metadata and controls
64 lines (39 loc) · 1.61 KB
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
import numpy as np
from netCDF4 import Dataset
def load_weather_data_daily(data_dir,filename,nc_key):
"""
This function takes the ERA5 reanalysis data and loads it.
Args:
data_dir (str): The parth for where the data is stored.
e.g '/home/users/zd907959/'
filename (str): The filename of a .netcdf file
e.g. 'ERA5_1979_01.nc'
nc_key (str): The string you need to load the .nc data
e.g. 't2m','rsds'
Returns:
weather_data (array): Country-masked daily weather data,
dimensions
[time,latitude,longitude] where there are 0's in locations where the data is
not within the country border.
lat_data (array): Dimensions [latitude] The latitudes of the array
lon_data (array): Dimensions [longitude] The longitudes of the array
"""
# load in the data you wish to mask
file_str = data_dir + filename
dataset = Dataset(file_str,mode='r')
lons = dataset.variables['longitude'][:]
lats = dataset.variables['latitude'][:]
data = dataset.variables[nc_key][:] # data in shape [time,lat,lon]
dataset.close()
# get data in appropriate units for models
if nc_key == 't2m':
data = data-273.15 # convert to Kelvin from Celsius
if nc_key == 'ssrd':
data = data/3600. # convert Jh-1m-2 to Wm-2
return(data,lons,lats)
##############
#
# Main code
#
##############
ERA5_data_jan_1979,lats,lons = load_weather_data_daily('/storage/silver/S2S4E/energymet/ERA5/native_grid_hourly/','ERA5_1hr_1979_01_DET.nc','t2m')