wolfhece 2.1.25__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,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
 
@@ -4689,6 +4798,54 @@ class WolfArray(Element_To_Draw, header_wolf):
4689
4798
  band.FlushCache()
4690
4799
  band.ComputeStatistics(True)
4691
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
+
4692
4849
  def import_geotif(self, fn:str='', which:int = None, crop:list[float]=None):
4693
4850
  """
4694
4851
  Import de la matrice au format Geotiff
@@ -4712,65 +4869,69 @@ class WolfArray(Element_To_Draw, header_wolf):
4712
4869
  return
4713
4870
 
4714
4871
  if crop is not None :
4715
- if os.path.exists(fn):
4872
+ if not os.path.exists(fn):
4873
+ logging.error(_('File not found'))
4874
+ return
4716
4875
 
4717
- tmpdx = self.dx
4876
+ tmpdx = self.dx
4718
4877
 
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
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
4887
+
4888
+ raster:gdal.Dataset
4889
+ raster = gdal.Open(fn)
4890
+ geotr = raster.GetGeoTransform()
4891
+ self.dx = geotr[1]
4892
+ self.dy = abs(geotr[5])
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])
4734
4894
 
4895
+ newcrop = CropDialog(None)
4735
4896
 
4736
- newcrop = CropDialog(None)
4897
+ if self.wx_exists:
4898
+ bounds = self.mapviewer.get_canvas_bounds()
4737
4899
 
4738
- if self.wx_exists:
4739
- bounds = self.mapviewer.get_canvas_bounds()
4900
+ newcrop.dx.Value = str(self.dx)
4901
+ newcrop.dy.Value = str(self.dy)
4740
4902
 
4741
- newcrop.dx.Value = str(self.dx)
4742
- newcrop.dy.Value = str(self.dy)
4903
+ newcrop.dx.Enable(False)
4904
+ newcrop.dy.Enable(False)
4743
4905
 
4744
- newcrop.dx.Enable(False)
4745
- newcrop.dy.Enable(False)
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.))
4746
4910
 
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.))
4911
+ badvalues = True
4912
+ while badvalues:
4913
+ badvalues = False
4751
4914
 
4752
- badvalues = True
4753
- while badvalues:
4754
- badvalues = False
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)]
4755
4922
 
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)]
4923
+ tmpdx = float(newcrop.dx.Value)
4924
+ tmpdy = float(newcrop.dy.Value)
4763
4925
 
4764
- tmpdx = float(newcrop.dx.Value)
4765
- 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
4766
4929
 
4767
- if self.dx != tmpdx or self.dy != tmpdy:
4768
- if tmpdx / self.dx != tmpdy / self.dy:
4769
- badvalues = True
4930
+ newcrop.Destroy()
4770
4931
 
4771
- newcrop.Destroy()
4932
+ xmin, xmax, ymin, ymax = crop
4772
4933
 
4773
- xmin, xmax, ymin, ymax = crop
4934
+ if self.wx_exists:
4774
4935
 
4775
4936
  with wx.FileDialog(None, _('Save the cropped file for later'), wildcard="Tiff files (*.tif)|*.tif",
4776
4937
  style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as fileDialog:
@@ -4783,6 +4944,11 @@ class WolfArray(Element_To_Draw, header_wolf):
4783
4944
 
4784
4945
  gdal.Translate(fn_crop, fn, projWin=[xmin, ymax, xmax, ymin])
4785
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)
4786
4952
 
4787
4953
  raster:gdal.Dataset
4788
4954
  raster = gdal.Open(fn)
@@ -4864,8 +5030,6 @@ class WolfArray(Element_To_Draw, header_wolf):
4864
5030
  elif self.array.dtype == np.int8:
4865
5031
  self.wolftype = WOLF_ARRAY_FULL_INTEGER8
4866
5032
 
4867
- self.mask_data(self.nullvalue)
4868
-
4869
5033
  except:
4870
5034
  logging.warning(_('Error during importing tif file'))
4871
5035
 
@@ -5860,8 +6024,9 @@ class WolfArray(Element_To_Draw, header_wolf):
5860
6024
 
5861
6025
  self.loaded = True
5862
6026
 
5863
- if self.rgb is None:
5864
- self.updatepalette(0)
6027
+ if VERSION_RGB==1 :
6028
+ if self.rgb is None:
6029
+ self.updatepalette(0)
5865
6030
 
5866
6031
  def uncheck_plot(self, unload:bool=True, forceresetOGL:bool=False, askquestion:bool=True):
5867
6032
  """
@@ -5886,7 +6051,7 @@ class WolfArray(Element_To_Draw, header_wolf):
5886
6051
  if unload:
5887
6052
  self.delete_lists()
5888
6053
  self.array = np.zeros([1])
5889
- self.rgb = None
6054
+ if VERSION_RGB==1 : self.rgb = None
5890
6055
  self.loaded = False
5891
6056
  return
5892
6057
 
@@ -5896,10 +6061,10 @@ class WolfArray(Element_To_Draw, header_wolf):
5896
6061
  ret = dlg.ShowModal()
