{"id":22752417,"url":"https://github.com/perrette/nml-to-f90","last_synced_at":"2025-04-14T14:20:46.175Z","repository":{"id":29053582,"uuid":"32581306","full_name":"perrette/nml-to-f90","owner":"perrette","description":"Generate fortran I/O source code from a namelist","archived":false,"fork":false,"pushed_at":"2017-01-01T17:48:12.000Z","size":207,"stargazers_count":9,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-28T03:14:53.429Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/perrette.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-03-20T11:58:48.000Z","updated_at":"2023-08-20T02:39:28.000Z","dependencies_parsed_at":"2022-07-21T03:18:52.417Z","dependency_job_id":null,"html_url":"https://github.com/perrette/nml-to-f90","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/perrette%2Fnml-to-f90","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/perrette%2Fnml-to-f90/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/perrette%2Fnml-to-f90/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/perrette%2Fnml-to-f90/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/perrette","download_url":"https://codeload.github.com/perrette/nml-to-f90/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248894939,"owners_count":21179154,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-11T05:11:41.717Z","updated_at":"2025-04-14T14:20:46.145Z","avatar_url":"https://github.com/perrette.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nml-to-f90\n\nGenerate fortran source code for handling parameter I/O from a namelist\n\n    nml2f90 namelist.nml --io-nml --command-line\n\nThis will create ioparams.f90, with corresponding derived types, \nnamelist I/O and command-line subroutines.\nTo be imported in any other program as follow (you need to compile \nioparams.f90 of course, e.g. see **compilation** below)\n\n    use ioparams, only: group1_t, group2_t\n    use ioparams, only: read_nml, write_nml, parse_command_args\n\nwhere `\u003cgroup\u003e_t` are newly defined types created from namelist groups\nand the four subroutines are handy I/O and set/get interfaces for the\nabove defined types.\n\nIn its simplest form, this would be:\n\n    use ioparams\n\n    implicit none\n\n    type(group1_t) :: par1\n    integer :: iounit=88, iostat\n    character(len=50), dimension(:), allocatable :: args\n\n    ! read namelist\n    open(iounit, file=\"namelist.nml\")\n    call read_nml(iounit, par1)\n    close(iounit)\n\n    ! parse command-line arguments and stop in case of error or with --help\n    call command_argument_as_array(args)\n    call parse_command_args(par1, args=args, unmatched=args, stop_on_help=.true.)\n\n    if (size(unmatched) \u003e 0) then\n        write(*,*) \"Some arguments were not matched:\"\n        write(*,*) trim(join_array(unmatched))\n        stop\n    endif\n    \n\n\n__NOTE__: the script interface will be simplified soon (see [issue #1](https://github.com/perrette/nml-to-f90/issues/1))\n\nSee example.f90 for an interactive variant of this snippet, and and test.f90 \nfor a more complete suite of tests for various functions in ioparams.\n\nGenerate source code and compile the example program in one go:\n\n    make clean src example\n\n    ./example.x -h  # to try it out\n\nHave a look at ioparams.f90 in the repo to get an impression of the generated code.\n\nGet all options of nml2f90 by typing:\n\n    nml2f90 --help  \n\n## Parse fortran source code for more precise results\n\nThey are a couple of caveats with the basic namelist to fortran conversion:\n\n- character strings all have the same length (default to 256)\n- certain types may be lost via conversion to python (e.g. all \"real\" converted to double precision)\n  or simply more error-prone procedure (say a '.' is missing, but you did mean a float)\n\nSo I found for myself more re-insuring to have the variables defined in my regular \nfortran source code, and tell nml2f90 to just look into the source code (parse it) \nto retrieve the actual type, via the `--src` argument:\n\n    nml2f90 namelist.nml --src src/*f90 --io-nml\n    \nNote the types found in the source code will simply be imported `use \u003cmod\u003e, only:\u003ctype\u003e`\nand not redefined in ioparams.f90. You will probably need to provide\nmore information concerning the naming conventions for mapping between\ngroup (namelist blocks) and type names. See options `--type-prefix`, `--type-suffix`\nand `--type-map` (`nml2f90 -h` for help).\n\nYou may also want to create interfaces for other types not defined \nin the namelist. Use `--include-groups` for that.\n\n## Use nml.f90 to make the code more re-usable and more robust\n\nThe --io-nml option creates type-specific interfaces to read from and\nwrite to namelist format using the built-in namelist capability.\n\nA caveat is that it requires determining the type of the various \nvariables when creating the routines (have a look at the generated \nioparams.f90), which may be error prone (see parsing of source code \nfor a solution) and generates much boiler-plate code. The second\nreason is of greater concern IMO, because it makes the generated\ncode harder to understand (not too big of an issue as long as \nnml2f90 is around, or as long as there is no bug to be tracked down!).\n\nAn elegant solution is offered by Alex Robinson's [nml.f90](https://github.com/alex-robinson/nml)\nlibrary, which parses (read-only) the namelist and allows calls like\n\n    call nml_read(filename,\"group1\",\"name1\",group1%name1)\n\nPassing the option `--io-nml-nml` will make nml2f90 use that\nkind of calls internally instead of fortran build-in `namelist` solution.\nIt assumes nml.f90 is present on the compilation path. That way\nnml2f90 becomes no more than a nice helper to avoid writing repetitive code, \nbut it involves no commitment in the future...(as long as you keep nml.f90 \naround of course !)\nAnd the command-line argument parsing for whole type of course, which \nremains a useful feature.  This feature is still a work-in-progress.\n\nNote this concerns ioparams.f90's internals and does not change the way you use \nioparams' `read_nml`, `parse_command_args`, `print_help`. For now, \nthe `write_nml` is not available with this option.\n\nIf`--io-nml-nml` is passed, there is no need to indicate `--io-nml`.\n\n## Summary of advices for sustainable using\n\nThe safer way of using nml2f90 is probably:\n\n    nml2f90 namelist.nml --src src/*f90 --io-nml-nml --command-line\n\nand make sure [nml.f90](https://github.com/alex-robinson/nml) is compiled before ioparams.f90\n\n## Caveats\n\n- each namelist block must be defined in the same fortran type, i.e. one type per block.\n- as a corrolary, no derived types nor portions of array can be present in the \n  namelist with the % syntax \n\n## Install (with administrator rights)\n\nClone this repository, and at the root execute:\n\npython setup.py install\n\nThis will install nml2f90 module (along with handly nml2f90.namelist)\nand nml2f90 as an alias for python -m nml2f90.nml2f90\n\n## Compilation\n\nAfter generating the source code by calling nml2f90, just put ioparam.f90 \ninto your code directory and compile your main program with:\n\n    gfortran -o example.x ioparams.f90 example.f90\n\nThat's it !\n\n## Credits\n\nThanks to Alex Robinson and its nml project https://github.com/alex-robinson/nml\nfor inspiration and some pieces of code (e.g. parse vector string) test namelist file). \nHave a look at it for an alternative approach to generic parameters I/O.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperrette%2Fnml-to-f90","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fperrette%2Fnml-to-f90","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperrette%2Fnml-to-f90/lists"}