2

Hi I have a CSV file that is for several monitoring stations that have measured isotope data repeatedly over a period of time.

     Country Latitude Longitude  Altitude  Sample Name        Date Begin of Period End of Period      H2 Precipitation  begin year  end year  Dif year  begin month  end month  begin days  end days  Days  Year  month      dates
5         DE  511.622   149.506       238       199706  15.06.1997      01.06.1997    30.06.1997   -71.7          74.3        1997      1997         0            1          6           6        30    24  1997      6 1997-06-15
6         DE  511.622   149.506       238       199707  15.07.1997      01.07.1997    31.07.1997   -70.1         171.7        1997      1997         0            1          7           7        31    24  1997      7 1997-07-15
7         DE  511.622   149.506       238       199708  15.08.1997      01.08.1997    31.08.1997   -64.5          57.5        1997      1997         0            1          8           8        31    23  1997      8 1997-08-15
8         DE  511.622   149.506       238       199709  15.09.1997      01.09.1997    30.09.1997   -39.1          37.9        1997      1997         0            1          9           9        30    21  1997      9 1997-09-15
9         DE  511.622   149.506       238       199710  15.10.1997      01.10.1997    31.10.1997   -56.4          68.2        1997      1997         0            1         10          10        31    21  1997     10 1997-10-15
...      ...      ...       ...       ...          ...         ...             ...           ...     ...           ...         ...       ...       ...          ...        ...         ...       ...   ...   ...    ...        ...
4995      DE  490.422   121.019       365       201304  15.04.2013      01.04.2013    30.04.2013  -41.86          35.7        2013      2013         0            1          4           4        30    26  2013      4 2013-04-15

Since I would like to plot this data nicely using the example: https://joehamman.com/2013/10/12/plotting-netCDF-data-with-Python/, the next step is to convert this CSV file into a NetCDF file. Does anyone have a suggestion for Python? For R there is already the question: convert csv to netcdf in R

Weiss
  • 176
  • 2
  • 16
  • You could give a chance to xarray, read this for example: https://stackoverflow.com/questions/46476920/xarray-writing-to-netcdf-from-pandas-dimension-issue – Gam May 06 '22 at 10:07

1 Answers1

1

you could use xarray to import the dataframe, build the dataset shape as you want and then save it as NetCDF file.

A small example:

import pandas as pd
# Execute pip install xarray first
import xarray

# Example dataframe
diz = {
    'Country':['DE','DE','DE'],
    'Latitude':[511.622,511.622,511.622],
    'Longitude':[149.506,149.506,149.506]
}

df = pd.DataFrame(diz) 

# Create xray Dataset from Pandas DataFrame
xr = xarray.Dataset.from_dataframe(df)

# Save to netCDF
xr.to_netcdf('test.nc')
Gam
  • 318
  • 2
  • 9
  • Hi Gam many thanks already! I have several thousand latitude and longitude entries in the CSV file. How can I enter them automatically e.g. diz = { 'Country': []} should I then write iz = { 'Country':[:]} but thank you very much! – Weiss May 06 '22 at 11:15
  • 1
    Hi @Weiss I'm not sure to understand, you can load your csv into a dataframe and then create an xarray dataset e.g. `df = pd.read_csv('your_path_to_csv') ` and from here on the code is the same. I'm misunderstanding? – Gam May 06 '22 at 12:24
  • Hi @Gam I have expressed myself a little unfortunate. I just posted a New question also with code pieces and hopefully now concrete questions :) – Weiss May 06 '22 at 12:43