-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_access.js
212 lines (187 loc) · 7.32 KB
/
data_access.js
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// create variable dropdown options
createMomCobaltVarDataOpt('varMOMCobaltData','hist_run'); // Data Query
// default initYear options for forecast (use forecast.js)
window.createMomCobaltInitYearOpt('iyearMOMCobaltForecastData');
// default initMonth options for forecast (use forecast.js)
window.createMomCobaltInitMonthOpt('imonthMOMCobaltForecastData');
// event lister for region radio button in the variable table section
$(document).ready(function(){
$("input[type='radio'].radioDataTable").change(function(){
if ($("#radioNWATable").is(":checked")) {
$('.nepTableOpt').addClass('hidden');
$('.nwaTableOpt').removeClass('hidden');
} else if ($("#radioNEPTable").is(":checked")) {
$('.nwaTableOpt').addClass('hidden');
$('.nepTableOpt').removeClass('hidden');
}
});
});
// event listener for changes in the modeling period
$('#periodMOMCobaltData').on('change', function() {
// Clear out the variable dropdown
$('#varMOMCobaltData').empty();
// Create variable option and year month select based on period selection
if ($('#periodMOMCobaltData').val() === 'hist_run') {
// createMomCobaltVarOpt('MOMCobalt','varMOMCobaltData');
createMomCobaltVarDataOpt('varMOMCobaltData','hist_run');
$('.forecastOpt').addClass('hidden');
} else if ($('#periodMOMCobaltData').val() === 'forecast') {
// createMomCobaltVarOptFcast('MOMCobaltFcast','varMOMCobaltData');
createMomCobaltVarDataOpt('varMOMCobaltData','forecast');
$('.forecastOpt').removeClass('hidden');
}
});
// event lister for radio button choice in data options in data query
$(document).ready(function(){
$("input[type='radio'].radioData").change(function(){
if ($("#radioOpendap").is(":checked")) {
$('.wgetOpt').addClass('hidden');
$('.citeOpt').addClass('hidden');
$('.opendapOpt').removeClass('hidden');
} else if ($("#radioWget").is(":checked")) {
$('.opendapOpt').addClass('hidden');
$('.citeOpt').addClass('hidden');
$('.wgetOpt').removeClass('hidden');
} else if ($("#radioCite").is(":checked")) {
$('.opendapOpt').addClass('hidden');
$('.wgetOpt').addClass('hidden');
$('.citeOpt').removeClass('hidden');
}
});
});
// event listener for data query button click
$('#genQueryButton').on('click', function() {
var dataType = $('#periodMOMCobaltData').val()
generateDataQuery(dataType) // the function return a promise obj from fetch
.then((jsonDataQuery)=>{
var dataInfo = jsonDataQuery.data_info;
$('#codeBlockDataInfo').text(dataInfo);
var http_href = jsonDataQuery.download;
$("#downloadHttp").attr("href", http_href); //direct download link
var wgetCode = jsonDataQuery.wget;
$('#codeBlockWget').text(wgetCode);
var opendapCode = jsonDataQuery.opendap
$('#codeBlockOpendap').text(opendapCode);
var pythonCode = jsonDataQuery.python
$('#codeBlockPython').text(pythonCode);
var rCode = jsonDataQuery.r
$('#codeBlockR').text(rCode);
var Citation = jsonDataQuery.citation
$('#codeBlockCite').text(Citation);
})
});
//event listener for data query code copy
$("#copyButtonWget").click(function () {
copyCode('codeBlockWget');
});
$("#copyButtonOpendap").click(function () {
copyCode('codeBlockOpendap');
});
$("#copyButtonPython").click(function () {
copyCode('codeBlockPython');
});
$("#copyButtonR").click(function () {
copyCode('codeBlockR');
});
$("#copyButtonCite").click(function () {
copyCode('codeBlockCite');
});
// functions for generating data query
async function generateDataQuery(dataType) {
var region = $('#regMOMCobaltData').val();
var variable = $('#varMOMCobaltData').val();
var grid = $('#gridMOMCobalt').val();
var year = -99
var month = -99
if (dataType === 'forecast') {
year = $('#iyearMOMCobaltForecastData').val();
month = $('#imonthMOMCobaltForecastData').val();
}
// find data frequency and create mock date for data query variable
var selectVarDataIndex = $("#varMOMCobaltData").prop('selectedIndex');
var varFreq = groupList[selectVarDataIndex]
// find out mockDate using (historical.js)
var mockDate = window.getMockDate(varFreq)
var ajaxGet = "/cgi-bin/cefi_portal/mom_data_query.py"
+"?variable="+variable
+"®ion="+region
+"&date="+mockDate
+"&grid="+grid
+"&datatype="+dataType
+"&year="+year
+"&month="+month
console.log('https://webtest.psd.esrl.noaa.gov/'+ajaxGet)
return fetch(ajaxGet)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.catch(error => {
// Handle errors here
console.error('Fetch json data query failed:', error);
});
}
function copyCode(codeBlockID) {
let code = $("#"+codeBlockID).text();
// document.getElementById(modalID).focus();
navigator.clipboard.writeText(code)
.then(function() {
console.log('Code copied to clipboard');
})
.catch(function(err) {
console.error('Failed to copy text: ', err);
});
}
// async fetching the data access json files
async function fetchDataAccessJson(region, dataType) {
try {
const response = await fetch('data_option_json/data_access_' + region + '_' + dataType + '.json');
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json(); // JSON data
// console.log('JSON data:', data);
return data;
} catch (error) {
console.error('There was a problem when async fetchDataAccessJson:', error);
}
}
// async function for create option for data access after fetch complete
// Arrays to store the results
var valuesList = [];
var textList = [];
var groupList = [];
async function createMomCobaltVarDataOpt(selectID,dataType='hist_run') {
let elm = document.getElementById(selectID);
let regVal = $("#regMOMCobaltData").val()
let varJson = await fetchDataAccessJson(regVal,dataType);
// console.log(varJson)
let varlist = [
varJson.var_values,
varJson.var_options,
varJson.var_freqs
];
df = window.optionSubgroupList(varlist[1],varlist[0],varlist[2]);
elm.appendChild(df);
// Loop through the select dropdown to store the reordered dropdown list
$("#varMOMCobaltData").children().each(function() {
// Check if the element is an optgroup
if ($(this).is('optgroup')) {
var groupLabel = $(this).attr('label'); // Get the optgroup label
// Now loop through each option inside the optgroup
$(this).children('option').each(function() {
var optionValue = $(this).val(); // Get the option's value
var optionText = $(this).text(); // Get the option's display text
// Store in the lists
valuesList.push(optionValue);
textList.push(optionText);
groupList.push(groupLabel);
});
} else if ($(this).is('option')) {
// Handle options outside of optgroups, if any exist
console.log('Error! one variable options does not have optgroup')
}
});
};