New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
fbthin.F90 in branches/dev_2802_OBStools/NEMOGCM/TOOLS/OBSTOOLS – NEMO

source: branches/dev_2802_OBStools/NEMOGCM/TOOLS/OBSTOOLS/fbthin.F90 @ 2893

Last change on this file since 2893 was 2893, checked in by djlea, 13 years ago

Adding obs tools to branch

File size: 5.4 KB
Line 
1PROGRAM fbthin
2   USE obs_fbm
3   IMPLICIT NONE
4   !
5   ! Command line arguments for output file and input file
6   !
7#ifndef NOIARGTHINROTO
8   INTEGER,EXTERNAL :: iargc
9#endif
10   INTEGER :: nargs
11   CHARACTER(len=256) :: cdoutfile
12   CHARACTER(len=256) :: cdinfile
13   CHARACTER(len=256) :: cdtmp
14   INTEGER :: nout,ninn,nadd,next,i,j,k
15   LOGICAL :: lgrid
16   !
17   ! Feedback data
18   !
19   TYPE(obfbdata) :: fbdatain
20   !
21   ! Get number of command line arguments
22   !
23   nargs=IARGC()
24   IF ((nargs /= 2)) THEN
25      WRITE(*,'(A)')'Usage:'
26      WRITE(*,'(A)')'fbthin inputfile outputfile'
27      CALL abort()
28   ENDIF
29   CALL getarg(1,cdinfile)
30   CALL getarg(2,cdoutfile)
31   !
32   ! Initialize feedback data
33   !
34   CALL init_obfbdata( fbdatain )
35   !
36   ! Read the file
37   !
38   CALL read_obfbdata( TRIM(cdinfile), fbdatain )
39   !
40   ! Do the thining
41   !
42   CALL fb_thin( fbdatain )
43   !
44   ! Write the file
45   !
46   CALL write_obfbdata( TRIM(cdoutfile), fbdatain )
47
48CONTAINS
49   
50   SUBROUTINE fb_thin( fbdata )
51      !
52      ! Observation thinning stub
53      !
54      IMPLICIT NONE
55      TYPE(obfbdata) :: fbdata
56      REAL :: dlat = 1.0
57      REAL :: dlon = 1.0
58      LOGICAL :: lred = .TRUE.
59      INTEGER, ALLOCATABLE :: thinobs(:,:), thinqc(:,:)
60      REAL, ALLOCATABLE :: thindist(:,:), thingridlat(:), thingridlon(:,:)
61      INTEGER :: nlon,nlat
62      INTEGER :: ii,ij,iv,ilon,ilat,iqc,iobs,irej
63      REAL :: zlon,zdlon,zrpi,zdist
64      LOGICAL :: lexists
65      NAMELIST/namthin/dlat,dlon,lred
66     
67      INQUIRE(file='namthin.in',exist=lexists)
68      IF (lexists) THEN
69         OPEN(10,file='namthin.in')
70         READ(10,namthin)
71         CLOSE(10)
72      ENDIF
73      WRITE(*,namthin)
74
75      nlon=360/dlon
76      nlat=180/dlat+1
77      ALLOCATE(thinobs(nlon,nlat))
78      ALLOCATE(thinqc(nlon,nlat))
79      ALLOCATE(thindist(nlon,nlat))
80      ALLOCATE(thingridlat(nlat))
81      ALLOCATE(thingridlon(nlon,nlat))
82      thinobs(:,:)=0
83      thinqc(:,:)=0
84      thindist(:,:)=0
85      zrpi=ATAN(1.0)/45.
86      DO ij=1,nlat
87         thingridlat(ij)=(ij-1)*dlat-90.0
88         IF (lred) THEN
89            zdlon=MIN(dlon/MAX(COS(ABS(zrpi*thingridlat(ij))),0.0001),360.0)
90         ELSE
91            zdlon=dlon
92         ENDIF
93         DO ii=1,nlon
94            thingridlon(ii,ij)=(ii-1)*zdlon
95         ENDDO
96      ENDDO
97      irej=0
98      iobs=0
99
100      DO ii=1,fbdata%nobs
101
102         ! Skip data with missing lon and lat.
103         IF (fbdata%plam(ii)==fbrmdi) CYCLE
104         IF (fbdata%pphi(ii)==fbrmdi) CYCLE
105
106         ! Count number of observations with qcflag<=2.
107         iqc=0
108         DO iv=1,fbdata%nvar
109            DO ij=1,fbdata%nlev
110               IF(fbdata%ivlqc(ij,ii,iv)<=2) iqc=iqc+1
111            ENDDO
112         ENDDO
113
114         ! Find ilat,ilon positions
115         zlon=fbdata%plam(ii)
116         IF (zlon>=360.0) zlon=zlon-360
117         IF (zlon<0.0) zlon=zlon+360
118         ! If reduced grid change zdlon accordingly
119         IF (lred) THEN
120            zdlon=MIN(dlon/MAX(COS(ABS(zrpi*fbdata%pphi(ii))),0.0001),360.0)
121         ELSE
122            zdlon=dlon
123         ENDIF
124         ilon=INT(zlon/zdlon)+1
125         ilat=INT((fbdata%pphi(ii)+90.0)/dlat)+1
126         iobs=iobs+1
127         ! Check which observation to pick based on number of valid obs.
128         IF (thinobs(ilon,ilat)>0) THEN
129            irej=irej+1
130            IF (iqc>=thinqc(ilon,ilat)) THEN
131               zdist=distance(zrpi,zlon,fbdata%pphi(ii),&
132                  & thingridlon(ilon,ilat),thingridlat(ilat))
133               IF(iqc==thinqc(ilon,ilat)) THEN
134                  IF (zdist<thindist(ilon,ilat)) THEN
135                     fbdata%ivlqc(:,thinobs(ilon,ilat),:)=4
136                     thinobs(ilon,ilat)=ii
137                     thinqc(ilon,ilat)=iqc
138                     thindist(ilon,ilat)=zdist
139                  ELSE
140                     fbdata%ivlqc(:,ii,:)=4
141                  ENDIF
142               ELSE
143                  fbdata%ivlqc(:,thinobs(ilon,ilat),:)=4
144                  thinobs(ilon,ilat)=ii
145                  thinqc(ilon,ilat)=iqc
146                  thindist(ilon,ilat)=zdist
147               ENDIF
148            ELSE
149               fbdata%ivlqc(:,ii,:)=4
150            ENDIF
151         ELSE
152            thinobs(ilon,ilat)=ii
153            thinqc(ilon,ilat)=iqc
154            thindist(ilon,ilat)=distance(zrpi,zlon,fbdata%pphi(ii),&
155               & thingridlon(ilon,ilat),thingridlat(ilat))
156         ENDIF
157      ENDDO
158      WRITE(*,*)'Observations considered = ',iobs
159      WRITE(*,*)'Observations rejected   = ',irej
160
161      DEALLOCATE(thinobs)
162      DEALLOCATE(thinqc)
163      DEALLOCATE(thindist)
164      DEALLOCATE(thingridlat)
165      DEALLOCATE(thingridlon)
166     
167   END SUBROUTINE fb_thin
168
169   REAL FUNCTION distance( prpi, plon, plat, gridlon, gridlat )
170      REAL :: prpi,plon,plat,gridlat,gridlon
171      REAL :: zplat,zplon,zgridlat,zgridlon
172      REAL :: za1,za2,zb1,zb2,zc1,zc2,zcos1,zcos2
173
174      zplon=plon
175      zgridlon=gridlon
176      IF (zplon<-180) zplon=zplon+360.0
177      IF (zplon>=180) zplon=zplon-360.0
178      IF (zgridlon<-180) zgridlon=zgridlon+360.0
179      IF (zgridlon>=180) zgridlon=zgridlon-360.0
180      zplon=zplon*prpi
181      zgridlon=zgridlon*prpi
182      zplat=plat*prpi
183      zgridlat=gridlat*prpi
184      zcos1=COS(zplat)
185      zcos2=COS(zgridlat)
186      za1=SIN(zplat)
187      za2=SIN(zgridlat)
188      zb1=zcos1*COS(zplon)
189      zb2=zcos2*COS(zgridlon)
190      zc1=zcos1*SIN(zplon)
191      zc2=zcos2*SIN(zgridlon)
192
193      distance=ASIN(SQRT(1.0-(za1*za2+zb1*zb2+zc1*zc2)**2))
194     
195    END FUNCTION distance
196
197END PROGRAM fbthin
Note: See TracBrowser for help on using the repository browser.