forked from szarkos/paradb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeo-xray.php
217 lines (186 loc) · 6.7 KB
/
geo-xray.php
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
213
214
215
216
217
<?php
//-------------------------------------------------------------------//
//
// AGHOST Paranormal Reporting Database (ParaDB)
// Copyright (c) 2007 Stephen A. Zarkos <[email protected]>
//
// See the file COPYING for terms of use and redistribution.
//
// File: geo-xray.php
// Functions related to obtaining geomagnetic and xray
// information.
//
//-------------------------------------------------------------------//
// AGHOSTOnline.org Space Weather Archive.
DEFINE( "SEC_ARCHIVE", "http://www.aghostonline.org/weather/archive/sec_history.php" );
// Function: populate_geo-xray( [array] )
// Retrieve geomagnetic and xray data and copy it to the passed array.
function populate_geo_xray( &$VALID_FIELDS ) {
global $ENABLE_GEO_XRAY;
if ( $ENABLE_GEO_XRAY != 1 ) {
return 0;
}
if ( !is_array($VALID_FIELDS) ) {
return ERR_UNDEF;
}
$geoxray_fields = array ( 'xray_long',
'xray_short',
'xray_high',
'xray_low',
'xray_peak',
'xray_summary',
'xray_list',
'xray_plot',
'geomag_kp',
'geomag_ap',
'geomag_summary',
'geomag_list',
'geomag_plot' );
// Check to see if case_date is today or in the future. If so, don't
// try to populate the fields.
$casedate = strtotime( get_case_date($VALID_FIELDS['case_id']['value']) );
$curdate = strtotime( date("Y-m-d") );
if ( $casedate >= $curdate ) {
return 0;
}
// Check the geomagnetic and xray related fields to see if they're already set.
// This is done to keep from querying the server every time the report is saved.
$count = 0;
foreach ( $geoxray_fields as $key ) {
if ( !empty($VALID_FIELDS[$key]['value']) && $VALID_FIELDS[$key]['value'] != 0 ) {
$count++;
}
}
if ( $count > 0 ) {
return 0;
}
$xray = get_xray_data( $VALID_FIELDS['date']['value'] );
if ( !is_array($xray) ) {
$xray = array();
}
$geomag = get_geomag_data( $VALID_FIELDS['date']['value'] );
if ( !is_array($geomag) ) {
$geomag = array();
}
foreach ( array_merge($xray, $geomag) as $key=>$value ) {
if ( isset($VALID_FIELDS[$key]['value']) ) {
$value = preg_replace( '/</', '<', $value );
$value = preg_replace( '/>/', '>', $value );
$VALID_FIELDS[$key]['value'] = $value;
}
}
return 0;
}
// Function get_xray_data ( [date] )
// Retrieves an array containing x-ray data for the specified date. Date should be in the format xxxx-xx-xx.
// The returned array has the following values:
// $xraydata = ( 'xray_long',
// 'xray_short',
// 'xray_high',
// 'xray_low',
// 'xray_peak',
// 'xray_summary',
// 'xray_list',
// 'xray_plot' );
function get_xray_data ( $date="" ) {
if ( empty($date) ) {
return ERR_UNDEF;
}
$xraydata = array();
$date = explode( '-', $date );
$date[1] = preg_replace( '/^0+/', '', $date[1] );
$date[2] = preg_replace( '/^0+/', '', $date[2] );
$url = SEC_ARCHIVE . '?format=0&year=' . $date[0] . '&month=' . $date[1] . '&day=' . $date[2];
$FH = fopen( $url, "r" );
if ( !$FH ) {
do_log( 'Error: get_xray_data(): ' . ERR_XRAY_FILE_RETR );
return ERR_FRETR;
}
while ( !feof($FH) ) {
$line = fgets( $FH, 1024 );
if ( preg_match( '/^Xray \(1.0-8.0 Ang\):/', $line) ) {
$xraydata['xray_long'] = preg_replace( '/^Xray \(1.0-8.0 Ang\):[\s\t]+/', '', $line );
$xraydata['xray_long'] = trim( $xraydata['xray_long'] );
}
elseif ( preg_match( '/^Xray \(0.5-3.0 Ang\):[\s\t]+/', $line) ) {
$xraydata['xray_short'] = preg_replace( '/^Xray \(0.5-3.0 Ang\):[\s\t]+/', '', $line );
$xraydata['xray_short'] = trim( $xraydata['xray_short'] );
}
elseif ( preg_match( '/^Xray High:[\s\t]+/', $line) ) {
$xraydata['xray_high'] = preg_replace( '/^Xray High:[\s\t]+/', '', $line );
$xraydata['xray_high'] = trim( $xraydata['xray_high'] );
}
elseif ( preg_match( '/^Xray Low:[\s\t]+/', $line) ) {
$xraydata['xray_low'] = preg_replace( '/^Xray Low:[\s\t]+/', '', $line );
$xraydata['xray_low'] = trim( $xraydata['xray_low'] );
}
elseif ( preg_match( '/^Xray Peak:[\s\t]+/', $line) ) {
$xraydata['xray_peak'] = preg_replace( '/^Xray Peak:[\s\t]+/', '', $line );
$xraydata['xray_peak'] = trim( $xraydata['xray_peak'] );
}
elseif ( preg_match( '/^Xray Summary:[\s\t]+/', $line) ) {
$xraydata['xray_summary'] = preg_replace( '/^Xray Summary:[\s\t]+/', '', $line );
$xraydata['xray_summary'] = trim( $xraydata['xray_summary'] );
}
elseif ( preg_match( '/^Xray List:[\s\t]+/', $line) ) {
$xraydata['xray_list'] = preg_replace( '/^Xray List:[\s\t]+/', '', $line );
$xraydata['xray_list'] = trim( $xraydata['xray_list'] );
}
elseif ( preg_match( '/^Xray Plot:[\s\t]+/', $line) ) {
$xraydata['xray_plot'] = preg_replace( '/^Xray Plot:[\s\t]+/', '', $line );
$xraydata['xray_plot'] = trim( $xraydata['xray_plot'] );
}
}
fclose( $FH );
return $xraydata;
}
// Function get_geomag_data ( [date] )
// Retrieves an array containing geomagnetic data for the specified date. Date should be in the
// format xxxx-xx-xx.
// The returned array has the following values:
// $geodata = ( 'geomag_kp',
// 'geomag_ap',
// 'geomag_summary',
// 'geomag_list',
// 'geomag_plot' );
function get_geomag_data ( $date="" ) {
if ( empty($date) ) {
return ERR_UNDEF;
}
$geodata = array();
$date = explode( '-', $date );
$date[1] = preg_replace( '/^0+/', '', $date[1] );
$date[2] = preg_replace( '/^0+/', '', $date[2] );
$url = SEC_ARCHIVE . '?format=0&year=' . $date[0] . '&month=' . $date[1] . '&day=' . $date[2];
$FH = fopen( $url, "r" );
if ( !$FH ) {
do_log( 'Error: get_geomag_data(): ' . ERR_GEOM_FILE_RETR );
return ERR_FRETR;
}
while ( !feof($FH) ) {
$line = fgets( $FH, 1024 );
if ( preg_match( '/^Geomagnetic Kp:/', $line) ) {
$geodata['geomag_kp'] = preg_replace( '/^Geomagnetic Kp:[\s\t]+/', '', $line );
$geodata['geomag_kp'] = trim( $geodata['geomag_kp'] );
}
elseif ( preg_match( '/^Geomagnetic Ap:/', $line) ) {
$geodata['geomag_ap'] = preg_replace( '/^Geomagnetic Ap:[\s\t]+/', '', $line );
$geodata['geomag_ap'] = trim( $geodata['geomag_ap'] );
}
elseif ( preg_match( '/^Geomagnetic Summary:/', $line) ) {
$geodata['geomag_summary'] = preg_replace( '/^Geomagnetic Summary:[\s\t]+/', '', $line );
$geodata['geomag_summary'] = trim( $geodata['geomag_summary'] );
}
elseif ( preg_match( '/^Geomagnetic List:/', $line) ) {
$geodata['geomag_list'] = preg_replace( '/^Geomagnetic List:[\s\t]+/', '', $line );
$geodata['geomag_list'] = trim( $geodata['geomag_list'] );
}
elseif ( preg_match( '/^Geomagnetic Plot:/', $line) ) {
$geodata['geomag_plot'] = preg_replace( '/^Geomagnetic Plot:[\s\t]+/', '', $line );
$geodata['geomag_plot'] = trim( $geodata['geomag_plot'] );
}
}
fclose( $FH );
return $geodata;
}
?>