lfi2cdf: options are now stored in a structure, command line is read in Fortran
[MNH-git_open_source-lfs.git] / tools / lfi2cdf / src / lfi2cdf.f90
1 program LFI2CDF
2   USE mode_options
3   USE mode_util
4   IMPLICIT NONE 
5
6   INTEGER :: ibuflen
7   INTEGER :: ji
8   INTEGER :: nbvar_infile ! number of variables available in the input file
9   INTEGER :: nbvar_tbr  ! number of variables to be read
10   INTEGER :: nbvar_calc ! number of variables to be computed from others
11   INTEGER :: nbvar_tbw  ! number of variables to be written
12   INTEGER :: nbvar      ! number of defined variables
13   INTEGER :: first_level, current_level, last_level, nb_levels
14   CHARACTER(LEN=:),allocatable :: hvarlist
15   TYPE(filelist_struct) :: infiles, outfiles
16   TYPE(workfield), DIMENSION(:), POINTER :: tzreclist
17
18   type(option),dimension(:),allocatable :: options
19   character(len=:),allocatable :: hinfile, houtfile
20   integer                      :: runmode
21
22
23   call read_commandline(options,hinfile,houtfile,runmode)
24
25   CALL OPEN_FILES(infiles, outfiles, hinfile, houtfile, nbvar_infile, options, runmode)
26   IF (options(OPTLIST)%set) return
27
28   IF (runmode == MODELFI2CDF .OR. runmode == MODECDF2CDF) THEN
29      IF (options(OPTVAR)%set) THEN
30         ! nbvar_tbr is computed from number of requested variables
31         ! by counting commas, = and +
32         nbvar_tbr  = 0
33         nbvar_calc = 0
34         nbvar_tbw = 0
35         hvarlist = options(OPTVAR)%cvalue
36         DO ji=1,len(hvarlist)
37            IF (hvarlist(ji:ji) == ',' .OR.hvarlist(ji:ji) == '+') THEN
38               nbvar_tbr = nbvar_tbr+1
39            END IF
40            IF (hvarlist(ji:ji) == ',') THEN
41               nbvar_tbw = nbvar_tbw+1
42            END IF
43            IF (hvarlist(ji:ji) == '=') THEN
44               nbvar_calc = nbvar_calc+1
45            END IF
46         END DO
47         nbvar = nbvar_calc + nbvar_tbr
48      ELSE
49         nbvar = nbvar_infile
50      END IF
51   END IF
52
53   IF (runmode == MODELFI2CDF) THEN
54      ! Conversion LFI -> NetCDF
55      
56      !Standard treatment (one LFI file only)
57      IF (.not.options(OPTMERGE)%set) THEN
58        CALL parse_infiles(infiles,nbvar_infile,nbvar_tbr,nbvar_calc,nbvar_tbw,tzreclist,ibuflen,options)
59        IF (options(OPTSPLIT)%set) call open_split_ncfiles_out(outfiles,houtfile,nbvar,tzreclist,options)
60        CALL def_ncdf(outfiles,tzreclist,nbvar,options)
61        CALL fill_ncdf(infiles,outfiles,tzreclist,nbvar,ibuflen,options)
62
63      ELSE
64      !Treat several LFI files and merge into 1 NC file
65
66        !Determine first level (eg needed to find suffix of the variable name)
67        read( hinfile(len(hinfile)-6:len(hinfile)-4) , "(I3)" ) first_level
68        nb_levels = options(OPTMERGE)%ivalue
69        current_level = first_level
70        last_level    = first_level + nb_levels - 1
71
72        !Read 1st LFI file
73        CALL parse_infiles(infiles,nbvar_infile,nbvar_tbr,nbvar_calc,nbvar_tbw,tzreclist,ibuflen,options,current_level)
74        IF (options(OPTSPLIT)%set) call open_split_ncfiles_out(outfiles,houtfile,nbvar,tzreclist,options)
75        !Define NC variables
76        CALL def_ncdf(outfiles,tzreclist,nbvar,options)
77
78        DO current_level = first_level,last_level
79          print *,'Treating level ',current_level
80          IF (current_level/=first_level) THEN
81            CALL open_split_lfifile_in(infiles,hinfile,current_level)
82            CALL read_data_lfi(infiles,nbvar,tzreclist,ibuflen,current_level)
83          END IF
84          CALL fill_ncdf(infiles,outfiles,tzreclist,nbvar,ibuflen,options,current_level)
85          IF (current_level/=last_level) CALL close_files(infiles)
86        END DO
87      END IF
88
89   ELSE IF (runmode == MODECDF2CDF) THEN
90      ! Conversion netCDF -> netCDF
91
92      !Standard treatment (one netCDF file only)
93      IF (.not.options(OPTMERGE)%set) THEN
94        CALL parse_infiles(infiles,nbvar_infile,nbvar_tbr,nbvar_calc,nbvar_tbw,tzreclist,ibuflen,options,current_level)
95        IF (options(OPTSPLIT)%set) call open_split_ncfiles_out(outfiles,houtfile,nbvar,tzreclist,options)
96        CALL def_ncdf(outfiles,tzreclist,nbvar,options)
97        CALL fill_ncdf(infiles,outfiles,tzreclist,nbvar,ibuflen,options)
98
99      ELSE
100      !Treat several NC files and merge into 1 NC file
101
102        !Determine first level (eg needed to find suffix of the variable name)
103        read( hinfile(len(hinfile)-5:len(hinfile)-3) , "(I3)" ) first_level
104        nb_levels = options(OPTMERGE)%ivalue
105        current_level = first_level
106        last_level    = first_level + nb_levels - 1
107
108        !Read 1st NC file
109        CALL parse_infiles(infiles,nbvar_infile,nbvar_tbr,nbvar_calc,nbvar_tbw,tzreclist,ibuflen,options,current_level)
110        IF (options(OPTSPLIT)%set) call open_split_ncfiles_out(outfiles,houtfile,nbvar,tzreclist,options)
111        !Define NC variables
112        CALL def_ncdf(outfiles,tzreclist,nbvar,options)
113
114        DO current_level = first_level,last_level
115          print *,'Treating level ',current_level
116          IF (current_level/=first_level) THEN
117            CALL open_split_ncfile_in(infiles,hinfile,current_level)
118            CALL update_varid_in(infiles,hinfile,tzreclist,nbvar,current_level)
119          END IF
120          CALL fill_ncdf(infiles,outfiles,tzreclist,nbvar,ibuflen,options,current_level)
121          IF (current_level/=last_level) CALL close_files(infiles)
122        END DO
123      END IF
124
125   ELSE
126      ! Conversion NetCDF -> LFI
127      CALL parse_infiles(infiles,nbvar_infile,nbvar_tbr,nbvar_calc,nbvar_tbw,tzreclist,ibuflen,options,current_level)
128      CALL build_lfi(infiles,outfiles,tzreclist,ibuflen)
129   END IF
130   
131   CALL CLOSE_FILES(infiles)
132   CALL CLOSE_FILES(outfiles)
133   
134 end program LFI2CDF