4 Make gsw_mod_check_data.c 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. Note 6 that it generates gsw_mod_check_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. 11 import math, os, sys, numpy, re
12 from netCDF4
import Dataset
16 pzeros = re.compile(
"0{7,}[1-9]*")
17 pnines = re.compile(
"9{7,}[0-8]*")
23 str_val = sformat % val
30 if str_val.find(
".") < 0
and str_val.find(
"e") < 0 :
40 (str_val, nsubs) = pnines.subn(
"",str_val)
42 last_digit =
"%d" % (int(str_val[-1]) + 1)
43 str_val = str_val[:-1] + last_digit
45 str_val = pzeros.sub(
"",str_val)
57 fortran_dims = (
", dimension(" + work_dims[
""][2] +
")")
59 fortran_dims = (
", dimension(" + work_dims[v.dimensions[1]][1] +
"," 60 + work_dims[v.dimensions[1]][2] +
")")
62 out.write(
"real (r8)%s :: %s\n" % (fortran_dims, var_name))
63 out.write(
"data %s / &\n" % var_name)
76 for val, x
in [(vv[cx],cx)
for cx
in range(dims[0])]:
78 if len(buf)+
len(sval) > maxlen:
86 for x
in range(dims[0]):
87 for val,y
in [(vv[x][cy],cy)
for cy
in range(dims[1])]:
88 sval =
float2string(val,num_format,(x != lastx
or y != lasty))
89 if len(buf)+
len(sval) > maxlen:
98 for x
in range(dims[0]):
99 for y
in range(dims[1]):
100 for val,z
in [(vv[x][y][cz],cz)
for cz
in range(dims[2])]:
102 (x != lastx
or y != lasty
or z != lastz))
103 if len(buf)+
len(sval) > maxlen:
108 out.write(buf+
" /\n\n")
113 gsw_type = work_dims[
""][3]
115 gsw_type = work_dims[v.dimensions[1]][3]
117 out.write(
"type(%s) :: %s\n\n" % (gsw_type, var_name.lower()))
118 out.write(
"data %s / %s( &\n" % (var_name.lower(), gsw_type))
120 if math.isnan(v.computation_accuracy):
123 str_ca =
"%.5g_r8" % v.computation_accuracy
125 out.write(
" \"%s\", %s, (/ &\n" % (var_name, str_ca))
127 vmax = numpy.nanmax(v)
129 precision = v.computation_accuracy/abs(vmax)
131 precision = v.computation_accuracy
133 if precision < 1e-12:
135 elif precision < 1e-10:
150 for val, x
in [(vv[cx],cx)
for cx
in range(dims[0])]:
152 if len(buf)+
len(sval) > maxlen:
160 for x
in range(dims[0]):
161 for val,y
in [(vv[x][cy],cy)
for cy
in range(dims[1])]:
162 sval =
float2string(val,num_format,(x != lastx
or y != lasty))
163 if len(buf)+
len(sval) > maxlen:
172 for x
in range(dims[0]):
173 for y
in range(dims[1]):
174 for val,z
in [(vv[x][y][cz],cz)
for cz
in range(dims[2])]:
176 (x != lastx
or y != lasty
or z != lastz))
177 if len(buf)+
len(sval) > maxlen:
182 out.write(buf+
" &\n")
183 out.write(
" /) ) /\n\n")
186 "test_cast_length": [
"test_cast_number",
187 "cast_m",
"cast_n",
"gsw_result"],
188 "Arctic_test_cast_length": [
"Arctic_test_cast_number",
189 "cast_ice_m",
"cast_ice_n",
"gsw_result_ice"],
190 "test_cast_midpressure_length": [
"test_cast_midpressure_number",
191 "cast_mpres_m",
"cast_mpres_n",
"gsw_result_mpres"],
192 "": [
"",
"",
"cast_n",
"gsw_result_cast"]
195 [
"ct",
"CT_chck_cast"],
196 [
"rt",
"Rt_chck_cast"],
197 [
"sa",
"SA_chck_cast"],
198 [
"sk",
"SK_chck_cast"],
199 [
"sp",
"SP_chck_cast"],
200 [
"t",
"t_chck_cast"],
201 [
"p",
"p_chck_cast"],
202 [
"delta_p",
"delta_p_chck_cast"],
203 [
"p_shallow",
"p_chck_cast_shallow"],
204 [
"p_deep",
"p_chck_cast_deep"],
205 [
"lat_cast",
"lat_chck_cast"],
206 [
"long_cast",
"long_chck_cast"],
207 [
"ct_arctic",
"CT_Arctic"],
208 [
"sa_arctic",
"SA_Arctic"],
209 [
"t_arctic",
"t_Arctic"],
210 [
"p_arctic",
"p_Arctic"],
211 [
"sa_seaice",
"SA_seaice"],
212 [
"t_seaice",
"t_seaice"],
213 [
"w_seaice",
"w_seaice"],
216 [
"sa_bulk",
"SA_bulk"],
217 [
"h_pot_bulk",
"h_pot_bulk"],
218 [
"h_bulk",
"h_bulk"],
226 [
'Sstar_from_SP',
""],
228 [
'deltaSA_from_SP',
""],
232 [
'Sstar_from_SA',
""],
233 [
'SA_from_Sstar',
""],
234 [
'SP_from_Sstar',
""],
242 [
'entropy_from_pt',
""],
243 [
'pt_from_entropy',
""],
244 [
'CT_from_entropy',
""],
245 [
'entropy_from_t',
""],
246 [
'adiabatic_lapse_rate_from_CT',
""],
250 [
'alpha_on_beta',
""],
264 [
'v_SA_SA_wrt_h',
""],
267 [
'specvol_anom_standard',
""],
280 [
'rho_SA_wrt_h',
""],
282 [
'rho_SA_SA_wrt_h',
""],
296 [
'CT_maxdensity',
""],
297 [
'internal_energy',
""],
299 [
'enthalpy_diff',
""],
300 [
'CT_from_enthalpy',
""],
301 [
'dynamic_enthalpy',
""],
323 [
'CT_freezing_poly',
""],
325 [
't_freezing_poly',
""],
326 [
'pot_enthalpy_ice_freezing',
""],
327 [
'pot_enthalpy_ice_freezing_poly',
""],
328 [
'SA_freezing_from_CT',
""],
329 [
'SA_freezing_from_CT_poly',
""],
330 [
'SA_freezing_from_t',
""],
331 [
'SA_freezing_from_t_poly',
""],
332 [
'CTfreezing_SA',
""],
333 [
'CTfreezing_P',
""],
334 [
'CTfreezing_SA_poly',
""],
335 [
'CTfreezing_P_poly',
""],
336 [
'tfreezing_SA',
""],
338 [
'tfreezing_SA_poly',
""],
339 [
'tfreezing_P_poly',
""],
340 [
'pot_enthalpy_ice_freezing_SA',
""],
341 [
'pot_enthalpy_ice_freezing_P',
""],
342 [
'pot_enthalpy_ice_freezing_SA_poly',
""],
343 [
'pot_enthalpy_ice_freezing_P_poly',
""],
344 [
'latentheat_melting',
""],
345 [
'latentheat_evap_CT',
""],
346 [
'latentheat_evap_t',
""],
348 [
'enthalpy_CT_exact',
""],
349 [
'h_SA_CT_exact',
""],
350 [
'h_CT_CT_exact',
""],
351 [
'h_SA_SA_CT_exact',
""],
352 [
'h_SA_CT_CT_exact',
""],
353 [
'h_CT_CT_CT_exact',
""],
355 [
'pot_rho_t_exact',
""],
356 [
'alpha_wrt_t_exact',
""],
357 [
'beta_const_t_exact',
""],
358 [
'specvol_t_exact',
""],
359 [
'sound_speed_t_exact',
""],
360 [
'kappa_t_exact',
""],
361 [
'enthalpy_t_exact',
""],
365 [
'chem_potential_water_t_exact',
""],
366 [
't_deriv_chem_potential_water_t_exact',
""],
367 [
'dilution_coefficient_t_exact',
""],
368 [
'deltaSA_atlas',
""],
377 [
'n2min_specvol',
""],
384 [
'p_mid_IPVfN2',
""],
385 [
'n2_lowerlimit',
""],
387 [
'geo_strf_dyn_height',
""],
388 [
'geo_strf_dyn_height_pc',
""],
389 [
'geo_strf_dyn_height_pc_p_mid',
""],
391 [
'alpha_wrt_t_ice',
""],
393 [
'pressure_coefficient_ice',
""],
394 [
'sound_speed_ice',
""],
396 [
'kappa_const_t_ice',
""],
397 [
'internal_energy_ice',
""],
398 [
'enthalpy_ice',
""],
401 [
'chem_potential_water_ice',
""],
402 [
'Helmholtz_energy_ice',
""],
403 [
'adiabatic_lapse_rate_ice',
""],
404 [
'pt0_from_t_ice',
""],
405 [
'pt_from_t_ice',
""],
406 [
't_from_pt0_ice',
""],
407 [
'pot_enthalpy_from_pt_ice',
""],
408 [
'pt_from_pot_enthalpy_ice',
""],
409 [
'pot_enthalpy_from_pt_ice_poly',
""],
410 [
'pt_from_pot_enthalpy_ice_poly',
""],
411 [
'pressure_freezing_CT',
""],
412 [
'melting_ice_SA_CT_ratio',
""],
413 [
'melting_ice_SA_CT_ratio_poly',
""],
414 [
'melting_ice_equilibrium_SA_CT_ratio',
""],
415 [
'melting_ice_equilibrium_SA_CT_ratio_poly',
""],
416 [
'melting_ice_into_seawater_SA_final',
""],
417 [
'melting_ice_into_seawater_CT_final',
""],
418 [
'ice_fraction_to_freeze_seawater_SA_freeze',
""],
419 [
'ice_fraction_to_freeze_seawater_CT_freeze',
""],
420 [
'ice_fraction_to_freeze_seawater_w_Ih',
""],
421 [
'dSA_dCT_frazil',
""],
422 [
'dSA_dP_frazil',
""],
423 [
'dCT_dP_frazil',
""],
424 [
'dSA_dCT_frazil_poly',
""],
425 [
'dSA_dP_frazil_poly',
""],
426 [
'dCT_dP_frazil_poly',
""],
427 [
'frazil_properties_potential_SA_final',
""],
428 [
'frazil_properties_potential_CT_final',
""],
429 [
'frazil_properties_potential_w_Ih_final',
""],
430 [
'frazil_properties_potential_poly_SA_final',
""],
431 [
'frazil_properties_potential_poly_CT_final',
""],
432 [
'frazil_properties_potential_poly_w_Ih_final',
""],
433 [
'frazil_properties_SA_final',
""],
434 [
'frazil_properties_CT_final',
""],
435 [
'frazil_properties_w_Ih_final',
""],
436 [
'melting_seaice_SA_CT_ratio',
""],
437 [
'melting_seaice_SA_CT_ratio_poly',
""],
438 [
'melting_seaice_equilibrium_SA_CT_ratio',
""],
439 [
'melting_seaice_equilibrium_SA_CT_ratio_poly',
""],
440 [
'melting_seaice_into_seawater_SA_final',
""],
441 [
'melting_seaice_into_seawater_CT_final',
""],
442 [
'seaice_fraction_to_freeze_seawater_SA_freeze',
""],
443 [
'seaice_fraction_to_freeze_seawater_CT_freeze',
""],
444 [
'seaice_fraction_to_freeze_seawater_w_Ih',
""]
446 rootgrp = Dataset(
'gsw_data_v3_0.nc',
'r') 447 v = rootgrp.variables 448 d = rootgrp.dimensions 450 version_date = rootgrp.version_date 451 version_number = rootgrp.version_number 453 fd = os.open(
"gsw_mod_check_data.f90", os.O_CREAT|os.O_EXCL|os.O_RDWR, 0644)
455 print str(sys.exc_info()[1])
456 print "Will not overwrite gsw_mod_check_data.f90 - exiting." 458 out = os.fdopen(fd,
"w")
462 !** Extracted from gsw_data_v3_0.nc 464 !========================================================================== 465 module gsw_mod_check_data 466 !========================================================================== 474 dim_format =
"integer, parameter :: %s = %d\n" 475 for key, value
in work_dims.items():
477 out.write(dim_format % (value[1],
len(d[key])))
478 out.write(dim_format % (value[2],
len(d[value[0]])))
482 character (50) :: variable_name 483 real (r8) :: computation_accuracy 484 real (r8), dimension(cast_m,cast_n) :: values 488 character (50) :: variable_name 489 real (r8) :: computation_accuracy 490 real (r8), dimension(cast_ice_m,cast_ice_n) :: values 491 end type gsw_result_ice 493 type gsw_result_mpres 494 character (50) :: variable_name 495 real (r8) :: computation_accuracy 496 real (r8), dimension(cast_mpres_m,cast_mpres_n) :: values 497 end type gsw_result_mpres 500 character (50) :: variable_name 501 real (r8) :: computation_accuracy 502 real (r8), dimension(cast_n) :: values 503 end type gsw_result_cast 507 for var_label, var_name
in [var
for var
in work_vars]:
510 dims = [
len(d[dname])
for dname
in v[var_name].dimensions]
513 for var_label, var_name
in [var
for var
in vars]:
516 dims = [
len(d[dname])
for dname
in v[var_name].dimensions]
521 !-------------------------------------------------------------------------- def float2string(val, sformat, addcomma)
def write_variable(var_name, dims, v)
def write_structure(var_name, dims, v)