Board index FlightGear Development Weather

Create global known vector from input data  Topic is solved

Everything related to weather simulation, visuals should be discussed in the shader subforum.

Create global known vector from input data

Postby gierschi@flightgear » Wed Aug 24, 2016 8:40 am

Dear FG community,

I need again your help. I've tried to implement wind data from a .txt file into flightgear. Of course, the reading process of the file shall be done only once, because the files a relatively large and reading the file each time step would make FG very slow. My current script in local_weather.nas look like this:

Code: Select all
...
        else if (wind_model_flag == 6) # PALM 3DWind
        {
        var input_PAlM_data_flag = getprop("local-weather/config/input-PALM-data-flag");   
        if (input_PAlM_data_flag == 0){
                # Input filenames as 1D vectors
                var u_input  = split("\n", io.readfile("/localdata/giersch/FG-3.6-v1/fgdata/PALMInput/Assist_gauss_6_3d_t_end/u.txt"));
                var v_input  = split("\n", io.readfile("/localdata/giersch/FG-3.6-v1/fgdata/PALMInput/Assist_gauss_6_3d_t_end/v.txt"));
                var w_input  = split("\n", io.readfile("/localdata/giersch/FG-3.6-v1/fgdata/PALMInput/Assist_gauss_6_3d_t_end/w.txt"));
                var x_input  = split("\n", io.readfile("/localdata/giersch/FG-3.6-v1/fgdata/PALMInput/Assist_gauss_6_3d_t_end/x.txt"));
                var y_input  = split("\n", io.readfile("/localdata/giersch/FG-3.6-v1/fgdata/PALMInput/Assist_gauss_6_3d_t_end/y.txt"));
                var zw_input = split("\n", io.readfile("/localdata/giersch/FG-3.6-v1/fgdata/PALMInput/Assist_gauss_6_3d_t_end/zw_3d.txt"));

                # Calculate dimensions and grid spacing (last three rows in input file are empty, only valid for constant grid spacing)
                var dimx   = size(x_input) - 3;
                var delta_x = x_input[1] - x_input[0];

                var dimy = size(y_input) - 3;
                var delta_y = y_input[1] - y_input[0];

                var dimz = size(zw_input) - 3;
                var delta_z = zw_input[1] - zw_input[0];

                # Determine model size
                var xsize = (dimx-1)*delta_x;
                var ysize = (dimy-1)*delta_y;
                var zsize = (dimz-1)*delta_z;

                # Inertial airplane position in PALM wind field [m] (center of the model domain)
                var xp_old = xsize / 2.0;
                var yp_old = ysize / 2.0;
                var zp_old = zsize / 2.0;

                # Create 3D array appropriate to the input data
                var n = 0;

                var u_palm = [];
                var v_palm = [];
                var w_palm = [];

                for (var k = 0; k < dimz; k += 1){

                    var dummy_2d = [];

                    for (var j = 0; j < dimy; j += 1){
       
                        var dummy_1d = [];
       
                        for (var i = 0; i < dimx; i += 1){
                     
                            var pointer = u_input[n];
                            n += 1;
                            append(dummy_1d, pointer); 
                        };
 
                    append(dummy_2d,dummy_1d); 
                    };
   
                append(u_palm, dummy_2d); 
                };
                setprop("local-weather/config/input-PALM-data-flag",1);

                #only exemplary.
                var winddir   = 45;   
                var windspeed = u_palm[129][256][256]; 

                }
        else
        #only exemplary.       
        var winddir   = 45;
        var windspeed = u_palm[129][256][256] - 2 * u_palm[128][255][255];       # this is the line which makes problems. Fictitious calculation 
       }


Thus, the data is read only once until the input_PALM_data_flag is set to 1. After the reading process, FG shall only compute the new wind speed (depending on the aircraft position) each time the local-weather script runs (on the basis of the 3D field u_palm). The problem what appears then is the error message.

Nasal runtime error: undefined symbol: u_palm


The reason for it seems to be that FG don't know u_palm as soon as the input_PALM_data_flag is set to 1. How can I change this that the script runs?

Thank you very much in advance.
gierschi@flightgear
 
Posts: 58
Joined: Tue Nov 03, 2015 10:40 pm

Re: Create global known vector from input data

Postby Thorsten » Wed Aug 24, 2016 10:42 am

Make it a global rather than a local variable (i.e. declare it outside the function).
Thorsten
 
Posts: 12490
Joined: Mon Nov 02, 2009 9:33 am

Re: Create global known vector from input data

Postby gierschi@flightgear » Wed Aug 24, 2016 1:07 pm

Declaring u_palm outside the else if instruction would mean that after "local-weather/config/input-PALM-data-flag" is set to 1 the vector u_palm would create each time the script local-weather.nas is called and all written wind data to this vector will be lost.

But i found another solution. It seems to be possible to declare u_palm in globals.nas and as soon as you have written data to the 3D vector you can call this data from every nasal script you want
gierschi@flightgear
 
Posts: 58
Joined: Tue Nov 03, 2015 10:40 pm

Re: Create global known vector from input data  

Postby Thorsten » Wed Aug 24, 2016 2:05 pm

Declaring u_palm outside the else if instruction would mean that after "local-weather/config/input-PALM-data-flag" is set to 1 the vector u_palm would create each time the script local-weather.nas is called and all written wind data to this vector will be lost.


Declaring a variable isn't the same as writing to it. It is definitely possible to declare a global vector, fill it once and use it later when needed - I'm doing this in dozens of places both in AW and in the Shuttle software.

But i found another solution. It seems to be possible to declare u_palm in globals.nas and as soon as you have written data to the 3D vector you can call this data from every nasal script you want


That's most definitely not design that's encouraged - what you do on your own harddisk is your business of course.

Declare the variables in the scope you need, if you need them elsewhere, refer to them by namespace.
Thorsten
 
Posts: 12490
Joined: Mon Nov 02, 2009 9:33 am

Re: Create global known vector from input data

Postby Hooray » Wed Aug 24, 2016 5:52 pm

just don't make it part of the global(s) namespace, but use a dedicated namespace (e.g. AW specific).
If in doubt, please refer to the wiki for the docs on Nasal namespaces - primarily, you need to understand how hashes work, and how submodules are loaded into such hashes to form namespaces
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: Create global known vector from input data

Postby gierschi@flightgear » Tue Aug 30, 2016 7:24 am

Okay thank you very much.
gierschi@flightgear
 
Posts: 58
Joined: Tue Nov 03, 2015 10:40 pm

Re: Create global known vector from input data

Postby gierschi@flightgear » Tue Aug 30, 2016 7:28 am

I'm doing this in dozens of places both in AW and in the Shuttle software.


Can you give an example, comparable to my situation?
gierschi@flightgear
 
Posts: 58
Joined: Tue Nov 03, 2015 10:40 pm

Re: Create global known vector from input data

Postby gierschi@flightgear » Tue Aug 30, 2016 7:49 am

Ah i did not recognize that everything of my code was written into the function interpolation loop. sorry for that ;-)
gierschi@flightgear
 
Posts: 58
Joined: Tue Nov 03, 2015 10:40 pm


Return to Weather

Who is online

Users browsing this forum: No registered users and 3 guests