Index: /BOL/trunk/IPCC/ts2IPCC.F90
===================================================================
--- /BOL/trunk/IPCC/ts2IPCC.F90	(revision 554)
+++ /BOL/trunk/IPCC/ts2IPCC.F90	(revision 555)
@@ -1,3 +1,5 @@
-
+!
+! $Header$
+!
 PROGRAM ts2IPCC
 
@@ -18,5 +20,6 @@
 
   use cmor_users_functions
-  
+  use netcdf
+
   implicit none
 
@@ -32,19 +35,21 @@
   CHARACTER (len=20), DIMENSION(100) :: ipsl_name, ipsl_units
   CHARACTER (len=20), DIMENSION(100) :: ipcc_name, ipcc_table
-  CHARACTER (len=80)         :: varname, units
+  CHARACTER (len=80)         :: varname, units, namedim
 
   INTEGER                    :: orig_file_id, nvars, ndims
   INTEGER                    :: verbos, exit_ctl, realis, indice,index_table
   INTEGER                    :: iargc, iostat, ierr
-  INTEGER                    :: i
+  INTEGER                    :: i,idim
+  INTEGER, ALLOCATABLE, DIMENSION(:)       :: dimids,axis_ids,lendim
   INTEGER                    :: latid, lonid, vertid, timeid
   INTEGER                    :: varid, cmorvarid
   INTEGER                    :: ilat, ilon, ivert, itime
   INTEGER                    :: lunout      ! device de sortie
+
   logical                    :: found = .false.
 
   REAL, ALLOCATABLE, DIMENSION(:) :: lon, lat, vert, time
   REAL, ALLOCATABLE, DIMENSION(:) :: lon_bounds, lat_bounds
-  REAL, ALLOCATABLE, DIMENSION(:,:,:) :: donnees
+  REAL, ALLOCATABLE, DIMENSION(:,:,:,:) :: donnees
   DOUBLE PRECISION, DIMENSION(1)                :: rdate
   real                            :: missing_value
@@ -244,64 +249,116 @@
 ! Definition des axes
 
