Ignore:
Timestamp:
05/11/06 12:35:53 (18 years ago)
Author:
smasson
Message:

debug + new xxx

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ToBeReviewed/WIDGET/AUTOUR_de_XXX/scanfile.pro

    r49 r69  
    99;       3) ce fichier contient une dimension infinie qui doit etre 
    1010;       celle qui se rapporte au temps et au mois 2 autres dimensions 
    11 ;       dont les noms sont 'x','lon','longitude' et 'y','lat' ou 
    12 ;       'latitude' ou bien en majuscule. 
     11;       dont les noms sont 'x','lon...','xi_...' et 'y','lat...' ou 
     12;       'eta_...' ou bien en majuscule. 
    1313;       4) il doit exiter ds ce fichier une unique variable n''ayant 
    1414;       qu''une dimension et etant la dimension temporelle. cette 
     
    2626; je crois que c''est tout! 
    2727; 
    28 ; 
    29 ;------------------------------------------------------------ 
    30 FUNCTION scanfile, nomficher, _extra = ex 
     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 
    3135@common 
    3236;------------------------------------------------------------ 
    33 ; bidouille pour utiliser les mots cles (on passe par des variables 
    34 ; declarees ds un common) 
    35 ;------------------------------------------------------------ 
    3637  res = -1 
    3738;------------------------------------------------------------ 
    38 ; choix du nom du fichier 
    39 ;------------------------------------------------------------ 
    40   nom = isafile(filename = nomficher, IODIRECTORY = iodir, _extra = ex) 
    41 ;------------------------------------------------------------ 
    42 ; ouverture du fichier nom 
    43 ;------------------------------------------------------------ 
    44   cdfid = ncdf_open(nom) 
    45 ;------------------------------------------------------------ 
    46 ; que contient le fichier?? 
    47 ;------------------------------------------------------------ 
    48   contient = ncdf_inquire(cdfid); 
     39; filename 
     40;------------------------------------------------------------ 
     41  fullname = isafile(filename = namefile, IODIRECTORY = iodir, _extra = ex) 
     42;------------------------------------------------------------ 
     43; open file 
     44;------------------------------------------------------------ 
     45  cdfid = ncdf_open(fullname) 
     46;------------------------------------------------------------ 
     47; What contains the file? 
     48;------------------------------------------------------------ 
     49  infile = ncdf_inquire(cdfid)  ; 
    4950; find vargrid ... 
    50   vargrid = 'T'                 ; default definition 
    51   pattern = ['GRID.', 'GRID_', 'GRID', 'UPID_', '30ID_'] 
    52   gdtype = ['T', 'U', 'V', 'W', 'F'] 
    53   fnametest = strupcase(nom) 
    54   FOR i = 0, n_elements(pattern)-1 DO BEGIN 
    55     FOR j = 0, n_elements(gdtype)-1 DO BEGIN 
    56       substr = pattern[i]+gdtype[j] 
    57       pos = strpos(fnametest, substr) 
    58       IF pos NE -1 THEN $ 
    59         vargrid = strmid(fnametest, pos+strlen(substr)-1, 1) 
    60     ENDFOR 
     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 
     67;------------------------------------------------------------ 
     68; name of all dimensions 
     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) 
    6174  ENDFOR 
    62 ;------------------------------------------------------------ 
    63 ; nom des differentes variables 
    64 ;------------------------------------------------------------ 
    65 ; on ne garde les noms de variable uniquement si cette variable 
    66 ; contient au moins les dimensions qui doivent etre appelles x et y 
    67 ; et la dimension infinie 
    68   nomdim   = strarr(contient.ndims) 
    69   for dimiq = 0, contient.ndims-1 do begin 
    70     ncdf_diminq, cdfid, dimiq, name, value ; nom et valeur de la dimension 
    71     nomdim[dimiq] = strlowcase(name) 
    72   ENDFOR 
    73   indexdimx = where(nomdim EQ 'x' OR nomdim EQ 'lon' OR nomdim EQ 'longitude' OR nomdim EQ 'xt_i7_156' OR nomdim EQ 'xi_rho' OR nomdim EQ 'xi_u' OR nomdim EQ 'xi_v' OR nomdim EQ 'xi_psi') 
    74   indexdimx = indexdimx[0] 
    75   if indexdimx EQ -1 then begin 
    76     print, 'one of the dimensions must have the name: ''x'' or ''lon'' or ''longitude'' or ''xt_i7_156''' 
     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''' 
    7780    stop 
    7881  endif 
    79   indexdimy = where(nomdim EQ 'y' OR nomdim EQ 'lat' OR nomdim EQ 'latitude' OR nomdim EQ 'yt_j6_75' OR nomdim EQ 'eta_rho' OR nomdim EQ 'eta_u' OR nomdim EQ 'eta_v' OR nomdim EQ 'eta_psi') 
    80   indexdimy = indexdimy[0] 
    81   if indexdimy EQ -1 then begin 
    82     print, 'one of the dimensions must have the name: ''y'' or ''lat'' or ''latitude'' or ''yt_j6_75''' 
     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''' 
    8387    stop 
    8488  endif 
    85 ; 
    86   namevar = strarr(contient.nvars) 
    87   for varid = 0, contient.nvars-1 do begin 
    88     varcontient = ncdf_varinq(cdfid, varid) ; que contient la variable 
    89     if (where(varcontient.dim EQ indexdimx))[0] NE -1 AND $ 
    90       (where(varcontient.dim EQ indexdimy))[0] NE -1 AND $ 
    91       (where(varcontient.dim EQ contient.recdim))[0] NE -1 THEN namevar[varid] = varcontient.name  
     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  
    92100  ENDFOR 
    93101  namevar = namevar[where(namevar NE '')] 
    94   listgrid = replicate(vargrid,  n_elements(namevar)) 
    95 ;------------------------------------------------------------ 
    96 ; on recupere l''axe des temps 
    97 ;------------------------------------------------------------ 
    98 ; on recupere le nom de la variable contenant l''axe des temps 
    99   varid = 0 
    100   repeat BEGIN ; tant que l''on a pas trouve une variable qui n''a qu'' 
    101                                 ; une dimension: la dimension infinie 
    102     varcontient = ncdf_varinq(cdfid, varid) ; que contient la variable 
    103     varid = varid+1 
    104   endrep until n_elements(varcontient.dim) EQ 1 AND varcontient.dim[0] EQ contient.recdim ; 
    105   varid = varid-1 
    106 ; 
    107 ; we want to know which attrributes are attached to the time variable... 
    108 ; 
    109   if varcontient.natts EQ 0 then BEGIN 
    110     ncdf_close, cdfid 
    111     return, report('the variable '+varcontient.name+' has no attribut.!C we need attribut ''units'' for the calendar ...') 
    112   endif 
    113   attnames = strarr(varcontient.natts) 
    114   for attiq = 0, varcontient.natts-1 do attnames[attiq] = ncdf_attname(cdfid, varid, attiq) 
    115   if (where(attnames EQ 'units'))[0] EQ -1 then BEGIN 
    116     ncdf_close, cdfid 
    117     return, report('Attribut ''units'' not found for the variable '+varid.name+'we need it for the calendar...') 
    118   endif 
     102  listgrid = replicate(vargrid, n_elements(namevar)) 
     103;------------------------------------------------------------ 
     104; time axis 
     105;------------------------------------------------------------ 
     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 
     121; 
     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 
     134; 
     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  
    119144; on lit l''axe des temps 
    120   ncdf_varget, cdfid, varid, time 
    121   time = double(time) 
    122   ncdf_attget, cdfid, varid, 'units', value 
     145          ncdf_varget, cdfid, varid, time 
     146          time = double(time) 
     147          ncdf_attget, cdfid, varid, 'units', value 
    123148; time_counter:units = "seconds since 0001-01-01 00:00:00" ; 
    124149; time_counter:units = "hours since 0001-01-01 00:00:00" ; 
     
    126151; time_counter:units = "months since 1979-01-01 00:00:00" ; 
    127152; time_counter:units = "years since 1979-01-01 00:00:00" ; 
    128   value = strtrim(strcompress(string(value)), 2) 
    129   mots = str_sep(value, ' ') 
    130   unite = mots[0] 
    131   debut = str_sep(mots[2], '-') 
     153          value = strtrim(strcompress(string(value)), 2) 
     154          mots = str_sep(value, ' ') 
     155          unite = mots[0] 
     156          debut = str_sep(mots[2], '-') 
    132157; 
    133158; now we try to find the attribut called calendar... 
     
    135160; If no, we suppose that the calendar is gregorian calendar 
    136161; 
    137   if (where(attnames EQ 'calendar'))[0] NE -1 then BEGIN 
    138       ncdf_attget, cdfid, varid, 'calendar', value 
    139       value = string(value) 
    140         CASE value OF 
    141           '360d':key_caltype = '360d' 
    142           'greg':IF n_elements(key_caltype) EQ 0 THEN key_caltype = 'greg' 
    143           ELSE:BEGIN 
     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 
    144170;            notused = report('Unknown calendar: '+value+', we use greg calendar.')  
    145             key_caltype = 'greg' 
    146           END 
    147         ENDCASE 
    148       ENDIF ELSE BEGIN 
     171                key_caltype = 'greg' 
     172              END 
     173            ENDCASE 
     174          ENDIF ELSE BEGIN 
    149175;        notused = report('Unknown calendar, we use '+key_caltype+' calendar.')  
    150         IF n_elements(key_caltype) EQ 0 THEN key_caltype = 'greg' 
    151       ENDELSE 
     176            IF n_elements(key_caltype) EQ 0 THEN key_caltype = 'greg' 
     177          ENDELSE 
    152178; 
    153179; ATTENTION il faut recuperer l''attribut calendar et ajuster time en 
     
    156182; 
    157183; on passe time en jour julien d''idl  
    158 ; on suppose qu''on utilise le vrai calendrier. 
    159 ; 
    160   unite = strlowcase(unite) 
    161   IF strpos(unite, 's', strlen(unite)-1) NE -1 THEN unite = strmid(unite, 0, strlen(unite)-1) 
    162   IF strpos(unite, 'julian_') NE -1 THEN unite = strmid(unite, 7) 
    163   case unite of 
    164     'second':time = julday(debut[1], debut[2], debut[0])+time/(long(24)*3600) 
    165     'hour':time = julday(debut[1], debut[2], debut[0])+time/(long(24)) 
    166     'day':time = julday(debut[1], debut[2], debut[0])+time 
    167     'month':BEGIN  
    168       if total(fix(time) NE time) NE 0 then $ ; we switch to days with 30d/m 
    169         time = julday(debut[1], debut[2], debut[0])+round(time*30) $ 
    170       ELSE for t = 0, n_elements(time)-1 DO $ 
    171         time[t] = julday(debut[1]+time[t], debut[2], debut[0]) 
    172     END 
    173     'year':BEGIN 
    174       if total(fix(time) NE time) NE 0 then $ ; we switch to days with 365d/y 
    175         time = julday(debut[1], debut[2], debut[0])+round(time*365) $ 
    176       ELSE for t = 0, n_elements(time)-1 do $ 
    177         time[t] = julday(debut[1], debut[2], debut[0]+time[t]) 
    178     END 
    179   ENDCASE 
    180   time = long(time) 
    181 ;------------------------------------------------------------ 
    182   return, {filename:nom, time_counter:time, listvar:namevar, listgrid:strupcase(listgrid), calendartype:key_caltype} 
     184; 
     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 
     207          IF max(histogram([long(time)])) GT 1 THEN fakecal = 1 ELSE fakecal = 0 
     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 
     216;------------------------------------------------------------ 
     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} 
    183222end 
Note: See TracChangeset for help on using the changeset viewer.