nc文件python读取变量值

# -*- coding: utf-8 -*-
import netCDF4,numpy
from netCDF4 import Dataset
a=Dataset('/home/rosfun/Downloads/olr.mon.mean.nc')
print(a.variables.keys())

dict_keys(['lon', 'lat', 'time', 'olr'])

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
import numpy as np
import netCDF4
from netCDF4 import Dataset
nc_obj=Dataset('/home/rosfun/Downloads/olr.mon.mean.nc')
print(nc_obj.variables.keys())

#查看nc文件有些啥东东
print(nc_obj)
print('---------------------------------------')

#查看nc文件中的变量
print(nc_obj.variables.keys())
for i in nc_obj.variables.keys():
    print(i)
print('---------------------------------------')

#查看每个变量的信息
print(nc_obj.variables['lat'])
print(nc_obj.variables['lon'])
print(nc_obj.variables['time'])
print(nc_obj.variables['olr'])

# print(nc_obj.variables['obs_operator'])
# print(nc_obj.variables['obs_err_var'])
# print('---------------------------------------')

'''

#查看每个变量的属性
print(nc_obj.variables['LAT'].ncattrs())
print(nc_obj.variables['LON'].ncattrs())
print(nc_obj.variables['PRCP'].ncattrs())
print(nc_obj.variables['LAT'].units)
print(nc_obj.variables['LON'].units)
print(nc_obj.variables['PRCP']._Fillvalue)
print('---------------------------------------')

'''

#读取数据值
lat=(nc_obj.variables['lat'][:])
lon=(nc_obj.variables['lon'][:])
time=(nc_obj.variables['time'][:])
olr=(nc_obj.variables['olr'][:])

# Obs_Operator=(nc_obj.variables['obs_operator'][:])
# Obs_Err_var=(nc_obj.variables['obs_err_var'][:])

# Xb = [0,0,0]
# for i in range(len(Prior)):
#     xb = []
#     for j in range(len(Prior[0,0,0,:])):
# #         print("j = " , j)
#         xi = Prior[i,:,:,j]
#         xim = np.mean(xi)
# #         print("xi = " , xi)
#         xb.append(xim)
# #     print("xb = " , xb)
#     Xb = np.c_[Xb,xb]
# Xb = Xb.T
# Xb = Xb[1:,:]
# print("Prior = " , Xb , Xb.shape)

# Xa = [0,0,0]
# for i in range(len(Posterior)):
#     xa = []
#     for j in range(len(Posterior[0,0,0,:])):
# #         print("j = " , j)
#         xi = Posterior[i,:,:,j]
#         xim = np.mean(xi)
# #         print("xi = " , xi)
#         xa.append(xim)
# #     print("xb = " , xb)
#     Xa = np.c_[Xa,xa]
# Xa = Xa.T
# Xa = Xa[1:,:]
# print("Posterior = " , Xa , Xa.shape)

print("lat = "+ '\n',lat , lat.shape)
print('-------------------******-------------------------')
print("lon = "+ '\n',lon , lon.shape)
print('-------------------******-------------------------')
print("time = "+ '\n',time , time.shape)
print('-------------------******-------------------------')
print("olr = "+ '\n',olr , olr.shape)
print('-------------------******-------------------------')
# print("Obs_Operator = "+ '\n',Obs_Operator)
# print('-------------------******-------------------------')
# print("Obs_Err_var = "+ '\n',Obs_Err_var)

dict_keys(['lon', 'lat', 'time', 'olr'])

root group (NETCDF3_CLASSIC data model, file format NETCDF3):
    title: Monthly means of OLR from interpolated OLR dataset
    history: Created from daily OLR files obtained at NCEP and further processed. Stored in netCDF in 1996. Last update 10/2003
    description: Data is interpolated in time and space from NOAA twice-daily OLR values and averaged to once daily (from which means are calculated)
    platform: Observation
    Conventions: CF-1.2
    References: https://www.esrl.noaa.gov/psd/data/gridded/data.interp_OLR.html
    references: https://www.esrl.noaa.gov/psd/data/gridded/data.interp_OLR.html
    Citation: Liebmann and Smith: June 2006: Description of a Complete (Interpolated) Outgoing Longwave Radiation Dataset. Bulletin of the American Meteorological Society, 77, 1275-1277
    dataset_title: NOAA Interpolated Outgoing Longwave Radiation (OLR)
    source: ftp.cpc.ncep.noaa.gov precip/noaa..
    date_modified: 12 Feb 2019
    dimensions(sizes): lon(144), lat(73), nmiss(7), time(535)
    variables(dimensions): float32 lon(lon), float32 lat(lat), float64 time(time), int16 olr(time,lat,lon)
    groups: 
---------------------------------------
dict_keys(['lon', 'lat', 'time', 'olr'])
lon
lat
time
olr
---------------------------------------

float32 lat(lat)
    units: degrees_north
    actual_range: [ 90. -90.]
    long_name: Latitude
    standard_name: latitude
    axis: Y
unlimited dimensions: 
current shape = (73,)
filling on, default _FillValue of 9.969209968386869e+36 used

float32 lon(lon)
    units: degrees_east
    long_name: Longitude
    actual_range: [  0. 360.]
    standard_name: longitude
    axis: X
unlimited dimensions: 
current shape = (144,)
filling on, default _FillValue of 9.969209968386869e+36 used

float64 time(time)
    units: hours since 1800-01-01 00:00:0.0
    long_name: Time
    actual_range: [1528872. 1918968.]
    delta_t: 0000-01-00 00:00:00
    avg_period: 0000-01-00 00:00:00
    standard_name: time
    axis: T
unlimited dimensions: time
current shape = (535,)
filling on, default _FillValue of 9.969209968386869e+36 used

int16 olr(time, lat, lon)
    long_name: OLR monthly means
    unpacked_valid_range: [  0. 500.]
    actual_range: [138.80646 297.41354]
    units: W/m^2
    add_offset: 327.65
    scale_factor: 0.01
    missing_value: 32766
    var_desc: Outgoing Longwave Radiation
    precision: 2
    dataset: NOAA Interpolated OLR
    level_desc: Other
    statistic: Mean
    parent_stat: Individual Obs
    valid_range: [-32765  17235]
unlimited dimensions: time
current shape = (535, 73, 144)
filling on, default _FillValue of -32767 used
lat = 
 [ 90.   87.5  85.   82.5  80.   77.5  75.   72.5  70.   67.5  65.   62.5
  60.   57.5  55.   52.5  50.   47.5  45.   42.5  40.   37.5  35.   32.5
  30.   27.5  25.   22.5  20.   17.5  15.   12.5  10.    7.5   5.    2.5
   0.   -2.5  -5.   -7.5 -10.  -12.5 -15.  -17.5 -20.  -22.5 -25.  -27.5
 -30.  -32.5 -35.  -37.5 -40.  -42.5 -45.  -47.5 -50.  -52.5 -55.  -57.5
 -60.  -62.5 -65.  -67.5 -70.  -72.5 -75.  -77.5 -80.  -82.5 -85.  -87.5
 -90. ] (73,)
-------------------******-------------------------
lon = 
 [  0.    2.5   5.    7.5  10.   12.5  15.   17.5  20.   22.5  25.   27.5
  30.   32.5  35.   37.5  40.   42.5  45.   47.5  50.   52.5  55.   57.5
  60.   62.5  65.   67.5  70.   72.5  75.   77.5  80.   82.5  85.   87.5
  90.   92.5  95.   97.5 100.  102.5 105.  107.5 110.  112.5 115.  117.5
 120.  122.5 125.  127.5 130.  132.5 135.  137.5 140.  142.5 145.  147.5
 150.  152.5 155.  157.5 160.  162.5 165.  167.5 170.  172.5 175.  177.5
 180.  182.5 185.  187.5 190.  192.5 195.  197.5 200.  202.5 205.  207.5
 210.  212.5 215.  217.5 220.  222.5 225.  227.5 230.  232.5 235.  237.5
 240.  242.5 245.  247.5 250.  252.5 255.  257.5 260.  262.5 265.  267.5
 270.  272.5 275.  277.5 280.  282.5 285.  287.5 290.  292.5 295.  297.5
 300.  302.5 305.  307.5 310.  312.5 315.  317.5 320.  322.5 325.  327.5
 330.  332.5 335.  337.5 340.  342.5 345.  347.5 350.  352.5 355.  357.5] (144,)
