How to write data from a NetCDF file to a CSV or XLSX/Excel file (NetCDF in Python #03)
HTML-код
- Опубликовано: 13 авг 2024
- In this video, you will learn to export your data to a Pandas dataframe which you can write to a CSV or XLSX file.
Chapters
00:00 Introduction
00:23 Importing modules
00:47 Loading in 1st dataset (1D)
03:05 Data to pandas dataframe
04:21 Exporting to CSV or XLSX
05:44 Second dataset (multiple dimensions)
08:40 Extracting a subset of the data
How to cite this video
If you think this course contributed to the work you are doing, consider citing it in your list of references. Here is a recommended citation:
Marsden, L. (2024, April 19). NetCDF in Python - from beginner to pro. Zenodo. doi.org/10.5281/zenodo.10997447
All videos in this course:
01: • How to open a NetCDF f...
02: • How to plot data from ...
03: • How to write data from...
04: • How to create a NetCDF...
05: • How to structure a dat...
06: • How batch create NetCD...
07: • How to extract data fr...
The code
This tutorial series is accompanied by a Jupyter Book with code, explanations and more examples. You can find the relevant section here:
lhmarsden.github.io/NetCDF_in...
Data used in this tutorial:
Elizabeth Jones (2022) CTD data from Nansen Legacy Cruise - Joint cruise 2-1 Staion: P1_NLEG01-1 doi.org/10.21335/NMDC-2085836...
OPeNDAP link: opendap1.nodc.no/opendap/phys...
H.-M. Zhang, B. Huang, J. H. Lawrimore, M. J. Menne, and T. M. Smith (2019): NOAA Global Surface Temperature Dataset (NOAAGlobalTemp), Version 5.0. NOAA National Centers for Environmental Information. doi:10.25921/9qth-2p70 Accessed 2024-01-04.
OPeNDAP link: www.ncei.noaa.gov/thredds/dod...
www.ncei.noaa.gov/access/meta...
Photo of me in thumbnail taken by Kathrine Lindsay
How can I do this for multiple data points with multiple dimensions? My dimensions are longitude, latitude, and time (year, month, day, hour [hour increments of 3]), and I want to extract two values from each longitude/latitude data point, for the entire year.
I'm not entirely sure what you mean but maybe you will find this helpful.
import xarray as xr
Load your dataset (replace 'filename.nc' with your actual file)
ds = xr.open_dataset('filename.nc')
# Suppose your latitude range is from 20 to 30 degrees, longitude range is from -10 to 10 degrees, and time range is from '2024-01-01' to '2024-01-31'
Selecting latitude range from 20 to 30 degrees
lat_range = ds.sel(lat=slice(20, 30))
Selecting longitude range from -10 to 10 degrees
lon_range = lat_range.sel(lon=slice(-10, 10))
Selecting time range from '2024-01-01' to '2024-01-31'
time_range = lon_range.sel(time=slice('2024-01-01', '2024-01-31'))
@@LukeDataManager i can do it individually, so scratch the multiple data points part. However, the year, month, time, and hour are each individual variables.
Interesting. I think I would be tempted to extract each variable as numpy arrays first and then combine them.
import numpy as np
years = np.array([2024, 2024, 2024])
months = np.array([2, 2, 2])
days = np.array([28, 29, 29])
hours = np.array([10, 12, 15])
# Create datetime array
timestamps = np.array([np.datetime64(f'{y}-{m:02}-{d:02}T{h:02}') for y, m, d, h in zip(years, months, days, hours)])
Then you can add that numpy array to your dataframe if you need to.
df['timestamps'] = timestamps
Assuming you have one row per time. If not, create another dataframe of your year, month.... first and then merge the dataframe together based on a shared column or list of columns
And if you are ever creating a netcdf file don't encode time like that! There is a section the next video in the series about how to encode time in CF-NetCDF.