def test_SingleMap_add_cyclic(self):
     file='/home/m300028/shared/data/SEP/variables/land/Ta_2m/cru_ts_3_00.1901.2006.tmp_miss_t63.nc'
     ofile = 'world.png'
     if os.path.exists(ofile):
         os.remove(ofile)
     d=Data(file,'tmp',read=True)
     map_plot(d, use_basemap=True, savegraphicfile=ofile)
     if os.path.exists(ofile):
         os.remove(ofile)
Example #2
0
 def test_SingleMap_add_cyclic(self):
     file = '/home/m300028/shared/data/SEP/variables/land/Ta_2m/cru_ts_3_00.1901.2006.tmp_miss_t63.nc'
     ofile = 'world.png'
     if os.path.exists(ofile):
         os.remove(ofile)
     d = Data(file, 'tmp', read=True)
     map_plot(d, use_basemap=True, savegraphicfile=ofile)
     if os.path.exists(ofile):
         os.remove(ofile)
Example #3
0
# -*- coding: utf-8 -*-
"""
This file is part of pyCMBS.
(c) 2012- Alexander Loew
For COPYING and LICENSE details, please refer to the LICENSE file
"""
"""
Basic plotting in pyCMBS
"""

from pycmbs.mapping import map_plot
import matplotlib.pyplot as plt
from pycmbs.examples import download

air = download.get_sample_file(name='air')
air.label = 'air temperature'
f1 = map_plot(air,
              show_timeseries=False,
              use_basemap=True,
              title='show_timeseries=True')
f2 = map_plot(air, show_zonal=True, use_basemap=True, title='show_zonal=True')
f3 = map_plot(air,
              show_histogram=True,
              use_basemap=True,
              title='show_histogram=True')
