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.
nclatlon.F90 in branches/dev_2802_OBStools/NEMOGCM/TOOLS/OBSTOOLS – NEMO

source: branches/dev_2802_OBStools/NEMOGCM/TOOLS/OBSTOOLS/nclatlon.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.1 KB
Line 
1PROGRAM nclatlon
2   USE netcdf
3   IMPLICIT NONE
4   !
5   ! Command line arguments for output file and input file
6   !
7#ifndef NOIARGCPROTO
8   INTEGER,EXTERNAL :: iargc
9#endif
10   INTEGER :: nargs
11   CHARACTER(len=256) :: cdinfile
12   CHARACTER(len=64) :: cdlatvar
13   CHARACTER(len=64) :: cdlonvar
14   CHARACTER(len=256) :: cdoutfile
15   CHARACTER(len=32) :: cdscale
16   !
17   ! Input data
18   !
19   REAL, ALLOCATABLE, DIMENSION(:) :: zlat, zlon
20   REAL :: zscalat,zscalon
21   INTEGER :: nopoint
22   !
23   ! NetCDF ids
24   !
25   INTEGER :: idfile, idlat, idlon
26   INTEGER, ALLOCATABLE, DIMENSION(:) :: idimlats, idimlons, idimlens
27   INTEGER :: ndimlat, ndimlon
28   !
29   ! Loop variables
30   !
31   INTEGER :: i, isize
32   !
33   ! Get number of command line arguments
34   !
35   nargs=IARGC()
36   IF ((nargs<4).OR.(nargs>5)) THEN
37      WRITE(*,'(A)')'Usage:'
38      WRITE(*,'(A)')'nclatlon inputfile latvar lonvar outputfile [scale]'
39      CALL abort()
40   ENDIF
41   CALL getarg(1,cdinfile)
42   CALL getarg(2,cdlatvar)
43   CALL getarg(3,cdlonvar)
44   CALL getarg(4,cdoutfile)
45   WRITE(*,*)'cdinfile  = ',TRIM(cdinfile)
46   WRITE(*,*)'cdlatvar  = ',TRIM(cdlatvar)
47   WRITE(*,*)'cdlonvar  = ',TRIM(cdlonvar)
48   WRITE(*,*)'cdoutfile = ',TRIM(cdoutfile)
49   !
50   IF (nargs==5) THEN
51      CALL getarg(5,cdscale)
52      WRITE(*,*)'cdscale = ',TRIM(cdscale)
53      READ(cdscale,*)zscalat
54      zscalon=zscalat
55   ELSE
56      zscalat=1.0
57      zscalon=1.0
58   ENDIF
59   !
60   ! Open netCDF file
61   !
62   IF (nf90_open(TRIM(cdinfile),nf90_nowrite,idfile)/=nf90_noerr) THEN
63      WRITE(*,*)'Error opening input file'
64      CALL abort
65   ENDIF
66   !
67   ! Get latitude id
68   !
69   IF (nf90_inq_varid(idfile,TRIM(cdlatvar),idlat)/=nf90_noerr) THEN
70      WRITE(*,*)'Error getting lat id'
71      CALL abort
72   ENDIF
73   !
74   ! Get number of latitude dimensions
75   !
76   IF (nf90_inquire_variable(idfile,idlat,ndims=ndimlat)/=nf90_noerr) THEN
77      WRITE(*,*)'Error getting number of latitude dimensions'
78      CALL abort
79   ENDIF
80   WRITE(*,*)'ndimlat=',ndimlat
81   !
82   ! Get latitude dimensions
83   !
84   ALLOCATE(idimlats(ndimlat))
85   IF (nf90_inquire_variable(idfile,idlat,dimids=idimlats)/=nf90_noerr) THEN
86      WRITE(*,*)'Error getting latitude dimensions'
87      CALL abort
88   ENDIF
89   WRITE(*,*)'idimlats=',idimlats
90   !
91   ! Get longitude id
92   !
93   IF (nf90_inq_varid(idfile,TRIM(cdlonvar),idlon)/=nf90_noerr) THEN
94      WRITE(*,*)'Error getting lon id'
95      CALL abort
96   ENDIF
97   !
98   ! Get number of longitude dimensions
99   !
100   IF (nf90_inquire_variable(idfile,idlon,ndims=ndimlon)/=nf90_noerr) THEN
101      WRITE(*,*)'Error getting number of longitude dimensions'
102      CALL abort
103   ENDIF
104   WRITE(*,*)'ndimlon=',ndimlon
105   !
106   ! Get longitude dimensions
107   !
108   ALLOCATE(idimlons(ndimlon))
109   IF (nf90_inquire_variable(idfile,idlon,dimids=idimlons)/=nf90_noerr) THEN
110      WRITE(*,*)'Error getting longitude dimensions'
111      CALL abort
112   ENDIF
113   WRITE(*,*)'idimlons=',idimlons
114   !
115   ! Check that latitude and longitude has the same shape
116   !
117   IF (ndimlon/=ndimlat) THEN
118      WRITE(*,*)'Different number of dimensions for latitude and longitude'
119      CALL abort
120   ENDIF
121   DO i=1,ndimlat
122      IF (idimlons(i)/=idimlats(i)) THEN
123         WRITE(*,*)'Different dimension for latitude and longitude'
124         CALL abort
125      ENDIF
126   ENDDO
127   !
128   ! Get dimensions
129   !
130   ALLOCATE(idimlens(ndimlat))
131   DO i=1,ndimlat
132      IF (nf90_inquire_dimension(idfile,idimlats(i),len=idimlens(i))/=nf90_noerr) THEN
133         WRITE(*,*)'Error getting dimension length'
134         CALL abort
135      ENDIF
136   ENDDO
137   WRITE(*,*)'idimlens=',idimlens
138   !
139   ! Get the data
140   !
141   isize=1
142   DO i=1,ndimlat
143      isize=isize*idimlens(i)
144   ENDDO
145   ALLOCATE(zlat(isize),zlon(isize))
146   IF (nf90_get_var(idfile,idlat,zlat)/=nf90_noerr) THEN
147      WRITE(*,*)'Error getting latitude'
148      CALL abort
149   ENDIF
150   IF (nf90_get_var(idfile,idlon,zlon)/=nf90_noerr) THEN
151      WRITE(*,*)'Error getting longitude'
152      CALL abort
153   ENDIF
154   !
155   ! Get the scale_factor for latitude (if it exists)
156   !
157   IF (zscalat==1.0) THEN
158      IF (nf90_inquire_attribute(idfile,idlat,&
159         &                     "scale_factor")==nf90_noerr) THEN
160         IF (nf90_get_att(idfile,idlat,"scale_factor",zscalat) &
161            & /=nf90_noerr) THEN
162            WRITE(*,*)'Error getting latitude scale_factor'
163            CALL abort
164         ENDIF
165         WRITE(*,*)'Latitude scale factor',zscalat
166      ENDIF
167   ENDIF
168   !
169   ! Get the scale_factor for longitude (if it exists)
170   !
171   IF (zscalon==1.0) THEN
172      IF (nf90_inquire_attribute(idfile,idlon,&
173         &                     "scale_factor")==nf90_noerr) THEN
174         IF (nf90_get_att(idfile,idlon,"scale_factor",zscalon) &
175            & /=nf90_noerr) THEN
176            WRITE(*,*)'Error getting longitude scale_factor'
177            CALL abort
178         ENDIF
179         WRITE(*,*)'Longitude scale factor',zscalon
180      ENDIF
181   ENDIF
182   !
183   ! Close netCDF file
184   !
185   IF (nf90_close(idfile)/=nf90_noerr) THEN
186      WRITE(*,*)'Error closing input file'
187      CALL abort
188   ENDIF
189   !
190   ! Write the data
191   !
192   OPEN(10,file=cdoutfile)
193   DO i=1,isize
194      WRITE(10,'(2F12.6)')zscalat*zlat(i),zscalon*zlon(i)
195   ENDDO
196   CLOSE(10)
197   !
198END PROGRAM nclatlon
Note: See TracBrowser for help on using the repository browser.