Changeset 27834
- Timestamp:
- 07/18/23 16:04:30 (20 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/m/contrib/musselman/write_netCDF_commit.py
r27832 r27834 1 1 # imports 2 import netCDF4 2 3 from netCDF4 import Dataset 3 4 import numpy as np … … 6 7 from os import path, remove 7 8 from model import * 9 from results import * 8 10 9 11 … … 37 39 # Walk through the model_var class and compare subclass states to empty_model 38 40 walk_through_model(model_var, model_name) 39 41 42 # in order to handle some subclasses in the results class, we have to utilize this band-aid 43 # there will likely be more band-aids added unless a class name library is created with all class names that might be added to a model 44 try: 45 # if results has meaningful data, save the name of the subclass and class instance 46 NetCDF.groups['results'] 47 results_subclasses_bandaid(model_var) 48 # otherwise, ignore 49 except KeyError: 50 pass 51 40 52 NetCDF.close() 41 53 print('Model successfully saved as NetCDF4') 42 54 55 56 57 def results_subclasses_bandaid(model_var): 58 # since the results class may have nested classes within it, we need to record the name of the 59 # nested class instance variable as it appears in the model that we're trying to save 60 quality_control = [] 61 for class_instance_name in model_var.results.__dict__.keys(): 62 # for each class instance in results, see which class its from and record that info in the netcdf to recreate structure later 63 # check to see if there is a solutionstep class instance 64 if isinstance(model_var.results.__dict__[class_instance_name],solutionstep): 65 quality_control.append(1) 66 write_string_to_netcdf(variable_name=str('solutionstep'), adress_of_child=str(class_instance_name), group=NetCDF.groups['results']) 67 # check to see if there is a solution class instance 68 if isinstance(model_var.results.__dict__[class_instance_name],solution): 69 quality_control.append(1) 70 write_string_to_netcdf(variable_name=str('solution'), adress_of_child=str(class_instance_name), group=NetCDF.groups['results']) 71 # check to see if there is a resultsdakota class instance 72 if isinstance(model_var.results.__dict__[class_instance_name],resultsdakota): 73 quality_control.append(1) 74 write_string_to_netcdf(variable_name=str('resultsdakota'), adress_of_child=str(class_instance_name), group=NetCDF.groups['results']) 75 if len(quality_control) != len(model_var.results.__dict__.keys()): 76 print('Error: The class instance within your model.results class is not currently supported by this application') 77 print(type(model_var.results.__dict__[class_instance_name])) 78 else: 79 print('The results class was successfully stored on disk') 80 43 81 44 82 … … 82 120 try: 83 121 # enter the subclass, see if it has nested classes and/or attributes 84 # then compare attribute states between models and write to netCDF if they differ85 # repeat starting from new location122 # then compare attributes between models and write to netCDF if they differ 123 # if subclass found, walk through it and repeat 86 124 for child in eval(adress + '.__dict__.keys()'): 87 125 # make a string variable so we can send thru this func again … … 89 127 # If the attribute is unchanged, move onto the next layer 90 128 adress_of_child_in_empty_class = 'empty_model' + adress_of_child.removeprefix(str(model_name)) 91 if type(child) == type(eval(adress_of_child_in_empty_class)): 92 walk_through_subclasses(model_var, adress_of_child, model_name) 93 # If it has been modified, record it in the NetCDF file 94 else: 129 # using try/except here because sometimes a model can have class instances/attributes that are not 130 # in the framework of an empty model. If this is the case, we move to the except statement 131 try: 132 if type(child) == type(eval(adress_of_child_in_empty_class)): 133 walk_through_subclasses(model_var, adress_of_child, model_name) 134 # If it has been modified, record it in the NetCDF file 135 else: 136 create_group(model_var, adress_of_child) 137 walk_through_subclasses(model_var, adress_of_child, model_name) 138 except AttributeError: 95 139 create_group(model_var, adress_of_child) 96 140 walk_through_subclasses(model_var, adress_of_child, model_name) 97 except: pass 98 99 141 except Exception as e: print(e) 142 143 144 100 145 def create_group(model_var, adress_of_child): 101 146 # start by splitting the adress_of_child into its components … … 133 178 variable = group.createVariable(variable_name, float, ('float',)) 134 179 variable[:] = eval(adress_of_child) 135 180 136 181 # or a string 137 182 elif isinstance(eval(adress_of_child), str): 138 183 write_string_to_netcdf(variable_name, adress_of_child, group) 139 184 140 # or a list of strings -- this needs work as it can only handle a list of 1 string141 elif isinstance(eval(adress_of_child)[0],str):142 for string in eval(adress_of_child):143 write_string_to_netcdf(variable_name, string, group)144 145 # or a list146 elif isinstance(eval(adress_of_child), list):147 variable = group.createVariable(variable_name, type(eval(adress_of_child)[0]), ('Unlim',))148 variable[:] = eval(adress_of_child)149 150 185 # or an empty list 151 186 elif isinstance(eval(adress_of_child), list) and len(eval(adress_of_child))==0: 152 187 variable = group.createVariable(variable_name, int, ('int',)) 188 189 # or a list of strings -- this needs work as it can only handle a list of 1 string 190 elif isinstance(eval(adress_of_child),list) and isinstance(eval(adress_of_child)[0],str): 191 for string in eval(adress_of_child): 192 write_string_to_netcdf(variable_name, string, group) 193 194 # or a regular list 195 elif isinstance(eval(adress_of_child), list): 196 print(eval(adress_of_child)) 197 variable = group.createVariable(variable_name, type(eval(adress_of_child)[0]), ('Unlim',)) 198 variable[:] = eval(adress_of_child) 153 199 154 200 # anything else... (will likely need to add more cases; ie dict) … … 163 209 print('Successfully transferred data from ' + adress_of_child + ' to the NetCDF') 164 210 211 212 165 213 166 214 def write_string_to_netcdf(variable_name, adress_of_child, group): … … 171 219 length_of_the_string = len(the_string_to_save) 172 220 numpy_datatype = 'S' + str(length_of_the_string) 173 str_out = netCDF4.stringtochar(np.array([ 'the_string_to_save'], dtype=numpy_datatype))221 str_out = netCDF4.stringtochar(np.array([the_string_to_save], dtype=numpy_datatype)) 174 222 #otherwise we need to treat it like a string: 175 223 except: … … 177 225 length_of_the_string = len(the_string_to_save) 178 226 numpy_datatype = 'S' + str(length_of_the_string) 179 str_out = netCDF4.stringtochar(np.array([ 'the_string_to_save'], dtype=numpy_datatype))227 str_out = netCDF4.stringtochar(np.array([the_string_to_save], dtype=numpy_datatype)) 180 228 181 229 # we'll need to make a new dimension for the string if it doesn't already exist 230 name_of_dimension = 'char' + str(length_of_the_string) 182 231 try: 183 name_of_dimension = 'char' + str(length_of_the_string)184 232 group.createDimension(name_of_dimension, length_of_the_string) 185 233 except: pass 186 234 # now we can make a variable in this dimension: 187 string = group.createVariable(variable_name, 'S1', ( 'nchar'))235 string = group.createVariable(variable_name, 'S1', (name_of_dimension)) 188 236 #finally we can write the variable: 189 string[:] = the_string_to_save237 string[:] = str_out 190 238 191 239
Note:
See TracChangeset
for help on using the changeset viewer.