How to save xarray data to csv
shalu.ashu50 at gmail.com
shalu.ashu50 at gmail.com
Mon Apr 16 10:55:43 EDT 2018
Hello All,
I have used xarray to merge several netcdf files into one file and then I subset the data of my point of interest using lat/long. Now I want to save this array data (time,lat,long) into csv file but I am getting an error with my code:
dsmerged = xarray.open_mfdataset('F:/NTU_PDF__Work/1_Codes/1_Python/testing/netcdf/*.nc')
#save to netcdf file
dsmerged.to_netcdf('combine.nc')
# Extraction of data as per given coordinates
#values (lat/long with their upper and lower bound)
fin = netCDF4.Dataset("combine.nc" ,"r")
# print the all the variables in this file
print (fin.variables)
# print the dimensions of the variable
print (fin.variables["clt"].shape)
#Out: (20075, 90, 144)
# retrieve time step from the variable
clt0 = (fin.variables["clt"])
# extract a subset of the full dataset contained in the file
clt0sub = clt0[10:30,20:30]
# xarray to numpy array
clt1=numpy.array(clt0sub)
# saving data into csv file
with open('combine11.csv', 'wb') as f:
writer = csv.writer(f, delimiter=',')
writer.writerows(enumerate(clt1))
getting this error - TypeError: a bytes-like object is required, not 'str' when I am removing "b" the error disappears but the data saving in wrong format example:-
0,"[[ 99.93312836 99.99977112 100. ..., 98.53624725
99.98111725 99.9799881 ]
[ 99.95301056 99.99489594 99.99998474 ..., 99.9998703 99.99951172
99.97265625]
[ 99.67852783 99.96372986 99.99999237 ..., 99.96694946 99.9842453
99.96450806]
...,
[ 78.29571533 45.00857544 24.39345932 ..., 90.86527252
84.48490143 62.53995895]
[ 42.03381348 46.1696701 22.71044922 ..., 80.88492584
71.15007019 50.95384216]
[ 34.75331879 49.99913025 17.66173935 ..., 57.12231827
62.56645584 40.6435585 ]]"
1,"[[ 100. 100. 100. ..., 99.93876648
99.98928833 100. ]
[ 99.9773941 100. 99.4933548 ..., 97.93031311
97.36623383 97.07974243]
[ 98.5934906 99.72555542 99.44548035 ..., 79.59191132
85.77541351 94.40919495]
...,
suggestions would be appreciated
More information about the Python-list
mailing list