5897
6062
  if ret == wx.ID_YES:
5898
6063
  self.delete_lists()
5899
- self.rgb = None
6064
+ if VERSION_RGB==1 : self.rgb = None
5900
6065
  else:
5901
6066
  self.delete_lists()
5902
- self.rgb = None
6067
+ if VERSION_RGB==1 : self.rgb = None
5903
6068
 
5904
6069
  def get_header(self, abs:bool=True) -> header_wolf:
5905
6070
  """
@@ -5964,7 +6129,7 @@ class WolfArray(Element_To_Draw, header_wolf):
5964
6129
 
5965
6130
  def __add__(self, other):
5966
6131
  """Surcharge de l'opérateur d'addition"""
5967
- newArray = WolfArray()
6132
+ newArray = WolfArray(whichtype=self.wolftype)
5968
6133
  newArray.nbx = self.nbx
5969
6134
  newArray.nby = self.nby
5970
6135
  newArray.dx = self.dx
@@ -5985,11 +6150,12 @@ class WolfArray(Element_To_Draw, header_wolf):
5985
6150
  newArray.array = np.ma.masked_array(self.array + other, self.array.mask)
5986
6151
  else:
5987
6152
  newArray.array = np.ma.masked_array(self.array + other.array, self.array.mask)
6153
+ newArray.count()
5988
6154
  return newArray
5989
6155
 
5990
6156
  def __mul__(self, other):
5991
6157
  """Surcharge de l'opérateur d'addition"""
5992
- newArray = WolfArray()
6158
+ newArray = WolfArray(whichtype=self.wolftype)
5993
6159
  newArray.nbx = self.nbx
5994
6160
  newArray.nby = self.nby
5995
6161
  newArray.dx = self.dx
@@ -6010,11 +6176,12 @@ class WolfArray(Element_To_Draw, header_wolf):
6010
6176
  newArray.array = np.ma.masked_array(self.array * other, self.array.mask)
6011
6177
  else:
6012
6178
  newArray.array = np.ma.masked_array(self.array * other.array, self.array.mask)
6179
+ newArray.count()
6013
6180
  return newArray
6014
6181
 
6015
6182
  def __sub__(self, other):
6016
6183
  """Surcharge de l'opérateur de soustraction"""
6017
- newArray = WolfArray()
6184
+ newArray = WolfArray(whichtype=self.wolftype)
6018
6185
  newArray.nbx = self.nbx
6019
6186
  newArray.nby = self.nby
6020
6187
  newArray.dx = self.dx
@@ -6035,11 +6202,12 @@ class WolfArray(Element_To_Draw, header_wolf):
6035
6202
  newArray.array = np.ma.masked_array(self.array - other, self.array.mask)
6036
6203
  else:
6037
6204
  newArray.array = np.ma.masked_array(self.array - other.array, self.array.mask)
6205
+ newArray.count()
6038
6206
  return newArray
6039
6207
 
6040
6208
  def __pow__(self, other):
6041
6209
  """Surcharge de l'opérateur puissance"""
6042
- newArray = WolfArray()
6210
+ newArray = WolfArray(whichtype=self.wolftype)
6043
6211
  newArray.nbx = self.nbx
6044
6212
  newArray.nby = self.nby
6045
6213
  newArray.dx = self.dx
@@ -6056,11 +6224,12 @@ class WolfArray(Element_To_Draw, header_wolf):
6056
6224
  newArray.translz = self.translz
6057
6225
 
6058
6226
  newArray.array = np.ma.masked_array(self.array ** other, self.array.mask)
6227
+ newArray.count()
6059
6228
  return newArray
6060
6229
 
6061
6230
  def __truediv__(self, other):
6062
6231
  """Surcharge de l'opérateur division"""
6063
- newArray = WolfArray()
6232
+ newArray = WolfArray(whichtype=self.wolftype)
6064
6233
  newArray.nbx = self.nbx
6065
6234
  newArray.nby = self.nby
6066
6235
  newArray.dx = self.dx
@@ -6081,7 +6250,7 @@ class WolfArray(Element_To_Draw, header_wolf):
6081
6250
  newArray.array = np.ma.masked_array(self.array / other, self.array.mask)
6082
6251
  else:
6083
6252
  newArray.array = np.ma.masked_array(np.where(other == 0., 0., self.array / other.array), self.array.mask)
6084
-
6253
+ newArray.count()
6085
6254
  return newArray
6086
6255
 
6087
6256
  def concatenate(self, list_arr:list["WolfArray"], nullvalue:float = 0.):
@@ -6363,70 +6532,48 @@ class WolfArray(Element_To_Draw, header_wolf):
6363
6532
  def read_all(self, which_band = None):
6364
6533
  """ Lecture d'un Wolf aray depuis le nom de fichier """
6365
6534
 
6535
+ THRESHOLD = 20_000_000
6536
+
6366
6537
  if not os.path.exists(self.filename):
6367
6538
  if self.wx_exists:
6368
6539
  logging.warning(_('No data file : ')+self.filename)
6369
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
6370
6552
 
6371
6553
  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
