Merge pull request #5243 from solgenomics/topic/observations_upload_catch_error
[sgn.git] / mason / breeders_toolbox / noaa_ncdc_analysis.mas
blobd20e7c0b3a06641d9969ba333e4e9604a649a9ce
2 <%args>
3 $locations => undef
4 </%args>
6 <& /util/import_javascript.mas, classes => [ ] &>
8 <& /page/page_title.mas, title=>"Analyze NOAA NCDC Weather Data" &>
10 <div class="form-horizontal">
11     <div class="form-group">
12         <label class="col-sm-4 control-label">Select Location: </label>
13         <div class="col-sm-8" >
14             <div id="noaa_ncdc_analysis_location_select"></div>
15         </div>
16     </div>
17 </div>
19 <div id="noaa_ncdc_analysis_input_div" style="display:none">
20     <div class="form-horizontal">
21         <div class="form-group">
22             <label class="col-sm-4 control-label">Start Date (YYYY-MM-DD): </label>
23             <div class="col-sm-8" >
24                 <input class="form-control" id="noaa_ncdc_analysis_start_date" name="noaa_ncdc_analysis_start_date" type="text" />
25             </div>
26         </div>
27     </div>
28     <div class="form-horizontal">
29         <div class="form-group">
30             <label class="col-sm-4 control-label">End Date (YYYY-MM-DD): </label>
31             <div class="col-sm-8" >
32                 <input class="form-control" id="noaa_ncdc_analysis_end_date" name="noaa_ncdc_analysis_end_date" type="text" />
33             </div>
34         </div>
35     </div>
36     <div class="form-horizontal">
37         <div class="form-group">
38             <label class="col-sm-4 control-label">Results: </label>
39             <div class="col-sm-8" >
40                 <select class="form-control" id="noaa_ncdc_analysis_type">
41                     <option value="daily_temp_prec">Daily Temperature and Precipitation Chart</option>
42                 </select>
43             </div>
44         </div>
45     </div>
46     <div class="form-horizontal">
47         <div class="form-group">
48             <label class="col-sm-4 control-label">Cumulative Values by Year: </label>
49             <div class="col-sm-8" >
50                 <select class="form-control" id="noaa_ncdc_analysis_cumulative_year">
51                     <option value="no">No</option>
52                     <option value="yes">Yes</option>
53                 </select>
54             </div>
55         </div>
56     </div>
57     <div class="form-horizontal">
58         <div class="form-group">
59             <label class="col-sm-4 control-label">Window Start (YYYY-MM-DD) (Optional): </label>
60             <div class="col-sm-8" >
61                 <input class="form-control" id="noaa_ncdc_analysis_window_start_date" name="noaa_ncdc_analysis_window_start_date" type="text" />
62             </div>
63         </div>
64     </div>
65     <div class="form-horizontal">
66         <div class="form-group">
67             <label class="col-sm-4 control-label">Window End (YYYY-MM-DD) (Optional): </label>
68             <div class="col-sm-8" >
69                 <input class="form-control" id="noaa_ncdc_analysis_window_end_date" name="noaa_ncdc_analysis_window_end_date" type="text" />
70             </div>
71         </div>
72     </div>
73     <br/><br/>
74     <center>
75         <button class="btn btn-primary" type="button" id="noaa_ncdc_analysis_submit" name="noaa_ncdc_analysis_submit">Submit</button>
76     </center>
77 </div>
79 <div id="noaa_ndcd_analysis_result">
80 </div>
82 <script>
84 jQuery(document).ready(function() {
86     get_select_box('locations', 'noaa_ncdc_analysis_location_select', {'id':'noaa_ncdc_analysis_location_id', 'name':'noaa_ncdc_analysis_location_id', 'empty':'1'});
88     var noaa_ncdc_analysis_location_id = '';
89     var noaa_ncdc_analysis_location_station_id = '';
90     var noaa_ncdc_analysis_start_date = '';
91     var noaa_ncdc_analysis_end_date = '';
92     var noaa_ncdc_analysis_type = '';
93     var noaa_ncdc_analysis_cumulative_year = '';
94     var noaa_ncdc_token = '<% $c->config->{noaa_ncdc_access_token} %>';
96     if (noaa_ncdc_token == 'NULL') {
97         alert(`Configuration variable 'noaa_ncdc_access_token' is undefined. Data from station `+noaa_station_id+` can't be accessed without a token.\r\nPlease visit https://www.ncdc.noaa.gov/cdo-web/token to generate a valid access token, then include the token in sgn_local.conf`);
98     }
100     jQuery(document).on('change', '#noaa_ncdc_analysis_location_id', function(){
101         noaa_ncdc_analysis_location_id = jQuery(this).val();
102         jQuery.ajax({
103             url : '/ajax/location/get_noaa_station_id/'+noaa_ncdc_analysis_location_id,
104             success: function(response){
105                 console.log(response);
106                 if (response.noaa_station_id) {
107                     noaa_ncdc_analysis_location_station_id = response.noaa_station_id;
109                     jQuery.ajax({
110                         url:'https://www.ncdc.noaa.gov/cdo-web/api/v2/stations/'+noaa_ncdc_analysis_location_station_id,
111                         headers:{ token: noaa_ncdc_token },
112                         success: function(response) {
113                             console.log(response);
114                             if (response.name) {
115                                 alert('Found a valid NOAA Station for this location: '+response.name);
116                                 jQuery('#noaa_ncdc_analysis_input_div').show();
117                             }
118                             else {
119                                 alert('Could not verify the NOAA StationID you provided. Go to Manage->Locations and make sure your location has a valid NOAA station id!');
120                                 jQuery('#noaa_ncdc_analysis_input_div').hide();
121                             }
122                         },
123                         error: function() {
124                             alert('The NOAA NCDC authorization token may not be valid! We could not verify your NOAA StationID. Please contact us!');
125                             jQuery('#noaa_ncdc_analysis_input_div').hide();
126                         }
127                     });
128                 }
129                 else {
130                     alert('No NOAA NCDC station id for this location. Go to Manage->Locations and edit your location to add a valid NOAA station id!');
131                     jQuery('#noaa_ncdc_analysis_input_div').hide();
132                 }
133             },
134             error: function(response){
135                 alert('Error getting NOAA NCDC ID for location!');
136                 jQuery('#noaa_ncdc_analysis_input_div').hide();
137             }
138         });
139     });
141     jQuery('#noaa_ncdc_analysis_submit').click(function(){
142         noaa_ncdc_analysis_start_date = jQuery('#noaa_ncdc_analysis_start_date').val();
143         noaa_ncdc_analysis_end_date = jQuery('#noaa_ncdc_analysis_end_date').val();
144         noaa_ncdc_analysis_type = jQuery('#noaa_ncdc_analysis_type').val();
145         noaa_ncdc_analysis_window_start_date = jQuery('#noaa_ncdc_analysis_window_start_date').val();
146         noaa_ncdc_analysis_window_end_date = jQuery('#noaa_ncdc_analysis_window_end_date').val();
147         noaa_ncdc_analysis_cumulative_year = jQuery('#noaa_ncdc_analysis_cumulative_year').val();
148         if (noaa_ncdc_analysis_start_date == '') {
149             alert('Please give a start date!');
150             return false;
151         }
152         if (noaa_ncdc_analysis_end_date == '') {
153             alert('Please give a end date!');
154             return false;
155         }
157         jQuery.ajax({
158             url : '/ajax/location/noaa_ncdc_analysis?location_id='+noaa_ncdc_analysis_location_id+'&station_id='+noaa_ncdc_analysis_location_station_id+'&start_date='+noaa_ncdc_analysis_start_date+'&end_date='+noaa_ncdc_analysis_end_date+'&analysis_type='+noaa_ncdc_analysis_type+'&w_start='+noaa_ncdc_analysis_window_start_date+'&w_end='+noaa_ncdc_analysis_window_end_date+'&cumul_year='+noaa_ncdc_analysis_cumulative_year,
159             success: function(response){
160                 console.log(response);
161                 if (response.error) {
162                     alert(response.error);
163                 }
164                 else if (response.noaa_station_id) {
165                     var html = '<img src="'+response.plot+'"><hr><img src="'+response.plot2+'">';
166                     jQuery('#noaa_ndcd_analysis_result').html(html);
167                 }
168                 else {
169                     alert('There was an error getting NOAA analysis!');
170                 }
171             },
172             error: function(response){
173                 alert('Error getting NOAA NCDC analysis!');
174             }
175         });
176     });
180 </script>