Ep1c1aN
Ep1c1aN

Reputation: 733

Convert a numpy dataset to netCDF

I have a numpy array in python with size (16,250,186) representing time, latitude and longitude.

I want to convert it to a netCDF file so that I can read the data easily with co-ordinates in future.

My numpy array looks like this

RZS = np.load("/home/chandra/Data/rootzone_CHIRPS_era5_2003-2015_daily-analysis_annual-result.npy")
RZS.shape

Output: (16, 250, 186)

As you can see my above numpy array represents annual values for 16 years.

chirps_precip =xarray.open_mfdataset("/home/chandra/Data/CHIRPS/chirps-v2.0.2000.days_p25.nc")
precip = chirps_precip.precip.sel(latitude = slice(-50,12.5), longitude = slice(-81.25,-34.75))
precip[0,:,:]

Output:
<xarray.DataArray 'precip' (latitude: 250, longitude: 186)>
dask.array<shape=(250, 186), dtype=float32, chunksize=(250, 186)>
Coordinates:
  * latitude   (latitude) float32 -49.875 -49.625 -49.375 ... 12.125 12.375
  * longitude  (longitude) float32 -81.125 -80.875 -80.625 ... -35.125 -34.875
    time       datetime64[ns] 2000-01-01
Attributes:
    units:               mm/day
    standard_name:       convective precipitation rate
    long_name:           Climate Hazards group InfraRed Precipitation with St...
    time_step:           day
    geostatial_lat_min:  -50.0
    geostatial_lat_max:  50.0
    geostatial_lon_min:  -180.0
    geostatial_lon_max:  180.0

These are the co-ordinates of the chirps_precip dataset that I want my numpy array RZS to have with years (as 2000, 2001, .....2015) on the timestep

I have tried some methods like

# from xarray
array = xarray.DataArray(RZS, latitude = 'precip.latitude')

#from netCDF
Dataset.createVariable('rootzone storage cap', np.float32, ('time','lat','lon'))

But I am not able to do anything. I also tried to copy attrs and coords but that also didn't work. It seems like I am doing this the wrong way. Can anyone suggest what am I missing.

I want my numpy array to have the same co-ordinate as the netcdf file, but with a modified time attribute to years.

Upvotes: 6

Views: 10437

Answers (1)

msi_gerva
msi_gerva

Reputation: 2078

I would suggest a code like using module netCDF4, assuming you have latitude and longitude in variables lat and lon and dataout is dataout.

#!/usr/bin/env ipython
# ---------------------
import numpy as np
import datetime
from netCDF4 import Dataset,num2date,date2num
# -----------------------
nyears = 16;
unout = 'days since 2000-01-01 00:00:00'
# -----------------------
ny, nx = (250, 186)
lon = np.linspace(9,30,nx);
lat = np.linspace(50,60,ny);

dataout = np.random.random((nyears,ny,nx)); # create some random data
datesout = [datetime.datetime(2000+iyear,1,1) for iyear in range(nyears)]; # create datevalues
# =========================
ncout = Dataset('myfile.nc','w','NETCDF3'); # using netCDF3 for output format 
ncout.createDimension('lon',nx);
ncout.createDimension('lat',ny);
ncout.createDimension('time',nyears);
lonvar = ncout.createVariable('lon','float32',('lon'));lonvar[:] = lon;
latvar = ncout.createVariable('lat','float32',('lat'));latvar[:] = lat;
timevar = ncout.createVariable('time','float64',('time'));timevar.setncattr('units',unout);timevar[:]=date2num(datesout,unout);
myvar = ncout.createVariable('myvar','float32',('time','lat','lon'));myvar.setncattr('units','mm');myvar[:] = dataout;
ncout.close();

Compared to xarray, you have to write more code, but it is still very easy to create the netCDF files using that module.

Upvotes: 10

Related Questions