6554
+ self.read_txt_header()
6381
6555
 
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
6556
+ if self.preload:
6557
+
6558
+ update_min_max = check_threshold(self.nbx, self.nby, THRESHOLD)
6397
6559
 
6398
- tmp_wolftype = self.wolftype
6560
+ self.import_geotif(which= which_band, crop = self.cropini)
6561
+ self.loaded = True
6399
6562
 
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)
6563
+ if update_min_max:
6564
+ self.mypal.distribute_values(self.array.min(), self.array.max())
6406
6565
 
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"])
6566
+ elif self.filename.endswith('.npy'):
6567
+ self.read_txt_header()
6416
6568
 
6417
- self.nullvalue = 99999.
6418
- elif (locpath.parent / (locpath.name + '.txt')).exists():
6419
- self.read_txt_header()
6569
+ if self.preload:
6570
+ update_min_max = check_threshold(self.nbx, self.nby, THRESHOLD)
6420
6571
 
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
6572
+ self._import_npy(crop = self.cropini)
6573
+ self.loaded = True
6427
6574
 
6428
- self.mask_data(self.nullvalue)
6429
- self.loaded = True
6575
+ if update_min_max:
6576
+ self.mypal.distribute_values(self.array.min(), self.array.max())
6430
6577
 
6431
6578
  else:
6432
6579
  self.read_txt_header()
@@ -6436,9 +6583,13 @@ class WolfArray(Element_To_Draw, header_wolf):
6436
6583
  self.myblocks = {}
6437
6584
 
6438
6585
  if self.preload:
6586
+ update_min_max = check_threshold(self.nbx, self.nby, THRESHOLD)
6587
+
6439
6588
  self.read_data()
6440
6589
  self.loaded = True
6441
- return
6590
+
6591
+ if update_min_max:
6592
+ self.mypal.distribute_values(self.array.min(), self.array.max())
6442
6593
 
6443
6594
  def write_all(self, newpath:str = None):
6444
6595
  """
@@ -6802,8 +6953,17 @@ class WolfArray(Element_To_Draw, header_wolf):
6802
6953
  if self.array is None:
6803
6954
  return
6804
6955
 
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)
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'))
6807
6967
 
6808
6968
  if value is not None:
6809
6969
 
@@ -7149,29 +7309,44 @@ class WolfArray(Element_To_Draw, header_wolf):
7149
7309
  if self.array is None:
7150
7310
  return
7151
7311
 
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
7312
  if self.mypal.automatic:
7159
7313
  if onzoom != []:
7160
7314
  self.mypal.isopop(self.get_working_array(onzoom), self.nbnotnullzoom)
7161
7315
  else:
7162
7316
  self.mypal.isopop(self.get_working_array(), self.nbnotnull)
7163
7317
 
7164
- 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
+
7165
7340
 
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]
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]
7173
7348
 
7174
- self.rgb[self.array.mask] = [1., 1., 1., 0.]
7349
+ if VERSION_RGB==1 : self.rgb[self.array.mask] = [1., 1., 1., 0.]
7175
7350
 
7176
7351
  if self.myops is not None:
7177
7352
  # update the wx
@@ -7333,6 +7508,8 @@ class WolfArray(Element_To_Draw, header_wolf):
7333
7508
  def fillonecellgrid(self, curscale, loci, locj, force=False):
7334
7509
  """ Fill one cell of the plotted grid """
7335
7510
 
7511
+
7512
+
7336
7513
  cursize = curscale # 2**curscale
7337
7514
 
7338
7515
  if not cursize in self.mygrid.keys():
@@ -7363,26 +7540,81 @@ class WolfArray(Element_To_Draw, header_wolf):
7363
7540
  iend = min(istart + step, self.nbx)
7364
7541
 
7365
7542
  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:
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
+
7383
7614
  pass
7384
7615
  glEndList()
7385
- except:
7616
+ except Exception as e:
7617
+ logging.error(repr(e))
7386
7618
  raise NameError(
7387
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')
7388
7620
 
@@ -7896,8 +8128,11 @@ class WolfArrayMB(WolfArray):
7896
8128
  self.read_data()
7897
8129
  if self.masknull:
7898
8130
  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)
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
+
7901
8136
  self.updatepalette(0)
7902
8137
  self.loaded = True
7903
8138
  else:
@@ -7923,7 +8158,7 @@ class WolfArrayMB(WolfArray):
7923
8158
 
7924
8159
  for curblock in self.myblocks.values():
7925
8160
  curblock.uncheck_plot(unload, forceresetOGL, askquestion=False)
7926
- self.rgb = None
8161
+ if VERSION_RGB==1 : self.rgb = None
7927
8162
 
7928
8163
  self.myblocks = {}
7929
8164
  self.loaded = False
@@ -8168,8 +8403,10 @@ class WolfArrayMB(WolfArray):
8168
8403
  self.mypal.isopop(allarrays, self.nbnotnull)
8169
8404
 
8170
8405
  self.link_palette()
8171
- for curblock in self.myblocks.values():
8172
- 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)
8173
8410
 
8174
8411
  if self.myops is not None:
8175
8412
  self.myops.update_palette()