File: test_python_CMIP6_projections.py

package info (click to toggle)
cmor 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 16,856 kB
  • sloc: ansic: 27,460; f90: 14,115; python: 11,636; sh: 3,501; makefile: 420; xml: 168
file content (112 lines) | stat: -rw-r--r-- 3,267 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# If this example is not executed from the directory containing the
# CMOR code, please first complete the following steps:
#
#   1. In any directory, create 'Tables/', 'Test/' and 'CMIP6/' directories.
#
#   2. Download
#      https://github.com/PCMDI/cmor/blob/master/TestTables/CMIP6_Omon.json
#      and https://github.com/PCMDI/cmor/blob/master/TestTables/CMIP6_CV.json
#      to the 'Tables/' directory.
#
#   3. Download
#      https://github.com/PCMDI/cmor/blob/master/Test/<filename>.json
#      to the 'Test/' directory.

import cmor
import numpy
import unittest
import os
import sys
import tempfile

# ==============================
#  main thread
# ==============================


def run():
    unittest.main()


class TestCase(unittest.TestCase):

    def testCMIP6(self):

        # Create x and y coordinates and bounds.
        #
        coordVals = numpy.arange(-533750.0, 533750.0 +
                                 2500.0, 2500.0, numpy.float32)
        coordBnds = numpy.zeros((coordVals.shape[0], 2), numpy.float32)

        coordBnds[:, 0] = coordVals - 1250.0
        coordBnds[:, 1] = coordVals + 1250.0

        # Create longitude and latitude fields for a polar stereographic projection.
        #
        xgrid, ygrid = numpy.broadcast_arrays(
            numpy.expand_dims(
                coordVals, 0), numpy.expand_dims(
                coordVals, 1))

        rhogrid = numpy.sqrt(xgrid**2 + ygrid**2)
        cgrid = 2.0 * numpy.arctan((0.5 / 6378137.0) * rhogrid)
        latgrid = (180.0 / 3.141592654) * numpy.arcsin(numpy.cos(cgrid))
        longrid = (180.0 / 3.141592654) * numpy.arctan2(xgrid, -ygrid)

        # Set up CMOR with information from the CMOR config dictionary.
        #

        cmor.setup("Tables", netcdf_file_action=cmor.CMOR_REPLACE_4)

        # Create the output CMOR dataset using the output configuration.
        #
        cmor.dataset_json("Test/CMOR_input_example.json")

        # Load the grid table.
        #
        cmor_table_obj = cmor.load_table("CMIP6_grids.json")

        # Create ygre and xgre axes.
        #
        entry = {
            'table_entry': 'y',
            'units': 'm',
            'coord_vals': coordVals,
            'cell_bounds': coordBnds}

        axis_ygre = cmor.axis(**entry)

        entry = {
            'table_entry': 'x',
            'units': 'm',
            'coord_vals': coordVals,
            'cell_bounds': coordBnds}

        axis_xgre = cmor.axis(**entry)

        # Create the grid
        #
        grid_id = cmor.grid(
            axis_ids=[
                axis_ygre,
                axis_xgre],
            latitude=latgrid,
            longitude=longrid)

        # Set the CMOR grid mapping.
        #
        #    mapnm = 'polar_stereographic'
        param_dict = {
            'latitude_of_projection_origin': [90.0, 'degrees_north'],
            'straight_vertical_longitude_from_pole': [135.0, 'degrees_east'],
            'standard_parallel': [70.0, 'degrees_north'],
            'false_northing': [0.0, 'meters'],
            'false_easting': [0.0, 'meters']
        }
        ierr = cmor.set_grid_mapping(
            grid_id, 'polar_stereographic', param_dict)
        self.assertEqual(ierr, 0)


if __name__ == '__main__':
    run()