wolfhece 2.1.25__py3-none-any.whl → 2.1.28__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
wolfhece/wolf_array.py CHANGED
@@ -77,6 +77,7 @@ WOLF_ARRAY_MNAP_INTEGER = 20
77
77
 
78
78
  WOLF_ARRAY_MB = [WOLF_ARRAY_MB_SINGLE, WOLF_ARRAY_MB_INTEGER, WOLF_ARRAY_MNAP_INTEGER]
79
79
 
80
+ VERSION_RGB = 2
80
81
 
81
82
  def getkeyblock(i, addone=True) -> str:
82
83
  """
@@ -648,6 +649,8 @@ class header_wolf():
648
649
  if isinstance(filename, Path):
649
650
  filename = str(filename)
650
651
 
652
+ locpath = Path(filename)
653
+
651
654
  if filename.endswith('.tif') or filename.endswith('.tiff') :
652
655
  from osgeo import gdal
653
656
 
@@ -680,7 +683,7 @@ class header_wolf():
680
683
  """
681
684
 
682
685
  dtype = raster.GetRasterBand(1).DataType
683
-
686
+
684
687
  if dtype == 1:
685
688
  self.wolftype = WOLF_ARRAY_FULL_INTEGER8
686
689
  elif dtype in [2,3]:
@@ -696,9 +699,61 @@ class header_wolf():
696
699
  logging.error(_('Please convert the raster to a supported datatype - or upgrade the code to support this datatype'))
697
700
  logging.error(_('See : read_txt_header and import_geotif in wolf_array.py'))
698
701
  return
699
- return
700
702
 
701
- if filename.endswith('.flt'):
703
+ elif filename.endswith('.npy') and not os.path.exists(filename + '.txt'):
704
+ # Il y de fortes chances que cette matrice numpy provienne d'une modélisation GPU
705
+ # et donc que les coordonnées et la résolution soient disponibles dans un fichier parameters.json
706
+ if (locpath.parent / 'parameters.json').exists():
707
+ with open(locpath.parent / 'parameters.json', 'r') as f:
708
+ params = json.load(f)
709
+
710
+ if 'parameters' in params.keys():
711
+ if "dx" in params['parameters'].keys() :
712
+ self.dx = float(params['parameters']["dx"])
713
+ if "dy" in params['parameters'].keys() :
714
+ self.dy = float(params['parameters']["dy"])
715
+ if "base_coord_x" in params['parameters'].keys() :
716
+ self.origx = float(params['parameters']["base_coord_x"])
717
+ if "base_coord_y" in params['parameters'].keys() :
718
+ self.origy = float(params['parameters']["base_coord_y"])
719
+
720
+ self.nullvalue = 99999.
721
+ else:
722
+
723
+ self.dx = 1.
724
+ self.dy = 1.
725
+ self.origx = 0.
726
+ self.origy = 0.
727
+
728
+ # Numpy format
729
+ with open(filename, 'rb') as f:
730
+ version = np.lib.format.read_magic(f)
731
+ if version[0] == 1:
732
+ shape, fortran, dtype = np.lib.format.read_array_header_1_0(f)
733
+ elif version[0] == 2:
734
+ shape, fortran, dtype = np.lib.format.read_array_header_2_0(f)
735
+ else:
736
+ raise ValueError("Unknown numpy version: %s" % version)
737
+
738
+ self.nbx, self.nby = shape
739
+
740
+ if dtype == np.float32:
741
+ self.wolftype = WOLF_ARRAY_FULL_SINGLE
742
+ elif dtype == np.float64:
743
+ self.wolftype = WOLF_ARRAY_FULL_DOUBLE
744
+ elif dtype == np.int32:
745
+ self.wolftype = WOLF_ARRAY_FULL_INTEGER
746
+ elif dtype == np.int16:
747
+ self.wolftype = WOLF_ARRAY_FULL_INTEGER16
748
+ elif dtype == np.uint8:
749
+ self.wolftype = WOLF_ARRAY_FULL_INTEGER8
750
+ elif dtype == np.int8:
751
+ self.wolftype = WOLF_ARRAY_FULL_INTEGER8
752
+ else:
753
+ logging.error(_('Unsupported type in numpy file -- Abort loading'))
754
+ return
755
+
756
+ elif filename.endswith('.flt'):
702
757
  # Fichier .flt
703
758
  if not os.path.exists(filename[:-4] + '.hdr'):
704
759
  logging.warning(_('File {} does not exist -- Retry!'.format(filename[:-4] + '.hdr')))
@@ -1523,7 +1578,7 @@ class Ops_Array(wx.Frame):
1523
1578
  Toolssizer.Add(self.labelling, 1, wx.EXPAND)
1524
1579
  Toolssizer.Add(self.extract_selection, 1, wx.EXPAND)
1525
1580
 
1526
- self.ApplyTools.SetToolTip(_("Apply Nullvalue into memmory/object"))
1581
+ self.ApplyTools.SetToolTip(_("Apply Nullvalue into memory/object"))
1527
1582
  self.nullborder.SetToolTip(_("Set null value on the border of the array\n\nYou will be asked for the width of the border (in cells)"))
1528
1583
  self.filter_zone.SetToolTip(_("Filter the array based on contiguous zones\n\nConservation of the ones which contain selected nodes"))
1529
1584
  self.labelling.SetToolTip(_("Labelling of contiguous zones using Scipy.label function\n\nReplacing the current values by the labels"))
@@ -4104,6 +4159,7 @@ class WolfArray(Element_To_Draw, header_wolf):
4104
4159
  mask_source:np.ndarray = None,
4105
4160
  ) -> None:
4106
4161
  """
4162
+ Constructor of the WolfArray class
4107
4163
 
4108
4164
  :param fname: filename/filepath - if provided, the file will be read on disk
4109
4165
  :param mold: initialize from a copy a the mold object --> must be a WolArray
@@ -4122,10 +4178,8 @@ class WolfArray(Element_To_Draw, header_wolf):
4122
4178
 
4123
4179
  """
4124
4180
 
4125
-
4126
4181
  Element_To_Draw.__init__(self, idx, plotted, mapviewer, need_for_wx)
4127
4182
  header_wolf.__init__(self)
4128
- """ Constructeur d'un WOLF array """
4129
4183
 
4130
4184
  self.mngselection = None
4131
4185
 
@@ -4147,7 +4201,7 @@ class WolfArray(Element_To_Draw, header_wolf):
4147
4201
  self.loaded = False
4148
4202
  self.masknull = masknull
4149
4203
 
4150
- self.rgb = None # numpy array with colorize values
4204
+ if VERSION_RGB==1 : self.rgb = None # numpy array with colorize values
4151
4205
  self.alpha = 1. # transparency alpha value
4152
4206
  self.shading = False # if True, rgb will be shaded
4153
4207
 
@@ -4158,6 +4212,8 @@ class WolfArray(Element_To_Draw, header_wolf):
4158
4212
  self.shaded = WolfArray(whichtype=WOLF_ARRAY_HILLSHAPE)
4159
4213
  self.shaded.mypal.defaultgray()
4160
4214
  self.shaded.mypal.automatic = False
4215
+ else:
4216
+ self.shaded = None
4161
4217
 
4162
4218
  self._nullvalue = nullvalue
4163
4219
  self.nbnotnull = 99999 # number of non-null values in the entire aray
@@ -4209,12 +4265,17 @@ class WolfArray(Element_To_Draw, header_wolf):
4209
4265
  if fname is not None:
4210
4266
 
4211
4267
  self.filename = str(fname)
4268
+ logging.info(_('Loading file : %s') % self.filename)
4212
4269
  self.read_all()
4213
4270
 
4214
4271
  if mask_source is not None:
4272
+ logging.info(_('Applying mask from source'))
4215
4273
  self.copy_mask_log(mask_source)
4216
- elif masknull and self.preload:
4274
+ logging.info(_('Data masked'))
4275
+ elif masknull and (self.preload or self.loaded):
4276
+ logging.info(_('Masking data with nullvalue'))
4217
4277
  self.mask_data(self.nullvalue)
4278
+ logging.info(_('Data masked'))
4218
4279
 
4219
4280
  elif mold is not None:
4220
4281
  if self.cropini is None:
@@ -4269,6 +4330,54 @@ class WolfArray(Element_To_Draw, header_wolf):
4269
4330
 
4270
4331
  self.add_ops_sel() # Ajout d'un gestionnaire de sélection et d'opérations
4271
4332
 
4333
+ @property
4334
+ def memory_usage(self):
4335
+ """
4336
+ Return the memory usage of the header
4337
+ """
4338
+
4339
+ if self.nbz == 0:
4340
+ size = self.nbx * self.nby
4341
+ else:
4342
+ size = self.nbx * self.nby * self.nbz
4343
+
4344
+ if self.wolftype == WOLF_ARRAY_FULL_SINGLE:
4345
+ return size * 4
4346
+ elif self.wolftype == WOLF_ARRAY_FULL_DOUBLE:
4347
+ return size * 8
4348
+ elif self.wolftype == WOLF_ARRAY_FULL_INTEGER:
4349
+ return size * 4
4350
+ elif self.wolftype in [WOLF_ARRAY_FULL_INTEGER16, WOLF_ARRAY_FULL_INTEGER16_2]:
4351
+ return size * 2
4352
+ elif self.wolftype == WOLF_ARRAY_FULL_INTEGER8:
4353
+ return size
4354
+ else:
4355
+ return size * 4
4356
+
4357
+ @property
4358
+ def memory_usage_mask(self):
4359
+ """
4360
+ Return the memory usage of the mask
4361
+ """
4362
+
4363
+ if self.nbz == 0:
4364
+ size = self.nbx * self.nby
4365
+ else:
4366
+ size = self.nbx * self.nby * self.nbz
4367
+
4368
+ return size * 1
4369
+
4370
+ def __del__(self):
4371
+ """ Destructeur de la classe """
4372
+ import gc
4373
+ self.delete_lists()
4374
+ del self.array
4375
+ if VERSION_RGB==1 : del self.rgb
4376
+ del self._array3d
4377
+ del self.mypal
4378
+ del self.shaded
4379
+ gc.collect()
4380
+
4272
4381
  def extract_selection(self):
4273
4382
  """ Extract the current selection """
4274
4383
 
@@ -4662,6 +4771,12 @@ class WolfArray(Element_To_Draw, header_wolf):
4662
4771
  elif arr.dtype == np.float64:
4663
4772
  arr_type = gdal.GDT_Float64
4664
4773
  nullvalue = self.nullvalue
4774
+ elif arr.dtype == np.int8:
4775
+ arr_type = gdal.GDT_Byte
4776
+ nullvalue = int(self.nullvalue)
4777
+ elif arr.dtype == np.int16:
4778
+ arr_type = gdal.GDT_Int16
4779
+ nullvalue = int(self.nullvalue)
4665
4780
  else:
4666
4781
  arr_type = gdal.GDT_Int32
4667
4782
  nullvalue = int(self.nullvalue)
@@ -4689,6 +4804,54 @@ class WolfArray(Element_To_Draw, header_wolf):
4689
4804
  band.FlushCache()
4690
4805
  band.ComputeStatistics(True)
4691
4806
 
