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

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

debug xxx and cie + clean data file + rm perldoc_idl

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