14. Computations according to intervals

 

13. Text analysis using regular expression

meteorolog.txt stores meteorological data of the year 2023 at certain stations. Based on the yearly precipitation at every station, find the humidity level in the area they are located. (it is humid if yearly precipitation exceeds 80cm, semi-humid if yearly precipitation is between 40 cm and 80cm, semi-arid if yearly precipitation is between 20cm and 40cm, and arid if yearly precipitation is equal to or less than 20cm).

Expected result:

SPL code:

A B
1 0 Arid
2 20 Semi-arid
3 40 Semi-humid
4 80 Humid
5 =create(PRCP, Level).record([A1:B4])
6 =T(“meteorolog.txt”,STATION,DATE,PRCP) =A6.groups(STATION; round(sum(PRCP), 2): TotalPRCP)
7 =B6.derive(A5.segp@r(PRCP, TotalPRCP).Level:HumidLevel)

Enter precipitation interval table data in the first four lines. A5 creates a table using them:

A6 retrieves meteorological data from the given data file. B6 computes yearly precipitation at each observaton station and has the following result:

A7 adds a humidity level field, during which segp()function gets the correponding level from the precipitation interval table by matching TotalPRCP value with PRCP ranges and works with @r option to enable left-open and right-closed intervals. The precipitation interval table is not necessary for handling the interval-based computation. Instead, we can direclty use a sequence. If we modify A7’s code as =B6.derive([B1:B4]([A1:A4].pseg@r(TotalPRCP)):HumidLevel), we can get the same result. The only difference is that pseg() will find the interval number corresponding to the precipitation and then get the humidity level by the result.


15. Displaying data in a dynamic number of column groups
Contents and Exercise Data