4807
+ def _import_npy(self, fn:str='', crop:list[float]=None):
4808
+ """
4809
+ Import a numpy file.
4810
+
4811
+ Must be called after the header is initialized, e.g. read_txt_header.
4812
+
4813
+ :param fn: filename
4814
+ :param crop: crop the data - [xmin, xmax, ymin, ymax]
4815
+ """
4816
+
4817
+ if fn !='':
4818
+ pass
4819
+ elif self.filename !='':
4820
+ fn = self.filename
4821
+ else:
4822
+ return
4823
+
4824
+ # Numpy format
4825
+ locarray = np.load(self.filename)
4826
+
4827
+ assert locarray.shape[0] == self.nbx, _('Incompatible dimensions')
4828
+ assert locarray.shape[1] == self.nby, _('Incompatible dimensions')
4829
+
4830
+ if crop is not None :
4831
+ logging.error(_('Cropping not yet implemented for numpy files'))
4832
+
4833
+ imin, jmin = self.get_ij_from_xy(crop[0][0], crop[1][0])
4834
+ imax, jmax = self.get_ij_from_xy(crop[0][1], crop[1][1])
4835
+
4836
+ imin = int(imin)
4837
+ jmin = int(jmin)
4838
+ imax = int(imax)
4839
+ jmax = int(jmax)
4840
+
4841
+ self.nbx = imax - imin
4842
+ self.nby = jmax - jmin
4843
+ self.dx = self.dx
4844
+ self.dy = self.dy
4845
+ self.origx, self.origy = self.get_xy_from_ij(imin, jmin)
4846
+ self.origx -= self.dx / 2.
4847
+ self.origy -= self.dy / 2.
4848
+ self.translx = self.translx
4849
+ self.transly = self.transly
4850
+
4851
+ locarray = locarray[imin:imax, jmin:jmax]
4852
+
4853
+ self.array = np.ma.asarray(locarray)
4854
+
4692
4855
  def import_geotif(self, fn:str='', which:int = None, crop:list[float]=None):
4693
4856
  """
4694
4857
  Import de la matrice au format Geotiff
@@ -4712,65 +4875,69 @@ class WolfArray(Element_To_Draw, header_wolf):
4712
4875
  return
4713
4876
 
4714
4877
  if crop is not None :
4715
- if os.path.exists(fn):
4878
+ if not os.path.exists(fn):
4879
+ logging.error(_('File not found'))
4880
+ return
4716
4881
 
4717
- tmpdx = self.dx
4882
+ tmpdx = self.dx
4718
4883
 
4719
- fn_crop = fn + '_crop.tif'
4720
- if type(crop) is np.ndarray:
4721
- pass
4722
- elif type(crop) is list:
4723
- pass
4724
- else:
4725
- if not self.wx_exists:
4726
- logging.error(_('Crop must be a list or a numpy array with 4 values - xmin, xmax, ymin, ymax'))
4727
- return
4884
+ fn_crop = fn + '_crop.tif'
4885
+ if type(crop) is np.ndarray:
4886
+ pass
4887
+ elif type(crop) is list:
4888
+ pass
4889
+ else:
4890
+ if not self.wx_exists:
4891
+ logging.error(_('Crop must be a list or a numpy array with 4 values - xmin, xmax, ymin, ymax'))
4892
+ return
4728
4893
 
4729
- raster:gdal.Dataset
4730
- raster = gdal.Open(fn)
4731
- geotr = raster.GetGeoTransform()
4732
- self.dx = geotr[1]
4733
- self.dy = abs(geotr[5])
4894
+ raster:gdal.Dataset
4895
+ raster = gdal.Open(fn)
4896
+ geotr = raster.GetGeoTransform()
4897
+ self.dx = geotr[1]
4898
+ self.dy = abs(geotr[5])
4734
4899
 
4735
4900
 
4736
- newcrop = CropDialog(None)
4901
+ newcrop = CropDialog(None)
4737
4902
 
4738
- if self.wx_exists:
4739
- bounds = self.mapviewer.get_canvas_bounds()
4903
+ if self.wx_exists:
4904
+ bounds = self.mapviewer.get_canvas_bounds()
4740
4905
 
4741
- newcrop.dx.Value = str(self.dx)
4742
- newcrop.dy.Value = str(self.dy)
4906
+ newcrop.dx.Value = str(self.dx)
4907
+ newcrop.dy.Value = str(self.dy)
4743
4908
 
4744
- newcrop.dx.Enable(False)
4745
- newcrop.dy.Enable(False)
4909
+ newcrop.dx.Enable(False)
4910
+ newcrop.dy.Enable(False)
4746
4911
 
4747
- newcrop.ox.Value = str(float((bounds[0] // 50.) * 50.))
4748
- newcrop.ex.Value = str(float((bounds[2] // 50.) * 50.))
4749
- newcrop.oy.Value = str(float((bounds[1] // 50.) * 50.))
4750
- newcrop.ey.Value = str(float((bounds[3] // 50.) * 50.))
4912
+ newcrop.ox.Value = str(float((bounds[0] // 50.) * 50.))
4913
+ newcrop.ex.Value = str(float((bounds[2] // 50.) * 50.))
4914
+ newcrop.oy.Value = str(float((bounds[1] // 50.) * 50.))
4915
+ newcrop.ey.Value = str(float((bounds[3] // 50.) * 50.))
4751
4916
 
4752
- badvalues = True
4753
- while badvalues:
4754
- badvalues = False
4917
+ badvalues = True
4918
+ while badvalues:
4919
+ badvalues = False
4755
4920
 
4756
- ret = newcrop.ShowModal()
4757
- if ret == wx.ID_CANCEL:
4758
- newcrop.Destroy()
4759
- return
4760
- else:
4761
- crop = [float(newcrop.ox.Value), float(newcrop.ex.Value),
4762
- float(newcrop.oy.Value), float(newcrop.ey.Value)]
4921
+ ret = newcrop.ShowModal()
4922
+ if ret == wx.ID_CANCEL:
4923
+ newcrop.Destroy()
4924
+ return
4925
+ else:
4926
+ crop = [float(newcrop.ox.Value), float(newcrop.ex.Value),
4927
+ float(newcrop.oy.Value), float(newcrop.ey.Value)]
4928
+
4929
+ tmpdx = float(newcrop.dx.Value)
4930
+ tmpdy = float(newcrop.dy.Value)
4763
4931
 
4764
- tmpdx = float(newcrop.dx.Value)
4765
- tmpdy = float(newcrop.dy.Value)
4932
+ if self.dx != tmpdx or self.dy != tmpdy:
4933
+ if tmpdx / self.dx != tmpdy / self.dy:
4934
+ badvalues = True
4766
4935
 
4767
- if self.dx != tmpdx or self.dy != tmpdy:
4768
- if tmpdx / self.dx != tmpdy / self.dy:
4769
- badvalues = True
4936
+ newcrop.Destroy()
4770
4937
 
4771
- newcrop.Destroy()
4938
+ xmin, xmax, ymin, ymax = crop
4772
4939
 
4773
- xmin, xmax, ymin, ymax = crop
4940
+ if self.wx_exists:
4774
4941
 
4775
4942
  with wx.FileDialog(None, _('Save the cropped file for later'), wildcard="Tiff files (*.tif)|*.tif",
4776
4943
  style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as fileDialog:
@@ -4783,6 +4950,11 @@ class WolfArray(Element_To_Draw, header_wolf):
4783
4950
 
4784
4951
  gdal.Translate(fn_crop, fn, projWin=[xmin, ymax, xmax, ymin])
4785
4952
  fn = fn_crop
4953
+ else:
4954
+ from tempfile import NamedTemporaryFile
4955
+ tmpfile = NamedTemporaryFile(suffix='.tif')
4956
+ gdal.Translate(str(tmpfile.name), fn, projWin=[xmin, ymax, xmax, ymin])
4957
+ fn = str(tmpfile.name)
4786
4958
 
4787
4959
  raster:gdal.Dataset
4788
4960
  raster = gdal.Open(fn)
@@ -4858,13 +5030,16 @@ class WolfArray(Element_To_Draw, header_wolf):
4858
5030
  elif self.array.dtype == np.int32:
4859
5031
  self.wolftype = WOLF_ARRAY_FULL_INTEGER
4860
5032
  elif self.array.dtype == np.uint8:
4861
- logging.warning(_('Conversion of uint8 to int8'))
5033
+ logging.warning(_('***************************************************'))
5034
+ logging.warning(_(' Conversion of uint8 to int8'))
5035
+ logging.warning(_(' If you save this file, it will be converted to int8'))
5036
+ logging.warning(_('***************************************************'))
4862
5037
  self.array = self.array.astype(np.int8)
4863
5038
  self.wolftype = WOLF_ARRAY_FULL_INTEGER8
4864
5039
  elif self.array.dtype == np.int8:
4865
5040
  self.wolftype = WOLF_ARRAY_FULL_INTEGER8
4866
-
4867
- self.mask_data(self.nullvalue)
5041
+ elif self.array.dtype == np.int16:
5042
+ self.wolftype = WOLF_ARRAY_FULL_INTEGER16
4868
5043
 
4869
5044
  except:
4870
5045
  logging.warning(_('Error during importing tif file'))
@@ -5860,8 +6035,9 @@ class WolfArray(Element_To_Draw, header_wolf):
5860
6035
 
5861
6036
  self.loaded = True
5862
6037
 
5863
- if self.rgb is None:
5864
- self.updatepalette(0)
6038
+ if VERSION_RGB==1 :
6039
+ if self.rgb is None:
6040
+ self.updatepalette(0)
5865
6041
 
5866
6042
  def uncheck_plot(self, unload:bool=True, forceresetOGL:bool=False, askquestion:bool=True):
5867
6043
  """
