hima8_pv/读tif获得.ipynb

168 lines
5.9 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"outputs": [],
"source": [
"import os\n",
"import pandas as pd\n",
"import netCDF4 as nc\n",
"import numpy as np\n",
"import datetime as dt\n",
"from osgeo import gdal"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": 2,
"outputs": [],
"source": [
"file_path = \"data/2022-07-22/PAR_Minutes_Download/\""
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": 3,
"outputs": [],
"source": [
"target_lon = '120.85'\n",
"target_lat = '29.6'"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": 7,
"outputs": [],
"source": [
"par_list = list()\n",
"file_list = [x for x in os.listdir(file_path) if x.endswith('.nc')]\n",
"for file in file_list:\n",
" file_name = f\"{file_path}{file}\"\n",
" date = file.split('_')[1] + ':' + file.split('_')[2]\n",
" nc_data = nc.Dataset(file_name)\n",
" lat = nc_data['latitude'][:] # 纬度\n",
" lon = nc_data['longitude'][:] # 经度\n",
" lat_str = list(map(lambda x: str(round(x, 2)), np.asarray(nc_data['latitude'][:])))\n",
" lon_str = list(map(lambda x: str(round(x, 2)), np.asarray(nc_data['longitude'][:])))\n",
" latMin, latMax, lonMin, lonMax = min(lat), max(lat), min(lon), max(lon)\n",
" # 分辨率\n",
" lat_Res = (latMax - latMin) / (lat.shape[0]-1)\n",
" lon_Res = (lonMax - lonMin) / (lon.shape[0]-1)\n",
" lon_index = lon_str.index(target_lon)\n",
" lat_index = lat_str.index(target_lat)\n",
" par = np.asarray(nc_data['PAR'][:])\n",
" par_list.append([date, par[lat_index][lon_index]])"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"par_df = pd.DataFrame.from_records(par_list, columns=['time', 'par'])"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": 20,
"outputs": [],
"source": [
"par_df.time = par_df.time.apply(lambda x: dt.datetime.strptime(x, '%Y%m%d:%H%M') + dt.timedelta(hours=8))"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": 21,
"outputs": [
{
"data": {
"text/plain": " time par\ntime \n2022-07-22 08:00:00 20220722:0000 1146.900024\n2022-07-22 08:10:00 20220722:0010 1217.099976\n2022-07-22 08:20:00 20220722:0020 1288.800049\n2022-07-22 08:30:00 20220722:0030 1354.800049\n2022-07-22 08:40:00 20220722:0040 1419.900024\n... ... ...\n2022-07-23 07:10:00 20220722:2310 778.400024\n2022-07-23 07:20:00 20220722:2320 854.799988\n2022-07-23 07:30:00 20220722:2330 928.000000\n2022-07-23 07:40:00 20220722:2340 1011.000000\n2022-07-23 07:50:00 20220722:2350 1081.800049\n\n[141 rows x 2 columns]",
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>time</th>\n <th>par</th>\n </tr>\n <tr>\n <th>time</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>2022-07-22 08:00:00</th>\n <td>20220722:0000</td>\n <td>1146.900024</td>\n </tr>\n <tr>\n <th>2022-07-22 08:10:00</th>\n <td>20220722:0010</td>\n <td>1217.099976</td>\n </tr>\n <tr>\n <th>2022-07-22 08:20:00</th>\n <td>20220722:0020</td>\n <td>1288.800049</td>\n </tr>\n <tr>\n <th>2022-07-22 08:30:00</th>\n <td>20220722:0030</td>\n <td>1354.800049</td>\n </tr>\n <tr>\n <th>2022-07-22 08:40:00</th>\n <td>20220722:0040</td>\n <td>1419.900024</td>\n </tr>\n <tr>\n <th>...</th>\n <td>...</td>\n <td>...</td>\n </tr>\n <tr>\n <th>2022-07-23 07:10:00</th>\n <td>20220722:2310</td>\n <td>778.400024</td>\n </tr>\n <tr>\n <th>2022-07-23 07:20:00</th>\n <td>20220722:2320</td>\n <td>854.799988</td>\n </tr>\n <tr>\n <th>2022-07-23 07:30:00</th>\n <td>20220722:2330</td>\n <td>928.000000</td>\n </tr>\n <tr>\n <th>2022-07-23 07:40:00</th>\n <td>20220722:2340</td>\n <td>1011.000000</td>\n </tr>\n <tr>\n <th>2022-07-23 07:50:00</th>\n <td>20220722:2350</td>\n <td>1081.800049</td>\n </tr>\n </tbody>\n</table>\n<p>141 rows × 2 columns</p>\n</div>"
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"par_df"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}