{ "cells": [ { "cell_type": "markdown", "id": "e88537e9-cac0-47a3-bc57-519c6c69a5a1", "metadata": {}, "source": [ "# %load pres_temp_4D_wr\n", "# the Scientific Python netCDF 3 interface\n", "# http://dirac.cnrs-orleans.fr/ScientificPython/\n", "#from Scientific.IO.NetCDF import NetCDFFile as Dataset\n", "# the 'classic' version of the netCDF4 python interface\n", "# http://code.google.com/p/netcdf4-python/" ] }, { "cell_type": "markdown", "id": "6c7461e8-454d-4389-8a5a-fc1e2c9fd585", "metadata": {}, "source": [ "See tutorials at http://unidata.github.io/netcdf4-python/" ] }, { "cell_type": "code", "execution_count": 1, "id": "eb009511-7227-41bf-bf92-48d46c831f94", "metadata": {}, "outputs": [], "source": [ "from netCDF4 import Dataset\n", "from numpy import arange, dtype # array module from http://numpy.scipy.org" ] }, { "cell_type": "markdown", "id": "617f19a2-5f80-41c9-b550-09d55ac426b9", "metadata": {}, "source": [ "\"\"\"\n", "This is an example program which writes some 4D pressure and\n", "temperatures. \n", "The companion program pres_temp_4D_rd.py shows how\n", "to read the netCDF data file created by this program.\n", "\n", "This example demonstrates the netCDF Python API.\n", "It will work either with the Scientific Python NetCDF version 3 interface\n", "(http://dirac.cnrs-orleans.fr/ScientificPython/)\n", "of the 'classic' version of the netCDF4 interface. \n", "(http://netcdf4-python.googlecode.com/svn/trunk/docs/netCDF4_classic-module.html)\n", "To switch from one to another, just comment/uncomment the appropriate\n", "import statements at the beginning of this file.\n", "\n", "Jeff Whitaker 20070202\n", "\"\"\"" ] }, { "cell_type": "markdown", "id": "96b63ece-e0d3-4ded-a7a0-17605eb72c86", "metadata": {}, "source": [ "# the netCDF variable will be nrecs x nlevs x nlats x nlons." ] }, { "cell_type": "code", "execution_count": 2, "id": "49554709-bf01-49a6-8681-15cbaa76a068", "metadata": {}, "outputs": [], "source": [ "nrecs = 2; nlevs = 2; nlats = 6; nlons = 12" ] }, { "cell_type": "markdown", "id": "1da6a8e1-04a1-4a3a-bf30-897b20293c74", "metadata": {}, "source": [ "# open a new netCDF file for writing." ] }, { "cell_type": "markdown", "id": "a483d22c-e678-41fa-a6af-08e9d01af7c1", "metadata": {}, "source": [ "See http://unidata.github.io/netcdf4-python/#creatingopeningclosing-a-netcdf-file" ] }, { "cell_type": "code", "execution_count": 4, "id": "c771364c-90f6-4dda-8415-11db9f9e4ff0", "metadata": {}, "outputs": [], "source": [ "ncfile = Dataset('pres_temp_4D.nc','w') " ] }, { "cell_type": "code", "execution_count": 5, "id": "434fcb2c-ddb7-41a9-b72a-1ec97f0cedb9", "metadata": {}, "outputs": [], "source": [ "# latitudes and longitudes of grid" ] }, { "cell_type": "markdown", "id": "54c37795-6416-4b3a-98b0-231451899c56", "metadata": {}, "source": [ "see https://numpy.org/doc/stable/reference/generated/numpy.arange.html" ] }, { "cell_type": "code", "execution_count": 6, "id": "a6ea71f2-4538-4539-918c-4d7fa32f99d5", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "(array([-25., -20., -15., -10., -5., 0.], dtype=float32),\n", " array([-125., -120., -115., -110., -105., -100., -95., -90., -85.,\n", " -80., -75., -70.], dtype=float32))" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lats_out = -25.0 + 5.0*arange(nlats,dtype='float32')\n", "lons_out = -125.0 + 5.0*arange(nlons,dtype='float32')\n", "lats_out, lons_out" ] }, { "cell_type": "code", "execution_count": 7, "id": "eab8f79d-590b-4ec0-9f6c-c03d3cac89b8", "metadata": {}, "outputs": [], "source": [ "# output data." ] }, { "cell_type": "code", "execution_count": 8, "id": "4a00e3dc-ce5f-4b20-8681-d11d70f6a309", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([[[ 900., 901., 902., 903., 904., 905., 906., 907., 908.,\n", " 909., 910., 911.],\n", " [ 912., 913., 914., 915., 916., 917., 918., 919., 920.,\n", " 921., 922., 923.],\n", " [ 924., 925., 926., 927., 928., 929., 930., 931., 932.,\n", " 933., 934., 935.],\n", " [ 936., 937., 938., 939., 940., 941., 942., 943., 944.,\n", " 945., 946., 947.],\n", " [ 948., 949., 950., 951., 952., 953., 954., 955., 956.,\n", " 957., 958., 959.],\n", " [ 960., 961., 962., 963., 964., 965., 966., 967., 968.,\n", " 969., 970., 971.]],\n", " \n", " [[ 972., 973., 974., 975., 976., 977., 978., 979., 980.,\n", " 981., 982., 983.],\n", " [ 984., 985., 986., 987., 988., 989., 990., 991., 992.,\n", " 993., 994., 995.],\n", " [ 996., 997., 998., 999., 1000., 1001., 1002., 1003., 1004.,\n", " 1005., 1006., 1007.],\n", " [1008., 1009., 1010., 1011., 1012., 1013., 1014., 1015., 1016.,\n", " 1017., 1018., 1019.],\n", " [1020., 1021., 1022., 1023., 1024., 1025., 1026., 1027., 1028.,\n", " 1029., 1030., 1031.],\n", " [1032., 1033., 1034., 1035., 1036., 1037., 1038., 1039., 1040.,\n", " 1041., 1042., 1043.]]], dtype=float32),\n", " array([[[ 9., 10., 11., 12., 13., 14., 15., 16., 17., 18.,\n", " 19., 20.],\n", " [ 21., 22., 23., 24., 25., 26., 27., 28., 29., 30.,\n", " 31., 32.],\n", " [ 33., 34., 35., 36., 37., 38., 39., 40., 41., 42.,\n", " 43., 44.],\n", " [ 45., 46., 47., 48., 49., 50., 51., 52., 53., 54.,\n", " 55., 56.],\n", " [ 57., 58., 59., 60., 61., 62., 63., 64., 65., 66.,\n", " 67., 68.],\n", " [ 69., 70., 71., 72., 73., 74., 75., 76., 77., 78.,\n", " 79., 80.]],\n", " \n", " [[ 81., 82., 83., 84., 85., 86., 87., 88., 89., 90.,\n", " 91., 92.],\n", " [ 93., 94., 95., 96., 97., 98., 99., 100., 101., 102.,\n", " 103., 104.],\n", " [105., 106., 107., 108., 109., 110., 111., 112., 113., 114.,\n", " 115., 116.],\n", " [117., 118., 119., 120., 121., 122., 123., 124., 125., 126.,\n", " 127., 128.],\n", " [129., 130., 131., 132., 133., 134., 135., 136., 137., 138.,\n", " 139., 140.],\n", " [141., 142., 143., 144., 145., 146., 147., 148., 149., 150.,\n", " 151., 152.]]], dtype=float32))" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "press_out = 900. + arange(nlevs*nlats*nlons,dtype='float32') # 1d array\n", "press_out.shape = (nlevs,nlats,nlons) # reshape to 2d array\n", "temp_out = 9. + arange(nlevs*nlats*nlons,dtype='float32') # 1d array\n", "temp_out.shape = (nlevs,nlats,nlons) # reshape to 2d array\n", "press_out, temp_out" ] }, { "cell_type": "markdown", "id": "5d30d538-280d-47ce-b098-7a2aea33e02c", "metadata": {}, "source": [ "# create the lat and lon dimensions." ] }, { "cell_type": "markdown", "id": "43dbb289-5b50-4b5c-af68-06bcbd69839d", "metadata": {}, "source": [ "See http://unidata.github.io/netcdf4-python/#dimensions-in-a-netcdf-file" ] }, { "cell_type": "code", "execution_count": 9, "id": "3d03e65f-19b3-414f-96c6-5cc316f8c7aa", "metadata": {}, "outputs": [ { "data": { "text/plain": [ ": name = 'latitude', size = 6" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ncfile.createDimension('latitude',nlats)" ] }, { "cell_type": "code", "execution_count": 10, "id": "65a75734-3a34-4f93-9710-14fcabe07be6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ ": name = 'longitude', size = 12" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ncfile.createDimension('longitude',nlons)" ] }, { "cell_type": "code", "execution_count": 11, "id": "2feefacc-c560-4cd2-9a08-04de6ad2191c", "metadata": {}, "outputs": [], "source": [ "# create level dimension." ] }, { "cell_type": "code", "execution_count": 12, "id": "06f4bf56-879c-4b49-a048-431fe1f4577d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ ": name = 'level', size = 2" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ncfile.createDimension('level',nlevs)" ] }, { "cell_type": "code", "execution_count": 13, "id": "52d7187c-abfa-46c8-a941-65fcef2b497a", "metadata": {}, "outputs": [], "source": [ "# create time dimension (record, or unlimited dimension)" ] }, { "cell_type": "code", "execution_count": 14, "id": "fe64d58a-c027-49a9-935a-b259fa7a9fbe", "metadata": {}, "outputs": [ { "data": { "text/plain": [ " (unlimited): name = 'time', size = 0" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ncfile.createDimension('time',None)" ] }, { "cell_type": "code", "execution_count": null, "id": "7462db70-a42b-4ff2-962a-bccde802b1f7", "metadata": {}, "outputs": [], "source": [ "# Define the coordinate variables. They will hold the coordinate\n", "# information, that is, the latitudes and longitudes.\n", "# Coordinate variables only given for lat and lon." ] }, { "cell_type": "markdown", "id": "c3d0fe0d-8a76-4ab5-9019-5fc1de1456c8", "metadata": {}, "source": [ "See http://unidata.github.io/netcdf4-python/#variables-in-a-netcdf-file" ] }, { "cell_type": "code", "execution_count": 15, "id": "8198e25e-106d-4986-b4c9-9e56ef5918ee", "metadata": { "tags": [] }, "outputs": [], "source": [ "lats = ncfile.createVariable('latitude',dtype('float32').char,('latitude',))\n", "lons = ncfile.createVariable('longitude',dtype('float32').char,('longitude',))" ] }, { "cell_type": "markdown", "id": "ab4d23af-3cfb-4a90-9291-de8917624a17", "metadata": {}, "source": [ "See http://unidata.github.io/netcdf4-python/#attributes-in-a-netcdf-file" ] }, { "cell_type": "code", "execution_count": null, "id": "3dc18822-f0ac-4083-ab2b-b319a3562f03", "metadata": {}, "outputs": [], "source": [ "# Assign units attributes to coordinate var data. This attaches a\n", "# text attribute to each of the coordinate variables, containing the\n", "# units." ] }, { "cell_type": "code", "execution_count": null, "id": "bb121eef-1d94-4ae4-af07-56883f69d2c0", "metadata": {}, "outputs": [], "source": [ "lats.units = 'degrees_north'\n", "lons.units = 'degrees_east'" ] }, { "cell_type": "code", "execution_count": null, "id": "0824f71f-72fc-4021-b34b-80e32a089858", "metadata": {}, "outputs": [], "source": [ "# write data to coordinate vars." ] }, { "cell_type": "code", "execution_count": null, "id": "9c660a30-6a62-400e-bd3c-712a13623841", "metadata": {}, "outputs": [], "source": [ "lats[:] = lats_out\n", "lons[:] = lons_out" ] }, { "cell_type": "code", "execution_count": null, "id": "f7844d03-75f3-4647-b450-d022d417ff66", "metadata": {}, "outputs": [], "source": [ "# create the pressure and temperature variables " ] }, { "cell_type": "code", "execution_count": null, "id": "3d400abf-cb85-43aa-bfb1-19f231bcd14b", "metadata": {}, "outputs": [], "source": [ "press = ncfile.createVariable('pressure',dtype('float32').char,('time','level','latitude','longitude'))\n", "temp = ncfile.createVariable('temperature',dtype('float32').char,('time','level','latitude','longitude'))" ] }, { "cell_type": "code", "execution_count": null, "id": "0db011ab-a089-4a0f-aab0-ebc68a6aae5e", "metadata": {}, "outputs": [], "source": [ "# set the units attribute." ] }, { "cell_type": "code", "execution_count": null, "id": "00cf0f06-a854-472b-a8f6-f47b6f7e6f51", "metadata": {}, "outputs": [], "source": [ "press.units = 'hPa'\n", "temp.units = 'celsius'" ] }, { "cell_type": "code", "execution_count": null, "id": "c591c60d-e555-4826-a32b-c13786066a08", "metadata": {}, "outputs": [], "source": [ "# write data to variables along record (unlimited) dimension.\n", "# same data is written for each record." ] }, { "cell_type": "code", "execution_count": null, "id": "cf61c6bb-2d15-47a6-85bb-3f210aa2f5f8", "metadata": {}, "outputs": [], "source": [ "for nrec in range(nrecs):\n", " press[nrec,:,::] = press_out\n", " temp[nrec,:,::] = temp_out" ] }, { "cell_type": "code", "execution_count": null, "id": "e4149697-cc1c-4896-a7cf-d2debe581f20", "metadata": {}, "outputs": [], "source": [ "# close the file." ] }, { "cell_type": "code", "execution_count": null, "id": "2012c68e-4aee-4777-a72d-9ae57b0569ce", "metadata": {}, "outputs": [], "source": [ "ncfile.close()" ] }, { "cell_type": "code", "execution_count": null, "id": "df91a6a9-69ff-4ac1-9a9e-dc3e3a230206", "metadata": {}, "outputs": [], "source": [ "print ('*** SUCCESS writing example file pres_temp_4D.nc')" ] }, { "cell_type": "code", "execution_count": null, "id": "e4215649-b36f-45de-9bed-cd620944d70b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "2d9719db-994e-4a35-82b6-3761f7891975", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "26103d19-8ff3-4db6-842f-274cd15838a1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "9f5cdf13-df2f-4e44-9740-6e173a2121ba", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }