0
votes

I have a NetCDF with one variable (front) and four dimensions (time, altitude, lat and lon). Downloaded from https://coastwatch.pfeg.noaa.gov/erddap/griddap/erdGAtfnt10day_LonPM180.html

It is a monthly composite, i.e. <xarray.DataArray 'time' (time: 151)> array(['2001-01-16T12:00:00.000000000', '2001-02-16T00:00:00.000000000', ...'2013-12-16T12:00:00.000000000'], dtype='datetime64[ns]').

I would like to create a single file (either a NetCDF or Geotif it doesn't matter which) for each timestamp.

I've tried:

ds = xr.open_dataset("Front_monthly2001to2013.nc", decode_times=True)

months, datasets = zip(*ds.groupby("time.month"))  #.groupby("time.month")
paths = ["test%s.nc" % m for m in months]
xr.save_mfdataset(datasets, paths)

But then it groups all the months and I get 12 output files instead of one for each year-month. How do I save by year-month (which is the same, in my case, as each timestamp)?

Thanks in advance

1

1 Answers

0
votes

You can use the datetime components of your time dimension to re-format the timestamp to year & month and use it for grouping:

months, datasets = zip(*ds.groupby(ds.time.dt.strftime("%Y%m")))