@@ -5886,7 +6062,7 @@ class WolfArray(Element_To_Draw, header_wolf):
5886
6062
  if unload:
5887
6063
  self.delete_lists()
5888
6064
  self.array = np.zeros([1])
5889
- self.rgb = None
6065
+ if VERSION_RGB==1 : self.rgb = None
5890
6066
  self.loaded = False
5891
6067
  return
5892
6068
 
@@ -5896,10 +6072,10 @@ class WolfArray(Element_To_Draw, header_wolf):
5896
6072
  ret = dlg.ShowModal()
5897
6073
  if ret == wx.ID_YES:
5898
6074
  self.delete_lists()
5899
- self.rgb = None
6075
+ if VERSION_RGB==1 : self.rgb = None
5900
6076
  else:
5901
6077
  self.delete_lists()
5902
- self.rgb = None
6078
+ if VERSION_RGB==1 : self.rgb = None
5903
6079
 
5904
6080
  def get_header(self, abs:bool=True) -> header_wolf:
5905
6081
  """
@@ -5964,7 +6140,7 @@ class WolfArray(Element_To_Draw, header_wolf):
5964
6140
 
5965
6141
  def __add__(self, other):
5966
6142
  """Surcharge de l'opérateur d'addition"""
5967
- newArray = WolfArray()
6143
+ newArray = WolfArray(whichtype=self.wolftype)
5968
6144
  newArray.nbx = self.nbx
5969
6145
  newArray.nby = self.nby
5970
6146
  newArray.dx = self.dx
@@ -5985,11 +6161,12 @@ class WolfArray(Element_To_Draw, header_wolf):
5985
6161
  newArray.array = np.ma.masked_array(self.array + other, self.array.mask)
5986
6162
  else:
5987
6163
  newArray.array = np.ma.masked_array(self.array + other.array, self.array.mask)
6164
+ newArray.count()
5988
6165
  return newArray
5989
6166
 
5990
6167
  def __mul__(self, other):
5991
6168
  """Surcharge de l'opérateur d'addition"""
5992
- newArray = WolfArray()
6169
+ newArray = WolfArray(whichtype=self.wolftype)
5993
6170
  newArray.nbx = self.nbx
5994
6171
  newArray.nby = self.nby
5995
6172
  newArray.dx = self.dx
@@ -6010,11 +6187,12 @@ class WolfArray(Element_To_Draw, header_wolf):
6010
6187
  newArray.array = np.ma.masked_array(self.array * other, self.array.mask)
6011
6188
  else:
6012
6189
  newArray.array = np.ma.masked_array(self.array * other.array, self.array.mask)
6190
+ newArray.count()
6013
6191
  return newArray
6014
6192
 
6015
6193
  def __sub__(self, other):
6016
6194
  """Surcharge de l'opérateur de soustraction"""
