Debian Codegraph @

Nasal/local_weather/local_weather.nas

raw
1
2 ########################################################
3 # routines to set up, transform and manage local weather
4 # Thorsten Renk, June 2011
5 # thermal model by Patrice Poly, April 2010
6 ########################################################
7
8 # function purpose
9 #
10 # calc_geo to compute the latitude to meter conversion
11 # calc_d_sq to compute a distance square in local Cartesian approximation
12 # effect_volume_loop to check if the aircraft has entered an effect volume
13 # assemble_effect_array to store the size of the effect volume array
14 # add_vectors to add two vectors in polar coordinates
15 # wind_altitude_interpolation to interpolate aloft winds in altitude
16 # wind_interpolation to interpolate aloft winds in altitude and position
17 # get_slowdown_fraction to compute the effect of boundary layer wind slowdown
18 # interpolation_loop to continuously interpolate weather parameters between stations
19 # thermal_lift_start to start the detailed thermal model
20 # thermal_lift_loop to manage the detailed thermal lift model
21 # thermal_lift_stop to end the detailed thermal lift model
22 # wave_lift_start to start the detailed wave lift model
23 # wave_lift_loop to manage the detailed wave lift model
24 # wave_lift_stop to end the detailed wave lift model
25 # effect_volume_start to manage parameters when an effect volume is entered
26 # effect_volume_stop to manage parameters when an effect volume is left
27 # ts_factor (helper function for thermal lift model)
28 # tl_factor (helper function for thermal lift model)
29 # calcLift_max to calculate the maximal available thermal lift for given altitude
30 # calcLift to calculate the thermal lift at aircraft position
31 # calcWaveLift to calculate wave lift at aircraft position
32 # create_cloud_vec to place a single cloud into an array to be written later
33 # clear_all to remove all clouds, effect volumes and weather stations and stop loops
34 # create_detailed_cumulus_cloud to place multiple cloudlets into a box based on a size parameter
35 # create_cumulonimbus_cloud to place multiple cloudlets into a box
36 # create_cumulonimbus_cloud_rain to place multiple cloudlets into a box and add a rain layer beneath
37 # create_cumosys (wrapper to place a convective cloud system based on terrain coverage)
38 # cumulus_loop to place 25 Cumulus clouds each frame
39 # create_cumulus to place a convective cloud system based on terrain coverage
40 # recreate_cumulus to respawn convective clouds as part of the convective dynamics algorithm
41 # cumulus_exclusion_layer to create a layer with 'holes' left for thunderstorm placement
42 # create_rise_clouds to create a barrier cloud system
43 # create_streak to create a cloud streak
44 # create_hollow_layer to create a cloud layer in a hollow cylinder (better for performance)
45 # create_cloudbox to create a sophisticated cumulus cloud with different textures (experimental)
46 # terrain_presampling_start to initialize terrain presampling
47 # terrain_presampling_loop to sample 25 terrain points per frame
48 # terrain_presampling to sample terrain elevation at a random point within specified area
49 # terrain_presampling_analysis to analyze terrain presampling results
50 # wave_detection_loop to detect if and where wave lift should be placed (currently unfinished)
51 # get_convective_altitude to determine the altitude at which a Cumulus cloud is placed
52 # manage presampling to take proper action when a presampling call has been finished
53 # set_wind_model_flag to convert the wind model string into an integer flag
54 # set_texture_mix to determine the texture mix between smooth and rough cloud appearance
55 # create_effect_volume to create an effect volume
56 # set_weather_station to specify a weather station for interpolation
57 # set_atmosphere_ipoint to specify an interpolation point for visibility, haze and shading in the atmosphere
58 # set_wind_ipoint to set an aloft wind interpolation point
59 # set_wind_ipoint_metar to set a wind interpolation point from available ground METAR info where aloft is modelled
60 # showDialog to pop up a dialog window
61 # readFlags to read configuration flags from the property tree into Nasal variables at startup
62 # streak_wrapper wrapper to execute streak from menu
63 # convection wrapper wrapper to execute convective clouds from menu
64 # barrier wrapper wrapper to execute barrier clouds from menu
65 # single_cloud_wrapper wrapper to create single cloud from menu
66 # layer wrapper wrapper to create layer from menu
67 # box wrapper wrapper to create a cloudbox (experimental)
68 # set_aloft wrapper wrapper to create aloft winds from menu
69 # set_tile to call a weather tile creation from menu
70 # startup to prepare the package at startup
71 # test to serve as a testbed for new functions
72
73 # object purpose
74
75 # weatherStation to store info about weather conditions
76 # atmopshereIpoint to store info about haze and light propagation in the atmosphere
77 # windIpoint to store an interpolation point of the windfield
78 # effectVolume to store effect volume info and provide methods to move and time-evolve effect volumes
79 # thermalLift to store thermal info and provide methods to move and time-evolve a thermal
80 # waveLift to store wave info
81
82
83
84
85 ###################################
86 # geospatial helper functions
87 ###################################
88
89 var calc_geo = func(clat) {
90
91 lon_to_m = math.cos(clat*math.pi/180.0) * lat_to_m;
92 m_to_lon = 1.0/lon_to_m;
93
94 weather_dynamics.lon_to_m = lon_to_m;
95 weather_dynamics.m_to_lon = m_to_lon;
96
97 }
98
99
100 var calc_d_sq = func (lat1, lon1, lat2, lon2) {
101
102 var x = (lat1 - lat2) * lat_to_m;
103 var y = (lon1 - lon2) * lon_to_m;
104
105 return (x*x + y*y);
106 }
107
108
109 ###################################
110 # effect volume management loop
111 ###################################
112
113 var effect_volume_loop = func (index, n_active) {
114
115
116 if (local_weather_running_flag == 0) {return;}
117
118 var n = 25;
119
120
121 var esize = n_effectVolumeArray;
122
123 var viewpos = geo.aircraft_position();
124 var active_counter = n_active;
125
126 var i_max = index + n;
127 if (i_max > esize) {i_max = esize;}
128
129 for (var i = index; i < i_max; i = i+1)
130 {
131 var e = effectVolumeArray[i];
132
133 var flag = 0; # default assumption is that we're not in the volume
134
135 var ealt_min = e.alt_low * ft_to_m;
136 var ealt_max = e.alt_high * ft_to_m;
137
138
139 if ((viewpos.alt() > ealt_min) and (viewpos.alt() < ealt_max)) # we are in the correct alt range
140 {
141 # so we load geometry next
142
143 var geometry = e.geometry;
144 var elat = e.lat;
145 var elon = e.lon;
146 var rx = e.r1;
147
148 if (geometry == 1) # we have a cylinder
149 {
150 var d_sq = calc_d_sq(viewpos.lat(), viewpos.lon(), elat, elon);
151 if (d_sq < (rx*rx)) {flag =1;}
152 }
153 else if (geometry == 2) # we have an elliptic shape
154 {
155 # get orientation
156
157 var ry = e.r2;
158 var phi = e.phi;
159
160 phi = phi * math.pi/180.0;
161
162
163 # first get unrotated coordinates
164 var xx = (viewpos.lon() - elon) * lon_to_m;
165 var yy = (viewpos.lat() - elat) * lat_to_m;
166
167 # then rotate to align with the shape
168 var x = xx * math.cos(phi) - yy * math.sin(phi);
169 var y = yy * math.cos(phi) + xx * math.sin(phi);
170
171 # then check elliptic condition
172 if ((x*x)/(rx*rx) + (y*y)/(ry*ry) <1) {flag = 1;}
173 }
174 else if (geometry == 3) # we have a rectangular shape
175 {
176 # get orientation
177
178 var ry = e.r2;
179 var phi = e.phi;
180
181 phi = phi * math.pi/180.0;
182 # first get unrotated coordinates
183 var xx = (viewpos.lon() - elon) * lon_to_m;
184 var yy = (viewpos.lat() - elat) * lat_to_m;
185 # then rotate to align with the shape
186 var x = xx * math.cos(phi) - yy * math.sin(phi);
187 var y = yy * math.cos(phi) + xx * math.sin(phi);
188 # then check rectangle condition
189 if ((x>-rx) and (x<rx) and (y>-ry) and (y<ry)) {flag = 1;}
190 }
191 } # end if altitude
192
193
194 # if flag ==1 at this point, we are inside the effect volume
195 # but we only need to take action on entering and leaving, so we check also active_flag
196
197 # if (flag==1) {print("Inside volume");}
198
199 var active_flag = e.active_flag;
200
201 if ((flag==1) and (active_flag ==0)) # we just entered the node
202 {
203 #print("Entered volume");
204 e.active_flag = 1;
205 effect_volume_start(e);
206 }
207 else if ((flag==0) and (active_flag ==1)) # we left an active node
208 {
209 #print("Left volume!");
210 e.active_flag = 0;
211 effect_volume_stop(e);
212 }
213 if (flag==1) {active_counter = active_counter + 1;} # we still count the active volumes
214
215 } # end foreach
216
217 # at this point, all active effect counters should have been set to zero if we're outside all volumes
218 # however there seem to be rare configurations of overlapping volumes for which this doesn't happen
219 # therefore we zero them for redundancy here so that the interpolation loop can take over
220 # and set the properties correctly for outside
221
222
223 if (i == esize) # we check the number of actives and reset all counters
224 {
225 if (active_counter == 0)
226 {
227 var vNode = props.globals.getNode("local-weather/effect-volumes", 1);
228 vNode.getChild("number-active-vis").setValue(0);
229 vNode.getChild("number-active-snow").setValue(0);
230 vNode.getChild("number-active-rain").setValue(0);
231 vNode.getChild("number-active-lift").setValue(0);
232 vNode.getChild("number-active-turb").setValue(0);
233 vNode.getChild("number-active-sat").setValue(0);
234 }
235 #print("n_active: ", active_counter);
236 active_counter = 0; i = 0;
237 }
238
239 # and we repeat the loop as long as the control flag is set
240
241
242 if (getprop(lw~"effect-loop-flag") ==1) {settimer( func {effect_volume_loop(i, active_counter); },0);}
243 }
244
245
246 ###################################
247 # assemble effect volume array
248 ###################################
249
250
251 var assemble_effect_array = func {
252
253 n_effectVolumeArray = size(effectVolumeArray);
254 }
255
256
257
258 ###################################
259 # vector addition
260 ###################################
261
262 var add_vectors = func (phi1, r1, phi2, r2) {
263
264 phi1 = phi1 * math.pi/180.0;
265 phi2 = phi2 * math.pi/180.0;
266
267 var x1 = r1 * math.sin(phi1);
268 var x2 = r2 * math.sin(phi2);
269
270 var y1 = r1 * math.cos(phi1);
271 var y2 = r2 * math.cos(phi2);
272
273 var x = x1+x2;
274 var y = y1+y2;
275
276 var phi = math.atan2(x,y) * 180.0/math.pi;
277 var r = math.sqrt(x*x + y*y);
278
279 var vec = [];
280
281 append(vec, phi);
282 append(vec,r);
283
284 return vec;
285 }
286
287
288 ###################################
289 # windfield altitude interpolation
290 ###################################
291
292
293 var wind_altitude_interpolation = func (altitude, w) {
294
295 if (altitude < wind_altitude_array[0]) {var alt_wind = wind_altitude_array[0];}
296 else if (altitude > wind_altitude_array[8]) {var alt_wind = 0.99* wind_altitude_array[8];}
297 else {var alt_wind = altitude;}
298
299 for (var i = 0; i<9; i=i+1)
300 {if (alt_wind < wind_altitude_array[i]) {break;}}
301
302
303 #var altNodeMin = w.getChild("altitude",i-1);
304 #var altNodeMax = w.getChild("altitude",i);
305
306 #var vmin = altNodeMin.getNode("windspeed-kt").getValue();
307 #var vmax = altNodeMax.getNode("windspeed-kt").getValue();
308
309 var vmin = w.alt[i-1].v;
310 var vmax = w.alt[i].v;
311
312 #var dir_min = altNodeMin.getNode("wind-from-heading-deg").getValue();
313 #var dir_max = altNodeMax.getNode("wind-from-heading-deg").getValue();
314
315 var dir_min = w.alt[i-1].d;
316 var dir_max = w.alt[i].d;
317
318 var f = (alt_wind - wind_altitude_array[i-1])/(wind_altitude_array[i] - wind_altitude_array[i-1]);
319
320 var res = add_vectors(dir_min, (1-f) * vmin, dir_max, f * vmax);
321
322 return res;
323 }
324
325
326 ###################################
327 # windfield spatial interpolation
328 ###################################
329
330 var wind_interpolation = func (lat, lon, alt) {
331
332 var sum_norm = 0;
333 var sum_wind = [0,0];
334
335 var wsize = size(windIpointArray);
336
337 for (var i = 0; i < wsize; i=i+1) {
338
339
340 var w = windIpointArray[i];
341
342 var wpos = geo.Coord.new();
343 wpos.set_latlon(w.lat,w.lon,1000.0);
344
345 var ppos = geo.Coord.new();
346 ppos.set_latlon(lat,lon,1000.0);
347
348 var d = ppos.distance_to(wpos);
349 if (d <100.0) {d = 100.0;} # to prevent singularity at zero
350
351 sum_norm = sum_norm + (1./d) * w.weight;
352
353 var res = wind_altitude_interpolation(alt,w);
354
355 sum_wind = add_vectors(sum_wind[0], sum_wind[1], res[0], (res[1]/d) * w.weight);
356
357 # gradually fade in the interpolation weight of newly added points to
358 # avoid sudden jumps
359
360 if (w.weight < 1.0) {w.weight = w.weight + 0.002;}
361 }
362
363 sum_wind[1] = sum_wind[1] /sum_norm;
364
365 return sum_wind;
366 }
367
368
369 ###################################
370 # boundary layer computations
371 ###################################
372
373
374 var get_slowdown_fraction = func {
375
376 var tile_index = getprop(lw~"tiles/tile[4]/tile-index");
377 var altitude_agl = getprop("/position/altitude-agl-ft");
378 var altitude = getprop("/position/altitude-ft");
379
380
381
382 if (presampling_flag == 0)
383 {
384 var base_layer_thickness = 600.0;
385 var f_slow = 1.0/3.0;
386 }
387 else
388 {
389 var alt_median = alt_50_array[tile_index - 1];
390 var alt_difference = alt_median - (altitude - altitude_agl);
391 var base_layer_thickness = 150.0;
392
393 # get the boundary layer size dependent on terrain altitude above terrain median
394
395 if (alt_difference > 0.0) # we're low and the boundary layer grows
396 {var boundary_alt = base_layer_thickness + 0.3 * alt_difference;}
397 else # the boundary layer shrinks
398 {var boundary_alt = base_layer_thickness + 0.1 * alt_difference;}
399
400 if (boundary_alt < 50.0){boundary_alt = 50.0;}
401 if (boundary_alt > 3000.0) {boundary_alt = 3000.0;}
402
403 # get the boundary effect as a function of bounday layer size
404
405 var f_slow = 1.0 - (0.2 + 0.17 * math.ln(boundary_alt/base_layer_thickness));
406 }
407
408 if (debug_output_flag == 1)
409 {
410 #print("Boundary layer thickness: ",base_layer_thickness);
411 #print("Boundary layer slowdown: ", f_slow);
412 }
413 return f_slow;
414 }
415
416
417 ###################################
418 # interpolation management loop
419 ###################################
420
421 var interpolation_loop = func {
422
423 if (local_weather_running_flag == 0) {return;}
424
425 var viewpos = geo.aircraft_position();
426
427
428
429 #var vis_before = getprop(lwi~"visibility-m");
430 var vis_before = interpolated_conditions.visibility_m;
431
432 # if applicable, do some work for fps sampling
433
434 if (fps_control_flag == 1)
435 {
436 fps_samples = fps_samples +1;
437 fps_sum = fps_sum + getprop("/sim/frame-rate");
438 }
439
440
441 # determine at which distance we no longer keep an interpolation point, needs to be larger for METAR since points are more scarce
442
443 if (metar_flag == 1)
444 {var distance_to_unload = 250000.0;}
445 else
446 {var distance_to_unload = 120000.0;}
447
448 # if we can set environment without a reset, the loop can run a bit faster for smoother interpolation
449 # so determine the suitable timing
450
451
452 var interpolation_loop_time = 0.1;
453 var vlimit = 1.01;
454
455
456 # get an inverse distance weighted average from all defined weather stations
457
458 var sum_alt = 0.0;
459 var sum_vis = 0.0;
460 var sum_T = 0.0;
461 var sum_p = 0.0;
462 var sum_D = 0.0;
463 var sum_norm = 0.0;
464
465 var n_stations = size(weatherStationArray);
466
467 for (var i = 0; i < n_stations; i=i+1) {
468
469 var s = weatherStationArray[i];
470
471
472 var stpos = geo.Coord.new();
473 stpos.set_latlon(s.lat,s.lon,0.0);
474
475 var d = viewpos.distance_to(stpos);
476 if (d <100.0) {d = 100.0;} # to prevent singularity at zero
477
478 sum_norm = sum_norm + 1./d * s.weight;
479
480 sum_alt = sum_alt + (s.alt/d) * s.weight;
481 sum_vis = sum_vis + (s.vis/d) * s.weight;
482 sum_T = sum_T + (s.T/d) * s.weight;
483 sum_D = sum_D + (s.D/d) * s.weight;
484 sum_p = sum_p + (s.p/d) * s.weight;
485
486 # gradually fade in the interpolation weight of newly added stations to
487 # avoid sudden jumps
488
489 if (s.weight < 1.0) {s.weight = s.weight + 0.02;}
490
491 # automatically delete stations out of range
492 # take care not to unload if weird values appear for a moment
493 # never unload if only one station left
494 if ((d > distance_to_unload) and (d < (distance_to_unload + 20000.0)) and (n_stations > 1))
495 {
496 if (debug_output_flag == 1)
497 {print("Distance to weather station ", d, " m, unloading ...", i);}
498 weatherStationArray = weather_tile_management.delete_from_vector(weatherStationArray,i);
499 i = i-1; n_stations = n_stations -1;
500 }
501 }
502
503 setprop(lwi~"station-number", i+1);
504
505
506 var ialt = sum_alt/sum_norm;
507 var vis = sum_vis/sum_norm;
508 var p = sum_p/sum_norm;
509 var D = sum_D/sum_norm + temperature_offset;
510 var T = sum_T/sum_norm + temperature_offset;
511
512
513
514 # get an inverse distance weighted average from all defined atmospheric condition points
515
516 sum_norm = 0.0;
517 var sum_vis_aloft = 0.0;
518 var sum_vis_alt1 = 0.0;
519 var sum_vis_ovcst = 0.0;
520 var sum_ovcst = 0.0;
521 var sum_ovcst_alt_low = 0.0;
522 var sum_ovcst_alt_high = 0.0;
523 var sum_scatt = 0.0;
524 var sum_scatt_alt_low = 0.0;
525 var sum_scatt_alt_high = 0.0;
526
527 var n_iPoints = size(atmosphereIpointArray);
528
529 for (var i = 0; i < n_iPoints; i=i+1) {
530
531 var a = atmosphereIpointArray[i];
532
533
534 var apos = geo.Coord.new();
535 apos.set_latlon(a.lat,a.lon,0.0);
536
537 var d = viewpos.distance_to(apos);
538 if (d <100.0) {d = 100.0;} # to prevent singularity at zero
539
540 sum_norm = sum_norm + 1./d * a.weight;
541 sum_vis_aloft = sum_vis_aloft + (a.vis_aloft/d) * a.weight;
542 sum_vis_alt1 = sum_vis_alt1 + (a.vis_alt1/d) * a.weight;
543 sum_vis_ovcst = sum_vis_ovcst + (a.vis_ovcst/d) * a.weight;
544 sum_ovcst = sum_ovcst + (a.ovcst/d) * a.weight;
545 sum_ovcst_alt_low = sum_ovcst_alt_low + (a.ovcst_alt_low/d) * a.weight;
546 sum_ovcst_alt_high = sum_ovcst_alt_high + (a.ovcst_alt_high/d) * a.weight;
547 sum_scatt = sum_scatt + (a.scatt/d) * a.weight;
548 sum_scatt_alt_low = sum_scatt_alt_low + (a.scatt_alt_low/d) * a.weight;
549 sum_scatt_alt_high = sum_scatt_alt_high + (a.scatt_alt_high/d) * a.weight;
550
551 # gradually fade in the interpolation weight of newly added stations to
552 # avoid sudden jumps
553
554 if (a.weight < 1.0) {a.weight = a.weight + 0.02;}
555
556 # automatically delete stations out of range
557 # take care not to unload if weird values appear for a moment
558 # never unload if only one station left
559 if ((d > distance_to_unload) and (d < (distance_to_unload + 20000.0)) and (n_iPoints > 1))
560 {
561 if (debug_output_flag == 1)
562 {print("Distance to atmosphere interpolation point ", d, " m, unloading ...", i);}
563 atmosphereIpointArray = weather_tile_management.delete_from_vector(atmosphereIpointArray,i);
564 i = i-1; n_iPoints = n_iPoints -1;
565 }
566 }
567
568 setprop(lwi~"atmosphere-ipoint-number", i+1);
569
570
571
572 var vis_aloft = sum_vis_aloft/sum_norm;
573 var vis_alt1 = sum_vis_alt1/sum_norm;
574 var vis_ovcst = sum_vis_ovcst/sum_norm;
575 var ovcst_max = sum_ovcst/sum_norm;
576 var ovcst_alt_low = sum_ovcst_alt_low/sum_norm;
577 var ovcst_alt_high = sum_ovcst_alt_high/sum_norm;
578 var scatt_max = sum_scatt/sum_norm;
579 var scatt_alt_low = sum_scatt_alt_low/sum_norm;
580 var scatt_alt_high = sum_scatt_alt_high/sum_norm;
581
582
583
584
585 # altitude model for visibility - increase above the lowest inversion layer to simulate ground haze
586
587 vis = vis * ground_haze_factor;
588
589 var altitude = getprop("position/altitude-ft");
590 # var current_mean_terrain_elevation = ialt;
591
592 var alt1 = vis_alt1;
593 var alt2 = alt1 + 1500.0;
594
595
596 setprop("/environment/ground-visibility-m",vis);
597 setprop("/environment/ground-haze-thickness-m",alt2 * ft_to_m);
598
599 # compute the visibility gradients
600
601 if (realistic_visibility_flag == 1)
602 {
603 vis_aloft = vis_aloft * 2.0;
604 vis_ovcst = vis_ovcst * 3.0;
605 }
606
607 var inc1 = 0.0 * (vis_aloft - vis)/(vis_alt1 - ialt);
608 var inc2 = 0.9 * (vis_aloft - vis)/1500.0;
609 var inc3 = (vis_ovcst - vis_aloft)/(ovcst_alt_high - vis_alt1+1500);
610 var inc4 = 0.5;
611
612
613 if (realistic_visibility_flag == 1)
614 {inc4 = inc4 * 3.0;}
615
616 # compute the visibility
617
618 if (altitude < alt1)
619 {vis = vis + inc1 * altitude;}
620 else if (altitude < alt2)
621 {
622 vis = vis + inc1 * alt1 + inc2 * (altitude - alt1);
623 }
624 else if (altitude < ovcst_alt_high)
625 {
626 vis = vis + inc1 * alt1 + inc2 * (alt2-alt1) + inc3 * (altitude - alt2);
627 }
628 else if (altitude > ovcst_alt_high)
629 {
630 vis = vis + inc1 * alt1 + inc2 * (alt2-alt1) + inc3 * (ovcst_alt_high - alt2) + inc4 * (altitude - ovcst_alt_high);
631 }
632
633 # limit visibility (otherwise memory consumption may be very bad...)
634
635 if (vis > max_vis_range)
636 {vis = max_vis_range;}
637
638
639 # determine scattering shader parameters if scattering shader is on
640
641 if (scattering_shader_flag == 1)
642 {
643
644 # values to be used with new exposure filter
645 var rayleigh = 0.0003;
646 var mie = 0.005;
647 var density = 0.3;
648
649 var vis_factor = (vis - 30000.0)/90000.0;
650 if (vis_factor < 0.0) {vis_factor = 0.0;}
651 if (vis_factor > 1.0) {vis_factor = 1.0;}
652
653
654 if (altitude < 36000.0)
655 {
656 rayleigh = 0.0003 - 0.0001 * vis_factor;
657 mie = 0.005 - vis_factor * 0.002;
658 }
659 else if (altitude < 85000.0)
660 {
661 rayleigh = (0.0003 - 0.0001 * vis_factor) - (altitude-36000.0)/49000.0 * 0.0001;
662 mie = 0.005 - vis_factor * 0.002 - (altitude-36000.0)/49000.0 * 0.002;
663 }
664 else
665 {rayleigh = 0.0002 - 0.0001 * vis_factor; mie = 0.003 - vis_factor * 0.002;}
666
667 # now the pollution factor
668
669 if (altitude < alt1)
670 {
671 rayleigh = rayleigh +0.0003 * air_pollution_norm + 0.0004 * air_pollution_norm * (1.0 - (altitude/alt1) * (altitude/alt1));
672 density = density + 0.05 * air_pollution_norm + 0.05 * air_pollution_norm * (1.0 - (altitude/alt1) * (altitude/alt1));
673 }
674 else
675 {
676 rayleigh = rayleigh + 0.0003 * air_pollution_norm;
677 density = density + 0.05 * air_pollution_norm;
678 }
679
680
681 }
682
683
684 # compute the horizon shading
685
686 if (altitude < scatt_alt_low)
687 {
688 var scatt = scatt_max;
689 }
690 else if (altitude < scatt_alt_high)
691 {
692 var scatt = scatt_max + (0.95 - scatt_max) * (altitude - scatt_alt_low)/(scatt_alt_high-scatt_alt_low);
693 }
694 else
695 {var scatt = 0.95;}
696
697
698 # compute the cloud layer self shading correction
699
700 var sun_angle = 1.57079632675 - getprop("/sim/time/sun-angle-rad");
701 var cloud_layer_shading = 1.0 - (0.8*(1.0 - scatt_max) * math.pow(math.cos(sun_angle),100.0));
702
703 # compute the overcast haze
704
705 if (altitude < ovcst_alt_low)
706 {
707 var ovcst = ovcst_max;
708 }
709 else if (altitude < ovcst_alt_high)
710 {
711 var ovcst = ovcst_max - ovcst_max * (altitude - ovcst_alt_low)/(ovcst_alt_high-ovcst_alt_low);
712 }
713 else
714 {var ovcst = 0.0;}
715
716
717 # compute heating and cooling of various terrain and object types
718
719 var time = getprop("sim/time/utc/day-seconds");
720 time = time + getprop("sim/time/local-offset");
721
722 # low thermal inertia follows the Sun more or less directly
723 # high thermal inertia takes some time to reach full heat
724
725 var t_factor1 = 0.5 * (1.0-math.cos((time * sec_to_rad)));
726 var t_factor2 = 0.5 * (1.0-math.cos((time * sec_to_rad)-0.4));
727 var t_factor3 = 0.5 * (1.0-math.cos((time * sec_to_rad)-0.9));
728
729 var amp = scatt_max;
730
731 setprop("/environment/surface/delta-T-soil", amp * (-5.0 + 10.0 * t_factor2));
732 setprop("/environment/surface/delta-T-vegetation", amp * (-5.0 + 10.0 * t_factor1));
733 setprop("/environment/surface/delta-T-rock", amp * (-7.0 + 14.0 * t_factor1));
734 setprop("/environment/surface/delta-T-water", amp * (-1.0 + 2.0* t_factor3));
735 setprop("/environment/surface/delta-T-structure", amp * 10.0* t_factor1);
736 setprop("/environment/surface/delta-T-cloud", amp * (-2.0 + 2.0* t_factor3));
737
738 # compute base turbulence
739
740 var base_turbulence = 0.0;
741
742 if (altitude < alt1)
743 {
744 base_turbulence = lowest_layer_turbulence;
745 }
746
747
748
749 # limit relative changes of the visibility, will make for gradual transitions
750
751 if (vis/vis_before > vlimit)
752 {vis = vlimit * vis_before;}
753 else if (vis/vis_before < (2.0-vlimit))
754 {vis = (2.0-vlimit) * vis_before;}
755
756
757
758
759 # write all properties into the weather interpolation record
760
761 setprop(lwi~"mean-terrain-altitude-ft",ialt);
762
763
764 if (vis > 0.0) interpolated_conditions.visibility_m = vis;
765 interpolated_conditions.temperature_degc = T;
766 interpolated_conditions.dewpoint_degc = D;
767 if (p>10.0) interpolated_conditions.pressure_sea_level_inhg = p;
768
769
770
771 if (scattering_shader_flag == 1)
772 {
773 local_weather.setSkydomeShader(rayleigh, mie, density);
774 setprop("/environment/cloud-self-shading", cloud_layer_shading);
775 }
776
777 local_weather.setScattering(scatt);
778 local_weather.setOvercast(ovcst);
779
780
781
782
783 # now check if an effect volume writes the property and set only if not
784 # but set visibility if interpolated is smaller than effect-specified
785
786 var flag = getprop("local-weather/effect-volumes/number-active-vis");
787
788 if ((flag ==0) and (vis > 0.0) and (getprop(lw~"lift-loop-flag") == 0) and (compat_layer.smooth_visibility_loop_flag == 0))
789 {
790 compat_layer.setVisibility(vis);
791 }
792 else if ((getprop("/local-weather/current/visibility-m") > vis) and (compat_layer.smooth_visibility_loop_flag == 0))
793 {
794 compat_layer.setVisibility(vis);
795 }
796
797
798
799
800
801 flag = getprop("local-weather/effect-volumes/number-active-lift");
802
803 if (flag ==0)
804 {
805 #setprop(lw~"current/thermal-lift",0.0);
806 }
807
808 # no need to check for these, as they are not modelled in effect volumes
809
810 compat_layer.setTemperature(T);
811 compat_layer.setDewpoint(D);
812 if (p>0.0) {compat_layer.setPressure(p);}
813
814
815 # determine whether low haze is icy and whether we see scattering
816
817 var ice_hex_sheet = 0.0;
818 var ice_hex_column = 0.0;
819
820 if (T < -5.0)
821 {
822 ice_hex_column = (-T - 5.0) /10.0;
823 ice_hex_sheet = (-T - 10.0 + (T-D)) /20.0;
824
825
826 var sheet_bias = (T-D)/ 20;
827 if (sheet_bias > 1.0) {sheet_bias = 1.0;}
828 ice_hex_column = ice_hex_column * sheet_bias;
829
830 if (ice_hex_sheet > 1.0) {ice_hex_sheet = 1.0;}
831 if (ice_hex_column > 1.0) {ice_hex_column = 1.0;}
832 }
833 #print("Col: ",ice_hex_column);
834 #print("Sheet: ", ice_hex_sheet);
835
836 setprop("/environment/scattering-phenomena/ice-hexagonal-column-factor", ice_hex_column);
837 setprop("/environment/scattering-phenomena/ice-hexagonal-sheet-factor", ice_hex_sheet);
838
839 # now determine the local wind
840
841
842 var tile_index = getprop(lw~"tiles/tile[4]/tile-index");
843
844 if (wind_model_flag ==1) # constant
845 {
846 var winddir = weather_dynamics.tile_wind_direction[0];
847 var windspeed = weather_dynamics.tile_wind_speed[0];
848
849 wind.cloudlayer = [winddir,windspeed];
850
851 }
852 else if (wind_model_flag ==2) # constant in tile
853 {
854 var winddir = weather_dynamics.tile_wind_direction[tile_index-1];
855 var windspeed = weather_dynamics.tile_wind_speed[tile_index-1];
856
857 wind.cloudlayer = [winddir, windspeed];
858
859 }
860 else if (wind_model_flag ==3) # aloft interpolated, constant in tiles
861 {
862 var w = windIpointArray[0];
863 var res = wind_altitude_interpolation(altitude,w);
864 var winddir = res[0];
865 var windspeed = res[1];
866
867 wind.cloudlayer = wind_altitude_interpolation(0.0,w);
868
869 }
870 else if (wind_model_flag == 5) # aloft waypoint interpolated
871 {
872 var res = wind_interpolation(viewpos.lat(), viewpos.lon(), altitude);
873
874 var winddir = res[0];
875 var windspeed = res[1];
876
877 wind.cloudlayer = wind_interpolation(viewpos.lat(), viewpos.lon(), 0.0);
878 }
879
880
881 wind.surface = [wind.cloudlayer[0], wind.cloudlayer[1] * get_slowdown_fraction()];
882
883 # now do the boundary layer computations
884
885 var altitude_agl = getprop("/position/altitude-agl-ft");
886
887 if (altitude_agl < 50.0)
888 {
889 base_turbulence = base_turbulence * altitude_agl/50.0;
890 }
891
892
893 if (presampling_flag == 0)
894 {
895 var boundary_alt = 600.0;
896 var windspeed_ground = windspeed/3.0;
897
898 var f_min = 2.0/3.0;
899
900 if (altitude_agl < boundary_alt)
901 {var windspeed_current = windspeed_ground + 2.0 * windspeed_ground * (altitude_agl/boundary_alt);}
902 else
903 {var windspeed_current = windspeed;}
904 }
905 else
906 {
907 var alt_median = alt_50_array[tile_index - 1];
908 var alt_difference = alt_median - (altitude - altitude_agl);
909 var base_layer_thickness = 150.0;
910
911 # get the boundary layer size dependent on terrain altitude above terrain median
912
913 if (alt_difference > 0.0) # we're low and the boundary layer grows
914 {var boundary_alt = base_layer_thickness + 0.3 * alt_difference;}
915 else # the boundary layer shrinks
916 {var boundary_alt = base_layer_thickness + 0.1 * alt_difference;}
917
918 if (boundary_alt < 50.0){boundary_alt = 50.0;}
919 if (boundary_alt > 3000.0) {boundary_alt = 3000.0;}
920
921 # get the boundary effect as a function of bounday layer size
922
923 var f_min = 0.2 + 0.17 * math.ln(boundary_alt/base_layer_thickness);
924
925
926 if (altitude_agl < boundary_alt)
927 {
928 var windspeed_current = (1-f_min) * windspeed + f_min * windspeed * (altitude_agl/boundary_alt);
929 }
930 else
931 {var windspeed_current = windspeed;}
932
933 }
934
935
936 var windspeed_ground = (1.0-f_min) * windspeed;
937
938
939 # set the wind hash before gusts, it represents mean wind
940
941 wind.current = [winddir,windspeed_current];
942
943
944
945 # determine gusts and turbulence in the bounday layer
946
947 var gust_frequency = getprop(lw~"tmp/gust-frequency-hz");
948
949
950
951
952 if (gust_frequency > 0.0)
953 {
954 var gust_relative_strength = getprop(lw~"tmp/gust-relative-strength");
955 var gust_angvar = getprop(lw~"tmp/gust-angular-variation-deg");
956
957 # if we have variability in the direction of the wind, the winds will
958 # drift by the Markov chain code below to adjust to a new winddir as computed
959 # above - however if the wind is not variable but still gusty, this won't happen
960 # so we have to take care of it explicitly
961
962 if (gust_angvar > 0.0)
963 {var winddir_last = interpolated_conditions.wind_from_heading_deg;}
964 else
965 {var winddir_last = winddir;}
966
967 var alt_scaling_factor = 1.2 * windspeed / 10.0;
968 if (alt_scaling_factor < 1.0) {alt_scaling_factor = 1.0;}
969
970 # expected mean number of gusts in time interval (should be < 1)
971 var p_gust = gust_frequency * interpolation_loop_time;
972
973 # real time series show a ~10-30 second modulation as well
974 var p_squall = gust_frequency * 0.1 * interpolation_loop_time;
975 var squall_scale = getprop("/local-weather/tmp/squall-scaling-norm");
976
977 if (rand() < p_squall)
978 {
979 squall_scale = rand();
980 # prefer large changes
981 if ((squall_scale > 0.3) and (squall_scale < 0.7))
982 {squall_scale = rand();}
983
984 setprop("/local-weather/tmp/squall-scaling-norm", squall_scale);
985 }
986
987 winddir_change = 0.0;
988
989 if (rand() < p_gust) # we change the offsets for windspeed and direction
990 {
991 var alt_fact = 1.0 - altitude_agl/(boundary_alt * alt_scaling_factor);
992 if (alt_fact < 0.0) {alt_fact = 0.0};
993
994 var random_factor = 0.3 * rand() + 0.7 * squall_scale;
995
996 windspeed_multiplier = (1.0 + (random_factor * gust_relative_strength * alt_fact));
997 winddir_change = alt_fact * (1.0 - 2.0 * rand()) * gust_angvar;
998 winddir_change = winddir_change * 0.2; # Markov chain parameter, max. change per frame is 1/5
999
1000 # if the Markov chain reaches the boundary, reflect
1001
1002 #print("Winddir: ", winddir, " winddir_last: ", winddir_last, " winddir_change: ", winddir_change);
1003 if (weather_tile_management.relangle(winddir_last + winddir_change, winddir) > gust_angvar)
1004 {winddir_change = -winddir_change;}
1005
1006 }
1007 windspeed_current = windspeed_current * windspeed_multiplier;
1008 winddir = winddir_last + winddir_change;
1009 }
1010
1011
1012
1013
1014
1015 compat_layer.setWindSmoothly(winddir, windspeed_current);
1016
1017 # set the interpolated conditions to the wind including gust
1018
1019 interpolated_conditions.wind_from_heading_deg = winddir;
1020 interpolated_conditions.windspeed_kt = windspeed_current;
1021
1022 # hack to get access to the water shader
1023
1024 setprop("/environment/config/boundary/entry[0]/wind-from-heading-deg",winddir);
1025 setprop("/environment/config/boundary/entry[0]/wind-speed-kt",windspeed_ground);
1026
1027 # end hack
1028
1029
1030
1031
1032 # set turbulence
1033 flag = getprop("local-weather/effect-volumes/number-active-turb");
1034
1035 var wind_enhancement_factor = windspeed_current/15.0;
1036 if (wind_enhancement_factor > 1.5) {wind_enhancement_factor = 1.5;}
1037
1038 var volcanic_turbulence = getprop("/environment/volcanoes/turbulence");
1039
1040 var total_turbulence = base_turbulence * wind_enhancement_factor + volcanic_turbulence;
1041 if (total_turbulence > 1.0) {total_turbulence = 1.0;}
1042
1043 if ((flag ==0))
1044 {compat_layer.setTurbulence(total_turbulence);}
1045
1046 # set scattering on the ground - this doesn't affect fog but is diffuse and specular light reduction
1047 # so it is stronger than normal scattering
1048
1049 var scatt_ground = (scatt_max - 0.4)/0.6;
1050 if (scatt_ground < 0.0) {scatt_ground = 0.0;}
1051
1052 setprop("/environment/surface/scattering", scatt_ground);
1053
1054 if (getprop(lw~"interpolation-loop-flag") ==1) {settimer(interpolation_loop, 0.0);}
1055
1056 }
1057
1058 ###################################
1059 # thermal lift loop startup
1060 ###################################
1061
1062 var thermal_lift_start = func (ev) {
1063
1064
1065 # if another lift loop is already running, do nothing
1066 if (getprop(lw~"lift-loop-flag") == 1) {return;}
1067
1068 # copy the properties from effect volume to the lift object
1069
1070 l = thermalLift.new(ev.lat, ev.lon, ev.radius, ev.height, ev.cn, ev.sh, ev.max_lift, ev.f_lift_radius);
1071
1072 l.index = ev.index;
1073
1074 if (dynamics_flag == 1)
1075 {
1076 l.timestamp = weather_dynamics.time_lw;
1077 if (dynamical_convection_flag == 1)
1078 {
1079 l.flt = ev.flt;
1080 l.evolution_timestamp = ev.evolution_timestamp;
1081 }
1082 }
1083
1084
1085
1086 thermal = l;
1087
1088 if (debug_output_flag == 1)
1089 {
1090 print("Entering thermal lift...");
1091 print("strength: ", thermal.max_lift, " radius: ", thermal.radius);
1092 if (dynamical_convection_flag ==1)
1093 {print("fractional lifetime: ", thermal.flt);}
1094
1095 }
1096
1097 # and start the lift loop, unless another one is already running
1098 # so we block overlapping calls
1099
1100
1101 setprop(lw~"lift-loop-flag",1);
1102 settimer(thermal_lift_loop,0);
1103
1104 }
1105
1106 ###################################
1107 # thermal lift loop
1108 ###################################
1109
1110 var thermal_lift_loop = func {
1111
1112 if (local_weather_running_flag == 0) {return;}
1113
1114 var apos = geo.aircraft_position();
1115
1116 var tlat = thermal.lat;
1117 var tlon = thermal.lon;
1118
1119 var tpos = geo.Coord.new();
1120 tpos.set_latlon(tlat,tlon,0.0);
1121
1122 var d = apos.distance_to(tpos);
1123 var alt = getprop("position/altitude-ft");
1124
1125 if (dynamical_convection_flag == 1)
1126 {var flt = thermal.flt;}
1127 else
1128 {var flt = 0.5;}
1129
1130 var lift = calcLift(d, alt, thermal.radius, thermal.height, thermal.cn, thermal.sh, thermal.max_lift, thermal.f_lift_radius, flt);
1131
1132 if (getprop(lw~"wave-loop-flag") ==1)
1133 {
1134 lift = lift + getprop(lw~"current/wave-lift");
1135 }
1136
1137 # compute a reduction in visibility when entering the cloudbase
1138
1139 #var vis = getprop(lw~"interpolation/visibility-m");
1140
1141 var vis = interpolated_conditions.visibility_m;
1142
1143 if (alt > 0.9 * thermal.height)
1144 {
1145 var visibility_reduction = math.pow((alt - 0.9 * thermal.height)/(0.2 * thermal.height),0.1);
1146 visibility_reduction = visibility_reduction * (1.0 - math.pow(d/(0.8*thermal.radius),14));
1147
1148 if (visibility_reduction > 1.0) {visibility_reduction = 1.0;} # this shouldn't ever happen
1149 if (visibility_reduction < 0.0) {visibility_reduction = 0.0;}
1150 vis = vis * (1.0 - 0.98 * visibility_reduction);
1151
1152 }
1153
1154 setprop(lw~"current/visibility-m",vis);
1155 compat_layer.setVisibility(vis);
1156
1157
1158
1159
1160 setprop(lw~"current/thermal-lift",lift);
1161 compat_layer.setLift(lift);
1162
1163 # if dynamics is on, move the thermal and occasionally compute altitude and age
1164
1165 if (dynamics_flag == 1)
1166 {
1167 thermal.move();
1168
1169 if ((rand() < 0.01) and (presampling_flag == 1)) # check every 100 frames
1170 {
1171 if (dynamical_convection_flag == 1)
1172 {
1173 thermal.correct_altitude_and_age();
1174 if (thermal.flt > 1.1)
1175 {thermal_lift_stop();}
1176 }
1177 else
1178 {
1179 thermal.correct_altitude();
1180 }
1181 }
1182 }
1183
1184
1185 if (getprop(lw~"lift-loop-flag") ==1) {settimer(thermal_lift_loop, 0);}
1186 }
1187
1188
1189
1190
1191
1192 ###################################
1193 # thermal lift loop stop
1194 ###################################
1195
1196 var thermal_lift_stop = func {
1197
1198 setprop(lw~"lift-loop-flag",0);
1199 setprop(lw~"current/thermal-lift",0.0);
1200 compat_layer.setLift(0.0);
1201
1202 if (debug_output_flag == 1)
1203 {
1204 print("Leaving thermal lift...");
1205 }
1206
1207 }
1208
1209
1210 ###################################
1211 # wave lift loop startup
1212 ###################################
1213
1214 var wave_lift_start = func (ev) {
1215
1216 # copy the properties from effect volume to the wave object
1217
1218
1219 w = waveLift.new (ev.lat, ev.lon, ev.r1, ev.r2, ev.phi, ev.height, ev.max_lift);
1220 w.index = ev.index;
1221 wave = w;
1222
1223 # and start the lift loop, unless another one is already running
1224 # so we block overlapping calls
1225
1226 if (getprop(lw~"wave-loop-flag") == 0)
1227 {setprop(lw~"wave-loop-flag",1); settimer(wave_lift_loop,0);}
1228
1229 }
1230
1231 ###################################
1232 # wave lift loop
1233 ###################################
1234
1235 var wave_lift_loop = func {
1236
1237 if (local_weather_running_flag == 0) {return;}
1238
1239 var lat = getprop("position/latitude-deg");
1240 var lon = getprop("position/longitude-deg");
1241 var alt = getprop("position/altitude-ft");
1242
1243
1244 var phi = wave.phi * math.pi/180.0;
1245
1246 var xx = (lon - wave.lon) * lon_to_m;
1247 var yy = (lat - wave.lat) * lat_to_m;
1248
1249 var x = xx * math.cos(phi) - yy * math.sin(phi);
1250 var y = yy * math.cos(phi) + xx * math.sin(phi);
1251
1252 var lift = calcWaveLift(x,y,alt);
1253
1254 # check if we are in a thermal, if so set wave lift and let the thermal lift loop add that
1255
1256 if (getprop(lw~"lift-loop-flag") ==1)
1257 {
1258 setprop(lw~"current/wave-lift",lift);
1259 }
1260 else
1261 {
1262 setprop(lw~"current/thermal-lift",lift);
1263 }
1264
1265 if (getprop(lw~"wave-loop-flag") ==1) {settimer(wave_lift_loop, 0);}
1266 }
1267
1268
1269
1270
1271 ###################################
1272 # wave lift loop stop
1273 ###################################
1274
1275 var wave_lift_stop = func {
1276
1277 setprop(lw~"wave-loop-flag",0);
1278 setprop(lw~"current/thermal-lift",0.0);
1279 }
1280
1281
1282
1283 ####################################
1284 # action taken when in effect volume
1285 ####################################
1286
1287 var effect_volume_start = func (ev) {
1288
1289 var cNode = props.globals.getNode(lw~"current");
1290
1291
1292 if (ev.vis_flag ==1)
1293 {
1294 # first store the current setting in case we need to restore on leaving
1295
1296 var vis = ev.vis;
1297 ev.vis_r = cNode.getNode("visibility-m").getValue();
1298
1299 # then set the new value in current and execute change
1300 cNode.getNode("visibility-m").setValue(vis);
1301 #compat_layer.setVisibility(vis);
1302 #print(vis);
1303 compat_layer.setVisibilitySmoothly(vis);
1304
1305 # then count the number of active volumes on entry (we need that to determine
1306 # what to do on exit)
1307 ev.n_entry_vis = getprop(lw~"effect-volumes/number-active-vis");
1308
1309 # and add to the counter
1310 setprop(lw~"effect-volumes/number-active-vis",getprop(lw~"effect-volumes/number-active-vis")+1);
1311 }
1312
1313 if (ev.rain_flag == 1)
1314 {
1315 var rain = ev.rain;
1316 #print("Setting rain to:", rain);
1317 ev.rain_r = cNode.getNode("rain-norm").getValue();
1318 cNode.getNode("rain-norm").setValue(rain);
1319 compat_layer.setRain(rain);
1320 ev.n_entry_rain = getprop(lw~"effect-volumes/number-active-rain");
1321 setprop(lw~"effect-volumes/number-active-rain",getprop(lw~"effect-volumes/number-active-rain")+1);
1322 }
1323 if (ev.snow_flag == 1)
1324 {
1325 var snow = ev.snow;
1326 ev.snow_r = cNode.getNode("snow-norm").getValue();
1327 cNode.getNode("snow-norm").setValue(snow);
1328 compat_layer.setSnow(snow);
1329 ev.n_entry_snow = getprop(lw~"effect-volumes/number-active-snow");
1330 setprop(lw~"effect-volumes/number-active-snow",getprop(lw~"effect-volumes/number-active-snow")+1);
1331 }
1332 if (ev.turb_flag == 1)
1333 {
1334 var turbulence = ev.turb;
1335 ev.turb_r = cNode.getNode("turbulence").getValue();
1336 cNode.getNode("turbulence").setValue(turbulence);
1337 compat_layer.setTurbulence(turbulence);
1338 ev.n_entry_turb = getprop(lw~"effect-volumes/number-active-turb");
1339 setprop(lw~"effect-volumes/number-active-turb",getprop(lw~"effect-volumes/number-active-turb")+1);
1340 }
1341 if (ev.sat_flag == 1)
1342 {
1343 var saturation = ev.sat;
1344 ev.sat_r = getprop("/rendering/scene/saturation");
1345 compat_layer.setLightSmoothly(saturation);
1346 ev.n_entry_sat = getprop(lw~"effect-volumes/number-active-sat");
1347 setprop(lw~"effect-volumes/number-active-sat",getprop(lw~"effect-volumes/number-active-sat")+1);
1348 }
1349
1350 if (ev.lift_flag == 1)
1351 {
1352 var lift = ev.lift;
1353 ev.lift_r = cNode.getNode("thermal-lift").getValue();
1354 cNode.getNode("thermal-lift").setValue(lift);
1355 compat_layer.setLift(lift);
1356 ev.n_entry_lift = getprop(lw~"effect-volumes/number-active-lift");
1357 setprop(lw~"effect-volumes/number-active-lift",getprop(lw~"effect-volumes/number-active-lift")+1);
1358 }
1359 else if (ev.lift_flag == 2)
1360 {
1361 ev.lift_r = cNode.getNode("thermal-lift").getValue();
1362 ev.n_entry_lift = getprop(lw~"effect-volumes/number-active-lift");
1363 setprop(lw~"effect-volumes/number-active-lift",getprop(lw~"effect-volumes/number-active-lift")+1);
1364 thermal_lift_start(ev);
1365 }
1366 else if (ev.lift_flag == 3)
1367 {
1368 ev.lift_r = cNode.getNode("thermal-lift").getValue();
1369 ev.n_entry_lift = getprop(lw~"effect-volumes/number-active-lift");
1370 setprop(lw~"effect-volumes/number-active-lift",getprop(lw~"effect-volumes/number-active-lift")+1);
1371 wave_lift_start(ev);
1372 }
1373
1374 }
1375
1376
1377
1378 var effect_volume_stop = func (ev) {
1379
1380 var cNode = props.globals.getNode(lw~"current");
1381
1382
1383 if (ev.vis_flag == 1)
1384 {
1385
1386 var n_active = getprop(lw~"effect-volumes/number-active-vis");
1387
1388
1389 var n_entry = ev.n_entry_vis;
1390
1391 # if no other nodes affecting property are active, restore to outside
1392 # else restore settings as they have been when entering the volume when the number
1393 # of active volumes is the same as on entry (i.e. volumes are nested), otherwise
1394 # leave property at current because new definitions are already active and should not
1395 # be cancelled
1396
1397 if (n_active ==1){var vis = interpolated_conditions.visibility_m;}
1398 else if ((n_active -1) == n_entry)
1399 {var vis = ev.vis_r;}
1400 else {var vis = cNode.getNode("visibility-m").getValue();}
1401 cNode.getNode("visibility-m").setValue(vis);
1402 compat_layer.setVisibilitySmoothly(vis);
1403
1404 # and subtract from the counter
1405 setprop(lw~"effect-volumes/number-active-vis",getprop(lw~"effect-volumes/number-active-vis")-1);
1406 }
1407 if (ev.rain_flag == 1)
1408 {
1409 var n_active = getprop(lw~"effect-volumes/number-active-rain");
1410 var n_entry = ev.n_entry_rain;
1411
1412 if (n_active ==1){var rain = interpolated_conditions.rain_norm;}
1413 else if ((n_active -1) == n_entry)
1414 {var rain = ev.rain_r;}
1415 else {var rain = cNode.getNode("rain-norm").getValue();}
1416 cNode.getNode("rain-norm").setValue(rain);
1417 compat_layer.setRain(rain);
1418 setprop(lw~"effect-volumes/number-active-rain",getprop(lw~"effect-volumes/number-active-rain")-1);
1419 }
1420
1421 if (ev.snow_flag == 1)
1422 {
1423 var n_active = getprop(lw~"effect-volumes/number-active-snow");
1424 var n_entry = ev.n_entry_snow;
1425
1426 if (n_active ==1){var snow = interpolated_conditions.snow_norm;}
1427 else if ((n_active -1) == n_entry)
1428 {var snow = ev.snow_r;}
1429 else {var snow = cNode.getNode("snow-norm").getValue();}
1430 cNode.getNode("snow-norm").setValue(snow);
1431 compat_layer.setSnow(snow);
1432 setprop(lw~"effect-volumes/number-active-snow",getprop(lw~"effect-volumes/number-active-snow")-1);
1433 }
1434
1435 if (ev.turb_flag == 1)
1436 {
1437 var n_active = getprop(lw~"effect-volumes/number-active-turb");
1438 var n_entry = ev.n_entry_turb;
1439 if (n_active ==1){var turbulence = interpolated_conditions.turbulence;}
1440 else if ((n_active -1) == n_entry)
1441 {var turbulence = ev.turb_r;}
1442 else {var turbulence = cNode.getNode("turbulence").getValue();}
1443 cNode.getNode("turbulence").setValue(turbulence);
1444 compat_layer.setTurbulence(turbulence);
1445 setprop(lw~"effect-volumes/number-active-turb",getprop(lw~"effect-volumes/number-active-turb")-1);
1446 }
1447
1448 if (ev.sat_flag == 1)
1449 {
1450 var n_active = getprop(lw~"effect-volumes/number-active-sat");
1451 var n_entry = ev.n_entry_sat;
1452 if (n_active ==1){var saturation = 1.0;}
1453 else if ((n_active -1) == n_entry)
1454 {var saturation = ev.sat_r;}
1455 else {var saturation = getprop("/rendering/scene/saturation");}
1456 compat_layer.setLightSmoothly(saturation);
1457 setprop(lw~"effect-volumes/number-active-sat",getprop(lw~"effect-volumes/number-active-sat")-1);
1458 }
1459
1460 if (ev.lift_flag == 1)
1461 {
1462 var n_active = getprop(lw~"effect-volumes/number-active-lift");
1463 var n_entry = ev.n_entry_lift;
1464 if (n_active ==1){var lift = interpolated_conditions.thermal_lift;}
1465 else if ((n_active -1) == n_entry)
1466 {var lift = ev.lift_r;}
1467 else {var lift = cNode.getNode("thermal-lift").getValue();}
1468 cNode.getNode("thermal-lift").setValue(lift);
1469 compat_layer.setLift(lift);
1470 setprop(lw~"effect-volumes/number-active-lift",getprop(lw~"effect-volumes/number-active-lift")-1);
1471 }
1472 else if (ev.lift_flag == 2)
1473 {
1474 thermal_lift_stop();
1475 setprop(lw~"effect-volumes/number-active-lift",getprop(lw~"effect-volumes/number-active-lift")-1);
1476 }
1477 else if (ev.lift_flag == 3)
1478 {
1479 wave_lift_stop();
1480 setprop(lw~"effect-volumes/number-active-lift",getprop(lw~"effect-volumes/number-active-lift")-1);
1481 }
1482
1483 }
1484
1485
1486
1487 #########################################
1488 # compute thermal lift in detailed model
1489 #########################################
1490
1491 var ts_factor = func (t, alt, height) {
1492
1493 var t1 = 0.1; # fractional time at which lift is fully developed
1494 var t2 = 0.9; # fractional time at which lift starts to decay
1495 var t3 = 1.0; # fractional time at which lift is gone
1496
1497 # no time dependence modelled yet
1498 # return 1.0;
1499
1500
1501
1502 var t_a = t - (alt/height) * t1 - t1;
1503
1504 if (t_a<0) {return 0.0;}
1505 else if (t_a<t1) {return 0.5 + 0.5 * math.cos((1.0-t_a/t1)* math.pi);}
1506 else if (t_a < t2) {return 1.0;}
1507 else {return 0.5 - 0.5 * math.cos((1.0-(t2-t_a)/(t3-t2))*math.pi);}
1508 }
1509
1510 var tl_factor = func (t, alt, height) {
1511
1512 var t1 = 0.1; # fractional time at which lift is fully developed
1513 var t2 = 0.9; # fractional time at which lift starts to decay
1514 var t3 = 1.0; # fractional time at which lift is gone
1515
1516 # no time dependence modelled yet
1517 # return 1.0;
1518
1519 var t_a = t - (alt/height) * t1;
1520
1521 if (t_a<0) {return 0.0;}
1522 else if (t_a<t1) {return 0.5 + 0.5 * math.cos((1.0-t_a/t1)* math.pi);}
1523 else if (t_a < t2) {return 1.0;}
1524 else {return 0.5 - 0.5 * math.cos((1.0-(t2-t_a)/(t3-t2))*math.pi);}
1525 }
1526
1527
1528 var calcLift_max = func (alt, max_lift, height) {
1529
1530 alt_agl = getprop("/position/altitude-agl-ft");
1531
1532 # no lift below ground
1533 if (alt_agl < 0.0) {return 0.0;}
1534
1535 # lift ramps up to full within 200 m
1536 else if (alt_agl < 200.0*m_to_ft)
1537 {return max_lift * 0.5 * (1.0 + math.cos((1.0-alt_agl/(200.0*m_to_ft))*math.pi));}
1538
1539 # constant max. lift in main body
1540 else if ((alt_agl > 200.0*m_to_ft) and (alt < height))
1541 {return max_lift;}
1542
1543 # decreasing lift from cloudbase to 10% above base
1544 else if ((alt > height ) and (alt < height*1.1))
1545 {return max_lift * 0.5 * (1.0 - math.cos((1.0-10.0*(alt-height)/height)*math.pi));}
1546
1547 # no lift available above
1548 else {return 0.0;}
1549 }
1550
1551
1552
1553 var calcLift = func (d, alt, R, height, cn, sh, max_lift, f_lift_radius, t) {
1554
1555 # radius of slice at given altitude
1556 var r_total = (cn + alt/height*(1.0-cn)) * (R - R * (1.0- sh ) * (1.0 - ((2.0*alt/height)-1.0)*((2.0*alt/height)-1.0)));
1557
1558
1559 # print("r_total: ", r_total, "d: ",d);
1560 # print("alt: ", alt, "height: ",height);
1561
1562 # no lift if we're outside the radius or above the thermal
1563 if ((d > r_total) or (alt > 1.1*height)) { return 0.0; }
1564
1565 # fraction of radius providing lift
1566 var r_lift = f_lift_radius * r_total;
1567
1568 # print("r_lift: ", r_lift);
1569
1570 # if we are in the sink portion, get the max. sink for this time and altitude and adjust for actual position
1571 if ((d < r_total ) and (d > r_lift))
1572 {
1573 var s_max = 0.5 * calcLift_max(alt, max_lift, height) * ts_factor(t, alt, height);
1574 # print("s_max: ", s_max);
1575 return s_max * math.sin(math.pi * (1.0 + (d-r_lift) * (1.0/(r_total - r_lift))));
1576 }
1577 # else we are in the lift portion, get the max. lift for this time and altitude and adjust for actual position
1578 else
1579 {
1580 var l_max = calcLift_max(alt, max_lift, height) * tl_factor(t, alt, height);
1581 # print("l_max: ", l_max);
1582 return l_max * math.cos(math.pi * (d/(2.0 * r_lift)));
1583 }
1584 }
1585
1586 #########################################
1587 # compute wave lift in detailed model
1588 #########################################
1589
1590 var calcWaveLift = func (x,y, alt) {
1591
1592 var lift = wave.max_lift * math.cos((y/wave.y) * 1.5 * math.pi);
1593
1594 if (abs(x)/wave.x > 0.9)
1595 {
1596 lift = lift * (abs(x) - 0.9 * wave.x)/(0.1 * wave.x);
1597 }
1598
1599
1600
1601 lift = lift * 2.71828 * math.exp(-alt/wave.height) * alt/wave.height;
1602
1603 var alt_agl = getprop("/position/altitude-agl-ft");
1604
1605 if (alt_agl < 1000.0)
1606 {
1607 lift = lift * (alt_agl/1000.0) * (alt_agl/1000.0);
1608 }
1609
1610 return lift;
1611 }
1612
1613
1614
1615
1616
1617
1618 ###########################################################
1619 # place a single cloud into a vector to be processed
1620 # separately
1621 ###########################################################
1622
1623 var create_cloud_vec = func(path, lat, lon, alt, heading) {
1624
1625 if (path == "new") # we have to switch to new cloud generating routines
1626 {
1627 local_weather.cloudAssembly.lat = lat;
1628 local_weather.cloudAssembly.lon = lon;
1629 local_weather.cloudAssembly.alt = alt;
1630 local_weather.cloudAssembly.top_shade = top_shade;
1631 local_weather.cloudAssembly.alpha_factor = alpha_factor;
1632
1633 #print(lat," ",long, " ", alt);
1634
1635 if (dynamics_flag == 1)
1636 {
1637 local_weather.cloudAssembly.mean_alt = cloud_mean_altitude;
1638 local_weather.cloudAssembly.flt = cloud_fractional_lifetime;
1639 local_weather.cloudAssembly.evolution_timestamp = cloud_evolution_timestamp;
1640 local_weather.cloudAssembly.rel_alt = cloudAssembly.alt - cloud_mean_altitude;
1641 }
1642
1643 append(cloudAssemblyArray,cloudAssembly);
1644
1645 # at this point we insert tracers for the depth buffer
1646
1647 #if (local_weather.cloudAssembly.tracer_flag == 1)
1648 # {
1649 # tracerAssembly = local_weather.cloud.new("Tracer", "default");
1650 # tracerAssembly.texture_sheet = "/Models/Weather/nimbus_sheet1.rgb";
1651 # tracerAssembly.n_sprites = 1;
1652 # tracerAssembly.bottom_shade = 0.0;
1653 # tracerAssembly.top_shade = 0.0;
1654 # tracerAssembly.num_tex_x = 1;
1655 # tracerAssembly.num_tex_y = 1;
1656 # tracerAssembly.lat = lat;
1657 # tracerAssembly.lon = lon;
1658 # tracerAssembly.alt = alt + local_weather.cloudAssembly.min_height *0.35 * m_to_ft ;
1659 # tracerAssembly.min_width = local_weather.cloudAssembly.min_width * 0.35;
1660 # tracerAssembly.max_width = local_weather.cloudAssembly.max_width * 0.35;
1661 # tracerAssembly.min_height = local_weather.cloudAssembly.min_height * 0.35;
1662 # tracerAssembly.max_height = local_weather.cloudAssembly.max_height * 0.35;
1663 # tracerAssembly.min_cloud_width = local_weather.cloudAssembly.min_cloud_width * 0.35;
1664 # tracerAssembly.min_cloud_height = local_weather.cloudAssembly.min_cloud_height * 0.35;
1665 # tracerAssembly.z_scale = local_weather.cloudAssembly.z_scale;
1666 # append(cloudAssemblyArray,tracerAssembly);
1667 # }
1668
1669 return;
1670 }
1671
1672 append(clouds_path,path);
1673 append(clouds_lat,lat);
1674 append(clouds_lon,lon);
1675 append(clouds_alt,alt);
1676 append(clouds_orientation,heading);
1677
1678 # globals (needed for Cumulus clouds) should be set if needed by the main cloud generating call
1679
1680 if (dynamics_flag ==1)
1681 {
1682 append(clouds_mean_alt, cloud_mean_altitude);
1683 append(clouds_flt, cloud_fractional_lifetime);
1684 append(clouds_evolution_timestamp,cloud_evolution_timestamp);
1685 }
1686
1687 }
1688 ###########################################################
1689 # clear all clouds and effects
1690 ###########################################################
1691
1692 var clear_all = func {
1693
1694 # clear the clouds and models
1695
1696 var cloudNode = props.globals.getNode(lw~"clouds", 1);
1697 cloudNode.removeChildren("tile");
1698
1699 var modelNode = props.globals.getNode("models", 1).getChildren("model");
1700
1701 foreach (var m; modelNode)
1702 {
1703 var l = m.getNode("tile-index",1).getValue();
1704 if (l != nil)
1705 {
1706 m.remove();
1707 }
1708 }
1709
1710
1711 # remove the hard-coded clouds
1712
1713 foreach (c; weather_tile_management.cloudArray)
1714 {
1715 c.remove();
1716 }
1717 setsize(weather_tile_management.cloudArray,0);
1718
1719 # reset pressure continuity
1720
1721 weather_tiles.last_pressure = 0.0;
1722
1723 # stop all loops
1724
1725 setprop(lw~"effect-loop-flag",0);
1726 setprop(lw~"interpolation-loop-flag",0);
1727 setprop(lw~"tile-loop-flag",0);
1728 setprop(lw~"lift-loop-flag",0);
1729 setprop(lw~"wave-loop-flag",0);
1730 setprop(lw~"dynamics-loop-flag",0);
1731 setprop(lw~"timing-loop-flag",0);
1732 setprop(lw~"buffer-loop-flag",0);
1733 setprop(lw~"housekeeping-loop-flag",0);
1734 setprop(lw~"convective-loop-flag",0);
1735 setprop(lw~"shadow-loop-flag",0);
1736 setprop(lw~"thunderstorm-loop-flag",0);
1737
1738 weather_dynamics.convective_loop_kill_flag = 1; # long-running loop needs a different scheme to end
1739
1740 # also remove rain, snow, haze and light effects
1741
1742 compat_layer.setRain(0.0);
1743 compat_layer.setSnow(0.0);
1744 compat_layer.setLight(1.0);
1745
1746
1747 # set placement indices to zero
1748
1749 setprop(lw~"clouds/placement-index",0);
1750 setprop(lw~"clouds/model-placement-index",0);
1751 setprop(lw~"effect-volumes/effect-placement-index",0);
1752 setprop(lw~"effect-volumes/number",0);
1753 setprop(lw~"effect-volumes/number-active-rain",0);
1754 setprop(lw~"effect-volumes/number-active-snow",0);
1755 setprop(lw~"effect-volumes/number-active-vis",0);
1756 setprop(lw~"effect-volumes/number-active-turb",0);
1757 setprop(lw~"effect-volumes/number-active-lift",0);
1758 setprop(lw~"effect-volumes/number-active-sat",0);
1759 setprop(lw~"tiles/tile-counter",0);
1760
1761
1762 # remove any quadtrees and arrays
1763
1764 settimer ( func { setsize(weather_dynamics.cloudQuadtrees,0);},0.1); # to avoid error generation in this frame
1765 setsize(effectVolumeArray,0);
1766 n_effectVolumeArray = 0;
1767
1768 # remove any impostors
1769
1770 weather_tile_management.remove_impostors();
1771
1772 # clear out the visual shadows
1773
1774 for (var i = 0; i<cloudShadowArraySize; i=i+1)
1775 {
1776 setprop("/local-weather/cloud-shadows/cloudpos-x["~i~"]",0.0);
1777 setprop("/local-weather/cloud-shadows/cloudpos-y["~i~"]",0.0);
1778 }
1779
1780 # clear any wxradar echos
1781
1782 if (wxradar_support_flag ==1)
1783 {props.globals.getNode("/instrumentation/wxradar", 1).removeChildren("storm");}
1784
1785 # if we have used METAR, we may no longer want to do so
1786
1787 metar_flag = 0;
1788
1789
1790 settimer ( func {
1791 setsize(weather_tile_management.modelArrays,0);
1792 setsize(weather_dynamics.tile_wind_direction,0);
1793 setsize(weather_dynamics.tile_wind_speed,0);
1794 setsize(weather_tile_management.cloudBufferArray,0);
1795 setsize(weather_tile_management.cloudSceneryArray,0);
1796 setsize(alt_20_array,0);
1797 setsize(alt_50_array,0);
1798 setsize(alt_min_array,0);
1799 setsize(alt_mean_array,0);
1800 setsize(weather_dynamics.cloudShadowArray,0);
1801 setsize(local_weather.thunderstormArray,0);
1802 setsize(weather_dynamics.cloudShadowCandidateArray,0);
1803 setsize(weather_dynamics.tile_convective_altitude,0);
1804 setsize(weather_dynamics.tile_convective_strength,0);
1805 setsize(weatherStationArray,0);
1806 setsize(windIpointArray,0);
1807 setsize(atmosphereIpointArray,0);
1808 setprop(lw~"clouds/buffer-count",0);
1809 setprop(lw~"clouds/cloud-scenery-count",0);
1810 weather_tile_management.n_cloudSceneryArray = 0;
1811 compat_layer.setScattering(0.8);
1812 compat_layer.setOvercast(0.0);
1813 setprop(lwi~"ipoint-number",0);
1814 setprop(lwi~"atmosphere-ipoint-number", 0);
1815 },0);
1816
1817 setprop(lw~"tmp/presampling-status", "idle");
1818
1819 # reset the random store
1820
1821 weather_tiles.rnd_store = rand();
1822
1823 # default 3d clouds layer wrapping back on, just in case
1824
1825 setprop("/sim/rendering/clouds3d-wrap",1);
1826
1827 # hand precipitation control back to automatic
1828
1829 props.globals.getNode("/environment/precipitation-control/detailed-precipitation").setBoolValue("false");
1830
1831 # indicate that we are no longer running
1832
1833
1834 local_weather_running_flag = 0;
1835
1836 }
1837
1838
1839
1840 ###########################################################
1841 # detailed Cumulus clouds created from multiple cloudlets
1842 ###########################################################
1843
1844 var create_detailed_cumulus_cloud = func (lat, lon, alt, size) {
1845
1846
1847 # various distribution biases
1848
1849 var edge_bias = convective_texture_mix;
1850 size = size + convective_size_bias;
1851 height_bias = 1.0;
1852 if (edge_bias > 0.0) {height_bias = height_bias + 15.0 *edge_bias + 20.0 * rand() * edge_bias;}
1853
1854
1855 #height_bias = 6.0;
1856
1857
1858 if (size > 2.0)
1859 {
1860 if (rand() > (size - 2.0))
1861 {create_cumulonimbus_cloud(lat, lon, alt, size); }
1862 else
1863 {create_cumulonimbus_cloud_rain(lat, lon, alt, size, 0.1 + 0.2* rand());}
1864 return;
1865 }
1866
1867 else if (size>1.5)
1868 {
1869 var type = "Congestus";
1870
1871 var height = 400;
1872 var n = 3;
1873 var x = 700.0;
1874 var y = 200.0;
1875 var edge = 0.2;
1876
1877 var alpha = rand() * 180.0;
1878 edge = edge + edge_bias;
1879
1880 create_streak(type,lat,lon, alt+ 0.3* (height )-offset_map["Congestus"], height,n,0.0,edge,x,1,0.0,0.0,y,alpha,1.0);
1881
1882 var type = "Cu (volume)";
1883 var height = 400;
1884 var n = 10 + int(height_bias);
1885 var x = 1400.0;
1886 var y = 400.0;
1887 var edge = 0.2;
1888
1889 edge = edge + edge_bias;
1890
1891 create_streak(type,lat,lon, alt+ 0.5* (height * height_bias )-offset_map["Cumulus"], height * height_bias ,n,0.0,edge,x,1,0.0,0.0,y,alpha,1.0);
1892
1893 var btype = "Congestus bottom";
1894 var n_b = 6;
1895 height_bias = 1.0;
1896 var top_shade_store = local_weather.top_shade;
1897 if (top_shade_store > 0.6) {local_weather.top_shade = 0.6;}
1898 create_streak(btype,lat,lon, alt -offset_map["Congestus"] -900.0, 100.0,n_b,0.0,edge,0.3*x,1,0.0,0.0,0.3*y,alpha,1.0);
1899 local_weather.top_shade = top_shade_store;
1900
1901 if (local_weather.cloud_shadow_flag == 1)
1902 {
1903 var cs = local_weather.cloudShadow.new(lat, lon, 0.9 * (1.5 * x)/5000.0 , 0.9);
1904 cs.index = getprop(lw~"tiles/tile-counter");
1905 append(cloudShadowCandidateArray,cs);
1906 }
1907
1908
1909 }
1910 else if (size>1.1)
1911 {
1912 var type = "Cumulus (cloudlet)";
1913 var btype = "Cumulus bottom";
1914 var height = 200;
1915 var n = 6 + int(height_bias);
1916 var n_b = 2;
1917 var x = 900.0;
1918 var y = 200.0;
1919 var edge = 0.2;
1920
1921 var alpha = rand() * 180.0;
1922 edge = edge + edge_bias;
1923 create_streak(type,lat,lon, alt+ 0.5* (height* height_bias )-offset_map["Cumulus"], height * height_bias,n,0.0,edge,x,1,0.0,0.0,y,alpha,1.0);
1924
1925 height_bias = 1.0;
1926 var top_shade_store = local_weather.top_shade;
1927 if (top_shade_store > 0.6) {local_weather.top_shade = 0.6;}
1928 create_streak(btype,lat,lon, alt -offset_map["Cumulus"] - 200.0, 100.0,n_b,0.0,edge,0.3*x,1,0.0,0.0,0.3*y,alpha,1.0);
1929 local_weather.top_shade = top_shade_store;
1930
1931 if (local_weather.cloud_shadow_flag == 1)
1932 {
1933 var cs = local_weather.cloudShadow.new(lat, lon, 0.9 * (1.5 * x)/5000.0 , 0.8);
1934 cs.index = getprop(lw~"tiles/tile-counter");
1935 append(cloudShadowCandidateArray,cs);
1936 }
1937
1938 }
1939 else if (size>0.8)
1940 {
1941 var type = "Cumulus (cloudlet)";
1942 var height = 150;
1943 var n = 4 + int(height_bias);
1944 var x = 300.0;
1945 var y = 300.0;
1946 var edge = 0.3;
1947
1948 var alpha = rand() * 180.0;
1949 edge = edge + edge_bias;
1950 create_streak(type,lat,lon, alt+ 0.5* (height * height_bias )-offset_map["Cumulus"], height * height_bias,n,0.0,edge,x,1,0.0,0.0,y,alpha,1.0);
1951
1952 n = 2;
1953 x = 700.0;
1954 y = 200.0;
1955 edge = 1.0;
1956 create_streak(type,lat,lon, alt+ 0.5* (height*height_bias )-offset_map["Cumulus"], height * height_bias,n,0.0,edge,x,1,0.0,0.0,y,alpha,1.0);
1957
1958 if (local_weather.cloud_shadow_flag == 1)
1959 {
1960 var cs = local_weather.cloudShadow.new(lat, lon, 0.9 * (1.5 * x)/5000.0 , 0.7);
1961 cs.index = getprop(lw~"tiles/tile-counter");
1962 append(cloudShadowCandidateArray,cs);
1963 }
1964
1965
1966 }
1967
1968 else if (size>0.4)
1969 {
1970 var type = "Cumulus (cloudlet)";
1971 var height = 100;
1972 var n = 2 + int(height_bias * 0.5);
1973 var x = 600.0;
1974 var y = 100.0;
1975 var edge = 1.0;
1976
1977 var alpha = rand() * 180.0;
1978 edge = edge + edge_bias;
1979 create_streak(type,lat,lon, alt+ 0.5* (height * height_bias)-offset_map["Cumulus"], height * height_bias,n,0.0,edge,x,1,0.0,0.0,y,alpha,1.0);
1980
1981 if (local_weather.cloud_shadow_flag == 1)
1982 {
1983 var cs = local_weather.cloudShadow.new(lat, lon, 0.9 * (1.0 * x)/5000.0 , 0.6);
1984 cs.index = getprop(lw~"tiles/tile-counter");
1985 append(cloudShadowCandidateArray,cs);
1986 }
1987
1988
1989 }
1990 else
1991 {
1992 var type = "Cumulus (whisp)";
1993 var height = 100;
1994 var n = 1;
1995 var x = 100.0;
1996 var y = 100.0;
1997 var edge = 1.0;
1998
1999 var alpha = rand() * 180.0;
2000 edge = edge + edge_bias;
2001 create_streak(type,lat,lon, alt+ 0.3* (height )-offset_map["Cumulus"], height,n,0.0,edge,x,1,0.0,0.0,y,alpha,1.0);
2002 }
2003 }
2004
2005 ###########################################################
2006 # detailed small Cumulonimbus clouds created from multiple cloudlets
2007 ###########################################################
2008
2009 var create_cumulonimbus_cloud = func(lat, lon, alt, size) {
2010
2011
2012 create_cloudbox("Cb_box", lat, lon, alt, 2500.0,2000.0, 1000.0,10, 0.2, 0.1, 1.0, 1, 0.8, 0.1, 6);
2013
2014 #create_cloudbox = func (type, blat, blon, balt, dx,dy,dz,n, f_core, r_core, h_core, n_core, f_bottom, h_bottom, n_bottom)
2015 }
2016
2017 ###########################################################
2018 # detailed small Cumulonimbus and rain created from multiple cloudlets
2019 ###########################################################
2020
2021 var create_cumulonimbus_cloud_rain = func(lat, lon, alt, size, rain) {
2022
2023
2024 create_cloudbox("Cb_box", lat, lon, alt, 2500.0,2000.0, 1000.0,10, 0.2, 0.1, 1.0, 1, 0.8, 0.1, 6);
2025
2026
2027
2028 # place a rain texture
2029
2030 var path = "Models/Weather/rain2.xml";
2031 if (thread_flag == 1)
2032 {create_cloud_vec(path, lat, lon, alt, 0.0);}
2033 else
2034 {compat_layer.create_cloud(path, lat, lon, alt, 0.0);}
2035
2036
2037
2038 # and some rain underneath
2039
2040 create_effect_volume(1, lat, lon, 2000.0, 2000.0, 0.0, 0.0, alt+1000.0, 8000.0 + 8000.0 * rand(), rain, -1, -1, -1 ,1,-1 );
2041
2042
2043 }
2044
2045
2046 ###########################################################
2047 # wrappers for convective cloud system to distribute
2048 # call across several frames if needed
2049 ###########################################################
2050
2051 var create_cumosys = func (blat, blon, balt, nc, size) {
2052
2053
2054 # realistic Cumulus has somewhat larger models, so compensate to get the same coverage
2055 if (detailed_clouds_flag == 1)
2056 {nc = int(0.7 * nc);}
2057
2058 nc = int(nc / cumulus_efficiency_factor);
2059
2060 if (thread_flag == 1)
2061 {setprop(lw~"tmp/convective-status", "computing");
2062 cumulus_loop(blat, blon, balt, nc, size);}
2063
2064 else
2065 {create_cumulus(blat, blon, balt, nc, size);
2066 if (debug_output_flag == 1)
2067 {print("Convective system done!");}
2068 }
2069 }
2070
2071
2072
2073 var cumulus_loop = func (blat, blon, balt, nc, size) {
2074
2075 if (local_weather_running_flag == 0) {return;}
2076
2077 if (local_weather.features.fast_geodinfo == 0)
2078 {var n = int(25/cumulus_efficiency_factor);}
2079 else
2080 {var n = int(200/cumulus_efficiency_factor);}
2081
2082 if (nc < 0)
2083 {
2084 if (debug_output_flag == 1)
2085 {print("Convective system done!");}
2086 setprop(lw~"tmp/convective-status", "idle");
2087 assemble_effect_array();
2088 convective_size_bias = 0.0;
2089 height_bias = 1.0;
2090 return;
2091 }
2092
2093 create_cumulus(blat, blon, balt, n, size);
2094
2095 settimer( func {cumulus_loop(blat, blon, balt, nc-n, size) },0);
2096 }
2097
2098 ###########################################################
2099 # place a convective cloud system
2100 ###########################################################
2101
2102 var create_cumulus = func (blat, blon, balt, nc, size) {
2103
2104
2105
2106 var path = "Models/Weather/blank.ac";
2107 var i = 0;
2108 var p = 0.0;
2109 var rn = 0.0;
2110 var place_lift_flag = 0;
2111 var strength = 0.0;
2112 var detail_flag = detailed_clouds_flag;
2113
2114 var alpha = getprop(lw~"tmp/tile-orientation-deg") * math.pi/180.0; # the tile orientation
2115
2116 var tile_index = getprop(lw~"tiles/tile-counter");
2117 var alt_base = balt;
2118 if (presampling_flag==1) {alt_base = alt_20_array[tile_index -1];}
2119
2120
2121
2122
2123 #var sec_to_rad = 2.0 * math.pi/86400; # conversion factor for sinusoidal dependence on daytime
2124
2125 calc_geo(blat);
2126
2127 # get the local time of the day in seconds
2128
2129 var t = getprop("sim/time/utc/day-seconds");
2130 t = t + getprop("sim/time/local-offset");
2131
2132 # print("t is now:", t);
2133
2134 # and make a simple sinusoidal model of thermal strength
2135
2136 # daily variation in number of thermals, peaks at noon
2137 var t_factor1 = 0.5 * (1.0-math.cos((t * sec_to_rad)));
2138
2139 # daily variation in strength of thermals, peaks around 15:30
2140 var t_factor2 = 0.5 * (1.0-math.cos((t * sec_to_rad)-0.9));
2141
2142
2143 # number of possible thermals equals overall strength times daily variation times geographic variation
2144 # this is a proxy for solar thermal energy
2145
2146 nc = t_factor1 * nc * math.cos(blat/180.0*math.pi);
2147
2148 # var thermal_conditions = getprop(lw~"config/thermal-properties");
2149
2150
2151 while (i < nc) {
2152
2153 p = 0.0;
2154 place_lift_flag = 0;
2155 strength = 0.0;
2156
2157 # pick a trial position inside the tile and rotate by tile orientation angle
2158 var x = (2.0 * rand() - 1.0) * size;
2159 var y = (2.0 * rand() - 1.0) * size;
2160
2161 var lat = blat + (y * math.cos(alpha) - x * math.sin(alpha)) * m_to_lat;
2162 var lon = blon + (x * math.cos(alpha) + y * math.sin(alpha)) * m_to_lon;
2163
2164 # now check ground cover type on chosen spot
2165 var info = geodinfo(lat, lon);
2166
2167 if (info != nil) {
2168 var elevation = info[0] * m_to_ft;
2169 if (info[1] != nil){
2170 var landcover = info[1].names[0];
2171 if (contains(landcover_map,landcover)) {p = p + landcover_map[landcover];}
2172 else {logprint(LOG_INFO,"local_weather: Unknown landcover ",p, " ", info[1].names[0]);}
2173 }}
2174 else {
2175 # to avoid gaps, we create default clouds
2176
2177 p = p + 0.1;
2178 var elevation = alt_base;
2179 # continue;
2180 }
2181
2182
2183 # apply some optional corrections, biases clouds towards higher elevations
2184
2185 var terrain_altitude_factor = 1.0;
2186 var terrain_strength_factor = 1.0;
2187
2188 if (detailed_terrain_interaction_flag == 1)
2189 {
2190
2191 terrain_altitude_factor = get_terrain_altitude_factor(tile_index, balt, elevation);
2192 terrain_strength_factor = get_terrain_strength_factor(terrain_altitude_factor);
2193
2194 }
2195
2196
2197 # then decide if the thermal energy at the spot generates an updraft and a cloud
2198
2199 if (rand() < (p * cumulus_efficiency_factor * terrain_altitude_factor)) # we decide to place a cloud at this spot
2200 {
2201
2202
2203 # check if we have a terrain elevation analysis available and can use a
2204 # detailed placement altitude correction
2205
2206 if (presampling_flag == 1)
2207 {
2208
2209 if (detailed_terrain_interaction_flag == 1)
2210 {
2211 var grad = get_terrain_gradient(lat, lon, elevation, alpha, 1000.0);
2212 }
2213 else
2214 {var grad = 0.0;}
2215
2216
2217 var place_alt = get_convective_altitude(balt, elevation, getprop(lw~"tiles/tile-counter"), grad);
2218 }
2219 else {var place_alt = balt;}
2220
2221 # no cloud placement into the ground
2222 if (place_alt < elevation) {continue;}
2223
2224 # if we're in a lee, we may not want to place the cloud
2225
2226 if (detailed_terrain_interaction_flag == 1)
2227 {
2228 var p_lee_suppression = get_lee_bias(grad, tile_index);
2229 if (rand() > p_lee_suppression) {continue;}
2230 }
2231
2232
2233 # now decide on the strength of the thermal at the spot and on cloud size
2234
2235 var rn = rand();
2236 strength = (1.5 * rn + (2.0 * p * terrain_strength_factor)) * t_factor2;
2237
2238 # the terrain effect cannot create Cb development, so we have to curb
2239 # the strength if it would not have been Cb otherwise
2240
2241 if (strength > 2.0)
2242 {
2243 if (((1.5 * rn + (2.0 * p)) * t_factor2) < 2.0)
2244 {strength = 1.7 + rand() * 0.2;}
2245 }
2246
2247
2248 if (strength > 1.0) {place_lift_flag = 1;}
2249
2250 cloud_mean_altitude = place_alt;
2251 cloud_fractional_lifetime = rand();
2252 cloud_evolution_timestamp = weather_dynamics.time_lw;
2253
2254
2255
2256 if (generate_thermal_lift_flag != 3) # no clouds if we produce blue thermals
2257 {
2258 create_detailed_cumulus_cloud(lat, lon, place_alt, strength);
2259 }
2260
2261 # now see if we need to create a thermal - first check the flag
2262 if (generate_thermal_lift_flag == 1) # thermal by constant
2263 {
2264 # now check if convection is strong
2265 if (place_lift_flag == 1)
2266 {
2267 var lift = 3.0 + 10.0 * (strength -1.0);
2268 var radius = 500 + 500 * rand();
2269 #print("Lift: ", lift * ft_to_m - 1.0);
2270 create_effect_volume(1, lat, lon, radius, radius, 0.0, 0.0, place_alt+500.0, -1, -1, -1, -1, lift, 1,-1);
2271 } # end if place_lift_flag
2272 } # end if generate-thermal-lift-flag
2273 else if ((generate_thermal_lift_flag == 2) or (generate_thermal_lift_flag == 3)) # thermal by function
2274 {
2275
2276 if (place_lift_flag == 1)
2277 {
2278 var lift = (3.0 + 10.0 * (strength -1.0))/thermal_conditions;
2279 var radius = (500 + 500 * rand())*thermal_conditions;
2280
2281 create_effect_volume(1, lat, lon, 1.1*radius, 1.1*radius, 0.0, 0.0, place_alt*1.15, -1, -1, -1, lift*0.03, lift, -2,-1);
2282 } # end if place_lift_flag
2283
2284 } # end if generate-thermal-lift-flag
2285
2286
2287 } # end if rand < p
2288 i = i + 1;
2289 } # end while
2290
2291 }
2292
2293
2294
2295
2296
2297 #################################################################
2298 # respawn convective clouds to compensate for decay
2299 # the difference being that new clouds get zero fractional
2300 # lifetime and are placed based on terrain with a different weight
2301 ##################################################################
2302
2303 var recreate_cumulus = func (blat, blon, balt, alpha, nc, size, tile_index) {
2304
2305 var path = "Models/Weather/blank.ac";
2306 var i = 0;
2307 var p = 0.0;
2308 var rn = 0.0;
2309 var place_lift_flag = 0;
2310 var strength = 0.0;
2311 var detail_flag = detailed_clouds_flag;
2312
2313 alpha = alpha * math.pi/180.0; # the tile orientation
2314
2315
2316 # current aircraft position
2317
2318 var alat = getprop("position/latitude-deg");
2319 var alon = getprop("position/longitude-deg");
2320
2321 # get the local time of the day in seconds
2322
2323 var t = getprop("sim/time/utc/day-seconds");
2324 t = t + getprop("sim/time/local-offset");
2325
2326
2327 # and make a simple sinusoidal model of thermal strength
2328
2329 # daily variation in number of thermals, peaks at noon
2330 var t_factor1 = 0.5 * (1.0-math.cos((t * sec_to_rad)));
2331
2332 # daily variation in strength of thermals, peaks around 15:30
2333 var t_factor2 = 0.5 * (1.0-math.cos((t * sec_to_rad)-0.9));
2334
2335
2336 # number of possible thermals equals overall strength times daily variation times geographic variation
2337 # this is a proxy for solar thermal energy
2338
2339 nc = t_factor1 * nc * math.cos(blat/180.0*math.pi);
2340
2341 # var thermal_conditions = getprop(lw~"config/thermal-properties");
2342
2343 var alt_base = alt_20_array[tile_index -1];
2344
2345 while (i < nc) {
2346
2347 p = 0.0;
2348 place_lift_flag = 0;
2349 strength = 0.0;
2350
2351 # pick a trial position inside the tile and rotate by tile orientation angle
2352 var x = (2.0 * rand() - 1.0) * size;
2353 var y = (2.0 * rand() - 1.0) * size;
2354
2355 var lat = blat + (y * math.cos(alpha) - x * math.sin(alpha)) * m_to_lat;
2356 var lon = blon + (x * math.cos(alpha) + y * math.sin(alpha)) * m_to_lon;
2357
2358 # check if the cloud would be spawned in visual range, if not don't bother
2359 var d_sq = calc_d_sq(alat, alon, lat, lon);
2360
2361 if (math.sqrt(d_sq) > weather_tile_management.cloud_view_distance)
2362 {i = i+1; continue;}
2363
2364 # now check ground cover type on chosen spot
2365 var info = geodinfo(lat, lon);
2366
2367 if (info != nil) {
2368 var elevation = info[0] * m_to_ft;
2369 if (info[1] != nil){
2370 var landcover = info[1].names[0];
2371 if (contains(landcover_map,landcover)) {p = p + landcover_map[landcover];}
2372 else {print(p, " ", info[1].names[0]);}
2373 }}
2374 else {
2375 # to avoid gaps, we create default clouds
2376
2377 p = p + 0.1;
2378 var elevation = alt_base;
2379 # continue;
2380 }
2381
2382
2383 # apply some optional corrections, biases clouds towards higher elevations
2384
2385 var terrain_altitude_factor = 1.0;
2386 var terrain_strength_factor = 1.0;
2387
2388 if (detailed_terrain_interaction_flag == 1)
2389 {
2390 terrain_altitude_factor = get_terrain_altitude_factor(tile_index, balt, elevation);
2391 terrain_strength_factor = get_terrain_strength_factor(terrain_altitude_factor);
2392 }
2393
2394
2395
2396
2397 # check if to place a cloud with weight sqrt(p), the lifetime gets another sqrt(p) factor
2398
2399 if (rand() > math.sqrt(p * cumulus_efficiency_factor * terrain_altitude_factor))
2400 {i=i+1; continue;}
2401
2402
2403 # then calculate the strength of the updraft
2404
2405 strength = (1.5 * rand() + (2.0 * p * terrain_strength_factor)) * t_factor2; # the strength of thermal activity at the spot
2406 if (strength > 1.0)
2407 {
2408 path = select_cloud_model("Cumulus","large"); place_lift_flag = 1;
2409 }
2410 else {path = select_cloud_model("Cumulus","small");}
2411
2412 if (presampling_flag == 1)
2413 {
2414 var place_alt = get_convective_altitude(balt, elevation, tile_index,0.0);
2415 }
2416 else {var place_alt = balt;}
2417
2418
2419 # no cloud placement into the ground
2420 if (place_alt < elevation) {continue;}
2421
2422 # if we're in a lee, we may not want to place the cloud
2423
2424 if (detailed_terrain_interaction_flag == 1)
2425 {
2426 var p_lee_suppression = get_lee_bias(grad, tile_index);
2427 if (rand() > math.sqrt(p_lee_suppression)) {continue;}
2428 }
2429
2430 cloud_mean_altitude = place_alt;
2431 cloud_fractional_lifetime = 0.0;
2432 cloud_evolution_timestamp = weather_dynamics.time_lw;
2433
2434 compat_layer.cloud_mean_altitude = place_alt;
2435 compat_layer.cloud_flt = cloud_fractional_lifetime;
2436 compat_layer.cloud_evolution_timestamp = cloud_evolution_timestamp;
2437
2438 if (generate_thermal_lift_flag != 3) # no clouds if we produce blue thermals
2439 {
2440 if (thread_flag == 1)
2441 {
2442 thread_flag = 0; # create clouds immediately
2443 if (detail_flag == 0){compat_layer.create_cloud(path,lat,lon, place_alt, 0.0);}
2444 else {create_detailed_cumulus_cloud(lat, lon, place_alt, strength);}
2445 thread_flag = 1; # and restore threading
2446 }
2447 else
2448 {
2449 if (detail_flag == 0){compat_layer.create_cloud(path, lat, lon, place_alt, 0.0);}
2450 else {create_detailed_cumulus_cloud(lat, lon, place_alt, strength);}
2451 }
2452 }
2453
2454 if (generate_thermal_lift_flag == 1) # thermal by constant
2455 {
2456 if (place_lift_flag == 1)
2457 {
2458 var lift = 3.0 + 10.0 * (strength -1.0);
2459 var radius = 500 + 500 * rand();
2460 create_effect_volume(1, lat, lon, radius, radius, 0.0, 0.0, place_alt+500.0, -1, -1, -1, -1, lift, 1,-1);
2461 } # end if place_lift_flag
2462 } # end if generate-thermal-lift-flag
2463 else if ((generate_thermal_lift_flag == 2) or (generate_thermal_lift_flag == 3)) # thermal by function
2464 {
2465 if (place_lift_flag == 1)
2466 {
2467 var lift = (3.0 + 10.0 * (strength -1.0))/thermal_conditions;
2468 var radius = (500 + 500 * rand())*thermal_conditions;
2469
2470 create_effect_volume(1, lat, lon, 1.1*radius, 1.1*radius, 0.0, 0.0, place_alt*1.15, -1, -1, -1, lift*0.03, lift, -2,-1);
2471 } # end if place_lift_flag
2472
2473 } # end if generate-thermal-lift-flag
2474
2475
2476 i = i + 1;
2477 } # end while
2478
2479 }
2480
2481
2482
2483
2484
2485
2486
2487 ###########################################################
2488 # place a barrier cloud system
2489 ###########################################################
2490
2491 var create_rise_clouds = func (blat, blon, balt, nc, size, winddir, dist) {
2492
2493 var path = "Models/Weather/blank.ac";
2494 var i = 0;
2495 var p = 0.0;
2496 var rn = 0.0;
2497 var nsample = 10;
2498 var counter = 0;
2499 var dir = (winddir + 180.0) * math.pi/180.0;
2500 var step = dist/nsample;
2501
2502 calc_geo(blat);
2503
2504 while (i < nc) {
2505
2506 counter = counter + 1;
2507 p = 0.0;
2508
2509 var x = (2.0 * rand() - 1.0) * size;
2510 var y = (2.0 * rand() - 1.0) * size;
2511
2512 var lat = blat + y * m_to_lat;
2513 var lon = blon + x * m_to_lon;
2514
2515 var elevation = compat_layer.get_elevation(lat, lon);
2516
2517 #print("elevation: ", elevation, "balt: ", balt);
2518
2519 if ((elevation < balt) and (elevation != -1.0))
2520 {
2521 for (var j = 0; j<nsample; j=j+1)
2522 {
2523 d = j * step;
2524 x = d * math.sin(dir);
2525 y = d * math.cos(dir);
2526 var tlat = lat + y * m_to_lat;
2527 var tlon = lon + x * m_to_lon;
2528
2529 #print("x: ", x, "y: ", y);
2530
2531 var elevation1 = compat_layer.get_elevation(tlat,tlon);
2532 #print("elevation1: ", elevation1, "balt: ", balt);
2533
2534 if (elevation1 > balt)
2535 {
2536 p = 1.0 - j * (1.0/nsample);
2537 #p = 1.0;
2538 break;
2539 }
2540
2541 }
2542 }
2543 if (counter > 500) {print("Cannot place clouds - exiting..."); i = nc;}
2544 if (rand() < p)
2545 {
2546 path = select_cloud_model("Stratus (structured)","large");
2547 compat_layer.create_cloud(path, lat, lon, balt, 0.0);
2548 counter = 0;
2549 i = i+1;
2550 }
2551
2552 } # end while
2553
2554 }
2555
2556
2557 ###########################################################
2558 # place a cloud streak
2559 ###########################################################
2560
2561 var create_streak = func (type, blat, blong, balt, alt_var, nx, xoffset, edgex, x_var, ny, yoffset, edgey, y_var, direction, tri) {
2562
2563 var flag = 0;
2564 var path = "Models/Weather/blank.ac";
2565 calc_geo(blat);
2566 var dir = direction * math.pi/180.0;
2567
2568 var ymin = -0.5 * ny * yoffset;
2569 var xmin = -0.5 * nx * xoffset;
2570 var xinc = xoffset * (tri-1.0) /ny;
2571
2572 var jlow = int(nx*edgex);
2573 var ilow = int(ny*edgey);
2574
2575
2576 for (var i=0; i<ny; i=i+1)
2577 {
2578 var y = ymin + i * yoffset;
2579
2580 for (var j=0; j<nx; j=j+1)
2581 {
2582 var y0 = y + y_var * 2.0 * (rand() -0.5);
2583 var x = xmin + j * (xoffset + i * xinc) + x_var * 2.0 * (rand() -0.5);
2584 var lat = blat + m_to_lat * (y0 * math.cos(dir) - x * math.sin(dir));
2585 var long = blong + m_to_lon * (x * math.cos(dir) + y0 * math.sin(dir));
2586
2587 var alt = balt + alt_var * 2 * (rand() - 0.5);
2588
2589 flag = 0;
2590 var rn = 6.0 * rand();
2591
2592 if (((j<jlow) or (j>(nx-jlow-1))) and ((i<ilow) or (i>(ny-ilow-1)))) # select a small or no cloud
2593 {
2594 if (rn > 2.0) {flag = 1;} else {path = select_cloud_model(type,"small");}
2595 }
2596 if ((j<jlow) or (j>(nx-jlow-1)) or (i<ilow) or (i>(ny-ilow-1)))
2597 {
2598 if (rn > 5.0) {flag = 1;} else {path = select_cloud_model(type,"small");}
2599 }
2600 else { # select a large cloud
2601 if (rn > 5.0) {flag = 1;} else {path = select_cloud_model(type,"large");}
2602 }
2603
2604
2605 if (flag==0){
2606 if (thread_flag == 1)
2607 {create_cloud_vec(path, lat, long, alt, 0.0);}
2608 else
2609 {compat_layer.create_cloud(path, lat, long, alt, 0.0);}
2610
2611
2612 }
2613 }
2614
2615 }
2616
2617 }
2618
2619
2620
2621
2622
2623
2624
2625 ###########################################################
2626 # place a cloud layer with a gap in the middle
2627 # (useful to reduce cloud count in large thunderstorms)
2628 ###########################################################
2629
2630 var create_hollow_layer = func (type, blat, blon, balt, bthick, rx, ry, phi, density, edge, gap_fraction) {
2631
2632
2633 var i = 0;
2634 var area = math.pi * rx * ry;
2635 var n = int(area/80000000.0 * 100 * density);
2636 var path = "Models/Weather/blank.ac";
2637
2638 phi = phi * math.pi/180.0;
2639
2640 if (contains(cloud_vertical_size_map, type))
2641 {var alt_offset = cloud_vertical_size_map[type]/2.0 * m_to_ft;}
2642 else {var alt_offset = 0.0;}
2643
2644 while(i<n)
2645 {
2646 var x = rx * (2.0 * rand() - 1.0);
2647 var y = ry * (2.0 * rand() - 1.0);
2648 var alt = balt + bthick * rand() + 0.8 * alt_offset;
2649 var res = (x*x)/(rx*rx) + (y*y)/(ry*ry);
2650
2651
2652 if ((res < 1.0) and (res > (gap_fraction * gap_fraction)))
2653 {
2654 var lat = blat + m_to_lat * (y * math.cos(phi) - x * math.sin(phi));
2655 var lon = blon + m_to_lon * (x * math.cos(phi) + y * math.sin(phi));
2656 if (res > ((1.0 - edge) * (1.0- edge)))
2657 {
2658 if (rand() > 0.4) {
2659 path = select_cloud_model(type,"small");
2660 compat_layer.create_cloud(path, lat, lon, alt, 0.0);
2661 }
2662 }
2663 else {
2664 path = select_cloud_model(type,"large");
2665 if (thread_flag == 1)
2666 {create_cloud_vec(path, lat, lon, alt, 0.0);}
2667 else
2668 {compat_layer.create_cloud(path, lat, lon, alt, 0.0);}
2669 }
2670 i = i + 1;
2671 }
2672 else # we are in the central gap region
2673 {
2674 i = i + 1;
2675 }
2676 }
2677
2678 i = 0;
2679
2680
2681 }
2682
2683
2684
2685
2686 ###########################################################
2687 # place a cloud box
2688 ###########################################################
2689
2690
2691 var create_cloudbox = func (type, blat, blon, balt, dx,dy,dz,n, f_core, r_core, h_core, n_core, f_bottom, h_bottom, n_bottom) {
2692
2693 var phi = 0;
2694
2695 # first get core coordinates
2696
2697 var core_dx = dx * f_core;
2698 var core_dy = dy * f_core;
2699 var core_dz = dz * h_core;
2700
2701 var core_x_offset = (1.0 * rand() - 0.5) * ((dx - core_dx) * r_core);
2702 var core_y_offset = (1.0 * rand() - 0.5) * ((dy - core_dy) * r_core);
2703
2704 # get the bottom geometry
2705
2706 var bottom_dx = dx * f_bottom;
2707 var bottom_dy = dy * f_bottom;
2708 var bottom_dz = dz * h_bottom;
2709
2710 var bottom_offset = 400.0; # in practice, need a small shift
2711
2712 # fill the main body of the box
2713
2714 for (var i=0; i<n; i=i+1)
2715 {
2716
2717 var x = 0.5 * dx * (2.0 * rand() - 1.0);
2718 var y = 0.5 * dy * (2.0 * rand() - 1.0);
2719
2720 # veto in core region
2721 if ((x > core_x_offset - 0.5 * core_dx) and (x < core_x_offset + 0.5 * core_dx))
2722 {
2723 if ((y > core_y_offset - 0.5 * core_dy) and (y < core_y_offset + 0.5 * core_dy))
2724 {
2725 i = i -1;
2726 continue;
2727 }
2728 }
2729
2730 var alt = balt + bottom_dz + bottom_offset + dz * rand();
2731
2732 var lat = blat + m_to_lat * (y * math.cos(phi) - x * math.sin(phi));
2733 var lon = blon + m_to_lon * (x * math.cos(phi) + y * math.sin(phi));
2734
2735 var path = select_cloud_model(type,"standard");
2736
2737
2738 create_cloud_vec(path, lat, lon, alt, 0.0);
2739
2740 }
2741
2742 # fill the core region
2743
2744 for (var i=0; i<n_core; i=i+1)
2745 {
2746 var x = 0.5 * core_dx * (2.0 * rand() - 1.0);
2747 var y = 0.5 * core_dy * (2.0 * rand() - 1.0);
2748 var alt = balt + bottom_dz + bottom_offset + core_dz * rand();
2749
2750
2751 var lat = blat + m_to_lat * (y * math.cos(phi) - x * math.sin(phi));
2752 var lon = blon + m_to_lon * (x * math.cos(phi) + y * math.sin(phi));
2753
2754 var path = select_cloud_model(type,"core");
2755
2756 if (thread_flag == 1)
2757 {create_cloud_vec(path, lat, lon, alt, 0.0);}
2758 else
2759 {compat_layer.create_cloud(path, lat, lon, alt, 0.0);}
2760
2761 }
2762
2763 # fill the bottom region
2764
2765
2766 for (var i=0; i<n_bottom; i=i+1)
2767 {
2768 var x = 0.5 * bottom_dx * (2.0 * rand() - 1.0);
2769 var y = 0.5 * bottom_dy * (2.0 * rand() - 1.0);
2770 var alt = balt + bottom_dz * rand();
2771
2772
2773 var lat = blat + m_to_lat * (y * math.cos(phi) - x * math.sin(phi));
2774 var lon = blon + m_to_lon * (x * math.cos(phi) + y * math.sin(phi));
2775
2776 var path = select_cloud_model(type,"bottom");
2777
2778 var top_shade_store = local_weather.top_shade;
2779 if (top_shade_store > 0.6) {local_weather.top_shade = 0.6;}
2780 if (thread_flag == 1)
2781 {create_cloud_vec(path, lat, lon, alt, 0.0);}
2782 else
2783 {compat_layer.create_cloud(path, lat, lon, alt, 0.0);}
2784 local_weather.top_shade = top_shade_store;
2785
2786 }
2787
2788
2789 }
2790
2791
2792
2793 ###########################################################
2794 # terrain presampling initialization
2795 ###########################################################
2796
2797 var terrain_presampling_start = func (blat, blon, nc, size, alpha) {
2798
2799 # terrain presampling start is always used the first time, and initializes
2800 # the hard-coded routine if that is available since the hard-coded routine cannot
2801 # be yet read out on startup
2802
2803 # initialize the result vector
2804
2805 setsize(terrain_n,40);
2806 for(var j=0;j<40;j=j+1){terrain_n[j]=0;}
2807
2808 if (thread_flag == 1)
2809 {
2810 var status = getprop(lw~"tmp/presampling-status");
2811 if (status != "idle") # we try a second later
2812 {
2813 settimer( func {terrain_presampling_start(blat, blon, nc, size, alpha);},1.00);
2814 return;
2815 }
2816 else
2817 {
2818 setprop(lw~"tmp/presampling-status", "sampling");
2819 terrain_presampling_loop (blat, blon, nc, size, alpha);
2820 }
2821 }
2822 else
2823 {
2824 terrain_presampling(blat, blon, nc, size, alpha);
2825 terrain_presampling_analysis();
2826 setprop(lw~"tmp/presampling-status", "finished");
2827 }
2828
2829 if (compat_layer.features.terrain_presampling == 1)
2830 {
2831 print("Starting hard-coded terrain presampling");
2832 setprop("/environment/terrain/area[0]/enabled",1);
2833 setprop(lw~"tmp/presampling-status", "sampling");
2834 setprop("/environment/terrain/area[0]/enabled", 1 );
2835 setprop("/environment/terrain/area[0]/input/latitude-deg", blat );
2836 setprop("/environment/terrain/area[0]/input/longitude-deg", blon );
2837 setprop("/environment/terrain/area[0]/input/use-aircraft-position",1);
2838 setprop("/environment/terrain/area[0]/input/radius-m",45000.0);
2839
2840 setprop("/environment/terrain/area[0]/output/valid", 0 );
2841
2842 }
2843 }
2844
2845 ###########################################################
2846 # terrain presampling loop
2847 ###########################################################
2848
2849 var terrain_presampling_loop = func (blat, blon, nc, size, alpha) {
2850
2851 if ((local_weather_running_flag == 0) and (local_weather_startup_flag == 0)) {return;}
2852
2853
2854 var n = 25;
2855 var n_out = 25;
2856 if (local_weather.features.fast_geodinfo == 0)
2857 {
2858 # dynamically drop accuracy if framerate is low
2859
2860 var dt = getprop("/sim/time/delta-sec");
2861
2862 if (dt > 0.2) # we have below 20 fps
2863 {n = 5;}
2864 else if (dt > 0.1) # we have below 10 fps
2865 {n = 10;}
2866 else if (dt > 0.05) # we have below 5 fps
2867 {n = 15;}
2868 }
2869 else
2870 {
2871 n = 250; n_out = 250;
2872 }
2873
2874 if (nc <= 0) # we're done and may analyze the result
2875 {
2876 terrain_presampling_analysis();
2877 if (debug_output_flag == 1)
2878 {print("Presampling done!");}
2879 setprop(lw~"tmp/presampling-status", "finished");
2880 return;
2881 }
2882
2883 terrain_presampling(blat, blon, n, size, alpha);
2884
2885 settimer( func {terrain_presampling_loop(blat, blon, nc-n_out, size, alpha) },0);
2886 }
2887
2888
2889 ###########################################################
2890 # terrain presampling routine
2891 ###########################################################
2892
2893 var terrain_presampling = func (blat, blon, ntries, size, alpha) {
2894
2895 var phi = alpha * math.pi/180.0;
2896 var elevation = 0.0;
2897
2898 var lat_vec = [];
2899 var lon_vec = [];
2900 var lat_lon_vec = [];
2901
2902
2903 for (var i=0; i<ntries; i=i+1)
2904 {
2905 var x = (2.0 * rand() - 1.0) * size;
2906 var y = (2.0 * rand() - 1.0) * size;
2907
2908 append(lat_vec, blat + (y * math.cos(phi) - x * math.sin(phi)) * m_to_lat);
2909 append(lon_vec, blon + (x * math.cos(phi) + y * math.sin(phi)) * m_to_lon);
2910 }
2911
2912
2913 var elevation_vec = compat_layer.get_elevation_array(lat_vec, lon_vec);
2914
2915
2916 for (var i=0; i<ntries;i=i+1)
2917 {
2918 for(var j=0;j<30;j=j+1)
2919 {
2920 if ((elevation_vec[i] != -1.0) and (elevation_vec[i] < 500.0 * (j+1)))
2921 {terrain_n[j] = terrain_n[j]+1; break;}
2922 }
2923
2924 }
2925
2926
2927
2928 }
2929
2930 ###########################################################
2931 # terrain presampling analysis
2932 ###########################################################
2933
2934 var terrain_presampling_analysis = func {
2935
2936 if ((compat_layer.features.terrain_presampling_active == 0) or (getprop(lw~"tiles/tile-counter") == 0))
2937 {
2938 var sum = 0;
2939 var alt_mean = 0;
2940 var alt_med = 0;
2941 var alt_20 = 0;
2942 var alt_min = 0;
2943 var alt_low_min = 0;
2944
2945
2946 for (var i=0; i<40;i=i+1)
2947 {sum = sum + terrain_n[i];}
2948
2949 var n_tot = sum;
2950
2951 sum = 0;
2952 for (var i=0; i<40;i=i+1)
2953 {
2954 sum = sum + terrain_n[i];
2955 if (sum > int(0.5 *n_tot)) {alt_med = i * 500.0; break;}
2956 }
2957
2958 sum = 0;
2959 for (var i=0; i<40;i=i+1)
2960 {
2961 sum = sum + terrain_n[i];
2962 if (sum > int(0.3 *n_tot)) {alt_20 = i * 500.0; break;}
2963 }
2964
2965
2966 for (var i=0; i<40;i=i+1) {alt_mean = alt_mean + terrain_n[i] * i * 500.0;}
2967 alt_mean = alt_mean/n_tot;
2968
2969 for (var i=0; i<40;i=i+1) {if (terrain_n[i] > 0) {alt_min = i * 500.0; break;}}
2970
2971 var n_max = 0;
2972 sum = 0;
2973
2974 for (var i=0; i<39;i=i+1)
2975 {
2976 sum = sum + terrain_n[i];
2977 if (terrain_n[i] > n_max) {n_max = terrain_n[i];}
2978 if ((n_max > terrain_n[i+1]) and (sum > int(0.3*n_tot)))
2979 {alt_low_min = i * 500; break;}
2980 }
2981 }
2982 else
2983 {
2984 # print("Hard-coded sampling...");
2985 var n_tot = getprop("/environment/terrain/area[0]/input/max-samples");
2986 var alt_mean = getprop("/environment/terrain/area[0]/output/alt-mean-ft");
2987 var alt_med = getprop("/environment/terrain/area[0]/output/alt-median-ft");
2988 var alt_min = getprop("/environment/terrain/area[0]/output/alt-min-ft");
2989 var alt_20 = getprop("/environment/terrain/area[0]/output/alt-offset-ft");
2990 }
2991
2992 if (debug_output_flag == 1)
2993 {print("Terrain presampling analysis results:");
2994 print("total: ",n_tot," mean: ",alt_mean," median: ",alt_med," min: ",alt_min, " alt_20: ", alt_20);}
2995
2996
2997
2998 setprop(lw~"tmp/tile-alt-offset-ft",alt_20);
2999 setprop(lw~"tmp/tile-alt-median-ft",alt_med);
3000 setprop(lw~"tmp/tile-alt-min-ft",alt_min);
3001 setprop(lw~"tmp/tile-alt-mean-ft",alt_mean);
3002 setprop(lw~"tmp/tile-alt-layered-ft",0.5 * (alt_min + alt_20));
3003
3004 append(alt_50_array, alt_med);
3005 append(alt_20_array, alt_20);
3006 append(alt_min_array, alt_min);
3007 append(alt_mean_array, alt_mean);
3008
3009
3010 current_mean_alt = 0.5 * (current_mean_alt + alt_20);
3011
3012
3013 }
3014
3015
3016
3017 ###########################################################
3018 # wave conditions search
3019 ###########################################################
3020
3021 var wave_detection_loop = func (blat, blon, nx, alpha) {
3022
3023 if (local_weather_running_flag == 0) {return;}
3024
3025 var phi = alpha * math.pi/180.0;
3026 var elevation = 0.0;
3027 var ny = 20;
3028
3029
3030 for (var i=0; i<ny; i=i+1)
3031 {
3032 var x = 5000.0;
3033 var y = -20000.0 + i * 2000.0;
3034
3035 var lat = blat + (y * math.cos(phi) - x * math.sin(phi)) * m_to_lat;
3036 var lon = blon + (x * math.cos(phi) + y * math.sin(phi)) * m_to_lon;
3037
3038 elevation = compat_layer.get_elevation(lat, lon);
3039
3040 print(elevation);
3041
3042 }
3043
3044
3045 }
3046
3047 ###########################################################
3048 # detailed altitude determination for convective calls
3049 # clouds follow the terrain to some degree, but not excessively so
3050 ###########################################################
3051
3052 var get_convective_altitude = func (balt, elevation, tile_index, grad) {
3053
3054
3055 var alt_offset = alt_20_array[tile_index - 1];
3056 var alt_median = alt_50_array[tile_index - 1];
3057
3058 # get the maximal shift
3059 var alt_variation = alt_median - alt_offset;
3060
3061 # always get some amount of leeway
3062 if (alt_variation < 500.0) {alt_variation = 500.0;}
3063
3064 # get the correction to the maximal shift by detailed terrain
3065
3066 if (detailed_terrain_interaction_flag == 1)
3067 {
3068 var gradfact = get_gradient_factor(grad);
3069
3070 if ((local_weather.wind_model_flag == 1) or (local_weather.wind_model_flag == 3))
3071 {
3072 var windspeed = tile_wind_speed[0];
3073 }
3074 else if ((local_weather.wind_model_flag ==2) or (local_weather.wind_model_flag == 4) or (local_weather.wind_model_flag == 5))
3075 {
3076 var windspeed = tile_wind_speed[tile_index-1];
3077 }
3078
3079 var gradfact = ((gradfact - 1.0) * windspeed) + 1.0;
3080 #print("gradfact: ", gradfact);
3081 }
3082 else
3083 {
3084 var gradfact = 1.0;
3085 }
3086
3087 var alt_variation = alt_variation * gradfact;
3088
3089 # get the difference between offset and foot point
3090 var alt_diff = elevation - alt_offset;
3091
3092 # now get the elevation-induced shift
3093
3094 var fraction = alt_diff / alt_variation;
3095
3096 if (fraction > 1.0) {fraction = 1.0;} # no placement above maximum shift
3097 if (fraction < 0.0) {fraction = 0.0;} # no downward shift
3098
3099 # get the cloud base
3100
3101 var cloudbase = balt - alt_offset;
3102
3103 var alt_above_terrain = balt - elevation;
3104
3105 # the shift strength is weakened if the layer is high above base elevation
3106 # the reference altitude is 1000 ft, anything higher has less sensitivity to terrain
3107
3108 var shift_strength = 1000.0/alt_above_terrain;
3109
3110 if (shift_strength > 1.0) {shift_strength = 1.0;} # no enhancement for very low layers
3111 if (shift_strength < 0.0) {shift_strength = 1.0;} # this shouldn't happen, but just in case...
3112
3113 if (alt_diff > alt_variation) {alt_diff = alt_variation;} # maximal shift is given by alt_variation
3114
3115 # print("balt: ", balt, " new alt: ", balt + shift_strength * alt_diff * fraction);
3116
3117 return balt + shift_strength * alt_diff * fraction;
3118
3119 }
3120
3121
3122 ###########################################################
3123 # detailed terrain gradient determination in wind direction
3124 ###########################################################
3125
3126
3127 var get_terrain_gradient = func (lat, lon, elevation1, phi, dist) {
3128
3129
3130 # get the first elevation
3131 # var elevation1 = compat_layer.get_elevation(lat,lon);
3132
3133 # look <dist> upwind to learn about the history of the cloud
3134 var elevation2 = compat_layer.get_elevation(lat+weather_tiles.get_lat(0.0,dist,phi), lon+weather_tiles.get_lon(0.0,dist,phi));
3135
3136 return (elevation2 - elevation1)/(dist * m_to_ft);
3137 }
3138
3139 ###########################################################
3140 # enhancement of the placement altitude due to terrain
3141 ###########################################################
3142
3143 var get_gradient_factor = func (grad) {
3144
3145 if (grad > 0.0)
3146 {return 1.0;}
3147 else
3148 {
3149 return 1.0 -2.0 * grad;
3150 }
3151 }
3152
3153
3154 ###########################################################
3155 # suppression of placement in lee terrain
3156 ###########################################################
3157
3158 var get_lee_bias = func (grad, tile_index) {
3159
3160
3161 if ((local_weather.wind_model_flag == 1) or (local_weather.wind_model_flag == 3))
3162 {
3163 var windspeed = tile_wind_speed[0];
3164 }
3165 else if ((local_weather.wind_model_flag ==2) or (local_weather.wind_model_flag == 4) or (local_weather.wind_model_flag == 5))
3166 {
3167 var windspeed = tile_wind_speed[tile_index-1];
3168 }
3169
3170
3171 if (grad < 0.0)
3172 {return 1.0;}
3173 else
3174 {
3175 var lee_bias = 1.0 - (grad * 0.2 * windspeed);
3176 }
3177 if (lee_bias < 0.2) {lee_bias = 0.2;}
3178
3179 return lee_bias;
3180 }
3181
3182 ###########################################################
3183 # enhancement of Cumulus in above average altitude
3184 ###########################################################
3185
3186
3187 var get_terrain_altitude_factor = func (tile_index, balt, elevation) {
3188
3189
3190 var alt_mean = alt_mean_array[tile_index -1];
3191 var alt_base = alt_20_array[tile_index -1];
3192
3193 var alt_layer = balt - alt_base;
3194 var alt_above_terrain = balt - elevation;
3195 var alt_above_mean = balt - alt_mean;
3196
3197 # the cloud may still be above terrain even if the layer altitude is negative, but we want to avoid neg. factors here
3198
3199 if (alt_above_terrain < 0.0) {alt_above_terrain = 0.0;}
3200
3201 var norm_alt_diff = (alt_above_mean - alt_above_terrain)/alt_layer;
3202
3203 if (norm_alt_diff > 0.0)
3204 {
3205 var terrain_altitude_factor = 1.0 + 2.0 * norm_alt_diff;
3206 }
3207 else
3208 {
3209 var terrain_altitude_factor = 1.0/(1.0 - 5.0 * norm_alt_diff);
3210 }
3211
3212 if (terrain_altitude_factor > 3.0) {terrain_altitude_factor = 3.0;}
3213 if (terrain_altitude_factor < 0.1) {terrain_altitude_factor = 0.1;}
3214
3215 return terrain_altitude_factor;
3216 }
3217
3218
3219 var get_terrain_strength_factor = func (terrain_altitude_factor) {
3220
3221 return 1.0+ (0.5 * (terrain_altitude_factor-1.0));
3222
3223 }
3224
3225
3226 ###########################################################
3227 # terrain presampling listener dispatcher
3228 ###########################################################
3229
3230 var manage_presampling = func {
3231
3232
3233
3234 var status = getprop(lw~"tmp/presampling-status");
3235
3236
3237 # we only take action when the analysis is done
3238 if (status != "finished") {return;}
3239
3240 if (getprop(lw~"tiles/tile-counter") == 0) # we deal with a tile setup call from the menu
3241 {
3242 set_tile();
3243 }
3244 else # the tile setup call came from weather_tile_management
3245 {
3246 var lat = getprop(lw~"tiles/tmp/latitude-deg");
3247 var lon = getprop(lw~"tiles/tmp/longitude-deg");
3248 var code = getprop(lw~"tiles/tmp/code");
3249 var dir_index = getprop(lw~"tiles/tmp/dir-index");
3250
3251 weather_tile_management.generate_tile(code, lat, lon, dir_index);
3252 }
3253
3254
3255 # set status to idle again
3256
3257 setprop(lw~"tmp/presampling-status", "idle");
3258
3259 }
3260
3261
3262 ###########################################################
3263 # hardcoded terrain presampling listener dispatcher
3264 ###########################################################
3265
3266 var manage_hardcoded_presampling = func {
3267
3268 var status = getprop("/environment/terrain/area[0]/enabled");
3269
3270 print("Hard-coded terrain presampling status: ", status);
3271
3272 # no action unless the sampler has finished
3273 if (status ==0) {return;}
3274
3275 # no action if the sampler hasn't been started
3276
3277 if (getprop(lw~"tmp/presampling-status") != "sampling") {return;}
3278
3279 terrain_presampling_analysis();
3280 if (debug_output_flag == 1)
3281 {print("Presampling done!");}
3282 setprop(lw~"tmp/presampling-status", "finished");
3283
3284
3285 }
3286
3287 ###########################################################
3288 # set wind model flag
3289 ###########################################################
3290
3291 var set_wind_model_flag = func {
3292
3293 var wind_model = getprop(lw~"config/wind-model");
3294
3295 if (wind_model == "constant") {wind_model_flag = 1;}
3296 else if (wind_model == "constant in tile") {wind_model_flag =2;}
3297 else if (wind_model == "aloft interpolated") {wind_model_flag =3; }
3298 else if (wind_model == "airmass interpolated") {wind_model_flag =4;}
3299 else if (wind_model == "aloft waypoints") {wind_model_flag =5;}
3300 else {print("Wind model not implemented!"); wind_model_flag =1;}
3301
3302
3303 }
3304
3305
3306 ###########################################################
3307 # set texture mix for convective clouds
3308 ###########################################################
3309
3310 var set_texture_mix = func {
3311
3312 var thermal_properties = getprop(lw~"config/thermal-properties");
3313 thermal_conditions = thermal_properties;
3314
3315 convective_texture_mix = -(thermal_properties - 1.0) * 0.4;
3316
3317 if (convective_texture_mix < -0.2) {convective_texture_mix = -0.2;}
3318 if (convective_texture_mix > 0.2) {convective_texture_mix = 0.2;}
3319
3320 lowest_layer_turbulence = 0.7 - thermal_properties;
3321 if (lowest_layer_turbulence < 0.0) {lowest_layer_turbulence = 0.0;}
3322 }
3323
3324 ###########################################################
3325 # create an effect volume
3326 ###########################################################
3327
3328 var create_effect_volume = func (geometry, lat, lon, r1, r2, phi, alt_low, alt_high, vis, rain, snow, turb, lift, lift_flag, sat) {
3329
3330
3331 var ev = effectVolume.new (geometry, lat, lon, r1, r2, phi, alt_low, alt_high, vis, rain, snow, turb, lift, lift_flag, sat);
3332 ev.index = getprop(lw~"tiles/tile-counter");
3333 ev.active_flag = 0;
3334
3335
3336 if (vis < 0.0) {ev.vis_flag = 0;} else {ev.vis_flag = 1;}
3337 if (rain < 0.0) {ev.rain_flag = 0;} else {ev.rain_flag = 1;}
3338 if (snow < 0.0) {ev.snow_flag = 0;} else {ev.snow_flag = 1;}
3339 if (turb < 0.0) {ev.turb_flag = 0;} else {ev.turb_flag = 1;}
3340 if (lift_flag == 0.0) {ev.lift_flag = 0;} else {ev.lift_flag = 1;}
3341 if (sat < 0.0) {ev.sat_flag = 0;} else {ev.sat_flag = 1;}
3342 if (sat > 1.0) {sat = 1.0;}
3343
3344 if (lift_flag == -2) # we create a thermal by function
3345 {
3346 ev.lift_flag = 2;
3347 ev.radius = 0.8 * r1;
3348 ev.height = alt_high * 0.87;
3349 ev.cn = 0.7 + rand() * 0.2;
3350 ev.sh = 0.7 + rand() * 0.2;
3351 ev.max_lift = lift;
3352 ev.f_lift_radius = 0.7 + rand() * 0.2;
3353 if (dynamics_flag == 1) # globals set by the convective system
3354 {
3355 ev.flt = cloud_fractional_lifetime;
3356 ev.evolution_timestamp = cloud_evolution_timestamp;
3357 }
3358 }
3359
3360 if (lift_flag == -3) # we create a wave lift
3361 {
3362 ev.lift_flag = 3;
3363 ev.height = 10000.0; # scale height in ft
3364 ev.max_lift = lift;
3365 ev.index = 0; # static objects are assigned tile id zero
3366 }
3367
3368 # set a timestamp if needed
3369
3370 if (dynamics_flag == 1)
3371 {
3372 ev.timestamp = weather_dynamics.time_lw;
3373 }
3374
3375 # and add to the counter
3376 setprop(lw~"effect-volumes/number",getprop(lw~"effect-volumes/number")+1);
3377
3378 append(effectVolumeArray,ev);
3379 }
3380
3381
3382
3383
3384
3385 ###########################################################
3386 # set a weather station for interpolation
3387 ###########################################################
3388
3389 var set_weather_station = func (lat, lon, alt, vis, T, D, p) {
3390
3391 var s = weatherStation.new (lat, lon, alt, vis, T, D, p);
3392 s.index = getprop(lw~"tiles/tile-counter");
3393 s.weight = 0.02;
3394
3395 # set a timestamp if needed
3396
3397 if (dynamics_flag == 1)
3398 {
3399 s.timestamp = weather_dynamics.time_lw;
3400 }
3401 append(weatherStationArray,s);
3402
3403 }
3404
3405
3406 ###########################################################
3407 # set an atmosphere condition point for interpolation
3408 ###########################################################
3409
3410 var set_atmosphere_ipoint = func (lat, lon, vis_aloft, vis_alt1, vis_ovcst, ovcst,ovcst_alt_low, ovcst_alt_high, scatt, scatt_alt_low, scatt_alt_high) {
3411
3412 var a = atmosphereIpoint.new (lat, lon, vis_aloft, vis_alt1, vis_ovcst, ovcst, ovcst_alt_low, ovcst_alt_high, scatt, scatt_alt_low, scatt_alt_high);
3413 a.index = getprop(lw~"tiles/tile-counter");
3414 a.weight = 0.02;
3415
3416 # set a timestamp if needed
3417
3418 if (dynamics_flag == 1)
3419 {
3420 a.timestamp = weather_dynamics.time_lw;
3421 }
3422 append(atmosphereIpointArray,a);
3423
3424 }
3425
3426
3427 ###########################################################
3428 # set a wind interpolation point
3429 ###########################################################
3430
3431 var set_wind_ipoint = func (lat, lon, d0, v0, d1, v1, d2, v2, d3, v3, d4, v4, d5, v5, d6, v6, d7, v7, d8, v8) {
3432
3433 var w = windIpoint.new(lat, lon, d0, v0, d1, v1, d2, v2, d3, v3, d4, v4, d5, v5, d6, v6, d7, v7, d8, v8);
3434
3435 append(windIpointArray, w);
3436
3437
3438 }
3439
3440
3441 ###########################################################
3442 # set a wind interpolation point from ground METAR data
3443 ###########################################################
3444
3445 var set_wind_ipoint_metar = func (lat, lon, d0, v0) {
3446
3447 # insert a plausible pattern of aloft winds based on ground info
3448
3449
3450 # direction of Coriolis deflection depends on hemisphere
3451 if (lat >0.0) {var dsign = -1.0;} else {var dsign = 1.0;}
3452
3453
3454 var v1 = v0 * (1.0 + rand() * 0.1);
3455 var d1 = d0 + dsign * 2.0 * rand();
3456
3457 var v2 = v0 * (1.2 + rand() * 0.2);
3458 var d2 = d0 + dsign * (3.0 * rand() + 2.0);
3459
3460 var v3 = v0 * (1.3 + rand() * 0.4) + 5.0;
3461 var d3 = d0 + dsign * (3.0 * rand() + 4.0);
3462
3463 var v4 = v0 * (1.7 + rand() * 0.5) + 10.0;
3464 var d4 = d0 + dsign * (4.0 * rand() + 8.0);
3465
3466 var v5 = v0 * (1.7 + rand() * 0.5) + 20.0;
3467 var d5 = d0 + dsign * (4.0 * rand() + 10.0);
3468
3469 var v6 = v0 * (1.7 + rand() * 0.5) + 40.0;
3470 var d6 = d0 + dsign * (4.0 * rand() + 12.0);
3471
3472 var v7 = v0 * (2.0 + rand() * 0.7) + 50.0;
3473 var d7 = d0 + dsign * (4.0 * rand() + 13.0);
3474
3475 var v8 = v0 * (2.0 + rand() * 0.7) + 55.0;;
3476 var d8 = d0 + dsign * (5.0 * rand() + 14.0);
3477
3478 var w = windIpoint.new(lat, lon, d0, v0, d1, v1, d2, v2, d3, v3, d4, v4, d5, v5, d6, v6, d7, v7, d8, v8);
3479
3480 append(windIpointArray, w);
3481
3482
3483
3484 }
3485
3486 ###########################################################
3487 # helper to show additional dialogs
3488 ###########################################################
3489
3490 var showDialog = func (name) {
3491
3492 fgcommand("dialog-show", props.Node.new({"dialog-name":name}));
3493
3494 }
3495
3496
3497 ###########################################################
3498 # helper to transfer configuration flags in menu to Nasal
3499 ###########################################################
3500
3501 var readFlags = func {
3502
3503 # thermal lift must be 1 for constant thermals (obsolete), 2 for thermals by model (menu default)
3504 # and 3 for blue thermals (set internally inside the tile only)
3505
3506 if (getprop(lw~"config/generate-thermal-lift-flag") ==1) {generate_thermal_lift_flag = 2;}
3507 else {generate_thermal_lift_flag = 0};
3508
3509 thread_flag = getprop(lw~"config/thread-flag");
3510 # dynamics_flag = getprop(lw~"config/dynamics-flag");
3511 presampling_flag = getprop(lw~"config/presampling-flag");
3512 detailed_clouds_flag = getprop(lw~"config/detailed-clouds-flag");
3513 dynamical_convection_flag = getprop(lw~"config/dynamical-convection-flag");
3514 debug_output_flag = getprop(lw~"config/debug-output-flag");
3515 fps_control_flag = getprop(lw~"config/fps-control-flag");
3516 realistic_visibility_flag = getprop(lw~"config/realistic-visibility-flag");
3517 detailed_terrain_interaction_flag = getprop(lw~"config/detailed-terrain-interaction-flag");
3518 scattering_shader_flag = getprop("/sim/rendering/shaders/skydome");
3519
3520 # also initialize menu entries
3521
3522 air_pollution_norm = getprop("/environment/air-pollution-norm");
3523
3524 }
3525
3526 ###########################################################
3527 # wrappers to call functions from the local weather menu bar
3528 ###########################################################
3529
3530 var streak_wrapper = func {
3531
3532 thread_flag = 0;
3533 dynamics_flag = 0;
3534 presampling_flag = 0;
3535
3536 var array = [];
3537 append(weather_tile_management.modelArrays,array);
3538 setprop(lw~"tiles/tile-counter",getprop(lw~"tiles/tile-counter")+1);
3539
3540 var lat = getprop("position/latitude-deg");
3541 var lon = getprop("position/longitude-deg");
3542 var type = getprop("/local-weather/tmp/cloud-type");
3543 var alt = getprop("/local-weather/tmp/alt");
3544 var nx = getprop("/local-weather/tmp/nx");
3545 var xoffset = getprop("/local-weather/tmp/xoffset");
3546 var xedge = getprop("/local-weather/tmp/xedge");
3547 var ny = getprop("/local-weather/tmp/ny");
3548 var yoffset = getprop("/local-weather/tmp/yoffset");
3549 var yedge = getprop("/local-weather/tmp/yedge");
3550 var dir = getprop("/local-weather/tmp/dir");
3551 var tri = getprop("/local-weather/tmp/tri");
3552 var rnd_alt = getprop("/local-weather/tmp/rnd-alt");
3553 var rnd_pos_x = getprop("/local-weather/tmp/rnd-pos-x");
3554 var rnd_pos_y = getprop("/local-weather/tmp/rnd-pos-y");
3555
3556 create_streak(type,lat,lon,alt,rnd_alt,nx,xoffset,xedge,rnd_pos_x,ny,yoffset,yedge,rnd_pos_y,dir,tri);
3557 }
3558
3559
3560 var convection_wrapper = func {
3561
3562 thread_flag = 0;
3563 dynamics_flag = 0;
3564 presampling_flag = 0;
3565
3566
3567 var array = [];
3568 append(weather_tile_management.modelArrays,array);
3569 setprop(lw~"tiles/tile-counter",getprop(lw~"tiles/tile-counter")+1);
3570
3571 var lat = getprop("position/latitude-deg");
3572 var lon = getprop("position/longitude-deg");
3573 var alt = getprop("/local-weather/tmp/conv-alt");
3574 var size = getprop("/local-weather/tmp/conv-size");
3575 var strength = getprop("/local-weather/tmp/conv-strength");
3576
3577 var n = int(10 * size * size * strength);
3578 create_cumosys(lat,lon,alt,n, size*1000.0);
3579
3580 }
3581
3582 var barrier_wrapper = func {
3583
3584
3585 thread_flag = 0;
3586 dynamics_flag = 0;
3587 presampling_flag = 0;
3588
3589
3590 var array = [];
3591 append(weather_tile_management.modelArrays,array);
3592 setprop(lw~"tiles/tile-counter",getprop(lw~"tiles/tile-counter")+1);
3593
3594 var lat = getprop("position/latitude-deg");
3595 var lon = getprop("position/longitude-deg");
3596 var alt = getprop("/local-weather/tmp/bar-alt");
3597 var n = getprop("/local-weather/tmp/bar-n");
3598 var dir = getprop("/local-weather/tmp/bar-dir");
3599 var dist = getprop("/local-weather/tmp/bar-dist") * 1000.0;
3600 var size = getprop("/local-weather/tmp/bar-size") * 1000.0;
3601
3602 create_rise_clouds(lat, lon, alt, n, size, dir, dist);
3603
3604 }
3605
3606 var single_cloud_wrapper = func {
3607
3608 thread_flag = 0;
3609 dynamics_flag = 0;
3610 presampling_flag = 0;
3611
3612
3613
3614 var array = [];
3615 append(weather_tile_management.modelArrays,array);
3616 setprop(lw~"tiles/tile-counter",getprop(lw~"tiles/tile-counter")+1);
3617
3618 var type = getprop("/local-weather/tmp/scloud-type");
3619 var subtype = getprop("/local-weather/tmp/scloud-subtype");
3620 var lat = getprop("/local-weather/tmp/scloud-lat");
3621 var lon = getprop("/local-weather/tmp/scloud-lon");
3622 var alt = getprop("/local-weather/tmp/scloud-alt");
3623 var heading = getprop("/local-weather/tmp/scloud-dir");
3624
3625 var path = select_cloud_model(type,subtype);
3626
3627 compat_layer.create_cloud(path, lat, lon, alt, heading);
3628
3629 }
3630
3631 var layer_wrapper = func {
3632
3633 thread_flag = 0;
3634 dynamics_flag = 0;
3635 presampling_flag = 0;
3636
3637
3638 var array = [];
3639 append(weather_tile_management.modelArrays,array);
3640 setprop(lw~"tiles/tile-counter",getprop(lw~"tiles/tile-counter")+1);
3641
3642 var lat = getprop("position/latitude-deg");
3643 var lon = getprop("position/longitude-deg");
3644 var type = getprop(lw~"tmp/layer-type");
3645 var rx = getprop(lw~"tmp/layer-rx") * 1000.0;
3646 var ry = getprop(lw~"tmp/layer-ry") * 1000.0;
3647 var phi = getprop(lw~"tmp/layer-phi");
3648 var alt = getprop(lw~"tmp/layer-alt");
3649 var thick = getprop(lw~"tmp/layer-thickness");
3650 var density = getprop(lw~"tmp/layer-density");
3651 var edge = getprop(lw~"tmp/layer-edge");
3652 var rain_flag = getprop(lw~"tmp/layer-rain-flag");
3653 var rain_density = getprop(lw~"tmp/layer-rain-density");
3654
3655 create_layer(type, lat, lon, alt, thick, rx, ry, phi, density, edge, rain_flag, rain_density);
3656
3657 }
3658
3659 var box_wrapper = func {
3660
3661 thread_flag = 0;
3662 dynamics_flag = 0;
3663 presampling_flag = 0;
3664
3665
3666 setprop(lw~"tiles/tile-counter",getprop(lw~"tiles/tile-counter")+1);
3667
3668 var lat = getprop("position/latitude-deg");
3669 var lon = getprop("position/longitude-deg");
3670 var alt = getprop("position/altitude-ft");
3671 var x = getprop(lw~"tmp/box-x-m");
3672 var y = getprop(lw~"tmp/box-y-m");
3673 var z = getprop(lw~"tmp/box-alt-ft");
3674 var n = getprop(lw~"tmp/box-n");
3675 var f_core = getprop(lw~"tmp/box-core-fraction");
3676 var r_core = getprop(lw~"tmp/box-core-offset");
3677 var h_core = getprop(lw~"tmp/box-core-height");
3678 var n_core = getprop(lw~"tmp/box-core-n");
3679 var f_bottom = getprop(lw~"tmp/box-bottom-fraction");
3680 var h_bottom = getprop(lw~"tmp/box-bottom-thickness");
3681 var n_bottom = getprop(lw~"tmp/box-bottom-n");
3682
3683 var type = "Box_test";
3684
3685
3686
3687 create_cloudbox(type, lat, lon, alt, x,y,z,n, f_core, r_core, h_core, n_core, f_bottom, h_bottom, n_bottom);
3688
3689 }
3690
3691
3692 var set_aloft_wrapper = func {
3693
3694
3695
3696 var lat = getprop(lw~"tmp/ipoint-latitude-deg");
3697 var lon = getprop(lw~"tmp/ipoint-longitude-deg");
3698
3699 var d0 = getprop(lw~"tmp/FL0-wind-from-heading-deg");
3700 var v0 = getprop(lw~"tmp/FL0-windspeed-kt");
3701
3702 var d1 = getprop(lw~"tmp/FL50-wind-from-heading-deg");
3703 var v1 = getprop(lw~"tmp/FL50-windspeed-kt");
3704
3705 var d2 = getprop(lw~"tmp/FL100-wind-from-heading-deg");
3706 var v2 = getprop(lw~"tmp/FL100-windspeed-kt");
3707
3708 var d3 = getprop(lw~"tmp/FL180-wind-from-heading-deg");
3709 var v3 = getprop(lw~"tmp/FL180-windspeed-kt");
3710
3711 var d4 = getprop(lw~"tmp/FL240-wind-from-heading-deg");
3712 var v4 = getprop(lw~"tmp/FL240-windspeed-kt");
3713
3714 var d5 = getprop(lw~"tmp/FL300-wind-from-heading-deg");
3715 var v5 = getprop(lw~"tmp/FL300-windspeed-kt");
3716
3717 var d6 = getprop(lw~"tmp/FL340-wind-from-heading-deg");
3718 var v6 = getprop(lw~"tmp/FL340-windspeed-kt");
3719
3720 var d7 = getprop(lw~"tmp/FL390-wind-from-heading-deg");
3721 var v7 = getprop(lw~"tmp/FL390-windspeed-kt");
3722
3723 var d8 = getprop(lw~"tmp/FL450-wind-from-heading-deg");
3724 var v8 = getprop(lw~"tmp/FL450-windspeed-kt");
3725
3726 set_wind_ipoint(lat, lon, d0, v0, d1, v1, d2, v2, d3, v3, d4, v4, d5, v5, d6, v6, d7, v7, d8, v8);
3727
3728 if (wind_model_flag == 5)
3729 {setprop(lwi~"ipoint-number", getprop(lwi~"ipoint-number") + 1);}
3730
3731 }
3732
3733 ####################################
3734 # tile setup call wrapper
3735 ####################################
3736
3737 var set_tile = func {
3738
3739 # check if another instance of local weather is running already
3740
3741
3742 if (local_weather_running_flag == 1)
3743 {
3744 setprop("/sim/messages/pilot", "Local weather: Local weather is already running, use Clear/End before restarting. Aborting...");
3745 return;
3746 }
3747
3748 local_weather_startup_flag = 1;
3749
3750 # randomize high ice scattering properties
3751
3752 setprop("/environment/scattering-phenomena/ring-factor", rand());
3753 setprop("/environment/scattering-phenomena/rainbow-factor", rand());
3754
3755
3756 var type = getprop("/local-weather/tmp/tile-type");
3757
3758 # set tile center coordinates to current position
3759
3760 var lat = getprop("position/latitude-deg");
3761 var lon = getprop("position/longitude-deg");
3762
3763 setprop(lw~"tiles/tmp/latitude-deg",lat);
3764 setprop(lw~"tiles/tmp/longitude-deg",lon);
3765 setprop(lw~"tiles/tmp/dir-index",4);
3766
3767 readFlags();
3768
3769 # check consistency of flags
3770
3771 if (dynamical_convection_flag == 1)
3772 {
3773 if (dynamics_flag == 0)
3774 {
3775 print("Dynamical convection needs weather dynamics to run! Aborting...");
3776 setprop("/sim/messages/pilot", "Local weather: dynamical convection needs weather dynamics to run! Aborting...");
3777 return;
3778 }
3779 if (presampling_flag == 0)
3780 {
3781 print("Dynamical convection needs terrain presampling to run! Aborting...");
3782 setprop("/sim/messages/pilot", "Local weather: dynamical convection needs terrain presampling to run! Aborting...");
3783 return;
3784 }
3785 }
3786
3787 if (detailed_terrain_interaction_flag == 1)
3788 {
3789 if (presampling_flag == 0)
3790 {
3791 print("Terrain effect needs terrain presampling to run! Aborting...");
3792 setprop("/sim/messages/pilot", "Local weather: terrain effect needs terrain presampling to run! Aborting...");
3793 return;
3794 }
3795 }
3796
3797
3798 # if we can do so, we switch global weather and METAR parsing in environment off at this point
3799
3800 if (compat_layer.features.can_disable_environment ==1)
3801 {
3802 props.globals.getNode("/environment/config/enabled").setBoolValue(0);
3803 props.globals.getNode("/environment/params/metar-updates-environment").setBoolValue(0);
3804 }
3805
3806
3807 # switch off normal 3d clouds
3808
3809 local_weather.setDefaultCloudsOff();
3810
3811 # read max. visibility range and set far camera clipping
3812
3813 max_vis_range = math.exp(getprop(lw~"config/aux-max-vis-range-m"));
3814 setprop(lw~"config/max-vis-range-m",max_vis_range);
3815 if (max_vis_range>120000.0){setprop("/sim/rendering/camera-group/zfar",max_vis_range);}
3816
3817 # now see if we need to presample the terrain
3818
3819 if ((presampling_flag == 1) and (getprop(lw~"tmp/presampling-status") == "idle"))
3820 {
3821 terrain_presampling_start(lat, lon, 1000, 40000, getprop(lw~"tmp/tile-orientation-deg"));
3822 return;
3823 }
3824
3825
3826 # indicate that we're up and running
3827
3828 local_weather_startup_flag = 0;
3829 local_weather_running_flag = 1;
3830
3831 # see if we use METAR for weather setup
3832
3833 if ((getprop("/environment/metar/valid") == 1) and (getprop(lw~"tmp/tile-management") == "METAR"))
3834 {
3835 type = "METAR";
3836 metar_flag = 1;
3837
3838 setprop(lw~"METAR/station-id","METAR");
3839
3840
3841
3842 }
3843 else if ((getprop("/environment/metar/valid") == 0) and (getprop(lw~"tmp/tile-management") == "METAR"))
3844 {
3845 print("No METAR available, aborting...");
3846 setprop("/sim/messages/pilot", "Local weather: No METAR available! Aborting...");
3847 return;
3848 }
3849
3850
3851 # see if we need to create an aloft wind interpolation structure
3852
3853 set_wind_model_flag();
3854
3855
3856 if ((wind_model_flag == 3) or ((wind_model_flag ==5) and (getprop(lwi~"ipoint-number") == 0)))
3857 {
3858 if (metar_flag != 1)
3859 {set_aloft_wrapper();}
3860 }
3861
3862
3863 # prepare the first tile wind field
3864
3865 if (metar_flag == 1) # the winds from current METAR are used
3866 {
3867
3868 # METAR reports ground winds, we want to set aloft, so we need to compute the local boundary layer
3869 # need to set the tile index for this
3870 setprop(lw~"tiles/tile[4]/tile-index",1);
3871
3872 var boundary_correction = 1.0/get_slowdown_fraction();
3873 var metar_base_wind_deg = getprop("environment/metar/base-wind-dir-deg");
3874 var metar_base_wind_speed = boundary_correction * getprop("environment/metar/base-wind-speed-kt");
3875
3876 # set the wind hash for the new scheme
3877
3878 wind.cloudlayer = [metar_base_wind_deg,metar_base_wind_speed];
3879 wind.surface = [metar_base_wind_deg,metar_base_wind_speed/boundary_correction];
3880 wind.current = wind.surface;
3881
3882
3883 if ((wind_model_flag == 1) or (wind_model_flag == 2))
3884 {
3885 append(weather_dynamics.tile_wind_direction, metar_base_wind_deg);
3886 append(weather_dynamics.tile_wind_speed, metar_base_wind_speed);
3887 setprop(lw~"tmp/tile-orientation-deg",metar_base_wind_deg);
3888 }
3889 else if (wind_model_flag == 5)
3890 {
3891 var station_lat = getprop("/environment/metar/station-latitude-deg");
3892 var station_lon = getprop("/environment/metar/station-longitude-deg");
3893
3894 set_wind_ipoint_metar(station_lat, station_lon, metar_base_wind_deg, metar_base_wind_speed);
3895
3896 var res = wind_interpolation(lat,lon,0.0);
3897
3898
3899
3900 append(weather_dynamics.tile_wind_direction,res[0]);
3901 append(weather_dynamics.tile_wind_speed,res[1]);
3902 setprop(lw~"tmp/tile-orientation-deg", weather_dynamics.tile_wind_direction[0]);
3903
3904 # in case of gusty winds, these need to be re-initialized to the base wind
3905 # from METAR rather than the menu
3906 interpolated_conditions.wind_from_heading_deg = metar_base_wind_deg;
3907 interpolated_conditions.windspeed_kt = metar_base_wind_speed;
3908 }
3909 else
3910 {
3911 print("Wind model currently not supported with live data!");
3912 setprop("/sim/messages/pilot", "Local weather: Wind model currently not supported with live data! Aborting...");
3913 return;
3914 }
3915 }
3916 else
3917 {
3918 setprop(lw~"tiles/tile[4]/tile-index",1);
3919 var boundary_correction = get_slowdown_fraction();
3920
3921 if (wind_model_flag == 5) # it needs to be interpolated
3922 {
3923 var res = wind_interpolation(lat,lon,0.0);
3924
3925 append(weather_dynamics.tile_wind_direction,res[0]);
3926 append(weather_dynamics.tile_wind_speed,res[1]);
3927
3928 # set the wind hash for the new scheme
3929
3930 wind.surface = [res[0],res[1] * boundary_correction];
3931 wind.cloudlayer = res;
3932 wind.current = wind.surface;
3933
3934
3935 }
3936 else if (wind_model_flag == 3) # it comes from a different menu
3937 {
3938 append(weather_dynamics.tile_wind_direction, getprop(lw~"tmp/FL0-wind-from-heading-deg"));
3939 append(weather_dynamics.tile_wind_speed, getprop(lw~"tmp/FL0-windspeed-kt"));
3940
3941 # set the wind hash for the new scheme
3942 wind.cloudlayer = [getprop(lw~"tmp/FL0-wind-from-heading-deg"),getprop(lw~"tmp/FL0-windspeed-kt") ];
3943 wind.surface = [wind.cloudlayer[0],wind.cloudlayer[1] * boundary_correction];
3944 wind.current = wind.surface;
3945
3946 }
3947 else # it comes from the standard menu
3948 {
3949 append(weather_dynamics.tile_wind_direction, getprop(lw~"tmp/tile-orientation-deg"));
3950 append(weather_dynamics.tile_wind_speed, getprop(lw~"tmp/windspeed-kt"));
3951
3952 # set the wind hash for the new scheme
3953 wind.cloudlayer = [getprop(lw~"tmp/tile-orientation-deg"),getprop(lw~"tmp/windspeed-kt")];
3954 wind.surface = [wind.cloudlayer[0],wind.cloudlayer[1] * boundary_correction];
3955 wind.current = wind.surface;
3956
3957 }
3958
3959 # when the aloft wind menu is used, the lowest winds should be taken from there
3960 # so we need to overwrite the setting from the tile generating menu in this case
3961 # otherwise the wrong orientation is built
3962
3963
3964 if (wind_model_flag ==3)
3965 {
3966 setprop(lw~"tmp/tile-orientation-deg", getprop(lw~"tmp/FL0-wind-from-heading-deg"));
3967 }
3968 else if (wind_model_flag == 5)
3969 {
3970 setprop(lw~"tmp/tile-orientation-deg", weather_dynamics.tile_wind_direction[0]);
3971 }
3972 }
3973
3974 # create all the neighbouring tile coordinate sets
3975
3976 weather_tile_management.create_neighbours(lat,lon,getprop(lw~"tmp/tile-orientation-deg"));
3977
3978
3979
3980
3981 setprop(lw~"tiles/tile-counter",getprop(lw~"tiles/tile-counter")+1);
3982
3983
3984 # see if we need to generate a quadtree structure for clouds
3985
3986 if (dynamics_flag ==1)
3987 {
3988 var quadtree = [];
3989 weather_dynamics.generate_quadtree_structure(0, quadtree);
3990 append(weather_dynamics.cloudQuadtrees,quadtree);
3991 }
3992
3993
3994
3995
3996 if (type == "High-pressure-core")
3997 {weather_tiles.set_high_pressure_core_tile();}
3998 else if (type == "High-pressure")
3999 {weather_tiles.set_high_pressure_tile();}
4000 else if (type == "High-pressure-border")
4001 {weather_tiles.set_high_pressure_border_tile();}
4002 else if (type == "Low-pressure-border")
4003 {weather_tiles.set_low_pressure_border_tile();}
4004 else if (type == "Low-pressure")
4005 {weather_tiles.set_low_pressure_tile();}
4006 else if (type == "Low-pressure-core")
4007 {weather_tiles.set_low_pressure_core_tile();}
4008 else if (type == "Cold-sector")
4009 {weather_tiles.set_cold_sector_tile();}
4010 else if (type == "Warm-sector")
4011 {weather_tiles.set_warm_sector_tile();}
4012 else if (type == "Tropical")
4013 {weather_tiles.set_tropical_weather_tile();}
4014 else if (type == "Coldfront")
4015 {weather_tiles.set_coldfront_tile();}
4016 else if (type == "Warmfront")
4017 {weather_tiles.set_warmfront1_tile();}
4018 else if (type == "Warmfront-2")
4019 {weather_tiles.set_warmfront2_tile();}
4020 else if (type == "Warmfront-3")
4021 {weather_tiles.set_warmfront3_tile();}
4022 else if (type == "Warmfront-4")
4023 {weather_tiles.set_warmfront4_tile();}
4024 else if (type == "Thunderstorms")
4025 {weather_tiles.set_thunderstorms_tile();}
4026 else if (type == "METAR")
4027 {weather_tiles.set_METAR_tile();}
4028 else if (type == "Altocumulus sky")
4029 {weather_tiles.set_altocumulus_tile();setprop(lw~"tiles/code","altocumulus_sky");}
4030 else if (type == "Broken layers")
4031 {weather_tiles.set_broken_layers_tile();setprop(lw~"tiles/code","broken_layers");}
4032 else if (type == "Cold front")
4033 {weather_tiles.set_coldfront_tile();setprop(lw~"tiles/code","coldfront");}
4034 else if (type == "Cirrus sky")
4035 {weather_tiles.set_cirrus_sky_tile();setprop(lw~"tiles/code","cirrus_sky");}
4036 else if (type == "Fair weather")
4037 {setprop(lw~"tiles/code","cumulus_sky");weather_tiles.set_fair_weather_tile();}
4038 else if (type == "Glider's sky")
4039 {setprop(lw~"tiles/code","gliders_sky");weather_tiles.set_gliders_sky_tile();}
4040 else if (type == "Blue thermals")
4041 {setprop(lw~"tiles/code","blue_thermals");weather_tiles.set_blue_thermals_tile();}
4042 else if (type == "Incoming rainfront")
4043 {weather_tiles.set_rainfront_tile();setprop(lw~"tiles/code","rainfront");}
4044 else if (type == "8/8 stratus sky")
4045 {weather_tiles.set_overcast_stratus_tile();setprop(lw~"tiles/code","overcast_stratus");}
4046 else if (type == "Test tile")
4047 {weather_tiles.set_4_8_stratus_tile();setprop(lw~"tiles/code","test");}
4048 else if (type == "Summer rain")
4049 {weather_tiles.set_summer_rain_tile();setprop(lw~"tiles/code","summer_rain");}
4050 else
4051 {print("Tile not implemented.");setprop(lw~"tiles/tile-counter",getprop(lw~"tiles/tile-counter")-1);return();}
4052
4053
4054 # mark tile as active
4055
4056 append(weather_tile_management.active_tile_list,1);
4057
4058 # start tile management loop if needed
4059
4060 if (getprop(lw~"tmp/tile-management") != "single tile") {
4061 if (getprop(lw~"tile-loop-flag") == 0)
4062 {
4063 setprop(lw~"tiles/tile[4]/code",getprop(lw~"tiles/code"));
4064 setprop(lw~"tile-loop-flag",1);
4065 weather_tile_management.tile_management_loop();}
4066 }
4067
4068 # start the interpolation loop
4069
4070 if (getprop(lw~"interpolation-loop-flag") == 0)
4071 {setprop(lw~"interpolation-loop-flag",1); local_weather.interpolation_loop();}
4072
4073 # start the effect volume loop
4074
4075 if (getprop(lw~"effect-loop-flag") == 0)
4076 {setprop(lw~"effect-loop-flag",1); local_weather.effect_volume_loop(0,0);}
4077
4078 # start weather dynamics loops if needed
4079
4080 if (getprop(lw~"timing-loop-flag") == 0)
4081 {setprop(lw~"timing-loop-flag",1); local_weather.timing_loop();}
4082
4083 if (dynamics_flag ==1)
4084 {
4085
4086
4087 if (getprop(lw~"dynamics-loop-flag") == 0)
4088 {
4089 setprop(lw~"dynamics-loop-flag",1);
4090 # weather_dynamics.quadtree_loop();
4091 weather_dynamics.weather_dynamics_loop(0,0);
4092 }
4093 if ((getprop(lw~"convective-loop-flag") == 0) and (getprop(lw~"config/dynamical-convection-flag") ==1))
4094 {
4095 setprop(lw~"convective-loop-flag",1);
4096 weather_dynamics.convective_loop();
4097 }
4098 }
4099
4100
4101
4102
4103 # and start the buffer loop and housekeeping loop if needed
4104
4105 if (buffer_flag == 1)
4106 {
4107 if (getprop(lw~"buffer-loop-flag") == 0)
4108 {
4109 # setprop(lw~"buffer-loop-flag",1); weather_tile_management.buffer_loop(0);
4110 setprop(lw~"housekeeping-loop-flag",1); weather_tile_management.housekeeping_loop(0,0);
4111 }
4112 }
4113
4114 # start the sea color loop
4115 local_weather.init_sea_colors();
4116
4117 # start the mask loop
4118 #local_weather.init_mask();
4119
4120 # create impostors - this should only happen when sufficiently high in air
4121 weather_tile_management.create_impostors();
4122
4123 # start the cloud shadow loop
4124
4125 local_weather.cloud_shadow_flag = getprop("/local-weather/config/generate-cloud-shadows");
4126
4127 if (local_weather.cloud_shadow_flag == 1)
4128 {
4129 setprop(lw~"shadow-loop-flag",1);
4130 weather_tile_management.shadow_management_loop(0);
4131 }
4132
4133 #weather_tile_management.watchdog_loop();
4134
4135 # start thunderstorm management
4136
4137 setprop(lw~"thunderstorm-loop-flag",1);
4138
4139 local_weather.place_model_controlled("lightning", "Models/Weather/lightning_combined.xml", lat, lon, 0.0, 0.0, 0.0, 0.0);
4140
4141 local_weather.thunderstorm_management_loop();
4142
4143 }
4144
4145
4146 #################################################
4147 # Anything that needs to run at startup goes here
4148 #################################################
4149
4150 var startup = func {
4151 print("Loading local weather routines...");
4152
4153 # get local Cartesian geometry
4154
4155 var lat = getprop("position/latitude-deg");
4156 var lon = getprop("position/longitude-deg");
4157 calc_geo(lat);
4158
4159
4160 # copy weather properties at startup to local weather
4161
4162
4163 interpolated_conditions.visibility_m = getprop(ec~"boundary/entry[0]/visibility-m");
4164 interpolated_conditions.pressure_sea_level_inhg = getprop(ec~"boundary/entry[0]/pressure-sea-level-inhg");
4165 interpolated_conditions.temperature_degc = getprop(ec~"boundary/entry[0]/temperature-degc");
4166 interpolated_conditions.dewpoint_degc = getprop(ec~"boundary/entry[0]/dewpoint-degc");
4167 interpolated_conditions.wind_from_heading_deg = getprop(ec~"boundary/entry[0]/wind-from-heading-deg");
4168 interpolated_conditions.wind_speed_kt = getprop(ec~"boundary/entry[0]/wind-speed-kt");
4169 interpolated_conditions.turbulence = getprop(ec~"boundary/entry[0]/turbulence/magnitude-norm");
4170 interpolated_conditions.rain_norm = 0.0;
4171 interpolated_conditions.snow_norm = 0.0;
4172 interpolated_conditions.thermal_lift = 0.0;
4173
4174
4175 # before interpolation starts, these are also initially current
4176
4177 setprop(lw~"current/visibility-m",interpolated_conditions.visibility_m);
4178 setprop(lw~"current/rain-norm",0.0);
4179 setprop(lw~"current/snow-norm",0.0);
4180 setprop(lw~"current/thermal-lift", 0.0);
4181 setprop(lw~"current/turbulence",interpolated_conditions.turbulence);
4182
4183
4184 # create default properties for METAR system, should be overwritten by real-weather-fetch
4185
4186 setprop(lw~"METAR/latitude-deg",lat);
4187 setprop(lw~"METAR/longitude-deg",lon);
4188 setprop(lw~"METAR/altitude-ft",0.0);
4189 setprop(lw~"METAR/wind-direction-deg",0.0);
4190 setprop(lw~"METAR/wind-strength-kt",10.0);
4191 setprop(lw~"METAR/visibility-m",17000.0);
4192 setprop(lw~"METAR/rain-norm",0.0);
4193 setprop(lw~"METAR/snow-norm",0.0);
4194 setprop(lw~"METAR/temperature-degc",10.0);
4195 setprop(lw~"METAR/dewpoint-degc",7.0);
4196 setprop(lw~"METAR/pressure-inhg",29.92);
4197 setprop(lw~"METAR/thunderstorm-flag",0);
4198 setprop(lw~"METAR/layer[0]/cover-oct",4);
4199 setprop(lw~"METAR/layer[0]/alt-agl-ft", 3000.0);
4200 setprop(lw~"METAR/layer[1]/cover-oct",0);
4201 setprop(lw~"METAR/layer[1]/alt-agl-ft", 20000.0);
4202 setprop(lw~"METAR/layer[2]/cover-oct",0);
4203 setprop(lw~"METAR/layer[2]/alt-agl-ft", 20000.0);
4204 setprop(lw~"METAR/layer[3]/cover-oct",0);
4205 setprop(lw~"METAR/layer[3]/alt-agl-ft", 20000.0);
4206 setprop(lw~"METAR/available-flag",1);
4207
4208
4209
4210
4211 # set listeners
4212
4213 setlistener(lw~"tmp/thread-status", func {var s = size(clouds_path); compat_layer.create_cloud_array(s, clouds_path, clouds_lat, clouds_lon, clouds_alt, clouds_orientation); });
4214 setlistener(lw~"tmp/convective-status", func {var s = size(clouds_path); compat_layer.create_cloud_array(s, clouds_path, clouds_lat, clouds_lon, clouds_alt, clouds_orientation); });
4215 setlistener(lw~"tmp/effect-thread-status", func {var s = size(effects_geo); effect_placement_loop(s); });
4216 setlistener(lw~"tmp/presampling-status", func {manage_presampling(); });
4217
4218 #setlistener(lw~"config/wind-model", func {set_wind_model_flag();});
4219 setlistener(lw~"config/thermal-properties", func {set_texture_mix();});
4220
4221 setlistener(lw~"config/clouds-in-dynamics-loop", func(n) {weather_dynamics.max_clouds_in_loop = int(n.getValue());});
4222
4223 setlistener(lw~"config/clouds-visible-range-m", func(n) {weather_tile_management.cloud_view_distance = n.getValue();});
4224 setlistener(lw~"config/distance-to-load-tile-m", func(n) {setprop(lw~"config/distance-to-remove-tile-m",n.getValue() + 500.0);});
4225
4226 setlistener(lw~"config/fps-control-flag", func(n) {fps_control_flag = n.getValue();});
4227 setlistener(lw~"config/target-framerate", func(n) {target_framerate = n.getValue();});
4228
4229 setlistener(lw~"config/small-scale-persistence", func(n) {weather_tiles.small_scale_persistence = n.getValue();});
4230 setlistener(lw~"config/ground-haze-factor", func(n) {ground_haze_factor = n.getValue();});
4231 setlistener(lw~"config/aux-max-vis-range-m", func(n) {
4232 max_vis_range = math.exp(n.getValue());
4233 setprop(lw~"config/max-vis-range-m",max_vis_range);
4234 if (max_vis_range>120000.0){setprop("/sim/rendering/camera-group/zfar",max_vis_range);}
4235 });
4236
4237
4238 setlistener(lw~"config/temperature-offset-degc", func(n) {temperature_offset = n.getValue();});
4239
4240 setlistener("/environment/air-pollution-norm", func(n) {air_pollution_norm = n.getValue() ;});
4241
4242 setlistener("/sim/rendering/shaders/skydome", func(n) {scattering_shader_flag = n.getValue() ; if (scattering_shader_flag ==1) {setprop("/sim/rendering/minimum-sky-visibility",0.0);} else {setprop("/sim/rendering/minimum-sky-visibility",1000.0);} });
4243 }
4244
4245
4246 #####################################################
4247 # Standard test call (for development and debug only)
4248 #####################################################
4249
4250 var test = func {
4251
4252 var lat = getprop("position/latitude-deg");
4253 var lon = getprop("position/longitude-deg");
4254 var alt = getprop("position/altitude-ft");
4255
4256 # thread_flag = 0;
4257 # dynamics_flag = 0;
4258 # presampling_flag = 0;
4259
4260
4261 #if (compat_layer.features.can_disable_environment ==1)
4262 # {
4263 # props.globals.getNode("/environment/config/enabled").setBoolValue(0);
4264 # props.globals.getNode("/environment/params/metar-updates-environment").setBoolValue(0);
4265 # }
4266 #
4267 #compat_layer.setDefaultCloudsOff();
4268
4269 #var array = [];
4270 #append(weather_tile_management.modelArrays,array);
4271 #setprop(lw~"tiles/tile-counter",getprop(lw~"tiles/tile-counter")+1);
4272
4273
4274 #var pos = geo.aircraft_position();
4275
4276 debug.dump(geodinfo(lat, lon));
4277
4278
4279
4280 #var info = {};
4281
4282 #for (var i = 0; i< 100000; i=i+1)
4283 # {
4284 # info = geodinfo(lat, lon);
4285 # }
4286
4287
4288 }
4289
4290
4291
4292 #################################################################
4293 # object classes
4294 #################################################################
4295
4296 var weatherStation = {
4297 new: func (lat, lon, alt, vis, T, D, p) {
4298 var s = { parents: [weatherStation] };
4299 s.lat = lat;
4300 s.lon = lon;
4301 s.alt = alt;
4302 s.vis = vis;
4303 s.T = T;
4304 s.D = D;
4305 s.p = p;
4306 s.scattering = 0.8;
4307 return s;
4308 },
4309 move: func {
4310 var windfield = weather_dynamics.get_windfield(me.index);
4311 var dt = weather_dynamics.time_lw - me.timestamp;
4312 me.lat = me.lat + windfield[1] * dt * local_weather.m_to_lat;
4313 me.lon = me.lon + windfield[0] * dt * local_weather.m_to_lon;
4314 me.timestamp = weather_dynamics.time_lw;
4315 },
4316 };
4317
4318
4319 var atmosphereIpoint = {
4320 new: func (lat, lon, vis_aloft, vis_alt1, vis_ovcst, ovcst, ovcst_alt_low, ovcst_alt_high, scatt, scatt_alt_low, scatt_alt_high){
4321 var a = { parents: [atmosphereIpoint] };
4322 a.lat = lat;
4323 a.lon = lon;
4324 a.vis_aloft = vis_aloft;
4325 a.vis_alt1 = vis_alt1;
4326 a.vis_ovcst = vis_ovcst;
4327 a.ovcst = ovcst;
4328 a.ovcst_alt_low = ovcst_alt_low;
4329 a.ovcst_alt_high = ovcst_alt_high;
4330 a.scatt = scatt;
4331 a.scatt_alt_low = scatt_alt_low;
4332 a.scatt_alt_high = scatt_alt_high;
4333 return a;
4334 },
4335 move: func {
4336 var windfield = weather_dynamics.get_windfield(me.index);
4337 var dt = weather_dynamics.time_lw - me.timestamp;
4338 me.lat = me.lat + windfield[1] * dt * local_weather.m_to_lat;
4339 me.lon = me.lon + windfield[0] * dt * local_weather.m_to_lon;
4340 me.timestamp = weather_dynamics.time_lw;
4341 },
4342 };
4343
4344
4345 var windIpoint = {
4346 new: func (lat, lon, d0, v0, d1, v1, d2, v2, d3, v3, d4, v4, d5, v5, d6, v6, d7, v7, d8, v8) {
4347 var w = { parents: [windIpoint] };
4348 w.lat = lat;
4349 w.lon = lon;
4350
4351 altvec = [];
4352
4353 var wv = windVec.new(d0, v0);
4354 append(altvec,wv);
4355
4356 wv = windVec.new(d1, v1);
4357 append(altvec, wv);
4358
4359 wv = windVec.new(d2, v2);
4360 append(altvec, wv);
4361
4362 wv = windVec.new(d3, v3);
4363 append(altvec, wv);
4364
4365 wv = windVec.new(d4, v4);
4366 append(altvec, wv);
4367
4368 wv = windVec.new(d5, v5);
4369 append(altvec, wv);
4370
4371 wv = windVec.new(d6, v6);
4372 append(altvec, wv);
4373
4374 wv = windVec.new(d7, v7);
4375 append(altvec, wv);
4376
4377 wv = windVec.new(d8, v8);
4378 append(altvec, wv);
4379
4380 w.alt = altvec;
4381
4382 w.weight = 0.02;
4383 return w;
4384 },
4385 };
4386
4387 var windVec = {
4388 new: func (d, v) {
4389 var wv = { parents: [windVec] };
4390 wv.d = d;
4391 wv.v = v;
4392 return wv;
4393 },
4394
4395 };
4396
4397
4398
4399
4400
4401 var effectVolume = {
4402 new: func (geometry, lat, lon, r1, r2, phi, alt_low, alt_high, vis, rain, snow, turb, lift, lift_flag, sat) {
4403 var e = { parents: [effectVolume] };
4404 e.geometry = geometry;
4405 e.lat = lat;
4406 e.lon = lon;
4407 e.r1 = r1;
4408 e.r2 = r2;
4409 e.phi = phi;
4410 e.alt_low = alt_low;
4411 e.alt_high = alt_high;
4412 e.vis = vis;
4413 e.rain = rain;
4414 e.snow = snow;
4415 e.turb = turb;
4416 e.lift = lift;
4417 e.lift_flag = lift_flag;
4418 e.sat = sat;
4419 return e;
4420 },
4421 move: func {
4422 var windfield = weather_dynamics.get_windfield(me.index);
4423 var dt = weather_dynamics.time_lw - me.timestamp;
4424 me.lat = me.lat + windfield[1] * dt * local_weather.m_to_lat;
4425 me.lon = me.lon + windfield[0] * dt * local_weather.m_to_lon;
4426 me.timestamp = weather_dynamics.time_lw;
4427 },
4428 correct_altitude: func {
4429 var convective_alt = weather_dynamics.tile_convective_altitude[me.index-1] + alt_20_array[me.index-1];
4430 var elevation = compat_layer.get_elevation(me.lat, me.lon);
4431 me.alt_high = local_weather.get_convective_altitude(convective_alt, elevation, me.index,0.0) *1.15;
4432 me.height = me.alt_high * 0.87;
4433 },
4434 correct_altitude_and_age: func {
4435 var convective_alt = weather_dynamics.tile_convective_altitude[me.index-1] + local_weather.alt_20_array[me.index-1];
4436 var elevation = -1.0; var p_cover = 0.2;
4437 var info = geodinfo(me.lat, me.lon);
4438 if (info != nil)
4439 {
4440 elevation = info[0] * local_weather.m_to_ft;
4441 if (info[1] != nil)
4442 {
4443 var landcover = info[1].names[0];
4444 if (contains(landcover_map,landcover)) {p_cover = landcover_map[landcover];}
4445 else {p_cover = 0.2;}
4446 }
4447 }
4448 me.alt_high = get_convective_altitude(convective_alt, elevation, me.index,0.0) * 1.15;
4449 me.height = me.alt_high * 0.87;
4450 var current_lifetime = math.sqrt(p_cover)/math.sqrt(0.35) * weather_dynamics.cloud_convective_lifetime_s;
4451 var fractional_increase = (weather_dynamics.time_lw - me.evolution_timestamp)/current_lifetime;
4452 me.flt = me.flt + fractional_increase;
4453 me.evolution_timestamp = weather_dynamics.time_lw;
4454 },
4455 get_distance: func {
4456 var lat = getprop("position/latitude-deg");
4457 var lon = getprop("position/longitude-deg");
4458 return math.sqrt(calc_d_sq(lat, lon, me.lat, me.lon));
4459 },
4460 };
4461
4462
4463 var thermalLift = {
4464 new: func (lat, lon, radius, height, cn, sh, max_lift, f_lift_radius) {
4465 var l = { parents: [thermalLift] };
4466 l.lat = lat;
4467 l.lon = lon;
4468 l.radius = radius;
4469 l.height = height;
4470 l.cn = cn;
4471 l.sh = sh;
4472 l.max_lift = max_lift;
4473 l.f_lift_radius = f_lift_radius;
4474 return l;
4475 },
4476 move: func {
4477 var windfield = weather_dynamics.get_windfield(me.index);
4478 var dt = weather_dynamics.time_lw - me.timestamp;
4479 me.lat = me.lat + windfield[1] * dt * local_weather.m_to_lat;
4480 me.lon = me.lon + windfield[0] * dt * local_weather.m_to_lon;
4481 me.timestamp = weather_dynamics.time_lw;
4482 },
4483 correct_altitude: func {
4484 var convective_alt = weather_dynamics.tile_convective_altitude[me.index-1] + alt_20_array[me.index-1];
4485 var elevation = compat_layer.get_elevation(me.lat, me.lon);
4486 me.height = local_weather.get_convective_altitude(convective_alt, elevation, me.index,0.0);
4487 },
4488 correct_altitude_and_age: func {
4489 var convective_alt = weather_dynamics.tile_convective_altitude[me.index-1] + local_weather.alt_20_array[me.index-1];
4490 var elevation = -1.0; var p_cover = 0.2;
4491 var info = geodinfo(me.lat, me.lon);
4492 if (info != nil)
4493 {
4494 elevation = info[0] * local_weather.m_to_ft;
4495 if (info[1] != nil)
4496 {
4497 var landcover = info[1].names[0];
4498 if (contains(landcover_map,landcover)) {p_cover = landcover_map[landcover];}
4499 else {p_cover = 0.2;}
4500 }
4501 }
4502 me.height = get_convective_altitude(convective_alt, elevation, me.index,0.0);
4503 var current_lifetime = math.sqrt(p_cover)/math.sqrt(0.35) * weather_dynamics.cloud_convective_lifetime_s;
4504 var fractional_increase = (weather_dynamics.time_lw - me.evolution_timestamp)/current_lifetime;
4505 me.flt = me.flt + fractional_increase;
4506 me.evolution_timestamp = weather_dynamics.time_lw;
4507 },
4508
4509 };
4510
4511
4512 var waveLift = {
4513 new: func (lat, lon, x, y, phi, height, max_lift) {
4514 var w = { parents: [waveLift] };
4515 w.lat = lat;
4516 w.lon = lon;
4517 w.x = x;
4518 w.y = y;
4519 w.phi = phi;
4520 w.height = height;
4521 w.max_lift = max_lift;
4522 w.phi = getprop(lw~"tmp/tile-orientation-deg");
4523 return w;
4524 },
4525
4526 };
4527
4528
4529
4530 #################################################################
4531 # global variable, property creation and the startup listener
4532 #################################################################
4533
4534 var rad_E = 6378138.12; # earth radius
4535 var lat_to_m = 110952.0; # latitude degrees to meters
4536 var m_to_lat = 9.01290648208234e-06; # meters to latitude degrees
4537 var ft_to_m = 0.30480;
4538 var m_to_ft = 1.0/ft_to_m;
4539 var sec_to_rad = 2.0 * math.pi/86400;
4540
4541 var lon_to_m = 0.0; # needs to be calculated dynamically
4542 var m_to_lon = 0.0; # we do this on startup
4543
4544 # some common abbreviations
4545
4546 var lw = "/local-weather/";
4547 var lwi = "/local-weather/interpolation/";
4548 var ec = "/environment/config/";
4549
4550 # a hash map of the strength for convection associated with terrain types
4551
4552 var landcover_map = {BuiltUpCover: 0.35, Town: 0.35, Freeway:0.35, BarrenCover:0.3, HerbTundraCover: 0.25, GrassCover: 0.2, CropGrassCover: 0.2, EvergreenBroadCover: 0.2, EvergreenNeedleCover: 0.2, Sand: 0.25, Grass: 0.2, Grassland: 0.2, Ocean: 0.01, Marsh: 0.05, Lake: 0.01, ShrubCover: 0.15, Shrub: 0.15, Landmass: 0.2, CropWoodCover: 0.15, MixedForestCover: 0.15, DryCropPastureCover: 0.25, MixedCropPastureCover: 0.2, MixedCrop: 0.2, ComplexCrop: 0.2, IrrCropPastureCover: 0.15, DeciduousBroadCover: 0.1, DeciduousNeedleCover: 0.1, Bog: 0.05, Littoral: 0.05, pa_taxiway : 0.35, pa_tiedown: 0.35, pc_taxiway: 0.35, pc_tiedown: 0.35, Glacier: 0.03, SnowCover: 0.04, DryLake: 0.3, IntermittentStream: 0.2, DryCrop: 0.2, Lava: 0.3, GolfCourse: 0.2, Rock: 0.3, Construction: 0.35, PackIce: 0.04, NaturalCrop: 0.2, Default: 0.2};
4553
4554 # a hash map of average vertical cloud model sizes
4555
4556 var cloud_vertical_size_map = {Altocumulus: 700.0, Cumulus: 600.0, Congestus: 2000.0, Nimbus: 1000.0, Stratus: 800.0, Stratus_structured: 600.0, Stratus_thin: 400.0, Cirrocumulus: 200.0, Cb_box: 2000.0};
4557
4558 # a hash map of offsets for the new cloud rendering system
4559
4560 var offset_map = {Nimbus: 2800.0, Stratus: 2000.0, Stratus_thin: 2500.0, Cirrostratus: 4500.0, Stratus_structured: 1800.0, Stratus_alt: 600.0, Cumulus: 200.0, Congestus: 600.0 };
4561
4562 # the array of aloft wind interpolation altitudes
4563
4564 var wind_altitude_array = [0.0, 5000.0, 10000.0, 18000.0, 24000.0, 30000.0, 34000.0, 39000.0, 45000.0];
4565
4566 # storage arrays for cloud generation
4567
4568 var clouds_path = [];
4569 var clouds_lat = [];
4570 var clouds_lon = [];
4571 var clouds_alt = [];
4572 var clouds_orientation = [];
4573
4574
4575
4576
4577 # storage array for assembled clouds
4578
4579 var cloudAssemblyArray = [];
4580
4581 # additional info needed for dynamical clouds: the base altitude around which cloudlets are distributed
4582 # and the fractional lifetime
4583
4584 var clouds_mean_alt = [];
4585 var clouds_flt = [];
4586 var clouds_evolution_timestamp = [];
4587
4588
4589 # storage arrays for terrain presampling and results by tile
4590
4591 var terrain_n = [];
4592 var alt_50_array = [];
4593 var alt_20_array = [];
4594 var alt_min_array = [];
4595 var alt_mean_array = [];
4596
4597 # array of currently existing effect volumes
4598
4599 var effectVolumeArray = [];
4600 var n_effectVolumeArray = 0;
4601
4602 # global weather hashes
4603
4604 var thermal = {};
4605 var wave = {};
4606 var interpolated_conditions = {};
4607 var current_conditions = {};
4608 var tracerAssembly = {};
4609
4610
4611 # the wind hash stores the current winds
4612
4613 var wind = {surface: [0.0,0.0] , cloudlayer: [0.0,0.0], current: [0.0,0.0]};
4614
4615
4616
4617 # arrays of currently existing weather stations, wind interpolation and atmospheric condition points
4618
4619 var weatherStationArray = [];
4620 var windIpointArray = [];
4621 var atmosphereIpointArray = [];
4622
4623
4624 # a flag for the wind model (so we don't have to do string comparisons all the time)
4625 # 1: constant 2: constant in tile 3: aloft interpolated 4: airmass interpolated
4626
4627 var wind_model_flag = 1;
4628
4629 # globals governing properties of the Cumulus system
4630
4631 var convective_texture_mix = 0.0;
4632 var height_bias = 1.0;
4633 var convective_size_bias = 0.0;
4634 var cumulus_efficiency_factor = 1.0;
4635 var cloud_mean_altitude = 0.0;
4636 var thermal_conditions = getprop(lw~"config/thermal-properties");
4637 var lowest_layer_turbulence = 0.6 - thermal_conditions;
4638 if (lowest_layer_turbulence < 0.0) {lowest_layer_turbulence = 0.0;}
4639
4640 # global keeping track of lighting
4641
4642 var top_shade = 1.0;
4643
4644 # global cloud transparency
4645
4646 var alpha_factor = 1.0;
4647
4648 # global cloud size scale;
4649
4650 var cloud_size_scale = 1.0;
4651
4652 # globals keeping track of the lifetime when building a Cumulus from individual cloudlets
4653
4654 var cloud_fractional_lifetime = 0.0;
4655 var cloud_evolution_timestamp = 0.0;
4656
4657 # globals propagating gust information inside the interpolation loop
4658
4659 var windspeed_multiplier = 1.0;
4660 var winddir_change = 0.0;
4661
4662 # global flags mirroring property tree menu settings
4663
4664 var generate_thermal_lift_flag = 0;
4665 var thread_flag = 1;
4666 var dynamics_flag = 1;
4667 var presampling_flag = 1;
4668 var detailed_clouds_flag = 1;
4669 var dynamical_convection_flag = 1;
4670 var debug_output_flag = 1;
4671 var metar_flag = 0;
4672 var local_weather_running_flag = 0;
4673 var local_weather_startup_flag = 0;
4674 var fps_control_flag = 0;
4675 var buffer_flag = 1;
4676 var detailed_terrain_interaction_flag = 1;
4677 var hardcoded_clouds_flag = 1;
4678 var realistic_visibility_flag = 0;
4679 var scattering_shader_flag = 0;
4680 var wxradar_support_flag = 1;
4681
4682 var ground_haze_factor = 1.0;
4683 var max_vis_range = math.exp(getprop(lw~"config/aux-max-vis-range-m"));
4684 var temperature_offset = 0.0;
4685 var current_mean_alt = 0.0;
4686 var air_pollution_norm = 0.0;
4687
4688 # globals for framerate controlled cloud management
4689
4690 var fps_average = 0.0;
4691 var fps_samples = 0;
4692 var fps_sum = 0.0;
4693 var target_framerate = 25.0;
4694
4695 # set all sorts of default properties for the menu
4696
4697
4698 setprop(lw~"tmp/scloud-lat",getprop("position/latitude-deg"));
4699 setprop(lw~"tmp/scloud-lon",getprop("position/longitude-deg"));
4700 setprop(lw~"tmp/tile-alt-median-ft",0.0);
4701 setprop(lw~"tmp/tile-alt-min-ft",0.0);
4702 setprop(lw~"tmp/last-reading-pos-del",0);
4703 setprop(lw~"tmp/last-reading-pos-mod",0);
4704 setprop(lw~"tmp/thread-status", "idle");
4705 setprop(lw~"tmp/convective-status", "idle");
4706 setprop(lw~"tmp/presampling-status", "idle");
4707 setprop(lw~"tmp/buffer-status", "idle");
4708 setprop(lw~"tmp/buffer-tile-index", 0);
4709 setprop(lw~"tmp/ipoint-latitude-deg",getprop("position/latitude-deg"));
4710 setprop(lw~"tmp/ipoint-longitude-deg",getprop("position/longitude-deg"));
4711
4712
4713
4714 # set the default loop flags to loops inactive
4715
4716
4717 setprop(lw~"effect-loop-flag",0);
4718 setprop(lw~"interpolation-loop-flag",0);
4719 setprop(lw~"tile-loop-flag",0);
4720 setprop(lw~"lift-loop-flag",0);
4721 setprop(lw~"wave-loop-flag",0);
4722 setprop(lw~"buffer-loop-flag",0);
4723 setprop(lw~"housekeeping-loop-flag",0);
4724 setprop(lw~"convective-loop-flag",0);
4725
4726 # create other management properties
4727
4728 #setprop(lw~"clouds/cloud-number",0);
4729 setprop(lw~"clouds/placement-index",0);
4730 setprop(lw~"clouds/model-placement-index",0);
4731 setprop(lw~"effect-volumes/effect-placement-index",0);
4732
4733 # create properties for effect volume management
4734
4735 setprop(lw~"effect-volumes/number",0);
4736 setprop(lw~"effect-volumes/number-active-vis",0);
4737 setprop(lw~"effect-volumes/number-active-rain",0);
4738 setprop(lw~"effect-volumes/number-active-snow",0);
4739 setprop(lw~"effect-volumes/number-active-turb",0);
4740 setprop(lw~"effect-volumes/number-active-lift",0);
4741 setprop(lw~"effect-volumes/number-active-sat",0);
4742
4743 # setprop(lw~"config/max-vis-range-m", 120000.0);
4744 setprop(lw~"config/temperature-offset-degc", 0.0);
4745
4746 setprop("/sim/rendering/eye-altitude-m", getprop("/position/altitude-ft") * ft_to_m);
4747
4748 # create properties for tile management
4749
4750 setprop(lw~"tiles/tile-counter",0);
4751
4752 # create properties for wind
4753
4754 setprop(lwi~"ipoint-number",0);
4755
4756 var updateMenu = func {
4757 var isEnabled = getprop("/nasal/local_weather/enabled");
4758 gui.menuEnable("local_weather", isEnabled);
4759 if (isEnabled) {setprop("/sim/rendering/clouds3d-enable", "true");}
4760 }
4761
4762 _setlistener("/nasal/local_weather/enabled", updateMenu);
4763
4764