Subversion Repositories lagranto.arpege

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 michaesp 1
      PROGRAM reformat
2
 
3
c     ***********************************************************************
4
c     * Change format of a trajectory file                                  *
5
c     * Michael Sprenger / Spring, summer 2010                              *
6
c     ***********************************************************************
7
 
8
      implicit none
9
 
10
c     ----------------------------------------------------------------------
11
c     Declaration of variables
12
c     ----------------------------------------------------------------------
13
 
14
c     Mode & Special parameters depending on mode
15
      character*80                           mode
16
      real                                   clon,clat,radius
17
 
18
c     Input and output files
19
      character*80                           inpfile     ! Input filename
20
      character*80                           outfile     ! Output filename
21
 
22
c     Trajectories
23
      integer                                ntra        ! Number of trajectories
24
      integer                                ntim        ! Number of times
25
      integer                                ncol        ! Number of columns
26
      real,allocatable, dimension (:,:,:) :: tra         ! Trajectories (ntra,ntim,ncol)
27
      real,allocatable, dimension (:,:)   :: proxy       ! Footprint
28
      character*80                           vars(100)   ! Variable names
29
      integer                                refdate(6)  ! Reference date
30
 
31
c     Auxiliary variables
32
      integer                                inpmode
33
      integer                                stat
34
      integer                                fid
35
      integer                                i,j,k
36
      real                                   dist
37
      real                                   lon0,lat0,lon1,lat1
38
      real                                   boost
39
      real                                   hhmm,tfrac
40
      integer                                nout
41
 
42
c     Externals
43
      real                                   sdis
44
      external                               sdis
45
 
46
c     ----------------------------------------------------------------------
47
c     Preparations
48
c     ----------------------------------------------------------------------
49
 
50
c     Read parameters
51
      open(10,file='footprint.param')
52
       read(10,*) inpfile
53
       read(10,*) outfile
54
       read(10,*) ntra,ntim,ncol
55
       read(10,*) mode
56
       if ( mode.eq.'proxy' ) then
57
          read(10,*) clon
58
          read(10,*) clat
59
          read(10,*) radius
60
       endif
61
      close(10)
62
 
63
c     Check that a valid mode is selected
64
      if ( mode.eq.'proxy' ) goto 10
65
      print*,' Unknown mode ',trim(mode)
66
      stop
67
 10   continue
68
 
69
c     Determine the formats
70
      call mode_tra(inpmode,inpfile)
71
      if (inpmode.eq.-1) inpmode=1
72
 
73
c     Allocate memory
74
      allocate(tra(ntra,ntim,ncol),stat=stat)
75
      if (stat.ne.0) print*,'*** error allocating array tra      ***' 
76
      if ( mode.eq. 'proxy' ) then
77
           allocate(proxy(ntra,ncol+1),stat=stat)
78
           if (stat.ne.0) print*,'*** error allocating array proxy  ***'  
79
      endif
80
 
81
c     Read inpufile
82
      call ropen_tra(fid,inpfile,ntra,ntim,ncol,refdate,vars,inpmode)
83
      call read_tra (fid,tra,ntra,ntim,ncol,inpmode)
84
      call close_tra(fid,inpmode)
85
 
86
c     ----------------------------------------------------------------------
87
c     Mode = 'proxy': get nearest distance to a lat/lon point
88
c     ----------------------------------------------------------------------
89
 
90
      if ( mode.ne.'proxy') goto 200
91
 
92
c     Transform into fractional time
93
      do i=1,ntra
94
         do j=1,ntim
95
            hhmm = tra(i,j,1)
96
            call hhmm2frac(hhmm,tfrac)
97
            tra(i,j,1) = tfrac
98
         enddo
99
      enddo
100
 
101
c     Get proxy and get number of output steps
102
      call get_proxy (proxy,clon,clat,tra,ntra,ntim,ncol,radius)
103
 
104
c     Transform into hhmm time       
105
      do i=1,ntra
106
         tfrac = proxy(i,1)
107
         call frac2hhmm(tfrac,hhmm)
108
         proxy(i,1) = hhmm
109
      enddo
110
 
111
c     Write output
112
      vars(ncol+1) = 'dist'
113
      open(10,file=outfile)
114
         call write_hea(10,refdate,vars,1,1,ncol+1,1)
115
         do i=1,ntra
116
            if ( proxy(i,ncol+1).lt.radius ) then
