source: trunk/procs/macros/make_ratio.pro

Last change on this file was 205, checked in by pinsard, 14 years ago

homegenize THEN BEGIN ... ENDIF

  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1;+
2;
3; compute ratio between 2 or 4 variables [ a/b or (a-b)/(c-d) ]
4;
5; @param FILE_NAME {in}{required}{type=string}
6;
7; @param NCDF_DB {in}{required}{type=string}
8; <location>:<path> or just <path>
9;
10; @keyword BOXZOOM
11;
12; @keyword TIME_1
13;
14; @keyword TIME_2
15;
16; @keyword ALL_DATA
17;
18; @keyword ZMTYP
19;
20; @returns
21; structure
22; -1 in case of error
23;
24; @uses
25; <pro>common</pro>
26; <propost_it>com_eg</propost_it>
27;
28; @history
29; - fplod 20100119T094252Z aedon.locean-ipsl.upmc.fr (Darwin)
30;
31;   * check parameters
32;
33; - fplod 20091208T102329Z aedon.locean-ipsl.upmc.fr (Darwin)
34;
35;   * syntax of array
36;
37; @version
38; $Id$
39;
40;-
41FUNCTION make_ratio, file_name, ncdf_db $
42         , BOXZOOM=boxzoom $
43         , TIME_1=time_1 $
44         , TIME_2=time_2 $
45         , ALL_DATA=all_data $
46         , ZMTYP=zmtyp
47;
48  compile_opt idl2, strictarrsubs
49;
50@common
51@com_eg
52;
53 usage='result=make_ratio(file_name, ncdf_db ' $
54         + ', BOXZOOM=boxzoom ' $
55         + ', TIME_1=time_1 ' $
56         + ', TIME_2=time_2 ' $
57         + ', ALL_DATA=all_data ' $
58         + ', ZMTYP=zmtyp)'
59
60 nparam = N_PARAMS()
61 IF (nparam LT 2) THEN BEGIN
62    ras = report(['Incorrect number of arguments.' $
63          + '!C' $
64          + 'Usage : ' + usage])
65    return, -1
66 ENDIF
67
68 arg_type = size(file_name,/type)
69 IF (arg_type NE 7) THEN BEGIN
70   ras = report(['Incorrect arg type file_name' $
71          + '!C' $
72          + 'Usage : ' + usage])
73   return, -1
74 ENDIF
75
76 arg_type = size(ncdf_db,/type)
77 IF (arg_type NE 7) THEN BEGIN
78   ras = report(['Incorrect arg type ncdf_db' $
79          + '!C' $
80          + 'Usage : ' + usage])
81   return, -1
82 ENDIF
83
84; Extracts the variables between the commas.
85; Pay attention : the order is important (a,b,c,d)
86
87  div = 0.0
88
89  tab_var = strsplit(macro_base_fld, ',', /EXTRACT)
90  IF ( debug_w ) THEN BEGIN
91   print, 'size(tab_var) : ', size(tab_var)
92  ENDIF
93
94  IF ( (size(tab_var))[1] NE 2 AND (size(tab_var))[1] NE 4 ) THEN BEGIN
95    print, 'Number of variables in the macro should be 2 or 4'
96  ENDIF
97
98  IF ( (size(tab_var))[1] EQ 4 ) THEN BEGIN
99    a = strtrim(tab_var[0], 2)
100    b = strtrim(tab_var[1], 2)
101    c = strtrim(tab_var[2], 2)
102    d = strtrim(tab_var[3], 2)
103
104    print, 'a : ', a
105    print, 'b : ', b
106    print, 'c : ', c
107    print, 'd : ', d
108
109    var_a = nc_read(file_name, a, ncdf_db, BOXZOOM = boxzoom, TIME_1 = time_1, TIME_2 = time_2, ALL_DATA = all_data)
110    var_b = nc_read(file_name, b, ncdf_db, BOXZOOM = boxzoom, TIME_1 = time_1, TIME_2 = time_2, ALL_DATA = all_data)
111    var_c = nc_read(file_name, c, ncdf_db, BOXZOOM = boxzoom, TIME_1 = time_1, TIME_2 = time_2, ALL_DATA = all_data)
112    var_d = nc_read(file_name, d, ncdf_db, BOXZOOM = boxzoom, TIME_1 = time_1, TIME_2 = time_2, ALL_DATA = all_data)
113
114    div = var_a.data
115    ;;div[*, *] = valmask
116    div[*, *] = -1000.0
117    diff = var_c.data - var_d.data
118
119    idx = WHERE( diff NE 0.0 )
120    IF( idx[0] EQ -1 ) THEN BEGIN
121     STOP, 'STOP. Division cannot be computed'
122    ENDIF
123
124    div[idx] = ( var_a.data[idx] - var_b.data[idx] )/( diff[idx] )
125    legend = ' : ( '+a+' - '+b+' ) / ( '+c+' - '+d+' )'
126
127   ENDIF
128
129   IF ( (size(tab_var))[1] EQ 2 ) THEN BEGIN
130
131    a = strtrim(tab_var[0], 2)
132    b = strtrim(tab_var[1], 2)
133
134    print, 'a : ', a
135    print, 'b : ', b
136
137    var_a = nc_read(file_name, a, ncdf_db, TIME_1 = time_1, TIME_2 = time_2)
138    var_b = nc_read(file_name, b, ncdf_db, TIME_1 = time_1, TIME_2 = time_2)
139
140    div = var_a.data
141    div[*, *] = valmask
142
143    idx = WHERE( var_b.data NE 0.0 )
144    IF( idx[0] EQ -1 ) THEN BEGIN
145     STOP, 'STOP. Division cannot be computed'
146    ENDIF
147
148    div[idx] = ( var_a.data[idx] )/( var_b.data[idx]  )
149    legend = ' : '+a+' / '+b
150
151   ENDIF
152
153   varname = 'ratio'
154   varunit = ''
155
156   field = {name: '', data: div, legend: '', units: '', origin: '', dim: 0, direc:''}
157   field.origin = var_a.origin
158   field.dim = var_a.dim
159   field.legend = legend
160   field.direc = var_a.direc
161
162   return, field
163
164END
Note: See TracBrowser for help on using the repository browser.