How to extract the data

#1
by acrigney - opened

I want to extract some data to have a look.
import xarray as xr
import pandas as pd

Load the dataset

dataset = xr.open_dataset('anomaly_variance_surface.nc')

Convert each variable in the dataset to a dictionary

data_dict = {var: dataset[var].values.item() for var in dataset.data_vars}

Convert the dictionary to a DataFrame

df = pd.DataFrame([data_dict])

Export to CSV

df.to_csv('anomaly_variance_surface.csv', index=False)
But I only got a small amount of data. U10M,V10M,T2M,QV2M,PS,SLP,TS,TQI,TQL,TQV,GWETROOT,LAI,EFLUX,HFLUX,Z0M,PRECTOT,LWGEM,LWGAB,LWTUP,SWGNT,SWTNT,PHIS,FRLAND,FROCEAN,FRACI
16.523505815026716,17.095847097516533,10.974715500648557,1.413706636575874e-06,675849.2220303438,719414.2215921007,11.079280570987207,0.0014124833179566501,0.0046736703931425806,32.791773618577324,0.000992058354735481,0.0008977774498764754,1818.5462147399926,605.3439087584461,2.4557930480950213e-05,1.4065749014870677e-08,164.24350505924792,683.497095808074,995.1994780201103,4448.437620727601,3859.176007582711,5.241133294475732e-06,5.714934259740092e-15,4.643750136620925e-15,4.887843856195936e-15

Is this compressed data?

And for other files can I just do this?

import xarray as xr
import pandas as pd

List of all NetCDF files in the dataset

file_list = ['anomaly_variance_surface.nc', 'other_file_1.nc', 'other_file_2.nc']

Open multiple files as a single dataset

combined_dataset = xr.open_mfdataset(file_list, combine='by_coords')

Inspect the combined dataset structure

print(combined_dataset)

Convert to DataFrame (depending on the structure of the combined dataset)

df = combined_dataset.to_dataframe().reset_index()

Export to CSV

df.to_csv('combined.csv', index=False)

Sign up or log in to comment