wolfhece 2.1.23__py3-none-any.whl → 2.1.27__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,7 +649,9 @@ class header_wolf():
648
649
  if isinstance(filename, Path):
649
650
  filename = str(filename)
650
651
 
651
- if filename.endswith('.tif'):
652
+ locpath = Path(filename)
653
+
654
+ if filename.endswith('.tif') or filename.endswith('.tiff') :
652
655
  from osgeo import gdal
653
656
 
654
657
  raster:gdal.Dataset
@@ -680,8 +683,12 @@ class header_wolf():
680
683
  """
681
684
 
682
685
  dtype = raster.GetRasterBand(1).DataType
683
-
684
- if dtype in [1, 4]:
686
+
687
+ if dtype == 1:
688
+ self.wolftype = WOLF_ARRAY_FULL_INTEGER8
689
+ elif dtype in [2,3]:
690
+ self.wolftype = WOLF_ARRAY_FULL_INTEGER16
691
+ elif dtype in [4,5] :
685
692
  self.wolftype = WOLF_ARRAY_FULL_INTEGER
686
693
  elif dtype ==6:
687
694
  self.wolftype = WOLF_ARRAY_FULL_SINGLE
@@ -692,9 +699,61 @@ class header_wolf():
692
699
  logging.error(_('Please convert the raster to a supported datatype - or upgrade the code to support this datatype'))
693
700
  logging.error(_('See : read_txt_header and import_geotif in wolf_array.py'))
694
701
  return
695
- return
696
702
 
697
- 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'):
698
757
  # Fichier .flt
699
758
  if not os.path.exists(filename[:-4] + '.hdr'):
700
759
  logging.warning(_('File {} does not exist -- Retry!'.format(filename[:-4] + '.hdr')))
@@ -1519,7 +1578,7 @@ class Ops_Array(wx.Frame):
1519
1578
  Toolssizer.Add(self.labelling, 1, wx.EXPAND)
1520
1579
  Toolssizer.Add(self.extract_selection, 1, wx.EXPAND)
1521
1580
 
1522
- self.ApplyTools.SetToolTip(_("Apply Nullvalue into memmory/object"))
1581
+ self.ApplyTools.SetToolTip(_("Apply Nullvalue into memory/object"))
1523
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)"))
1524
1583
  self.filter_zone.SetToolTip(_("Filter the array based on contiguous zones\n\nConservation of the ones which contain selected nodes"))
1525
1584
  self.labelling.SetToolTip(_("Labelling of contiguous zones using Scipy.label function\n\nReplacing the current values by the labels"))
@@ -4100,6 +4159,7 @@ class WolfArray(Element_To_Draw, header_wolf):
4100
4159
  mask_source:np.ndarray = None,
4101
4160
  ) -> None:
4102
4161
  """
4162
+ Constructor of the WolfArray class
4103
4163
 
4104
4164
  :param fname: filename/filepath - if provided, the file will be read on disk
4105
4165
  :param mold: initialize from a copy a the mold object --> must be a WolArray
@@ -4118,10 +4178,8 @@ class WolfArray(Element_To_Draw, header_wolf):
4118
4178
 
4119
4179
  """
4120
4180
 
4121
-
4122
4181
  Element_To_Draw.__init__(self, idx, plotted, mapviewer, need_for_wx)
4123
4182
  header_wolf.__init__(self)
4124
- """ Constructeur d'un WOLF array """
4125
4183
 
4126
4184
  self.mngselection = None
4127
4185
 
@@ -4143,7 +4201,7 @@ class WolfArray(Element_To_Draw, header_wolf):
4143
4201
  self.loaded = False
4144
4202
  self.masknull = masknull
4145
4203
 
4146
- self.rgb = None # numpy array with colorize values
4204
+ if VERSION_RGB==1 : self.rgb = None # numpy array with colorize values
4147
4205
  self.alpha = 1. # transparency alpha value
4148
4206
  self.shading = False # if True, rgb will be shaded
4149
4207
 
@@ -4154,6 +4212,8 @@ class WolfArray(Element_To_Draw, header_wolf):
4154
4212
  self.shaded = WolfArray(whichtype=WOLF_ARRAY_HILLSHAPE)
4155
4213
  self.shaded.mypal.defaultgray()
4156
4214
  self.shaded.mypal.automatic = False
4215
+ else:
4216
+ self.shaded = None
4157
4217
 
4158
4218
  self._nullvalue = nullvalue
4159
4219
  self.nbnotnull = 99999 # number of non-null values in the entire aray
@@ -4205,12 +4265,17 @@ class WolfArray(Element_To_Draw, header_wolf):
4205
4265
  if fname is not None:
4206
4266
 
4207
4267
  self.filename = str(fname)
4268
+ logging.info(_('Loading file : %s') % self.filename)
4208
4269
  self.read_all()
4209
4270
 
4210
4271
  if mask_source is not None:
4272
+ logging.info(_('Applying mask from source'))
4211
4273
  self.copy_mask_log(mask_source)
4212
- 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'))
4213
4277
  self.mask_data(self.nullvalue)
4278
+ logging.info(_('Data masked'))
4214
4279
 
4215
4280
  elif mold is not None:
