Jump to content

Obtaining Wind Data


Lidia

Recommended Posts

Posted

Hello!

I want to use some reanalysis data from CDS API (Copernicus), but I´ve only found it in NC or GRID files, I don't know how to convert it into a file type that Climate Analyst 3 can read. Could someone tell me how? Or perhaps where I can find the data already in a txt file. I'm currently looking for information in central Spain.

Thank you 🙂

 

Lidia N

Posted

Hi Lidia,

This can be done using Python with the xarray and Pandas packages. 

To create a text file using the xarray package in Python, you can use the open_dataset() function to open the NetCDF file, and then use the to_dataframe() function to convert the data to a Pandas DataFrame. You can then use the drop() function from Pandas to remove the columns that you do not want to include in the output text file, and then use the to_csv() function to write the resulting DataFrame to a text file.

Here is an example of how you can do this:

import xarray as xr
import pandas as pd

# Open the NetCDF file using xarray
ds = xr.open_dataset('input.nc')

# Convert the data to a Pandas DataFrame
df = ds.to_dataframe()

# Remove the "height" and "crs" columns from the DataFrame
df = df.drop(columns=['height', 'crs'])

# Write the DataFrame to a text file in the preferred format for WAsP Climate Analyst
df.to_csv('output.txt', sep='\t', index=False)
 

Note that this makes a tab-separated file with "sep='\t'. This can be changed to your preferred delimiter.

In this example, we are using the drop() function from Pandas to remove the "height" and "crs" columns from the DataFrame. We are then using the to_csv() function to write the resulting DataFrame to a text file with a tab-separated value (TSV) format, which is the format that is required by the WAsP Climate Analyst tool.
 

For more information and examples of using the xarray and Pandas packages to convert NetCDF data to text files, you can refer to the xarray and Pandas documentation:
http://xarray.pydata.org/en/stable/generated/xarray.Dataset.to_dataframe.html
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html

  • 2 weeks later...
Posted

Thank you so much, it worked! I actually simplified the code even more, I'm not great at programming so I tried to use only the stuff I understood, I'll leave it here in case it proves useful to anyone:

import xarray as xr
 
# Open the NetCDF file using xarray
DS = xr.open_dataset(r'input-file.nc')
 
# Convert the data to CSV
DS.to_dataframe().to_csv('output-file.csv')

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...