plt.show()
Example #4
0
For COPYING and LICENSE details, please refer to the file
COPYRIGHT.md
"""

from pycmbs.icon import Icon
from pycmbs.mapping import map_plot
import matplotlib.pyplot as plt

plt.close('all')

##### PLEASE NOTE THAT THIS EXAMPLE IS CURRENTLY LIMITED USERS WITH ACCESS TO SPECIFIC DATA


#//// read data ////
gridfile ='../..//example_data/icon/r2b4_amip.nc'
datafile = '../../example_data/icon/rms0006_atm_phy_DOM01_ML_0001.nc'

#/// read data ///
IC = Icon(datafile,gridfile,'rsns')
IC.read()
IC.label='shortwave net flux at surface'
IC.unit='$W/^2$'

#/// Do plotting ///
print('Doing first plot ...')
map_plot(IC, use_basemap=True)
print('Doing second plot ...')
map_plot(IC, use_basemap=False)

plt.show()
#~ file='./example_data/air.mon.mean.nc'
#~ if not os.path.exists(file):
#~ raise ValueError('Sample file not existing: see example-01.py')

# read data
#~ D = Data('./example_data/air.mon.mean.nc', 'air',read=True)
D = download.get_sample_file(name='air')
D.label = 'air temperature'

#~ P = Data('./example_data/pr_wtr.eatm.mon.mean.nc','pr_wtr',read=True)
P = download.get_sample_file(name='rain')

# some analysis
print 'Temporal stdv. ...'
t = D.timstd(return_object=True)
map_plot(t, use_basemap=True, title='Temporal stdv.', show_stat=True)

print 'Some LinePlot'
L = LinePlot(regress=True, title='This is a LinePlot with regression')
L.plot(D, label='2m air temperature')
L.plot(P, label='Precipitable water', ax=L.ax.twinx(),
       color='green')  # use secondary axis for plotting here
L.legend()

print 'Scatterplot between different variables ...'
#here we just generate some random second variable
D1 = D.copy()
D1.data += np.random.random(D.shape) * 50.
S = ScatterPlot(
    D)  # scatterplot is initialized with definition of X-axis object
S.plot(D1)
Example #6
0
# -*- coding: utf-8 -*-
"""
This file is part of pyCMBS.
(c) 2012- Alexander Loew
For COPYING and LICENSE details, please refer to the LICENSE file
"""
"""
Basic plotting in pyCMBS
"""

from pycmbs.mapping import map_plot
import matplotlib.pyplot as plt
from pycmbs.examples import download

# Read some sample data ...
air = download.get_sample_file(name='air')
air.label = 'air temperature'

# a quick plot as well as a projection plot
f1 = map_plot(air)  # unprojected
f2 = map_plot(air, use_basemap=True)  # projected
plt.show()
# -*- coding: utf-8 -*-

"""
This file is part of pyCMBS.
(c) 2012- Alexander Loew
For COPYING and LICENSE details, please refer to the LICENSE file
"""

"""
Basic plotting in pyCMBS
"""

from pycmbs.mapping import map_plot
import matplotlib.pyplot as plt
from pycmbs.examples import download

air = download.get_sample_file(name='air')
air.label = 'air temperature'
f1 = map_plot(air, show_timeseries=False, use_basemap=True, title='show_timeseries=True')
f2 = map_plot(air, show_zonal=True, use_basemap=True, title='show_zonal=True')
f3 = map_plot(air, show_histogram=True, use_basemap=True, title='show_histogram=True')
plt.show()
# 1) calculate the weights ...
#     these are dependent on the number of days  which you get as ...

ml = air._get_days_per_month()
print ml

w = ml / float(sum(ml))  # relative weights
print w
print 'The sum of the weights should be 1.: ', sum(w)

# 2) now we need to multiply the data with the weights and then
#  sum up over time
new = air.mul_tvec(w, copy=True)  # check also the other options of this routine!
res = new.timsum(return_object=True)  # gives a 2D array with weighted results
print res.shape

map_plot(air, title='classical unweighted temporal mean', show_stat=True)
map_plot(res, title='weighted temporal mean', show_stat=True)
map_plot(air.sub(res), title='difference', show_stat=True, cmap_data='RdBu_r', vmin=-0.1, vmax=0.1)

# you will see that the differences are marginal, but they are nevertheless there
# for this example.

plt.show()






#~ file='./example_data/air.mon.mean.nc'
#~ if not os.path.exists(file):
    #~ raise ValueError('Sample file not existing: see example-01.py')

# read data
#~ D = Data('./example_data/air.mon.mean.nc', 'air',read=True)
D = download.get_sample_file(name='air')
D.label = 'air temperature'

#~ P = Data('./example_data/pr_wtr.eatm.mon.mean.nc','pr_wtr',read=True)
P = download.get_sample_file(name='rain')

# some analysis
print 'Temporal stdv. ...'
t = D.timstd(return_object=True)
map_plot(t,use_basemap=True,title='Temporal stdv.',show_stat=True)


print 'Some LinePlot'
L=LinePlot(regress=True, title='This is a LinePlot with regression')
L.plot(D, label='2m air temperature')
L.plot(P, label='Precipitable water', ax=L.ax.twinx(), color='green')  # use secondary axis for plotting here
L.legend()

print 'Scatterplot between different variables ...'
#here we just generate some random second variable
D1 = D.copy()
D1.data += np.random.random(D.shape)*50.
S=ScatterPlot(D)  # scatterplot is initialized with definition of X-axis object
S.plot(D1)
S.legend()
Example #10
0
from pycmbs.examples import download
from pycmbs.mapping import map_plot

plt.close('all')

air = download.get_sample_file(name='air')
air.label = 'air temperature'

# generate some artificial data by rescaling the original data
air2 = air.copy()  # copy data object
air2.mulc(1.3, copy=False)  # copy=False applies operation directly on data, otherwise a copy is returned

# difference
print 'Calculate differences ...'
diff = air.sub(air2)  # calculate difference
f1 = map_plot(diff, cmap_data = 'RdBu_r', vmin=-10., vmax=10.)

# temporal evolution of mean and stdv
print 'Temporal evolution of mean and stdv of a field; note that values are calculated as area weigted values'
f = plt.figure()
ax = f.add_subplot(111)
ax.plot(air.fldmean(), label='fldmean()')
ax.plot(air.fldstd(), label='fldstd()')
ax.legend()
ax.grid()

plt.show()


Example #11
0
# the way how you would do it is like
# y = sum(w[i] * x[i])
# whereas w is a weighting factor for each timestep and 'x' is the input data

# how can you easily do that with the Data object?

# 1) calculate the weights ...
#     these are dependent on the number of days  which you get as ...

ml = air._get_days_per_month()
print ml

w = ml / float(sum(ml))  # relative weights
print w
print "The sum of the weights should be 1.: ", sum(w)

# 2) now we need to multiply the data with the weights and then
#  sum up over time
new = air.mul_tvec(w, copy=True)  # check also the other options of this routine!
res = new.timsum(return_object=True)  # gives a 2D array with weighted results
print res.shape

map_plot(air, title="classical unweighted temporal mean", show_stat=True)
map_plot(res, title="weighted temporal mean", show_stat=True)
map_plot(air.sub(res), title="difference", show_stat=True, cmap_data="RdBu_r", vmin=-0.1, vmax=0.1)

# you will see that the differences are marginal, but they are nevertheless there
# for this example.

plt.show()
Example #12
0
For COPYING and LICENSE details, please refer to the file
COPYRIGHT.md
"""

"""
Basic plotting in pyCMBS
"""
from pycmbs.mapping import map_plot
import matplotlib.pyplot as plt
import numpy as np
from pycmbs.examples import download

# Read some data ...
air = download.get_sample_file(name='air')

f=plt.figure(figsize=(10,6))
ax1=f.add_subplot(221)
ax2=f.add_subplot(222)
ax3=f.add_subplot(223)
ax4=f.add_subplot(224)

f1 = map_plot(air, use_basemap=False, title='vmin=-30.,vmax=30.,cmap_data=RdBu_r', vmin=-30., vmax=30., cmap_data='RdBu_r', ax=ax1)
#~ f2 = map_plot(air, contours=True, use_basemap=True, levels=np.arange(-50.,50.,10.), title='contours=True', ax=ax2)
f3 = map_plot(air, show_stat=True, use_basemap=False,title='show_stat=True',ax=ax3)
f4 = map_plot(air, show_stat=True, stat_type='median', use_basemap=False, title='show_stat=True,stat_type="median"', ax=ax4)

ax2.set_frame_on(False)
ax2.set_xticks([])
ax2.set_yticks([])
plt.show()
Example #13
0
mask = np.zeros(air.data[0,:,:].shape)
mask[20:30, 50:60] = 1.
mask[40:60, 100:120] = 1.
mask = np.asarray(mask).astype('bool')  # make a bool array




# generate figure for illustration purposes
f = plt.figure()
ax1 = f.add_subplot(2,2,1)
ax2 = f.add_subplot(2,2,2)
ax3 = f.add_subplot(2,2,3)
ax4 = f.add_subplot(2,2,4)

f1 = map_plot(air, ax=ax1, title='This is the original data (unprojected)')
ax2.imshow(mask)
ax2.set_title('This is the mask')

# now we apply the mask to the Data object
air._apply_mask(mask)  # applies a mask to each timestep
f3 = map_plot(air, ax=ax3, title='Masked data')

# if you want you can estimate automatically the bounding box
air.cut_bounding_box()
f4 = map_plot(air, ax=ax4, title='Cutted data')

plt.show()


# just print the region keys for illustration
for k in RS.regions.keys():
    print k

# if you now want to generate a particular mask we can do that
# in the following example we mask the airt temperature for the
# Tibetean plateau

# load the air temperature data
air = download.get_sample_file(name='air')
air.label = 'air temperature'
org = air.copy()

