4 Make gsw_mod_saar_data.f from the current gsw_data_v3_0.nc. This is a developer 5 utility and not a part of the public distribution, but its end-product is. 6 Note that it generates gsw_saar_data.c but will not overwrite it if it exists. 7 General concept: we don't want end-users of this distribution to require having 8 netcdf installed, nor do we want to incur the I/O overhead every time this 9 library is used. So we simply generate static data from the netcdf file. 12 from netCDF4
import Dataset
18 str_val = sformat % val
19 if str_val.find(
".") < 0
and str_val.find(
"e") < 0 :
30 fortran_dims =
"(%s)" % v.dimensions[0]
32 fortran_dims =
"(%s,%s)" % v.dimensions[::-1]
34 fortran_dims =
"(%s,%s,%s)" % v.dimensions[::-1]
36 out.write(
"real (r8), dimension%s :: %s\n" % (fortran_dims, var_name))
42 out.write(
"data %s / &\n" % var_name)
50 for val, x
in [(vv[cx],cx)
for cx
in range(dims[0])]:
52 if len(buf)+
len(sval) > maxlen:
60 out.write(
"data %s / &\n" % var_name)
64 for x
in range(dims[0]):
65 for val,y
in [(vv[x][cy],cy)
for cy
in range(dims[1])]:
66 sval =
float2string(val,numformat,(x != lastx
or y != lasty))
67 if len(buf)+
len(sval) > maxlen:
82 for x
in range(dims[0]):
83 for y
in range(dims[1]):
84 out.write(
"data %s(:,%d,%d) / &\n" % (var_name, y+1, x+1))
85 for val,z
in [(vv[x][y][cz],cz)
for cz
in range(dims[2])]:
87 if len(buf)+
len(sval) > maxlen:
101 fortran_dims =
"(%s)" % v.dimensions[0]
103 fortran_dims =
"(%s,%s)" % v.dimensions[::-1]
105 fortran_dims =
"(%s,%s,%s)" % v.dimensions[::-1]
107 out.write(
"integer, dimension%s :: %s\n" % (fortran_dims, var_name))
108 out.write(
"data %s / &\n" % var_name)
121 for val, x
in [(vv[cx],cx)
for cx
in range(dims[0])]:
128 if len(buf)+
len(sval) > maxlen:
136 for x
in range(dims[0]):
137 for val,y
in [(vv[x][cy],cy)
for cy
in range(dims[1])]:
142 if x != lastx
or y != lasty:
144 if len(buf)+
len(sval) > maxlen:
153 for x
in range(dims[0]):
154 for y
in range(dims[1]):
155 for val,z
in [(vv[x][y][cz],cz)
for cz
in range(dims[2])]:
160 if x != lastx
or y != lasty
or z != lastz:
162 if len(buf)+
len(sval) > maxlen:
167 out.write(buf+
" &\n")
170 rootgrp = Dataset(
'gsw_data_v3_0.nc',
'r') 171 v = rootgrp.variables 172 d = rootgrp.dimensions 178 version_date = rootgrp.version_date
179 version_number = rootgrp.version_number
181 vars_real = [[
"p_ref",
"", [nz]], [
"lats_ref",
"", [ny]],
182 [
"longs_ref",
"", [nx]], [
"saar_ref",
"SAAR_ref", [nx,ny,nz]],
183 [
"delta_sa_ref",
"deltaSA_ref", [nx,ny,nz]]]
185 vars_int = [[
"ndepth_ref",
"", [nx,ny]]]
188 fd = os.open(
"gsw_mod_saar_data.f90", os.O_CREAT|os.O_EXCL|os.O_RDWR, 0644)
190 print str(sys.exc_info()[1])
191 print "Will not overwrite gsw_mod_saar_data.f90. Exiting." 193 out = os.fdopen(fd,
"w")
194 out.write(
"!\n!** $Id$\n!** Extracted from gsw_data_v3_0.nc\n!\n")
196 !========================================================================== 197 module gsw_mod_saar_data 198 !========================================================================== 204 integer, dimension(4) :: deli = (/0,1,1,0/), delj = (/0,0,1,1/) 206 integer, parameter :: npan = 6 207 real (r8), dimension(npan) :: longs_pan, lats_pan 209 data longs_pan /260.00_r8, 272.59_r8, 276.50_r8, 278.65_r8, 280.73_r8, 292.0_r8/ 210 data lats_pan / 19.55_r8, 13.97_r8, 9.60_r8, 8.10_r8, 9.33_r8, 3.4_r8/ 214 out.write(
"integer, parameter :: nx = %d\n"% nx)
215 out.write(
"integer, parameter :: ny = %d\n"% ny)
216 out.write(
"integer, parameter :: nz = %d\n"% nz)
218 out.write(
"character(*), parameter :: gsw_version_date = \"%s\"\n" % version_date)
219 out.write(
"character(*), parameter :: gsw_version_number = \"%s\"\n\n" % version_number)
221 for var_label, var_name, dims
in [var
for var
in vars_real]:
226 for var_label, var_name, dims
in [var
for var
in vars_int]:
234 subroutine gsw_get_version (version_date, version_number) 238 character(*), intent(out) :: version_date, version_number 240 version_date = gsw_version_date 241 version_number = gsw_version_number 243 end subroutine gsw_get_version 245 end module gsw_mod_saar_data 246 !-------------------------------------------------------------------------- def write_variable_int(var_name, dims, v)
def float2string(val, sformat, addcomma)
def write_variable_real(var_name, dims, v)