-! lecture de la latitude:
-  ierr = nf_inq_dimid(orig_file_id,'lat',latid)
-  ierr = nf_inq_dimlen(orig_file_id, latid, ilat)
-  allocate(lat(ilat))
-  ierr = nf_inq_varid(orig_file_id,'lat',latid)
-  ierr = nf_get_var_real(orig_file_id, latid, lat)
-  ierr = nf_get_att_text(orig_file_id, latid, 'units', units)
-  ALLOCATE(lat_bounds(ilat+1))
-  DO i = 2, ilat
-    lat_bounds(i) = lat(i-1) - (lat(i-1) - lat(i))/2
-  enddo 
-  lat_bounds(1) = lat(1)
-  lat_bounds(ilat+1) = lat(ilat)
-! definition de la latitude
-  latid = cmor_axis(                         &
-     table=trim(ipcc_table(index_table)),    &
-     table_entry='latitude',                 &
-     units=units,                            &  
-     length=ilat,                            &
-     coord_vals=lat,                         &
-     cell_bounds=lat_bounds)        
-
-! lecture de la longitude:
-  units=' ' 
-  ierr = nf_inq_dimid(orig_file_id,'lon',lonid)
-  ierr = nf_inq_dimlen(orig_file_id, lonid, ilon)
-  allocate(lon(ilon))
-  ierr = nf_inq_varid(orig_file_id,'lon',lonid)
-  ierr = nf_get_var_real(orig_file_id, lonid, lon)
-  ierr = nf_get_att_text(orig_file_id, lonid, 'units', units)
-  ALLOCATE(lon_bounds(ilon+1))
-  DO i = 2, ilon
-    lon_bounds(i) = lon(i-1) - (lon(i-1) - lon(i))/2
-  enddo 
-  lon_bounds(1) = lon(1) - (lon_bounds(3) -lon_bounds(2))/2.
-  lon_bounds(ilon+1) = lon(ilon) + (lon_bounds(ilon)-lon_bounds(ilon-1))/2. 
-
-! definition de la longitude
-  lonid = cmor_axis(                         &
-     table=trim(ipcc_table(index_table)),    &
-     table_entry='longitude',                &
-     units=units,                            &  
-     length=ilon,                            &
-     coord_vals=lon,                         &
-     cell_bounds=lon_bounds)        
-
-! definition du temps
-  units=' ' 
-  ierr = nf_inq_dimid(orig_file_id,'time_counter',timeid)
-  ierr = nf_inq_dimlen(orig_file_id,timeid,itime)
-  allocate(time(itime))
-  ierr = nf_inq_varid(orig_file_id,'time_counter',timeid)
-  ierr = nf_get_var_real(orig_file_id, timeid, time)
-  ierr = nf_get_att_text(orig_file_id,timeid, 'units', units)
-  timeid = cmor_axis(                          &
-       table=trim(ipcc_table(index_table)),    &
-       table_entry='time',                     &
-       units=units,                            &
-       length=itime,                           &
-       interval='30 minutes')
+  ierr = nf90_inq_varid(orig_file_id,TRIM(varname), varid)
+  if (ierr /= 0) call handle_err(ierr)
+  ierr = nf90_Inquire_Variable(orig_file_id, varid, ndims = ndims)
+  if (ierr /= 0) call handle_err(ierr)
+  allocate (dimids(ndims))
+  allocate (axis_ids(ndims))
+  allocate (lendim(ndims))
+  ierr = nf90_Inquire_Variable(orig_file_id, varid, dimids = dimids)
+  if (ierr /= 0) call handle_err(ierr)
+
+  do idim = 1, ndims
+    ierr = nf90_Inquire_Dimension(orig_file_id, dimids(idim), &
+                          name = namedim, len = lendim(idim))
+    if (ierr /= 0) call handle_err(ierr)
+    units=' ' 
+    selectcase(trim(namedim))
+      case('lat')
+!     lecture de la latitude:
+        allocate(lat(lendim(idim)))
+        ierr = nf_inq_varid(orig_file_id, namedim, latid)
+        if (ierr /= 0) call handle_err(ierr)
+        ierr = nf_get_var_real(orig_file_id, latid, lat)
+        if (ierr /= 0) call handle_err(ierr)
+        ierr = nf_get_att_text(orig_file_id, latid, 'units', units)
+        if (ierr /= 0) call handle_err(ierr)
+        allocate(lat_bounds(lendim(idim)+1))
+        DO i = 2, lendim(idim)
+          lat_bounds(i) = lat(i-1) - (lat(i-1) - lat(i))/2
+        enddo 
+        lat_bounds(1) = lat(1)
+        lat_bounds(lendim(idim)+1) = lat(lendim(idim))
+!       definition de la latitude
+        axis_ids(idim) = cmor_axis(                           &
+                    table=trim(ipcc_table(index_table)),      &
+                    table_entry='latitude',                   &
+                    units=units,                              &  
+                    length=lendim(idim),                      &
+                    coord_vals=lat,                           &
+                    cell_bounds=lat_bounds)
+!
+!        
+      case('lon')
+!       lecture de la longitude:
+        allocate(lon(lendim(idim)))
+        ierr = nf_inq_varid(orig_file_id, namedim, lonid)
+        if (ierr /= 0) call handle_err(ierr)
+        ierr = nf_get_var_real(orig_file_id, lonid, lon)
+        if (ierr /= 0) call handle_err(ierr)
+        ierr = nf_get_att_text(orig_file_id, lonid, 'units', units)
+        if (ierr /= 0) call handle_err(ierr)
+        ALLOCATE(lon_bounds(lendim(idim)+1))
+        DO i = 2, lendim(idim)
+         lon_bounds(i) = lon(i-1) - (lon(i-1) - lon(i))/2
+        enddo 
+        lon_bounds(1) = lon(1) - (lon_bounds(3) -lon_bounds(2))/2.
+        lon_bounds(lendim(idim)+1) = lon(lendim(idim)) + (lon_bounds(lendim(idim))-lon_bounds(lendim(idim)-1))/2. 
+
+!       definition de la longitude
+        axis_ids(idim) = cmor_axis(                           &
+                    table=trim(ipcc_table(index_table)),      &
+                    table_entry='longitude',                  &
+                    units=units,                              &  
+                    length=lendim(idim),                            &
+                    coord_vals=lon,                           &
+                    cell_bounds=lon_bounds)        
+!
+!
+      case('presnivs')
+!     lecture de la verticale:
+        allocate(vert(lendim(idim)))
+        ierr = nf_inq_varid(orig_file_id, namedim, vertid)
+        if (ierr /= 0) call handle_err(ierr)
+        ierr = nf_get_var_real(orig_file_id, vertid, vert)
+        if (ierr /= 0) call handle_err(ierr)
+        ierr = nf_get_att_text(orig_file_id, vertid, 'units', units)
+        if (ierr /= 0) call handle_err(ierr)
+!
+!       definition de la verticale
+        if (units == 'mb') units='Pa'
+        axis_ids(idim) = cmor_axis(                           &
+                    table=trim(ipcc_table(index_table)),      &
+                    table_entry='pressure',                   &
+                    units=units,                              &  
+                    length=lendim(idim),                      &
+                    coord_vals=vert)
+!
+!        
+      case('time_counter')         
+!     definition du temps
+      if (idim /= ndims) then
+        write(lunout,*)'la dimension temps doit etre la derniere dimension'
+        stop
+      endif
+      allocate(time(lendim(idim)))
+      ierr = nf_inq_varid(orig_file_id,namedim,timeid)
+      if (ierr /=0) call handle_err(ierr)
+      ierr = nf_get_var_real(orig_file_id, timeid, time)
+      if (ierr /=0) call handle_err(ierr)
+      ierr = nf_get_att_text(orig_file_id,timeid, 'units', units)
+      if (ierr /=0) call handle_err(ierr)
+      axis_ids(idim) = cmor_axis(                          &
+           table=trim(ipcc_table(index_table)),            &
+           table_entry='time',                             &
+           units=units,                                    &
+           length=lendim(idim),                            &
+           interval='30 minutes')
+      itime= lendim(idim)
+    case default
+      write(lunout,*)'Dimension: ', trim(namedim),' non reconnue'
+      stop
+   endselect
+  enddo
   
 !