4216
4281
  if self.cropini is None:
@@ -4265,6 +4330,54 @@ class WolfArray(Element_To_Draw, header_wolf):
4265
4330
 
4266
4331
  self.add_ops_sel() # Ajout d'un gestionnaire de sélection et d'opérations
4267
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
+
4268
4381
  def extract_selection(self):
4269
4382
  """ Extract the current selection """
4270
4383
 
@@ -4685,6 +4798,54 @@ class WolfArray(Element_To_Draw, header_wolf):
4685
4798
  band.FlushCache()
4686
4799
  band.ComputeStatistics(True)
4687
4800
 
4801
+ def _import_npy(self, fn:str='', crop:list[float]=None):
4802
+ """
4803
+ Import a numpy file.
4804
+
4805
+ Must be called after the header is initialized, e.g. read_txt_header.
4806
+
4807
+ :param fn: filename
4808
+ :param crop: crop the data - [xmin, xmax, ymin, ymax]
4809
+ """
4810
+
4811
+ if fn !='':
4812
+ pass
4813
+ elif self.filename !='':
4814
+ fn = self.filename
4815
+ else:
4816
+ return
4817
+
4818
+ # Numpy format
4819
+ locarray = np.load(self.filename)
4820
+
4821
+ assert locarray.shape[0] == self.nbx, _('Incompatible dimensions')
4822
+ assert locarray.shape[1] == self.nby, _('Incompatible dimensions')
4823
+
4824
+ if crop is not None :
4825
+ logging.error(_('Cropping not yet implemented for numpy files'))
4826
+
4827
+ imin, jmin = self.get_ij_from_xy(crop[0][0], crop[1][0])
4828
+ imax, jmax = self.get_ij_from_xy(crop[0][1], crop[1][1])
4829
+
4830
+ imin = int(imin)
4831
+ jmin = int(jmin)
4832
+ imax = int(imax)
4833
+ jmax = int(jmax)
4834
+
4835
+ self.nbx = imax - imin
4836
+ self.nby = jmax - jmin
4837
+ self.dx = self.dx
4838
+ self.dy = self.dy
4839
+ self.origx, self.origy = self.get_xy_from_ij(imin, jmin)
4840
+ self.origx -= self.dx / 2.
4841
+ self.origy -= self.dy / 2.
4842
+ self.translx = self.translx
4843
+ self.transly = self.transly
4844
+
4845
+ locarray = locarray[imin:imax, jmin:jmax]
4846
+
4847
+ self.array = np.ma.asarray(locarray)
4848
+
4688
4849
  def import_geotif(self, fn:str='', which:int = None, crop:list[float]=None):
4689
4850
  """
4690
4851
  Import de la matrice au format Geotiff
@@ -4708,65 +4869,69 @@ class WolfArray(Element_To_Draw, header_wolf):
4708
4869
  return
4709
4870
 
4710
4871
  if crop is not None :
4711
- if os.path.exists(fn):
4872
+ if not os.path.exists(fn):
4873
+ logging.error(_('File not found'))
4874
+ return
4712
4875
 
4713
- tmpdx = self.dx
4876
+ tmpdx = self.dx
4714
4877
 
4715
- fn_crop = fn + '_crop.tif'
4716
- if type(crop) is np.ndarray:
4717
- pass
4718
- elif type(crop) is list:
4719
- pass
4720
- else:
4721
- if not self.wx_exists:
4722
- logging.error(_('Crop must be a list or a numpy array with 4 values - xmin, xmax, ymin, ymax'))
4723
- return
4878
+ fn_crop = fn + '_crop.tif'
4879
+ if type(crop) is np.ndarray:
4880
+ pass
4881
+ elif type(crop) is list:
4882
+ pass
4883
+ else:
4884
+ if not self.wx_exists:
4885
+ logging.error(_('Crop must be a list or a numpy array with 4 values - xmin, xmax, ymin, ymax'))
4886
+ return
4724
4887
 
4725
- raster:gdal.Dataset
4726
- raster = gdal.Open(fn)
4727
- geotr = raster.GetGeoTransform()
4728
- self.dx = geotr[1]
4729
- self.dy = abs(geotr[5])
4888
+ raster:gdal.Dataset
4889
+ raster = gdal.Open(fn)
4890
+ geotr = raster.GetGeoTransform()
4891
+ self.dx = geotr[1]
4892
+ self.dy = abs(geotr[5])
4730
4893
 
4731
4894
 
4732
- newcrop = CropDialog(None)
4895
+ newcrop = CropDialog(None)
4733
4896
 
4734
- if self.wx_exists:
4735
- bounds = self.mapviewer.get_canvas_bounds()
4897
+ if self.wx_exists:
4898
+ bounds = self.mapviewer.get_canvas_bounds()
4736
4899
 
4737
- newcrop.dx.Value = str(self.dx)
4738
- newcrop.dy.Value = str(self.dy)
4900
+ newcrop.dx.Value = str(self.dx)
4901
+ newcrop.dy.Value = str(self.dy)
4739
4902
 
4740
- newcrop.dx.Enable(False)
4741
- newcrop.dy.Enable(False)
4903
+ newcrop.dx.Enable(False)
4904
+ newcrop.dy.Enable(False)
4742
4905
 
4743
- newcrop.ox.Value = str(float((bounds[0] // 50.) * 50.))
4744
- newcrop.ex.Value = str(float((bounds[2] // 50.) * 50.))
4745
- newcrop.oy.Value = str(float((bounds[1] // 50.) * 50.))
4746
- newcrop.ey.Value = str(float((bounds[3] // 50.) * 50.))
4906
+ newcrop.ox.Value = str(float((bounds[0] // 50.) * 50.))
4907
+ newcrop.ex.Value = str(float((bounds[2] // 50.) * 50.))
4908
+ newcrop.oy.Value = str(float((bounds[1] // 50.) * 50.))
4909
+ newcrop.ey.Value = str(float((bounds[3] // 50.) * 50.))
4747
4910
 
4748
- badvalues = True
4749
- while badvalues:
4750
- badvalues = False
4911
+ badvalues = True
4912
+ while badvalues:
4913
+ badvalues = False
4751
4914
 
4752
- ret = newcrop.ShowModal()
4753
- if ret == wx.ID_CANCEL:
4754
- newcrop.Destroy()
4755
- return
4756
- else:
4757
- crop = [float(newcrop.ox.Value), float(newcrop.ex.Value),
4758
- float(newcrop.oy.Value), float(newcrop.ey.Value)]
4915
+ ret = newcrop.ShowModal()
4916
+ if ret == wx.ID_CANCEL:
4917
+ newcrop.Destroy()
4918
+ return
4919
+ else:
4920
+ crop = [float(newcrop.ox.Value), float(newcrop.ex.Value),
4921
+ float(newcrop.oy.Value), float(newcrop.ey.Value)]
4922
+
4923
+ tmpdx = float(newcrop.dx.Value)
4924
+ tmpdy = float(newcrop.dy.Value)
4759
4925
 
4760
- tmpdx = float(newcrop.dx.Value)
4761
- tmpdy = float(newcrop.dy.Value)
4926
+ if self.dx != tmpdx or self.dy != tmpdy:
4927
+ if tmpdx / self.dx != tmpdy / self.dy:
4928
+ badvalues = True
4762
4929
 
4763
- if self.dx != tmpdx or self.dy != tmpdy:
4764
- if tmpdx / self.dx != tmpdy / self.dy:
4765
- badvalues = True
4930
+ newcrop.Destroy()
4766
4931
 
4767
- newcrop.Destroy()
4932
+ xmin, xmax, ymin, ymax = crop
4768
4933
 
4769
- xmin, xmax, ymin, ymax = crop
4934
+ if self.wx_exists:
4770
4935
 
4771
4936
  with wx.FileDialog(None, _('Save the cropped file for later'), wildcard="Tiff files (*.tif)|*.tif",
4772
4937
  style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as fileDialog:
@@ -4779,6 +4944,11 @@ class WolfArray(Element_To_Draw, header_wolf):
4779
4944
 
4780
4945
  gdal.Translate(fn_crop, fn, projWin=[xmin, ymax, xmax, ymin])
4781
4946
  fn = fn_crop
4947
+ else:
4948
+ from tempfile import NamedTemporaryFile
4949
+ tmpfile = NamedTemporaryFile(suffix='.tif')
4950
+ gdal.Translate(str(tmpfile.name), fn, projWin=[xmin, ymax, xmax, ymin])
4951
+ fn = str(tmpfile.name)
4782
4952
 
4783
4953
  raster:gdal.Dataset
4784
4954
  raster = gdal.Open(fn)
@@ -4854,10 +5024,11 @@ class WolfArray(Element_To_Draw, header_wolf):
4854
5024
  elif self.array.dtype == np.int32:
4855
5025
  self.wolftype = WOLF_ARRAY_FULL_INTEGER
4856
5026
  elif self.array.dtype == np.uint8:
4857
- self.array = self.array.astype(np.int32)
5027
+ logging.warning(_('Conversion of uint8 to int8'))
5028
+ self.array = self.array.astype(np.int8)
5029
+ self.wolftype = WOLF_ARRAY_FULL_INTEGER8
5030
+ elif self.array.dtype == np.int8:
4858
5031
  self.wolftype = WOLF_ARRAY_FULL_INTEGER8
4859
-
4860
- self.mask_data(self.nullvalue)
4861
5032
 
4862
5033
  except:
4863
5034
  logging.warning(_('Error during importing tif file'))
@@ -5697,6 +5868,8 @@ class WolfArray(Element_To_Draw, header_wolf):
5697
5868
  dtype = np.int32
5698
5869
  elif self.wolftype in [WOLF_ARRAY_FULL_INTEGER16, WOLF_ARRAY_FULL_INTEGER16_2]:
5699
5870
  dtype = np.int16
5871
+ elif self.wolftype == WOLF_ARRAY_FULL_INTEGER8:
5872
+ dtype = np.int8
5700
5873
 
5701
5874
  self.dx = myhead.dx
5702
5875
  self.dy = myhead.dy
@@ -5851,8 +6024,9 @@ class WolfArray(Element_To_Draw, header_wolf):
5851
6024
 
5852
6025
  self.loaded = True
5853
6026
 
5854
- if self.rgb is None:
5855
- self.updatepalette(0)
6027
+ if VERSION_RGB==1 :
6028
+ if self.rgb is None:
6029
+ self.updatepalette(0)
5856
6030
 
5857
6031
  def uncheck_plot(self, unload:bool=True, forceresetOGL:bool=False, askquestion:bool=True):
5858
6032
  """