# and then mask it
r_tibet = RS.regions[1]  # gives a Region object
map_plot(air, title='before')
air1 = air.copy()

# mask with region
air.mask_region(r_tibet)
map_plot(air, title='after')
#~ b = air1.mask_region(r_tibet, return_object=True) # this is an alternative which will keep the original data unchanged

# probably you want to save you rmask in a file. If you do this it is much more efficient if you want to apply it again.
file_to_store_mask = tempfile.mktemp(suffix='.nc')  # use temporary file here for this example

x = org.copy()
y = x.mask_region(r_tibet, maskfile=file_to_store_mask, return_object=True)  #this will store the mask in a netcdf file --> have a look at it!

#if you now do the same masking again, then it is much faster as it looks first if the maskfile is already there. If so its beeing used
y1 = x.mask_region(r_tibet, maskfile=file_to_store_mask, return_object=True)  #this will store the mask in a netcdf file --> have a look at it!
Example #15
0
import numpy as np
from pycmbs.utils import download

# Read some data ...
air = download.get_sample_file(name='air')

f = plt.figure(figsize=(10, 6))
ax1 = f.add_subplot(221)
ax2 = f.add_subplot(222)
ax3 = f.add_subplot(223)
ax4 = f.add_subplot(224)

f1 = map_plot(air,
              use_basemap=False,
              title='vmin=-30.,vmax=30.,cmap_data=RdBu_r',
              vmin=-30.,
              vmax=30.,
              cmap_data='RdBu_r',
              ax=ax1)
#~ f2 = map_plot(air, contours=True, use_basemap=True, levels=np.arange(-50.,50.,10.), title='contours=True', ax=ax2)
f3 = map_plot(air,
              show_stat=True,
              use_basemap=False,
              title='show_stat=True',
              ax=ax3)
f4 = map_plot(air,
              show_stat=True,
              stat_type='median',
              use_basemap=False,
              title='show_stat=True,stat_type="median"',
              ax=ax4)
Example #16
0
from pycmbs.examples import download
from pycmbs.data import Data
from pycmbs.mapping import map_plot

import matplotlib.pyplot as plt

# specify some region using bounding box
# here: 20deg W ... 30 DEG E, 40 DEG S ... 5 DEG N
r = RegionBboxLatLon(777, -20.0, 30.0, -40.0, 5.0, label="testregion")  # 777 is just the ID value

# read some data as Data object
filename = download.get_sample_file(name="air", return_object=False)
air = Data(filename, "air", read=True)

# generate some plot BEFORE the masking
map_plot(air, title="before", use_basemap=True)

# now mask the data ...
air.get_aoi_lat_lon(r)

# generate some plot AFTER the masking
map_plot(air, title="after", use_basemap=True)

# ... o.k., so far so good, but the dataset "air" still contains data for the entire domain.
# even if it is masked it will eat some of your memory. You can see this by plotting the size of the matrix
print(air.shape)

# wouldn't it be nice to just remove everything which is not needed?
# Here you go ...
air.cut_bounding_box()
map_plot(air, title="after cutting", use_basemap=True)
from pycmbs.examples import download
from pycmbs.data import Data
from pycmbs.mapping import map_plot

import matplotlib.pyplot as plt

# specify some region using bounding box
# here: 20deg W ... 30 DEG E, 40 DEG S ... 5 DEG N
r = RegionBboxLatLon(777, -20.,30.,-40.,5., label='testregion')   #777 is just the ID value

# read some data as Data object
filename = download.get_sample_file(name='air', return_object=False)
air = Data(filename, 'air', read=True)

# generate some plot BEFORE the masking
map_plot(air, title='before', use_basemap=True)

# now mask the data ...
air.get_aoi_lat_lon(r)

# generate some plot AFTER the masking
map_plot(air, title='after', use_basemap=True)

#... o.k., so far so good, but the dataset "air" still contains data for the entire domain.
# even if it is masked it will eat some of your memory. You can see this by plotting the size of the matrix
print (air.shape)

# wouldn't it be nice to just remove everything which is not needed?
# Here you go ...
air.cut_bounding_box()
map_plot(air, title='after cutting', use_basemap=True)
Example #18
0
"""
update the gallery plots
"""

from pycmbs.mapping import map_plot
import matplotlib.pyplot as plt
from pycmbs.examples import download
import numpy as np

# Read some sample data ...
air = download.get_sample_file(name='air')
air.label = 'air temperature'

# a quick plot as well as a projection plot
f1 = map_plot(air, title='unprojected data')  # unprojected
f2 = map_plot(air, use_basemap=True, title='projected data')  # projected

f1.savefig('./fig/figure1.png')
f2.savefig('./fig/figure2.png')

f = map_plot(air,show_zonal=True, use_basemap=True,title='show_zonal=True')
f.savefig('./fig/figure3.png')
del f


f = plt.figure()
ax1 = f.add_subplot(221)
ax2 = f.add_subplot(222)
ax3 = f.add_subplot(223)
ax4 = f.add_subplot(224)
Example #19
0
filename = download.get_sample_file(name='air', return_object=False)
air = Data(filename, 'air', read=True)

# generate some mask that fits geometry of data. Could be also read from some
# file. We mimic here some irregular mask
mask = np.zeros(air.data[0, :, :].shape)
mask[20:30, 50:60] = 1.
mask[40:60, 100:120] = 1.
mask = np.asarray(mask).astype('bool')  # make a bool array

# generate figure for illustration purposes
f = plt.figure()
ax1 = f.add_subplot(2, 2, 1)
ax2 = f.add_subplot(2, 2, 2)
ax3 = f.add_subplot(2, 2, 3)
ax4 = f.add_subplot(2, 2, 4)

f1 = map_plot(air, ax=ax1, title='This is the original data (unprojected)')
ax2.imshow(mask)
ax2.set_title('This is the mask')

# now we apply the mask to the Data object
air._apply_mask(mask)  # applies a mask to each timestep
f3 = map_plot(air, ax=ax3, title='Masked data')

# if you want you can estimate automatically the bounding box
air.cut_bounding_box()
f4 = map_plot(air, ax=ax4, title='Cutted data')

plt.show()
Example #20
0
from pycmbs.data import Data
from pycmbs.mapping import map_plot

import matplotlib.pyplot as plt

# specify some region using bounding box
# here: 20deg W ... 30 DEG E, 40 DEG S ... 5 DEG N
r = RegionBboxLatLon(777, -20., 30., -40., 5.,
                     label='testregion')  #777 is just the ID value

# read some data as Data object
filename = download.get_sample_file(name='air', return_object=False)
air = Data(filename, 'air', read=True)

# generate some plot BEFORE the masking
map_plot(air, title='before', use_basemap=True)

# now mask the data ...
air.get_aoi_lat_lon(r)

# generate some plot AFTER the masking
map_plot(air, title='after', use_basemap=True)

#... o.k., so far so good, but the dataset "air" still contains data for the entire domain.
# even if it is masked it will eat some of your memory. You can see this by plotting the size of the matrix
print(air.shape)

# wouldn't it be nice to just remove everything which is not needed?
# Here you go ...
air.cut_bounding_box()
map_plot(air, title='after cutting', use_basemap=True)
# -*- coding: utf-8 -*-

"""
This file is part of pyCMBS.
(c) 2012- Alexander Loew
For COPYING and LICENSE details, please refer to the LICENSE file
"""

"""
Basic plotting in pyCMBS
"""

from pycmbs.mapping import map_plot
import matplotlib.pyplot as plt
from pycmbs.examples import download

# Read some sample data ...
air = download.get_sample_file(name='air')
air.label = 'air temperature'

