-
Notifications
You must be signed in to change notification settings - Fork 0
/
Make_value_of_data_plot.py
executable file
·182 lines (108 loc) · 5.92 KB
/
Make_value_of_data_plot.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import os
import glob
import numpy as np
import Make_time_series_plot as mtsp
import sys
import pdb
"""
This script produces a time series plot showing the value of the energy balance variables for a given station according to the sampling percentage used to produce the 3 hour means from the 30 minute mean.
The time series covers roughly the first three days of data. One plot per station per variable.
Author : Élise Comeau
Created : July 28th, 2021
Last modified : August 9th, 2021
"""
# Step 0 : Define constants
# Step 0.1 : Define directories
INPUT_DIRECTORY_1 = '/snow/diluca/FLUXNET_America/1990-2017/v3/TA-SW_IN-SW_OUT-LW_IN-LW_OUT-H-LE/sampling-percentage'
INPUT_DIRECTORY_2 = '/num-years1'
INPUT_DIRECTORY_3 = '/npy'
OUTPUT_DIRECTORY = '/snow/comeau/FLUXNET_America/AMF_1990-2017/png'
# Step 0.2 : Define filenames
STATION_NAMES_FILENAME = 'AMF_station-filelist_1990-2017_TA-SW_IN-SW_OUT-LW_IN-LW_OUT-H-LE.txt'
# Step 0.3 : Define prefixes and suffixes
AMF_PREFIX = 'AMF_'
HALF_HR_SUFFIX = '_30.npy'
THREE_HR_SUFFIX = '_3.npy'
PNG_SUFFIX = '.png'
TXT_SUFFIX = '.txt'
# Step 0.4 : Define delimiters
STATION_NAMES_DELIMITER = '_'
VAR_NAMES_DELIMITER = '-'
# Step 0.5 : Define special characters
NULL_CHAR = ''
READING_CHAR = 'r'
SLASH_BAR = '/'
STAR = '*'
# Step 0.6 : Define indexes
FILEPATH_INDEX = 0
FIRST_ITEM_INDEX = 0
STATION_ID_INDEX = 3
STATION_NBR_INDEX = 2
VAR_NAMES_INDEX = 3
# Step 0.7 : Define values
DATA_ID = 'data'
DATES_ID = 'dates'
SAMPLING_PERCENTAGES = ['10', '25', '50', '60', '75', '100'] # minimum sampling percentages
MIN_NBR_DATA = ['1', '2', '3', '4', '5', '6']
# Step 0.8 : Define plot constants
HALF_HR_LEGEND_LABEL = 'AMF - 30 mins'
THREE_HR_LEGEND_LABEL_1 = 'AMF - 180 mins ('
THREE_HR_LEGEND_LABEL_2 = '/6+ data)'
PLOT_TYPE = 'var-values'
# Step 1 : Obtain variables of interest
var_names = STATION_NAMES_FILENAME.split(STATION_NAMES_DELIMITER, VAR_NAMES_INDEX)[VAR_NAMES_INDEX].replace(TXT_SUFFIX, NULL_CHAR)
var_names_list = var_names.split(VAR_NAMES_DELIMITER)
# Step 2 : Obtain station numbers and ids
station_names_file_pathname = INPUT_DIRECTORY_1 + SAMPLING_PERCENTAGES[0] + INPUT_DIRECTORY_2 + SLASH_BAR + STATION_NAMES_FILENAME
station_names_file = open(station_names_file_pathname, READING_CHAR)
station_nbrs_list = []
station_ids_list = []
for station_names_line in station_names_file :
amf_filename = os.path.basename(station_names_line)
station_nbr = amf_filename.split(STATION_NAMES_DELIMITER)[STATION_NBR_INDEX]
station_id = amf_filename.split(STATION_NAMES_DELIMITER)[STATION_ID_INDEX]
station_ids_list.append(station_id)
station_nbrs_list.append(station_nbr)
station_names_file.close()
# Step 3 : Obtain dates and data for plot
for station_id, station_nbr in zip(station_ids_list, station_nbrs_list) :
station_dates_list = []
station_data_list = []
labels_list = []
# Step 3.1 : Obtain dates and data for 0.5 hour average
dates_0_5_hr_filepath_pattern = INPUT_DIRECTORY_1 + SAMPLING_PERCENTAGES[0] + INPUT_DIRECTORY_2 + INPUT_DIRECTORY_3 + SLASH_BAR + STAR + DATES_ID + STAR + station_id + STAR + HALF_HR_SUFFIX
dates_0_5_hr_files = glob.glob(dates_0_5_hr_filepath_pattern)
if ( dates_0_5_hr_files ) :
dates_0_5_hr_filepath = glob.glob(dates_0_5_hr_filepath_pattern)[FILEPATH_INDEX]
dates_0_5_hr = np.load(dates_0_5_hr_filepath)
data_0_5_hr_filepath_pattern = dates_0_5_hr_filepath_pattern.replace(DATES_ID, DATA_ID)
data_0_5_hr_filepath = glob.glob(data_0_5_hr_filepath_pattern)[FILEPATH_INDEX]
data_0_5_hr = np.load(data_0_5_hr_filepath)
station_dates_list.append(dates_0_5_hr)
station_data_list.append(data_0_5_hr)
labels_list.append(HALF_HR_LEGEND_LABEL)
for sampling_percentage in SAMPLING_PERCENTAGES :
# Step 3.2 : Obtain dates and data for 3 hour averages
dates_3_hr_filepath_pattern = INPUT_DIRECTORY_1 + sampling_percentage + INPUT_DIRECTORY_2 + INPUT_DIRECTORY_3 + SLASH_BAR + STAR + DATES_ID + STAR + station_id + STAR + THREE_HR_SUFFIX
dates_3_hr_files = glob.glob(dates_3_hr_filepath_pattern)
if ( dates_3_hr_files ) :
dates_3_hr_filepath = glob.glob(dates_3_hr_filepath_pattern)[FILEPATH_INDEX]
dates_3_hr = np.load(dates_3_hr_filepath)
data_3_hr_filepath_pattern = dates_3_hr_filepath_pattern.replace(DATES_ID, DATA_ID)
data_3_hr_filepath = glob.glob(data_3_hr_filepath_pattern)[FILEPATH_INDEX]
data_3_hr = np.load(data_3_hr_filepath)
min_nbr_data = MIN_NBR_DATA[SAMPLING_PERCENTAGES.index(sampling_percentage)]
values_3_hr_label = THREE_HR_LEGEND_LABEL_1 + min_nbr_data + THREE_HR_LEGEND_LABEL_2
station_dates_list.append(dates_3_hr)
station_data_list.append(data_3_hr)
labels_list.append(values_3_hr_label)
# Step 4 : Create plots
for var_name in var_names_list :
var_index = var_names_list.index(var_name)
nbr_of_combos = len(station_data_list) # combos refers to combinations of sampling percentages and minimum numbers of years of data
station_data_ploting_list = []
for combo_nbr in range(0, nbr_of_combos) :
station_data_ploting_list.append(station_data_list[combo_nbr][:, var_index])
plot_filename = PLOT_TYPE + STATION_NAMES_DELIMITER + station_nbr + STATION_NAMES_DELIMITER + station_id + STATION_NAMES_DELIMITER + var_name + STATION_NAMES_DELIMITER + INPUT_DIRECTORY_2.replace(SLASH_BAR, NULL_CHAR) + PNG_SUFFIX
plot_pathname = OUTPUT_DIRECTORY + SLASH_BAR + plot_filename
mtsp.Make_time_series_plot(station_dates_list, station_data_ploting_list, labels_list, var_name, plot_pathname)