6017
- newArray = WolfArray()
6195
+ newArray = WolfArray(whichtype=self.wolftype)
6018
6196
  newArray.nbx = self.nbx
6019
6197
  newArray.nby = self.nby
6020
6198
  newArray.dx = self.dx
@@ -6035,11 +6213,12 @@ class WolfArray(Element_To_Draw, header_wolf):
6035
6213
  newArray.array = np.ma.masked_array(self.array - other, self.array.mask)
6036
6214
  else:
6037
6215
  newArray.array = np.ma.masked_array(self.array - other.array, self.array.mask)
6216
+ newArray.count()
6038
6217
  return newArray
6039
6218
 
6040
6219
  def __pow__(self, other):
6041
6220
  """Surcharge de l'opérateur puissance"""
6042
- newArray = WolfArray()
6221
+ newArray = WolfArray(whichtype=self.wolftype)
6043
6222
  newArray.nbx = self.nbx
6044
6223
  newArray.nby = self.nby
6045
6224
  newArray.dx = self.dx
@@ -6056,11 +6235,12 @@ class WolfArray(Element_To_Draw, header_wolf):
6056
6235
  newArray.translz = self.translz
6057
6236
 
6058
6237
  newArray.array = np.ma.masked_array(self.array ** other, self.array.mask)
6238
+ newArray.count()
6059
6239
  return newArray
6060
6240
 
6061
6241
  def __truediv__(self, other):
6062
6242
  """Surcharge de l'opérateur division"""
6063
- newArray = WolfArray()
6243
+ newArray = WolfArray(whichtype=self.wolftype)
6064
6244
  newArray.nbx = self.nbx
6065
6245
  newArray.nby = self.nby
6066
6246
  newArray.dx = self.dx
@@ -6081,7 +6261,7 @@ class WolfArray(Element_To_Draw, header_wolf):
6081
6261
  newArray.array = np.ma.masked_array(self.array / other, self.array.mask)
6082
6262
  else:
6083
6263
  newArray.array = np.ma.masked_array(np.where(other == 0., 0., self.array / other.array), self.array.mask)
6084
-
6264
+ newArray.count()
6085
6265
  return newArray
6086
6266
 
6087
6267
  def concatenate(self, list_arr:list["WolfArray"], nullvalue:float = 0.):
@@ -6363,70 +6543,48 @@ class WolfArray(Element_To_Draw, header_wolf):
6363
6543
  def read_all(self, which_band = None):
6364
6544
  """ Lecture d'un Wolf aray depuis le nom de fichier """
6365
6545
 
6546
+ THRESHOLD = 20_000_000
6547
+
6366
6548
  if not os.path.exists(self.filename):
6367
6549
  if self.wx_exists:
6368
6550
  logging.warning(_('No data file : ')+self.filename)
6369
6551
  return
6552
+
6553
+ def check_threshold(nbx, nby, THRESHOLD) -> bool:
6554
+ if nbx * nby > THRESHOLD:
6555
+ logging.info(_('The array is very large > 100M pixels'))
6556
+ logging.info(_('Preloading is not recommended for efficiency reasons'))
6557
+ logging.info(_('Maybe could you crop the array to a smaller size'))
6558
+ logging.info(_('Disabling automatic colormap update'))
6559
+ self.mypal.automatic = False
6560
+ return True
6561
+ else:
6562
+ return False
6370
6563
 
6371
6564
  if self.filename.endswith('.tif') or self.filename.endswith('.tiff'):
6372
- self.import_geotif(which= which_band, crop = self.cropini)
6373
- # self.mask_data(self.nullvalue)
6374
- self.loaded = True
6375
- elif self.filename.endswith('.npy'):
6376
- # Numpy format
6377
- locarray = np.load(self.filename)
6378
- self.array = np.ma.asarray(locarray)
6379
-
6380
- self.nbx, self.nby = self.array.shape
6565
+ self.read_txt_header()
6381
6566
 
6382
- if locarray.dtype == np.float32:
6383
- self.wolftype = WOLF_ARRAY_FULL_SINGLE
6384
- elif locarray.dtype == np.float64:
6385
- self.wolftype = WOLF_ARRAY_FULL_DOUBLE
6386
- elif locarray.dtype == np.int32:
6387
- self.wolftype = WOLF_ARRAY_FULL_INTEGER
6388
- elif locarray.dtype == np.int16:
6389
- self.wolftype = WOLF_ARRAY_FULL_INTEGER16
6390
- elif locarray.dtype == np.uint8:
6391
- self.wolftype = WOLF_ARRAY_FULL_INTEGER8
6392
- elif locarray.dtype == np.int8:
6393
- self.wolftype = WOLF_ARRAY_FULL_INTEGER8
6394
- else:
6395
- logging.error(_('Unsupported type in numpy file -- Abort loading'))
6396
- return
6567
+ if self.preload:
6568
+
6569
+ update_min_max = check_threshold(self.nbx, self.nby, THRESHOLD)
6397
6570
 
6398
- tmp_wolftype = self.wolftype
6571
+ self.import_geotif(which= which_band, crop = self.cropini)
6572
+ self.loaded = True
6399
6573
 
6400
- # Il y de fortes chances que cette matrice numpy provienne d'une modélisation GPU
6401
- # et donc que les coordonnées et la résolution soient disponibles dans un fichier parameters.json
6402
- locpath = Path(self.filename)
6403
- if (locpath.parent / 'parameters.json').exists():
6404
- with open(locpath.parent / 'parameters.json', 'r') as f:
6405
- params = json.load(f)
6574
+ if update_min_max:
6575
+ self.mypal.distribute_values(self.array.min(), self.array.max())
6406
6576
 
