lfi2cdf: open and close files in separate subroutines in all cases (cleaner)
[MNH-git_open_source-lfs.git] / tools / lfi2cdf / src / lfi2cdf.f90
1 subroutine  LFI2CDFMAIN(hinfile,iiflen,ooutname,houtfile,ioflen,hvarlist,ivlen,olfi2cdf,olfilist,ohdf5,omerge,nb_levels,&
2                         oreduceprecision,osplit,ocompress,compress_level)
3   USE mode_util
4   IMPLICIT NONE 
5   INTEGER :: iiflen, ioflen, ivlen
6   INTEGER :: nb_levels !Number of vertical levels to merge (for LFI splitted files)
7   CHARACTER(LEN=iiflen) :: hinfile
8   CHARACTER(LEN=ioflen) :: houtfile
9   CHARACTER(LEN=ivlen)  :: hvarlist
10   LOGICAL :: ooutname, olfi2cdf, olfilist, ohdf5, omerge, oreduceprecision, osplit, ocompress
11   INTEGER :: compress_level
12
13   INTEGER :: ibuflen
14   INTEGER :: ilu
15   INTEGER :: ji
16   INTEGER :: nbvar_lfi  ! number of variables available in the LFI file
17   INTEGER :: nbvar_tbr  ! number of variables to be read
18   INTEGER :: nbvar_calc ! number of variables to be computed from others
19   INTEGER :: nbvar_tbw  ! number of variables to be written
20   INTEGER :: nbvar      ! number of defined variables
21   INTEGER :: first_level, current_level, last_level
22   TYPE(cdf_files) :: cdffiles
23   TYPE(workfield), DIMENSION(:), POINTER :: tzreclist
24
25
26   cdffiles%nbfiles = 0
27   cdffiles%opened  = .FALSE.
28
29   !Remove level in the filename if merging LFI splitted files
30   if (.NOT.ooutname) then
31     if (omerge .AND. .NOT.osplit) then
32        houtfile=houtfile(1:len(houtfile)-9)//houtfile(len(houtfile)-3:)
33     end if
34     if (.NOT.omerge .AND. osplit) then
35        if (ohdf5) then
36          ji=4
37        else
38          ji=3
39        end if
40        houtfile=houtfile(1:len(houtfile)-ji)
41     end if
42     if (omerge .AND. osplit) then
43        if (ohdf5) then
44          ji=9
45        else
46          ji=8
47        end if
48        houtfile=houtfile(1:len(houtfile)-ji)
49     end if
50   end if
51
52   CALL OPEN_FILES(hinfile, houtfile, olfi2cdf, olfilist, ohdf5, cdffiles, ilu, nbvar_lfi, osplit)
53   IF (olfilist) return
54
55   IF (olfi2cdf) THEN
56      ! Conversion LFI -> NetCDF
57      IF (ivlen > 0) THEN
58         ! nbvar_tbr is computed from number of requested variables
59         ! by counting commas, = and +
60         nbvar_tbr  = 0
61         nbvar_calc = 0
62         DO ji=1,ivlen
63            IF (hvarlist(ji:ji) == ',' .OR.hvarlist(ji:ji) == '+') THEN
64               nbvar_tbr = nbvar_tbr+1
65            END IF
66            IF (hvarlist(ji:ji) == ',') THEN
67               nbvar_tbw = nbvar_tbw+1
68            END IF
69            IF (hvarlist(ji:ji) == '=') THEN
70               nbvar_calc = nbvar_calc+1
71            END IF
72         END DO
73         nbvar = nbvar_calc + nbvar_tbr
74      ELSE
75         nbvar = nbvar_lfi
76      END IF
77      
78      !Standard treatment (one LFI file only)
79      IF (.not.omerge) THEN
80        CALL parse_lfi(ilu,hvarlist,nbvar_lfi,nbvar_tbr,nbvar_calc,nbvar_tbw,tzreclist,ibuflen)
81        IF (osplit) call open_split_ncfiles(houtfile,nbvar,tzreclist,cdffiles,ohdf5)
82        CALL def_ncdf(tzreclist,nbvar,oreduceprecision,cdffiles,omerge,ocompress,compress_level)
83        CALL fill_ncdf(ilu,tzreclist,nbvar,ibuflen,cdffiles)
84
85      ELSE
86      !Treat several LFI files and merge into 1 NC file
87
88        !Determine first level (eg needed to find suffix of the variable name)
89        read( hinfile(len(hinfile)-6:len(hinfile)-4) , "(I3)" ) first_level
90        current_level = first_level
91        last_level    = first_level + nb_levels - 1
92
93        !Read 1st LFI file
94        CALL parse_lfi(ilu,hvarlist,nbvar_lfi,nbvar_tbr,nbvar_calc,nbvar_tbw,tzreclist,ibuflen,current_level)
95        IF (osplit) call open_split_ncfiles(houtfile,nbvar,tzreclist,cdffiles,ohdf5)
96        !Define NC variables
97        CALL def_ncdf(tzreclist,nbvar,oreduceprecision,cdffiles,omerge,ocompress,compress_level)
98
99        DO current_level = first_level,last_level
100          print *,'Treating level ',current_level
101          IF (current_level/=first_level) THEN
102            CALL open_split_lfifile(ilu,hinfile,current_level)
103            CALL read_data_lfi(ilu,hvarlist,nbvar,tzreclist,ibuflen,current_level)
104          END IF
105          CALL fill_ncdf(ilu,tzreclist,nbvar,ibuflen,cdffiles,current_level)
106          IF (current_level/=last_level) CALL close_split_lfifile(ilu)
107        END DO
108      END IF
109
110   ELSE
111      ! Conversion NetCDF -> LFI
112      CALL parse_cdf(cdffiles%cdf_id(1),tzreclist,ibuflen)
113      CALL build_lfi(cdffiles%cdf_id(1),ilu,tzreclist,ibuflen)
114   END IF
115   
116   CALL CLOSE_FILES(ilu,cdffiles,osplit)
117   
118 end subroutine LFI2CDFMAIN
119