source: trunk/tools/idl_netcdf/ncdf_extrait.pro @ 161

Last change on this file since 161 was 161, checked in by pinsard, 15 years ago

conversion from IDL help header syntax to IDLDOC 2. header syntax

File size: 13.2 KB
Line 
1;+
2;
3; @categories
4;
5; @examples
6; ncdf_extrait,nomfich1,nomfich2 [,domaine]
7;
8; @param NOMFICH1 {in}{required}
9; le nom du fichier d'entree
10;
11; @param NOMFICH2 {in}{required}
12; le nom du fichier de sortie
13;
14; @param DOMAINE {in}{optional}
15; un vecteur de chaines de chatacteres qui permet de
16;        definir les dimensions a extraire. sa forme doit etre la
17;        suivante:
18;        ['nomdim1,indiceinf1,indicesup1','nomdim2,indiceinf2,indicesup2',...]
19;        rq: seules les dimensions a reduire doivent etre specifiees
20;        les dimensions ne figurant pas ds domaine seront selectionnees ds leur integralite.
21;
22; @param NOMFICH2 {out}
23;
24; @keyword GARDE
25; vecteur donnant les noms des variables a
26;        selectionner. si il n'est pas active toutes les variables
27;        sont selectionnees.
28;
29; @keyword EXCLU
30; vecteur donnant les noms des variablers a ne pas
31;        selectionner. si il n'est pas active toutes les variables
32;        sont selectionnees.
33;
34; @uses
35; common.pro
36;
37; @restrictions
38;              ce programme ne marche que si les tableaux sont de
39; dimension inferieure ou egale a 4.
40;              ATTENTION: AVANT DE CREER LE FICHIER NET-CDF,
41; S'ASSURER QU'IL Y A DE LA PLACE DS LE REPERTOIRE POUR LE FAIRE SINON
42; LE PROGRAMME PLANTE LORS DE L'ECRITURE DU FICHIER ET DONNE UNE
43; INDICATION BIDON...
44;
45; @examples
46;
47; @history
48; Sebastien Masson (smasson\@lodyc.jussieu.fr)
49;                       20/11/98
50; 13/1/98 : attribut concernant le min et le max
51; 18/1/98 : bug qd ont fait disparaitre une dimension (sa taille
52; devenant =1)
53;-
54PRO ncdf_extrait, nomfich1, nomfich2, domaine, GARDE = garde, EXCLU = exclu
55;------------------------------------------------------------
56; contenu de nomfich1
57;------------------------------------------------------------
58   cdfid1 = ncdf_open(nomfich1)
59   contient1 = ncdf_inquire(cdfid1)
60;------------------------------------------------------------
61; decripter domaine s'il y en a un
62;------------------------------------------------------------
63   case n_params() of
64      2:domaine = ['rien, 0,0']
65      3:
66      ELSE:
67   endcase
68   taille = n_elements(domaine)
69   nomdim = strarr(taille[0])
70   indiceinf = lonarr(taille[0])
71   indicesup = lonarr(taille[0])
72   for i = 0, taille[0]-1 do begin
73      separe = str_sep(domaine[i], ',')
74      nomdim[i] = strlowcase(strtrim(separe[0], 2))
75      indiceinf[i] = long(separe[1])
76      indicesup[i] = long(separe[2])
77   endfor
78;------------------------------------------------------------
79; mise en place du resultat
80;------------------------------------------------------------
81   cdfid2 = ncdf_create('travaille.nc', /clobber)
82;------------------------------------------------------------
83; dimensions qui vont rester
84;------------------------------------------------------------
85   nomdim1 = strarr(contient1.ndims)
86   tailledim1 = lonarr(contient1.ndims)
87   tailledim2 = lonarr(contient1.ndims)
88   indiceinf2 = lonarr(contient1.ndims)
89   indicesup2 = lonarr(contient1.ndims)
90   gardedim = lonarr(contient1.ndims)
91   for dimid = 0,contient1.ndims-1 do begin
92      ncdf_diminq, cdfid1, dimid, name, value
93      nomdim1[dimid] = name
94      tailledim1[dimid] = value
95      pos = where(nomdim EQ strlowcase(name),  count)
96      if count NE 0 then BEGIN  ; si c'est une dim a reduire
97         tailledim2[dimid] = indicesup[pos]-indiceinf[pos]+1
98         indicesup2[dimid] = indicesup[pos]
99         indiceinf2[dimid] = indiceinf[pos]
100                                ; dim degeneree ?
101         IF tailledim2[dimid] EQ 1 THEN gardedim[dimid] = 0 ELSE gardedim[dimid] = 1
102      ENDIF ELSE BEGIN
103         tailledim2[dimid] = tailledim1[dimid]
104         indicesup2[dimid] = tailledim1[dimid]-1
105         indiceinf2[dimid] = 0
106         gardedim[dimid] = 1
107      ENDELSE
108   endfor
109   reduit = where(gardedim EQ 1, nbredim2)
110   if reduit[0] NE -1  then begin
111      tailledim2 = tailledim2(reduit)
112      nomdim2 = nomdim1(reduit)
113;------------------------------------------------------------
114; definition des dimensions du nouveau fichier
115;------------------------------------------------------------
116      dimid2 = lonarr(nbredim2)
117      for i = 0,nbredim2-1 do begin
118         if nomdim2[i] NE nomdim1[contient1.recdim] then $
119          dimid2[i] = ncdf_dimdef(cdfid2, nomdim2[i], tailledim2[i]) $
120         ELSE dimid2[i] = ncdf_dimdef(cdfid2, nomdim2[i], /unlimited)
121      endfor
122   endif
123;
124; on recupere l''id, pour le fichier de sortie, des dimensions a garder.
125; pour cela on construit indice qui est la liste des dimensions a
126; garder et leurs indices ds nomdim2.
127;
128   if contient1.recdim NE -1 then $ ; si la dim infinie existe ds le fichier d''entree
129    if gardedim[contient1.recdim] NE 0 then begin ; si la dim infinie existe ds le fichier de sortie
130      case 1 of
131         nomdim2[0] EQ nomdim1[contient1.recdim]:renverser = 1
132         nomdim2[n_elements(nomdim2)-1] EQ nomdim1[contient1.recdim]:renverser = 0
133         ELSE: BEGIN
134            print, 'la dimension infinie doit etre la premiere ou la derniere des dimensions du tableau'
135            stop
136         end
137      endcase
138
139   endif
140;------------------------------------------------------------
141; definitions des attributs globeaux du nouveau fichier
142;------------------------------------------------------------
143   for attiq = 0, contient1.ngatts-1 do begin
144      name=ncdf_attname(cdfid1,attiq,/global)
145      rien = ncdf_attcopy(cdfid1,name,cdfid2,/in_global, /out_global)
146   endfor
147;------------------------------------------------------------
148; definition des variables
149;------------------------------------------------------------
150   varid2 = lonarr(contient1.nvars)
151   for i = 0,contient1.nvars-1 do begin
152      varcontient = ncdf_varinq(cdfid1, i)
153      case 1 of
154         keyword_set(GARDE):rien = where(strlowcase(garde) EQ strlowcase(varcontient.name), test)
155         keyword_set(EXCLU):BEGIN
156            rien = where(strlowcase(exclu) EQ strlowcase(varcontient.name), test)
157            test = 1-test
158         END
159         ELSE :test = 1
160      endcase
161      if test EQ 1 then begin
162         
163
164         IF total(gardedim(varcontient.dim)) NE 0 THEN BEGIN
165                                ; la variable extraite n'est pas un scalaire
166;
167; attention au cas ou l''on a des dim qui on disparuent, il faut
168; verifier si elles sont presentes ds varcontient.dim et les enlever
169; au besoin.
170;
171            varcontientvraidim = varcontient.dim[where(gardedim[varcontient.dim] eq 1)]
172
173            indice = -1
174            for ii = 0, n_elements(nomdim2)-1 do begin
175               if total(nomdim2[ii] EQ nomdim1[varcontientvraidim])  NE 0 then $
176                indice = [indice, ii]
177            ENDFOR
178
179            indice = indice[1:n_elements(indice)-1 ]
180            if renverser EQ 1 then indice = reverse(indice)
181
182            commande = 'varid2[i] = ncdf_vardef(cdfid2, varcontient.name, dimid2(indice),'+varcontient.datatype+'=1)'
183            rien = execute(commande)
184         ENDIF ELSE BEGIN
185            commande = 'varid2[i] = ncdf_vardef(cdfid2, varcontient.name,'+varcontient.datatype+'=1)'
186            rien = execute(commande)
187         ENDELSE
188;------------------------------------------------------------
189; atributs des variables
190;------------------------------------------------------------
191         FOR attid = 0,varcontient.natts-1 do begin
192            name = ncdf_attname(cdfid1,i , attid)
193            case 1 of
194               strpos(name, 'max') ne -1:BEGIN
195                  ncdf_attput,cdfid2,varcontient.name, 'valid_max', '-0.0000000e+00f'
196                  maxexiste = 1
197               END
198               strpos(name, 'min') ne -1:BEGIN
199                  ncdf_attput,cdfid2,varcontient.name, 'valid_min', '-0.0000000e+00f'
200                  minexiste = 1
201               END
202               ELSE:bidon = ncdf_attcopy(cdfid1,varcontient.name , name, cdfid2,varcontient.name)
203            endcase
204            if n_elements(maxexiste) EQ 0 then ncdf_attput,cdfid2,varcontient.name, 'valid_max', '-0.0000000e+00f'
205            if n_elements(minexiste) EQ 0 then ncdf_attput,cdfid2,varcontient.name, 'valid_min', '-0.0000000e+00f'
206         endfor
207      endif
208   endfor
209;------------------------------------------------------------
210; fin de la definition du fichier de sortie.
211;------------------------------------------------------------
212   ncdf_control,  cdfid2, /endef
213;------------------------------------------------------------
214; debut du remplissage des variables
215;------------------------------------------------------------
216   for i = 0,contient1.nvars-1 do begin
217      varcontient = ncdf_varinq(cdfid1, i)
218      case 1 of
219         keyword_set(GARDE):rien = where(strlowcase(garde) EQ strlowcase(varcontient.name), test)
220         keyword_set(EXCLU):BEGIN
221            rien = where(strlowcase(exclu) EQ strlowcase(varcontient.name), test)
222            test = 1-test
223         END
224         ELSE :test = 1
225      endcase
226      if test EQ 1 then begin
227         print, varcontient.name
228;         case n_elements(varcontient.dim[where(gardedim[varcontient.dim] eq 1)]) of
229         case varcontient.ndims of
230            0:BEGIN
231               ncdf_varget, cdfid1,  varcontient.name, tab
232               ncdf_varput, cdfid2,  varcontient.name,tab
233            end
234            1:BEGIN
235               count = [indicesup2(varcontient.dim[0])-indiceinf2(varcontient.dim[0])+1]
236               ncdf_varget, cdfid1, i, tab, $
237                offset = [indiceinf2(varcontient.dim[0])], $
238                count = count
239               ncdf_varput, cdfid2,  varcontient.name,tab
240            end
241            2:BEGIN
242               count =  [indicesup2(varcontient.dim[0])-indiceinf2(varcontient.dim[0])+1 $
243                         ,indicesup2(varcontient.dim[1])-indiceinf2(varcontient.dim[1])+1 ]
244               ncdf_varget, cdfid1, i, tab, $
245                offset = [indiceinf2(varcontient.dim[0]),indiceinf2(varcontient.dim[1])], $
246                count = count
247               test = where(count[0] EQ 1)
248               IF test[0] NE -1 THEN tab = reform(tab)
249               ncdf_varput, cdfid2,  varcontient.name,tab
250            end
251            3:BEGIN
252               count = [indicesup2(varcontient.dim[0])-indiceinf2(varcontient.dim[0])+1 $
253                        ,indicesup2(varcontient.dim[1])-indiceinf2(varcontient.dim[1])+1 $
254                        ,indicesup2(varcontient.dim[2])-indiceinf2(varcontient.dim[2])+1 ]
255               ncdf_varget, cdfid1, i, tab, $
256                offset = [indiceinf2(varcontient.dim[0]),indiceinf2(varcontient.dim[1]) $
257                          ,indiceinf2(varcontient.dim[2])], $
258                count = count
259               test = where(count[0:1] EQ 1)
260               IF test[0] NE -1 THEN tab = reform(tab)
261               ncdf_varput, cdfid2, varcontient.name,tab
262            end
263            4:BEGIN
264               count = [indicesup2(varcontient.dim[0])-indiceinf2(varcontient.dim[0])+1 $
265                        ,indicesup2(varcontient.dim[1])-indiceinf2(varcontient.dim[1])+1 $
266                        ,indicesup2(varcontient.dim[2])-indiceinf2(varcontient.dim[2])+1 $
267                        ,indicesup2(varcontient.dim[3])-indiceinf2(varcontient.dim[3])+1 ]
268               ncdf_varget, cdfid1, i, tab, $
269                offset = [indiceinf2(varcontient.dim[0]),indiceinf2(varcontient.dim[1]) $
270                          ,indiceinf2(varcontient.dim[2]),indiceinf2(varcontient.dim[3])], $
271                count = count
272               test = where(count[0:2] EQ 1)
273               IF test[0] NE -1 THEN tab = reform(tab)
274               ncdf_varput, cdfid2, varcontient.name,tab
275            end
276            ELSE:
277         endcase
278;------------------------------------------------------------
279; mise a jour du min et du max de la variable
280;------------------------------------------------------------
281; recherche de la missing value de l'argument max et min ...
282         FOR attid = 0,varcontient.natts-1 do begin
283            name = ncdf_attname(cdfid1,i , attid)
284            if strpos(strlowcase(name), 'missing') ne -1 then BEGIN
285               ncdf_attget, cdfid1,i ,name , value ; la missing value existe ??
286               missing = float(string(value))
287            endif
288         endfor
289         if n_elements(missing) NE 0 then BEGIN ; test different suivant la valeure
290            if abs(missing) LT 1e6 then BEGIN ; de missing  pour eviter les erreures
291               nomask = where(tab NE missing) ; d''arondis...
292               max = max(tab[nomask], min = min)
293            ENDIF ELSE BEGIN
294               nomask = where(abs(tab) LT abs(missing)/10.)
295               max = max(tab[nomask], min = min)
296            ENDELSE
297            nomask = 1
298         ENDIF ELSE max = max(tab, min = min)
299         ncdf_attput,cdfid2,varcontient.name, 'valid_max', strtrim(max, 2)+'f'
300         ncdf_attput,cdfid2,varcontient.name, 'valid_min', strtrim(min, 2)+'f'
301         tempvar = SIZE(TEMPORARY(min)) ; pour effacer le min
302         tempvar = SIZE(TEMPORARY(max)) ; pour effacer le max
303         tab = 1
304      endif
305   endfor
306;------------------------------------------------------------
307; fermeture des fichiers
308;------------------------------------------------------------
309   ncdf_close, cdfid1
310   ncdf_close, cdfid2
311;------------------------------------------------------------
312; changement de nom du fichier de resultat
313;------------------------------------------------------------
314   commande = 'mv travaille.nc '+nomfich2
315   spawn, commande
316
317;------------------------------------------------------------
318   return
319end
Note: See TracBrowser for help on using the repository browser.