# Simple netcdf writer xarray compatible

**URL:** <https://fortran-lang.discourse.group/t/simple-netcdf-writer-xarray-compatible/11013>\
**Category:** Uncategorized\
**Created:** [July 22, 2026, 3:09pm UTC](https://fortran-lang.discourse.group/t/simple-netcdf-writer-xarray-compatible/11013 "2026-07-22T15:09:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![egio](https://avatars.discourse-cdn.com/v4/letter/e/dbc845/32.png) [@egio](https://fortran-lang.discourse.group/u/egio)\
**Post date:** [July 22, 2026, 3:09pm UTC](https://fortran-lang.discourse.group/t/simple-netcdf-writer-xarray-compatible/11013/1 "2026-07-22T15:09:13Z")

</div>

I wrote a small library for writing NETCDF files compatible with python xarray library.

> **[GitHub - egiovan/fortxarray: Simple Fortran interface to compatible xarray...](https://github.com/egiovan/fortxarray)**
>
> Simple Fortran interface to compatible xarray netcdf files

It started a few year ago when I decided to write the output of code to NETCDF in a format compatible with xarray so it can be easily read.  
I decided in the last few days to make a full library that is not going to use all the NETCDF possibilities but that would have been as easy to use as possible. I extensively used Google gemini to achieve that.  
The idea is that you can write a netcdf as easily as:

```fortran
program test_simple
  use fortxarray, only: xarray_type
  use iso_fortran_env, only: dp => real64
  implicit none

  integer, parameter :: NP = 5, NX = 3
  type(xarray_type) :: xr
  real(dp) :: z(NP, NX)
  integer :: j, k

  do concurrent(j=1:NP, k=1:NX)
      z(j,k) = j*10 + k
  end do

  call xr%create_netcdf('temporary')
  call xr%add_dimension('t', NP)
  call xr%add_dimension('x', [real(dp) :: 10, 20, 30])

  call xr%add_variable('z', z, ['t','x'])
  call xr%add_variable('zz', z, ['tt', 'xx'], add_dimension=.true.)
  call xr%close_netcdf()

end program test_simple

```

The really bare minimum is:

```fortran
call xr%create_netcdf('temporary')
call xr%add_variable('zz', z, ['tt', 'xx'], add_dimension=.true.)
call xr%close_netcdf()

```

If you don’t care about the dimension being an array of something.

There are routines similar to those to read back the data.
