# Homework Problem \#2 - Arctic Ice

AI悦创原创python 1v11v1物理python 1v11v1物理大约 6 分钟...约 1837 字

Hand-in format: IPython Notebook or python program. Submit via email.

As a reminder: please make sure your code is clean, documentated, and understandable. Make sure it runs without errors.

## Background

The purpose of this problem is to become familiar with loading, manipulating, and analyzing image-like data, plotting it. We will use a dataset collected by the AMSR-E instrument Aquaopen in new window satellite.

The data consists of maps of the concentration of ice in the Arctic collected between 2006 and 2011. The data obtained from the amsr databaseopen in new window and converted into a single HDF5 file format.

## Part 1 - Examining a single map

Begin by examining the HDF5 file - you can use h5ls at the command line, or h5py inside the notebook.

If you don't remember how to open HDF5 files, and read datasets from HDF5 files, look at our Day 2 lecture.

There are many datasets, each with a name of the format YYYYMMDD, giving the data. Each dataset is a single map (i.e. 2D array), where the values give the ice concentration (fraction, from 0.0 to 100.0) in that pixel of the map. Careful of NaN values!

Read one of the maps, and plot it with Matplotlib.

Note: to get the correct orientation, you need the origin='lower' argument for imshow(). Include a colorbar. Remove the tick labels (0, 100, and so on, indicating pixel number) since they are not useful.

## 答案

# -*- coding: utf-8 -*-
# @Time    : 2022/10/6 18:26
# @Author  : AI悦创
# @FileName: hw2.py
# @Software: PyCharm
# @Blog    ：https://bornforthis.cn/
import h5py

# f = h5py.File('p2_icedata_area.hdf5', 'r')
# # f = h5py.File('p2_icedata.hdf5', 'r')
# print(f)
# print(f.keys())
# dataset = f["pixel_areas"]
# print(dataset)
# print(dataset.shape)
# print(dataset.dtype)

f = h5py.File(path, 'r')
keys = f.keys()
for key in keys:
# print(key)
dataset = f[key]
print(key, dataset.shape, dataset.dtype, sep="\t")

if __name__ == '__main__':


import matplotlib.pyplot as plt
import h5py
year_lst = []
dataset_lst = []
f = h5py.File(path, 'r')
keys = f.keys()
for key in keys:
# print(key)
dataset = f[key]
#         print(key, dataset.shape, dataset.dtype, sep="\t")
year_lst.append(key)
dataset_lst.append(dataset.shape)
# "g" 表示红色，marksize用来设置'D'菱形的大小
plt.plot(year_lst, dataset_lst, "g", marker='D', markersize=5, label="year")
# 绘制坐标轴标签
plt.xlabel("年")
plt.ylabel("数据")
plt.title("HDF5")
# 显示图例
plt.legend(loc="lower right")
# 调用 text()在图像上绘制注释文本
# x1、y1表示文本所处坐标位置，ha参数控制水平对齐方式, va控制垂直对齐方式，str(y1)表示要绘制的文本
#     for x1, y1 in zip(year_lst, dataset_lst):
#         plt.text(x1, y1, str(y1), ha='center', va='bottom', fontsize=10)
# 保存图片
plt.savefig("1.jpg")
plt.show()



## Part 2 - Ice concentration versus time

We want to make a plot of the ice concentration over time.

First, write a loop to read all the datasets of the HDF5 file (e.g. into a dict).

Then, write an analysis function frac_pixels_above(dict,value) which, for each array in the input dict, computes the fraction of pixels above the input value. Use this to make a plot of the number of pixels with concentration above 50%, versus time.

Note: to include "time" on the x-axis of a plot, you may want to write a helper function to convert the dict keys from their YYYYMMDD string format into a 3-tuple of (year, month, day) integer values.

This can then be converted into fractional years (e.g. 1 July 2012 is 2012.5). For simplicity you can assume each month has 30 days.

Try experimenting with matplotlib set_major_formatter to get a good representation of dates in the tick labelsopen in new window.

Describe what you see in the plot.

def read_hdf5(path):
# year_lst = []
dataset_lst = []
f = h5py.File(path, 'r')
keys = f.keys()
for key in keys:
# print(key)
dataset = f[key]
# print(key, dataset.shape, dataset.dtype, sep="\t")
# year_lst.append(key)
dataset_lst.append((key, dataset.shape))
return dict(dataset_lst)


## Part 3 - Physical units

To be more quantitative we will compute the actual surface area of Earth in $\rm{km}^2$ over which the ice concentration is above a given threshold.

However, these maps are projections of a spherical surface, so pixels have different areaopen in new window.

Every map uses the same projection, so the pixel areas in each are the same.

The areas (in $\rm{km}^2$) are available in the file named data/p2_icedata_area.hdf5. Inspect, then load, this datafile. Plot it (with colorbar and units).

## 文件读取

AI悦创·编程一对一

AI悦创·推出辅导班啦，包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发」，全部都是一对一教学：一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然，还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线，随时响应！微信：Jiabcdefh

C++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

• 0
• 0
• 0
• 0
• 0
• 0

• 按正序
• 按倒序
• 按热度