'How to create a new xarray dataset based on an old one?
I have a netcdf file of gridded daily temperature. Time is not a coordinate of the file so I am trying to make a new xarray dataset that does have time as a coordinate.
import os
import matplotlib.pyplot as plt
from netCDF4 import Dataset as netcdf_dataset
import numpy as np
import xarray as xr
import pandas as pd
#open NASA GISS gridded temperature netcdf file
df = xr.open_dataset('Berkeley1950-2021.nc')
#pull variables from netcdf file
latitude=df.latitude
longitude=df.longitude
temperature=df.temperature
#create dates
dates = pd.date_range("1950-01-01", "2021-12-31", freq="D")
#new xarray dataset
NewBerkeley = xr.Dataset(
coords={
"time": dates,"lat": (latitude),"lon": (longitude)
},
data_vars={
"temperature":(( "time", "lat","lon"),temperature),
}
)
But when I do this I get the following error message
"MergeError: coordinate lat shares a name with a dataset dimension, but is not a 1D variable along that dimension. This is disallowed by the xarray data model."
Why is this happening? And how can I fix it?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

