Skip to content

Instantly share code, notes, and snippets.

@fehiepsi
Last active March 17, 2018 13:55
Show Gist options
  • Save fehiepsi/08471b6f63c9eaef3c762f30dc263088 to your computer and use it in GitHub Desktop.
Save fehiepsi/08471b6f63c9eaef3c762f30dc263088 to your computer and use it in GitHub Desktop.
Hướng dẫn đọc file .nc
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Hướng dẫn cách đọc opencdf file bằng python\n",
"\n",
"1. Cài conda, tạo môi trường tên pydata: `conda create -n pydata`, kích hoạt môi trường pydata: `source activate pydata`\n",
"2. Cài python 3.5 và các thư viện cần thiết:\n",
"```sh\n",
"conda install python=3.5 jupyter xarray netcdf4\n",
"conda install -c conda-forge netcdftime --no-deps\n",
"```\n",
"3. Download 1 số file mẫu từ trang https://www.unidata.ucar.edu/software/netcdf/examples/files.html\n",
"4. Chạy `jupyter notebook` và download file notebook ví dụ này: https://gist.github.com/fehiepsi/08471b6f63c9eaef3c762f30dc263088\n",
"5. Tham khảo thêm cách sử dụng xarray ở: http://xarray.pydata.org/en/stable/"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import xarray"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<xarray.Dataset>\n",
"Dimensions: (bnds: 2, lat: 128, lon: 256, plev: 17, time: 1)\n",
"Coordinates:\n",
" * lat (lat) float32 -88.927734 -87.538704 -86.14147 -84.742386 ...\n",
" * lon (lon) float32 0.0 1.40625 2.8125 4.21875 5.625 7.03125 8.4375 ...\n",
" * plev (plev) float64 1e+05 9.25e+04 8.5e+04 7e+04 6e+04 5e+04 4e+04 ...\n",
" * time (time) datetime64[ns] 2000-05-16T12:00:00\n",
"Dimensions without coordinates: bnds\n",
"Data variables:\n",
" area (lat, lon) float32 ...\n",
" lat_bnds (lat, bnds) float64 ...\n",
" lon_bnds (lon, bnds) float64 ...\n",
" msk_rgn (lat, lon) int32 ...\n",
" pr (time, lat, lon) float32 ...\n",
" tas (time, lat, lon) float32 ...\n",
" time_bnds (time, bnds) float64 ...\n",
" ua (time, plev, lat, lon) float32 ...\n",
"Attributes:\n",
" CVS_Id: $Id$\n",
" creation_date: \n",
" prg_ID: Source file unknown Version unknown Date unknown\n",
" cmd_ln: bds -x 256 -y 128 -m 23 -o /data/zender/data/dst_T85.nc\n",
" history: Tue Oct 25 15:08:51 2005: ncks -O -x -v va -m sresa1...\n",
" table_id: Table A1\n",
" title: model output prepared for IPCC AR4\n",
" institution: NCAR (National Center for Atmospheric \\nResearch, Bo...\n",
" source: CCSM3.0, version beta19 (2004): \\natmosphere: CAM3.0...\n",
" contact: [email protected]\n",
" project_id: IPCC Fourth Assessment\n",
" Conventions: CF-1.0\n",
" references: Collins, W.D., et al., 2005:\\n The Community Climate...\n",
" acknowledgment: Any use of CCSM data should acknowledge the contrib...\n",
" realization: 1\n",
" experiment_id: 720 ppm stabilization experiment (SRESA1B)\n",
" comment: This simulation was initiated from year 2000 of \\n C...\n",
" model_name_english: NCAR CCSM"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ds = xarray.open_dataset(\"sresa1b_ncar_ccsm3-example.nc\")\n",
"ds"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<xarray.Dataset>\n",
"Dimensions: (bnds: 2, lat: 170, lon: 180, time: 24)\n",
"Coordinates:\n",
" * lon (lon) float64 1.0 3.0 5.0 7.0 9.0 11.0 13.0 15.0 17.0 19.0 ...\n",
" * lat (lat) float64 -79.5 -78.5 -77.5 -76.5 -75.5 -74.5 -73.5 -72.5 ...\n",
" * time (time) datetime64[ns] 2001-01-16 2001-02-16 2001-03-16 ...\n",
"Dimensions without coordinates: bnds\n",
"Data variables:\n",
" lon_bnds (lon, bnds) float64 ...\n",
" lat_bnds (lat, bnds) float64 ...\n",
" time_bnds (time, bnds) float64 ...\n",
" tos (time, lat, lon) float32 ...\n",
"Attributes:\n",
" title: IPSL model output prepared for IPCC Fourth Assessment SR...\n",
" institution: IPSL (Institut Pierre Simon Laplace, Paris, France)\n",
" source: IPSL-CM4_v1 (2003) : atmosphere : LMDZ (IPSL-CM4_IPCC, 96...\n",
" contact: Sebastien Denvil, [email protected]\n",
" project_id: IPCC Fourth Assessment\n",
" table_id: Table O1 (13 November 2004)\n",
" experiment_id: SRES A2 experiment\n",
" realization: 1\n",
" cmor_version: 0.96\n",
" Conventions: CF-1.0\n",
" history: YYYY/MM/JJ: data generated; YYYY/MM/JJ+1 data transformed...\n",
" references: Dufresne et al, Journal of Climate, 2015, vol XX, p 136\n",
" comment: Test drive"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ds2 = xarray.open_dataset(\"tos_O1_2001-2002.nc\")\n",
"ds2"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment