简体   繁体   中英

Creating NetCDF files in python from csv data

I currently have a number of csv files each for different locations. Within each file there are two columns one is datetime and the other is hourly maximum wind gust in knots. I also have a separate csv that contains the coordinates for each of these csv file locations.

Initially i want to create a netcdf from 12 locations in a 3 x 4 grid with spacing of 0.25 degrees.

All of the examples I have read online about creating netcdf files from csv start with csv files with lat long and then the variable in them where as I am starting with timeseries and the variable. and lat long for each point separate.

As well as this all the examples I've seen load each timestep in manually one at a time. Obviously if I am using hourly data from 1979 this is unfeasible and if possible would like to load all the data in in one go. If this is not possible then it would still be quicker to load in the data for each grid point as opposed to each time step. Any help at all with these problems would be much appreciated.

I have been following the example from https://www.esri.com/arcgis-blog/products/arcgis/data-management/creating-netcdf-files-for-analysis-and-visualization-in-arcgis/ if this is of any use to those providing assistance

I am also familiar with CDO but I'm not sure if it has any useful functionality here

cheers

There are a variety of ways of doing this. The simplest is possibly to use pandas and xarray. The code below shows how to create a simple dataframe and save it netCDF using pandas/xarray.

import pandas as pd
import xarray as xr

df = pd.DataFrame({"lon":range(0,10), "lat":range(0,10), 
"value":range(0,10)})
df = df.set_index(["lat", "lon"])
df.to_xarray().to_netcdf("outfile.nc")

You haven't specified how the time is stored etc., so I will leave it up to you to work out how to read the csvs and get the times in the necessary format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM