pankajkarman / bias_correction

python library for bias correction
MIT License
30 stars 8 forks source link

The issue with Dimensions while saving the bias corrected data output? #18

Open abhilashsinghimd opened 1 year ago

abhilashsinghimd commented 1 year ago

The plots seems fine but the NetCDF are not being written/exported properly.

The dimensions should have been time, lat, long (365, 34, 65) But somehow there are 34 bands i.e. latitude somehow became band i.e number of timesteps

image

image

!/usr/bin/env python

coding: utf-8

In[1]:

from bias_correction import XBiasCorrection from dask.diagnostics import ProgressBar import numpy as np import xarray as xr import pandas as pd import matplotlib.pyplot as plt import os

In[2]:

obs_data = np.random.randn(2365, 34, 65) model_data = np.random.randn(2365, 34, 65) sce_data = np.random.randn(365, 34, 65)

lat = range(34) lon = range(65)

obs_data = xr.DataArray(obs_data, dims=['time','lat','lon'], coords=[pd.date_range('2010-01-01', '2011-12-31', freq='D'), lat, lon]) model_data = xr.DataArray(model_data, dims=['time','lat','lon'], coords=[pd.date_range('2010-01-01', '2011-12-31', freq='D'), lat, lon]) sce_data = xr.DataArray(sce_data, dims=['time','lat','lon'], coords=[pd.date_range('2021-01-01', '2021-12-31', freq='D'), lat, lon])

ds = xr.Dataset({'model_data':model_data,'obs_data':obs_data, 'sce_data':sce_data}) ds['sce_data']

bc = XBiasCorrection(ds['obs_data'], ds['model_data'], ds['sce_data']) df3 = bc.correct(method='modified_quantile')

In[3]:

bc.sce_data.sel(time='2021-12-15').plot(figsize=(14, 5), robust=True) plt.show()

In[4]:

df3.sel(time='2021-12-15').plot(figsize=(14, 5), robust=True) plt.show()

In[5]:

########### convert dataarray to Dataset ################# out_var_name = "tasmax" dataset_out = df3.to_dataset(name=out_var_name) dataset_out

The time dimension switches with latitude somehow while saving this dataset as NetCDF.

In[6]:

for year in range (2021,2022): out_file_dir = r"F:\CMIP6\Raw_CMIP_Data\Bias_Correction\Bias_Corrected\Tmax\" if not os.path.exists(out_file_dir): os.makedirs(out_file_dir) out_file_name = out_file_dir + str(year)+"_Biases_Corrected_Tmax.nc" print(out_file_name) yearly_ds = df3.where(((dataset_out['time.year'] == year)), drop=True) write_job = yearly_ds.to_netcdf(out_file_name, compute=False) with ProgressBar(): print(f"Writing to {year}") write_job.compute()

In[7 ]:

Details are here at this link.

https://stackoverflow.com/questions/73196955/dimensions-position-error-in-converting-xarray-dataarray-to-xarray-dataset

How to rectify it?

The NetCDF is attached here:

2021_Biases_Corrected_Tmax.zip