0
votes

I have a set of satellite data file here, I created a grid for lat & lon and a 2D array for Ozone values.

I know that in order to plot the contourf of the data in a map I need the projection coordinates, but I can't get find a way around it as my grid is not square (144x24). I am covering the geographical area (0 to 360; -30 to 30) and I require square pixels.

The data is quite long to post it but this is my code so far,

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.basemap import Basemap, cm
%matplotlib inline

path = '/home/rafaella/month_files_CSV/O3_COLUMNS_MATCHED_fv0005_200306.csv'
df = pd.read_csv(path, skiprows=1)

df = pd.read_csv(path, delim_whitespace=True)
lat = np.array(df['AVG_LAT'])
lon = np.array(df['AVG_LON'])
toc = np.array(df['TROP_COL'])

#new grid for lon[0,360] lat[-30,30]

lomin = 0
lomax = 360
lamin = -30
lamax = 30
stp   = 2.5

loc_lon = np.zeros(int((lomax-lomin)/stp))
loc_lat = np.zeros(int((lamax-lamin)/stp))

for i in range(0,len(loc_lon)):
    loc_lon[i] = i*stp +lomin

for j in range(0,len(loc_lat)):
    loc_lat[j] = j*stp +lamin

mtoc_local = np.zeros((len(loc_lon),len(loc_lat))) 
sdtoc_local = np.zeros((len(loc_lon),len(loc_lat))) 
mtoc_local[:,:] = np.nan      
sdtoc_local[:,:] = np.nan

for i in range (0, len(loc_lon)):
    for j in range (0,len(loc_lat)):
        ix = np.where((lon>=loc_lon[i])& (lat>=loc_lat[j]) & (lon<loc_lon[i]+stp) & (lat<loc_lat[j]+stp))[0] 
        mtoc_local[i,j]=np.nanmean(toc[ix]) 
        sdtoc_local[i,j]=np.nanstd(toc[ix])

fig = plt.figure(figsize=(20, 5))
map = Basemap(llcrnrlon=0,llcrnrlat=-30, urcrnrlon=360.,urcrnrlat=30.,\
        rsphere=(6378137.00,6356752.3142),\
        resolution='l',projection='merc',\
        lat_0=0,lon_0=-30.,lat_ts=30.)

map.drawcoastlines()
# draw parallels
map.drawparallels(np.arange(-30,30,10),labels=[1,1,0,1])
# draw meridians
map.drawmeridians(np.arange(-180,180,20),labels=[1,1,0,1])

map = plt.contourf(loc_lon, loc_lat , mtoc_local.T, vmin=210, vmax=350,  cmap='RdPu')
plt.colorbar(orientation='horizontal', ticks=[200, 220, 240, 260, 280, 300, 320, 340] )
plt.title('Tropical TOC monthly mean 06,2009')

plt.show()

It plots very well the map OR the data but not both. here an image of both separately map

real data

I am very new to python, I started a month ago, so it is still not familiar to me all the functions and libraries.

1
Can you post an example of either your csv file, or the lat/lon/toc variables? very hard to help without some sample data that recreates the problem. - Ken Syme
Sure, but the problem is with the projected coordinates, I am not using the coordinates given in the data, but a grid. - Rafaella Chiarella
I think that Basemap does not like longitude in the range of [0,360] but rather [-180, 180]. Not sure if this is the root cause of your problems though. - Tasko Olevski
Your link to the data does not seem to work. - Hagne
I changed the link, I hope this works - Rafaella Chiarella

1 Answers

4
votes

Your code has two problems. First you have to apply the projection on your coordinates which is done using x,y = map(lon, lat). However, this will raise an error in your case since the dimensions of loc_lon and loc_lat are different. Instead of passing x and y vectors to the contourf function you can pass arrays with the same shape as z (mtoc_local.T). You can use np.meshgrid to create those. Long story short, replace the line with the contourf command with the following three lines

X, Y = np.meshgrid(loc_lon, loc_lat)
x,y = map(X,Y)
map = plt.contourf(x, y , mtoc_local.T, vmin=210, vmax=350,  cmap='RdPu')

and the result looks like this enter image description here