ttest_epsg_processing.py - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch)
 (HTM) git clone git://src.adamsgaard.dk/pism
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
       ttest_epsg_processing.py (2766B)
       ---
            1 #!/usr/bin/env python3
            2 
            3 from netCDF4 import Dataset
            4 import os
            5 import shutil
            6 import sys
            7 
            8 PISM_PATH = sys.argv[1]
            9 PISMR = os.path.join(PISM_PATH, "pismr")
           10 PISMS = os.path.join(PISM_PATH, "pisms")
           11 
           12 
           13 def run(command):
           14     print(command)
           15     return os.system(command)
           16 
           17 
           18 files = ["pisms-output.nc",
           19          "pismr-output.nc",
           20          "both-consistent.nc",
           21          "both-string-missing.nc",
           22          "both-string-mismatch.nc",
           23          "both-double-missing.nc",
           24          "both-double-mismatch.nc"]
           25 
           26 # create an input file
           27 run(PISMS + " -verbose 1 -Mx 3 -My 3 -Mz 5 -y 10 -o pisms-output.nc")
           28 
           29 # add the PROJ string
           30 nc = Dataset("pisms-output.nc", "a")
           31 nc.proj = "epsg:3413"
           32 nc.close()
           33 
           34 print("Test running PISM initialized from a file w/o mapping  but with proj...")
           35 assert run(PISMR + " -verbose 1 -i pisms-output.nc -y 10 -o both-consistent.nc") == 0
           36 
           37 print("Test that the mapping variable was initialized using the proj attribute...")
           38 nc = Dataset("both-consistent.nc", "r")
           39 mapping = nc.variables["mapping"]
           40 assert mapping.grid_mapping_name == "polar_stereographic"
           41 nc.close()
           42 
           43 print("Test re-starting PISM with consistent proj and mapping...")
           44 assert run(PISMR + " -verbose 1 -i both-consistent.nc -o pismr-output.nc") == 0
           45 
           46 # remove a required string attribute
           47 shutil.copy("both-consistent.nc", "both-string-missing.nc")
           48 nc = Dataset("both-string-missing.nc", "a")
           49 del nc.variables["mapping"].grid_mapping_name
           50 nc.close()
           51 
           52 print("Test that PISM stops if a required string attribute is missing...")
           53 assert run(PISMR + " -verbose 1 -i both-string-missing.nc -o pismr-output.nc") != 0
           54 
           55 # alter a required string sttribute
           56 shutil.copy("both-consistent.nc", "both-string-mismatch.nc")
           57 nc = Dataset("both-string-mismatch.nc", "a")
           58 nc.variables["mapping"].grid_mapping_name = "wrong"
           59 nc.close()
           60 
           61 print("Test that PISM stops if a required string attribute has a wrong value...")
           62 assert run(PISMR + " -verbose 1 -i both-string-mismatch.nc -o pismr-output.nc") != 0
           63 
           64 # remove a required double attribute
           65 shutil.copy("both-consistent.nc", "both-double-missing.nc")
           66 nc = Dataset("both-double-missing.nc", "a")
           67 del nc.variables["mapping"].standard_parallel
           68 nc.close()
           69 
           70 print("Test that PISM stops when a required double attribute is missing...")
           71 assert run(PISMR + " -verbose 1 -i both-double-missing.nc -o pismr-output.nc") != 0
           72 
           73 # alter a required double attribute
           74 shutil.copy("both-consistent.nc", "both-double-mismatch.nc")
           75 nc = Dataset("both-double-mismatch.nc", "a")
           76 nc.variables["mapping"].standard_parallel = 45.0
           77 nc.close()
           78 
           79 print("Test that PISM stops if a required double attribute has a wrong value...")
           80 assert run(PISMR + " -verbose 1 -i both-double-mismatch.nc -o pismr-output.nc") != 0
           81 
           82 # cleanup
           83 for f in files:
           84     print("Removing %s..." % f)
           85     os.remove(f)