@@ -309,16 +366,23 @@
   units=' ' 
   ierr = nf_inq_varid(orig_file_id,TRIM(varname), varid)
+  if (ierr /= 0) call handle_err(ierr)
   ierr = nf_get_att_text(orig_file_id, varid, 'units', units) 
+  if (ierr /= 0) call handle_err(ierr)
   ierr = nf_get_att_real(orig_file_id, varid, 'missing_value', missing_value)
+  if (ierr /= 0) call handle_err(ierr)
   cmorvarid = cmor_variable(                         &
        table=trim(ipcc_table(index_table)),          &
        table_entry=trim(ipcc_name(index_table)),     &
        units=units,                                  &
-       axis_ids=(/ lonid, latid, timeid /),          &
+       axis_ids= axis_ids,                           &
        missing_value=real(missing_value),            &
        original_name=varname)
 !
 ! Lecture de la variable
-  ALLOCATE (donnees(ilon, ilat, itime))
+  if (ndims == 3) then
+    ALLOCATE (donnees(lendim(1), lendim(2), 1, lendim(3) ))
+  else if (ndims ==4) then
+    allocate (donnees(lendim(1), lendim(2), lendim(3), lendim(4) ))
+  endif
   ierr = nf_get_var_real(orig_file_id, varid, donnees)
 !
@@ -329,5 +393,5 @@
     ierr = cmor_write(                                     &
              var_id        = cmorvarid,                    &
-             data          = real(donnees(:,:,i)),         &
+             DATA          = REAL(donnees(:,:,:,i)),       &
              ntimes_passed = 1,                            &
              time_vals     = rdate)
@@ -347,4 +411,18 @@
     stop
   endif
+
+contains
+
+subroutine handle_err(status)
+  use netcdf
+
+  implicit none
+  integer, intent(in) :: status
+
+  write(lunout,*)nf90_strerror(status)
+  stop
+
+end subroutine handle_err
   
-END
+END PROGRAM ts2IPCC
+