-------------------******-------------------------
time = 
 [1528872. 1529592. 1530336. 1531080. 1531800. 1532544. 1533264. 1534008.
 1534752. 1535424. 1536168. 1536888. 1537632. 1538352. 1539096. 1539840.
 1540560. 1541304. 1542024. 1542768. 1543512. 1544208. 1544952. 1545672.
 1546416. 1547136. 1547880. 1548624. 1549344. 1550088. 1550808. 1551552.
 1552296. 1552968. 1553712. 1554432. 1555176. 1555896. 1556640. 1557384.
 1558104. 1558848. 1559568. 1560312. 1561056. 1561728. 1562472. 1563192.
 1563936. 1564656. 1565400. 1566144. 1566864. 1567608. 1568328. 1569072.
 1569816. 1570488. 1571232. 1571952. 1572696. 1573416. 1574160. 1574904.
 1575624. 1576368. 1577088. 1577832. 1578576. 1579272. 1580016. 1580736.
 1581480. 1582200. 1582944. 1583688. 1584408. 1585152. 1585872. 1586616.
 1587360. 1588032. 1588776. 1589496. 1590240. 1590960. 1591704. 1592448.
 1593168. 1593912. 1594632. 1595376. 1596120. 1596792. 1597536. 1598256.
 1599000. 1599720. 1600464. 1601208. 1601928. 1602672. 1603392. 1604136.
 1604880. 1605552. 1606296. 1607016. 1607760. 1608480. 1609224. 1609968.
 1610688. 1611432. 1612152. 1612896. 1613640. 1614336. 1615080. 1615800.
 1616544. 1617264. 1618008. 1618752. 1619472. 1620216. 1620936. 1621680.
 1622424. 1623096. 1623840. 1624560. 1625304. 1626024. 1626768. 1627512.
 1628232. 1628976. 1629696. 1630440. 1631184. 1631856. 1632600. 1633320.
 1634064. 1634784. 1635528. 1636272. 1636992. 1637736. 1638456. 1639200.
 1639944. 1640616. 1641360. 1642080. 1642824. 1643544. 1644288. 1645032.
 1645752. 1646496. 1647216. 1647960. 1648704. 1649400. 1650144. 1650864.
 1651608. 1652328. 1653072. 1653816. 1654536. 1655280. 1656000. 1656744.
 1657488. 1658160. 1658904. 1659624. 1660368. 1661088. 1661832. 1662576.
 1663296. 1664040. 1664760. 1665504. 1666248. 1666920. 1667664. 1668384.
 1669128. 1669848. 1670592. 1671336. 1672056. 1672800. 1673520. 1674264.
 1675008. 1675680. 1676424. 1677144. 1677888. 1678608. 1679352. 1680096.
 1680816. 1681560. 1682280. 1683024. 1683768. 1684464. 1685208. 1685928.
 1686672. 1687392. 1688136. 1688880. 1689600. 1690344. 1691064. 1691808.
 1692552. 1693224. 1693968. 1694688. 1695432. 1696152. 1696896. 1697640.
 1698360. 1699104. 1699824. 1700568. 1701312. 1701984. 1702728. 1703448.
 1704192. 1704912. 1705656. 1706400. 1707120. 1707864. 1708584. 1709328.
 1710072. 1710744. 1711488. 1712208. 1712952. 1713672. 1714416. 1715160.
 1715880. 1716624. 1717344. 1718088. 1718832. 1719528. 1720272. 1720992.
 1721736. 1722456. 1723200. 1723944. 1724664. 1725408. 1726128. 1726872.
 1727616. 1728288. 1729032. 1729752. 1730496. 1731216. 1731960. 1732704.
 1733424. 1734168. 1734888. 1735632. 1736376. 1737048. 1737792. 1738512.
 1739256. 1739976. 1740720. 1741464. 1742184. 1742928. 1743648. 1744392.
 1745136. 1745808. 1746552. 1747272. 1748016. 1748736. 1749480. 1750224.
 1750944. 1751688. 1752408. 1753152. 1753896. 1754592. 1755336. 1756056.
 1756800. 1757520. 1758264. 1759008. 1759728. 1760472. 1761192. 1761936.
 1762680. 1763352. 1764096. 1764816. 1765560. 1766280. 1767024. 1767768.
 1768488. 1769232. 1769952. 1770696. 1771440. 1772112. 1772856. 1773576.
 1774320. 1775040. 1775784. 1776528. 1777248. 1777992. 1778712. 1779456.
 1780200. 1780872. 1781616. 1782336. 1783080. 1783800. 1784544. 1785288.
 1786008. 1786752. 1787472. 1788216. 1788960. 1789656. 1790400. 1791120.
 1791864. 1792584. 1793328. 1794072. 1794792. 1795536. 1796256. 1797000.
 1797744. 1798416. 1799160. 1799880. 1800624. 1801344. 1802088. 1802832.
 1803552. 1804296. 1805016. 1805760. 1806504. 1807176. 1807920. 1808640.
 1809384. 1810104. 1810848. 1811592. 1812312. 1813056. 1813776. 1814520.
 1815264. 1815936. 1816680. 1817400. 1818144. 1818864. 1819608. 1820352.
 1821072. 1821816. 1822536. 1823280. 1824024. 1824720. 1825464. 1826184.
 1826928. 1827648. 1828392. 1829136. 1829856. 1830600. 1831320. 1832064.
 1832808. 1833480. 1834224. 1834944. 1835688. 1836408. 1837152. 1837896.
 1838616. 1839360. 1840080. 1840824. 1841568. 1842240. 1842984. 1843704.
 1844448. 1845168. 1845912. 1846656. 1847376. 1848120. 1848840. 1849584.
 1850328. 1851000. 1851744. 1852464. 1853208. 1853928. 1854672. 1855416.
 1856136. 1856880. 1857600. 1858344. 1859088. 1859784. 1860528. 1861248.
 1861992. 1862712. 1863456. 1864200. 1864920. 1865664. 1866384. 1867128.
 1867872. 1868544. 1869288. 1870008. 1870752. 1871472. 1872216. 1872960.
 1873680. 1874424. 1875144. 1875888. 1876632. 1877304. 1878048. 1878768.
 1879512. 1880232. 1880976. 1881720. 1882440. 1883184. 1883904. 1884648.
 1885392. 1886064. 1886808. 1887528. 1888272. 1888992. 1889736. 1890480.
 1891200. 1891944. 1892664. 1893408. 1894152. 1894848. 1895592. 1896312.
 1897056. 1897776. 1898520. 1899264. 1899984. 1900728. 1901448. 1902192.
 1902936. 1903608. 1904352. 1905072. 1905816. 1906536. 1907280. 1908024.
 1908744. 1909488. 1910208. 1910952. 1911696. 1912368. 1913112. 1913832.
 1914576. 1915296. 1916040. 1916784. 1917504. 1918248. 1918968.] (535,)
-------------------******-------------------------
olr = 
 [[[207.6099853515625 207.6099853515625 207.6099853515625 ...
   207.6099853515625 207.6099853515625 207.6099853515625]
  [208.64999389648438 208.55999755859375 208.47000122070312 ...
   208.91000366210938 208.82998657226562 208.739990234375]
  [210.51998901367188 210.20999145507812 209.91000366210938 ...
   211.27999877929688 211.05999755859375 210.7899932861328]
  ...
  [118.5 117.85000610351562 117.24000549316406 ... 120.33999633789062
   119.72000122070312 119.1300048828125]
  [119.44000244140625 119.19999694824219 118.97000122070312 ...
   120.1199951171875 119.8800048828125 119.67999267578125]
  [112.66999816894531 112.66999816894531 112.66999816894531 ...
   112.66999816894531 112.66999816894531 112.66999816894531]]

 [[209.32000732421875 209.32000732421875 209.32000732421875 ...
   209.32000732421875 209.32000732421875 209.32000732421875]
  [210.83999633789062 210.97999572753906 211.1199951171875 ...
   210.35000610351562 210.52999877929688 210.6999969482422]
  [214.7899932861328 214.95999145507812 215.10000610351562 ...
   214.17999267578125 214.41000366210938 214.6300048828125]
  ...
  [112.44999694824219 112.16000366210938 111.8699951171875 ...
   113.27000427246094 113.02000427246094 112.74000549316406]
  [110.58999633789062 110.42999267578125 110.22999572753906 ...
   111.08999633789062 110.94000244140625 110.77999877929688]
  [104.77999877929688 104.77999877929688 104.77999877929688 ...
   104.77999877929688 104.77999877929688 104.77999877929688]]

 [[207.16000366210938 207.16000366210938 207.16000366210938 ...
   207.16000366210938 207.16000366210938 207.16000366210938]
  [209.13999938964844 209.22999572753906 209.30999755859375 ...
   208.8800048828125 208.97000122070312 209.05999755859375]
  [211.10000610351562 211.22000122070312 211.30999755859375 ...
   210.64999389648438 210.82998657226562 210.97000122070312]
  ...
  [114.55000305175781 114.13999938964844 113.74000549316406 ...
   115.6300048828125 115.28999328613281 114.91000366210938]
  [113.47000122070312 113.31999206542969 113.17999267578125 ...
   113.83999633789062 113.72000122070312 113.60000610351562]
  [109.24000549316406 109.24000549316406 109.24000549316406 ...
   109.24000549316406 109.24000549316406 109.24000549316406]]

 ...

 [[188.77000427246094 188.77000427246094 188.77000427246094 ...
   188.77000427246094 188.77000427246094 188.77000427246094]
  [187.69000244140625 187.69000244140625 187.69000244140625 ...
   186.14999389648438 186.14999389648438 186.14999389648438]
  [188.739990234375 188.739990234375 188.739990234375 ...
   188.77000427246094 188.77000427246094 188.77000427246094]
  ...
  [125.91999816894531 125.91999816894531 125.91999816894531 ...
   129.4199981689453 129.4199981689453 129.4199981689453]
  [128.0 128.0 128.0 ... 129.08999633789062 129.08999633789062
   129.08999633789062]
  [127.1300048828125 127.1300048828125 127.1300048828125 ...
   127.1300048828125 127.1300048828125 127.1300048828125]]

 [[172.9199981689453 172.9199981689453 172.9199981689453 ...
   172.9199981689453 172.9199981689453 172.9199981689453]
  [172.02000427246094 172.02000427246094 172.02000427246094 ...
   170.3699951171875 170.3699951171875 170.3699951171875]
  [173.47999572753906 173.47999572753906 173.47999572753906 ...
   172.3300018310547 172.3300018310547 172.3300018310547]
  ...
  [146.52999877929688 146.52999877929688 146.52999877929688 ...
   150.14999389648438 150.14999389648438 150.14999389648438]
  [147.5 147.5 147.5 ... 149.86000061035156 149.86000061035156
   149.86000061035156]
  [145.27000427246094 145.27000427246094 145.27000427246094 ...
   145.27000427246094 145.27000427246094 145.27000427246094]]

 [[165.44000244140625 165.44000244140625 165.44000244140625 ...
   165.44000244140625 165.44000244140625 165.44000244140625]
  [163.91000366210938 163.91000366210938 163.91000366210938 ...
   162.94000244140625 162.94000244140625 162.94000244140625]
  [169.33999633789062 169.33999633789062 169.33999633789062 ...
   169.4499969482422 169.4499969482422 169.4499969482422]
  ...
  [173.19000244140625 173.19000244140625 173.19000244140625 ...
   174.38999938964844 174.38999938964844 174.38999938964844]
  [172.77000427246094 172.77000427246094 172.77000427246094 ...
   173.8199920654297 173.8199920654297 173.8199920654297]
  [169.11000061035156 169.11000061035156 169.11000061035156 ...
   169.11000061035156 169.11000061035156 169.11000061035156]]] (535, 73, 144)
-------------------******-------------------------

B = np.reshape(olr,(-1,144))
print(B)
print("B的维度:",B.shape)

B = np.reshape(olr,(-1,144))
print(B)
print("B的维度:",B.shape)
B = np.reshape(olr,(-1,144))
print(B)
print("B的维度:",B.shape)
[[207.6099853515625 207.6099853515625 207.6099853515625 ...
  207.6099853515625 207.6099853515625 207.6099853515625]
 [208.64999389648438 208.55999755859375 208.47000122070312 ...
  208.91000366210938 208.82998657226562 208.739990234375]
 [210.51998901367188 210.20999145507812 209.91000366210938 ...
  211.27999877929688 211.05999755859375 210.7899932861328]
 ...
 [173.19000244140625 173.19000244140625 173.19000244140625 ...
  174.38999938964844 174.38999938964844 174.38999938964844]
 [172.77000427246094 172.77000427246094 172.77000427246094 ...
  173.8199920654297 173.8199920654297 173.8199920654297]
 [169.11000061035156 169.11000061035156 169.11000061035156 ...
  169.11000061035156 169.11000061035156 169.11000061035156]]
B的维度: (39055, 144)

import numpy  
numpy.savetxt('/home/rosfun/Downloads/olr的经度.csv', lon, delimiter = ',') 
numpy.savetxt('/home/rosfun/Downloads/olr维度.csv', lat, delimiter = ',') 
numpy.savetxt('/home/rosfun/Downloads/olr的.csv', B, delimiter = ',')
numpy.savetxt('/home/rosfun/Downloads/olr时间间隔一个月.csv', time, delimiter = ',')

 

你可能感兴趣的:(nc文件python读取变量值)