@@ -5877,7 +6051,7 @@ class WolfArray(Element_To_Draw, header_wolf):
5877
6051
  if unload:
5878
6052
  self.delete_lists()
5879
6053
  self.array = np.zeros([1])
5880
- self.rgb = None
6054
+ if VERSION_RGB==1 : self.rgb = None
5881
6055
  self.loaded = False
5882
6056
  return
5883
6057
 
@@ -5887,10 +6061,10 @@ class WolfArray(Element_To_Draw, header_wolf):
5887
6061
  ret = dlg.ShowModal()
5888
6062
  if ret == wx.ID_YES:
5889
6063
  self.delete_lists()
5890
- self.rgb = None
6064
+ if VERSION_RGB==1 : self.rgb = None
5891
6065
  else:
5892
6066
  self.delete_lists()
5893
- self.rgb = None
6067
+ if VERSION_RGB==1 : self.rgb = None
5894
6068
 
5895
6069
  def get_header(self, abs:bool=True) -> header_wolf:
5896
6070
  """
@@ -5955,7 +6129,7 @@ class WolfArray(Element_To_Draw, header_wolf):
5955
6129
 
5956
6130
  def __add__(self, other):
5957
6131
  """Surcharge de l'opérateur d'addition"""
5958
- newArray = WolfArray()
6132
+ newArray = WolfArray(whichtype=self.wolftype)
5959
6133
  newArray.nbx = self.nbx
5960
6134
  newArray.nby = self.nby
5961
6135
  newArray.dx = self.dx
@@ -5976,11 +6150,12 @@ class WolfArray(Element_To_Draw, header_wolf):
5976
6150
  newArray.array = np.ma.masked_array(self.array + other, self.array.mask)
5977
6151
  else:
5978
6152
  newArray.array = np.ma.masked_array(self.array + other.array, self.array.mask)
6153
+ newArray.count()
5979
6154
  return newArray
5980
6155
 
5981
6156
  def __mul__(self, other):
5982
6157
  """Surcharge de l'opérateur d'addition"""
5983
- newArray = WolfArray()
6158
+ newArray = WolfArray(whichtype=self.wolftype)
5984
6159
  newArray.nbx = self.nbx
5985
6160
  newArray.nby = self.nby
5986
6161
  newArray.dx = self.dx
@@ -6001,11 +6176,12 @@ class WolfArray(Element_To_Draw, header_wolf):
6001
6176
  newArray.array = np.ma.masked_array(self.array * other, self.array.mask)
6002
6177
  else:
6003
6178
  newArray.array = np.ma.masked_array(self.array * other.array, self.array.mask)
6179
+ newArray.count()
6004
6180
  return newArray
6005
6181
 
6006
6182
  def __sub__(self, other):
6007
6183
  """Surcharge de l'opérateur de soustraction"""
6008
- newArray = WolfArray()
6184
+ newArray = WolfArray(whichtype=self.wolftype)
6009
6185
  newArray.nbx = self.nbx
6010
6186
  newArray.nby = self.nby
6011
6187
  newArray.dx = self.dx
@@ -6026,11 +6202,12 @@ class WolfArray(Element_To_Draw, header_wolf):
6026
6202
  newArray.array = np.ma.masked_array(self.array - other, self.array.mask)
6027
6203
  else:
6028
6204
  newArray.array = np.ma.masked_array(self.array - other.array, self.array.mask)
6205
+ newArray.count()
6029
6206
  return newArray
6030
6207
 
6031
6208
  def __pow__(self, other):
6032
6209
  """Surcharge de l'opérateur puissance"""
6033
- newArray = WolfArray()
6210
+ newArray = WolfArray(whichtype=self.wolftype)
6034
6211
  newArray.nbx = self.nbx
6035
6212
  newArray.nby = self.nby
6036
6213
  newArray.dx = self.dx
@@ -6047,11 +6224,12 @@ class WolfArray(Element_To_Draw, header_wolf):
6047
6224
  newArray.translz = self.translz
6048
6225
 
6049
6226
  newArray.array = np.ma.masked_array(self.array ** other, self.array.mask)
6227
+ newArray.count()
6050
6228
  return newArray
6051
6229
 
6052
6230
  def __truediv__(self, other):
6053
6231
  """Surcharge de l'opérateur division"""
6054
- newArray = WolfArray()
6232
+ newArray = WolfArray(whichtype=self.wolftype)
6055
6233
  newArray.nbx = self.nbx
6056
6234
  newArray.nby = self.nby
6057
6235
  newArray.dx = self.dx
@@ -6072,7 +6250,7 @@ class WolfArray(Element_To_Draw, header_wolf):
6072
6250
  newArray.array = np.ma.masked_array(self.array / other, self.array.mask)