6407
- if 'parameters' in params.keys():
6408
- if "dx" in params['parameters'].keys() :
6409
- self.dx = float(params['parameters']["dx"])
6410
- if "dy" in params['parameters'].keys() :
6411
- self.dy = float(params['parameters']["dy"])
6412
- if "base_coord_x" in params['parameters'].keys() :
6413
- self.origx = float(params['parameters']["base_coord_x"])
6414
- if "base_coord_y" in params['parameters'].keys() :
6415
- self.origy = float(params['parameters']["base_coord_y"])
6577
+ elif self.filename.endswith('.npy'):
6578
+ self.read_txt_header()
6416
6579
 
6417
- self.nullvalue = 99999.
6418
- elif (locpath.parent / (locpath.name + '.txt')).exists():
6419
- self.read_txt_header()
6580
+ if self.preload:
6581
+ update_min_max = check_threshold(self.nbx, self.nby, THRESHOLD)
6420
6582
 
6421
- # Le header contient également un type
6422
- # Vérification de la cohérence
6423
- if self.wolftype != tmp_wolftype:
6424
- logging.warning(_('Inconsistent type in header and numpy file'))
6425
- logging.warning(_('Forcing type from Numpy file'))
6426
- self.wolftype = tmp_wolftype
6583
+ self._import_npy(crop = self.cropini)
6584
+ self.loaded = True
6427
6585
 
6428
- self.mask_data(self.nullvalue)
6429
- self.loaded = True
6586
+ if update_min_max:
6587
+ self.mypal.distribute_values(self.array.min(), self.array.max())
6430
6588
 
6431
6589
  else:
6432
6590
  self.read_txt_header()
@@ -6436,9 +6594,13 @@ class WolfArray(Element_To_Draw, header_wolf):
6436
6594
  self.myblocks = {}
6437
6595
 
6438
6596
  if self.preload:
6597
+ update_min_max = check_threshold(self.nbx, self.nby, THRESHOLD)
6598
+
6439
6599
  self.read_data()
6440
6600
  self.loaded = True
6441
- return
6601
+
6602
+ if update_min_max:
6603
+ self.mypal.distribute_values(self.array.min(), self.array.max())
6442
6604
 
6443
6605
  def write_all(self, newpath:str = None):
