source: trunk/SRC/ToBeReviewed/WIDGET/AUTOUR_de_XXX/scanfile.pro @ 114

Last change on this file since 114 was 114, checked in by smasson, 18 years ago

new compilation options (compile_opt idl2, strictarrsubs) in each routine

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 9.4 KB
Line 
1;
2;  liste des presupposes:
3;       1) le fichier a lire est un fichier netcdf
4;       2) le nom de ce fichier finit
5;       par U.nc, V.nc, W.nc, T.nc ou F.nc, la lettre avant le
6;       nc designant la grille a laquelle se rapporte la champ.
7;       Si tel n''est pas la cas, le fichier est attribue a la grille
8;       T.
9;       3) ce fichier contient une dimension infinie qui doit etre
10;       celle qui se rapporte au temps et au mois 2 autres dimensions
11;       dont les noms sont 'x','lon...','xi_...' et 'y','lat...' ou
12;       'eta_...' ou bien en majuscule.
13;       4) il doit exiter ds ce fichier une unique variable n''ayant
14;       qu''une dimension et etant la dimension temporelle. cette
15;       variable sera prise comme axe des temps. Rq: si plusieurs
16;       variables verifient ces criteres on considere la premiere
17;       variable
18;       5) Cette variable axe des temps doit contenir l''attribut
19;       'units'qui doit etre ecrit suivant la syntaxe:
20;               "seconds since 0001-01-01 00:00:00"
21;               "hours since 0001-01-01 00:00:00"
22;               "days since 1979-01-01 00:59:59"
23;               "months since 1979-01-01 00:59:59"
24;               "years since 1979-01-01 00:59:59"
25;
26; je crois que c''est tout!
27;
28;        GRID='[UTVWF]' to specify the type of grid. Defaut is (1)
29;        based on the name of the file if the file ends by
30;        GRID[._][TUVFW].NC (not case sensible) or (2) T if case (1)
31;        is not found.
32;
33;------------------------------------------------------------
34FUNCTION scanfile, namefile, GRID = GRID, _extra = ex
35;
36  compile_opt idl2, strictarrsubs
37;
38@common
39;------------------------------------------------------------
40  res = -1
41;------------------------------------------------------------
42; filename
43;------------------------------------------------------------
44  fullname = isafile(filename = namefile, IODIRECTORY = iodir, _extra = ex)
45;------------------------------------------------------------
46; open file
47;------------------------------------------------------------
48  cdfid = ncdf_open(fullname)
49;------------------------------------------------------------
50; What contains the file?
51;------------------------------------------------------------
52  infile = ncdf_inquire(cdfid)  ;
53; find vargrid ...
54  IF keyword_set(grid) THEN vargrid = strupcase(grid) ELSE BEGIN
55    vargrid = 'T'               ; default definition
56    IF finite(glamu[0]) EQ 1 THEN BEGIN
57      pattern = ['GRID.', 'GRID_', 'GRID', 'UPID_', '30ID_']
58      gdtype = ['T', 'U', 'V', 'W', 'F']
59      fnametest = strupcase(fullname)
60      FOR i = 0, n_elements(pattern)-1 DO BEGIN
61        FOR j = 0, n_elements(gdtype)-1 DO BEGIN
62          substr = pattern[i]+gdtype[j]
63          pos = strpos(fnametest, substr)
64          IF pos NE -1 THEN $
65             vargrid = strmid(fnametest, pos+strlen(substr)-1, 1)
66        ENDFOR
67      ENDFOR
68    ENDIF
69  ENDELSE
70;------------------------------------------------------------
71; name of all dimensions
72;------------------------------------------------------------
73  namedim = strarr(infile.ndims)
74  for dimiq = 0, infile.ndims-1 do begin
75    ncdf_diminq, cdfid, dimiq, tmpname, value
76    namedim[dimiq] = strlowcase(tmpname)
77  ENDFOR
78; we are looking for a x dimension...
79  dimidx = where(namedim EQ 'x' OR strmid(namedim, 0, 3) EQ 'lon' OR strmid(namedim, 0, 3) EQ 'xi_' OR namedim EQ 'xt_i7_156')
80  dimidx = dimidx[0]
81  if dimidx EQ -1 then begin
82    print, 'one of the dimensions must have the name: ''x'' or ''lon...'' or ''xi_...'' or ''xt_i7_156'''
83    stop
84  endif
85; we are looking for a y dimension...
86  dimidy = where(namedim EQ 'y' OR strmid(namedim, 0, 3) EQ 'lat' OR strmid(namedim, 4) EQ 'eta_' OR namedim EQ 'yt_j6_75')
87  dimidy = dimidy[0]
88  if dimidy EQ -1 then begin
89    print, 'one of the dimensions must have the name: ''y'' or ''lat...'' or ''eta_...'' or ''yt_j6_75'''
90    stop
91  endif
92;------------------------------------------------------------
93; name of all variables
94;------------------------------------------------------------
95; we keep only the variables containing at least x, y and time dimension (if existing...)
96  namevar = strarr(infile.nvars)
97  for varid = 0, infile.nvars-1 do begin
98    invar = ncdf_varinq(cdfid, varid) ; what contains the variable?
99    if (where(invar.dim EQ dimidx))[0] NE -1 AND $
100       (where(invar.dim EQ dimidy))[0] NE -1 AND $
101       ((where(invar.dim EQ infile.recdim))[0] NE -1 OR infile.recdim EQ -1) $
102    THEN namevar[varid] = invar.name
103  ENDFOR
104  namevar = namevar[where(namevar NE '')]
105  listgrid = replicate(vargrid, n_elements(namevar))
106;------------------------------------------------------------
107; time axis
108;------------------------------------------------------------
109  date0fk = date2jul(19000101)
110  IF infile.recdim EQ -1 THEN BEGIN
111    jpt = 1
112    time = date0fk
113    fakecal = 1
114  ENDIF ELSE BEGIN
115    ncdf_diminq, cdfid, infile.recdim, timedimname, jpt
116; we look for the variable containing the time axis
117; we look for the first variable having for only dimension infile.recdim
118    varid = 0
119    repeat BEGIN
120      invar = ncdf_varinq(cdfid, varid)
121      varid = varid+1
122    endrep until n_elements(invar.dim) EQ 1 AND invar.dim[0] EQ infile.recdim
123    varid = varid-1
124;
125    CASE 1 OF
126      varid EQ -1:BEGIN
127        dummy = report('the file '+fullname+' has no time axis.!C we create a fake calendar ...')
128        fakecal = 1
129        time = date0fk + lindgen(jpt)
130      END
131      invar.natts EQ 0:BEGIN
132        dummy = report('the variable '+invar.name+' has no attribut.!C we create a fake calendar ...')
133        fakecal = 1
134        time = date0fk + lindgen(jpt)
135      END
136      ELSE:BEGIN
137;
138; we want to know which attributes are attached to the time variable...
139;
140        attnames = strarr(invar.natts)
141        for attiq = 0, invar.natts-1 do attnames[attiq] = ncdf_attname(cdfid, varid, attiq)
142        if (where(attnames EQ 'units'))[0] EQ -1 then BEGIN
143          dummy = report('Attribut ''units'' not found for the variable '+varid.name+'!C we create a fake calendar ...')
144          fakecal = 1
145          time = date0fk + lindgen(jpt)
146        ENDIF ELSE BEGIN
147; on lit l''axe des temps
148          ncdf_varget, cdfid, varid, time
149          time = double(time)
150          ncdf_attget, cdfid, varid, 'units', value
151; time_counter:units = "seconds since 0001-01-01 00:00:00" ;
152; time_counter:units = "hours since 0001-01-01 00:00:00" ;
153; time_counter:units = "days since 1979-01-01 00:00:00" ;
154; time_counter:units = "months since 1979-01-01 00:00:00" ;
155; time_counter:units = "years since 1979-01-01 00:00:00" ;
156          value = strtrim(strcompress(string(value)), 2)
157          mots = str_sep(value, ' ')
158          unite = mots[0]
159          debut = str_sep(mots[2], '-')
160;
161; now we try to find the attribut called calendar...
162; the the attribute "calendar" exists?
163; If no, we suppose that the calendar is gregorian calendar
164;
165          if (where(attnames EQ 'calendar'))[0] NE -1 then BEGIN
166            ncdf_attget, cdfid, varid, 'calendar', value
167            value = string(value)
168            CASE value OF
169              'noleap':key_caltype = 'noleap'
170              '360d':key_caltype = '360d'
171              'greg':IF n_elements(key_caltype) EQ 0 THEN key_caltype = 'greg'
172              ELSE:BEGIN
173;            notused = report('Unknown calendar: '+value+', we use greg calendar.')
174                key_caltype = 'greg'
175              END
176            ENDCASE
177          ENDIF ELSE BEGIN
178;        notused = report('Unknown calendar, we use '+key_caltype+' calendar.')
179            IF n_elements(key_caltype) EQ 0 THEN key_caltype = 'greg'
180          ENDELSE
181;
182; ATTENTION il faut recuperer l''attribut calendar et ajuster time en
183; consequense...
184;
185;
186; on passe time en jour julien d''idl
187;
188          unite = strlowcase(unite)
189          IF strpos(unite, 's', strlen(unite)-1) NE -1 THEN unite = strmid(unite, 0, strlen(unite)-1)
190          IF strpos(unite, 'julian_') NE -1 THEN unite = strmid(unite, 7)
191          case unite of
192            'second':time = julday(debut[1], debut[2], debut[0])+time/86400.d
193            'hour':time = julday(debut[1], debut[2], debut[0])+time/24.d
194            'day':time = julday(debut[1], debut[2], debut[0])+time
195            'month':BEGIN
196              if total(fix(time) NE time) NE 0 then $ ; we switch to days with 30d/m
197                 time = julday(debut[1], debut[2], debut[0])+round(time*30) $
198              ELSE for t = 0, n_elements(time)-1 DO $
199                 time[t] = julday(debut[1]+time[t], debut[2], debut[0])
200            END
201            'year':BEGIN
202              if total(fix(time) NE time) NE 0 then $ ; we switch to days with 365d/y
203                 time = julday(debut[1], debut[2], debut[0])+round(time*365) $
204              ELSE for t = 0, n_elements(time)-1 do $
205                 time[t] = julday(debut[1], debut[2], debut[0]+time[t])
206            END
207          ENDCASE
208;
209; high frequency calendar: more than one element per day
210          IF max(histogram([long(time-time[0])])) GT 1 THEN fakecal = 1 ELSE fakecal = 0
211          date0fk = date2jul(19000101)
212          IF keyword_set(fakecal) THEN time = date0fk+lindgen(jpt) $
213          ELSE time = long(time)
214;
215        ENDELSE
216      END
217    ENDCASE
218  ENDELSE
219;------------------------------------------------------------
220  ncdf_close, cdfid
221;------------------------------------------------------------
222  return, {filename:fullname, time_counter:time, listvar:namevar $
223           , listgrid:strupcase(listgrid), caltype:key_caltype $
224           , fakecal:date0fk*fakecal}
225end
Note: See TracBrowser for help on using the repository browser.