6073
6251
  else:
6074
6252
  newArray.array = np.ma.masked_array(np.where(other == 0., 0., self.array / other.array), self.array.mask)
6075
-
6253
+ newArray.count()
6076
6254
  return newArray
6077
6255
 
6078
6256
  def concatenate(self, list_arr:list["WolfArray"], nullvalue:float = 0.):
@@ -6354,68 +6532,48 @@ class WolfArray(Element_To_Draw, header_wolf):
6354
6532
  def read_all(self, which_band = None):
6355
6533
  """ Lecture d'un Wolf aray depuis le nom de fichier """
6356
6534
 
6535
+ THRESHOLD = 20_000_000
6536
+
6357
6537
  if not os.path.exists(self.filename):
6358
6538
  if self.wx_exists:
6359
6539
  logging.warning(_('No data file : ')+self.filename)
6360
6540
  return
6541
+
6542
+ def check_threshold(nbx, nby, THRESHOLD) -> bool:
6543
+ if nbx * nby > THRESHOLD:
6544
+ logging.info(_('The array is very large > 100M pixels'))
6545
+ logging.info(_('Preloading is not recommended for efficiency reasons'))
6546
+ logging.info(_('Maybe could you crop the array to a smaller size'))
6547
+ logging.info(_('Disabling automatic colormap update'))
6548
+ self.mypal.automatic = False
6549
+ return True
6550
+ else:
6551
+ return False
6361
6552
 
6362
6553
  if self.filename.endswith('.tif') or self.filename.endswith('.tiff'):
6363
- self.import_geotif(which= which_band, crop = self.cropini)
6364
- # self.mask_data(self.nullvalue)
6365
- self.loaded = True
6366
- elif self.filename.endswith('.npy'):
6367
- # Numpy format
6368
- locarray = np.load(self.filename)
6369
- self.array = np.ma.asarray(locarray)
6370
-
6371
- self.nbx, self.nby = self.array.shape
6554
+ self.read_txt_header()
6372
6555
 
6373
- if locarray.dtype == np.float32:
6374
- self.wolftype = WOLF_ARRAY_FULL_SINGLE
6375
- elif locarray.dtype == np.float64:
6376
- self.wolftype = WOLF_ARRAY_FULL_DOUBLE
6377
- elif locarray.dtype == np.int32:
6378
- self.wolftype = WOLF_ARRAY_FULL_INTEGER
6379
- elif locarray.dtype == np.int16:
6380
- self.wolftype = WOLF_ARRAY_FULL_INTEGER16
6381
- elif locarray.dtype == np.int8:
6382
- self.wolftype = WOLF_ARRAY_FULL_INTEGER8
6383
- else:
6384
- logging.error(_('Unsupported type in numpy file -- Abort loading'))
6385
- return
6556
+ if self.preload:
6557
+
6558
+ update_min_max = check_threshold(self.nbx, self.nby, THRESHOLD)
6386
6559
 
6387
- tmp_wolftype = self.wolftype
6560
+ self.import_geotif(which= which_band, crop = self.cropini)
6561
+ self.loaded = True
6388
6562
 
6389
- # Il y de fortes chances que cette matrice numpy provienne d'une modélisation GPU
6390
- # et donc que les coordonnées et la résolution soient disponibles dans un fichier parameters.json
6391
- locpath = Path(self.filename)
6392
- if (locpath.parent / 'parameters.json').exists():
6393
- with open(locpath.parent / 'parameters.json', 'r') as f:
6394
- params = json.load(f)
6563
+ if update_min_max:
6564
+ self.mypal.distribute_values(self.array.min(), self.array.max())
6395
6565
 
6396
- if 'parameters' in params.keys():
6397
- if "dx" in params['parameters'].keys() :
6398
- self.dx = float(params['parameters']["dx"])
6399
- if "dy" in params['parameters'].keys() :
6400
- self.dy = float(params['parameters']["dy"])
6401
- if "base_coord_x" in params['parameters'].keys() :
6402
- self.origx = float(params['parameters']["base_coord_x"])
6403
- if "base_coord_y" in params['parameters'].keys() :
6404
- self.origy = float(params['parameters']["base_coord_y"])
6566
+ elif self.filename.endswith('.npy'):
6567
+ self.read_txt_header()
6405
6568
 
6406
- self.nullvalue = 99999.
6407
- elif (locpath.parent / (locpath.name + '.txt')).exists():
6408
- self.read_txt_header()
6569
+ if self.preload:
6570
+ update_min_max = check_threshold(self.nbx, self.nby, THRESHOLD)
6409
6571
 
6410
- # Le header contient également un type
6411
- # Vérification de la cohérence
6412
- if self.wolftype != tmp_wolftype:
6413
- logging.warning(_('Inconsistent type in header and numpy file'))
6414
- logging.warning(_('Forcing type from Numpy file'))
6415
- self.wolftype = tmp_wolftype
6572
+ self._import_npy(crop = self.cropini)
6573
+ self.loaded = True
6416
6574
 
6417
- self.mask_data(self.nullvalue)
6418
- self.loaded = True
6575
+ if update_min_max:
6576
+ self.mypal.distribute_values(self.array.min(), self.array.max())
6419
6577
 
