-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRAMS_functions.py
More file actions
105 lines (83 loc) · 3.21 KB
/
RAMS_functions.py
File metadata and controls
105 lines (83 loc) · 3.21 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
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
102
103
104
105
import numpy as np
import numpy.ma as ma
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.colors as colors
import datetime
import sys
import glob
import os
import hdf5plugin
import h5py
# have to install h5netcdf for xarray to read .h5 files
import pandas as pd
import xarray as xr
import cartopy.crs as crs
import csv
import random
from multiprocessing import Pool, cpu_count
import os
import time
def read_head(headfile,h5file):
# Function that reads header files from RAMS
# Inputs:
# headfile: header file including full path in str format
# h5file: h5 datafile including full path in str format
# Returns:
# zmn: height levels for momentum values (i.e., grid box upper and lower levels)
# ztn: height levels for thermodynaic values (i.e., grid box centers)
# nx:: the number of x points for the domain associated with the h5file
# ny: the number of y points for the domain associated with the h5file
# npa: the number of surface patches
dom_num = h5file[h5file.index('.h5')-1] # Find index of .h5 to determine position showing which nest domain to use
with open(headfile) as f:
contents = f.readlines()
idx_zmn = contents.index('__zmn0'+dom_num+'\n')
nz_m = int(contents[idx_zmn+1])
zmn = np.zeros(nz_m)
for i in np.arange(0,nz_m):
zmn[i] = float(contents[idx_zmn+2+i])
idx_ztn = contents.index('__ztn0'+dom_num+'\n')
nz_t = int(contents[idx_ztn+1])
ztn = np.zeros(nz_t)
for i in np.arange(0,nz_t):
ztn[i] = float(contents[idx_ztn+2+i])
ztop = np.max(ztn) # Model domain top (m)
# Grad the size of the horizontal grid spacing
idx_dxy = contents.index('__deltaxn\n')
dxy = float(contents[idx_dxy+1+int(dom_num)].strip())
idx_npatch = contents.index('__npatch\n')
npa = int(contents[idx_npatch+2])
idx_ny = contents.index('__nnyp\n')
idx_nx = contents.index('__nnxp\n')
ny = np.ones(int(contents[idx_ny+1]))
nx = np.ones(int(contents[idx_ny+1]))
for i in np.arange(0,len(ny)):
nx[i] = int(contents[idx_nx+2+i])
ny[i] = int(contents[idx_ny+2+i])
ny_out = ny[int(dom_num)-1]
nx_out = nx[int(dom_num)-1]
return zmn, ztn, nx_out, ny_out, dxy, npa
def get_time_from_RAMS_file(INPUT_FILE):
cur_time = os.path.split(INPUT_FILE)[1][4:21] # Grab time string from RAMS file
pd_time = pd.to_datetime(cur_time[0:10]+' '+cur_time[11:13]+":"+cur_time[13:15]+":"+cur_time[15:17])
return pd_time.strftime('%Y-%m-%d %H:%M:%S'), pd_time.strftime('%Y%m%d%H%M%S')
def read_var_h5py(filename,varname):
with h5py.File(filename,"r") as f:
data_out = f[varname][:]
return data_out
def read_var(filename,varname):
with h5py.File(filename,"r") as f:
data_out = f[varname][:]
return data_out
def read_file_h5py(filename):
f = h5py.File(filename,"r")
return f
def read_3dvar_subset(filename,varname,X1,X2,Y1,Y2):
with h5py.File(filename,"r") as f:
data_out = f[varname][:,Y1:Y2,X1:X2]
return data_out
def read_2dvar_subset(filename,varname,X1,X2,Y1,Y2):
with h5py.File(filename,"r") as f:
data_out = f[varname][Y1:Y2,X1:X2]
return data_out