forked from NOAA-CEFI-Portal/cefi-portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_access.js
149 lines (134 loc) · 5.06 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
// default historical run options
createMomCobaltVarOpt('MOMCobalt','varMOMCobaltData'); // Data Query
// default initYear options for forecast
createMomCobaltInitYearOpt('iyearMOMCobaltForecastData');
// default initMonth options for forecast
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');
$('.forecastOpt').addClass('hidden');
} else if ($('#periodMOMCobaltData').val() === 'forecast') {
createMomCobaltVarOptFcast('MOMCobaltFcast','varMOMCobaltData');
$('.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
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 TS2
var selectVarDataIndex = $("#varMOMCobaltData").prop('selectedIndex');
var varlist = momCobaltVars();
var varFreq = varlist[2][selectVarDataIndex]
var mockDate = 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);
});
}