6420
6578
  else:
6421
6579
  self.read_txt_header()
@@ -6425,9 +6583,13 @@ class WolfArray(Element_To_Draw, header_wolf):
6425
6583
  self.myblocks = {}
6426
6584
 
6427
6585
  if self.preload:
6586
+ update_min_max = check_threshold(self.nbx, self.nby, THRESHOLD)
6587
+
6428
6588
  self.read_data()
6429
6589
  self.loaded = True
6430
- return
6590
+
6591
+ if update_min_max:
6592
+ self.mypal.distribute_values(self.array.min(), self.array.max())
6431
6593
 
6432
6594
  def write_all(self, newpath:str = None):
6433
6595
  """
@@ -6791,8 +6953,17 @@ class WolfArray(Element_To_Draw, header_wolf):
6791
6953
  if self.array is None:
6792
6954
  return
6793
6955
 
6794
- if self.wolftype in [WOLF_ARRAY_FULL_INTEGER, WOLF_ARRAY_FULL_INTEGER16, WOLF_ARRAY_FULL_INTEGER16_2]:
6795
- value=int(value)
6956
+ try:
6957
+ if not (np.isnan(value) or math.isnan(value)):
6958
+ if self.wolftype in [WOLF_ARRAY_FULL_INTEGER]:
6959
+ value=np.int32(value)
6960
+ elif self.wolftype in [WOLF_ARRAY_FULL_INTEGER16, WOLF_ARRAY_FULL_INTEGER16_2]:
6961
+ value=np.int16(value)
6962
+ elif self.wolftype in [WOLF_ARRAY_FULL_INTEGER8]:
6963
+ value=np.int8(value)
6964
+ except:
6965
+ logging.error(_('Type not supported : {} - {}'.format(value, type(value))))
6966
+ logging.warning(_('Masking operation compromised'))
6796
6967
 
6797
6968
  if value is not None:
6798
6969
 
@@ -7138,28 +7309,44 @@ class WolfArray(Element_To_Draw, header_wolf):
7138
7309
  if self.array is None:
7139
7310
  return
7140
7311
 
7141
- if self.wolftype != WOLF_ARRAY_FULL_SINGLE:
7142
- self._tmp_float32 = self.array.astype(dtype=np.float32)
7143
- else:
7144
- self._tmp_float32 = None
7145
-
7146
7312
  if self.mypal.automatic:
7147
7313
  if onzoom != []:
7148
7314
  self.mypal.isopop(self.get_working_array(onzoom), self.nbnotnullzoom)
7149
7315
  else:
7150
7316
  self.mypal.isopop(self.get_working_array(), self.nbnotnull)
7151
7317
 
7152
- self.rgb = self.mypal.get_rgba(self.array)
7318
+ if VERSION_RGB==1 :
7319
+ if self.nbx * self.nby > 1_000_000 : logging.info(_('Computing colors'))
7320
+ if self.wolftype not in [WOLF_ARRAY_FULL_SINGLE, WOLF_ARRAY_FULL_INTEGER8]:
7321
+ # FIXME: Currently, only some types are supported in Cython/OpenGL library
7322
+ self._tmp_float32 = self.array.astype(dtype=np.float32)
7323
+ self.rgb = self.mypal.get_rgba(self._tmp_float32)
7324
+ else:
7325
+ self._tmp_float32 = None
7326
+ self.rgb = self.mypal.get_rgba(self.array)
7327
+ if self.nbx * self.nby > 1_000_000 : logging.info(_('Colors computed'))
7328
+ elif VERSION_RGB==2 :
7329
+ if self.wolftype not in [WOLF_ARRAY_FULL_SINGLE,
7330
+ WOLF_ARRAY_FULL_INTEGER8,
7331
+ WOLF_ARRAY_FULL_INTEGER16,
7332
+ WOLF_ARRAY_FULL_INTEGER16_2,
7333
+ WOLF_ARRAY_FULL_INTEGER,
7334
+ WOLF_ARRAY_FULL_DOUBLE]:
7335
+ # FIXME: Currently, only some types are supported in Cython/OpenGL library
7336
+ self._tmp_float32 = self.array.astype(dtype=np.float32)
7337
+ else:
7338
+ self._tmp_float32 = None
7339
+
7153
7340
 
7154
- if self.shading:
7155
- pond = (self.shaded.array-.5)*2.
7156
- pmin = (1. - self.shaded.alpha) * self.rgb
7157
- pmax = self.shaded.alpha * np.ones(self.rgb.shape) + (1. - self.shaded.alpha) * self.rgb
7158
- for i in range(4):
7159
- self.rgb[pond<0,i] = self.rgb[pond<0,i] * (1.+pond[pond<0]) - pmin[pond<0,i] * pond[pond<0]
7160
- self.rgb[pond>0,i] = self.rgb[pond>0,i] * (1.-pond[pond>0]) + pmax[pond>0,i] * pond[pond>0]
7341
+ # if self.shading:
7342
+ # pond = (self.shaded.array-.5)*2.
7343
+ # pmin = (1. - self.shaded.alpha) * self.rgb
7344
+ # pmax = self.shaded.alpha * np.ones(self.rgb.shape) + (1. - self.shaded.alpha) * self.rgb
7345
+ # for i in range(4):
7346
+ # self.rgb[pond<0,i] = self.rgb[pond<0,i] * (1.+pond[pond<0]) - pmin[pond<0,i] * pond[pond<0]
7347
+ # self.rgb[pond>0,i] = self.rgb[pond>0,i] * (1.-pond[pond>0]) + pmax[pond>0,i] * pond[pond>0]
7161
7348
 
7162
- self.rgb[self.array.mask] = [1., 1., 1., 0.]
7349
+ if VERSION_RGB==1 : self.rgb[self.array.mask] = [1., 1., 1., 0.]
7163
7350
 
7164
7351
  if self.myops is not None:
7165
7352
  # update the wx
@@ -7321,6 +7508,8 @@ class WolfArray(Element_To_Draw, header_wolf):
7321
7508
  def fillonecellgrid(self, curscale, loci, locj, force=False):
7322
7509
  """ Fill one cell of the plotted grid """
7323
7510
 
7511
+
7512
+
7324
7513
  cursize = curscale # 2**curscale
7325
7514
 
7326
7515
  if not cursize in self.mygrid.keys():
@@ -7351,26 +7540,81 @@ class WolfArray(Element_To_Draw, header_wolf):
7351
7540
  iend = min(istart + step, self.nbx)
7352
7541
 
7353
7542
  try:
7354
- if self.wolftype != WOLF_ARRAY_FULL_SINGLE:
7355
- if self.nbnotnull != self.nbx * self.nby:
7356
- if self.nbnotnull > 0:
7357
- wolfogl.addme(self._tmp_float32, self.rgb, ox, oy, dx, dy, jstart,
7358
- jend, istart, iend, cursize, self.nullvalue, self.alpha)
7359
- elif self.nbnotnull > 0:
7360
- wolfogl.addmeall(self._tmp_float32, self.rgb, ox, oy, dx, dy, jstart,
7361
- jend, istart, iend, cursize, self.nullvalue, self.alpha)
7362
- else:
7363
- if self.nbnotnull != self.nbx * self.nby:
7364
- if self.nbnotnull > 0:
7365
- wolfogl.addme(self.array, self.rgb, ox, oy, dx, dy, jstart, jend, istart, iend, cursize,
7366
- self.nullvalue, self.alpha)
7367
- elif self.nbnotnull > 0:
7368
- wolfogl.addmeall(self.array, self.rgb, ox, oy, dx, dy, jstart, jend, istart, iend, cursize,
7369
- self.nullvalue, self.alpha)
7370
- except:
7543
+ if VERSION_RGB == 1:
7544
+ if self.wolftype != WOLF_ARRAY_FULL_SINGLE:
7545
+ if self.nbnotnull != self.nbx * self.nby:
7546
+ if self.nbnotnull > 0:
7547
+ wolfogl.addme_uint8(self._tmp_float32, self.rgb, ox, oy, dx, dy, jstart,
7548
+ jend, istart, iend, cursize, self.nullvalue, np.uint8(self.alpha*255))
7549
+ elif self.nbnotnull > 0:
7550
+ wolfogl.addmeall_uint8(self._tmp_float32, self.rgb, ox, oy, dx, dy, jstart,
7551
+ jend, istart, iend, cursize, self.nullvalue, np.uint8(self.alpha*255))
7552
+ else:
7553
+ if self.nbnotnull != self.nbx * self.nby:
7554
+ if self.nbnotnull > 0:
7555
+ wolfogl.addme_uint8(self.array, self.rgb, ox, oy, dx, dy, jstart, jend, istart, iend, cursize,
7556
+ self.nullvalue, np.uint8(self.alpha*255))
7557
+ elif self.nbnotnull > 0:
7558
+ wolfogl.addmeall_uint8(self.array, self.rgb, ox, oy, dx, dy, jstart, jend, istart, iend, cursize,
7559
+ self.nullvalue, np.uint8(self.alpha*255))
7560
+ elif VERSION_RGB == 2:
7561
+ if self.wolftype == WOLF_ARRAY_FULL_INTEGER8:
7562
+ if self.nbnotnull != self.nbx * self.nby:
7563
+ if self.nbnotnull > 0:
7564
+ wolfogl.addme_int8_pal(self.array, self.mypal.colorsflt, self.mypal.values, ox, oy, dx, dy, jstart,
7565
+ jend, istart, iend, cursize, self.nullvalue, self.alpha, int(self.mypal.interval_cst))
7566
+ elif self.nbnotnull > 0:
7567
+ wolfogl.addmeall_int8_pal(self.array, self.mypal.colorsflt, self.mypal.values, ox, oy, dx, dy, jstart,
7568
+ jend, istart, iend, cursize, self.nullvalue, self.alpha, int(self.mypal.interval_cst))
7569
+ elif self.wolftype in [WOLF_ARRAY_FULL_INTEGER16, WOLF_ARRAY_FULL_INTEGER16_2]:
7570
+ if self.nbnotnull != self.nbx * self.nby:
7571
+ if self.nbnotnull > 0:
7572
+ wolfogl.addme_int16_pal(self.array, self.mypal.colorsflt, self.mypal.values, ox, oy, dx, dy, jstart,
7573
+ jend, istart, iend, cursize, self.nullvalue, self.alpha, int(self.mypal.interval_cst))
7574
+ elif self.nbnotnull > 0:
7575
+ wolfogl.addmeall_int16_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.wolftype == WOLF_ARRAY_FULL_INTEGER:
7578
+ if self.nbnotnull != self.nbx * self.nby:
7579
+ if self.nbnotnull > 0:
7580
+ wolfogl.addme_int_pal(self.array, self.mypal.colorsflt, self.mypal.values, ox, oy, dx, dy, jstart,
7581
+ jend, istart, iend, cursize, self.nullvalue, self.alpha, int(self.mypal.interval_cst))
7582
+ elif self.nbnotnull > 0:
7583
+ wolfogl.addmeall_int_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.wolftype == WOLF_ARRAY_FULL_DOUBLE:
7586
+ if self.nbnotnull != self.nbx * self.nby:
7587
+ if self.nbnotnull > 0:
7588
+ wolfogl.addme_double_pal(self.array, self.mypal.colorsflt, self.mypal.values, ox, oy, dx, dy, jstart,
7589
+ jend, istart, iend, cursize, self.nullvalue, self.alpha, int(self.mypal.interval_cst))
7590
+ elif self.nbnotnull > 0:
7591
+ wolfogl.addmeall_double_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.wolftype != WOLF_ARRAY_FULL_SINGLE:
7594
+ if self.nbnotnull != self.nbx * self.nby:
7595
+ if self.nbnotnull > 0:
7596
+ wolfogl.addme_pal(self._tmp_float32, self.mypal.colorsflt, self.mypal.values, ox, oy, dx, dy, jstart,
7597
+ jend, istart, iend, cursize, self.nullvalue, self.alpha, int(self.mypal.interval_cst))
7598
+ elif self.nbnotnull > 0:
7599
+ wolfogl.addmeall_pal(self._tmp_float32, 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
+ else:
7602
+ if self.nbnotnull != self.nbx * self.nby:
7603
+ if self.nbnotnull > 0:
7604
+ wolfogl.addme_pal(self.array, self.mypal.colorsflt, self.mypal.values, ox, oy, dx, dy, jstart, jend, istart, iend, cursize,
7605
+ self.nullvalue, self.alpha, int(self.mypal.interval_cst))
7606
+ elif self.nbnotnull > 0:
7607
+ wolfogl.addmeall_pal(self.array, self.mypal.colorsflt, self.mypal.values, ox, oy, dx, dy, jstart, jend, istart, iend, cursize,
7608
+ self.nullvalue, self.alpha, int(self.mypal.interval_cst))
7609
+
7610
+ except Exception as e:
7611
+ logging.error(repr(e))
7612
+ raise NameError(_('OpenGL error in WolfArray.fillonecellgrid -- Please report this case with the data file and the context in which the error occured'))
7613
+
7371
7614
  pass
7372
7615
  glEndList()
7373
- except:
7616
+ except Exception as e:
7617
+ logging.error(repr(e))
7374
7618
  raise NameError(
7375
7619
  'Opengl in WolfArray_fillonecellgrid -- maybe a conflict with an existing opengl32.dll file - please rename the opengl32.dll in the libs directory and retry')
7376
7620
 
@@ -7884,8 +8128,11 @@ class WolfArrayMB(WolfArray):
7884
8128
  self.read_data()
7885
8129
  if self.masknull:
7886
8130
  self.mask_data(self.nullvalue)
7887
- if self.rgb is None:
7888
- self.rgb = np.ones((self.nbx, self.nby, 4), order='F', dtype=np.integer)
8131
+
8132
+ if VERSION_RGB==1 :
8133
+ if self.rgb is None:
8134
+ self.rgb = np.ones((self.nbx, self.nby, 4), order='F', dtype=np.integer)
8135
+
7889
8136
  self.updatepalette(0)
7890
8137
  self.loaded = True
7891
8138
  else:
@@ -7911,7 +8158,7 @@ class WolfArrayMB(WolfArray):
7911
8158
 
7912
8159
  for curblock in self.myblocks.values():
7913
8160
  curblock.uncheck_plot(unload, forceresetOGL, askquestion=False)
7914
- self.rgb = None
8161
+ if VERSION_RGB==1 : self.rgb = None
7915
8162
 
7916
8163
  self.myblocks = {}
7917
8164
  self.loaded = False
@@ -8156,8 +8403,10 @@ class WolfArrayMB(WolfArray):
8156
8403
  self.mypal.isopop(allarrays, self.nbnotnull)
8157
8404
 
8158
8405
  self.link_palette()
8159
- for curblock in self.myblocks.values():
8160
- curblock.rgb = self.mypal.get_rgba(curblock.array)
8406
+
8407
+ if VERSION_RGB ==1:
8408
+ for curblock in self.myblocks.values():
8409
+ curblock.rgb = self.mypal.get_rgba(curblock.array)
8161
8410
 
8162
8411
  if self.myops is not None:
8163
8412
  self.myops.update_palette()