I am trying to fit a linear regression to climate data from a Netcdf file. The data look like the following..
print(dsloc_lvl)
<xarray.DataArray 'sla' (time: 10227)>
array([0.0191, 0.0193, 0.0197, ..., 0.0936, 0.0811, 0.0695])
Coordinates:
latitude float32 21.62
* time (time) datetime64[ns] 1993-01-01 1993-01-02 ... 2020-12-31
longitude float32 -89.12
Attributes:
ancillary_variables: err_sla
comment: The sea level anomaly is the sea surface height abo...
grid_mapping: crs
long_name: Sea level anomaly
standard_name: sea_surface_height_above_sea_level
units: m
_ChunkSizes: [ 1 50 50]``
I've been using Xarray library to process data, so I've use the xarray.DataArray.polyfit and xarray.DataArray.polyval. Regression line looks good when plotting results.
However, when looking into the coefficients I've noticed they are very small. I've compare coefficients with the np.polyfit approach which are consisitent with what is expected. I figure this is because for np. ppolyfit I convert dates using date2num
x1=mdates.date2num(dsloc_lvl['time'])
Out: array([ 8401., 8402., 8403., ..., 18625., 18626., 18627.])
and the xarray approach converts dates differently, I believe is with:
dsloc_lvl.time.astype(float)
<xarray.DataArray 'time' (time: 10227)>
array([7.2584640e+17, 7.2593280e+17, 7.2601920e+17, ..., 1.6092000e+18,
1.6092864e+18, 1.6093728e+18])
Coordinates:
latitude float32 21.62
* time (time) datetime64[ns] 1993-01-01 1993-01-02 ... 2020-12-31
longitude float32 -89.12
Attributes:
axis: T
long_name: Time
standard_name: time
_ChunkSizes: 1
_CoordinateAxisType: Time
valid_min: 15706.0
valid_max: 25932.0
So this makes coefficients look totally different:
np approach:
np.polyfit(x1,y1,1)
Out: array([ 1.31727420e-05, -1.31428413e-01])
xarray aprroach:
dsloc_lvl.polyfit('time',1)
Out:
<xarray.Dataset>
Dimensions: (degree: 2)
Coordinates:
* degree (degree) int32 1 0
Data variables:
polyfit_coefficients (degree) float64 1.525e-19 -0.1314
My question is, what are the units of time de xarray approach is using, and is there a way to scale it to match de numpy approach?
Thanks.