From 36d84147fd21f83118167ca20affddca77163952 Mon Sep 17 00:00:00 2001 From: Ramil <ramil.rgk@gmail.com> Date: Thu, 6 Jun 2024 16:06:55 +0300 Subject: [PATCH] netcdf_io_module.f90 init. Series recording functionality --- .idea/.gitignore | 8 ++++++ .idea/misc.xml | 4 +-- .idea/modules.xml | 8 ++++++ .idea/scm-io.iml | 2 ++ src/main.f90 | 15 ++++++++++ src/netcdf_io_module.f90 | 61 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/modules.xml create mode 100644 .idea/scm-io.iml create mode 100644 src/main.f90 create mode 100644 src/netcdf_io_module.f90 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index 8cb85cd..79b3c94 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,4 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> - <component name="CMakeWorkspace"> - <contentRoot DIR="$PROJECT_DIR$" /> - </component> + <component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" /> </project> \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..3ead05b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectModuleManager"> + <modules> + <module fileurl="file://$PROJECT_DIR$/.idea/scm-io.iml" filepath="$PROJECT_DIR$/.idea/scm-io.iml" /> + </modules> + </component> +</project> \ No newline at end of file diff --git a/.idea/scm-io.iml b/.idea/scm-io.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/.idea/scm-io.iml @@ -0,0 +1,2 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module classpath="CMake" type="CPP_MODULE" version="4" /> \ No newline at end of file diff --git a/src/main.f90 b/src/main.f90 new file mode 100644 index 0000000..062885d --- /dev/null +++ b/src/main.f90 @@ -0,0 +1,15 @@ +program main + use netcdf_io_module + implicit none + type(io_struct) :: ios + real, dimension(10) :: time_data = (/1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0/) + real, dimension(10) :: series_data = (/10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0/) + + call open_netcdf('series_data.nc', ios, 10) + if (ios%is_open) then + call write_series(ios, time_data, series_data) + call close_netcdf(ios) + else + print *, 'Failed to open NetCDF file.' + endif +end program main diff --git a/src/netcdf_io_module.f90 b/src/netcdf_io_module.f90 new file mode 100644 index 0000000..2a46718 --- /dev/null +++ b/src/netcdf_io_module.f90 @@ -0,0 +1,61 @@ +module netcdf_io_module + use netcdf + implicit none + + type :: io_struct + integer :: ncid + integer :: time_dimid, varid_time, varid_series + logical :: is_open = .false. + character(len=128) :: series_name = 'timeseries of variable' + character(len=128) :: series_long_name = 'timeseries of some variable' + character(len=128) :: series_units = 'unit' + end type io_struct + +contains + subroutine open_netcdf(filename, ios, time_len) + character(len=*), intent(in) :: filename + type(io_struct), intent(out) :: ios + integer, intent(in) :: time_len + integer :: ierr + + ierr = nf90_create(filename, nf90_clobber, ios%ncid) + if (ierr == nf90_noerr) then + ios%is_open = .true. + ierr = nf90_def_dim(ios%ncid, 'time', time_len, ios%time_dimid) + ierr = nf90_def_var(ios%ncid, 'time', nf90_float, ios%time_dimid, ios%varid_time) + ierr = nf90_def_var(ios%ncid, 'series', nf90_float, ios%time_dimid, ios%varid_series) + ierr = nf90_put_att(ios%ncid, ios%varid_series, 'standard_name', ios%series_name) + ierr = nf90_put_att(ios%ncid, ios%varid_series, 'long_name', ios%series_long_name) + ierr = nf90_put_att(ios%ncid, ios%varid_series, 'units', ios%series_units) + ierr = nf90_enddef(ios%ncid) + else + print *, 'Error opening file: ', ierr + ios%is_open = .false. + endif + end subroutine open_netcdf + + subroutine write_series(ios, time_data, series_data) + type(io_struct), intent(in) :: ios + real, dimension(:), intent(in) :: time_data, series_data + integer :: ierr + + if (.not. ios%is_open) then + print *, "File is not open." + return + endif + + ierr = nf90_put_var(ios%ncid, ios%varid_time, time_data) + ierr = nf90_put_var(ios%ncid, ios%varid_series, series_data) + if (ierr /= nf90_noerr) then + print *, 'Error writing data:', ierr + endif + end subroutine write_series + + subroutine close_netcdf(ios) + type(io_struct), intent(inout) :: ios + integer :: ierr + + ierr = nf90_close(ios%ncid) + ios%is_open = .false. + end subroutine close_netcdf +end module netcdf_io_module -- GitLab