0

I first downloaded NetCDF4 data from this website. Now, I wanted to make the file more cloud friendly so I converted it into CSV. After that, my rows and columns in the CSV looked like this.

enter image description here

Now, I want to make a plot which will plot an image which will be similar to the earth plot in this link. The only difference is that they are plotting the plot directly from the netCDF4 file without converting it into CSV but I am trying to plot it after converting to CSV. I want to show the TLML values in the Earth plot.

If anyone is interested, this is how I converted my netCDF4 file into CSV.

import xarray as xr

file = "C:/Users/xyz/plotting-try/MERRA2_400.inst1_2d_lfo_Nx.20220203.nc4"

nc = xr.open_dataset(file)
nc.TLML[1,:,:].to_dataframe().to_csv('T2M.csv')

Can anyone help me on the plotting part? As mentioned, I want to plot it similar to how it was shown in the above link, just that I want to plot it using my CSV file.

If you want to download the netCDF4 file on your system, you can download it here. However, you'll first need to create an account on the Nasa website.

Edit: I have tried the below code but it isn't working.

df = pd.read_csv('T2M.csv')

lat = df['lat']
lon = df['lon']
time = df['time']
T2M = df['TLML']

fig = plt.figure(figsize=(8,4))
ax = plt.axes(projection=ccrs.Robinson())
ax.set_global()
ax.coastlines(resolution="110m",linewidth=1)
ax.gridlines(linestyle='--',color='black')

clevs = np.arange(576,361,24)
plt.contourf(lon, lat, T2M, clevs, transform=ccrs.PlateCarree(),cmap=plt.cm.jet)

plt.title('MERRA-2 Air Temperature at 2m, January 2010', size=14)
cb = plt.colorbar(ax=ax, orientation="vertical", pad=0.02, aspect=16, shrink=0.8)
cb.set_label('K',size=12,rotation=0,labelpad=15)
cb.ax.tick_params(labelsize=10)

fig.savefig('MERRA2_t2m.png', format='png', dpi=360)

This throws an error that TypeError: Input z must be 2D, not 1D. And it throws on this line: plt.contourf(lon, lat, T2M, clevs, transform=ccrs.PlateCarree(),cmap=plt.cm.jet)

I'm not even sure whether the conversion into CSV format is done correctly. Anyone who has an experience working with these kind of files, please help me on this.

CuriousLearner
  • 421
  • 4
  • 14

1 Answers1

0

I can't download the file, but I have a sugestion that seens that will work.

The columns lat and lon are not pivoted. Because this there are a lot of values repeating in one column for different values of the another column. You have to separate and convert your data.

First of all, I suggest using the pandas(https://pandas.pydata.org/) library that is very good to read csv files and make transformations on your data.

Your data will be stored in a DataFrame that is a pandas object that present similarities with a table. The lat and lon will be converted to unique values and the table will be pivoted to create the 2d table, for each lat(row) and for each lon(column) we will have only one T2M.

The code will be like this:

import pandas as pd

file = 'some_path/file_name.csv'
df = pd.read_csv(file)

lat = df['lat'].unique()
len = df['lat'].unique()
T2M = df.pivot(index='lat', columns='len', values='TLML')

Now your T2M variable will have 2 dimensions.