-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFSTD_PrecipMedian.tcl
More file actions
75 lines (63 loc) · 1.89 KB
/
Copy pathFSTD_PrecipMedian.tcl
File metadata and controls
75 lines (63 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/sh
# the next line restarts using tclsh \
exec $SPI_PATH/tclsh "$0" "$@"
#============================================================================
# Environnement Canada
# Centre Meteorologique Canadien
# 2100 Trans-Canadienne
# Dorval, Quebec
#
# Projet : Exemple de scripts.
# Fichier : FSTD_PrecipMedian.tcl
# Creation : Juin 2010 - J.P. Gauthier - CMC/CMOE
# Description: Calcul de la mediane pour les precips (Faut pas tenir compte des zeros)
#
# Parametres :
#
# Retour:
#
# Remarques :
#
#============================================================================
package require TclData
#package require TclGeoEER
package require Logger
Log::Start [info script] 0.1
fstdfile open 1 read $env(CI_DATA_IN)/RT.fstd
fstdfile open 2 write $env(CI_DATA_OUT)/FSTD_PrecipMedian.fstd
set fldlist {}
Log::Print INFO "Reading fields"
foreach fld [fstdfield find 1 -1 "" -1 -1 -1 "" "RT"] {
fstdfield read FLD$fld 1 $fld
lappend fldlist FLD$fld
}
set n [llength $fldlist]
#----- Sort all the fields gridpoints
Log::Print INFO "Sorting them"
fstdfield sort $fldlist
#----- Make buffer copies for calculations
fstdfield copy ZEROS FLD$fld
vexpr ZEROS ZEROS<<0
fstdfield copy MEDIAN ZEROS
#----- Count number of zeros cause we dont want them to influance the median
Log::Print INFO "Calculating indexes"
foreach field $fldlist {
vexpr ZEROS ifelse($field==0,ZEROS+1,ZEROS)
}
#----- Get the median index in the field list by gridpoint
vexpr IDX ceil(ZEROS+($n-ZEROS)/2)
#----- Extract the median by using the field at IDX in the list
Log::Print INFO "Calculating median"
set i 0
foreach field $fldlist {
vexpr MEDIAN ifelse(IDX==$i,$field,MEDIAN)
incr i
}
#----- Write output median
fstdfield read TIC 1 -1 "" -1 -1 -1 "" ">>"
fstdfield read TAC 1 -1 "" -1 -1 -1 "" "^^"
fstdfield write TIC 2 0 True
fstdfield write TAC 2 0 True
fstdfield write MEDIAN 2 0 Fals
fstdfile close 1 2
Log::End