6444
6606
  """
@@ -6802,8 +6964,17 @@ class WolfArray(Element_To_Draw, header_wolf):
6802
6964
  if self.array is None:
6803
6965
  return
6804
6966
 
6805
- if self.wolftype in [WOLF_ARRAY_FULL_INTEGER, WOLF_ARRAY_FULL_INTEGER16, WOLF_ARRAY_FULL_INTEGER16_2, WOLF_ARRAY_FULL_INTEGER8]:
6806
- value=int(value)
6967
+ try:
6968
+ if not (np.isnan(value) or math.isnan(value)):
6969
+ if self.wolftype in [WOLF_ARRAY_FULL_INTEGER]:
6970
+ value=np.int32(value)
6971
+ elif self.wolftype in [WOLF_ARRAY_FULL_INTEGER16, WOLF_ARRAY_FULL_INTEGER16_2]:
6972
+ value=np.int16(value)
6973
+ elif self.wolftype in [WOLF_ARRAY_FULL_INTEGER8]:
6974
+ value=np.int8(value)
6975
+ except:
6976
+ logging.error(_('Type not supported : {} - {}'.format(value, type(value))))
6977
+ logging.warning(_('Masking operation compromised'))
6807
6978
 
6808
6979
  if value is not None:
6809
6980
 
@@ -7149,29 +7320,44 @@ class WolfArray(Element_To_Draw, header_wolf):
7149
7320
  if self.array is None:
7150
7321
  return
7151
7322
 
7152
- if self.wolftype != WOLF_ARRAY_FULL_SINGLE:
7153
- # FIXME: Currently, only float32 is supported in Cython/OpenGL library
7154
- self._tmp_float32 = self.array.astype(dtype=np.float32)
7155
- else:
7156
- self._tmp_float32 = None
7157
-
7158
7323
  if self.mypal.automatic:
7159
7324
  if onzoom != []:
7160
7325
  self.mypal.isopop(self.get_working_array(onzoom), self.nbnotnullzoom)
7161
7326
  else:
7162
7327
  self.mypal.isopop(self.get_working_array(), self.nbnotnull)
7163
7328
 
7164
- self.rgb = self.mypal.get_rgba(self.array)
7329
+ if VERSION_RGB==1 :
7330
+ if self.nbx * self.nby > 1_000_000 : logging.info(_('Computing colors'))
7331
+ if self.wolftype not in [WOLF_ARRAY_FULL_SINGLE, WOLF_ARRAY_FULL_INTEGER8]:
7332
+ # FIXME: Currently, only some types are supported in Cython/OpenGL library
7333
+ self._tmp_float32 = self.array.astype(dtype=np.float32)
7334
+ self.rgb = self.mypal.get_rgba(self._tmp_float32)
7335
+ else:
7336
+ self._tmp_float32 = None
7337
+ self.rgb = self.mypal.get_rgba(self.array)
7338
+ if self.nbx * self.nby > 1_000_000 : logging.info(_('Colors computed'))
7339
+ elif VERSION_RGB==2 :
7340
+ if self.wolftype not in [WOLF_ARRAY_FULL_SINGLE,
7341
+ WOLF_ARRAY_FULL_INTEGER8,
7342
+ WOLF_ARRAY_FULL_INTEGER16,
7343
+ WOLF_ARRAY_FULL_INTEGER16_2,
7344
+ WOLF_ARRAY_FULL_INTEGER,
7345
+ WOLF_ARRAY_FULL_DOUBLE]:
7346
+ # FIXME: Currently, only some types are supported in Cython/OpenGL library
7347
+ self._tmp_float32 = self.array.astype(dtype=np.float32)
7348
+ else:
7349
+ self._tmp_float32 = None
7350
+
7165
7351
 
7166
- if self.shading:
7167
- pond = (self.shaded.array-.5)*2.
7168
- pmin = (1. - self.shaded.alpha) * self.rgb
7169
- pmax = self.shaded.alpha * np.ones(self.rgb.shape) + (1. - self.shaded.alpha) * self.rgb
7170
- for i in range(4):
7171
- self.rgb[pond<0,i] = self.rgb[pond<0,i] * (1.+pond[pond<0]) - pmin[pond<0,i] * pond[pond<0]
7172
- self.rgb[pond>0,i] = self.rgb[pond>0,i] * (1.-pond[pond>0]) + pmax[pond>0,i] * pond[pond>0]
7352
+ # if self.shading:
7353
+ # pond = (self.shaded.array-.5)*2.
7354
+ # pmin = (1. - self.shaded.alpha) * self.rgb
7355
+ # pmax = self.shaded.alpha * np.ones(self.rgb.shape) + (1. - self.shaded.alpha) * self.rgb
7356
+ # for i in range(4):
7357
+ # self.rgb[pond<0,i] = self.rgb[pond<0,i] * (1.+pond[pond<0]) - pmin[pond<0,i] * pond[pond<0]
7358
+ # self.rgb[pond>0,i] = self.rgb[pond>0,i] * (1.-pond[pond>0]) + pmax[pond>0,i] * pond[pond>0]
7173
7359
 
7174
- self.rgb[self.array.mask] = [1., 1., 1., 0.]
7360
+ if VERSION_RGB==1 : self.rgb[self.array.mask] = [1., 1., 1., 0.]
7175
7361
 
7176
7362
  if self.myops is not None:
7177
7363
  # update the wx
@@ -7333,6 +7519,8 @@ class WolfArray(Element_To_Draw, header_wolf):
7333
7519
  def fillonecellgrid(self, curscale, loci, locj, force=False):
7334
7520
  """ Fill one cell of the plotted grid """
7335
7521
 
7522
+
7523
+
7336
7524
  cursize = curscale # 2**curscale
7337
7525
 
7338
7526
  if not cursize in self.mygrid.keys():
@@ -7363,26 +7551,81 @@ class WolfArray(Element_To_Draw, header_wolf):
7363
7551
  iend = min(istart + step, self.nbx)
7364
7552
 
7365
7553
  try:
7366
- if self.wolftype != WOLF_ARRAY_FULL_SINGLE:
7367
- if self.nbnotnull != self.nbx * self.nby:
7368
- if self.nbnotnull > 0:
7369
- wolfogl.addme(self._tmp_float32, self.rgb, ox, oy, dx, dy, jstart,
7370
- jend, istart, iend, cursize, self.nullvalue, self.alpha)
7371
- elif self.nbnotnull > 0:
7372
- wolfogl.addmeall(self._tmp_float32, self.rgb, ox, oy, dx, dy, jstart,
7373
- jend, istart, iend, cursize, self.nullvalue, self.alpha)
7374
- else:
7375
- if self.nbnotnull != self.nbx * self.nby:
7376
- if self.nbnotnull > 0:
7377
- wolfogl.addme(self.array, self.rgb, ox, oy, dx, dy, jstart, jend, istart, iend, cursize,
7378
- self.nullvalue, self.alpha)
7379
- elif self.nbnotnull > 0:
7380
- wolfogl.addmeall(self.array, self.rgb, ox, oy, dx, dy, jstart, jend, istart, iend, cursize,
7381
- self.nullvalue, self.alpha)
7382
- except:
7554
+ if VERSION_RGB == 1:
7555
+ if self.wolftype != WOLF_ARRAY_FULL_SINGLE:
7556
+ if self.nbnotnull != self.nbx * self.nby:
7557
+ if self.nbnotnull > 0:
7558
+ wolfogl.addme_uint8(self._tmp_float32, self.rgb, ox, oy, dx, dy, jstart,
7559
+ jend, istart, iend, cursize, self.nullvalue, np.uint8(self.alpha*255))
7560
+ elif self.nbnotnull > 0:
7561
+ wolfogl.addmeall_uint8(self._tmp_float32, self.rgb, ox, oy, dx, dy, jstart,
7562
+ jend, istart, iend, cursize, self.nullvalue, np.uint8(self.alpha*255))
7563
+ else:
7564
+ if self.nbnotnull != self.nbx * self.nby:
7565
+ if self.nbnotnull > 0:
7566
+ wolfogl.addme_uint8(self.array, self.rgb, ox, oy, dx, dy, jstart, jend, istart, iend, cursize,
7567
+ self.nullvalue, np.uint8(self.alpha*255))
7568
+ elif self.nbnotnull > 0:
7569
+ wolfogl.addmeall_uint8(self.array, self.rgb, ox, oy, dx, dy, jstart, jend, istart, iend, cursize,
7570
+ self.nullvalue, np.uint8(self.alpha*255))
7571
+ elif VERSION_RGB == 2:
7572
+ if self.wolftype == WOLF_ARRAY_FULL_INTEGER8:
7573
+ if self.nbnotnull != self.nbx * self.nby:
7574
+ if self.nbnotnull > 0:
7575
+ wolfogl.addme_int8_pal(self.array, self.mypal.colorsflt, self.mypal.values, ox, oy, dx, dy, jstart,
7576
+ jend, istart, iend, cursize, self.nullvalue, self.alpha, int(self.mypal.interval_cst))
7577
+ elif self.nbnotnull > 0:
7578
+ wolfogl.addmeall_int8_pal(self.array, self.mypal.colorsflt, self.mypal.values, ox, oy, dx, dy, jstart,
7579
+ jend, istart, iend, cursize, self.nullvalue, self.alpha, int(self.mypal.interval_cst))
7580
+ elif self.wolftype in [WOLF_ARRAY_FULL_INTEGER16, WOLF_ARRAY_FULL_INTEGER16_2]:
7581
+ if self.nbnotnull != self.nbx * self.nby:
7582
+ if self.nbnotnull > 0:
7583
+ wolfogl.addme_int16_pal(self.array, self.mypal.colorsflt, self.mypal.values, ox, oy, dx, dy, jstart,
7584
+ jend, istart, iend, cursize, self.nullvalue, self.alpha, int(self.mypal.interval_cst))
7585
+ elif self.nbnotnull > 0:
7586
+ wolfogl.addmeall_int16_pal(self.array, self.mypal.colorsflt, self.mypal.values, ox, oy, dx, dy, jstart,
7587
+ jend, istart, iend, cursize, self.nullvalue, self.alpha, int(self.mypal.interval_cst))
7588
+ elif self.wolftype == WOLF_ARRAY_FULL_INTEGER:
7589
+ if self.nbnotnull != self.nbx * self.nby:
7590
+ if self.nbnotnull > 0:
7591
+ wolfogl.addme_int_pal(self.array, self.mypal.colorsflt, self.mypal.values, ox, oy, dx, dy, jstart,
7592
+ jend, istart, iend, cursize, self.nullvalue, self.alpha, int(self.mypal.interval_cst))
7593
+ elif self.nbnotnull > 0:
7594
+ wolfogl.addmeall_int_pal(self.array, self.mypal.colorsflt, self.mypal.values, ox, oy, dx, dy, jstart,
7595
+ jend, istart, iend, cursize, self.nullvalue, self.alpha, int(self.mypal.interval_cst))
7596
+ elif self.wolftype == WOLF_ARRAY_FULL_DOUBLE:
7597
+ if self.nbnotnull != self.nbx * self.nby:
7598
+ if self.nbnotnull > 0:
7599
+ wolfogl.addme_double_pal(self.array, self.mypal.colorsflt, self.mypal.values, ox, oy, dx, dy, jstart,
7600
+ jend, istart, iend, cursize, self.nullvalue, self.alpha, int(self.mypal.interval_cst))
7601
+ elif self.nbnotnull > 0:
7602
+ wolfogl.addmeall_double_pal(self.array, self.mypal.colorsflt, self.mypal.values, ox, oy, dx, dy, jstart,
7603
+ jend, istart, iend, cursize, self.nullvalue, self.alpha, int(self.mypal.interval_cst))
7604
+ elif self.wolftype != WOLF_ARRAY_FULL_SINGLE:
7605
+ if self.nbnotnull != self.nbx * self.nby:
7606
+ if self.nbnotnull > 0:
7607
+ wolfogl.addme_pal(self._tmp_float32, self.mypal.colorsflt, self.mypal.values, ox, oy, dx, dy, jstart,
7608
+ jend, istart, iend, cursize, self.nullvalue, self.alpha, int(self.mypal.interval_cst))
7609
+ elif self.nbnotnull > 0:
7610
+ wolfogl.addmeall_pal(self._tmp_float32, self.mypal.colorsflt, self.mypal.values, ox, oy, dx, dy, jstart,
7611
+ jend, istart, iend, cursize, self.nullvalue, self.alpha, int(self.mypal.interval_cst))
7612
+ else:
7613
+ if self.nbnotnull != self.nbx * self.nby:
7614
+ if self.nbnotnull > 0:
7615
+ wolfogl.addme_pal(self.array, self.mypal.colorsflt, self.mypal.values, ox, oy, dx, dy, jstart, jend, istart, iend, cursize,
7616
+ self.nullvalue, self.alpha, int(self.mypal.interval_cst))
7617
+ elif self.nbnotnull > 0:
7618
+ wolfogl.addmeall_pal(self.array, self.mypal.colorsflt, self.mypal.values, ox, oy, dx, dy, jstart, jend, istart, iend, cursize,
7619
+ self.nullvalue, self.alpha, int(self.mypal.interval_cst))
7620
+
7621
+ except Exception as e:
7622
+ logging.error(repr(e))
7623
+ raise NameError(_('OpenGL error in WolfArray.fillonecellgrid -- Please report this case with the data file and the context in which the error occured'))
7624
+
7383
7625
  pass
7384
7626
  glEndList()
7385
- except:
7627
+ except Exception as e:
7628
+ logging.error(repr(e))
7386
7629
  raise NameError(
7387
7630
  'Opengl in WolfArray_fillonecellgrid -- maybe a conflict with an existing opengl32.dll file - please rename the opengl32.dll in the libs directory and retry')
7388
7631
 
@@ -7896,8 +8139,11 @@ class WolfArrayMB(WolfArray):
7896
8139
  self.read_data()
7897
8140
  if self.masknull:
7898
8141
  self.mask_data(self.nullvalue)
7899
- if self.rgb is None:
7900
- self.rgb = np.ones((self.nbx, self.nby, 4), order='F', dtype=np.integer)
8142
+
8143
+ if VERSION_RGB==1 :
8144
+ if self.rgb is None:
8145
+ self.rgb = np.ones((self.nbx, self.nby, 4), order='F', dtype=np.integer)
8146
+
7901
8147
  self.updatepalette(0)
7902
8148
  self.loaded = True
7903
8149
  else:
@@ -7923,7 +8169,7 @@ class WolfArrayMB(WolfArray):
7923
8169
 
7924
8170
  for curblock in self.myblocks.values():
7925
8171
  curblock.uncheck_plot(unload, forceresetOGL, askquestion=False)
7926
- self.rgb = None
8172
+ if VERSION_RGB==1 : self.rgb = None
7927
8173
 
7928
8174
  self.myblocks = {}
7929
8175
  self.loaded = False
@@ -8168,8 +8414,10 @@ class WolfArrayMB(WolfArray):
8168
8414
  self.mypal.isopop(allarrays, self.nbnotnull)
8169
8415
 
8170
8416
  self.link_palette()
8171
- for curblock in self.myblocks.values():
8172
- curblock.rgb = self.mypal.get_rgba(curblock.array)
8417
+
8418
+ if VERSION_RGB ==1:
8419
+ for curblock in self.myblocks.values():
8420
+ curblock.rgb = self.mypal.get_rgba(curblock.array)
8173
8421
 
8174
8422
  if self.myops is not None:
8175
8423
  self.myops.update_palette()