Changeset 25478


Ignore:
Timestamp:
08/26/20 07:07:33 (5 years ago)
Author:
bdef
Message:

CHG: nc export to deal with outpudefinitions and also non full results (in term of time)contrib/defleurian/netCDF/export_netCDF.py

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/io/loadvars.py

    r25460 r25478  
    55except ImportError:
    66    from whichdb import whichdb # Python 2
    7 from re import findall
     7from re import findall, split
    88import shelve
    9 
    109from netCDF4 import Dataset
    1110import numpy as np
    12 
    1311from model import *
    1412
     
    9593                curclass = NCFile.groups[classtree[mod][0]].groups[classtree[mod][1]]
    9694                if debug:
    97                     print("now checking {} for list : {}".format(mod, classtype[mod][0] == 'list' or (classtype[mod][0] == 'results' and 'Time' in NCFile.dimensions)))
    98                 if classtype[mod][0] == 'list' or (classtype[mod][0] == 'results' and 'Time' in NCFile.dimensions):  #We have a list of variables
     95                    print("===> {} is of class {}".format(mod, classtype[mod][0]))
     96                if classtype[mod][0] == 'results' and 'Time' in NCFile.dimensions:  #Treating results
    9997                    keylist = [key for key in curclass.groups]
    10098                    # this is related to the old structure of NC files where every steps of results had its own group
     
    123121                        #that is the current treatment
    124122                        #here we have a more NC approach with time being a dimension
    125                         keylist = [key for key in curclass.variables]
    126                         dimlist = [curclass.variables[key].dimensions for key in keylist]
    127                         indexlist = np.arange(0, len(NCFile.dimensions['Time']))
    128                         AllHaveTime = np.all(['Time' in dimtuple for dimtuple in dimlist])
    129123                        listtype = curclass.classtype
    130                         if AllHaveTime:
     124                        if len(NCFile.dimensions['Time']) == 1:
     125                            nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(__import__(listtype), listtype)()
     126                            Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]]
     127                        else:
    131128                            #Time dimension is in all the variables so we take that as stepnumber for the results
    132129                            if onlylast:   #we load only the last result to save on time and memory
     
    136133                                nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [getattr(__import__(listtype), listtype)() for i in range(max(1, len(NCFile.dimensions['Time'])))]
    137134                                Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]][:]
    138                         elif len(NCFile.dimensions['Time']) == 1:
    139                             nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(__import__(listtype), listtype)()
    140                             Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]]
    141                         else:
    142 
    143                             print("ERROR: Time dimension is not in all results. That has been overlooked for now but your resulat are not saved.")
     135
     136                elif classtype[mod][0] == 'massfluxatgate':  #this is for output definitions
     137                    defname = split('Output|[0-9]+', classtree[mod][1])[1] + 's'
     138                    defindex = int(findall('[0-9]+', classtree[mod][1])[0])
     139                    nvdict['md'].__dict__[classtree[mod][0]].__dict__[defname].append(getattr(classtype[mod][1], classtype[mod][0])())
     140                    Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[defname][defindex - 1]
    144141
    145142                else:
     143                    if debug:
     144                        print("Using the default for md.{}.{}, is that right??".format(classtree[mod][0], classtree[mod][1]))
    146145                    nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(classtype[mod][1], classtype[mod][0])()
    147146                    Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]]
     
    180179                                    print('table dimension greater than 3 not implemented yet')
    181180                            else:
     181                                incomplete = 'Time' not in varval.dimensions
     182                                if incomplete:
     183                                    chosendim = varval.dimensions[0]
     184                                    indexlist = np.arange(0, len(NCFile.dimensions[chosendim]))
     185                                    print('WARNING, {} is not present on every times, we chose {}({}) as the dimension to write it with'.format(var, chosendim, len(NCFile.dimensions[chosendim])))
     186                                else:
     187                                    indexlist = np.arange(0, len(NCFile.dimensions['Time']))
    182188                                for t in indexlist:
    183189                                    if vardim == 0:
     
    266272                #==== And with atribute {{{
    267273                for attr in listclass.ncattrs():
     274                    if debug:
     275                        print("treating attribute {}".format(attr))
    268276                    if attr != 'classtype':  #classtype is for treatment, don't get it back
     277                        attribute = str(attr).swapcase()  #there is a reason for swapcase, no sure what it isanymore
     278                        if attr == 'VARNAME':
     279                            attribute = 'name'
    269280                        if type(Tree) == list:
    270281                            t = int(indexlist[i])
    271282                            if listtype == 'dict':
    272                                 Tree[t][str(attr).swapcase()] = str(listclass.getncattr(attr))
    273                             else:
    274                                 Tree[t].__dict__[str(attr).swapcase()] = str(listclass.getncattr(attr))
     283                                Tree[t][attribute] = str(listclass.getncattr(attr))
     284                            else:
     285                                Tree[t].__dict__[attribute] = str(listclass.getncattr(attr))
    275286                        else:
    276                             Tree.__dict__[str(attr).swapcase()] = str(listclass.getncattr(attr))
     287                            Tree.__dict__[attribute] = str(listclass.getncattr(attr))
    277288                            if listclass.getncattr(attr) == 'True':
    278                                 Tree.__dict__[str(attr).swapcase()] = True
     289                                Tree.__dict__[attribute] = True
    279290                            elif listclass.getncattr(attr) == 'False':
    280                                 Tree.__dict__[str(attr).swapcase()] = False
     291                                Tree.__dict__[attribute] = False
    281292                # }}}
    282293            # }}}
Note: See TracChangeset for help on using the changeset viewer.