In [ ]:
class A():
pass
In [ ]:
a = A() # create an instance of class A
print (a)
print (type(a))
In [ ]:
class Human(object):
name = ''
age = 0
In [ ]:
human1 = Human() # create instance of Human
human1.name = 'Anton' # name him (add data to this object)
human1.age = 39 # set the age (add data to this object)
print (type(human1))
print (human1.name)
print (human1.age)
In [ ]:
class Human(object):
name = ''
age = 0
def __init__(self, name):
self.name = name
In [ ]:
h1 = Human('Anton')
print (h1.name)
print (h1.age)
In [ ]:
class Human(object):
''' Human being '''
name = ''
age = 0
def __init__(self, name):
''' Create a Human '''
self.name = name
def grow(self):
''' Grow a Human by one year (in-place) '''
self.age += 1
In [ ]:
human1 = Human('Adam')
human1.grow()
print (human1.name)
print (human1.age)
In [ ]:
class Human(object):
''' Human being '''
name = ''
age = 0
def __init__(self, name):
''' Create a Human '''
self.name = name
def grow(self):
''' Grow a Human by one year (in-place) '''
self.age += 1
def get_name(self):
''' Return name of a Human '''
return self.name
def get_age(self):
''' Return name of a Human '''
return self.age
In [ ]:
h1 = Human('Eva')
print (h1.get_name())
In [ ]:
class Teacher(Human):
''' Teacher of Python '''
def give_lecture(self):
''' Print lecture on the screen '''
print ('bla bla bla')
In [ ]:
t1 = Teacher('Anton')
while t1.get_age() < 50:
t1.grow()
print (t1.get_name())
print (t1.get_age())
t1.give_lecture()
Store class definition in a separate file. E.g.:
https://github.com/nansencenter/nansat-lectures/blob/master/human_teacher.py
In [1]:
# add directory scripts to PYTHONPATH (searchable path)
import sys
sys.path.append('scripts')
In [2]:
from human_teacher import Teacher
t1 = Teacher('Morten')
t1.give_lecture()
In [3]:
## add scripts to the list of searchable directories
import sys
sys.path.append('scripts')
# import class definiton our module
from ts_profile import Profile
# load data
p = Profile('data/tsprofile.txt')
# work with the object
print (p.get_ts_at_level(5))
print (p.get_ts_at_depth(200))
print (p.get_mixed_layer_depth(.1))
In [ ]:
from st_profile import load_profile, get_ts_at_level, get_ts_at_depth
from st_profile import get_mixed_layer_depth, plot_ts
In [ ]:
depth, temp, sal = load_profile('tsprofile.txt')
print (get_ts_at_level(depth, temp, sal))
In [ ]:
from nansat import Nansat
n = Nansat('satellite_filename.hdf')
'tsprofile.txt' contains three columns of synthetic values: depth (H), temperature (T), salinity (S)
http://192.168.33.10:8888/edit/data/tsprofile.txt
Add attributes temperature
, salinity
, depth
of type list
Use f = open(fileName)
and lines = f.readlines()
to read data from file
If you have two measurements of e.g. temperature (t1 and t1) at two depths (d1 and d2) you can linearly interpolate between these values and calculate a temperature (t) at depth (d):
t = t1 + (d - d1) * (t2 - t1) / (d2 - d1)
Get the nansat-lectures course material at Github: 'git clone https://github.com/nansencenter/nansat-lectures' To get stay updated: 'git pull'
In [ ]: