/[escript]/branches/doubleplusgood/doc/examples/inversion/create_netcdf.py
ViewVC logotype

Contents of /branches/doubleplusgood/doc/examples/inversion/create_netcdf.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4345 - (show annotations)
Fri Mar 29 07:09:41 2013 UTC (5 years, 11 months ago) by jfenwick
File MIME type: text/x-python
File size: 3490 byte(s)
Spelling fixes
1 ##############################################################################
2 #
3 # Copyright (c) 2003-2013 by University of Queensland
4 # http://www.uq.edu.au
5 #
6 # Primary Business: Queensland, Australia
7 # Licensed under the Open Software License version 3.0
8 # http://www.opensource.org/licenses/osl-3.0.php
9 #
10 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
11 # Development since 2012 by School of Earth Sciences
12 #
13 ##############################################################################
14
15 """
16 This example shows how to create a netCDF input file that is suitable for
17 inversions in esys.downunder.
18 """
19
20 import sys
21 from datetime import datetime
22 import numpy as np
23 try:
24 from scipy.io import netcdf_file
25
26 # output filename
27 FILENAME='output.nc'
28
29 # a concise title and summary of the dataset
30 TITLE="custom_data"
31 SUMMARY="Bouguer gravity anomaly data"
32
33 # Origin longitude (degrees east) and latitude (degrees north)
34 ORIGIN_X=130.2
35 ORIGIN_Y=-29.1
36
37 # spacing in longitude,latitude direction (degrees)
38 DELTA_X=0.05
39 DELTA_Y=0.05
40
41 # Number of data points in longitude,latitude direction
42 NX=20
43 NY=10
44
45 # Dummy value (for unset areas)
46 MISSING=-9999
47
48 # Data error (can be constant or variable over the data points)
49 SIGMA = 3.
50
51 # The actual data array, must have shape (NY, NX).
52 # These are just some random numbers.
53 DATA = 10*np.random.normal(size=(NY, NX), scale=SIGMA)
54 ERROR = np.ones((NY, NX)) * SIGMA
55
56 # Use MISSING where data is invalid/not available, example:
57 DATA[NY/2,NX/2]=MISSING
58 ERROR[NY/2,NX/2]=MISSING
59
60 ##############################################################################
61 ###################### Keep everything below this line #######################
62 ##############################################################################
63 # conventions used in the file
64 conventions="CF-1.0, COARDS, Unidata Dataset Discovery v1.0"
65 # file log
66 history=datetime.now().strftime("%d-%m-%Y")+" created using python script"
67 # license
68 license="Free to use"
69
70 # Create the output file and write a few metadata entries
71 o=netcdf_file(FILENAME,'w')
72 o.Conventions=conventions
73 o.Metadata_Conventions=conventions
74 o.history=history
75 o.license=license
76 o.summary=SUMMARY
77 o.title=TITLE
78
79 # Create longitude dimension and variable
80 longitude=np.linspace(ORIGIN_X, ORIGIN_X+NX*DELTA_X, NX, endpoint=False)
81 o.createDimension("longitude", NX)
82 v=o.createVariable("longitude", longitude.dtype, ["longitude"])
83 v.data[:]=longitude
84 v.units="degrees_east"
85 v.long_name="Longitude"
86
87 # Create latitude dimension and variable
88 latitude=np.linspace(ORIGIN_Y-NY*DELTA_Y, ORIGIN_Y, NY, endpoint=False)
89 o.createDimension("latitude", NY)
90 v=o.createVariable("latitude", latitude.dtype, ["latitude"])
91 v.data[:]=latitude
92 v.units="degrees_north"
93 v.long_name="Latitude"
94
95 # Create the main data variable
96 v=o.createVariable("data", DATA.dtype, ["latitude","longitude"])
97 v.missing_value=MISSING
98 v.data[:]=DATA
99 v.long_name="Bouguer_anomaly"
100
101 # Create the error variable (can be omitted)
102 v=o.createVariable("error", ERROR.dtype, ["latitude","longitude"])
103 v.missing_value=MISSING
104 v.data[:]=ERROR
105
106 # Close the file
107 o.close()
108
109 except ImportError:
110 print("The scipy module was not found but is required to write netCDF files. Exiting...")
111

  ViewVC Help
Powered by ViewVC 1.1.26