# a quick plot as well as a projection plot
f1 = map_plot(air)  # unprojected
f2 = map_plot(air, use_basemap=True)  # projected
plt.show()




Example #22
0
This file is part of pyCMBS. (c) 2012-2014
For COPYING and LICENSE details, please refer to the file
COPYRIGHT.md
"""

from pycmbs.icon import Icon
from pycmbs.mapping import map_plot
import matplotlib.pyplot as plt

plt.close('all')

##### PLEASE NOTE THAT THIS EXAMPLE IS CURRENTLY LIMITED USERS WITH ACCESS TO SPECIFIC DATA

#//// read data ////
gridfile = '../..//example_data/icon/r2b4_amip.nc'
datafile = '../../example_data/icon/rms0006_atm_phy_DOM01_ML_0001.nc'

#/// read data ///
IC = Icon(datafile, gridfile, 'rsns')
IC.read()
IC.label = 'shortwave net flux at surface'
IC.unit = '$W/^2$'

#/// Do plotting ///
print('Doing first plot ...')
map_plot(IC, use_basemap=True)
print('Doing second plot ...')
map_plot(IC, use_basemap=False)

plt.show()
Example #23
0
from pycmbs.examples import download
from pycmbs.mapping import map_plot

plt.close('all')

air = download.get_sample_file(name='air')
air.label = 'air temperature'

# generate some artificial data by rescaling the original data
air2 = air.copy()  # copy data object
air2.mulc(1.3, copy=False)  # copy=False applies operation directly on data, otherwise a copy is returned

# difference
print 'Calculate differences ...'
diff = air.sub(air2)  # calculate difference
f1 = map_plot(diff, cmap_data = 'RdBu_r', vmin=-10., vmax=10.)

# temporal evolution of mean and stdv
print 'Temporal evolution of mean and stdv of a field; note that values are calculated as area weigted values'
f = plt.figure()
ax = f.add_subplot(111)
ax.plot(air.fldmean(), label='fldmean()')
ax.plot(air.fldstd(), label='fldstd()')
ax.legend()
ax.grid()

plt.show()


# 1) calculate the weights ...
#     these are dependent on the number of days  which you get as ...

ml = air._get_days_per_month()
print ml

w = ml / float(sum(ml))  # relative weights
print w
print 'The sum of the weights should be 1.: ', sum(w)

# 2) now we need to multiply the data with the weights and then
#  sum up over time
new = air.mul_tvec(w,
                   copy=True)  # check also the other options of this routine!
res = new.timsum(return_object=True)  # gives a 2D array with weighted results
print res.shape

map_plot(air, title='classical unweighted temporal mean', show_stat=True)
map_plot(res, title='weighted temporal mean', show_stat=True)
map_plot(air.sub(res),
         title='difference',
         show_stat=True,
         cmap_data='RdBu_r',
         vmin=-0.1,
         vmax=0.1)

# you will see that the differences are marginal, but they are nevertheless there
# for this example.

plt.show()
Example #25
0
# just print the region keys for illustration
for k in RS.regions.keys():
    print k

# if you now want to generate a particular mask we can do that
# in the following example we mask the airt temperature for the
# Tibetean plateau

# load the air temperature data
air = download.get_sample_file(name='air')
air.label = 'air temperature'
org = air.copy()

# and then mask it
r_tibet = RS.regions[1]  # gives a Region object
map_plot(air, title='before')
air1 = air.copy()

# mask with region
air.mask_region(r_tibet)
map_plot(air, title='after')
#~ b = air1.mask_region(r_tibet, return_object=True) # this is an alternative which will keep the original data unchanged

# probably you want to save you rmask in a file. If you do this it is much more efficient if you want to apply it again.
file_to_store_mask = tempfile.mktemp(
    suffix='.nc')  # use temporary file here for this example

x = org.copy()
y = x.mask_region(
    r_tibet, maskfile=file_to_store_mask, return_object=True
)  #this will store the mask in a netcdf file --> have a look at it!