nfw.f90 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. # 0 "<stdin>"
  2. # 0 "<built-in>"
  3. # 0 "<command-line>"
  4. # 1 "/usr/include/stdc-predef.h" 1 3 4
  5. # 17 "/usr/include/stdc-predef.h" 3 4
  6. # 2 "<command-line>" 2
  7. # 1 "<stdin>"
  8. # 10 "<stdin>"
  9. !
  10. ! File: nfw.f90
  11. !
  12. ! Author: Pavel Sakov, CSIRO Marine Research
  13. !
  14. ! Created: 17 March 2005
  15. !
  16. ! Purpose: Contains wrappers to netcdf functions, mainly for easier
  17. ! error handling.
  18. !
  19. ! Description:
  20. !
  21. ! Each subroutine in nfw.f90 is a simple wrapper of a similar
  22. ! function in the NetCDF Fortran interface. The rules of use are
  23. ! pretty simple: for a given NetCDF Fortran function, replace
  24. ! prefix "nf_" by "nfw_" and add the NetCDF file name as the
  25. ! first argument.
  26. !
  27. ! Here is the current list of subroutines in nfw_mod:
  28. !
  29. ! nfw_create(fname, mode, ncid)
  30. ! nfw_open(fname, mode, ncid)
  31. ! nfw_enddef(fname, ncid)
  32. ! nfw_close(fname, ncid)
  33. ! nfw_inq_unlimdim(fname, ncid, unlimdimid)
  34. ! nfw_inq_dimid(fname, ncid, name, dimid)
  35. ! nfw_inq_dimlen(fname, ncid, dimid, length)
  36. ! nfw_def_dim(fname, ncid, name, length, dimid)
  37. ! nfw_def_var(fname, ncid, name, type, ndims, dimids, varid)
  38. ! nfw_inq_varid(fname, ncid, name, varid)
  39. ! nfw_inq_varname(fname, ncid, varid, name)
  40. ! nfw_inq_varndims(fname, ncid, varid, ndims)
  41. ! nfw_inq_vardimid(fname, ncid, varid, dimids)
  42. ! nfw_rename_var(fname, ncid, oldname, newname)
  43. ! nfw_put_var_int(fname, ncid, varid, v)
  44. ! nfw_put_var_double(fname, ncid, varid, v)
  45. ! nfw_put_var_real(fname, ncid, varid, v)
  46. ! nfw_get_var_int(fname, ncid, varid, v)
  47. ! nfw_get_var_double(fname, ncid, varid, v)
  48. ! nfw_put_vara_int(fname, ncid, varid, start, length, v)
  49. ! nfw_put_vara_double(fname, ncid, varid, start, length, v)
  50. ! nfw_get_vara_int(fname, ncid, varid, start, length, v)
  51. ! nfw_get_vara_double(fname, ncid, varid, start, length, v)
  52. ! nfw_get_att_int(fname, ncid, varid, attname, v)
  53. ! nfw_get_att_real(fname, ncid, varid, attname, v)
  54. ! nfw_get_att_double(fname, ncid, varid, attname, v)
  55. ! nfw_put_att_text(fname, ncid, varid, attname, length, text)
  56. ! nfw_put_att_int(fname, ncid, varid, attname, type, length, v)
  57. ! nfw_put_att_real(fname, ncid, varid, attname, type, length, v)
  58. ! nfw_put_att_double(fname, ncid, varid, attname, type, length, v)
  59. !
  60. ! Derived procedures:
  61. !
  62. ! nfw_get_var_double_firstrecord(fname, ncid, varid, v)
  63. ! nfw_var_exists(ncid, name)
  64. ! nfw_dim_exists(ncid, name)
  65. ! Modifications:
  66. !
  67. ! 29/04/2008 PS: added nfw_rename_var(fname, ncid, oldname, newname)
  68. ! 21/10/2009 PS: added nfw_var_exists(ncid, name)
  69. ! 22/10/2009 PS: added nfw_put_att_double(fname, ncid, varid, attname, type,
  70. ! length, v)
  71. ! 06/11/2009 PS: added nfw_dim_exists(ncid, name)
  72. ! nfw_put_att_real(fname, ncid, varid, attname, type, length, v)
  73. ! nfw_get_att_real(fname, ncid, varid, attname, v)
  74. module nfw_mod
  75. implicit none
  76. include 'netcdf.inc'
  77. character(*), private, parameter :: nfw_version = "0.03"
  78. integer, private, parameter :: logunit = 6
  79. character(*), private, parameter :: errprefix = "nfw: error: "
  80. private quit1, quit2, quit3
  81. contains
  82. ! Common exit point -- for the sake of debugging
  83. subroutine quit
  84. stop
  85. end subroutine quit
  86. subroutine quit1(fname, procname, status)
  87. character*(*), intent(in) :: fname
  88. character*(*), intent(in) :: procname
  89. integer, intent(in) :: status
  90. write(logunit, *)
  91. write(logunit, *) errprefix, '"', trim(fname), '": ', procname, '(): ',&
  92. nf_strerror(status)
  93. call flush(logunit)
  94. call quit
  95. end subroutine quit1
  96. subroutine quit2(fname, procname, name, status)
  97. character*(*), intent(in) :: fname
  98. character*(*), intent(in) :: procname
  99. character*(*), intent(in) :: name
  100. integer, intent(in) :: status
  101. write(logunit, *)
  102. write(logunit, *) errprefix, '"', trim(fname), '": ', procname, '(): "',&
  103. trim(name), '": ', nf_strerror(status)
  104. call flush(logunit)
  105. call quit
  106. end subroutine quit2
  107. subroutine quit3(fname, procname, name1, name2, status)
  108. character*(*), intent(in) :: fname
  109. character*(*), intent(in) :: procname
  110. character*(*), intent(in) :: name1
  111. character*(*), intent(in) :: name2
  112. integer, intent(in) :: status
  113. write(logunit, *)
  114. write(logunit, *) errprefix, '"', trim(fname), '": ', procname, '(): "',&
  115. trim(name1), '": "', trim(name2), '": ', nf_strerror(status)
  116. call flush(logunit)
  117. call quit
  118. end subroutine quit3
  119. subroutine nfw_create(fname, mode, ncid)
  120. character*(*), intent(in) :: fname
  121. integer, intent(in) :: mode
  122. integer, intent(out) :: ncid
  123. integer :: status
  124. status = nf_create(trim(fname), mode, ncid)
  125. if (status /= 0) call quit1(fname, 'nf_create', status)
  126. end subroutine nfw_create
  127. subroutine nfw_open(fname, mode, ncid)
  128. character*(*), intent(in) :: fname
  129. integer, intent(in) :: mode
  130. integer, intent(out) :: ncid
  131. integer :: status
  132. status = nf_open(trim(fname), mode, ncid)
  133. if (status /= 0) call quit1(fname, 'nf_open', status)
  134. end subroutine nfw_open
  135. subroutine nfw_enddef(fname, ncid)
  136. character*(*), intent(in) :: fname
  137. integer, intent(in) :: ncid
  138. integer :: status
  139. status = nf_enddef(ncid)
  140. if (status /= 0) call quit1(fname, 'nf_enddef', status)
  141. end subroutine nfw_enddef
  142. subroutine nfw_redef(fname, ncid)
  143. character*(*), intent(in) :: fname
  144. integer, intent(in) :: ncid
  145. integer :: status
  146. status = nf_redef(ncid)
  147. if (status /= 0) call quit1(fname, 'nf_redef', status)
  148. end subroutine nfw_redef
  149. subroutine nfw_close(fname, ncid)
  150. character*(*), intent(in) :: fname
  151. integer, intent(in) :: ncid
  152. integer :: status
  153. status = nf_close(ncid)
  154. if (status /= 0) call quit1(fname, 'nf_close', status)
  155. end subroutine nfw_close
  156. subroutine nfw_inq_unlimdim(fname, ncid, unlimdimid)
  157. character*(*), intent(in) :: fname
  158. integer, intent(in) :: ncid
  159. integer, intent(out) :: unlimdimid
  160. integer :: status
  161. status = nf_inq_unlimdim(ncid, unlimdimid)
  162. if (status /= 0) call quit1(fname, 'nf_inq_unlimdimid', status)
  163. end subroutine nfw_inq_unlimdim
  164. subroutine nfw_inq_dimid(fname, ncid, name, dimid)
  165. character*(*), intent(in) :: fname
  166. integer, intent(in) :: ncid
  167. character*(*), intent(in) :: name
  168. integer, intent(out) :: dimid
  169. integer :: status
  170. status = nf_inq_dimid(ncid, trim(name), dimid)
  171. if (status /= 0) call quit2(fname, 'nf_inq_dimid', name, status)
  172. end subroutine nfw_inq_dimid
  173. subroutine nfw_inq_dimlen(fname, ncid, dimid, length)
  174. character*(*), intent(in) :: fname
  175. integer, intent(in) :: ncid
  176. integer, intent(in) :: dimid
  177. integer, intent(out) :: length
  178. integer :: status
  179. status = nf_inq_dimlen(ncid, dimid, length)
  180. if (status /= 0) call quit1(fname, 'nf_inq_dimlen', status)
  181. end subroutine nfw_inq_dimlen
  182. subroutine nfw_def_dim(fname, ncid, name, length, dimid)
  183. character*(*), intent(in) :: fname
  184. integer, intent(in) :: ncid
  185. character*(*), intent(in) :: name
  186. integer, intent(in) :: length
  187. integer, intent(out) :: dimid
  188. integer :: status
  189. status = nf_def_dim(ncid, name, length, dimid)
  190. if (status /= 0) call quit2(fname, 'nf_def_dim', name, status)
  191. end subroutine nfw_def_dim
  192. subroutine nfw_def_var(fname, ncid, name, type, ndims, dimids, varid)
  193. character*(*), intent(in) :: fname
  194. integer, intent(in) :: ncid
  195. character*(*), intent(in) :: name
  196. integer, intent(in) :: type
  197. integer, intent(in) :: ndims
  198. integer, intent(in) :: dimids(*)
  199. integer, intent(out) :: varid
  200. integer :: status
  201. status = nf_def_var(ncid, name, type, ndims, dimids, varid)
  202. if (status /= 0) call quit2(fname, 'nf_def_var', name, status)
  203. end subroutine nfw_def_var
  204. subroutine nfw_inq_varid(fname, ncid, name, varid)
  205. character*(*), intent(in) :: fname
  206. integer, intent(in) :: ncid
  207. character*(*), intent(in) :: name
  208. integer, intent(out) :: varid
  209. integer :: status
  210. status = nf_inq_varid(ncid, trim(name), varid)
  211. if (status /= 0) call quit2(fname, 'nf_inq_varid', name, status)
  212. end subroutine nfw_inq_varid
  213. subroutine nfw_inq_varname(fname, ncid, varid, name)
  214. character*(*), intent(in) :: fname
  215. integer, intent(in) :: ncid
  216. integer, intent(in) :: varid
  217. character*(*), intent(out) :: name
  218. integer :: status
  219. status = nf_inq_varname(ncid, varid, name)
  220. if (status /= 0) call quit1(fname, 'nf_inq_varname', status)
  221. end subroutine nfw_inq_varname
  222. subroutine nfw_inq_varndims(fname, ncid, varid, ndims)
  223. character*(*), intent(in) :: fname
  224. integer, intent(in) :: ncid
  225. integer, intent(in) :: varid
  226. integer, intent(out) :: ndims
  227. character*(NF_MAX_NAME) :: name
  228. integer :: status
  229. status = nf_inq_varndims(ncid, varid, ndims)
  230. if (status /= 0) then
  231. call nfw_inq_varname(fname, ncid, varid, name)
  232. call quit2(fname, 'nf_inq_varndims', name, status)
  233. end if
  234. end subroutine nfw_inq_varndims
  235. subroutine nfw_inq_vardimid(fname, ncid, varid, dimids)
  236. character*(*), intent(in) :: fname
  237. integer, intent(in) :: ncid
  238. integer, intent(in) :: varid
  239. integer, intent(out) :: dimids(*)
  240. character*(NF_MAX_NAME) :: name
  241. integer :: status
  242. status = nf_inq_vardimid(ncid, varid, dimids)
  243. if (status /= 0) then
  244. call nfw_inq_varname(fname, ncid, varid, name)
  245. call quit2(fname, 'nf_inq_vardimid', name, status)
  246. end if
  247. end subroutine nfw_inq_vardimid
  248. subroutine nfw_rename_var(fname, ncid, oldname, newname)
  249. character*(*), intent(in) :: fname
  250. integer, intent(in) :: ncid
  251. character*(*), intent(in) :: oldname
  252. character*(*), intent(in) :: newname
  253. integer :: varid
  254. integer :: status
  255. call nfw_inq_varid(fname, ncid, oldname, varid)
  256. status = nf_rename_var(ncid, varid, newname)
  257. if (status /= 0) then
  258. call quit2(fname, 'nf_rename_var', oldname, status)
  259. end if
  260. end subroutine nfw_rename_var
  261. subroutine nfw_put_var_int(fname, ncid, varid, v)
  262. character*(*), intent(in) :: fname
  263. integer, intent(in) :: ncid
  264. integer, intent(in) :: varid
  265. integer, intent(in) :: v(*)
  266. character*(NF_MAX_NAME) :: name
  267. integer :: status
  268. status = nf_put_var_int(ncid, varid, v)
  269. if (status /= 0) then
  270. call nfw_inq_varname(fname, ncid, varid, name)
  271. call quit2(fname, 'nf_put_var_double', name, status)
  272. end if
  273. end subroutine nfw_put_var_int
  274. subroutine nfw_put_var_double(fname, ncid, varid, v)
  275. character*(*), intent(in) :: fname
  276. integer, intent(in) :: ncid
  277. integer, intent(in) :: varid
  278. real(8), intent(in) :: v(*)
  279. character*(NF_MAX_NAME) :: name
  280. integer :: status
  281. status = nf_put_var_double(ncid, varid, v)
  282. if (status /= 0) then
  283. call nfw_inq_varname(fname, ncid, varid, name)
  284. call quit2(fname, 'nf_put_var_double', name, status)
  285. end if
  286. end subroutine nfw_put_var_double
  287. subroutine nfw_put_var_real(fname, ncid, varid, v)
  288. character*(*), intent(in) :: fname
  289. integer, intent(in) :: ncid
  290. integer, intent(in) :: varid
  291. real(4), intent(in) :: v(*)
  292. character*(NF_MAX_NAME) :: name
  293. integer :: status
  294. status = nf_put_var_real(ncid, varid, v)
  295. if (status /= 0) then
  296. call nfw_inq_varname(fname, ncid, varid, name)
  297. call quit2(fname, 'nf_put_var_real', name, status)
  298. end if
  299. end subroutine nfw_put_var_real
  300. subroutine nfw_get_var_int(fname, ncid, varid, v)
  301. character*(*), intent(in) :: fname
  302. integer, intent(in) :: ncid
  303. integer, intent(in) :: varid
  304. integer, intent(out) :: v(*)
  305. character*(NF_MAX_NAME) :: name
  306. integer :: status
  307. status = nf_get_var_int(ncid, varid, v)
  308. if (status /= 0) then
  309. call nfw_inq_varname(fname, ncid, varid, name)
  310. call quit2(fname, 'nf_get_var_int', name, status)
  311. end if
  312. end subroutine nfw_get_var_int
  313. subroutine nfw_get_var_double(fname, ncid, varid, v)
  314. character*(*), intent(in) :: fname
  315. integer, intent(in) :: ncid
  316. integer, intent(in) :: varid
  317. real(8), intent(out) :: v(*)
  318. character*(NF_MAX_NAME) :: name
  319. integer :: status
  320. status = nf_get_var_double(ncid, varid, v)
  321. if (status /= 0) then
  322. call nfw_inq_varname(fname, ncid, varid, name)
  323. call quit2(fname, 'nf_get_var_double', name, status)
  324. end if
  325. end subroutine nfw_get_var_double
  326. subroutine nfw_get_var_text(fname, ncid, varid, v)
  327. character*(*), intent(in) :: fname
  328. integer, intent(in) :: ncid
  329. integer, intent(in) :: varid
  330. character, intent(out) :: v(*)
  331. character*(NF_MAX_NAME) :: name
  332. integer :: status
  333. status = nf_get_var_text(ncid, varid, v)
  334. if (status /= 0) then
  335. call nfw_inq_varname(fname, ncid, varid, name)
  336. call quit2(fname, 'nf_get_var_int', name, status)
  337. end if
  338. end subroutine nfw_get_var_text
  339. subroutine nfw_put_vara_int(fname, ncid, varid, start, length, v)
  340. character*(*), intent(in) :: fname
  341. integer, intent(in) :: ncid
  342. integer, intent(in) :: varid
  343. integer, intent(in) :: start(*)
  344. integer, intent(in) :: length(*)
  345. integer, intent(in) :: v(*)
  346. character*(NF_MAX_NAME) :: name
  347. integer :: status
  348. status = nf_put_vara_int(ncid, varid, start, length, v)
  349. if (status /= 0) then
  350. call nfw_inq_varname(fname, ncid, varid, name)
  351. call quit2(fname, 'nf_put_vara_int', name, status)
  352. end if
  353. end subroutine nfw_put_vara_int
  354. subroutine nfw_put_vara_double(fname, ncid, varid, start, length, v)
  355. character*(*), intent(in) :: fname
  356. integer, intent(in) :: ncid
  357. integer, intent(in) :: varid
  358. integer, intent(in) :: start(*)
  359. integer, intent(in) :: length(*)
  360. real(8), intent(in) :: v(*)
  361. character*(NF_MAX_NAME) :: name
  362. integer :: status
  363. status = nf_put_vara_double(ncid, varid, start, length, v)
  364. if (status /= 0) then
  365. call nfw_inq_varname(fname, ncid, varid, name)
  366. call quit2(fname, 'nf_put_vara_double', name, status)
  367. end if
  368. end subroutine nfw_put_vara_double
  369. subroutine nfw_get_vara_int(fname, ncid, varid, start, length, v)
  370. character*(*), intent(in) :: fname
  371. integer, intent(in) :: ncid
  372. integer, intent(in) :: varid
  373. integer, intent(in) :: start(*)
  374. integer, intent(in) :: length(*)
  375. integer, intent(out) :: v(*)
  376. character*(NF_MAX_NAME) :: name
  377. integer :: status
  378. status = nf_get_vara_int(ncid, varid, start, length, v)
  379. if (status /= 0) then
  380. call nfw_inq_varname(fname, ncid, varid, name)
  381. call quit2(fname, 'nf_get_vara_int', name, status)
  382. end if
  383. end subroutine nfw_get_vara_int
  384. subroutine nfw_get_vara_double(fname, ncid, varid, start, length, v)
  385. character*(*), intent(in) :: fname
  386. integer, intent(in) :: ncid
  387. integer, intent(in) :: varid
  388. integer, intent(in) :: start(*)
  389. integer, intent(in) :: length(*)
  390. real(8), intent(out) :: v(*)
  391. character*(NF_MAX_NAME) :: name
  392. integer :: status
  393. status = nf_get_vara_double(ncid, varid, start, length, v)
  394. if (status /= 0) then
  395. call nfw_inq_varname(fname, ncid, varid, name)
  396. call quit2(fname, 'nf_get_vara_double', name, status)
  397. end if
  398. end subroutine nfw_get_vara_double
  399. subroutine nfw_get_att_int(fname, ncid, varid, attname, v)
  400. character*(*), intent(in) :: fname
  401. integer, intent(in) :: ncid
  402. integer, intent(in) :: varid
  403. character*(*), intent(in) :: attname
  404. integer, intent(out) :: v(*)
  405. character*(NF_MAX_NAME) :: varname
  406. integer :: status
  407. status = nf_get_att_int(ncid, varid, attname, v)
  408. if (status /= 0) then
  409. if (varid /= nf_global) then
  410. call nfw_inq_varname(fname, ncid, varid, varname)
  411. else
  412. varname = 'NF_GLOBAL'
  413. end if
  414. call quit3(fname, 'nf_get_att_int', varname, attname, status)
  415. end if
  416. end subroutine nfw_get_att_int
  417. subroutine nfw_get_att_real(fname, ncid, varid, attname, v)
  418. character*(*), intent(in) :: fname
  419. integer, intent(in) :: ncid
  420. integer, intent(in) :: varid
  421. character*(*), intent(in) :: attname
  422. real(4), intent(out) :: v(*)
  423. character*(NF_MAX_NAME) :: varname
  424. integer :: status
  425. status = nf_get_att_real(ncid, varid, attname, v)
  426. if (status /= 0) then
  427. if (varid /= nf_global) then
  428. call nfw_inq_varname(fname, ncid, varid, varname)
  429. else
  430. varname = 'NF_GLOBAL'
  431. end if
  432. call quit3(fname, 'nf_get_att_real', varname, attname, status)
  433. end if
  434. end subroutine nfw_get_att_real
  435. subroutine nfw_get_att_double(fname, ncid, varid, attname, v)
  436. character*(*), intent(in) :: fname
  437. integer, intent(in) :: ncid
  438. integer, intent(in) :: varid
  439. character*(*), intent(in) :: attname
  440. real(8), intent(out) :: v(*)
  441. character*(NF_MAX_NAME) :: varname
  442. integer :: status
  443. status = nf_get_att_double(ncid, varid, attname, v)
  444. if (status /= 0) then
  445. if (varid /= nf_global) then
  446. call nfw_inq_varname(fname, ncid, varid, varname)
  447. else
  448. varname = 'NF_GLOBAL'
  449. end if
  450. call quit3(fname, 'nf_get_att_double', varname, attname, status)
  451. end if
  452. end subroutine nfw_get_att_double
  453. subroutine nfw_put_att_text(fname, ncid, varid, attname, length, text)
  454. character*(*), intent(in) :: fname
  455. integer, intent(in) :: ncid
  456. integer, intent(in) :: varid
  457. character*(*), intent(in) :: attname
  458. integer, intent(in) :: length
  459. character*(*), intent(in) :: text
  460. integer :: status
  461. character*(NF_MAX_NAME) :: varname
  462. status = nf_put_att_text(ncid, varid, attname, length, trim(text))
  463. if (status /= 0) then
  464. if (varid /= nf_global) then
  465. call nfw_inq_varname(fname, ncid, varid, varname)
  466. else
  467. varname = 'NF_GLOBAL'
  468. end if
  469. call quit3(fname, 'nf_put_att_text', varname, attname, status)
  470. end if
  471. end subroutine nfw_put_att_text
  472. subroutine nfw_put_att_int(fname, ncid, varid, attname, type, length, v)
  473. character*(*), intent(in) :: fname
  474. integer, intent(in) :: ncid
  475. integer, intent(in) :: varid
  476. character*(*), intent(in) :: attname
  477. integer, intent(in) :: type
  478. integer, intent(in) :: length
  479. integer, intent(in) :: v(*)
  480. integer :: status
  481. character*(NF_MAX_NAME) :: varname
  482. status = nf_put_att_int(ncid, varid, attname, type, length, v)
  483. if (status /= 0) then
  484. if (varid /= nf_global) then
  485. call nfw_inq_varname(fname, ncid, varid, varname)
  486. else
  487. varname = 'NF_GLOBAL'
  488. end if
  489. call quit3(fname, 'nf_put_att_int', varname, attname, status)
  490. end if
  491. end subroutine nfw_put_att_int
  492. subroutine nfw_put_att_real(fname, ncid, varid, attname, type, length, v)
  493. character*(*), intent(in) :: fname
  494. integer, intent(in) :: ncid
  495. integer, intent(in) :: varid
  496. character*(*), intent(in) :: attname
  497. integer, intent(in) :: type
  498. integer, intent(in) :: length
  499. real(4), intent(in) :: v(*)
  500. integer :: status
  501. character*(NF_MAX_NAME) :: varname
  502. status = nf_put_att_real(ncid, varid, attname, type, length, v)
  503. if (status /= 0) then
  504. if (varid /= nf_global) then
  505. call nfw_inq_varname(fname, ncid, varid, varname)
  506. else
  507. varname = 'NF_GLOBAL'
  508. end if
  509. call quit3(fname, 'nf_put_att_real', varname, attname, status)
  510. end if
  511. end subroutine nfw_put_att_real
  512. subroutine nfw_put_att_double(fname, ncid, varid, attname, type, length, v)
  513. character*(*), intent(in) :: fname
  514. integer, intent(in) :: ncid
  515. integer, intent(in) :: varid
  516. character*(*), intent(in) :: attname
  517. integer, intent(in) :: type
  518. integer, intent(in) :: length
  519. real(8), intent(in) :: v(*)
  520. integer :: status
  521. character*(NF_MAX_NAME) :: varname
  522. status = nf_put_att_double(ncid, varid, attname, type, length, v)
  523. if (status /= 0) then
  524. if (varid /= nf_global) then
  525. call nfw_inq_varname(fname, ncid, varid, varname)
  526. else
  527. varname = 'NF_GLOBAL'
  528. end if
  529. call quit3(fname, 'nf_put_att_double', varname, attname, status)
  530. end if
  531. end subroutine nfw_put_att_double
  532. ! Derived subroutines
  533. ! Reads the first record only
  534. subroutine nfw_get_var_double_firstrecord(fname, ncid, varid, v)
  535. character*(*), intent(in) :: fname
  536. integer, intent(in) :: ncid
  537. integer, intent(in) :: varid
  538. real(8), intent(out) :: v(*)
  539. integer :: ndims
  540. integer :: unlimdimid
  541. integer :: dimids(NF_MAX_VAR_DIMS)
  542. integer :: dimlen(NF_MAX_VAR_DIMS)
  543. integer :: dstart(NF_MAX_VAR_DIMS)
  544. integer :: i
  545. character*(NF_MAX_NAME) :: name
  546. integer :: status
  547. call nfw_inq_varndims(fname, ncid, varid, ndims)
  548. call nfw_inq_vardimid(fname, ncid, varid, dimids)
  549. call nfw_inq_unlimdim(fname, ncid, unlimdimid)
  550. do i = 1, ndims
  551. call nfw_inq_dimlen(fname, ncid, dimids(i), dimlen(i))
  552. dstart(i) = 1
  553. end do
  554. ! check size of v
  555. if (dimids(ndims) == unlimdimid) then
  556. dimlen(ndims) = 1 ! 1 record only
  557. end if
  558. status = nf_get_vara_double(ncid, varid, dstart, dimlen, v)
  559. if (status /= 0) then
  560. call nfw_inq_varname(fname, ncid, varid, name)
  561. call quit2(fname, 'nf_get_vara_double', name, status)
  562. end if
  563. end subroutine nfw_get_var_double_firstrecord
  564. logical function nfw_var_exists(ncid, name)
  565. integer, intent(in) :: ncid
  566. character*(*), intent(in) :: name
  567. integer :: varid
  568. integer :: status
  569. status = nf_inq_varid(ncid, trim(name), varid)
  570. nfw_var_exists = (status == 0)
  571. end function nfw_var_exists
  572. logical function nfw_dim_exists(ncid, name)
  573. integer, intent(in) :: ncid
  574. character*(*), intent(in) :: name
  575. integer :: dimid
  576. integer :: status
  577. status = nf_inq_dimid(ncid, trim(name), dimid)
  578. nfw_dim_exists = (status == 0)
  579. end function nfw_dim_exists
  580. end module nfw_mod