117
                write(10,'(1f7.2,f9.2,f8.2,i6,100f10.3)') 
118
     >               (proxy(i,j),j=1,3),             ! time, lon, lat
119
     >               nint(proxy(i,4)),               ! p
120
     >               (proxy(i,j),j=5,ncol+1)         ! fields
121
           endif
122
        enddo
123
      close(10)
124
 
125
 200  continue
126
 
127
      end
128
 
129
 
130
c     --------------------------------------------------------------------
131
c     Subroutines to write the netcdf output file
132
c     --------------------------------------------------------------------
133
 
134
      subroutine writecdf2D(cdfname,
135
     >                      varname,arr,time,
136
     >                      dx,dy,xmin,ymin,nx,ny,
137
     >                      crefile,crevar)
138
 
139
c     Create and write to the netcdf file <cdfname>. The variable
140
c     with name <varname> and with time <time> is written. The data
141
c     are in the two-dimensional array <arr>. The list <dx,dy,xmin,
142
c     ymin,nx,ny> specifies the output grid. The flags <crefile> and
143
c     <crevar> determine whether the file and/or the variable should
144
c     be created
145
 
146
      IMPLICIT NONE
147
 
148
c     Declaration of input parameters
149
      character*80 cdfname,varname
150
      integer nx,ny
151
      real arr(nx,ny)
152
      real dx,dy,xmin,ymin
153
      real time
154
      integer crefile,crevar
155
 
156
c     Further variables
157
      real varmin(4),varmax(4),stag(4)
158
      integer ierr,cdfid,ndim,vardim(4)
159
      character*80 cstname
160
      real mdv
161
      integer datar(14),stdate(5)
162
      integer i
163
 
164
C     Definitions
165
      varmin(1)=xmin
166
      varmin(2)=ymin
167
      varmin(3)=1050.
168
      varmax(1)=xmin+real(nx-1)*dx
169
      varmax(2)=ymin+real(ny-1)*dy
170
      varmax(3)=1050.
171
      cstname=trim(cdfname)//'_cst'
172
      ndim=4
173
      vardim(1)=nx
174
      vardim(2)=ny
175
      vardim(3)=1
176
      stag(1)=0.
177
      stag(2)=0.
178
      stag(3)=0.
179
      mdv=-999.98999
180
 
181
C     Create the file
182
      if (crefile.eq.0) then
183
         call cdfwopn(cdfname,cdfid,ierr)
184
         if (ierr.ne.0) goto 906
185
      else if (crefile.eq.1) then
186
         call crecdf(cdfname,cdfid,
187
     >        varmin,varmax,ndim,cstname,ierr)
188
         if (ierr.ne.0) goto 903
189
 
190
C        Write the constants file
191
         datar(1)=vardim(1)
192
         datar(2)=vardim(2)
193
         datar(3)=int(1000.*varmax(2))
194
         datar(4)=int(1000.*varmin(1))
195
         datar(5)=int(1000.*varmin(2))
196
         datar(6)=int(1000.*varmax(1))
197
         datar(7)=int(1000.*dx)
198
         datar(8)=int(1000.*dy)
199
         datar(9)=1
200
         datar(10)=1
201
         datar(11)=0            ! data version
202
         datar(12)=0            ! cstfile version
203
         datar(13)=0            ! longitude of pole
204
         datar(14)=90000        ! latitude of pole
205
         do i=1,5
206
            stdate(i)=0
207
         enddo
208
c         call wricst(cstname,datar,0.,0.,0.,0.,stdate)
209
      endif
210
 
211
c     Write the data to the netcdf file, and close the file
212
      if (crevar.eq.1) then
213
         call putdef(cdfid,varname,ndim,mdv,
214
     >        vardim,varmin,varmax,stag,ierr)
215
         if (ierr.ne.0) goto 904
216
      endif
217
      call putdat(cdfid,varname,time,0,arr,ierr)
218
      if (ierr.ne.0) goto 905
219
      call clscdf(cdfid,ierr)
220
 
221
      return
222
 
223
c     Error handling
224
 903  print*,'*** Problem to create netcdf file ***'
225
      stop
226
 904  print*,'*** Problem to write definition ***'
227
      stop
228
 905  print*,'*** Problem to write data ***'
229
      stop
230
 906  print*,'*** Problem to open netcdf file ***'
231
      stop
232
 
233
      END
234
 
235
 
236